.word $0801 ; *** Symbol table follows *** sstart = $8000 sstop = $9000 zINDEX = $22 zFREKZP = $FB vControl = $D011 vSprEnable = $D015 sVoc3Control = $D412 sVoc3AttDec = $D413 sVoc3SusRel = $D414 sVoc3Env = $D41C kCHROUT = $FFD2 ; *** text follows *** * = $0801 .byte $0b,$08,$01,$00,$9e,$32,$30,$36,$31,$00,$00,$00 jmp start .dsb $1000 - *, 0 *=$1000 start ldx #0 stx fails+1 stx $d020 stx $d021 inx stx $0286 LDX #text JSR print ; Start with R=1 (no delay possible for R=0). LDX #$01 step STX susrel TXA PHA ; Generate and run sampling code JSR disable JSR genrun JSR enable ; Clear timer. LDA #$00 STA timer STA timer+1 ; Count # of cycles spent before envelope step. LDA #sstart STA zINDEX+1 LDY #$00 nxtcyc LDA (zINDEX),Y CMP #$20 ; Look for envelope step BNE count INY BNE nxtcyc INC zINDEX+1 INC timer+1 JMP nxtcyc count TYA ; Add sampling delay of $7F93. CLC ADC #<$7F93 STA timer LDA #>$7F93 ADC timer+1 STA timer+1 ldx #13 ; l.green lda #<$8011 cmp timer lda #>$8011 sbc timer+1 beq pass ldx #10 ; l.red inc fails+1 pass stx $0286 ; Print timer in hex LDX #$01 nxtdig LDA timer,X PHA LSR LSR LSR LSR JSR hexout PLA AND #$0F JSR hexout DEX BPL nxtdig LDA #13 JSR kCHROUT PLA TAX INX CPX #$10 BNE step ldx #13 ; l.green ldy #0 fails lda #0 beq allpass ldx #10 ; l.red ldy #$ff allpass stx $d020 sty $d7ff jmp * text .byte $93 .asc "ENVELOPE DELAY BUG, # OF CYCLES:" .byte 13, 13, 0 timer .byte 0, 0 susrel .byte $00 ; Generate and run sampling code. genrun LDA #scode STA zINDEX+1 ; X holds code offset, Y holds sample offset LDX #$00 LDY #$00 JSR gencode ; no delay JSR sample LDX #$00 LDY #$01 JSR gencode ; lda $d3f0,x (one cycle delay) LDY #$00 LDA #$BD STA (zINDEX),Y INY LDA #$F0 STA (zINDEX),Y INY LDA #$D3 STA (zINDEX),Y ; Offset for lda,x LDX #$2C JSR sample LDX #$01 LDY #$02 JSR gencode ; nop (two cycle delay) LDY #$00 LDA #$EA STA (zINDEX),Y JSR sample LDX #$02 LDY #$03 JSR gencode ; bit $ff (three cycle delay) LDY #$00 LDA #$24 STA (zINDEX),Y INY LDA #$FF STA (zINDEX),Y JSR sample LDX #$02 LDY #$04 JSR gencode ; nop nop (four cycle delay) LDY #$00 LDA #$EA STA (zINDEX),Y INY LDA #$EA STA (zINDEX),Y JSR sample LDX #$02 LDY #$05 JSR gencode ; asl $ff (five cycle delay) LDY #$00 LDA #$06 STA (zINDEX),Y INY LDA #$FF STA (zINDEX),Y JSR sample LDX #$02 LDY #$06 JSR gencode ; asl $ff,x (six cycle delay) LDY #$00 LDA #$16 STA (zINDEX),Y INY LDA #$FF STA (zINDEX),Y ; Offset for asl,x LDX #$00 JSR sample LDX #$03 LDY #$07 JSR gencode ; asl $00ff,x (seven cycle delay) LDY #$00 LDA #$1E STA (zINDEX),Y INY LDA #$FF STA (zINDEX),Y INY LDA #$00 STA (zINDEX),Y ; Offset for asl,x LDX #$00 JSR sample RTS ; Generate code on the form ; LDA $D41C ; STA $8000 ; LDA $D41C ; STA $8008 ; ... gencode TXA CLC ADC zINDEX STA zFREKZP LDA #$00 ADC zINDEX+1 STA zFREKZP+1 TYA CLC ADC #sstart STA zFREKZP+3 nxtinst LDY #$00 ; lda $d41c LDA #$AD STA (zFREKZP),Y INY LDA #$1C STA (zFREKZP),Y INY LDA #$D4 STA (zFREKZP),Y INY ; sta $xxxx LDA #$8D STA (zFREKZP),Y INY LDA zFREKZP+2 STA (zFREKZP),Y INY LDA zFREKZP+3 STA (zFREKZP),Y INY TYA CLC ADC zFREKZP STA zFREKZP LDA #$00 ADC zFREKZP+1 STA zFREKZP+1 LDA #$08 CLC ADC zFREKZP+2 STA zFREKZP+2 LDA #$00 ADC zFREKZP+3 STA zFREKZP+3 SEC LDA zFREKZP+2 SBC #sstop BCC nxtinst LDY #$00 ; rts LDA #$60 STA (zFREKZP),Y RTS disable SEI LDA #$00 STA vSprEnable LDA vControl AND #$EF STA vControl vblank ; Wait for vblank (raster = 0) raslo BIT vControl BPL raslo rashi BIT vControl BMI rashi RTS enable LDA vControl ORA #$10 STA vControl CLI RTS hexout CMP #10 BCC digit CLC ADC #$07 digit ADC #$30 JSR kCHROUT RTS print STX zINDEX STY zINDEX+1 LDY #$00 nxtchr LDA (zINDEX),Y BEQ nul JSR kCHROUT INY JMP nxtchr nul RTS ; align to page boundary to avoid surprises .dsb $100 - (* & $ff) sample ; attack = 0 (rate period = 9 cycles) LDA #$00 STA sVoc3AttDec ; Release to zero LDA #$00 STA sVoc3SusRel STA sVoc3Control ; Release to zero. LDA #$00 env00 CMP sVoc3Env BNE env00 LDA #$01 STA sVoc3Control ; Wait for env = $01 env01 CMP sVoc3Env BNE env01 ; Prepare to trigger delay bug. LDA susrel STA sVoc3SusRel ; Prepare for release LDA #$00 ; If the envelope counter is the same for 9 cycles ; we have hit the first and the last cycle of the 9 cycle period. ; I.e. wait 8 cycles before next read (NOP*2 (4) + CPX (4)), align_r NOP LDY sVoc3Env NOP NOP CPY sVoc3Env ; Unless hit, wait 9 + 8 = 17 cycles for next period and try again. ; (NOP (2) + LDY (4) + NOP*2 (4) + CPY (4) + BNE (3)) ; We will get a hit within 9 iterations. BNE align_r ; NB! Must branch to same page. ; We're synchronized with the rate counter, ; now synchronize with the envelope counter. ; Loop must be 9 cycles (NOP (2) + INY (2) + CPY (2) + BNE (3)) align_e NOP INY CPY #$20-2 ; Requested envelope counter start value - 2. BNE align_e ; NB! Must branch to same page. ; We're now fully synchronized with ENV3, but which value does ; the rate counter have? ; At the point of the rate sync we were at the last cycle of ; the period. We're now offset by the following instructions: ; BNE (2) - BNE (1) = 1 cycles, i.e. we're at the first cycle. ; Now trigger the ADSR delay bug: ; 1. Increase period by starting release. ; 2. Wait for rate_counter > 8 ; 3. Reset period to 9 by starting attack. ; We need to wait at least 14 cycles before starting the next ; attack to trigger the delay bug. If the NOP below is removed the ; delay bug will not be triggered. NOP NOP STA sVoc3Control ; VICE/reSID doesn't work with the NOP (can have NOP here instead), ; it stops at $1F instead of $20 if the NOP is removed. LDA #$01 STA sVoc3Control ; The first sample is now delayed 18 cycles: ; NOP (2) + LDA (2) + STA (4) + LDA (2) + STA (4) + LDA (4) ; Add delay of a little less than 2^15 = 32768 cycles. LDY #$FF delay ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ; 50 ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ; 100 ASL zFREKZP ASL zFREKZP ASL zFREKZP ASL zFREKZP ; 120 BIT zFREKZP ; 123 DEY ; 125 BNE delay ; 128 - NB! Must branch to same page. ; Total loop delay is 2 (LDY) + 128*255 - 1 (BNE) = 32641 ; Total sample delay is 18 + 32641 = $7F93. scode .byte $00