summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Höglund <firefly@firefly.nu>2014-07-26 11:36:08 +0200
committerJonas Höglund <firefly@firefly.nu>2014-07-26 11:36:08 +0200
commit1add32ab4d4544bb10bcb6c85d7427732c2821c4 (patch)
tree8430c555fc514b87240b33c9d5865a7220326727
parent24fd45099664e2b6dab105cebfea118a1605efc3 (diff)
Home: do palette functions, vblank & timer interrupts
-rwxr-xr-xengine/bank3.asm10
-rwxr-xr-xengine/home.asm138
-rwxr-xr-xengine/music1.asm4
-rwxr-xr-xwram.asm34
4 files changed, 106 insertions, 80 deletions
diff --git a/engine/bank3.asm b/engine/bank3.asm
index e212969..1ff0abf 100755
--- a/engine/bank3.asm
+++ b/engine/bank3.asm
@@ -242,11 +242,11 @@ Func_c1b1: ; c1b1 (3:41b1)
farcall Func_1c82e
farcall Func_131b3
xor a
- ld [wCounter0], a
- ld [wCounter1], a
- ld [wCounter2], a
- ld [wCounter3], a
- ld [wCounter4], a
+ ld [wCounter + 0], a
+ ld [wCounter + 1], a
+ ld [wCounter + 2], a
+ ld [wCounter + 3], a
+ ld [wCounter + 4], a
ret
Func_c1ed: ; c1ed (3:41ed)
diff --git a/engine/home.asm b/engine/home.asm
index 79c52f7..b52e18b 100755
--- a/engine/home.asm
+++ b/engine/home.asm
@@ -56,7 +56,7 @@ Start: ; 0150 (0:0150)
call SetupVRAM
call SetupLCD
call SetupPalettes
- call Func_377f
+ call SetupSound_T
call SetupTimer
call ResetSerial
call CopyDMAFunction
@@ -73,17 +73,18 @@ VBlankHandler: ; 019b (0:019b)
push hl
ld a, [hBankROM]
push af
- ld hl, $caba
+ ld hl, wReentrancyFlag
bit 0, [hl]
- jr nz, .asm_1dd
+ jr nz, .done
set 0, [hl]
- ld a, [$cac0]
+ ld a, [wVBlankOAMCopyToggle]
or a
- jr z, .asm_1b8
- call hDMAFunction
+ jr z, .no_oam_copy
+ call hDMAFunction ; DMA-copy $ca00-$ca9f to OAM memory
xor a
- ld [$cac0], a
-.asm_1b8
+ ld [wVBlankOAMCopyToggle], a
+.no_oam_copy
+ ; flush scaling/windowing parameters
ld a, [hSCX]
ld [rSCX], a
ld a, [hSCY]
@@ -92,16 +93,17 @@ VBlankHandler: ; 019b (0:019b)
ld [rWX], a
ld a, [hWY]
ld [rWY], a
+ ; flush LCDC
ld a, [wLCDC]
ld [rLCDC], a
ei
call $cad0
- call Func_042d
+ call FlushPalettes
ld hl, wVBlankCtr
inc [hl]
- ld hl, $caba
+ ld hl, wReentrancyFlag
res 0, [hl]
-.asm_1dd
+.done
pop af
call BankswitchHome
pop hl
@@ -116,27 +118,31 @@ TimerHandler: ; 01e6 (0:01e6)
push de
push bc
ei
- call Func_0c91
- ld hl, $cac3
+ call SerialTimerHandler
+ ; only trigger every fourth interrupt ≈ 60.24 Hz
+ ld hl, wCounterCtr
ld a, [hl]
inc [hl]
and $3
- jr nz, .asm_217
+ jr nz, .done
+ ; increment the 60-60-60-255-255 counter
call IncrementCounter
- ld hl, $caba
+ ; check in-timer flag
+ ld hl, wReentrancyFlag
bit 1, [hl]
- jr nz, .asm_217
+ jr nz, .done
set 1, [hl]
ld a, [hBankROM]
push af
- ld a, BANK(Func_f4003)
+ ld a, BANK(SoundTimerHandler_Ext)
call BankswitchHome
- call Func_f4003
+ call SoundTimerHandler_Ext
pop af
call BankswitchHome
- ld hl, $caba
+ ; clear in-timer flag
+ ld hl, wReentrancyFlag
res 1, [hl]
-.asm_217
+.done
pop bc
pop de
pop hl
@@ -144,12 +150,11 @@ TimerHandler: ; 01e6 (0:01e6)
reti
; increment timer counter by a tick
-; the counter consists of three fields 0..60 and two fields 0..255
IncrementCounter: ; 021c (0:021c)
ld a, [wCounterEnable]
or a
ret z
- ld hl, wCounter0
+ ld hl, wCounter
inc [hl]
ld a, [hl]
cp 60
@@ -174,15 +179,16 @@ IncrementCounter: ; 021c (0:021c)
inc [hl]
ret
-; setup timer to roughly 60 Hz
+; setup timer to 16384/68 ≈ 240.94 Hz
SetupTimer: ; 0241 (0:0241)
- ld b, $bc
+ ld b, $100 - 68
+ ; ld b, $bc
call CheckForCGB
jr c, .asm_250
ld a, [rKEY1]
and $80
jr z, .asm_250
- ld b, $78
+ ld b, $100 - 2*68
.asm_250
ld a, b
ld [rTMA], a
@@ -225,7 +231,7 @@ EnableLCD: ; 0277 (0:0277)
ld [wLCDC], a ;
ld [rLCDC], a ; turn LCD on
ld a, $c0
- ld [$cabf], a
+ ld [wFlushPaletteFlags], a
ret
; wait for vblank, then turn LCD off
@@ -303,14 +309,14 @@ SetupLCD: ; 030b (0:030b)
ld [hWX], a
ld [hWY], a
xor a
- ld [$caba], a
- ld a, $c3
+ ld [wReentrancyFlag], a
+ ld a, $c3 ; $c3 = jp nn
ld [$cacd], a
- ld [$cad0], a
- ld hl, $cad1
- ld [hl], NopF & $ff
- inc hl
- ld [hl], NopF >> $8
+ ld [wVBlankFunctionTrampoline], a
+ ld hl, wVBlankFunctionTrampoline + 1
+ ld [hl], NopF & $ff ;
+ inc hl ; load `jp NopF`
+ ld [hl], NopF >> $8 ;
ld a, $47
ld [wLCDC], a
ld a, $1
@@ -350,7 +356,7 @@ SetupPalettes: ; 036a (0:036a)
ld [hli], a
ld [hl], a
xor a
- ld [$cabf], a
+ ld [wFlushPaletteFlags], a
ld a, [wConsole]
cp CONSOLE_CGB
ret nz
@@ -367,7 +373,7 @@ SetupPalettes: ; 036a (0:036a)
jr nz, .asm_38c
dec c
jr nz, .asm_387
- call Func_0458
+ call FlushBothCGBPalettes
ret
InitialPalette: ; 0399 (0:0399)
@@ -457,14 +463,14 @@ Func_040c: ; 040c (0:040c)
asm_40f
ld a, $80
asm_411
- ld [$cabf], a
+ ld [wFlushPaletteFlags], a
ld a, [wLCDC]
rla
ret c
push hl
push de
push bc
- call Func_042d
+ call FlushPalettes
pop bc
pop de
pop hl
@@ -478,10 +484,16 @@ Set_OBP1: ; 0428 (0:0428)
ld [wOBP1], a
jr asm_40f
-Func_042d: ; 042d (0:042d)
- ld a, [$cabf]
+; 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.
+FlushPalettes: ; 042d (0:042d)
+ ld a, [wFlushPaletteFlags]
or a
ret z
+ ; flush grayscale (non-CGB) palettes
ld hl, wBGP
ld a, [hli]
ld [rBGP], a
@@ -491,29 +503,33 @@ Func_042d: ; 042d (0:042d)
ld [rOBP1], a
ld a, [wConsole]
cp CONSOLE_CGB
- jr z, asm_44a
-asm_445
+ jr z, flushPaletteCGB
+flushPaletteDone
xor a
- ld [$cabf], a
+ ld [wFlushPaletteFlags], a
ret
-asm_44a
- ld a, [$cabf]
+flushPaletteCGB
+ ; flush BG palette (BGP)
+ ; if bit6 of [wFlushPaletteFlags] is set, flush OBP too
+ ld a, [wFlushPaletteFlags]
bit 6, a
- jr nz, Func_0458
+ jr nz, FlushBothCGBPalettes
ld b, $8
- call InitializePalettes
- jr asm_445
+ call CopyPalette
+ jr flushPaletteDone
-Func_0458: ; 0458 (0:0458)
+FlushBothCGBPalettes: ; 0458 (0:0458)
xor a
ld b, $40
- call InitializePalettes
+ ; flush BGP $00-$1f
+ call CopyPalette
ld a, $8
ld b, $40
- call InitializePalettes
- jr asm_445
+ ; flush OBP $00-$1f
+ call CopyPalette
+ jr flushPaletteDone
-InitializePalettes: ; 0467 (0:0467)
+CopyPalette: ; 0467 (0:0467)
add a
add a
add a
@@ -559,10 +575,10 @@ Func_04a2: ; 04a2 (0:04a2)
ld a, [wConsole]
cp CONSOLE_SGB
ret nz
- call EnableLCD
- ld hl, SGB_04bf
- call SendSGB
- call DisableLCD
+ call EnableLCD ;
+ ld hl, SGB_04bf ; send SGB data
+ call SendSGB ;
+ call DisableLCD ;
ret
SGB_04bf: ; 04bf (0:04bf)
@@ -1061,7 +1077,7 @@ SetupExtRAM: ; 080b (0:080b)
Func_084d: ; 084d (0:084d)
ld a, $3
.asm_84f
- call Func_0863
+ call ClearExtRAMBank
dec a
cp $ff
jr nz, .asm_84f
@@ -1073,7 +1089,7 @@ Func_084d: ; 084d (0:084d)
ld [hl], $5
ret
-Func_0863: ; 0863 (0:0863)
+ClearExtRAMBank: ; 0863 (0:0863)
push af
call BankswitchRAM
call EnableExtRAM
@@ -1580,7 +1596,7 @@ MemcpyDEHL_hblank: ; 0c32 (0:0c32)
INCBIN "baserom.gbc",$0c4b,$0c91 - $0c4b
-Func_0c91: ; 0c91 (0:0c91)
+SerialTimerHandler: ; 0c91 (0:0c91)
ld a, [$cb74]
cp $29
jr z, .asm_c9d
@@ -3613,8 +3629,8 @@ Func_3212: ; 3212 (0:3212)
INCBIN "baserom.gbc",$321d,$377f - $321d
-Func_377f: ; 377f (0:377f)
- farcall Func_f4000
+SetupSound_T: ; 377f (0:377f)
+ farcall SetupSound_Ext
ret
Func_3784: ; 3784 (0:3784)
diff --git a/engine/music1.asm b/engine/music1.asm
index 22392bb..21b7007 100755
--- a/engine/music1.asm
+++ b/engine/music1.asm
@@ -1,7 +1,7 @@
-Func_f4000: ; f4000 (3d:4000)
+SetupSound_Ext: ; f4000 (3d:4000)
jp Func_f407d
-Func_f4003: ; f4003 (3d:4003)
+SoundTimerHandler_Ext: ; f4003 (3d:4003)
jp Func_f40e9
Func_f4006: ; f4006 (3d:4006)
diff --git a/wram.asm b/wram.asm
index 12f66a7..05f2c24 100755
--- a/wram.asm
+++ b/wram.asm
@@ -6,7 +6,8 @@ wInitialA: ; cab3
ds $1
wConsole: ; cab4
- ds $2
+ ds $1
+ ds $1
wTileMapFill: ; cab6
ds $1
@@ -15,7 +16,13 @@ wIE: ; cab7
ds $1
wVBlankCtr: ; cab8
- ds $3
+ ds $1
+ ds $1
+
+; bit0: is in vblank interrupt?
+; bit1: is in timer interrupt?
+wReentrancyFlag: ; caba
+ ds $1
wLCDC: ; cabb
ds $1
@@ -27,28 +34,31 @@ wOBP0: ; cabd
ds $1
wOBP1: ; cabe
- ds $6
-
-wCounterEnable: ; cac4
ds $1
-wCounter0: ; cac5
+wFlushPaletteFlags: ; cabf
ds $1
-wCounter1: ; cac6
+wVBlankOAMCopyToggle: ; cac0
ds $1
+ ds $2
-wCounter2: ; cac7
+wCounterCtr: ; cac3
ds $1
-wCounter3: ; cac8
+wCounterEnable: ; cac4
ds $1
-wCounter4: ; cac9
- ds $27
+wCounter: ; cac5
+ ds $5
+ ds $6
+
+wVBlankFunctionTrampoline: ; cad0
+ ds $20 ; unknown length
wBufPalette: ; caf0
- ds $84
+ ds $80
+ ds $4
wBufSerial: ; cb74
ds $1