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?
2020-01-20 16:26
Frantic

Registered: Mar 2003
Posts: 1648
Looks like great material for a Codebase 64 article. :) Neat code!
2020-01-20 17:03
Krill

Registered: Apr 2002
Posts: 2980
Thanks! =) But i'm not quite satisfied with this approach yet.

I've just realised that the overall number of cycles in a frame is still 312 * 63 (otherwise the timer would drift, which it doesn't). There might be something with testing $d012 on two or more lines. Possibly with more than 14 bytes of code, though.
2020-01-20 17:49
Krill

Registered: Apr 2002
Posts: 2980
13 bytes, still long wait. =)
initstabilise   ldx #11
                lda $d012
-               dex              ;   (11 * 5) + 4
                bpl -            ; 59
                eor $d012        ; 4 = 63
                bne initstabilise; 9 = 72
2020-01-20 18:44
Frantic

Registered: Mar 2003
Posts: 1648
In an actual 4K context, I guess this should be pretty minimal, since I guess you would remove the x register delay counting stuff and replace it with some other unrelated init code that you would have to execute anyway (and that wouldn't break things even if it was executed a number of times).
2020-01-20 21:18
Oswald

Registered: Apr 2002
Posts: 5094
"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."

basicly its not 0 jitter, just then you know how the raster and the cpu is synchronized :P cool idea! dont think anyone ever coded this for size, guess you could make it run faster if you would do this on the border on consecutive lines. bne would jump out from a loop.
2020-01-20 21:22
Krill

Registered: Apr 2002
Posts: 2980
Frantic: True that. =)

But i wasn't so happy with the possible long wait of about one second.

Here's a fast version with 15 bytes:
initstabilise   ldx #10
                lda $d012
                lsr              ; 2
                rol              ; 2
-               dex              ;   (10 * 5) + 4
                bpl -            ; 54
                cmp $d012        ; 4 = 62
                bne initstabilise; 9 = 71
This only considers even-numbered raster lines, so the problematic last line 311 won't terminate the loop.

Note that when replacing the X register delay stuff with some code of the same cycle count, the loop must still have 71 cycles, that is, a number of cycles that is co-prime with 63. Otherwise, not all cycles in a raster line may be reached and the loop spin endlessly.
2020-01-20 21:26
Krill

Registered: Apr 2002
Posts: 2980
Quoting Oswald
basicly its not 0 jitter, just then you know how the raster and the cpu is synchronized :P
I meant that the loop always comes out at the same cycle on a raster line, with no jitter. =)

Quoting Oswald
guess you could make it run faster if you would do this on the border on consecutive lines. bne would jump out from a loop.
One must avoid the 64-cycles line, because this would in fact produce a 1-cycle jitter with a 63-cycles check. But i think i've got this, now. :D
2020-01-20 21:33
Krill

Registered: Apr 2002
Posts: 2980
Ah, bummer. This is the correct one with even-numbered lines only. =)
initstabilise   ldx #10
                lda $d012
                lsr              ; 2
                asl              ; 2
-               dex              ;   (10 * 5) + 4
                bpl -            ; 54
                cmp $d012        ; 4 = 62
                bne initstabilise; 9 = 71
2020-01-20 21:50
ChristopherJam

Registered: Aug 2004
Posts: 1409
13 bytes, at most a frame.

But, only works 99.9% of the time (fails to trigger DMA if it starts during line $ee), and puts about 3 and a half lines of black at the bottom of the screen for a frame.

    ldx#$ee
:   cpx $d012
    bne :-
:   dex
    bmi :-
    stx $d011
2020-01-20 22:58
Krill

Registered: Apr 2002
Posts: 2980
Hah, that's pretty dirty. :)

I briefly considered DMA-based methods, but yeah, they usually come with visual artefacts or VSP hazards and the like.

One could argue that
- nop
  lda $d012
  lsr
  asl
  [54 cycles worth of user code not touching the accu]
  cmp $d012
  bne -
with 11 bytes net size as proposed by Frantic is shorter, though. =D
 
... 177 posts hidden. Click here to view all posts....
 
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
Linus/MSL
Krill/Plush
zscs
Derision/Longshot
Fred/Channel 4
X-Raffi/X-Rated
REBEL 1/HF
Paul Bearer
Guests online: 79
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 Party Elk 2  (9.6)
4 Cubic Dream  (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 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.06 sec.