Need Help Assembling Code for NSF to Cart

Discuss NSF files, FamiTracker, MML tools, or anything else related to NES music.

Moderator: Moderators

Post Reply
mysteriousity
Posts: 52
Joined: Mon Oct 03, 2011 5:44 pm

Need Help Assembling Code for NSF to Cart

Post by mysteriousity »

Hello all,

I'm trying to Assemble my source code for NSF to Cart and I'm getting some errors. I would be much very much obliged if some could help me with this, as I'm very VERY new to NES programming and really need some help.

Also, I got the idea for doing this from the following article:

http://www.nullsleep.com/treasure/nsf_c ... e_v1.0.txt

They are using X816 as the assembler.

Below my posted code are the errors I receive in NESASM3

Here is my code:

Code: Select all


.mem 8			; 8-bit memory mode
.index 8		; 8-bit index mode
.opt on			; address optimize

.org $0000		; replace dashes with load address MINUS $80
.incbin "metroid.nsf"	; include NSF tune

Reset_Routine:
	cld			; clear decimal flag
	sei			; disable interrupts
	lda #%00000000		; disable vblank interrupts by clearing
	sta $2000		; the most significant bit of $2000
	
; *** WAIT 2 VBLANKS ***
WaitV1:	
	lda $2002		; give the PPU a little time to initialize
	bpl WaitV1		; by waiting for a vblank
WaitV2:	
	lda $2002		; wait for a second vblank to be safe
	bpl WaitV2		; and now the PPU should be initialized
	
; *** CLEAR SOUND REGISTERS ***
	lda #$00		; clear all the sound registers by setting
	ldx #$00		; everything to 0 in the Clear_Sound loop
Clear_Sound:
	sta $4000,x		; store accumulator at $4000 offset by x
	inx			; increment x
	cpx #$0F		; compare x to $0F
	bne Clear_Sound		; branch back to Clear_Sound if x != $0F

	lda #$10		; load accumulator with $10
	sta $4010		; store accumulator in $4010
	lda #$00		; load accumulator with 0
	sta $4011		; clear these 3 registers that are 
	sta $4012		; associated with the delta modulation
	sta $4013		; channel of the NES
	
; *** ENABLE SOUND CHANNELS ***
	lda #%00001111		; enable all sound channels except
	sta $4015		; the delta modulation channel

; *** RESET FRAME COUNTER AND CLOCK DIVIDER ***
	lda #$C0		; synchronize the sound playback routine 
	sta $4017		; to the internal timing of the NES
	
; *** SET SONG # & PAL/NTSC SETTING ***
	lda #$00		; replace dashes with song number
	ldx #$00		; replace with $00 for NTSC or $01 for PAL
	jsr $A000		; replace dashes with init address

; *** ENABLE VBLANK NMI ***
	lda #%10000000		; enable vblank interrupts by setting the 
	sta $2000		; most significant bit of $2000
	
NMI_Routine:
	lda $2002		; read $2002 to reset the vblank flag
	lda #%00000000		; clear the first PPU control register  
	sta $2000		; writing 0 to it
	lda #%10000000		; reenable vblank interrupts by setting
	sta $2000		; the most significant bit of $2000
	jsr $B3B4		; replace dashes with play address
	rti			; return from interrupt routine
	
IRQ_Routine:
	rti			; return from interrupt routine

.pad $FFFA
	.dw	NMI_Routine	; setup the NMI vector at $FFFA
	.dw	Reset_Routine	; setup the Reset vector at $FFFC
	.dw	IRQ_Routine	; setup the IRQ vector at $FFFE





When I run this in NESASM3 I get the following errors:

pass 1
#[1] NSF.asm
1 00:E000 .mem 8 ; 8-bit memory mode
Local symbol not allowed here!
2 00:E000 .index 8 ; 8-bit index mode
Local symbol not allowed here!
3 00:E000 .opt on ; address optimize
Local symbol not allowed here!
5 00:E000 .org $0000 ; replace dashes with load add
ress MINUS $80
Local symbol not allowed here!
6 00:E000 .incbin "metroid.nsf" ; include NSF tune
Local symbol not allowed here!
67 00:E055 .pad $FFFA
Unknown instruction!
# 6 error(s)

Any help with this would be greatly appreciated.

Thank You

-M
Last edited by mysteriousity on Thu Dec 29, 2011 12:28 am, edited 1 time in total.
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

This approach won't work at all. A NSF file is a ROM, complete with its own headers, not some kind of music data to include in a program.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
mysteriousity
Posts: 52
Joined: Mon Oct 03, 2011 5:44 pm

Post by mysteriousity »

Dwedit wrote:This approach won't work at all. A NSF file is a ROM, complete with its own headers, not some kind of music data to include in a program.
Here is the guide to do this where I got the idea from. Others have apparently gotten this approach to work.

http://www.nullsleep.com/treasure/nsf_c ... e_v1.0.txt

Unless I'm completely misunderstanding what the guide is trying to do. I'm having problems getting it to compile.
mic_
Posts: 922
Joined: Thu Oct 05, 2006 6:29 am

Post by mic_ »

You typically can't use code written for one assembler with another assembler as-is.

For example, I've never heard of ".mem", ".index" or ".opt" for NESASM. It's possible that they exist, but it's not something that I've ever used.

Also, IIRC NESASM requires you to place some kind of whitespace (a space or a tab) before any directive (.org, .incbin, etc), otherwise it'll interpret it as a local label.
User avatar
Kasumi
Posts: 1293
Joined: Wed Apr 02, 2008 2:09 pm

Post by Kasumi »

I don't quite understand the point of this. Can you help me understand your end goal? The actual reason you want to make an NSF rom?

If you just want to listen to your NSF on real hardware, you can do this with a powerpak.

If you want to put your NSF on a standalone cart, famitracker will make a rom you can burn if you used famitracker to make the music.

Because if you want to make a cart of music you made, this is not the way to do it.

I did get this method working with the Super Mario Bros. NSF and nesasm, but I had to use my NES programming knowledge to do it. I had to hack out the header to the NSF which is pretty simple. Then I just followed the guide, except I had to put the NMI routine and setup described in the guide at $8000 rather than after the nsf. Super Mario Bros. sets itself up at $BDC4, and is long, so there would be no room for it afterwards.

There are so many things that can go wrong with this method. If you've got an nsf with songs you actually made, let us know what you used to make them and we may be able to help you out.

If it's just commercial NSFs, I would just get a powerpak and call it day.

Edit: In any case, here's the code to get the first song of the Super Mario NSF to play on a rom compiled with nesasm: http://pastebin.com/gU30fF4V

Edit 2: Or if you're feeling adventurous, you can try rainwarrior's program that imports any NSF track into famitracker and then use famitracker to export that as a rom. This of course means you only get one song per file, but that's also case with the guide's method unless you learn how to read the joypad.

Edit 3: Nevermind. The import is not optimized for size, so it's often far too large for an NROM rom. Still a neat tool, though.
User avatar
Memblers
Site Admin
Posts: 3902
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Post by Memblers »

This will only work on NSFs that don't use bankswitching (many NSFs could use it to optimize size even if they don't need the bankswitching itself). Better watch for that too, if you're playing ripped NSFs.
mysteriousity
Posts: 52
Joined: Mon Oct 03, 2011 5:44 pm

Post by mysteriousity »

Kasumi wrote:I don't quite understand the point of this. Can you help me understand your end goal? The actual reason you want to make an NSF rom?

If you just want to listen to your NSF on real hardware, you can do this with a powerpak.

If you want to put your NSF on a standalone cart, famitracker will make a rom you can burn if you used famitracker to make the music.

Because if you want to make a cart of music you made, this is not the way to do it.

I did get this method working with the Super Mario Bros. NSF and nesasm, but I had to use my NES programming knowledge to do it. I had to hack out the header to the NSF which is pretty simple. Then I just followed the guide, except I had to put the NMI routine and setup described in the guide at $8000 rather than after the nsf. Super Mario Bros. sets itself up at $BDC4, and is long, so there would be no room for it afterwards.

There are so many things that can go wrong with this method. If you've got an nsf with songs you actually made, let us know what you used to make them and we may be able to help you out.

If it's just commercial NSFs, I would just get a powerpak and call it day.

Edit: In any case, here's the code to get the first song of the Super Mario NSF to play on a rom compiled with nesasm: http://pastebin.com/gU30fF4V

Edit 2: Or if you're feeling adventurous, you can try rainwarrior's program that imports any NSF track into famitracker and then use famitracker to export that as a rom. This of course means you only get one song per file, but that's also case with the guide's method unless you learn how to read the joypad.

Edit 3: Nevermind. The import is not optimized for size, so it's often far too large for an NROM rom. Still a neat tool, though.
This is going to sound incredibly silly, but the reason I want to do this, is because I'm creating a Super Mario Brothers pinball game for my girlfriend and I want the sound to be generated by a real Nintendo. My plan was to create a cart that allowed sound effects and music to play by the press of the buttons on the control pad, and then jumper the pinball switches into the controller of the nes to activate the sound effects.

So for example, when the start button for the pinball game is pressed, the Mario Brothers theme, which is set to the start button on the controller as well as jumpered to the start button of the pinball machine, plays. Bumpers would be jumpered to the B/A buttons etc.

Again, silly, probably not a great way to do it, but it's forcing me to learn about coding, which I sort of wanted to do anyway. Thanks for all the help so far guys. Any more would be highly appreciated.
User avatar
Kasumi
Posts: 1293
Joined: Wed Apr 02, 2008 2:09 pm

Post by Kasumi »

That's not silly. But I don't think anything like this will help you much with learning how to code, because it's a rather difficult thing to do if you don't already know 6502 assembly.

My first idea is to use one of the Super Mario Bros. disassemblies and remove everything that's not related to the music engine.

This is technically what an NSF is, but an NSF is assembled code that may or may not have stripped out the ability to play music and sound effects at the same time. Even if that code is there, you have to find how it works in assembled code which I think would be harder than with disassembled code.

When you've got just the music engine, you can figure out how it works from the disassembly, and trigger sound effects on button presses.
Post Reply