News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Adding CHR-bank Swaps to a ROM  (Read 2321 times)

M-Tee

  • Hero Member
  • *****
  • Posts: 596
  • One pixel at a time…
    • View Profile
    • M-Tee Retro Graphics
Adding CHR-bank Swaps to a ROM
« on: May 03, 2014, 07:08:41 am »
So, I'm working on the Shogi hack, and so far have a version with Kanji and a version with English training pieces. The only difference between the two are the CHR banks. The ROM is Naitou 9 Dan Shougi Hiden (J).nes.

So what I want to do is have the player be able to toggle between the CHR banks while playing. The original game is Mapper 0.

So my attempts so far have been to:

Check out the docs NES Rom Expansion 101 and 102 by TFG, along with King Mike's also.

Afterward, using nflate, I've expanded the ROM's CHR bank, changed the mapper to CNROM, and placed the correct graphics into the freely available space.

Then, from the Nerdy Nights tutorials, there is an example in the tutorial that uses Start and Select to swap CHR banks back and forth.

I figured this control scheme would work well because the Shogi game uses neither start nor select during gameplay, and if the CHR bank were to also be swapped during the title screen, the only time start or select are used, it wouldn't make a difference.

So, like a monkey with a screwdriver, I crudely try to identify the relevant code, remove the segments between asterisks below...

Code: [Select]
LatchController:
  LDA #$01
  STA $4016
  LDA #$00
  STA $4016       ; tell both the controllers to latch buttons


ReadA:
  LDA $4016       ; player 1 - A
  AND #%00000001  ; only look at bit 0
****BEQ ReadADone   ; branch to ReadADone if button is NOT pressed (0)
                  ; add instructions here to do something when button IS pressed (1)
  LDA $0203       ; load sprite X position
  CLC             ; make sure the carry flag is clear
  ADC #$01        ; A = A + 1
  STA $0203       ; save sprite X position****
ReadADone:        ; handling this button is done
 

ReadB:
  LDA $4016       ; player 1 - B
  AND #%00000001  ; only look at bit 0
***BEQ ReadBDone   ; branch to ReadBDone if button is NOT pressed (0)
                  ; add instructions here to do something when button IS pressed (1)
  LDA $0203       ; load sprite X position
  SEC             ; make sure carry flag is set
  SBC #$01        ; A = A - 1
  STA $0203       ; save sprite X position[/s]****
ReadBDone:        ; handling this button is done


ReadSelect:
  LDA $4016       ; player 1 - select
  AND #%00000001  ; only look at bit 0
  BEQ ReadSelectDone   ; branch to ReadSelectDone if button is NOT pressed (0)
                  ; add instructions here to do something when button IS pressed (1)

  LDA #$00
  JSR Bankswitch  ; change to graphics bank 0

ReadSelectDone:   ; handling this button is done


ReadStart:
  LDA $4016       ; player 1 - start
  AND #%00000001  ; only look at bit 0
  BEQ ReadStartDone   ; branch to ReadStartDone if button is NOT pressed (0)
                  ; add instructions here to do something when button IS pressed (1)

  LDA #$01
  JSR Bankswitch  ; change to graphics bank 1

ReadStartDone:   ; handling this button is done

  RTI             ; return from interrupt
 
Bankswitch:
  TAX                    ;;copy A into X
  STA Bankvalues, X      ;;new bank to use
  RTS

Bankvalues:
  .db $00, $01, $02, $03 ;;bank numbers

And after assembly, copy and paste it into what I am guessing is free space in the PRG-ROM, (A long consecutive string of $AAs near the end) and as expected, no results.

This is my first time attempting something like this, so I'm pretty sure I'm going about it wrong.

Any advice?

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Adding CHR-bank Swaps to a ROM
« Reply #1 on: May 03, 2014, 10:27:05 am »
You need to actually make the game run the code that you added.

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 7060
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: Adding CHR-bank Swaps to a ROM
« Reply #2 on: May 03, 2014, 12:43:02 pm »
You're not inserting it into the NMI function are you?

Also, you probably shouldn't read $4016/4017 directly. Let NMI do that, most games I've ever looked at will have code there that reads them and stores the results somewhere in RAM: P1 pushed buttons (newly pressed this frame. You probably can't possibly remove your finger fast enough between checks for pressing the button :) so they need to filter that), P1 held buttons, P2 pushed buttons, P2 held buttons. Do a read breakpoint on 4016, and you will likely see it quickly SHIFT (ror/rol) bits into some RAM address.

Then to insert your code, look for the code where the game checks for whatever button is needed to progress is pushed (using RAM mirrors above) and modify that routine.
"My watch says 30 chickens" Google, 2018

M-Tee

  • Hero Member
  • *****
  • Posts: 596
  • One pixel at a time…
    • View Profile
    • M-Tee Retro Graphics
Re: Adding CHR-bank Swaps to a ROM
« Reply #3 on: May 05, 2014, 12:57:41 am »
Thanks, man. I'll dig in and give it a shot.