How to make a song play during the game

6502, 65c02, and 6280 CPU programming
Post Reply
puppydrum64
Posts: 23
Joined: Thu Apr 22, 2021 9:30 pm

How to make a song play during the game

Post by puppydrum64 » Sat Apr 24, 2021 2:16 am

I'm trying to figure out how to make a song play so that each frame, the next note is played. I'm using a data block to contain a sequence of notes and a simple loop counter to track which note to play. After a note is played, the note counter is incremented by 1 and the main game loop continues. The tricky part is getting the song to play at the correct speed.

This is my code (it uses the Konami VRC6 chip, aka Mapper 24, so you might not be familiar with this but similar logic should work with the built in sound channels. Since my asm file is pretty big I'm only leaving in the parts that deal with the music itself.

Code: Select all

song_illuminator:
	db $9f,$9f,$9f,$9f,$9f,$9f
	db $af,$af,$9f,$9f,$9f,$9f
	db $9f,$9f,$cf,$cf 
	
handleMusic:
	pha
	txa
	pha
	tya
	pha
		lda #%01001001	;Volume 9, Duty Mode 04
		ldx musicloop
		jsr ChibiSoundVRC6
	pla
	tay
	pla
	tax
	pla
	rts

ChibiSoundVRC6:	
	STA VRC6_PULSE1_CTRL
	LDA musicloop,x
	STA VRC6_PULSE1_FREQ_LO
	LDA #%10000000
	STA VRC6_PULSE1_FREQ_HI
	inx
	cpx #$10                     ; See Note 1
	beq loopMusic           ; See Note 1
	RTS			        ; See Note 1
	loopMusic:             ; See Note 1
	ldx #$00	                ; See Note 1
	rts

; Note 1: Only the first note plays if these lines of code are here. Otherwise the whole song does play but much too fast.

Problem is, this isn't working. It's late at night so I might not be thinking clearly enough to proofread this code.

User avatar
akuyou
Posts: 491
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: How to make a song play during the game

Post by akuyou » Sun Apr 25, 2021 9:56 pm

If you're looking to play music during gameplay, you will typically update the music during Vblank (once per frame) or some other time based interrupt.

Is that what you're doing? I can't see anything in the code fragment defining what causes the music update to execute
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

puppydrum64
Posts: 23
Joined: Thu Apr 22, 2021 9:30 pm

Re: How to make a song play during the game

Post by puppydrum64 » Mon Apr 26, 2021 8:42 pm

I had "handlemusic" within the "main loop" which I omitted to keep the post brief. Would putting the music code in the NMI handler count as waiting for vblank?

User avatar
akuyou
Posts: 491
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: How to make a song play during the game

Post by akuyou » Mon Apr 26, 2021 11:13 pm

Yes, The NMI is Vblank on the NES - that's where I'd put any music update code on that system - I'd put it at the end, after any tile/sprite update routines, as (to my knowledge) there's no reason the sound can't update after Vblank actually ended.
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

puppydrum64
Posts: 23
Joined: Thu Apr 22, 2021 9:30 pm

Re: How to make a song play during the game

Post by puppydrum64 » Tue Apr 27, 2021 9:04 pm

I think I get it. I struggle with understanding vblank since it happens so fast you can't see it happen. I tried to understand sprite zero hit and couldn't get that to work either

User avatar
akuyou
Posts: 491
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: How to make a song play during the game

Post by akuyou » Thu Apr 29, 2021 2:04 pm

Sound is a good one to try in vblank!
If your music plays to fast, there's something wrong... if it doesn't play at all, the interrupt isn't enabled.

By the way, I've got a tutorial on Sprite 0 hit on the way... it was an utter pain to figure out, so I'm not surprised you found it difficult!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Post Reply

Return to “General 6502 Programming”