4

Let's say I want to write K to the top of the status bar in SMB. I can use the following set of statements to write to PPU memory.

    memory.writebyte(0x2001, 0x00) -- Turn off rendering
    memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)
    memory.writebyte(0x2006, 0x20) -- PPUADDR high byte
    memory.writebyte(0x2006, 0x2A) -- PPUADDR low byte
    memory.writebyte(0x2007, 0x14) -- PPUDATA ('K')
    memory.writebyte(0x2001, 0x1e) -- Turn on rendering

enter image description here

I tried changing the color of the text by changing the color palette before writing text as -

memory.writebyte(0x2001, 0x00) -- Turn off rendering
memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)

memory.writebyte(0x2006, 0x3F) -- Selecting high byte color palette
memory.writebyte(0x2006, 0x11) -- Selecting low byte color palette
memory.writebyte(0x2007, 0x16) -- Red color

memory.writebyte(0x2006, 0x20) -- PPUADDR high byte
memory.writebyte(0x2006, 0x2A) -- PPUADDR low byte
memory.writebyte(0x2007, 0x14) -- PPUDATA ('K')
memory.writebyte(0x2001, 0x1e) -- Turn on rendering

However that changes the color of the background objects.

My question is how I can set the color of the displayed text?

3
  • 3
    I've never tried programming the NES, so maybe I'm completely wrong, but shouldn't you be updating the relevant block in the attribute table (wiki.nesdev.com/w/index.php/PPU_attribute_tables) rather than changing palette entries for this?
    – occipita
    Jul 24 '20 at 5:12
  • I am pretty sure the NES cannot be programmed with Lua…? Aug 27 at 6:31
  • The emulator Mesen lets you write a Lua script to interact with the game. Sep 4 at 2:04
0

Look at it in the Mesen emulator, under Debugger / PPU Viewer / Palette Viewer (there you can change the palette to see the effect).

For the text color, you want palette address $3F09. This is also the cloud's color. $3F11 is in the sprite palette rather than the background, and it seems to be Mario's outfit color.

Also, note that most games store the palette in RAM, and write to the PPU every vblank. Makes it easier to handle palette cycling, transitions, stuff like that.

1
  • Weird, when I try it I get the following error message: "attempt to index a nil value (global 'memory')" Sep 4 at 1:57
0

Instead of changing the palette itself you might find it easier to change the attributes instead.

Try this:

memory.writebyte(0x2001, 0x00) -- Turn off rendering
memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)

memory.writebyte(0x2006, 0x23) -- Selecting high byte attribute table
memory.writebyte(0x2006, 0xC1) -- Selecting low byte attribute table 
; you might need to fiddle with this number, should be somewhere between 0xC0 and 0xC8
memory.writebyte(0x2007, 0x00) -- Use Palette 0

memory.writebyte(0x2006, 0x20) -- PPUADDR high byte
memory.writebyte(0x2006, 0x2A) -- PPUADDR low byte
memory.writebyte(0x2007, 0x14) -- PPUDATA ('K')
memory.writebyte(0x2001, 0x1e) -- Turn on rendering

Equivalent assembly code if you prefer:

LDA #$00
STA $2001
LDA $2002

LDA #$23
STA $2006
LDA #$C1
STA $2006

LDA #$00
STA $2007

LDA #$20
STA $2006
LDA #$2A
STA $2006

LDA #$14
STA $2007

LDA #$1E
STA $2001

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.