Merge pull request #114 from yenatch/master

Split out files and write comments.
This commit is contained in:
Bryan Bishop 2013-02-22 17:51:43 -08:00
commit 75a4b16a18
13 changed files with 810 additions and 670 deletions

View File

@ -16,27 +16,27 @@ SoundRestart: ; e8000
push bc push bc
push af push af
call MusicOff call MusicOff
ld hl, $ff24 ; channel control registers ld hl, rNR50 ; channel control registers
xor a xor a
ld [hli], a ; ff24 ; volume/vin ld [hli], a ; rNR50 ; volume/vin
ld [hli], a ; ff25 ; sfx channels ld [hli], a ; rNR51 ; sfx channels
ld a, $80 ; all channels on ld a, $80 ; all channels on
ld [hli], a ; ff26 ; music channels ld [hli], a ; ff26 ; music channels
ld hl, $ff10 ; sound channel registers ld hl, rNR10 ; sound channel registers
ld e, $04 ; number of channels ld e, $04 ; number of channels
.clearsound .clearsound
; sound channel 1 2 3 4 ; sound channel 1 2 3 4
xor a xor a
ld [hli], a ; $ff10, $ff15, $ff1a, $ff1f ; sweep = 0 ld [hli], a ; rNR10, $ff15, rNR30, $ff1f ; sweep = 0
ld [hli], a ; $ff11, $ff16, $ff1b, $ff20 ; length/wavepattern = 0 ld [hli], a ; rNR11, rNR21, rNR31, rNR41 ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [hli], a ; $ff12, $ff17, $ff1c, $ff21 ; envelope = 0 ld [hli], a ; rNR12, rNR22, rNR32, rNR42 ; envelope = 0
xor a xor a
ld [hli], a ; $ff13, $ff18, $ff1d, $ff22 ; frequency lo = 0 ld [hli], a ; rNR13, rNR23, rNR33, rNR43 ; frequency lo = 0
ld a, $80 ld a, $80
ld [hli], a ; $ff14, $ff19, $ff1e, $ff23 ; restart sound (freq hi = 0) ld [hli], a ; rNR14, rNR24, rNR34, rNR44 ; restart sound (freq hi = 0)
dec e dec e
jr nz, .clearsound jr nz, .clearsound
@ -208,10 +208,10 @@ UpdateSound: ; e805c
call FadeMusic call FadeMusic
; write volume to hardware register ; write volume to hardware register
ld a, [Volume] ld a, [Volume]
ld [$ff24], a ld [rNR50], a
; write SO on/off to hardware register ; write SO on/off to hardware register
ld a, [SoundOutput] ld a, [SoundOutput]
ld [$ff25], a ld [rNR51], a
ret ret
; e8125 ; e8125
@ -251,7 +251,7 @@ UpdateChannels: ; e8125
jr z, .asm_e8159 jr z, .asm_e8159
; ;
ld a, [SoundInput] ld a, [SoundInput]
ld [$ff10], a ld [rNR10], a
.asm_e8159 .asm_e8159
bit 5, [hl] ; rest bit 5, [hl] ; rest
jr nz, .ch1rest jr nz, .ch1rest
@ -264,48 +264,48 @@ UpdateChannels: ; e8125
jr .asm_e8175 jr .asm_e8175
.asm_e816b .asm_e816b
ld a, [$c294] ld a, [$c294]
ld [$ff13], a ld [rNR13], a
ld a, [$c295] ld a, [$c295]
ld [$ff14], a ld [rNR14], a
.asm_e8175 .asm_e8175
bit 0, [hl] bit 0, [hl]
ret z ret z
ld a, [$c292] ld a, [$c292]
ld d, a ld d, a
ld a, [$ff11] ld a, [rNR11]
and a, $3f ; sound length and a, $3f ; sound length
or d or d
ld [$ff11], a ld [rNR11], a
ret ret
.asm_e8184 .asm_e8184
ld a, [$c292] ld a, [$c292]
ld d, a ld d, a
ld a, [$ff11] ld a, [rNR11]
and a, $3f ; sound length and a, $3f ; sound length
or d or d
ld [$ff11], a ld [rNR11], a
ld a, [$c294] ld a, [$c294]
ld [$ff13], a ld [rNR13], a
ret ret
.ch1rest .ch1rest
ld a, [$ff26] ld a, [rNR52]
and a, %10001110 ; ch1 off and a, %10001110 ; ch1 off
ld [$ff26], a ld [rNR52], a
ld hl, $ff10 ld hl, rNR10
call ClearChannel call ClearChannel
ret ret
.asm_e81a2 .asm_e81a2
ld hl, $c292 ld hl, $c292
ld a, $3f ; sound length ld a, $3f ; sound length
or [hl] or [hl]
ld [$ff11], a ld [rNR11], a
ld a, [$c293] ld a, [$c293]
ld [$ff12], a ld [rNR12], a
ld a, [$c294] ld a, [$c294]
ld [$ff13], a ld [rNR13], a
ld a, [$c295] ld a, [$c295]
or a, $80 or a, $80
ld [$ff14], a ld [rNR14], a
ret ret
.Channel2 .Channel2
@ -322,31 +322,31 @@ UpdateChannels: ; e8125
ret z ret z
ld a, [$c292] ld a, [$c292]
ld d, a ld d, a
ld a, [$ff16] ld a, [rNR21]
and a, $3f ; sound length and a, $3f ; sound length
or d or d
ld [$ff16], a ld [rNR21], a
ret ret
.asm_e81db ; unused .asm_e81db ; unused
ld a, [$c294] ld a, [$c294]
ld [$ff18], a ld [rNR23], a
ld a, [$c295] ld a, [$c295]
ld [$ff19], a ld [rNR24], a
ret ret
.asm_e81e6 .asm_e81e6
ld a, [$c292] ld a, [$c292]
ld d, a ld d, a
ld a, [$ff16] ld a, [rNR21]
and a, $3f ; sound length and a, $3f ; sound length
or d or d
ld [$ff16], a ld [rNR21], a
ld a, [$c294] ld a, [$c294]
ld [$ff18], a ld [rNR23], a
ret ret
.ch2rest .ch2rest
ld a, [$ff26] ld a, [rNR52]
and a, %10001101 ; ch2 off and a, %10001101 ; ch2 off
ld [$ff26], a ld [rNR52], a
ld hl, $ff15 ld hl, $ff15
call ClearChannel call ClearChannel
ret ret
@ -354,14 +354,14 @@ UpdateChannels: ; e8125
ld hl, $c292 ld hl, $c292
ld a, $3f ; sound length ld a, $3f ; sound length
or [hl] or [hl]
ld [$ff16], a ld [rNR21], a
ld a, [$c293] ld a, [$c293]
ld [$ff17], a ld [rNR22], a
ld a, [$c294] ld a, [$c294]
ld [$ff18], a ld [rNR23], a
ld a, [$c295] ld a, [$c295]
or a, $80 ; initial (restart) or a, $80 ; initial (restart)
ld [$ff19], a ld [rNR24], a
ret ret
.Channel3 .Channel3
@ -377,34 +377,34 @@ UpdateChannels: ; e8125
ret ret
.asm_e822f ; unused .asm_e822f ; unused
ld a, [$c294] ld a, [$c294]
ld [$ff1d], a ld [rNR33], a
ld a, [$c295] ld a, [$c295]
ld [$ff1e], a ld [rNR34], a
ret ret
.asm_e823a .asm_e823a
ld a, [$c294] ld a, [$c294]
ld [$ff1d], a ld [rNR33], a
ret ret
.ch3rest .ch3rest
ld a, [$ff26] ld a, [rNR52]
and a, %10001011 ; ch3 off and a, %10001011 ; ch3 off
ld [$ff26], a ld [rNR52], a
ld hl, $ff1a ld hl, rNR30
call ClearChannel call ClearChannel
ret ret
.asm_e824d .asm_e824d
ld a, $3f ld a, $3f
ld [$ff1b], a ld [rNR31], a
xor a xor a
ld [$ff1a], a ld [rNR30], a
call .asm_e8268 call .asm_e8268
ld a, $80 ld a, $80
ld [$ff1a], a ld [rNR30], a
ld a, [$c294] ld a, [$c294]
ld [$ff1d], a ld [rNR33], a
ld a, [$c295] ld a, [$c295]
or a, $80 or a, $80
ld [$ff1e], a ld [rNR34], a
ret ret
.asm_e8268 .asm_e8268
push hl push hl
@ -458,7 +458,7 @@ UpdateChannels: ; e8125
ld a, [$c293] ld a, [$c293]
and a, $f0 and a, $f0
sla a sla a
ld [$ff1c], a ld [rNR32], a
ret ret
.Channel4 .Channel4
@ -472,24 +472,24 @@ UpdateChannels: ; e8125
ret ret
.asm_e82c1 ; unused .asm_e82c1 ; unused
ld a, [$c294] ld a, [$c294]
ld [$ff22], a ld [rNR43], a
ret ret
.ch4rest .ch4rest
ld a, [$ff26] ld a, [rNR52]
and a, %10000111 ; ch4 off and a, %10000111 ; ch4 off
ld [$ff26], a ld [rNR52], a
ld hl, $ff1f ld hl, $ff1f
call ClearChannel call ClearChannel
ret ret
.asm_e82d4 .asm_e82d4
ld a, $3f ; sound length ld a, $3f ; sound length
ld [$ff20], a ld [rNR41], a
ld a, [$c293] ld a, [$c293]
ld [$ff21], a ld [rNR42], a
ld a, [$c294] ld a, [$c294]
ld [$ff22], a ld [rNR43], a
ld a, $80 ld a, $80
ld [$ff23], a ld [rNR44], a
ret ret
; e82e7 ; e82e7
@ -535,15 +535,15 @@ Functione8307: ; e8307
ld hl, Tablee8350 ld hl, Tablee8350
.updatehw .updatehw
xor a xor a
ld [$ff10], a ; sweep off ld [rNR10], a ; sweep off
ld a, [hli] ld a, [hli]
ld [$ff11], a ; sound length / duty cycle ld [rNR11], a ; sound length / duty cycle
ld a, [hli] ld a, [hli]
ld [$ff12], a ; ch1 volume envelope ld [rNR12], a ; ch1 volume envelope
ld a, [hli] ld a, [hli]
ld [$ff13], a ; ch1 frequency lo ld [rNR13], a ; ch1 frequency lo
ld a, [hli] ld a, [hli]
ld [$ff14], a ; ch1 frequency hi ld [rNR14], a ; ch1 frequency hi
.asm_e8335 .asm_e8335
ld a, d ld a, d
inc a inc a
@ -1192,7 +1192,7 @@ ParseMusic: ; e85e1
jr nz, .ok jr nz, .ok
; ???? ; ????
xor a xor a
ld [$ff10], a ; sweep = 0 ld [rNR10], a ; sweep = 0
.ok .ok
; stop playing ; stop playing
; turn channel off ; turn channel off
@ -2454,56 +2454,56 @@ LoadSFX: ; e8c04
jr z, .ch6 jr z, .ch6
res 0, [hl] ; turn it off res 0, [hl] ; turn it off
xor a xor a
ld [$ff11], a ; length/wavepattern = 0 ld [rNR11], a ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [$ff12], a ; envelope = 0 ld [rNR12], a ; envelope = 0
xor a xor a
ld [$ff13], a ; frequency lo = 0 ld [rNR13], a ; frequency lo = 0
ld a, $80 ld a, $80
ld [$ff14], a ; restart sound (freq hi = 0) ld [rNR14], a ; restart sound (freq hi = 0)
xor a xor a
ld [SoundInput], a ; global sound off ld [SoundInput], a ; global sound off
ld [$ff10], a ; sweep = 0 ld [rNR10], a ; sweep = 0
.ch6 .ch6
ld hl, $c1fe ; ch6 on? ld hl, $c1fe ; ch6 on?
bit 0, [hl] bit 0, [hl]
jr z, .ch7 jr z, .ch7
res 0, [hl] ; turn it off res 0, [hl] ; turn it off
xor a xor a
ld [$ff16], a ; length/wavepattern = 0 ld [rNR21], a ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [$ff17], a ; envelope = 0 ld [rNR22], a ; envelope = 0
xor a xor a
ld [$ff18], a ; frequency lo = 0 ld [rNR23], a ; frequency lo = 0
ld a, $80 ld a, $80
ld [$ff19], a ; restart sound (freq hi = 0) ld [rNR24], a ; restart sound (freq hi = 0)
.ch7 .ch7
ld hl, $c230 ; ch7 on? ld hl, $c230 ; ch7 on?
bit 0, [hl] bit 0, [hl]
jr z, .ch8 jr z, .ch8
res 0, [hl] ; turn it off res 0, [hl] ; turn it off
xor a xor a
ld [$ff1a], a ; sound mode #3 off ld [rNR30], a ; sound mode #3 off
ld [$ff1b], a ; length/wavepattern = 0 ld [rNR31], a ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [$ff1c], a ; envelope = 0 ld [rNR32], a ; envelope = 0
xor a xor a
ld [$ff1d], a ; frequency lo = 0 ld [rNR33], a ; frequency lo = 0
ld a, $80 ld a, $80
ld [$ff1e], a ; restart sound (freq hi = 0) ld [rNR34], a ; restart sound (freq hi = 0)
.ch8 .ch8
ld hl, $c262 ; ch8 on? ld hl, $c262 ; ch8 on?
bit 0, [hl] bit 0, [hl]
jr z, .chscleared jr z, .chscleared
res 0, [hl] ; turn it off res 0, [hl] ; turn it off
xor a xor a
ld [$ff20], a ; length/wavepattern = 0 ld [rNR41], a ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [$ff21], a ; envelope = 0 ld [rNR42], a ; envelope = 0
xor a xor a
ld [$ff22], a ; frequency lo = 0 ld [rNR43], a ; frequency lo = 0
ld a, $80 ld a, $80
ld [$ff23], a ; restart sound (freq hi = 0) ld [rNR44], a ; restart sound (freq hi = 0)
xor a xor a
ld [NoiseSampleAddressLo], a ld [NoiseSampleAddressLo], a
ld [NoiseSampleAddressHi], a ld [NoiseSampleAddressHi], a
@ -3126,13 +3126,13 @@ ChannelPointers: ; e8fd9
ClearChannels: ; e8fe9 ClearChannels: ; e8fe9
; runs ClearChannel for all 4 channels ; runs ClearChannel for all 4 channels
; doesn't seem to be used, but functionally identical to SoundRestart ; doesn't seem to be used, but functionally identical to SoundRestart
ld hl, $ff24 ld hl, rNR50
xor a xor a
ld [hli], a ld [hli], a
ld [hli], a ld [hli], a
ld a, $80 ld a, $80
ld [hli], a ld [hli], a
ld hl, $ff10 ld hl, rNR10
ld e, $04 ld e, $04
.loop .loop
call ClearChannel call ClearChannel
@ -3142,19 +3142,19 @@ ClearChannels: ; e8fe9
; e8ffe ; e8ffe
ClearChannel: ; e8ffe ClearChannel: ; e8ffe
; input: hl = beginning hw sound register ($ff10, $ff15, $ff1a, $ff1f) ; input: hl = beginning hw sound register (rNR10, $ff15, rNR30, $ff1f)
; output: 00 00 80 00 80 ; output: 00 00 80 00 80
; sound channel 1 2 3 4 ; sound channel 1 2 3 4
xor a xor a
ld [hli], a ; $ff10, $ff15, $ff1a, $ff1f ; sweep = 0 ld [hli], a ; rNR10, $ff15, rNR30, $ff1f ; sweep = 0
ld [hli], a ; $ff11, $ff16, $ff1b, $ff20 ; length/wavepattern = 0 ld [hli], a ; rNR11, rNR21, rNR31, rNR41 ; length/wavepattern = 0
ld a, $08 ld a, $08
ld [hli], a ; $ff12, $ff17, $ff1c, $ff21 ; envelope = 0 ld [hli], a ; rNR12, rNR22, rNR32, rNR42 ; envelope = 0
xor a xor a
ld [hli], a ; $ff13, $ff18, $ff1d, $ff22 ; frequency lo = 0 ld [hli], a ; rNR13, rNR23, rNR33, rNR43 ; frequency lo = 0
ld a, $80 ld a, $80
ld [hli], a ; $ff14, $ff19, $ff1e, $ff23 ; restart sound (freq hi = 0) ld [hli], a ; rNR14, rNR24, rNR34, rNR44 ; restart sound (freq hi = 0)
ret ret
; e900a ; e900a

220
battle/magikarp_length.asm Normal file
View File

@ -0,0 +1,220 @@
CalcMagikarpLength: ; fbbfc
; Return Magikarp's length (in mm) in MagikarpLength (big endian)
;
; input:
; de: EnemyMonDVs
; bc: PlayerID
; This function is needlessly convoluted, and poorly commented.
; Reading is discouraged.
; In short, it generates a value between 190 and 1786 using
; a Magikarp's DVs and its trainer ID. This value is further
; scrutinized in GetEnemyMon to make longer Magikarp even rarer.
; This is done by calculating the value using operands from
; a conversion lookup table.
; Our index is calculated by xoring DVs with the trainer ID:
; bc = rrc(rrc(dvs)) xor rrc(id)
; if bc < $a: MagikarpLength = c + 190
; if bc >= $ff00: MagikarpLength = c + 1370
; else: MagikarpLength = z*100 + (bc-x)/y
; X, Y, and Z depend on the value of b as follows:
; if b = 0: x = 310, y = 2, z = 3
; if b = 1: x = 710, y = 4, z = 4
; if b = 2-9: x = 2710, y = 20, z = 5
; if b = 10-29: x = 7710, y = 50, z = 6
; if b = 30-68: x = 17710, y = 100, z = 7
; if b = 69-126: x = 32710, y = 150, z = 8
; if b = 127-185: x = 47710, y = 150, z = 9
; if b = 186-224: x = 57710, y = 100, z = 10
; if b = 225-243: x = 62710, y = 50, z = 11
; if b = 244-251: x = 64710, y = 20, z = 12
; if b = 252-253: x = 65210, y = 5, z = 13
; if b = 254: x = 65410, y = 2, z = 14
; These values represent arbitrary conversion points.
; b = rrcrrc(atkdefdv) xor rrc(id[0])
; id
ld h, b
ld l, c
ld a, [hli]
ld b, a
ld c, [hl]
rrc b
rrc c
; dvs
ld a, [de]
inc de
rrca
rrca
xor b
ld b, a
; c = rrcrrc(spdspcdv) xor rrc(id[1])
ld a, [de]
rrca
rrca
xor c
ld c, a
; if bc < $000a:
ld a, b
and a
jr nz, .loadtable
ld a, c
cp a, $a
jr nc, .loadtable
; de = hl = bc + $be
ld hl, $be
add hl, bc
ld d, h
ld e, l
jr .endtable
.loadtable
ld hl, .MagikarpLengthTable
ld a, $02
ld [$d265], a
.readtable
ld a, [hli]
ld e, a
ld a, [hli]
ld d, a
call .BLessThanD
jr nc, .advancetable
; c = bc / [hl]
call .BCMinusDE
ld a, b
ld [$ffb3], a
ld a, c
ld [$ffb4], a
ld a, [hl]
ld [$ffb7], a
ld b, $02
call Divide
ld a, [$ffb6]
ld c, a
; de = c + 100 * (2 + number of rows down the table)
xor a
ld [$ffb4], a
ld [$ffb5], a
ld a, $64
ld [$ffb6], a
ld a, [$d265]
ld [$ffb7], a
call Multiply
ld b, $00
ld a, [$ffb6]
add c
ld e, a
ld a, [$ffb5]
adc b
ld d, a
jr .endtable
.advancetable
inc hl ; align to next triplet
ld a, [$d265]
inc a
ld [$d265], a
cp a, $10
jr c, .readtable
call .BCMinusDE
ld hl, $0640
add hl, bc
ld d, h
ld e, l
.endtable
ld h, d
ld l, e
add hl, hl
add hl, hl
add hl, de
add hl, hl ; hl = de * 10
ld de, $ff02
ld a, $ff
.loop
inc a
add hl, de ; - 254
jr c, .loop
ld d, $00
; mod $0c
.modloop
cp a, $0c
jr c, .done
sub a, $0c
inc d
jr .modloop
.done
ld e, a
ld hl, MagikarpLength
ld [hl], d
inc hl
ld [hl], e
ret
; fbc9a
.BLessThanD ; fbc9a
; return carry if b < d
ld a, b
cp d
ret c
ret nc
; fbc9e
.CLessThanE ; fbc9e
; unused
ld a, c
cp e
ret
; fbca1
.BCMinusDE ; fbca1
; bc -= de
ld a, c
sub e
ld c, a
ld a, b
sbc d
ld b, a
ret
; fbca8
.MagikarpLengthTable ; fbca8
; ????, divisor
dwb $006e, $01
dwb $0136, $02
dwb $02c6, $04
dwb $0a96, $14
dwb $1e1e, $32
dwb $452e, $64
dwb $7fc6, $96
dwb $ba5e, $96
dwb $e16e, $64
dwb $f4f6, $32
dwb $fcc6, $14
dwb $feba, $05
dwb $ff82, $02
; fbccf

View File

@ -3417,15 +3417,20 @@ BATTLETYPE_SUICUNE EQU $0c
; joypad ; joypad
NO_INPUT EQU %00000000 BUTTONS EQU %00010000
BUTTON_A EQU %00000001 D_PAD EQU %00100000
BUTTON_B EQU %00000010
SELECT EQU %00000100 AUTO_INPUT EQU $ff
START EQU %00001000
D_RIGHT EQU %00010000 NO_INPUT EQU %00000000
D_LEFT EQU %00100000 BUTTON_A EQU %00000001
D_UP EQU %01000000 BUTTON_B EQU %00000010
D_DOWN EQU %10000000 SELECT EQU %00000100
START EQU %00001000
D_RIGHT EQU %00010000
D_LEFT EQU %00100000
D_UP EQU %01000000
D_DOWN EQU %10000000
; mbc ; mbc
NUM_SRAM_BANKS EQU 4 NUM_SRAM_BANKS EQU 4

65
gbhw.asm Normal file
View File

@ -0,0 +1,65 @@
; Graciously aped from http://nocash.emubase.de/pandocs.htm .
rJOYP EQU $ff00 ; Joypad (R/W)
rSB EQU $ff01 ; Serial transfer data (R/W)
rSC EQU $ff02 ; Serial Transfer Control (R/W)
rDIV EQU $ff04 ; Divider Register (R/W)
rTIMA EQU $ff05 ; Timer counter (R/W)
rTMA EQU $ff06 ; Timer Modulo (R/W)
rTAC EQU $ff07 ; Timer Control (R/W)
rIF EQU $ff0f ; Interrupt Flag (R/W)
rNR10 EQU $ff10 ; Channel 1 Sweep register (R/W)
rNR11 EQU $ff11 ; Channel 1 Sound length/Wave pattern duty (R/W)
rNR12 EQU $ff12 ; Channel 1 Volume Envelope (R/W)
rNR13 EQU $ff13 ; Channel 1 Frequency lo (Write Only)
rNR14 EQU $ff14 ; Channel 1 Frequency hi (R/W)
rNR21 EQU $ff16 ; Channel 2 Sound Length/Wave Pattern Duty (R/W)
rNR22 EQU $ff17 ; Channel 2 Volume Envelope (R/W)
rNR23 EQU $ff18 ; Channel 2 Frequency lo data (W)
rNR24 EQU $ff19 ; Channel 2 Frequency hi data (R/W)
rNR30 EQU $ff1a ; Channel 3 Sound on/off (R/W)
rNR31 EQU $ff1b ; Channel 3 Sound Length
rNR32 EQU $ff1c ; Channel 3 Select output level (R/W)
rNR33 EQU $ff1d ; Channel 3 Frequency's lower data (W)
rNR34 EQU $ff1e ; Channel 3 Frequency's higher data (R/W)
rNR41 EQU $ff20 ; Channel 4 Sound Length (R/W)
rNR42 EQU $ff21 ; Channel 4 Volume Envelope (R/W)
rNR43 EQU $ff22 ; Channel 4 Polynomial Counter (R/W)
rNR44 EQU $ff23 ; Channel 4 Counter/consecutive; Inital (R/W)
rNR50 EQU $ff24 ; Channel control / ON-OFF / Volume (R/W)
rNR51 EQU $ff25 ; Selection of Sound output terminal (R/W)
rNR52 EQU $ff26 ; Sound on/off
rLCDC EQU $ff40 ; LCD Control (R/W)
rSTAT EQU $ff41 ; LCDC Status (R/W)
rSCY EQU $ff42 ; Scroll Y (R/W)
rSCX EQU $ff43 ; Scroll X (R/W)
rLY EQU $ff44 ; LCDC Y-Coordinate (R)
rLYC EQU $ff45 ; LY Compare (R/W)
rDMA EQU $ff46 ; DMA Transfer and Start Address (W)
rBGP EQU $ff47 ; BG Palette Data (R/W) - Non CGB Mode Only
rOBP0 EQU $ff48 ; Object Palette 0 Data (R/W) - Non CGB Mode Only
rOBP1 EQU $ff49 ; Object Palette 1 Data (R/W) - Non CGB Mode Only
rWY EQU $ff4a ; Window Y Position (R/W)
rWX EQU $ff4b ; Window X Position minus 7 (R/W)
rKEY1 EQU $ff4d ; CGB Mode Only - Prepare Speed Switch
rVBK EQU $ff4f ; CGB Mode Only - VRAM Bank
rHDMA1 EQU $ff51 ; CGB Mode Only - New DMA Source, High
rHDMA2 EQU $ff52 ; CGB Mode Only - New DMA Source, Low
rHDMA3 EQU $ff53 ; CGB Mode Only - New DMA Destination, High
rHDMA4 EQU $ff54 ; CGB Mode Only - New DMA Destination, Low
rHDMA5 EQU $ff55 ; CGB Mode Only - New DMA Length/Mode/Start
rRP EQU $ff56 ; CGB Mode Only - Infrared Communications Port
rBGPI EQU $ff68 ; CGB Mode Only - Background Palette Index
rBGPD EQU $ff69 ; CGB Mode Only - Background Palette Data
rOBPI EQU $ff6a ; CGB Mode Only - Sprite Palette Index
rOBPD EQU $ff6b ; CGB Mode Only - Sprite Palette Data
rUNKNOWN1 EQU $ff6c ; (FEh) Bit 0 (Read/Write) - CGB Mode Only
rSVBK EQU $ff70 ; CGB Mode Only - WRAM Bank
rUNKNOWN2 EQU $ff72 ; (00h) - Bit 0-7 (Read/Write)
rUNKNOWN3 EQU $ff73 ; (00h) - Bit 0-7 (Read/Write)
rUNKNOWN4 EQU $ff74 ; (00h) - Bit 0-7 (Read/Write) - CGB Mode Only
rUNKNOWN5 EQU $ff75 ; (8Fh) - Bit 4-6 (Read/Write)
rUNKNOWN6 EQU $ff76 ; (00h) - Always 00h (Read Only)
rUNKNOWN7 EQU $ff77 ; (00h) - Always 00h (Read Only)
rIE EQU $ffff ; Interrupt Enable (R/W)

275
joypad.asm Normal file
View File

@ -0,0 +1,275 @@
JoypadInt: ; 92e
; Replaced by Joypad, called from VBlank instead of the useless
; joypad interrupt.
; This is a placeholder in case the interrupt is somehow enabled.
reti
; 92f
ClearJoypadPublic: ; 92f
xor a
; Pressed this frame (delta)
ld [$ffa7], a
; Currently pressed
ld [$ffa8], a
ret
; 935
Joypad: ; 935
; Read the joypad register and translate it to something more
; workable for use in-game. There are 8 buttons, so we can use
; one byte to contain all player input.
; Updates:
; $ffa2: released this frame (delta)
; $ffa3: pressed this frame (delta)
; $ffa4: currently pressed
; $ffa5: pressed so far
; Any of these three bits can be used to disable input.
ld a, [$cfbe]
and %11010000
ret nz
; If we're saving, input is disabled.
ld a, [$c2cd]
and a
ret nz
; We can only get four inputs at a time.
; We take d-pad first for no particular reason.
ld a, D_PAD
ld [rJOYP], a
; Read twice to give the request time to take.
ld a, [rJOYP]
ld a, [rJOYP]
; The Joypad register output is in the lo nybble (inversed).
; We make the hi nybble of our new container d-pad input.
cpl
and $f
swap a
; We'll keep this in b for now.
ld b, a
; Buttons make 8 total inputs (A, B, Select, Start).
; We can fit this into one byte.
ld a, BUTTONS
ld [rJOYP], a
; Wait for input to stabilize.
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
; Buttons take the lo nybble.
cpl
and $f
or b
ld b, a
; Reset the joypad register since we're done with it.
ld a, $30
ld [rJOYP], a
; To get the delta we xor the last frame's input with the new one.
ld a, [$ffa4] ; last frame
ld e, a
xor b
ld d, a
; Released this frame:
and e
ld [$ffa2], a
; Pressed this frame:
ld a, d
and b
ld [$ffa3], a
; Add any new presses to the list of collective presses:
ld c, a
ld a, [$ffa5]
or c
ld [$ffa5], a
; Currently pressed:
ld a, b
ld [$ffa4], a
; Now that we have the input, we can do stuff with it.
; For example, soft reset:
and BUTTON_A | BUTTON_B | SELECT | START
cp BUTTON_A | BUTTON_B | SELECT | START
jp z, $0150 ; reset
ret
; 984
GetJoypadPublic: ; 984
; Update mirror joypad input from $ffa4 (real input)
; $ffa6: released this frame (delta)
; $ffa7: pressed this frame (delta)
; $ffa8: currently pressed
; bit 0 A
; 1 B
; 2 SELECT
; 3 START
; 4 RIGHT
; 5 LEFT
; 6 UP
; 7 DOWN
push af
push hl
push de
push bc
; The player input can be automated using an input stream.
; See more below.
ld a, [InputType]
cp a, AUTO_INPUT
jr z, .auto
; To get deltas, take this and last frame's input.
ld a, [$ffa4] ; real input
ld b, a
ld a, [$ffa8] ; last frame mirror
ld e, a
; Released this frame:
xor b
ld d, a
and e
ld [$ffa6], a
; Pressed this frame:
ld a, d
and b
ld [$ffa7], a
; It looks like the collective presses got commented out here.
ld c, a
; Currently pressed:
ld a, b
ld [$ffa8], a ; frame input
.quit
pop bc
pop de
pop hl
pop af
ret
.auto
; Use a predetermined input stream (used in the catching tutorial).
; Stream format: [input][duration]
; A value of $ff will immediately end the stream.
; Read from the input stream.
ld a, [$ff9d]
push af
ld a, [AutoInputBank]
rst Bankswitch
ld hl, AutoInputAddress
ld a, [hli]
ld h, [hl]
ld l, a
; We only update when the input duration has expired.
ld a, [AutoInputLength]
and a
jr z, .updateauto
; Until then, don't change anything.
dec a
ld [AutoInputLength], a
pop af
rst Bankswitch
jr .quit
.updateauto
; An input of $ff will end the stream.
ld a, [hli]
cp a, $ff
jr z, .stopauto
ld b, a
; A duration of $ff will end the stream indefinitely.
ld a, [hli]
ld [AutoInputLength], a
cp a, $ff
jr nz, .next
; The current input is overwritten.
dec hl
dec hl
ld b, NO_INPUT
jr .finishauto
.next
; On to the next input...
ld a, l
ld [AutoInputAddress], a
ld a, h
ld [AutoInputAddress+1], a
jr .finishauto
.stopauto
call StopAutoInput
ld b, NO_INPUT
.finishauto
pop af
rst Bankswitch
ld a, b
ld [$ffa7], a ; pressed
ld [$ffa8], a ; input
jr .quit
; 9ee
StartAutoInput: ; 9ee
; Start reading automated input stream at a:hl.
ld [AutoInputBank], a
ld a, l
ld [AutoInputAddress], a
ld a, h
ld [AutoInputAddress+1], a
; Start reading the stream immediately.
xor a
ld [AutoInputLength], a
; Reset input mirrors.
xor a
ld [$ffa7], a ; pressed this frame
ld [$ffa6], a ; released this frame
ld [$ffa8], a ; currently pressed
ld a, AUTO_INPUT
ld [InputType], a
ret
; a0a
StopAutoInput: ; a0a
; Clear variables related to automated input.
xor a
ld [AutoInputBank], a
ld [AutoInputAddress], a
ld [AutoInputAddress+1], a
ld [AutoInputLength], a
; Back to normal input.
ld [InputType], a
ret
; a1b

632
main.asm

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
INCLUDE "wram.asm" INCLUDE "wram.asm"
INCLUDE "constants.asm" INCLUDE "constants.asm"
INCLUDE "gbhw.asm"
INCLUDE "main.tx" INCLUDE "main.tx"

View File

@ -69,13 +69,13 @@ VBlank0: ; 2b1
inc [hl] inc [hl]
; advance rng ; advance rng
ld a, [$ff04] ; divider ld a, [rDIV]
ld b, a ld b, a
ld a, [$ffe1] ld a, [$ffe1]
adc b adc b
ld [$ffe1], a ld [$ffe1], a
ld a, [$ff04] ; divider ld a, [rDIV]
ld b, a ld b, a
ld a, [$ffe2] ld a, [$ffe2]
sbc b sbc b
@ -87,16 +87,16 @@ VBlank0: ; 2b1
; scroll x ; scroll x
ld a, [$ffcf] ld a, [$ffcf]
ld [$ff43], a ; scx ld [rSCX], a
; scroll y ; scroll y
ld a, [$ffd0] ld a, [$ffd0]
ld [$ff42], a ; scy ld [rSCY], a
; window y ; window y
ld a, [$ffd2] ld a, [$ffd2]
ld [$ff4a], a ; wy ld [rWY], a
; window x + 7 ; window x + 7
ld a, [$ffd1] ld a, [$ffd1]
ld [$ff4b], a ; wx ld [rWX], a
; some time management is in order ; some time management is in order
; only have time for one of these during vblank ; only have time for one of these during vblank
@ -131,7 +131,7 @@ VBlank0: ; 2b1
call $ff80 call $ff80
; 403f: ; 403f:
; ld a, $c4 ; ld a, $c4
; ld [$ff46], a ; oam dma ; ld [rDMA], a
; ld a, $28 ; ld a, $28
; .loop ; .loop
; dec a ; dec a
@ -216,11 +216,11 @@ VBlank1: ; 337
; scroll x ; scroll x
ld a, [$ffcf] ld a, [$ffcf]
ld [$ff43], a ; scx ld [rSCX], a
; scroll y ; scroll y
ld a, [$ffd0] ld a, [$ffd0]
ld [$ff42], a ; scy ld [rSCY], a
; time-sensitive fns ; time-sensitive fns
call UpdatePals call UpdatePals
@ -233,7 +233,7 @@ VBlank1: ; 337
call $ff80 call $ff80
; 403f: ; 403f:
; ld a, $c4 ; ld a, $c4
; ld [$ff46], a ; oam dma ; ld [rDMA], a
; ld a, $28 ; ld a, $28
; .loop ; .loop
; dec a ; dec a
@ -246,20 +246,20 @@ VBlank1: ; 337
ld [VBlankOccurred], a ld [VBlankOccurred], a
; get requested ints ; get requested ints
ld a, [$ff0f] ; IF ld a, [rIF]
ld b, a ld b, a
; discard requested ints ; discard requested ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; enable lcd stat ; enable lcd stat
ld a, %10 ; lcd stat ld a, %10 ; lcd stat
ld [$ffff], a ; IE ld [rIE], a
; rerequest serial int if applicable (still disabled) ; rerequest serial int if applicable (still disabled)
; request lcd stat ; request lcd stat
ld a, b ld a, b
and %1000 ; serial and %1000 ; serial
or %10 ; lcd stat or %10 ; lcd stat
ld [$ff0f], a ; IF ld [rIF], a
ei ei
; update sound ; update sound
@ -272,17 +272,17 @@ VBlank1: ; 337
di di
; get requested ints ; get requested ints
ld a, [$ff0f] ; IF ld a, [rIF]
ld b, a ld b, a
; discard requested ints ; discard requested ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; enable ints besides joypad ; enable ints besides joypad
ld a, %1111 ; serial timer lcdstat vblank ld a, %1111 ; serial timer lcdstat vblank
ld [$ffff], a ; IE ld [rIE], a
; rerequest ints ; rerequest ints
ld a, b ld a, b
ld [$ff0f], a ; IF ld [rIF], a
ret ret
; 37f ; 37f
@ -297,13 +297,13 @@ UpdatePals: ; 37f
; update gb pals ; update gb pals
ld a, [$cfc7] ld a, [$cfc7]
ld [$ff47], a ; BGP ld [rBGP], a
ld a, [$cfc8] ld a, [$cfc8]
ld [$ff48], a ; OBP0 ld [rOBP0], a
ld a, [$cfc9] ld a, [$cfc9]
ld [$ff49], a ; 0BP1 ld [rOBP1], a
and a and a
ret ret
@ -324,10 +324,10 @@ VBlank3: ; 396
; scroll x ; scroll x
ld a, [$ffcf] ld a, [$ffcf]
ld [$ff43], a ; scx ld [rSCX], a
; scroll y ; scroll y
ld a, [$ffd0] ld a, [$ffd0]
ld [$ff42], a ; scy ld [rSCY], a
; any pals to update? ; any pals to update?
ld a, [$ffe5] ld a, [$ffe5]
@ -342,7 +342,7 @@ VBlank3: ; 396
call $ff80 call $ff80
; 403f: ; 403f:
; ld a, $c4 ; Sprites / $100 ; ld a, $c4 ; Sprites / $100
; ld [$ff46], a ; oam dma ; ld [rDMA], a
; ld a, $28 ; ld a, $28
; .loop ; .loop
; dec a ; dec a
@ -355,15 +355,15 @@ VBlank3: ; 396
ld [VBlankOccurred], a ld [VBlankOccurred], a
; save int flag ; save int flag
ld a, [$ff0f] ; IF ld a, [rIF]
push af push af
; reset ints ; reset ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; force lcdstat int during sound update ; force lcdstat int during sound update
ld a, %10 ; lcd stat ld a, %10 ; lcd stat
ld [$ffff], a ; IE ld [rIE], a
ld [$ff0f], a ; IF ld [rIF], a
ei ei
; update sound ; update sound
@ -376,7 +376,7 @@ VBlank3: ; 396
di di
; request lcdstat ; request lcdstat
ld a, [$ff0f] ; IF ld a, [rIF]
ld b, a ld b, a
; and any other ints ; and any other ints
pop af pop af
@ -384,13 +384,13 @@ VBlank3: ; 396
ld b, a ld b, a
; reset ints ; reset ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; enable ints besides joypad ; enable ints besides joypad
ld a, %1111 ; serial timer lcdstat vblank ld a, %1111 ; serial timer lcdstat vblank
ld [$ffff], a ; IE ld [rIE], a
; request ints ; request ints
ld a, b ld a, b
ld [$ff0f], a ; IF ld [rIF], a
ret ret
; 3df ; 3df
@ -414,7 +414,7 @@ VBlank4: ; 3df
call $ff80 call $ff80
; 403f: ; 403f:
; ld a, $c4 ; ld a, $c4
; ld [$ff46], a ; oam dma ; ld [rDMA], a
; ld a, $28 ; ld a, $28
; .loop ; .loop
; dec a ; dec a
@ -456,7 +456,7 @@ VBlank5: ; 400
; scroll x ; scroll x
ld a, [$ffcf] ld a, [$ffcf]
ld [$ff43], a ; scx ld [rSCX], a
; if we can update pals, skip this part ; if we can update pals, skip this part
call UpdatePalsIfCGB call UpdatePalsIfCGB
@ -475,12 +475,12 @@ VBlank5: ; 400
; discard requested ints ; discard requested ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; enable lcd stat ; enable lcd stat
ld a, %10 ; lcd stat ld a, %10 ; lcd stat
ld [$ffff], a ; IE ld [rIE], a
; request lcd stat ; request lcd stat
ld [$ff0f], a ; IF ld [rIF], a
ei ei
; update sound ; update sound
@ -494,10 +494,10 @@ VBlank5: ; 400
; discard requested ints ; discard requested ints
xor a xor a
ld [$ff0f], a ; IF ld [rIF], a
; enable ints besides joypad ; enable ints besides joypad
ld a, %1111 ; serial timer lcdstat vblank ld a, %1111 ; serial timer lcdstat vblank
ld [$ffff], a ; IE ld [rIE], a
ret ret
; 436 ; 436

View File

@ -837,11 +837,9 @@ TileSetPalettes: ; d1e6
ds 2 ds 2
Buffer1: Buffer1:
MagikarpLength: MagikarpLength: ; d1ea
MagikarpLengthHi: ; d1ea
ds 1 ds 1
Buffer2: Buffer2: ; d1eb
MagikarpLengthLo: ; d1eb
ds 1 ds 1
SECTION "prng2",BSS[$d1fa] SECTION "prng2",BSS[$d1fa]