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
ldx #initval ldy #<(zp_pos+1) loop: shx $ffff,y lda <($100 + zp_pos - initval),x beq loop
The STA $ZP instruction (see post #44) can be made part of the init code, which reduces the timer-based stabilization approach to effectively 10 bytes: ldy #init_value ;Init code sync: lax $dc04 sbx #51 sty ZP ;RRW instruction. Part of init code. cpx $dc04 bne sync: STY ABS is also allowed, in combination with SBX #52. If I'm not mistaken, this should work on PAL, NTSC, and DREAN, but the loop exit cycle may depend on the system.
ldy #init_value ;Init code sync: lax $dc04 sbx #51 sty ZP ;RRW instruction. Part of init code. cpx $dc04 bne sync:
CJam's approach also takes 10 bytes, but maybe it requires blanking the screen? I'm not sure, but if it does, it is an extra constraint, since you do not always want that. For example, in a demo you can have some effect working on the startup screen, and then you don't want to blank it.
VIC always steals the same amount of cycles from the CPU on a badline. This is system independent.
Woah, this thread has been busy, nice work all :) Um, no need to blank the screen for my bracketing method. There's never character DMA on line $0ff, which is the only one for which there are 63 cycles for which an INC $d012 will read $ff and write $00. (assuming of course that I'm remembering correctly rumours that line $00 is only 62 cycles long - can anyone point me at documentation to confirm that? There's nothing in the venerable VIC Article [english], and I'm not spotting it anywhere on CodeBase either)