我是ASM的新手,尝试理解以下代码如何创建延迟:
org $1000
loop: inc $d021
jmp loop
评论还算清晰明了。
每帧更改颜色的代码示例(每秒50次)
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
每秒改变颜色的代码示例
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
这样怎么样?这应该会改变背景,等待4秒钟,然后再次更改。无限循环。
请注意,您可以将秒数更改为从0到255的任何值。
这是针对NTSC
机器的,但您可以将60
更改为PAL
的50
。
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
例如:
loop: ldx $d021
inx
stx $d021
cpx #100
bne loop
TableStart:
cmp #$C9
cmp #$C9
cmp #$C9
cmp #$C9
cmp #$C9
...
TableEnd:
nop
如果跳转向量指向tableEnd,代码将在NOP后的七个周期后到达指令。如果它指向一个字节之前,则需要八个周期。两个字节之前,需要九个周期,以此类推。设置跳转向量可能需要一些时间,但延迟本身将可以平稳地从七个周期调整到任何更高的值,每个周期增加一个。标志位将被清除,但不会影响任何寄存器。
$d012
的变化。 - Michael$d012
改变320*50次,或者使用光栅中断来计算帧数。 - Michael