Writing Your First SNES Program

Now it is time to get your hands dirty. You will be making your first SNES ASM program today. It's about time, huh. So let's get on with it!

What this program will do:

Well it will basically fill then entire screen with one color. MAN is that EXciting OR WHAT! Now, before we begin, I want you to know what you are writing. So some very Quick (actually long) theory. let's go! If you've read any of Qwertie's SNES documentation, you may have come by the term CG or CGRAM. CG is the color palette data. A palette is all the colors that our tiles will be able to use, but sprites and tiles don't say "I need color Red from the palette," they ask for a certain entry, or number, in the palette. So if color 1 in the palette is yellow, any tiles that used color 1 are using yellow. What if you changed that palette entry later on? Then that tile's color would change right in front of you! Cool, huh. Alright, I think you get it now..

The CGRAM is where we store the palette data. Now, there's 2 registers that we need to deal with in order to modify the palette. They are $2121, and $2122. Let's see what Yoshi has to say about these.

| w   |$2121   |Color # (or palette) selection register [CGDATA]
|     |        |xxxxxxxx             x: address (color #).
|     |        |
|     |        |
| wd  |$2122   |Color data register [CGDATA]
|     |        |xxxxxxxx             x: Value of color. 
|     |        |
|     |        |SNES color is 15 bit; 5 bits for red, green, and blue. The order isn't RGB though: it's BGR (RGB reversed!).

Ok so now we know what we need to do to modify the palette. We need to write the number of the color we wish to modify to $2121, and then write the new color data to $2122. And Look, it says you need to write to $2122 twice (wd - d = double-write required). But what's the format of these 2 bytes we need to write? Let's look in Snes2.txt

The format (in binary) is the following:

?bbbbbgg gggrrrrr

b: blue
g: green
r: red
?: "The infamous bit-of-confusion." (just leave it 0 ;)

Yoshi even provides you with some sample colors. Well let's use one of his sample colors, Red ;). We'll fill the screen with red.

Since Init SNES turns off the screen, we need to turn it back on. Look up register $2100 in Yoshi's doc to see what I did to turn on the screen.

Note that the screen is turned off in order to force a vertical blank.(VBLANK). When the screen is being drawn, you cannot edit data in VRAM (or at least not accurately). You need to either wait for the tracer to be in the stage where it's not drawing, or just shut off the entire screen and "force VBLANK." Just thought you should know that if you didn't already.

;== Include memorymap, header info, and SNES initialization routines
.INCLUDE "header.inc"
.INCLUDE "SnesINIT.asm"

;========================
; Start
;========================

.BANK 0 SLOT 0
.ORG 0
.SECTION "MainCode"

Start:
        InitSNES            ; Init Snes :)

        stz $2121           ; Edit color 0 - snes' screen color you can write it in binary or hex
        lda #%00011111      ; binary is more visual, but if you wanna be cool, use hex ;)
        sta $2122
        stz $2122           ; second byte has no data, so we write a 0

        lda #$0F            ; = 00001111
        sta $2100           ; Turn on screen, full brightness

forever:
        jmp forever

.ENDS

Ok, how does it fill the whole screen?

Well, the big important part is that we edited color 0. From what I understand, the SNES uses Color 0 as the "transparent" color and fills the screen with it. When we edit it, the screen color changes. The loop just loops.. It keeps the program running, nothing more. Although nothing is updated later, the loop is not really necessary, but it's good practice.

That's that! Complete Source Code: first-snes-program.7z.

Go to Working with VRAM - Loading the Palette.

Tutorial by bazz