Skip to content

Commit

Permalink
Improve documentation of some SRAM routines (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
SatoMew authored Jun 27, 2024
1 parent b135272 commit 57bec62
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 71 deletions.
5 changes: 3 additions & 2 deletions constants/hardware_constants.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ DEF MBC1RomBank EQU $2000
DEF MBC1SRamBank EQU $4000
DEF MBC1SRamBankingMode EQU $6000

DEF SRAM_DISABLE EQU $00
DEF SRAM_ENABLE EQU $0a
DEF SRAM_DISABLE EQU $00
DEF SRAM_BANKING_MODE EQU $01
DEF SRAM_ENABLE EQU $0a

DEF NUM_SRAM_BANKS EQU 4

Expand Down
4 changes: 2 additions & 2 deletions engine/battle/core.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6555,14 +6555,14 @@ LoadPlayerBackPic:
ld de, vBackPic
call InterlaceMergeSpriteBuffers
ld a, $0
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
ld hl, vSprites
ld de, sSpriteBuffer1
ldh a, [hLoadedROMBank]
ld b, a
ld c, 7 * 7
call CopyVideoData
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ld a, $31
ldh [hStartTileID], a
hlcoord 1, 5
Expand Down
4 changes: 2 additions & 2 deletions engine/battle/scale_sprites.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
; resulting in a 7*7 tile output sprite chunk
ScaleSpriteByTwo:
ld a, $0
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
call .ScaleSpriteByTwo
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ret

.ScaleSpriteByTwo:
Expand Down
12 changes: 6 additions & 6 deletions engine/debug/debug_menu.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ Data_ff2eb:

Func_ff2f3:
push hl
call Func_ff316
call DebugEnableSRAM
ld a, b
ld [MBC1SRamBank], a
ld bc, sBox2 - sBox1
Expand All @@ -1615,18 +1615,18 @@ Func_ff2f3:
ld bc, sBox5 - sBox1 + 1
call Func_ff32a
ld [sBox5], a
call Func_ff321
call DebugDisableSRAM
ret

Func_ff316:
ld a, 1
DebugEnableSRAM: ; duplicate of EnableSRAM
ld a, SRAM_BANKING_MODE
ld [MBC1SRamBankingMode], a
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
ret

Func_ff321:
ld a, 0
DebugDisableSRAM: ; duplicate of DisableSRAM
ld a, SRAM_DISABLE
ld [MBC1SRamBankingMode], a
ld [MBC1SRamEnable], a
ret
Expand Down
7 changes: 2 additions & 5 deletions engine/menus/main_menu.asm
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,11 @@ DisplayOptionMenu:
ret

CheckForPlayerNameInSRAM:
; Check if the player name data in SRAM has a string terminator character
; (indicating that a name may have been saved there) and return whether it does
; in carry.
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
ld a, $1
ld a, SRAM_BANKING_MODE
ld [MBC1SRamBankingMode], a
assert SRAM_BANKING_MODE == BANK("Save Data")
ld [MBC1SRamBank], a
ld b, NAME_LENGTH
ld hl, sPlayerName
Expand All @@ -292,7 +290,6 @@ CheckForPlayerNameInSRAM:
jr z, .found
dec b
jr nz, .loop
; not found
xor a
ld [MBC1SRamEnable], a
ld [MBC1SRamBankingMode], a
Expand Down
72 changes: 36 additions & 36 deletions engine/menus/save.asm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ FileDataDestroyedText:
text_end

LoadSAV0:
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
; This vc_hook does not have to be in any particular location.
; It is defined here because it refers to the same labels as the two lines below.
Expand Down Expand Up @@ -80,8 +80,8 @@ LoadSAV0:
jp SAVGoodChecksum

LoadSAV1:
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
Expand All @@ -98,8 +98,8 @@ LoadSAV1:
jp SAVGoodChecksum

LoadSAV2:
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
Expand All @@ -123,7 +123,7 @@ SAVBadCheckSum:
scf

SAVGoodChecksum:
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

LoadSAVIgnoreBadCheckSum:
Expand Down Expand Up @@ -195,8 +195,8 @@ OlderFileWillBeErasedText:
text_end

SaveSAVtoSRAM0:
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld hl, wPlayerName
ld de, sPlayerName
Expand All @@ -220,13 +220,13 @@ SaveSAVtoSRAM0:
ld bc, sGameDataEnd - sGameData
call SAVCheckSum
ld [sMainDataCheckSum], a
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

SaveSAVtoSRAM1:
; stored pokémon
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld hl, wBoxDataStart
ld de, sCurBoxData
Expand All @@ -236,12 +236,12 @@ SaveSAVtoSRAM1:
ld bc, sGameDataEnd - sGameData
call SAVCheckSum
ld [sMainDataCheckSum], a
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

SaveSAVtoSRAM2:
call EnableSRAMAndLatchClockData
ld a, $1
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld hl, wPartyDataStart
ld de, sPartyData
Expand All @@ -262,7 +262,7 @@ SaveSAVtoSRAM2:
ld bc, sGameDataEnd - sGameData
call SAVCheckSum
ld [sMainDataCheckSum], a
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

SaveSAVtoSRAM::
Expand Down Expand Up @@ -386,7 +386,7 @@ WhenYouChangeBoxText:
CopyBoxToOrFromSRAM:
; copy an entire box from hl to de with b as the SRAM bank
push hl
call EnableSRAMAndLatchClockData
call EnableSRAM
ld a, b
ld [MBC1SRamBank], a
ld bc, wBoxDataEnd - wBoxDataStart
Expand All @@ -404,7 +404,7 @@ CopyBoxToOrFromSRAM:
call SAVCheckSum
ld [sBank2AllBoxesChecksum], a ; sBank3AllBoxesChecksum
call CalcIndividualBoxCheckSums
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

DisplayChangeBoxMenu:
Expand Down Expand Up @@ -500,14 +500,14 @@ BoxNoText:
EmptyAllSRAMBoxes:
; marks all boxes in SRAM as empty (initialisation for the first time the
; player changes the box)
call EnableSRAMAndLatchClockData
ld a, 2
call EnableSRAM
ld a, BANK("Saved Boxes 1")
ld [MBC1SRamBank], a
call EmptySRAMBoxesInBank
ld a, 3
ld a, BANK("Saved Boxes 2")
ld [MBC1SRamBank], a
call EmptySRAMBoxesInBank
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

EmptySRAMBoxesInBank:
Expand Down Expand Up @@ -541,14 +541,14 @@ EmptySRAMBox:
GetMonCountsForAllBoxes:
ld hl, wBoxMonCounts
push hl
call EnableSRAMAndLatchClockData
ld a, $2
call EnableSRAM
ld a, BANK("Saved Boxes 1")
ld [MBC1SRamBank], a
call GetMonCountsForBoxesInBank
ld a, $3
ld a, BANK("Saved Boxes 2")
ld [MBC1SRamBank], a
call GetMonCountsForBoxesInBank
call DisableSRAMAndPrepareClockData
call DisableSRAM
pop hl

; copy the count for the current box from WRAM
Expand Down Expand Up @@ -581,8 +581,8 @@ SAVCheckRandomID:
; checks if Sav file is the same by checking player's name 1st letter
; and the two random numbers generated at game beginning
; (which are stored at wPlayerID)s
call EnableSRAMAndLatchClockData
ld a, $01
call EnableSRAM
ld a, BANK("Save Data")
ld [MBC1SRamBank], a
ld a, [sPlayerName]
and a
Expand All @@ -604,7 +604,7 @@ SAVCheckRandomID:
ld a, [wPlayerID + 1]
cp h
.next
ld a, $00
ld a, SRAM_DISABLE
ld [MBC1SRamBankingMode], a
ld [MBC1SRamEnable], a
ret
Expand Down Expand Up @@ -645,23 +645,23 @@ LoadHallOfFameTeams:
; fallthrough

HallOfFame_Copy:
call EnableSRAMAndLatchClockData
call EnableSRAM
xor a
ld [MBC1SRamBank], a
call CopyData
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

ClearSAV:
call EnableSRAMAndLatchClockData
call EnableSRAM
ld a, $4
.loop
dec a
push af
call PadSRAM_FF
pop af
jr nz, .loop
call DisableSRAMAndPrepareClockData
call DisableSRAM
ret

PadSRAM_FF:
Expand All @@ -671,14 +671,14 @@ PadSRAM_FF:
ld a, $ff
jp FillMemory

EnableSRAMAndLatchClockData:
ld a, $1
EnableSRAM:
ld a, SRAM_BANKING_MODE
ld [MBC1SRamBankingMode], a
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
ret

DisableSRAMAndPrepareClockData:
DisableSRAM:
ld a, SRAM_DISABLE
ld [MBC1SRamBankingMode], a
ld [MBC1SRamEnable], a
Expand Down
4 changes: 2 additions & 2 deletions engine/movie/hall_of_fame.asm
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ HoFLoadPlayerPics:
ld a, BANK(RedPicFront)
call UncompressSpriteFromDE
ld a, $0
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
ld hl, sSpriteBuffer1
ld de, sSpriteBuffer0
ld bc, $310
call CopyData
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ld de, vFrontPic
call InterlaceMergeSpriteBuffers
ld de, RedPicBack
Expand Down
4 changes: 2 additions & 2 deletions engine/movie/oak_speech/oak_speech.asm
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ IntroDisplayPicCenteredOrUpperRight:
ld a, b
call UncompressSpriteFromDE
ld a, $0
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
ld hl, sSpriteBuffer1
ld de, sSpriteBuffer0
ld bc, $310
call CopyData
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ld de, vFrontPic
call InterlaceMergeSpriteBuffers
pop bc
Expand Down
4 changes: 2 additions & 2 deletions engine/movie/title.asm
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ IncrementResetCounter:

FillSpriteBuffer0WithAA:
xor a
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
ld hl, sSpriteBuffer0
ld bc, $20
ld a, $aa
call FillMemory
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ret
4 changes: 2 additions & 2 deletions engine/pikachu/pikachu_pic_animation.asm
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,12 @@ DecompressRequestPikaPicAnimGFX:
ld a, b
call UncompressSpriteFromDE
ld a, BANK(sSpriteBuffer1)
call SwitchSRAMBankAndLatchClockData
call OpenSRAM
ld hl, sSpriteBuffer1
ld de, sSpriteBuffer0
ld bc, SPRITEBUFFERSIZE * 2
call CopyData
call PrepareRTCDataAndDisableSRAM
call CloseSRAM
ld a, [wPikaPicAnimCurGraphicID]
call LookUpTileOffsetForCurrentPikaPicAnimGFX
call GetPikaPicVRAMAddressForNewGFX
Expand Down
8 changes: 4 additions & 4 deletions home/bankswitch2.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ Bankswitch::
JumpToAddress::
jp hl

SwitchSRAMBankAndLatchClockData::
OpenSRAM::
push af
ld a, $1
ld a, SRAM_BANKING_MODE
ld [MBC1SRamBankingMode], a
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
pop af
ld [MBC1SRamBank], a
ret

PrepareRTCDataAndDisableSRAM::
CloseSRAM::
push af
ld a, $0
ld a, SRAM_DISABLE
ld [MBC1SRamBankingMode], a
ld [MBC1SRamEnable], a
pop af
Expand Down
Loading

0 comments on commit 57bec62

Please sign in to comment.