How to create delay in asm for MOS 6502

I am new to ASM and I am trying to train how to create a delay for the following code:

org $1000

loop: inc $d021
    jmp loop
+3
source share
4 answers

The comments are clear enough, I think.

Sample code to change the color of each frame (1/50 second)

        sei       ; enable interrupts

loop1:  lda #$fb  ; wait for vertical retrace
loop2:  cmp $d012 ; until it reaches 251th raster line ($fb)
        bne loop2 ; which is out of the inner screen area

        inc $d021 ; increase background color

        lda $d012 ; make sure we reached
loop3:  cmp $d012 ; the next raster line so next time we
        beq loop3 ; should catch the same line next frame

        jmp loop1 ; jump to main loop

Sample code to change color per second

counter = $fa ; a zeropage address to be used as a counter

        lda #$00    ; reset
        sta counter ; counter

        sei       ; enable interrupts

loop1:  lda #$fb  ; wait for vertical retrace
loop2:  cmp $d012 ; until it reaches 251th raster line ($fb)
        bne loop2 ; which is out of the inner screen area

        inc counter ; increase frame counter
        lda counter ; check if counter
        cmp #$32    ; reached 50
        bne out     ; if not, pass the color changing routine

        lda #$00    ; reset
        sta counter ; counter

        inc $d021 ; increase background color
out:
        lda $d012 ; make sure we reached
loop3:  cmp $d012 ; the next raster line so next time we
        beq loop3 ; should catch the same line next frame

        jmp loop1 ; jump to main loop
+6
source

eg:

loop: ldx $d021
      inx
      stx $d021
      cpx #100
      bne loop
0
source

? , 4 , . .

. : 0 255.

NTSC, 60 50 PAL.

main:
    inc $D021

    ldx #4          //  Wait 4 seconds
loop1:
    ldy #60
loop2:

waitvb:
    bit $D011
    bpl waitvb
waitvb2:
    bit $D011
    bmi waitvb2

    dey
    bne loop2
    dex
    bne loop1

    jmp main
0

, , , - , , :

TableStart:
    cmp #$C9
    cmp #$C9
    cmp #$C9
    cmp #$C9
    cmp #$C9
    ...
TableEnd:
    nop

If the jump vector points to tableEnd, the code will reach the instruction after the NOP after seven cycles. If it indicates one byte before, eight cycles. Two bytes before, nine loops, etc. Adjusting the transition vector may take some time, but the delay itself will be continuously adjustable from seven cycles to any higher value in increments of one cycle. The flags will be broken, but there will be no registers.

0
source

All Articles