Using beeper - help. (RST 40 and Stack)

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Profmyster
Drutt
Posts: 13
Joined: Wed Apr 10, 2019 8:46 am

Using beeper - help. (RST 40 and Stack)

Post by Profmyster »

Hi

I have a piece of code using the Stack & RST40 calculator, plays a few notes and then bums out with Int error. Any ideas. Not really sure what I'm doing to be honest. Ive seen use of this method in a few games I've disassembled - but can't seem o get it to work for me. Any help much appreciated, cheers

Code: Select all

org $8000
    di
    ld hl, tablea   ; $61da
    ld (pointer),hl 
    ld b, $ff

MainLoop
    call PlayNote
MainLoopCont
    jp MainLoop

Exit
    pop hl          ; clear stack of RET address
loop
    jp loop

PlayNote
    ld hl, (pointer)
    ld a,(hl)
    cp $00
    jp z, Exit
    srl a
    call $2d28
    rst 40
    defb $a4,$a4,$04,$05,$38    
    ld hl,(pointer)
    inc hl
    ld a, (hl)
    inc hl
    ld (pointer),hl
    call $2d28
    ld hl, (pointer)
    dec hl
    bit 7,(hl)
    jp z, PlayNotejp1
    rst 40
    defb $1b,$38
PlayNotejp1
    call $03f8
    ret                 ; reurn to MainLoopCont

pointer defw 0

tablea
 defb $c0,$00,$20,$00,$20,$02,$40,$03,$20,$02,$20,$00,$20,$82,$20,$00
 defb $40,$02,$ff,$85,$00,$00,$00,$00,$00,$00,$8c,$c8,$c0,$80,$ff,$ff

end $8000 
User avatar
djnzx48
Manic Miner
Posts: 730
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Using beeper - help. (RST 40 and Stack)

Post by djnzx48 »

I added in AND 127 to mask off the pitch value sign bit and it seems to work.

Code: Select all

org $8000
    di
    ld hl, tablea   ; $61da
    ld (pointer),hl 
    ld b, $ff

MainLoop
    call PlayNote
MainLoopCont
    jp MainLoop

Exit
    pop hl          ; clear stack of RET address
loop
    jp loop

PlayNote
    ld hl, (pointer)
    ld a,(hl)
    cp $00
    jp z, Exit
    srl a
    call $2d28
    rst 40
    defb $a4,$a4,$04,$05,$38    
    ld hl,(pointer)
    inc hl
    ld a, (hl)
    and 127        ; add this bit
    inc hl
    ld (pointer),hl
    call $2d28
    ld hl, (pointer)
    dec hl
    bit 7,(hl)
    jp z, PlayNotejp1
    rst 40
    defb $1b,$38
PlayNotejp1
    call $03f8
    ret                 ; reurn to MainLoopCont

pointer defw 0

tablea
 defb $c0,$00,$20,$00,$20,$02,$40,$03,$20,$02,$20,$00,$20,$82,$20,$00
 defb $40,$02,$ff,$85,$00,$00,$00,$00,$00,$00,$8c,$c8,$c0,$80,$ff,$ff

end $8000
Nienn Heskil
Microbot
Posts: 134
Joined: Tue Jun 09, 2020 6:14 am
Contact:

Re: Using beeper - help. (RST 40 and Stack)

Post by Nienn Heskil »

Normal people: 'Here's my beep routine guys. This sets the note frequency, this sets the duration...'
Spectrum ROM devs: 'OK, here's your awkward dependency of duration on frequency that only makes sense with crazy formulas, have fun!' 8-)
Spoiler
I've seen a game breaking out of the ROM BEEP loop using IM2 just to get around this issue. :lol:
Profmyster
Drutt
Posts: 13
Joined: Wed Apr 10, 2019 8:46 am

Re: Using beeper - help. (RST 40 and Stack)

Post by Profmyster »

Cool, many thanks...

djnzx48 wrote: Tue Jul 14, 2020 11:52 pm I added in AND 127 to mask off the pitch value sign bit and it seems to work.

Code: Select all

org $8000
    di
    ld hl, tablea   ; $61da
    ld (pointer),hl 
    ld b, $ff

MainLoop
    call PlayNote
MainLoopCont
    jp MainLoop

Exit
    pop hl          ; clear stack of RET address
loop
    jp loop

PlayNote
    ld hl, (pointer)
    ld a,(hl)
    cp $00
    jp z, Exit
    srl a
    call $2d28
    rst 40
    defb $a4,$a4,$04,$05,$38    
    ld hl,(pointer)
    inc hl
    ld a, (hl)
    and 127        ; add this bit
    inc hl
    ld (pointer),hl
    call $2d28
    ld hl, (pointer)
    dec hl
    bit 7,(hl)
    jp z, PlayNotejp1
    rst 40
    defb $1b,$38
PlayNotejp1
    call $03f8
    ret                 ; reurn to MainLoopCont

pointer defw 0

tablea
 defb $c0,$00,$20,$00,$20,$02,$40,$03,$20,$02,$20,$00,$20,$82,$20,$00
 defb $40,$02,$ff,$85,$00,$00,$00,$00,$00,$00,$8c,$c8,$c0,$80,$ff,$ff

end $8000
Post Reply