Page 1 of 1

Need help with my first code please.

Posted: Wed Apr 08, 2009 9:56 am
by Ealdor
First of all, hi all im new in nesdev (great site and community btw).

In new in NES programming and for my first program i tried to show some tiles of a chr file (tiles.chr) in the screen. Here is my code so far (im using asm6):

Code: Select all

.DB "NES", $1A
.DB $01
.DB $01
.DB %00000000, %00000000
.DB $00, $00, $00, $00, $00, $00, $00, $00

.org $C000

reset: sei
       cld

-      lda $2002
       bpl -
-      lda $2002
       bpl -

       lda #$3F
       ldx #$00
       sta $2006
       stx $2006
-      lda pallete,x
       sta $2007
       inx
       cpx #$32
       bne -

       lda #$20
       sta $2006
       sta $2006
       lda #$01
       sta $2007
       
       lda #%10001000
       sta $2000
       lda #%00011110
       sta $2001

       cli

loop:  jmp loop

irq:   rti

pallete:
.byte $0D,$00,$28,$01,$0D,$00,$10,$20,$0D,$20,$08,$06,$0D,$22,$28,$2D
.byte $0D,$38,$0D,$01,$0D,$30,$07,$1A,$0D,$06,$16,$26,$0D,$31,$32,$33

.pad $FFFA
.DW 0, reset, irq

.include "tiles.chr"
I expected to see the first tile of the chr file in the screen but all i got is a grey screen in the emulator, dont know whats is wrong.

Also i want to ask if i can put an attribute table after the ".include .chr", something like this:

Code: Select all

.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$F0,$F0,$F0,$F0,$F0,$F0,$F0,$F0
.byte $FF,$FF,$FF,%FF,$FF,$FF,$FF,$FF,$0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
Thanks in advance.

PS: im from Spain, sorry for my english.

Posted: Wed Apr 08, 2009 10:04 am
by Bregalad
You should replace in palette loading rotine "cpx #$32" by either "cpx #32" or "cpx #$20" and replace the $0d palette entires by $0f (the proper black color, $0d should be avoided).

Posted: Wed Apr 08, 2009 10:33 am
by Ealdor
Thanks Bregalad i fixed what you said, now i got a blue screen but no tiles in it yet.

Posted: Wed Apr 08, 2009 2:41 pm
by Roth
Try storing zeroes into $2000 and $2001 after your cld command. This will make sure that the screen is off. You want the screen to be turned off before doing anything to the PPU.

Is that .chr file something drawn in a tile editor? I don't know asm6 really, but I would expect there to be something to include binary data as well as files. .include is usually to include other asm files, whereas something like .incbin is for including binary files. I'm not sure what this would be in asm6, though. I might be totally off-base here, and your tiles might be drawn up in .byte form. If so, disregard that part hehe

I would stick that attribute table around the palette data, I think.

Code: Select all

attrib_table:
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$F0,$F0,$F0,$F0,$F0,$F0,$F0,$F0
.byte $FF,$FF,$FF,%FF,$FF,$FF,$FF,$FF,$0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F

Posted: Wed Apr 08, 2009 3:15 pm
by Ealdor
Thanks for your response Roth.
Try storing zeroes into $2000 and $2001 after your cld command. This will make sure that the screen is off. You want the screen to be turned off before doing anything to the PPU.
Done, but still the same result, green screen.
Is that .chr file something drawn in a tile editor?
Yup, when i open the file with "YY-CHR" i can see tiles drawn from bank 00 to bank 48.
I don't know asm6 really, but I would expect there to be something to include binary data as well as files. .include is usually to include other asm files, whereas something like .incbin is for including binary files. I'm not sure what this would be in asm6, though. I might be totally off-base here, and your tiles might be drawn up in .byte form. If so, disregard that part hehe
I got the example of the rom NES_101 in asm6 that loopy adapted from NESASM, and he used include ".chr".

Here is my updated code:

Code: Select all

.db "NES", $1A
.db $01
.db $01
.db %00000000, %00000000
.db $00, $00, $00, $00, $00, $00, $00, $00

.org $C000

RESET: sei 
       cld 

       lda #%00000000
       sta $2000
       lda #%00000000
       sta $2001

-      lda $2002 
       bpl - 
-      lda $2002
       bpl -

       lda #$3F
       sta $2006
       lda #$00
       sta $2006
       ldx #$00
-      lda PALLETE,x
       sta $2007
       inx
       cpx #$20
       bne -

       lda #$20
       sta $2006
       lda #$43
       sta $2006
       lda #$01
       sta $2007
       
       lda #%10001000
       sta $2000
       lda #%00011110
       sta $2001

       cli

LOOP:  jmp LOOP

PALLETE:
.db $32,$01,$06,$2A,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.db $0F,$38,$0F,$01,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00

.pad $FFFA
.dw 0, RESET, 0

.include "tiles.chr"

Posted: Wed Apr 08, 2009 3:38 pm
by Roth
Ah, I found what you were talking about. I didn't even know he had adapted NES101 hehe Anyway, in it, he does this:

Code: Select all

; CHR-ROM
.include "tutorchr.asm"
If you notice, that is an .asm file that he is including. You are using a binary file for your CHR. Try .incbin and see if that changes anything. I just checked the documentation, and that is indeed what is used for binary files.

Posted: Wed Apr 08, 2009 3:44 pm
by Ealdor
Roth wrote:Ah, I found what you were talking about. I didn't even know he had adapted NES101 hehe Anyway, in it, he does this:

Code: Select all

; CHR-ROM
.include "tutorchr.asm"
If you notice, that is an .asm file that he is including. You are using a binary file for your CHR. Try .incbin and see if that changes anything. I just checked the documentation, and that is indeed what is used for binary files.
Yeah, you are right is "tutorchr.asm" and not .chr. With incbin i got a black screen.

Posted: Wed Apr 08, 2009 4:01 pm
by frantik
it's working fine.. it's just you need to reset the scroll so you can see the tile on the screen :)

when writing to the screen, you can use FCEUD's nametable viewer to make sure everything is being written to the nametable. it also shows the scroll markers so you can see where the window is located

Code: Select all

 ...
      lda #$20
       sta $2006
       lda #$43
       sta $2006
       lda #$01
       sta $2007

; ADD THIS CODE
	    lda #$00
	    sta $2005
	    sta $2005
; END NEW CODE

       lda #%10001000
       sta $2000
       lda #%00011110
       sta $2001
...

Posted: Wed Apr 08, 2009 4:22 pm
by Ealdor
it's working fine.. it's just you need to reset the scroll so you can see the tile on the screen Smile
Yay, thanks frantik, now i can see the tiles :D

PS: the attribute table should go after the chr ?

Posted: Wed Apr 08, 2009 4:26 pm
by Celius
It seems also that you are enabling NMIs, but in the NMI vector you are pointing to $0000. I'm surprised this didn't cause total catastrophe!

Posted: Wed Apr 08, 2009 4:30 pm
by Roth
Ealdor wrote:
it's working fine.. it's just you need to reset the scroll so you can see the tile on the screen Smile
Yay, thanks frantik, now i can see the tiles :D

PS: the attribute table should go after the chr ?
Nah, you want the chr after your prg. Stick it above the vector stuff.

Also, I was thinking the same thing Celius : P

Posted: Thu Apr 09, 2009 12:49 am
by Ealdor
Celius wrote:It seems also that you are enabling NMIs, but in the NMI vector you are pointing to $0000. I'm surprised this didn't cause total catastrophe!
Not sure what you mean, should i insert this?:

Code: Select all

vblank: rti
nmi: rti
.....................
.....................
.....................

.pad $FFFA
.dw vblank, RESET, nmi
Or maybe just disable execute NMI on VBlank at the PPU Control Register.

Im trying for set the attribute table for my name table #0, not sure if this is correct:

Code: Select all

.db "NES", $1A
.db $01
.db $01
.db %00000000, %00000000
.db $00, $00, $00, $00, $00, $00, $00, $00

.org $C000

RESET: sei 
       cld 

       lda #%00000000
       sta $2000
       lda #%00000000
       sta $2001

-      lda $2002 
       bpl - 
-      lda $2002
       bpl -

       lda #$3F
       sta $2006
       lda #$00
       sta $2006
       ldx #$00
-      lda PALLETE,x
       sta $2007
       inx
       cpx #$20
       bne -

       lda #$20
       sta $2006
       lda #$43
       sta $2006
       lda #$2A
       sta $2007

       lda #$23
       sta $2006
       lda #$C0
       sta $2006
       ldy #$00
-      lda ATTRIB,y
       sta $2007
       iny
       cpy #$20
       bne -
       
       lda #$00
       sta $2005
       sta $2005 

       lda #%10001000
       sta $2000
       lda #%00011110
       sta $2001

       cli

LOOP:  jmp LOOP

VBLANK: rti

NMI:  rti

PALLETE:
.db $17,$01,$06,$2A,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.db $06,$38,$32,$06,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00

ATTRIB:
.byte $00,$00,$00,$00,$00,$00,$00,$00,$F0,$F0,$F0,$F0,$F0,$F0,$F0,$F0
.byte $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F

.pad $FFFA
.dw VBLANK, RESET, NMI

.incbin "1.chr"
Also im wondering why my background color is set in the sprite pallete and not in the background one.

Posted: Thu Apr 09, 2009 7:14 am
by tepples
You probably don't want to disable NMI on vblank unless you've got rendering turned off. If you try to detect vertical blanking by spinning on PPUSTATUS, you will miss some vblanks.

About background colors: $3F00 and $3F10 are actually the same memory cell inside the PPU. So if you write to $3F00 and then you write to $3F10 (as most naive palette loader loops seem to do), the value written to $3F10 will overwrite the value written to $3F00.

Posted: Thu Apr 09, 2009 7:25 am
by Celius
Ealdor wrote: Not sure what you mean, should i insert this?:

Code: Select all

vblank: rti
nmi: rti
.....................
.....................
.....................

.pad $FFFA
.dw vblank, RESET, nmi
That will do what you want it to do, however either the name "VBlank" or "NMI" should refer to the code executed in the NMI routine. You have your IRQ routine label named "nmi", which will cause confusion. This is how it's set up:

.pad $FFFA
.dw NMIAddress, ResetAddress, IRQAddress

So you should change the name of the label "vblank" to "NMI", and the "nmi" label to "IRQ".

Posted: Thu Apr 09, 2009 7:42 am
by Ealdor
Thanks Tepples now i understand what i read in a nes doc:

"The palette starts at $3F00 and goes to $3F1F in the VRAM. From $3F00 to $3F0F is the image(background) palette. From $3F10 to $3F1F is the sprite palette. This gives you 2 palettes that are 16 bytes each. The most important address in the palette is $3F00. The value that is written here defines background colour, transparency for both background and sprites, and is mirrored every 4 bytes. This means that $3F04,$3F08, $3F0C, $3F14, $3F18, are just mirrors of $3F00. Writing to these places does nothing."

I had to reread it like 4 or 5 times to full understand what it said :D
Celius wrote:So you should change the name of the label "vblank" to "NMI", and the "nmi" label to "IRQ".
Thanks Celius i fixed it.

Still fighting with the "attribute table" :?

EDIT: Yay, my attribute table is working!