| |
Krill
Registered: Apr 2002 Posts: 1786 |
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? |
|
... 176 posts hidden. Click here to view all posts.... |
| |
Groepaz
Registered: Dec 2001 Posts: 9747 |
thats what the opcode does - why exactly it happens is unknown, but its likely some analog effect |
| |
Krill
Registered: Apr 2002 Posts: 1786 |
Yes, something to do with DMA interference* disturbing the inner workings of that opcode. And it's a non-intended (illegal) opcode anyways, so short-circuiting inner logic to begin with. :)
* It has been observed that other 6502-based platforms without DMA (1541, e.g.) do not exhibit the &H-dropoff behaviour. |
| |
Copyfault
Registered: Dec 2001 Posts: 382 |
Quoting Oswaldwhy does the &hi disappear on a badline? In my text "badline" was short for "the 1st DMA-overtake-cycle @cycle#12 in every badline".
If the 4th cycle of the SHX abs,Y coincides with a DMA-overtake cycle, the &(hi+1) drops off, see e.g. post#88 or the latest No More Secrets V0.95.
Ofcourse all sprites must be turned off while the routine is running (they'd throw in other DMA-overtake-cycles and break the whole thing). So the basic trick (courtesy of Quiss;)) is to uniquely mark badlines with the DMA-overtake-cycles and loop until the beforementioned 4th cycle of the SHX lands on the correct cycle (or short: "on a badline";)). |
| |
Devia
Registered: Oct 2004 Posts: 401 |
What an awful thread.. fell down the rabbit hole, turned utterly insane, lost several days of sleep...
Quiss' original approach is short and elegant, but opcode rewriting confuses my old brain.
Adding a single byte to it's size, the flexibility can be increased somewhat (from 64 to 112 possible pages) and no opcode rewrites, just operand rewrite.
* = $xy00, where x=$0-$f and y=$0-$6
loop: ldy #$08
ldx #$0a
shx loop,y
jmp loop
$xx0A:
Loop times kept at either 10 or 12 cycles, depending on address choice.
As an added bonus, A is not touched - which may or may not matter in your overall timer setup.
So, it's obvious my priority is not size, but readability - and I find this approach a tad more readable ;-) |
| |
Frantic
Registered: Mar 2003 Posts: 1486 |
Yes, this thread should have some kind of warning sign attached to it. :) |
| |
Copyfault
Registered: Dec 2001 Posts: 382 |
Quoting DeviaWhat an awful thread.. fell down the rabbit hole, turned utterly insane, lost several days of sleep... How can it be so awful when it guided you into Wonderland :) ??
Quoting DeviaQuiss' original approach is short and elegant, but opcode rewriting confuses my old brain.
Adding a single byte to it's size, the flexibility can be increased somewhat (from 64 to 112 possible pages) and no opcode rewrites, just operand rewrite.
* = $xy00, where x=$0-$f and y=$0-$6
loop: ldy #$08
ldx #$0a
shx loop,y
jmp loop
$xx0A:
Loop times kept at either 10 or 12 cycles, depending on address choice. Wow, loop times depending on the adress-highbyte. But yeah, a JMP-instruction is needed to change the basic structure from opcode-change to operand-change... (unless one wants to exit from the loop by jumping back, but let's not go that route)
Quoting DeviaAs an added bonus, A is not touched - which may or may not matter in your overall timer setup.
So, it's obvious my priority is not size, but readability - and I find this approach a tad more readable ;-) Hm, ok, I agree that readability was not the main focus most of the times... but at least some variants that leave A untouched were given already (basically those with a CPX#val).
Still, I think the sniplet presented in post#90 (and all variants thereof) is best regarding trade-off between readability and size. And it imposes almost no restriction on the highbyte that can be used;)
Thanks for your version; it just rang the "things that I wanted to do"-bell for me (writing it all up on codebase, that is;)). |
| |
Devia
Registered: Oct 2004 Posts: 401 |
Quoting CopyfaultStill, I think the sniplet presented in post#90 (and all variants thereof) is best regarding trade-off between readability and size. And it imposes almost no restriction on the highbyte that can be used;)
That's what I mean about the insanity..I do remember reading that post and the following ones, but apparently didn't recognize their brilliancy the first time around - that one is a keeper! ;-) |
| |
Oswald
Registered: Apr 2002 Posts: 4682 |
is there any requierement for the nr of cycles for the whole loop to make sure it will 'always' land on a different cycle of the shx opcode on the badline's 12th cycle? |
| |
Rastah Bar
Registered: Oct 2012 Posts: 290 |
Yes, see posts 61 and 72 (and 69 for an explanation):
"In the range 5-20, the lengths that do work are 5, 10, 12, 16, 18 and 19. But note that in particular, length 8 (a.k.a. branching directly to the SHX) does not."
"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." |
| |
Oswald
Registered: Apr 2002 Posts: 4682 |
thanks for everyone for the explanations, this shx method is really cool :) |
Previous - 1 | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 - Next |