| |
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.... |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
There's still some more to squeeze out of the "0-byte-approach", though one may argue that it becomes a 1-byte-routine this way...
Instead of changing the byte following the SHA (vec),y, we could change the low-byte of the BRK-vector. By doing so, the mem constraint of the routine (best fit was the $ffxx-page) can be lowered a bit:
*= page*$100 - 1
sta $d093,y
brk
Now init the BRK-vector with $00/#page, put #($fe-y)/$ff at $d0/$d1 and choose values for accu and x s.t. x&a=1 and the routine will still work (also widens the choice for a and x somewhat). |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
Oh nice. There's always a use for a value pre-initialised to zero too, so I'm sure that would not be wasted. |
| |
Rastah Bar Account closed
Registered: Oct 2012 Posts: 336 |
The choice of A&X can be much wider than s.t. A&X=1.
A&X=3 would also work to continue with the code after the BRK instruction. Or, if you store some useful data such as a small table after the BRK, it can be skipped with a larger value of A&X. Ideally the BRK instruction would serve as the first element of a table :-) |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
Yes, you're both right, the BRK can be used as some data and the step from the beginning of the page to the adress where to continue can be chosen almost arbitrarily... but it imposes more (and weirder!!) restrictions on the choices for a and x;)
The example with A&X=1 was on intention, 'cause this way the $d0 turns into a BNE to the next opcode, and since A&X!=0, it's quite save to assume that the zero-flag is not set when the loop starts;)) |
| |
Rastah Bar Account closed
Registered: Oct 2012 Posts: 336 |
Quoting CopyfaultYes, you're both right, the BRK can be used as some data and the step from the beginning of the page to the adress where to continue can be chosen almost arbitrarily... but it imposes more (and weirder!!) restrictions on the choices for a and x;)
Not more restrictions, you just have more choice. |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
Quoting Rastah BarQuoting CopyfaultYes, you're both right, the BRK can be used as some data and the step from the beginning of the page to the adress where to continue can be chosen almost arbitrarily... but it imposes more (and weirder!!) restrictions on the choices for a and x;)
Not more restrictions, you just have more choice. Hmm, don't know, but the more bits are fixed by the AND-conditiion, the fewer variants for the pair (a,x) are permitted, no? |
| |
Rastah Bar Account closed
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. |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
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. |
| |
Krill
Registered: Apr 2002 Posts: 2980 |
Quoting CopyfaultQuoting QuissNeat! 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. =) |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
Quote: Quoting CopyfaultQuoting QuissNeat! 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. |
Previous - 1 | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 - Next |