summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorxCrystal <rgr.crystal@gmail.com>2018-02-16 17:49:37 +0100
committerxCrystal <rgr.crystal@gmail.com>2018-02-16 17:49:37 +0100
commitcdd2d4eac475082078cf4a266dd6429d0d8c6379 (patch)
treee88f997be7e13dcbebd507fea8132880cc412201 /src
parent1082494d9886e99f2eab1d924ec4091a1a902055 (diff)
Correct palette engine functions and more clean up
Diffstat (limited to 'src')
-rw-r--r--src/constants/misc_constants.asm3
-rwxr-xr-xsrc/engine/bank1c.asm4
-rwxr-xr-xsrc/engine/bank3.asm8
-rwxr-xr-xsrc/engine/bank4.asm6
-rw-r--r--src/engine/effect_functions.asm12
-rwxr-xr-xsrc/engine/home.asm168
-rwxr-xr-xsrc/wram.asm8
7 files changed, 117 insertions, 92 deletions
diff --git a/src/constants/misc_constants.asm b/src/constants/misc_constants.asm
index 422fda4..18bf014 100644
--- a/src/constants/misc_constants.asm
+++ b/src/constants/misc_constants.asm
@@ -6,6 +6,9 @@ SCREEN_HEIGHT EQU 18 ; tiles
BG_MAP_WIDTH EQU 32 ; tiles
BG_MAP_HEIGHT EQU 32 ; tiles
+; palettes
+CGB_PAL_SIZE EQU 8
+
; console types (for wConsole)
CONSOLE_DMG EQU $00
CONSOLE_SGB EQU $01
diff --git a/src/engine/bank1c.asm b/src/engine/bank1c.asm
index c7ae122..60dfb26 100755
--- a/src/engine/bank1c.asm
+++ b/src/engine/bank1c.asm
@@ -164,7 +164,7 @@ Func_70136: ; 70136 (1c:4136)
ld [wLCDC], a
ld a, $e4
ld [rBGP], a
- call Func_040c
+ call SetBGP
xor a
ldh [hSCX], a
ld [rSCX], a
@@ -188,7 +188,7 @@ Func_70177: ; 70177 (1c:4177)
ld a, [wd41e]
ldh [hSCY], a
ld a, [wd41f]
- call Func_040c
+ call SetBGP
ld a, [wd420]
ld [wLCDC], a
call DisableLCD
diff --git a/src/engine/bank3.asm b/src/engine/bank3.asm
index f591863..dd8638b 100755
--- a/src/engine/bank3.asm
+++ b/src/engine/bank3.asm
@@ -219,11 +219,11 @@ Func_c1a0: ; c1a0 (3:41a0)
Func_c1a4: ; c1a4 (3:41a4)
xor a
- call Func_040c
+ call SetBGP
xor a
- call Set_OBP0
+ call SetOBP0
xor a
- call Set_OBP1
+ call SetOBP1
ret
Func_c1b1: ; c1b1 (3:41b1)
@@ -452,7 +452,7 @@ Func_c34e: ; c34e (3:434e)
ld de, $cb30
ld bc, $0040
call CopyDataHLtoDE_SaveRegisters
- call Func_0404
+ call SetFlushAllPalettes
ret
Func_c36a: ; c36a (3:436a)
diff --git a/src/engine/bank4.asm b/src/engine/bank4.asm
index 29d96fc..757c670 100755
--- a/src/engine/bank4.asm
+++ b/src/engine/bank4.asm
@@ -31,11 +31,11 @@ Func_10031: ; 10031 (4:4031)
call $4cbb
call DisableExtRAM
call $4b28
- call Func_0404
+ call SetFlushAllPalettes
call EnableLCD
call DoFrameIfLCDEnabled
call $4cea
- call Func_0404
+ call SetFlushAllPalettes
pop af
call BankswitchRAM
call DisableExtRAM
@@ -1094,7 +1094,7 @@ Func_126d1: ; 126d1 (4:66d1)
scf
ret
-PointerTable_126fc
+PointerTable_126fc:
dw CardPop_12768
dw Func_12741
dw Func_12704
diff --git a/src/engine/effect_functions.asm b/src/engine/effect_functions.asm
index 5b9d6ef..88a8361 100644
--- a/src/engine/effect_functions.asm
+++ b/src/engine/effect_functions.asm
@@ -5,17 +5,17 @@ Poison50PercentEffect: ; 2c000 (b:4000)
PoisonEffect: ; 2c007 (b:4007)
lb bc, $0f, POISONED
- jr applyEffect
+ jr ApplyStatusEffect
lb bc, $0f, DOUBLE_POISONED
- jr applyEffect
+ jr ApplyStatusEffect
Paralysis50PercentEffect: ; 2c011 (b:4011)
ldtx de, ParalysisCheckText
call TossCoin_BankB
ret nc
lb bc, $f0, PARALYZED
- jr applyEffect
+ jr ApplyStatusEffect
Confusion50PercentEffect: ; 2c01d (b:401d)
ldtx de, ConfusionCheckText
@@ -24,7 +24,7 @@ Confusion50PercentEffect: ; 2c01d (b:401d)
ConfusionEffect: ; 2c024 (b:4024)
lb bc, $f0, CONFUSED
- jr applyEffect
+ jr ApplyStatusEffect
ldtx de, SleepCheckText
call TossCoin_BankB
@@ -32,9 +32,9 @@ ConfusionEffect: ; 2c024 (b:4024)
SleepEffect: ; 2c030 (b:4030)
lb bc, $f0, ASLEEP
- jr applyEffect
+ jr ApplyStatusEffect
-applyEffect
+ApplyStatusEffect:
ldh a, [hWhoseTurn]
ld hl, wcc05
cp [hl]
diff --git a/src/engine/home.asm b/src/engine/home.asm
index b572b07..799d476 100755
--- a/src/engine/home.asm
+++ b/src/engine/home.asm
@@ -379,30 +379,30 @@ SetupPalettes: ; 036a (0:036a)
ld hl, wBGP
ld a, %11100100
ld [rBGP], a
- ld [hli], a
+ ld [hli], a ; wBGP
ld [rOBP0], a
ld [rOBP1], a
- ld [hli], a
- ld [hl], a
+ ld [hli], a ; wOBP0
+ ld [hl], a ; wOBP1
xor a
ld [wFlushPaletteFlags], a
ld a, [wConsole]
cp CONSOLE_CGB
ret nz
- ld de, wBufPalette
- ld c, $10
-.asm_387
+ ld de, wBackgroundPalettesCGB
+ ld c, 16
+.copy_pals_loop
ld hl, InitialPalette
- ld b, $8
-.asm_38c
+ ld b, CGB_PAL_SIZE
+.copy_bytes_loop
ld a, [hli]
ld [de], a
inc de
dec b
- jr nz, .asm_38c
+ jr nz, .copy_bytes_loop
dec c
- jr nz, .asm_387
- call FlushBothCGBPalettes
+ jr nz, .copy_pals_loop
+ call FlushAllCGBPalettes
ret
InitialPalette: ; 0399 (0:0399)
@@ -479,19 +479,25 @@ ZeroRAM: ; 03ec (0:03ec)
jr nz, .zero_hram_loop
ret
-Func_0404: ; 0404 (0:0404)
+; Flush all non-CGB and CGB palettes
+SetFlushAllPalettes: ; 0404 (0:0404)
ld a, $c0
- jr asm_411
+ jr SetFlushPalettes
-Func_0408: ; 0408 (0:0408)
+; Flush non-CGB palettes and a single CGB palette,
+; provided in a as an index between 0-7 (BGP) or 8-15 (OBP)
+SetFlushPalette: ; 0408 (0:0408)
or $80
- jr asm_411
+ jr SetFlushPalettes
-Func_040c: ; 040c (0:040c)
+; Set wBGP to the specified value, flush non-CGB palettes, and the first CGB palette.
+SetBGP: ; 040c (0:040c)
ld [wBGP], a
-asm_40f
+
+SetFlushPalette0:
ld a, $80
-asm_411
+
+SetFlushPalettes:
ld [wFlushPaletteFlags], a
ld a, [wLCDC]
rla
@@ -505,19 +511,21 @@ asm_411
pop hl
ret
-Set_OBP0: ; 0423 (0:0423)
+; Set wOBP0 to the specified value, flush non-CGB palettes, and the first CGB palette.
+SetOBP0: ; 0423 (0:0423)
ld [wOBP0], a
- jr asm_40f
+ jr SetFlushPalette0
-Set_OBP1: ; 0428 (0:0428)
+; Set wOBP1 to the specified value, flush non-CGB palettes, and the first CGB palette.
+SetOBP1: ; 0428 (0:0428)
ld [wOBP1], a
- jr asm_40f
+ jr SetFlushPalette0
-; flushes non-CGB palettes from [wBGP], [wOBP0], [wOBP1] as well as CGB
-; palettes from [wBufPalette..wBufPalette+$1f] (BG palette) and
-; [wBufPalette+$20..wBufPalette+$3f] (sprite palette).
-; only flushes if [wFlushPaletteFlags] is nonzero, and only flushes sprite
-; palette if bit6 of that location is set.
+; Flushes non-CGB palettes from [wBGP], [wOBP0], [wOBP1] as well as CGB
+; palettes from [wBackgroundPalettesCGB..wBackgroundPalettesCGB+$3f] (BG palette)
+; and [wObjectPalettesCGB+$00..wObjectPalettesCGB+$3f] (sprite palette).
+; Only flushes if [wFlushPaletteFlags] is nonzero, and only flushes
+; a single CGB palette if bit6 of that location is reset.
FlushPalettes: ; 042d (0:042d)
ld a, [wFlushPaletteFlags]
or a
@@ -532,65 +540,67 @@ FlushPalettes: ; 042d (0:042d)
ld [rOBP1], a
ld a, [wConsole]
cp CONSOLE_CGB
- jr z, flushPaletteCGB
-flushPaletteDone
+ jr z, .CGB
+.done
xor a
ld [wFlushPaletteFlags], a
ret
-flushPaletteCGB
- ; flush BG palette (BGP)
- ; if bit6 of [wFlushPaletteFlags] is set, flush OBP too
+.CGB
+ ; flush a single CGB BG or OB palette
+ ; if bit6 of [wFlushPaletteFlags] is set, flush all 16 of them
ld a, [wFlushPaletteFlags]
bit 6, a
- jr nz, FlushBothCGBPalettes
- ld b, $8
- call CopyPalette
- jr flushPaletteDone
+ jr nz, FlushAllCGBPalettes
+ ld b, CGB_PAL_SIZE
+ call CopyCGBPalettes
+ jr .done
-FlushBothCGBPalettes: ; 0458 (0:0458)
+FlushAllCGBPalettes: ; 0458 (0:0458)
+ ; flush 8 BGP palettes
xor a
- ld b, $40
- ; flush BGP $00-$1f
- call CopyPalette
- ld a, $8
- ld b, $40
- ; flush OBP $00-$1f
- call CopyPalette
- jr flushPaletteDone
-
-CopyPalette: ; 0467 (0:0467)
+ ld b, 8 * CGB_PAL_SIZE
+ call CopyCGBPalettes
+ ; flush 8 OBP palettes
+ ld a, CGB_PAL_SIZE
+ ld b, 8 * CGB_PAL_SIZE
+ call CopyCGBPalettes
+ jr FlushPalettes.done
+
+; copy b bytes of CGB palette data starting at
+; wBackgroundPalettesCGB + a * CGB_PAL_SIZE into rBGPD or rOGPD.
+CopyCGBPalettes: ; 0467 (0:0467)
add a
add a
add a
ld e, a
ld d, $0
- ld hl, wBufPalette
+ ld hl, wBackgroundPalettesCGB
add hl, de
- ld c, $68
- bit 6, a
- jr z, .asm_479
- ld c, $6a
-.asm_479
- and $bf
+ ld c, LOW(rBGPI)
+ bit 6, a ; was a between 0-7 (BGP), or between 8-15 (OBP)?
+ jr z, .copy
+ ld c, LOW(rOBPI)
+.copy
+ and %10111111
ld e, a
-.asm_47c
+.next_byte
ld a, e
ld [$ff00+c], a
inc c
-.asm_47f
+.wait
ld a, [rSTAT]
and $2
- jr nz, .asm_47f
+ jr nz, .wait
ld a, [hl]
ld [$ff00+c], a
ld a, [$ff00+c]
cp [hl]
- jr nz, .asm_47f
+ jr nz, .wait
inc hl
dec c
inc e
dec b
- jr nz, .asm_47c
+ jr nz, .next_byte
ret
Func_0492: ; 0492 (0:0492)
@@ -644,7 +654,6 @@ BCCoordToBGMap0Address: ; 04cf (0:04cf)
ld d, h
ret
-; read joypad
ReadJoypad: ; 04de (0:04de)
ld a, $20
ld [rJOYP], a
@@ -665,7 +674,7 @@ ReadJoypad: ; 04de (0:04de)
cpl
and $f
or b
- ld c, a ; joypad data
+ ld c, a ; joypad data
cpl
ld b, a
ldh a, [hButtonsHeld]
@@ -680,13 +689,17 @@ ReadJoypad: ; 04de (0:04de)
ldh a, [hButtonsHeld]
and BUTTONS
cp BUTTONS
- jr nz, asm_522 ; handle reset
+ jr nz, ReadJoypad_SaveButtonsHeld
+ ; A + B + Start + Select: reset game
call ResetSerial
+; fallthrough
+
Reset: ; 051b (0:051b)
ld a, [wInitialA]
di
jp Start
-asm_522
+
+ReadJoypad_SaveButtonsHeld:
ld a, c
ldh [hButtonsHeld], a
ld a, $30
@@ -819,7 +832,7 @@ CallIndirect: ; 05b6 (0:05b6)
ld l, [hl]
ld h, a
pop af
- ; fallthrough
+; fallthrough
CallHL: ; 05c1 (0:05c1)
jp hl
; 0x5c2
@@ -1590,7 +1603,7 @@ RST18: ; 09ae (0:09ae)
ld a, [de]
ld [hl], a
ld a, $1
- ; fallthrough
+; fallthrough
Func_09ce: ; 09ce (0:09ce)
call BankswitchHome
ld hl, sp+$d
@@ -2263,7 +2276,7 @@ ResetSerial: ; 0ea6 (0:0ea6)
xor a
ld [rSB], a
ld [rSC], a
- ; fallthrough
+; fallthrough
ClearSerialData: ; 0eb1 (0:0eb1)
ld hl, wSerialOp
ld bc, $0051
@@ -3668,12 +3681,12 @@ SwapTurn: ; 1c72 (0:1c72)
PrintPlayerName: ; 1c7d (0:1c7d)
call EnableExtRAM
ld hl, $a010
-printNameLoop
+.loop
ld a, [hli]
ld [de], a
inc de
or a
- jr nz, printNameLoop
+ jr nz, .loop
dec de
call DisableExtRAM
ret
@@ -3692,7 +3705,7 @@ PrintOpponentName: ; 1c8e (0:1c8e)
ld a, [hl]
or a
jr z, .print_player2
- jr printNameLoop
+ jr PrintPlayerName.loop
.print_player2
ldtx hl, Player2Text
jp PrintTextBoxBorderLabel
@@ -4049,7 +4062,9 @@ DrawRegularTextBoxDMG: ; 1e88 (0:1e88)
ld a, $1c
lb de, $18, $19
call CopyLine
-ContinueDrawingTextBoxDMGorSGB
+; fallthrough
+
+ContinueDrawingTextBoxDMGorSGB:
dec c
dec c
.draw_text_box_body_loop
@@ -4105,7 +4120,9 @@ DrawRegularTextBoxCGB:
ld a, $1c
lb de, $18, $19
call CopyCurrentLineTilesAndAttrCGB
-ContinueDrawingTextBoxCGB
+; fallthrough
+
+ContinueDrawingTextBoxCGB:
dec c
dec c
.draw_text_box_body_loop
@@ -4137,7 +4154,8 @@ CopyCurrentLineTilesAndAttrCGB: ; 1efb (0:1efb)
push hl
call CopyLine
pop hl
-CopyCurrentLineAttrCGB
+; fallthrough
+CopyCurrentLineAttrCGB:
call BankswitchVRAM_1
ld a, [wFrameType] ; on CGB, wFrameType determines the palette and the other attributes
ld e, a
@@ -6110,7 +6128,7 @@ LoadCardGfx: ; 2fa0 (0:2fa0)
res 7, h
set 6, h
call CopyGfxData
- ld b, $8 ; length of palette
+ ld b, CGB_PAL_SIZE
ld de, $ce23
.copy_card_palette
ld a, [hli]
@@ -6565,7 +6583,7 @@ Func_31fc: ; 31fc (0:31fc)
adc [hl]
ld [hl], a
ld a, e
- ; fallthrough
+; fallthrough
Func_3212: ; 3212 (0:3212)
ld [rSB], a
ld a, $1
@@ -8019,7 +8037,7 @@ Func_3df3: ; 3df3 (0:3df3)
ld hl, sp+$5
ld a, [hl]
call Func_12c7f
- call Func_0404
+ call SetFlushAllPalettes
pop hl
pop af
call BankswitchHome
diff --git a/src/wram.asm b/src/wram.asm
index 4431587..0a92ff2 100755
--- a/src/wram.asm
+++ b/src/wram.asm
@@ -355,8 +355,12 @@ wcade:: ; cade
wcae2:: ; cae2
ds $e
-wBufPalette:: ; caf0
- ds $80
+; temporal CGB palette data buffer to eventually save into BGPD or OBPD registers.
+wBackgroundPalettesCGB:: ; caf0
+ ds 8 * CGB_PAL_SIZE
+
+wObjectPalettesCGB:: ; cb30
+ ds 8 * CGB_PAL_SIZE
ds $4