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
sync: inc $d012 ; result will be zero on cycles 0-7 or 8 of raster line $ff bne sync ; (or very rarely, cycle 9..62) .res 27,$ea ; wait 62-8=54 cycles. Replace with 54 cycles of init code inc $d012 ; if result is nonzero then we are too late. bne sync ; carry on if we read on cycle 62, 62 cycles after cycle 0
sync: ldy #val line0x100_wait: lax $d012 bne line0x100_wait /* // 56 cycles of init code go here */ lax $d012 // the R-cycle occurs exactly after 62 cycles of the upper R-cycle of the lax $d012 bne sync // this gives 0 if and only if the upper $d012-read was exactly @cycle=0 of the rasterline // as rasterline 0 is only 62 cycles long, this will only be // the case if the upper $d012-read was @cyc=0 of line=$100
lda #$08 sta zp_val ldx #$fe loop: wait_startline: cpx $d012 bne wait_startline inx bmi wait_startline //at cycle 6..12 of line $ff ldy zp_val waste_cycles: dey bpl waste_cycles cpx $d012 bne loop //leaves at cycle 2 of the first line in which raster is stable ($100..$106) done: