Points of Required Attention™
Please chime in on a proposed restructuring of the ROM hacking sections.
Views: 88,491,388
Main | FAQ | Uploader | IRC chat | Radio | Memberlist | Active users | Latest posts | Calendar | Stats | Online users | Search 04-27-24 06:15 AM
Guest: Register | Login

0 users currently in ROM Hacking | 3 guests | 1 bot

Main - ROM Hacking - CHR-RAM Writing/Swapping Thread New thread | New reply


RetroRain
Posted on 08-06-12 10:24 PM Link | Quote | ID: 151830


Fuzz Ball
Level: 66

Posts: 600/994
EXP: 2438351
Next: 23500

Since: 09-30-07

Last post: 1936 days
Last view: 958 days
I had a painful day yesterday. I spent literally the whole day trying to figure out how to deal with CHR-RAM. I have had tons of trouble with CHR-RAM in the past, really getting turned off with games that have it, and not CHR-ROM, since CHR-ROM is easier to deal with. But after all of my trouble yesterday, I finally figured out how to deal with CHR-RAM. A couple of years of mystery has now been unveiled. And yes, it has been a couple of years, but bear in mind, that I haven't hacked in a long time. So, it simply was a question that I never got an answer to, until yesterday when I was finally determined to get to the bottom of this once and for all.

Now, why am I making this thread? Very simple.

Through ALL of the searching I have done on the Internet, and you can search too if you want, there is not one decent document that covers how to deal with CHR-RAM, AT ALL.

Which is bullshit considering how long the ROM hacking scene has been around.

So, I will unveil the mystery, and post it here, for anyone who needs to deal with CHR-RAM, but doesn't know how to.

So, here it goes:

1. Put the wait for Vblank code:

wait_vblank:
LDA $2002
BPL wait_vblank

2. Set $2006 to where you want to write to. So if tile $00 on the left side of the PPU, then

LDA #$00
STA $2006
STA $2006

If $00 on the right side of the PPU, then

LDA #$10
STA $2006
LDA #$00
STA $2006

You have to write to $2006 twice. If it is tile $20 on the right side, you want to write to, it's:

LDA #$12
STA $2006
LDA #$00
STA $2006

The numbers are different when it comes to PPU tiles, because of the size.

3. Run a loop 16 times, since 16 bytes make up one 8x8 tile.

During that loop, write your tiles from a table close-by, to $2007. That's it! See below.


$B758:AD 02 20 LDA $2002 = #$10 ; Wait for vblank
$B75B:10 FB BPL $B758

$B75D:A9 00 LDA #$00 ; Set $2006 to tile $00 on the left side of the PPU, we're writing to that tile
$B75F:8D 06 20 STA $2006 = #$C1
$B762:A9 00 LDA #$00
$B764:8D 06 20 STA $2006 = #$C1

; Loop code
$B767:A2 00 LDX #$00
$B769: BD 78 B7 LDA $B778,X @ $B779 = #$10
$B76C:8D 07 20 STA $2007 = #$00
$B76F:E8 INX
$B770:E0 10 CPX #$10
$B772: D0 F5 BNE $B769

; Graphics table. 16 bytes make up one 8x8 tile.
$B778:00 BRK
$B779:10 1B BPL $B796
$B77B:1F UNDEFINED
$B77C:1F UNDEFINED
$B77D:1F UNDEFINED
$B77E:1F UNDEFINED
$B77F:0F UNDEFINED
$B780:00 BRK
$B781:00 BRK
$B782:00 BRK
$B783:00 BRK
$B784:00 BRK
$B785:00 BRK
$B786:00 BRK
$B787:00 BRK
So, whenever you need to write one tile to the PPU for a game that uses CHR-RAM, you can copy this code, and use it for your own purposes.

For multiple tiles, just use another loop for multiple cycles.

Note: I was able to write $90 bytes without problems. If you try to write too much, this will create problems in the PPU and on the screen. I believe it is because you are trying to write too much during vblank. It can only take so much.

I believe the way around that, is to wait for vblank, then shut the screen off, write everything you need to write, and then turn the screen back on. I haven't tested that yet, but I will soon. If anyone can confirm that this works, feel free to reply.

____________________
My YouTube Channel

Hamtaro126
Posted on 08-07-12 12:20 AM Link | Quote | ID: 151832


Cheep-cheep
Level: 33

Posts: 158/194
EXP: 212852
Next: 16327

Since: 05-02-07
From: Shelton, WA

Last post: 2473 days
Last view: 2316 days
Here is another, Better to read one, Using your base code:


;Original code by RetroRain!

; Wait for vblank
wait_vblank:
LDA $2002
BPL wait_vblank
RTS

; Set $2006 to tile $00 on the left side of the PPU, we're writing to that tile+
SmileCHR:
JSR wait_vblank
LDA #$00
STA $2006
LDA #$00
STA $2006

; Loop code
LDX #$00
TileLoop:
LDA Tile0,X
STA $2007
INX
CPX #$10
BNE TileLoop
RTS

; Graphics table. 16 bytes make up one 8x8 tile.
; (Did Words instead of Bytes make this easy)

Tile0:
;Layers: 1111111122222222
.dw %0000000000111100
.dw %0000000001011010
.dw %0000000011011011
.dw %0000000011111111
.dw %0000000010111101
.dw %0000000011000011
.dw %0000000001100111
.dw %0000000000111100


____________________
Mah boi, romhacking is what all true warriors strive for!

I wonder what's for dinner?

Main - ROM Hacking - CHR-RAM Writing/Swapping Thread New thread | New reply

Acmlmboard 2.1+4δ (2023-01-15)
© 2005-2023 Acmlm, blackhole89, Xkeeper et al.

Page rendered in 0.019 seconds. (349KB of memory used)
MySQL - queries: 42, rows: 64/64, time: 0.015 seconds.