title: Stable raster on C64
body: <p>Using 6510 assembly on the Commodore 64, I am trying to create a stable raster effect. Using the double IRQ principle I draw some raster lines on the screen. I pad with NOPs to match 63 cycles for every normal scanline, and to 23 cycles for every badline. I realise that there is a specific start line I need to set, in order to match my 8th iteration with a badline, but no matter on what line I put the first line or what combination of NOPs I use, I can't get the timing right. I want complete lines that are not "broken". Can anyone see what I am doing wrong? Code is in Kick Assembler format. And here is a screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/3gNyG.png" alt="Screenshot"></p>
  
  <pre><code>.pc = $0801 "Basic upstart"
  :BasicUpstart($8000)
  
  .pc = $8000 "Program"
  
    jsr $ff81
  
    sei
    lda #$35
    sta $01
  
    jsr setupInterrupts
    cli
  
    jmp *
  
  setupInterrupts:
    lda #&lt;int1
    ldy #&gt;int1
    sta $fffe
    sty $ffff
  
    lda #$01
    sta $d01a
    lda #$7f
    sta $dc0d
    sta $dd0d
    lda $dc0d  
    lda $dd0d
    lda #$1b
    sta $d011
    lda #$01
    sta $d019
  
    lda start
    sta $d012
  
    rts
  
  start:
    .byte 56
  
  int1:
    pha txa pha tya pha
  
    :STABILIZE()
  
  .for (var i=0; i&lt;7; i++) {
    inc $d020   // 6 cycles
    inc $d021   // 6 cycles
    nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop // 24*2=48 cycles
    bit $ea     // 3 cycles
                // = 63 cycles
  }
    inc $d020   // 6 cycles
    inc $d021   // 6 cycles
    nop nop nop nop // 4*2=8 cycles
    bit $ea     // 3 cycles
                // = 23 cycles (badline)
  
    lda #$00
    sta $d020
    sta $d021
  
    lda start
    sta $d012
  
    lda #&lt;int1 
    ldy #&gt;int1 
    sta $fffe
    sty $ffff
  
    lda #$01
    sta $d019
  
    pla tay pla tax pla
  
    rti
  
  
  .macro STABILIZE() {
  
    lda #&lt;nextRasterLineIRQ
    sta $fffe
    lda #&gt;nextRasterLineIRQ
    sta $ffff   
  
    inc $d012
  
    lda #$01
    sta $d019
  
    tsx
  
    cli
  
    nop nop nop nop nop nop nop nop
  
  nextRasterLineIRQ:
    txs
  
    ldx #$08
    dex
    bne *-1
    bit $00
  
    lda $d012
    cmp $d012
  
    beq *+2      
  }
  </code></pre>