Offline
Chicago

Hello Everyone! I'm back with another NES sound related question.
Having scrapped getting sound from the NES via a .nsf file, I have been trying to hard-code some little ditties into the chip in assembly.
My current process is this: I am making separate sub-routines for each note/note length and then calling them in an order, to form a sort of song.
Right now, my code for the note D, with a fade out looks like this:

D3:
    ;pitch D3
    lda #%11111001
    sta $4002
    lda #%10000010
    sta $4003

    ;volume & pulse width
    ;fade out
    ;F-D-B-9-7-5-3-2-1-0
    lda #%00111111
    sta $4000
    sta $4000
    sta $4000
    sta $4000
    sta $4000
    sta $4000
    lda #%00111101
    sta $4000
    lda #%00111011
    sta $4000
    lda #%00111001
    sta $4000
    lda #%00110111
    sta $4000
    lda #%00110101
    sta $4000
    lda #%00110011
    sta $4000
    lda #%00110010
    sta $4000
    lda #%00110001
    sta $4000

    RTS

and to an extent, this works! the NES generating a D3 pitch. HOWEVER: the fade is not working as expected. What I expect to happen is for the note to hold at maximum volume for a moment, before going down in volume. what happens in actuality, is that the APU only seems to be reading (from what i can hear) the last write to $4000, meaning that i have a steady tone at volume 1, rather than a tone that decreases in volume. If anyone can help me troubleshoot this, that would be very helpful.

Note: I understand that the length can be changed via the highest 5 bits written to $4003, but I still would like to experiment with this method and get it to work.

Offline
Sweeeeeeden

All the writes happen within microseconds of each other, and only the last (or should I say current) write is in effect. You would need to write a sequencer which writes a new value (for example) every frame in the NMI interrupt handler. So you would need to store a variable somewhere, and maybe a list of volume values in ROM that you would be reading one by one and writing to $4000.

Offline
Sea of Souls

Ugh, interrupts are such a headache for me, but if anyone can help it is nitro. I will be keeping an eye on the progress of this.

Offline
Sweeeeeeden
Orgia Mode wrote:

Ugh, interrupts are such a headache for me, but if anyone can help it is nitro. I will be keeping an eye on the progress of this.

It's not me in this case. I haven't coded for the NES at all, so I can only give general advice. GB is my console. I'd recommend asking for help in the NESdev forums or wait for one of the NES gurus to come around.

Offline
Praha, Czech republic

You're exactly right though, that's what he needs to do :]
Look at this link and also this one, you can just

jsr wait_nmi

before each lda / sta pair you do to get up and running, though it's a bit ugly. But hey, it's a start!
Each VBL wait is approx. 1/60 s.