A jump sound that borrows the music's channel, the way Mario's does

The jump lesson’s square with a tune of eight notes going round and a jump sound, both made up here. Play it with the sound on to hear the tune step aside. The program is 267 instructions in 616 bytes, built and checked the same way.

Play it here The cartridge, to play anywhere that plays NES cartridges: sound.nes

On the screen

frame 100
frame 100
frame 155
frame 155
frame 165
frame 165
frame 200
frame 200

Two sounds, one channel

The console has two square-wave channels, a triangle, a noise channel and one for samples, and a game has more sounds than that. Super Mario Bros. plays part of its music on the first square channel, and when Mario jumps, the jump sound takes that channel over: its settings are written there with the pitch sweep turned on, and the music does not write to the channel again until a later note. This cartridge shares its channel the same way. The tune keeps counting while the jump sound has the channel, so it comes back in time, at its next note. Only that way of sharing is taken from the game; what either sound plays is not compared, because a sound is something somebody composed.

this cartridgeSuper Mario Bros.
writes to the first square channel in the 60 frames before the jump16136
frames from A to the jump sound01
settings written with the pitch sweep on, and on which of its frames1 (frames 0)3 (frames 0, 3, 8)
frames until the music writes to the channel again1239

As written

The part of the program this lesson is about. The whole program is a plain text file: sound.s

;; The jump sound: the first square channel, a half-and-half wave whose
;; loudness falls away by itself, the pitch sweep on and rising, from a
;; low note. It holds the channel for 10 frames.
jumpsound:
    LDA #$84
    STA $4000
    LDA #$9B
    STA $4001
    LDA #$90
    STA $4002
    LDA #$11
    STA $4003
    LDA #$0A
    STA $52
    RTS
;; The tune: a note every 16 frames, round the eight. While the jump
;; sound holds the channel the tune only counts; it writes again at its
;; next note after the jump sound has let go.
tune:
    LDA $52
    BEQ free
    DEC $52
free:
    DEC $51
    BPL held
    LDA #$0F
    STA $51
    LDX $50
    INX
    TXA
    AND #$07
    STA $50
    LDA $52
    BNE held
    LDX $50
    LDA #$B6
    STA $4000
    LDA #$08
    STA $4001
    LDA notelow,X
    STA $4002
    LDA notehigh,X
    STA $4003
held:
    RTS

All the lessons