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

Registered: Apr 2002
Posts: 1522
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?
 
... 64 posts hidden. Click here to view all posts....
 
2020-07-04 09:48
Rastah Bar

Registered: Oct 2012
Posts: 231
Quoting Copyfault

The SHX will be SHX $0fa0,y. Then you start this with a JMP $0fad which is just an LDY #$0f. This is also the reason for that code blob to begin at $0faa.

Excellent! Thanks for the explanation. I had the same problem as Perplex. The code you posted gives LDA $100F.

Quoting CopyFault
Yes, yes, it's so true! After I wrote this post, two things haunted me some hours later: 1. that branching to SHX is not possible when doing the SHX $0fa0,Y-trick, so it was confusing to start with it and writing that branch-idea after it
2. those 63 cycles do only apply for non-badlines, but your approach needs badlines badly (pun intended). So my calculation was wrong. Thanks for putting this right, with the corresponding cycle calculations included :)

461 is a prime number, so I don't understand the (a)cyclic graph explanation. Could someone explain it in a bit more depth, please?
2020-07-04 20:12
Copyfault

Registered: Dec 2001
Posts: 308
Quoting Rastah Bar
Quoting Copyfault

The SHX will be SHX $0fa0,y. Then you start this with a JMP $0fad which is just an LDY #$0f. This is also the reason for that code blob to begin at $0faa.

Excellent! Thanks for the explanation. I had the same problem as Perplex. The code you posted gives LDA $100F.
[...]

Ooops, this was ofcourse wrong in the post, but correct in my head. So, excuses to Perplex, Rastah Bar and all readers - though I think the optimization was obvious, more or less;)

So next task is to get his <7 bytes *ducks+runs*

CF
2020-07-04 21:39
Rastah Bar

Registered: Oct 2012
Posts: 231
Perhaps it might be possible in 6 bytes if done something like this:
*=$XX??     ;Suitable starting address we have to find.

loop:	tay
uoc:	shx cont-offset,y
cont:	bpl loop:

If cont-offset equals $XXA7, jumping to uoc+1 (like in Copyfault's modification) would execute LAX $XX first.
The problem is to find a suitable offset and starting address.

Note that the SHX instruction does not necessarily have to change the BPL instruction, but could change the byte after the BPL or perhaps even the SHX instruction itself.
This gives a bit more freedom to solve the problem. Perhaps the BPL could be a different branch instruction too.
2020-07-04 22:53
Quiss

Registered: Nov 2016
Posts: 6
Quoting Rastah Bar

461 is a prime number, so I don't understand the (a)cyclic graph explanation. Could someone explain it in a bit more depth, please?

I drew a diagram of the state transitions: https://photos.app.goo.gl/g6Ba3YzbzBe1GSq67
Maybe that clarifies things? Happy to elaborate further.
2020-07-05 10:13
Rastah Bar

Registered: Oct 2012
Posts: 231
Thanks! That makes it a lot clearer. So the possibibilty of having 462 cycles due to the W-cycle can result in a periodic loop that excludes the "golden" R-cycle that would end the loop.

I think though that the borders could make some of the other loop lengths work.

The border is 112*63 cycles = 7056 cycles.

So perhaps a loop, when it is stuck in a wrong loop on the visible screen, will be "shifted out" of that loop in the border.

This will happen for a loop length of 17, since 7056 = 415*17 + 1.
Loop lengths 11, 13, and 15 still don't seem to work.

(Btw, with longer loop lengths there may be more W-cycles in the loop.)
2020-07-05 20:39
Rastah Bar

Registered: Oct 2012
Posts: 231
Quoting Copyfault
So next task is to get his <7 bytes *ducks+runs*

Here is one example of a six bytes loop. ZP-address $39 contains the basic line number. So that one is free to choose, if we choose 17 decimal, then all registers will contain $11 after the SYS with the code:
17 SYS 14774 : REM $39B6

*=$39B4 (14772 decimal)
39B4 loop:  TAY
39B5        SHX $39A7,Y
39B8        BPL loop

The SYS 14774 jumps to LAX $39, which contains $11. Since $3A AND $11 equals $10, the SHX stores the BPL opcode at $39B8, except when the &{H+1} drops off due to DMA. Then it stores opcode $11, which is ORA (ZP),Y and the loop exits.

Surely there are other working examples, with perhaps more convenient addresses?
2020-07-05 22:23
Quiss

Registered: Nov 2016
Posts: 6
Quoting Rastah Bar

I think though that the borders could make some of the other loop lengths work.

The border is 112*63 cycles = 7056 cycles.

So perhaps a loop, when it is stuck in a wrong loop on the visible screen, will be "shifted out" of that loop in the border.

This will happen for a loop length of 17, since 7056 = 415*17 + 1.


Oh, good point! 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.

Quoting Rastah Bar

(Btw, with longer loop lengths there may be more W-cycles in the loop.)


Right. Any extra W cycles would make things more complicated. I guess they could both break loops and create loops.
2020-07-05 23:14
Rastah Bar

Registered: Oct 2012
Posts: 231
The six byte example as shown does not work since it could get interrupted. An SEI should be executed first before jumping into the loop.
2020-07-06 01:10
Copyfault

Registered: Dec 2001
Posts: 308
Quoting Rastah Bar
The six byte example as shown does not work since it could get interrupted. An SEI should be executed first before jumping into the loop.

Hmm, this is more or less the same for all examples we had before, or do I miss smth?? Ok, here a basic sys-line is part of the trick, but it could also jump to an init routine that does SEI or LDA #$7F:STA $DC0D:etc. first.


Another instability of the approach with having the operand bytes of the SHX actually doing something usefull (i.e. filling a register) is that y != 0 in all the examples we had until now. This can lead to the ORA ($f9),Y taking one cycle more, depending on the content of $f9/$fa.

So I vote for $14 instead of $11 as value for X. It'll change the BPL into a NOP zp,X which always takes 4 cycles.
2020-07-06 09:04
Rastah Bar

Registered: Oct 2012
Posts: 231
It can be fixed by putting an SEI in a 7 byte loop. Adding your improvement as well:
20 SYS 14777 : REM $39B9

*=$39B6 (14774 decimal)

39B6 loop:  SEI
39B7        TAY
39B8        SHX $39A7,Y
39BB        BPL loop

But since the basic SYS 14777 instruction occupies one more byte in memory than a SYS to $08xx, I suppose we have to count this as an eight bytes method.
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 - 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
Hydrogen/Glance
Brush/Elysium
Jammer/TooMany
Mason/Unicess
Sander
Guests online: 84
Top Demos
1 Uncensored  (9.7)
2 Edge of Disgrace  (9.6)
3 Coma Light 13  (9.6)
4 Memento Mori  (9.6)
5 Comaland 100%  (9.6)
6 The Shores of Reflec..  (9.6)
7 Unboxed  (9.6)
8 Lunatico  (9.6)
9 Remains  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Dawnfall V1.1  (9.5)
2 Gumbo Revised  (9.5)
3 Smile to the Sky  (9.5)
4 Daah, Those Acid Pil..  (9.5)
5 Bad Boy  (9.5)
6 Crystal Gazer  (9.5)
7 Cuarentenauta  (9.5)
8 Instinct  (9.5)
9 The Night It Snowed  (9.5)
10 Rewind  (9.4)
Top Groups
1 PriorArt  (9.4)
2 Booze Design  (9.4)
3 Censor Design  (9.4)
4 Fossil  (9.4)
5 Performers  (9.3)
Top Crackers
1 Mr. Z  (10)
2 S!R  (9.9)
3 Mr Zero Page  (9.8)
4 Mitch  (9.8)
5 1103  (9.8)

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