NES Programming/Initializing the NES

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Although there are some NES compilers that utilize higher level languages (see references), most NES programming is done using assembly. If you haven't done so already, it would be best to look at the prior tutorial "A few words on assembly". For those of you looking for more information about NES Assembly, check out some of the links provided below. For the code examples below, I will be using the P65 Assembler, which I believe is the less confusing than NesAsm. Also it is written in Perl so anyone can use it.

INES Header[edit | edit source]

The INES Header is used to let the Emulator know the specifications of the NES Rom that you're trying to load. The first three bytes of any nes file contain the characters "NES" followed by the hex 0x1A. Note that hex values for the P65 begin with a '$'. After this initial information are two bytes representing the number of 16K Prg-Rom blocks and 8K Chr-Rom blocks respectively. Without mappers there can be up to two Prg-Rom blocks.

; iNES header  

; iNES identifier
.byte "NES"
.byte $1a
; Number of PRG-ROM blocks
.byte $01
; Number of CHR-ROM blocks
.byte $01
; ROM control bytes: Horizontal mirroring, no SRAM
; or trainer, Mapper #0
.byte $00, $00
; Filler
.byte $00,$00,$00,$00,$00,$00,$00,$00

Interrupt Vectors[edit | edit source]

The NES has three interrupt vectors which represent locations within the program code that the CPU jumps to when certain types of interrupts occur. There are three different interrupts for the NES: The Non-Maskable Interrupt or NMI ($FFFA), the Reset Vector ($FFFC), and the IRQ/BRK Vector ($FFFE). The hex values that are in parenthesis are where the NES looks to find the 16 bit location of where to jump to within the NES.

Normally the Reset vector will point to the beginning of your code which depending on the number of Prg-Rom blocks you have can be either be somewhere in the locate $8000 to $BFFF or $C000 to $FFFF.

The Non-Maskable interrupt refers to the interrupt that occurs when the PPU refreshes the screen (also known as the V-Blank period). During this time the screen can be updated without any flicker effect occurring.

Finally the IRQ/BRK interrupt (which I like to call the maskable interrupt) is used when the BRK command occurs. Often it is used by the programmer for various purposes.


 ; ... more code
 .advance $FFFA
 .word nmi, reset, irqq; words pointing to the location of nmi, reset and irq.

Programming the PPU[edit | edit source]

The Picture Processing Unit (PPU) on the NES handles the displaying of graphics onto the screen. It consists of four main components to display both background and sprite images: Pattern Tables, Name Tables, Attribute Tables, and Sprite/Image Palettes. The Pattern Tables store 8x8 or 8x16 pixel images that are used to display anything onto the screen. Each of these images can store a maximum of four different colors. The Name Tables are used to display images on the background, and the Attribute Tables affect the colors that can be displayed on the background. Finally there is the Sprite and Image Palettes which contain the possible colors for the foreground sprites and the background image respectively.

Pattern Tables:

Coming Soon...

Name Tables:

Coming Soon...

Attribute Tables:

Coming Soon...

Sprite and Image Palettes:

Coming Soon...

Links[edit | edit source]

P65 Assembler - Portable 6502 family assembler, it is what all the example code in this program is written using.

6502 Assembler Guide - A good introduction into the world of 6502 Assembly

Nes Dev - Great Site with tons of resources on NES Programming as well as links to alternative compilers.