ref: 0c6338836d714f464bfcaa0c8c5b8c76e6f4958d
parent: 52993d20efd5315c9d3e2e684a84456783717fc8
author: Remy Oukaour <[email protected]>
date: Fri Jan 19 09:49:43 EST 2018
Resolve #471 (although link code still needs more cleanup, like its WRAM labels)
--- a/constants/serial_constants.asm
+++ b/constants/serial_constants.asm
@@ -6,7 +6,25 @@
const LINK_COLOSSEUM ; 3
const LINK_MOBILE ; 4
-
+; hSerialReceive high nybbles
SERIAL_TIMECAPSULE EQU $60
SERIAL_TRADECENTER EQU $70
SERIAL_BATTLE EQU $80
+
+ESTABLISH_CONNECTION_WITH_INTERNAL_CLOCK EQU $01
+ESTABLISH_CONNECTION_WITH_EXTERNAL_CLOCK EQU $02
+
+START_TRANSFER_EXTERNAL_CLOCK EQU $80 ; 1 << rSC_ON
+START_TRANSFER_INTERNAL_CLOCK EQU $81 ; (1 << rSC_ON) | 1
+
+; hSerialConnectionStatus
+USING_EXTERNAL_CLOCK EQU $01
+USING_INTERNAL_CLOCK EQU $02
+CONNECTION_NOT_ESTABLISHED EQU $ff
+
+; signals the start of an array of bytes transferred over the link cable
+SERIAL_PREAMBLE_BYTE EQU $fd
+; this byte is used when there is no data to send
+SERIAL_NO_DATA_BYTE EQU $fe
+; signals the end of one part of a patch list (there are two parts) for player/enemy party data
+SERIAL_PATCH_LIST_PART_TERMINATOR EQU $ff
--- a/engine/battle/core.asm
+++ b/engine/battle/core.asm
@@ -26,7 +26,7 @@
and a
jr z, .not_linked
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $2
jr z, .player_2
@@ -97,7 +97,7 @@
ld a, [wLinkMode]
and a
jr z, .not_linked_2
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $2
jr nz, .not_linked_2
xor a
@@ -251,7 +251,7 @@
; 3c1d6
HandleBetweenTurnEffects: ; 3c1d6
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .CheckEnemyFirst
call CheckFaint_PlayerThenEnemy
@@ -350,7 +350,7 @@
; 3c27c
HandleBerserkGene: ; 3c27c
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .reverse
@@ -458,7 +458,7 @@
ld a, [wPlayerAction]
cp $2
jr nz, .switch
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $2
jr z, .player_2
@@ -515,7 +515,7 @@
jp .enemy_first
.both_have_quick_claw
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $2
jr z, .player_2b
call BattleRandom
@@ -545,7 +545,7 @@
jp .enemy_first
.speed_tie
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $2
jr z, .player_2c
call BattleRandom
@@ -709,7 +709,7 @@
; 3c4df
HandleEncore: ; 3c4df
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player_1
call .do_player
@@ -1161,7 +1161,7 @@
; 3c801
HandlePerishSong: ; 3c801
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .EnemyFirst
call SetPlayerTurn
@@ -1230,7 +1230,7 @@
; 3c874
HandleWrap: ; 3c874
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .EnemyFirst
call SetPlayerTurn
@@ -1302,7 +1302,7 @@
; 3c8eb
HandleLeftovers: ; 3c8eb
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .DoEnemyFirst
call SetPlayerTurn
@@ -1352,7 +1352,7 @@
; 3c93c
HandleMysteryberry: ; 3c93c
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .DoEnemyFirst
call SetPlayerTurn
@@ -1491,7 +1491,7 @@
; 3ca26
HandleFutureSight: ; 3ca26
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .enemy_first
call SetPlayerTurn
@@ -1550,7 +1550,7 @@
; 3ca8f
HanleDefrost: ; 3ca8f
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .enemy_first
call .do_player_turn
@@ -1610,7 +1610,7 @@
; 3cafb
HandleSafeguard: ; 3cafb
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player1
call .CheckPlayer
@@ -1647,7 +1647,7 @@
jp StdBattleTextBox
HandleScreens: ; 3cb36
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp 1
jr z, .Both
call .CheckPlayer
@@ -1731,7 +1731,7 @@
cp WEATHER_SANDSTORM
ret nz
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp 1
jr z, .enemy_first
@@ -2115,7 +2115,7 @@
; 3cdca
DoubleSwitch: ; 3cdca
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player_1
call ClearSprites
@@ -4350,7 +4350,7 @@
; 3dcf9
HandleHealingItems: ; 3dcf9
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player_1
call SetPlayerTurn
@@ -4595,7 +4595,7 @@
HandleStatBoostingHeldItems: ; 3de97
; The effects handled here are not used in-game.
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player_1
call .DoPlayer
@@ -5374,7 +5374,7 @@
ret
.dont_run
- ld a, [hLinkPlayerNumber]
+ ld a, [hSerialConnectionStatus]
cp $1
jr z, .player_1
call BattleMonEntrance
--- a/engine/link.asm
+++ b/engine/link.asm
@@ -28,9 +28,9 @@
call SetTradeRoomBGPals
call WaitBGMap2
ld hl, wcf5d
- xor a
+ xor a ; LOW($5000)
ld [hli], a
- ld [hl], $50
+ ld [hl], HIGH($5000)
ld a, [wLinkMode]
cp LINK_TIMECAPSULE
jp nz, Gen2ToGen2LinkComms
@@ -42,29 +42,31 @@
xor a
ld [wPlayerLinkAction], a
call WaitLinkTransfer
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr nz, .player_1
+
ld c, 3
call DelayFrames
xor a
ld [hSerialSend], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
+
call DelayFrame
xor a
ld [hSerialSend], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
.player_1
ld de, MUSIC_NONE
call PlayMusic
- ld c, $3
+ ld c, 3
call DelayFrames
xor a
ld [rIF], a
@@ -73,19 +75,19 @@
ld hl, wd1f3
ld de, EnemyMonSpecies
ld bc, $11
- call Function75f
- ld a, $fe
+ call Serial_ExchangeBytes
+ ld a, SERIAL_NO_DATA_BYTE
ld [de], a
ld hl, wLinkData
ld de, OTPlayerName
ld bc, $1a8
- call Function75f
- ld a, $fe
+ call Serial_ExchangeBytes
+ ld a, SERIAL_NO_DATA_BYTE
ld [de], a
ld hl, wMisc
ld de, wPlayerTrademonSpecies
ld bc, wPlayerTrademonSpecies - wMisc
- call Function75f
+ call Serial_ExchangeBytes
xor a
ld [rIF], a
ld a, $1d
@@ -107,25 +109,25 @@
call Link_CopyOTData
ld de, wPlayerTrademonSpecies
ld hl, wTimeCapsulePartyMon1Species
- ld c, $2
+ ld c, 2
.loop
ld a, [de]
inc de
and a
jr z, .loop
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .loop
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop
- cp $ff
+ cp SERIAL_PATCH_LIST_PART_TERMINATOR
jr z, .next
push hl
push bc
- ld b, $0
+ ld b, 0
dec a
ld c, a
add hl, bc
- ld a, $fe
+ ld a, SERIAL_NO_DATA_BYTE
ld [hl], a
pop bc
pop hl
@@ -168,8 +170,8 @@
ld [wUnusedD102 + 1], a
ld de, MUSIC_NONE
call PlayMusic
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
ld c, 66
call z, DelayFrames
ld de, MUSIC_ROUTE_30
@@ -185,23 +187,25 @@
ld a, [ScriptVar]
and a
jp z, LinkTimeout
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr nz, .Player1
+
ld c, 3
call DelayFrames
xor a
ld [hSerialSend], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
+
call DelayFrame
xor a
ld [hSerialSend], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
.Player1:
@@ -216,19 +220,19 @@
ld hl, wd1f3
ld de, EnemyMonSpecies
ld bc, $11
- call Function75f
- ld a, $fe
+ call Serial_ExchangeBytes
+ ld a, SERIAL_NO_DATA_BYTE
ld [de], a
ld hl, wLinkData
ld de, OTPlayerName
ld bc, $1c2
- call Function75f
- ld a, $fe
+ call Serial_ExchangeBytes
+ ld a, SERIAL_NO_DATA_BYTE
ld [de], a
ld hl, wMisc
ld de, wPlayerTrademonSpecies
ld bc, $c8
- call Function75f
+ call Serial_ExchangeBytes
ld a, [wLinkMode]
cp LINK_TRADECENTER
jr nz, .not_trading
@@ -235,7 +239,7 @@
ld hl, wc9f4
ld de, wcb84
ld bc, $186
- call Function283f2
+ call ExchangeBytes
.not_trading
xor a
@@ -252,25 +256,25 @@
call Link_CopyOTData
ld de, wPlayerTrademonSpecies
ld hl, wLinkPlayerPartyMon1Species
- ld c, $2
+ ld c, 2
.loop1
ld a, [de]
inc de
and a
jr z, .loop1
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .loop1
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop1
- cp $ff
+ cp SERIAL_PATCH_LIST_PART_TERMINATOR
jr z, .next1
push hl
push bc
- ld b, $0
+ ld b, 0
dec a
ld c, a
add hl, bc
- ld a, $fe
+ ld a, SERIAL_NO_DATA_BYTE
ld [hl], a
pop bc
pop hl
@@ -286,13 +290,13 @@
ld hl, wcb84
.loop2
ld a, [hli]
- cp $20
+ cp MAIL_MSG_LENGTH
jr nz, .loop2
.loop3
ld a, [hli]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop3
- cp $20
+ cp MAIL_MSG_LENGTH
jr z, .loop3
dec hl
ld de, wcb84
@@ -302,9 +306,9 @@
ld bc, $c6 ; 198
.loop4
ld a, [hl]
- cp $21
+ cp MAIL_MSG_LENGTH + 1
jr nz, .okay1
- ld [hl], $fe
+ ld [hl], SERIAL_NO_DATA_BYTE
.okay1
inc hl
dec bc
@@ -315,7 +319,7 @@
.loop5
ld a, [de]
inc de
- cp $ff
+ cp SERIAL_PATCH_LIST_PART_TERMINATOR
jr z, .start_copying_mail
ld hl, wcc4a
dec a
@@ -322,7 +326,7 @@
ld b, $0
ld c, a
add hl, bc
- ld [hl], $fe
+ ld [hl], SERIAL_NO_DATA_BYTE
jr .loop5
.start_copying_mail
@@ -395,7 +399,7 @@
ld bc, NAME_LENGTH
call CopyBytes
ld de, OTPartyCount
- ld bc, 8
+ ld bc, 1 + PARTY_LENGTH + 1
call CopyBytes
ld de, OTPlayerID
ld bc, 2
@@ -409,8 +413,8 @@
ld [wUnusedD102 + 1], a
ld de, MUSIC_NONE
call PlayMusic
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
ld c, 66
call z, DelayFrames
ld a, [wLinkMode]
@@ -423,8 +427,8 @@
ld hl, Options
ld a, [hl]
push af
- and $20
- or $3
+ and 1 << STEREO
+ or TEXT_DELAY_MED
ld [hl], a
ld hl, OTPlayerName
ld de, OTClassName
@@ -510,21 +514,21 @@
db "@"
; 0x283f2
-Function283f2: ; 283f2
- ld a, $1
- ld [hFFCC], a
+ExchangeBytes: ; 283f2
+ ld a, TRUE
+ ld [hSerialIgnoringInitialData], a
.loop
ld a, [hl]
ld [hSerialSend], a
- call Function78a
+ call Serial_ExchangeByte
push bc
ld b, a
inc hl
- ld a, $30
+ ld a, 48
.delay_cycles
dec a
jr nz, .delay_cycles
- ld a, [hFFCC]
+ ld a, [hSerialIgnoringInitialData]
and a
ld a, b
pop bc
@@ -531,7 +535,7 @@
jr z, .load
dec hl
xor a
- ld [hFFCC], a
+ ld [hSerialIgnoringInitialData], a
jr .loop
.load
@@ -563,7 +567,7 @@
FixDataForLinkTransfer: ; 28434
ld hl, wd1f3
- ld a, $fd
+ ld a, SERIAL_PREAMBLE_BYTE
ld b, LinkBattleRNs - wd1f3
.loop1
ld [hli], a
@@ -572,13 +576,13 @@
ld b, TempEnemyMonSpecies - LinkBattleRNs
.loop2
call Random
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr nc, .loop2
ld [hli], a
dec b
jr nz, .loop2
ld hl, wMisc
- ld a, $fd
+ ld a, SERIAL_PREAMBLE_BYTE
ld [hli], a
ld [hli], a
ld [hli], a
@@ -594,7 +598,7 @@
.loop4
inc c
ld a, c
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .next1
ld a, b
dec a
@@ -613,16 +617,16 @@
.next2
inc hl
ld a, [hl]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr nz, .loop4
ld a, c
ld [de], a
inc de
- ld [hl], $ff
+ ld [hl], SERIAL_PATCH_LIST_PART_TERMINATOR
jr .loop4
.next1
- ld a, $ff
+ ld a, SERIAL_PATCH_LIST_PART_TERMINATOR
ld [de], a
inc de
lb bc, 1, 0
@@ -629,7 +633,7 @@
jr .loop4
.done
- ld a, $ff
+ ld a, SERIAL_PATCH_LIST_PART_TERMINATOR
ld [de], a
ret
; 28499
@@ -636,8 +640,8 @@
Link_PrepPartyData_Gen1: ; 28499
ld de, wLinkData
- ld a, $fd
- ld b, 6
+ ld a, SERIAL_PREAMBLE_BYTE
+ ld b, PARTY_LENGTH
.loop1
ld [de], a
inc de
@@ -804,8 +808,8 @@
Link_PrepPartyData_Gen2: ; 28595
ld de, wLinkData
- ld a, $fd
- ld b, 6
+ ld a, SERIAL_PREAMBLE_BYTE
+ ld b, PARTY_LENGTH
.loop1
ld [de], a
inc de
@@ -907,7 +911,7 @@
ld bc, PARTY_LENGTH * (sPartyMon1MailAuthor - sPartyMon1Mail)
.loop5
ld a, [hl]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr nz, .skip2
ld [hl], sPartyMon1MailAuthor - sPartyMon1Mail
@@ -924,9 +928,9 @@
.loop6
inc c
ld a, [hl]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr nz, .skip3
- ld [hl], $ff
+ ld [hl], SERIAL_PATCH_LIST_PART_TERMINATOR
ld a, c
ld [de], a
inc de
@@ -935,13 +939,13 @@
inc hl
dec b
jr nz, .loop6
- ld a, $ff
+ ld a, SERIAL_PATCH_LIST_PART_TERMINATOR
ld [de], a
ret
; 28682
Function28682: ; 28682
- ld c, $5
+ ld c, 5
.loop
ld [de], a
inc de
@@ -1129,7 +1133,7 @@
Link_CopyOTData: ; 2879e
.loop
ld a, [hli]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop
ld [de], a
inc de
@@ -1141,8 +1145,8 @@
; 287ab
Link_CopyRandomNumbers: ; 287ab
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
ret z
ld hl, EnemyMonSpecies
call Link_FindFirstNonControlCharacter_AllowZero
@@ -1150,9 +1154,9 @@
ld c, 10
.loop
ld a, [hli]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .loop
ld [de], a
inc de
@@ -1166,9 +1170,9 @@
ld a, [hli]
and a
jr z, .loop
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .loop
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop
dec hl
ret
@@ -1177,9 +1181,9 @@
Link_FindFirstNonControlCharacter_AllowZero: ; 287d8
.loop
ld a, [hli]
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr z, .loop
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
jr z, .loop
dec hl
ret
@@ -1219,7 +1223,7 @@
ld [wMenuCursorX], a
ln a, 1, 0
ld [w2DMenuCursorOffsets], a
- ld a, $20
+ ld a, MENU_UNUSED_3
ld [w2DMenuFlags1], a
xor a
ld [w2DMenuFlags2], a
@@ -1231,7 +1235,7 @@
jp z, LinkTradePartiesMenuMasterLoop
bit A_BUTTON_F, a
jr z, .not_a_button
- ld a, $1
+ ld a, INIT_ENEMYOT_LIST
ld [wInitListType], a
callfar InitList
ld hl, OTPartyMon1Species
@@ -1284,7 +1288,7 @@
ld [wMenuCursorX], a
ln a, 1, 0
ld [w2DMenuCursorOffsets], a
- ld a, $20
+ ld a, MENU_UNUSED_3
ld [w2DMenuFlags1], a
xor a
ld [w2DMenuFlags2], a
@@ -1424,7 +1428,7 @@
.show_stats
pop af
ld [wMenuCursorY], a
- ld a, $4
+ ld a, INIT_PLAYEROT_LIST
ld [wInitListType], a
callfar InitList
farcall LinkMonStatsScreen
@@ -1478,7 +1482,7 @@
ld a, [wd003]
ld hl, OTPartySpecies
ld c, a
- ld b, $0
+ ld b, 0
add hl, bc
ld a, [hl]
ld [wd265], a
@@ -1583,9 +1587,9 @@
xor a
ld [rSB], a
ld [hSerialSend], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
ret
; 28b42
@@ -1639,14 +1643,14 @@
ld [wcf57], a
ld [wOtherPlayerLinkAction], a
hlcoord 0, 12
- ld b, $4
- ld c, $12
+ ld b, 4
+ ld c, 18
call LinkTextboxAtHL
farcall Link_WaitBGMap
ld a, [wd002]
ld hl, PartySpecies
ld c, a
- ld b, $0
+ ld b, 0
add hl, bc
ld a, [hl]
ld [wd265], a
@@ -1658,7 +1662,7 @@
ld a, [wd003]
ld hl, OTPartySpecies
ld c, a
- ld b, $0
+ ld b, 0
add hl, bc
ld a, [hl]
ld [wd265], a
@@ -1749,7 +1753,7 @@
.asm_28c96
inc c
ld a, c
- cp $6
+ cp PARTY_LENGTH
jr z, .asm_28ca6
push bc
ld bc, MAIL_STRUCT_LENGTH
@@ -1778,7 +1782,7 @@
call CopyBytes
ld a, [wd002]
ld hl, PartySpecies
- ld b, $0
+ ld b, 0
ld c, a
add hl, bc
ld a, [hl]
@@ -1818,7 +1822,7 @@
call CopyBytes
ld a, [wd003]
ld hl, OTPartySpecies
- ld b, $0
+ ld b, 0
ld c, a
add hl, bc
ld a, [hl]
@@ -1854,7 +1858,7 @@
ld a, [wd002]
ld [CurPartyMon], a
ld hl, PartySpecies
- ld b, $0
+ ld b, 0
ld c, a
add hl, bc
ld a, [hl]
@@ -1865,12 +1869,12 @@
ld a, [PartyCount]
dec a
ld [CurPartyMon], a
- ld a, $1
+ ld a, TRUE
ld [wForceEvolution], a
ld a, [wd003]
push af
ld hl, OTPartySpecies
- ld b, $0
+ ld b, 0
ld c, a
add hl, bc
ld a, [hl]
@@ -1881,8 +1885,8 @@
call LoadFontsBattleExtra
ld b, SCGB_DIPLOMA
call GetSGBLayout
- ld a, [hLinkPlayerNumber]
- cp $1
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
jr z, .player_2
predef TradeAnimation
jr .done_animation
@@ -1895,7 +1899,7 @@
ld c, a
ld [CurPartyMon], a
ld hl, OTPartySpecies
- ld d, $0
+ ld d, 0
ld e, a
add hl, de
ld a, [hl]
@@ -1936,7 +1940,7 @@
ld a, b
ld [wPlayerLinkAction], a
push bc
- call Function862
+ call Serial_PrintWaitingTextAndSyncAndExchangeNybble
pop bc
ld a, [wLinkMode]
cp LINK_TIMECAPSULE
@@ -2113,7 +2117,7 @@
sub b
ld c, a
inc c
- ld b, $0
+ ld b, 0
ld hl, PartyCount
add hl, bc
ld a, [hl]
@@ -2140,13 +2144,13 @@
ld c, 3
call DelayFrames
ld a, -1
- ld [hLinkPlayerNumber], a
+ ld [hSerialConnectionStatus], a
xor a
ld [rSB], a
ld [hSerialReceive], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
ld c, 3
call DelayFrames
@@ -2153,11 +2157,11 @@
xor a
ld [rSB], a
ld [hSerialReceive], a
- ld a, $0
+ ld a, (0 << rSC_ON) | 0
ld [rSC], a
- ld a, $80
+ ld a, (1 << rSC_ON) | 0
ld [rSC], a
- ld c, $3
+ ld c, 3
call DelayFrames
xor a
ld [rSB], a
@@ -2166,7 +2170,7 @@
ld c, 3
call DelayFrames
ld a, -1
- ld [hLinkPlayerNumber], a
+ ld [hSerialConnectionStatus], a
ld a, [rIF]
push af
xor a
@@ -2203,9 +2207,9 @@
ld [rSB], a
xor a
ld [hSerialReceive], a
- ld a, $0
+ ld a, (0 << rSC_ON) | 0
ld [rSC], a
- ld a, $80
+ ld a, (1 << rSC_ON) | 0
ld [rSC], a
xor a ; LINK_TIMECAPSULE - 1
ld [wPlayerLinkAction], a
@@ -2221,9 +2225,9 @@
ld [rSB], a
xor a
ld [hSerialReceive], a
- ld a, $0
+ ld a, (0 << rSC_ON) | 0
ld [rSC], a
- ld a, $80
+ ld a, (1 << rSC_ON) | 0
ld [rSC], a
call DelayFrame
call DelayFrame
@@ -2235,20 +2239,20 @@
ld a, $ff
ld [wLinkTimeoutFrames], a
.loop
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr z, .connected
- cp $1
+ cp USING_EXTERNAL_CLOCK
jr z, .connected
- ld a, -1
- ld [hLinkPlayerNumber], a
+ ld a, CONNECTION_NOT_ESTABLISHED
+ ld [hSerialConnectionStatus], a
ld a, $2
ld [rSB], a
xor a
ld [hSerialReceive], a
- ld a, $0
+ ld a, (0 << rSC_ON) | 0
ld [rSC], a
- ld a, $80
+ ld a, (1 << rSC_ON) | 0
ld [rSC], a
ld a, [wLinkTimeoutFrames]
dec a
@@ -2262,9 +2266,9 @@
.not_done
ld a, $1
ld [rSB], a
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
call DelayFrame
jr .loop
@@ -2357,7 +2361,7 @@
Link_CheckCommunicationError: ; 29e0c
xor a
- ld [hFFCA], a
+ ld [hSerialReceivedNewData], a
ld a, [wLinkTimeoutFrames]
ld h, a
ld a, [wLinkTimeoutFrames + 1]
@@ -2409,7 +2413,7 @@
.ConvertDW: ; 29e53
; [wLinkTimeoutFrames] = ((hl - $100) / 4) + $100
- ; = (hl / 4) + $c0
+ ; = (hl / 4) + $c0
dec h
srl h
rr l
@@ -2427,9 +2431,9 @@
ld a, [wd265]
push af
farcall Link_SaveGame
- ld a, $1
+ ld a, TRUE
jr nc, .return_result
- xor a
+ xor a ; FALSE
.return_result
ld [ScriptVar], a
ld c, 30
@@ -2456,12 +2460,12 @@
ld [wLinkMode], a
xor a
ld [hVBlank], a
- ld a, $1
+ ld a, TRUE
ld [ScriptVar], a
ret
.fail
- xor a
+ xor a ; FALSE
ld [ScriptVar], a
ret
; 29eaf
@@ -2517,8 +2521,8 @@
Link_ResetSerialRegistersAfterLinkClosure: ; 29f04
ld c, 3
call DelayFrames
- ld a, -1
- ld [hLinkPlayerNumber], a
+ ld a, CONNECTION_NOT_ESTABLISHED
+ ld [hSerialConnectionStatus], a
ld a, $2
ld [rSB], a
xor a
@@ -2536,7 +2540,7 @@
call DelayFrame
call DelayFrame
.receive_loop
- call Function83b
+ call Serial_ExchangeLinkMenuSelection
ld a, [wOtherPlayerLinkMode]
ld b, a
and $f0
@@ -2557,11 +2561,11 @@
; 29f47
Special_CableClubCheckWhichChris: ; 29f47
- ld a, [hLinkPlayerNumber]
- cp $1
- ld a, $1
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
+ ld a, TRUE
jr z, .yes
- dec a
+ dec a ; FALSE
.yes
ld [ScriptVar], a
@@ -2575,7 +2579,7 @@
Unreferenced_Function29fe4:
ld a, BANK(sPartyMail)
call GetSRAMBank
- ld d, $0
+ ld d, FALSE
ld b, CHECK_FLAG
predef SmallFarFlagAction
call CloseSRAM
--- a/engine/link_2.asm
+++ b/engine/link_2.asm
@@ -56,7 +56,7 @@
.PlaceBorder: ; 4d37e
push hl
- ld a, $76
+ ld a, "ぁ" ; $76
ld [hli], a
inc a
call .PlaceRow
--- a/engine/link_trade.asm
+++ b/engine/link_trade.asm
@@ -139,7 +139,7 @@
LinkComms_LoadPleaseWaitTextboxBorderGFX: ; 16d69a
ld de, LinkCommsBorderGFX + $30 tiles
- ld hl, vTiles2 tile $76
+ ld hl, vTiles2 tile "ぁ"
lb bc, BANK(LinkCommsBorderGFX), 8
call Get2bpp
ret
--- a/home/init.asm
+++ b/home/init.asm
@@ -32,7 +32,7 @@
.load
ld [hCGB], a
ld a, $1
- ld [hFFEA], a
+ ld [hSystemBooted], a
; 17d
@@ -84,7 +84,7 @@
; Clear HRAM
ld a, [hCGB]
push af
- ld a, [hFFEA]
+ ld a, [hSystemBooted]
push af
xor a
ld hl, HRAM_Begin
@@ -91,7 +91,7 @@
ld bc, HRAM_End - HRAM_Begin
call ByteFill
pop af
- ld [hFFEA], a
+ ld [hSystemBooted], a
pop af
ld [hCGB], a
@@ -137,7 +137,7 @@
ld [rLCDC], a
ld a, -1
- ld [hLinkPlayerNumber], a
+ ld [hSerialConnectionStatus], a
farcall InitCGBPals
--- a/home/map.asm
+++ b/home/map.asm
@@ -1352,7 +1352,7 @@
dec c
jr nz, .loop
ld a, SCREEN_WIDTH
- ld [hFFDC], a
+ ld [hBGMapTileCount], a
ret
; 27f8
@@ -1379,7 +1379,7 @@
dec c
jr nz, .loop
ld a, SCREEN_HEIGHT
- ld [hFFDC], a
+ ld [hBGMapTileCount], a
ret
; 2816
--- a/home/serial.asm
+++ b/home/serial.asm
@@ -14,9 +14,9 @@
bit 0, a
jr nz, .printer
- ld a, [hLinkPlayerNumber]
- inc a ; is it equal to -1?
- jr z, .init_player_number
+ ld a, [hSerialConnectionStatus]
+ inc a ; is it equal to CONNECTION_NOT_ESTABLISHED?
+ jr z, .establish_connection
ld a, [rSB]
ld [hSerialReceive], a
@@ -24,8 +24,8 @@
ld a, [hSerialSend]
ld [rSB], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr z, .player2
ld a, 0 << rSC_ON
@@ -42,22 +42,22 @@
call PrinterReceive
jr .end
-.init_player_number
+.establish_connection
ld a, [rSB]
- cp $1
+ cp USING_EXTERNAL_CLOCK
jr z, .player1
- cp $2
+ cp USING_INTERNAL_CLOCK
jr nz, .player2
.player1
ld [hSerialReceive], a
- ld [hLinkPlayerNumber], a
- cp $2
+ ld [hSerialConnectionStatus], a
+ cp USING_INTERNAL_CLOCK
jr z, ._player2
xor a
ld [rSB], a
- ld a, $3
+ ld a, 3
ld [rDIV], a
.wait_bit_7
@@ -76,9 +76,9 @@
ld [rSB], a
.player2
- ld a, $1
- ld [hFFCA], a
- ld a, $fe
+ ld a, TRUE
+ ld [hSerialReceivedNewData], a
+ ld a, SERIAL_NO_DATA_BYTE
ld [hSerialSend], a
.end
@@ -89,13 +89,13 @@
reti
; 75f
-Function75f:: ; 75f
+Serial_ExchangeBytes:: ; 75f
ld a, $1
- ld [hFFCC], a
+ ld [hSerialIgnoringInitialData], a
.loop
ld a, [hl]
ld [hSerialSend], a
- call Function78a
+ call Serial_ExchangeByte
push bc
ld b, a
inc hl
@@ -103,16 +103,16 @@
.wait
dec a
jr nz, .wait
- ld a, [hFFCC]
+ ld a, [hSerialIgnoringInitialData]
and a
ld a, b
pop bc
jr z, .load
dec hl
- cp $fd
+ cp SERIAL_PREAMBLE_BYTE
jr nz, .loop
xor a
- ld [hFFCC], a
+ ld [hSerialIgnoringInitialData], a
jr .loop
.load
@@ -125,24 +125,24 @@
ret
; 78a
-Function78a:: ; 78a
+Serial_ExchangeByte:: ; 78a
.loop
xor a
- ld [hFFCA], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld [hSerialReceivedNewData], a
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr nz, .not_player_2
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
.not_player_2
.loop2
- ld a, [hFFCA]
+ ld a, [hSerialReceivedNewData]
and a
jr nz, .reset_ffca
- ld a, [hLinkPlayerNumber]
- cp $1
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
jr nz, .not_player_1_or_wLinkTimeoutFrames_zero
call CheckwLinkTimeoutFramesNonzero
jr z, .not_player_1_or_wLinkTimeoutFrames_zero
@@ -162,8 +162,8 @@
.not_player_1_or_wLinkTimeoutFrames_zero
ld a, [rIE]
- and $f
- cp $8
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp 1 << SERIAL
jr nz, .loop2
ld a, [wcf5d]
dec a
@@ -173,8 +173,8 @@
dec a
ld [wcf5d + 1], a
jr nz, .loop2
- ld a, [hLinkPlayerNumber]
- cp $1
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
jr z, .reset_ffca
ld a, 255
@@ -184,19 +184,20 @@
.reset_ffca
xor a
- ld [hFFCA], a
+ ld [hSerialReceivedNewData], a
ld a, [rIE]
- and $f
- sub $8
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ sub 1 << SERIAL
jr nz, .rIE_not_equal_8
+ ; LOW($5000)
ld [wcf5d], a
- ld a, $50
+ ld a, HIGH($5000)
ld [wcf5d + 1], a
.rIE_not_equal_8
ld a, [hSerialReceive]
- cp $fe
+ cp SERIAL_NO_DATA_BYTE
ret nz
call CheckwLinkTimeoutFramesNonzero
jr z, .wLinkTimeoutFrames_zero
@@ -216,9 +217,9 @@
.wLinkTimeoutFrames_zero
ld a, [rIE]
- and $f
- cp $8
- ld a, $fe
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp 1 << SERIAL
+ ld a, SERIAL_NO_DATA_BYTE
ret z
ld a, [hl]
ld [hSerialSend], a
@@ -243,29 +244,31 @@
; 833
SerialDisconnected:: ; 833
- dec a
+ dec a ; a is always 0 when this is called
ld [wLinkTimeoutFrames], a
ld [wLinkTimeoutFrames + 1], a
ret
; 83b
-Function83b:: ; 83b
+; This is used to exchange the button press and selected menu item on the link menu.
+; The data is sent thrice and read twice to increase reliability.
+Serial_ExchangeLinkMenuSelection:: ; 83b
ld hl, wPlayerLinkAction
ld de, wOtherPlayerLinkMode
- ld c, $2
- ld a, $1
- ld [hFFCC], a
+ ld c, 2
+ ld a, TRUE
+ ld [hSerialIgnoringInitialData], a
.asm_847
call DelayFrame
ld a, [hl]
ld [hSerialSend], a
- call Function78a
+ call Serial_ExchangeByte
ld b, a
inc hl
- ld a, [hFFCC]
+ ld a, [hSerialIgnoringInitialData]
and a
- ld a, $0
- ld [hFFCC], a
+ ld a, FALSE
+ ld [hSerialIgnoringInitialData], a
jr nz, .asm_847
ld a, b
ld [de], a
@@ -275,7 +278,7 @@
ret
; 862
-Function862:: ; 862
+Serial_PrintWaitingTextAndSyncAndExchangeNybble:: ; 862
call LoadTileMapToTempTileMap
callfar PlaceWaitingText
call WaitLinkTransfer
@@ -282,8 +285,7 @@
jp Call_LoadTempTileMapToTileMap
; 871
-
-Function871:: ; 871
+Serial_SyncAndExchangeNybble:: ; 871
call LoadTileMapToTempTileMap
callfar PlaceWaitingText
jp WaitLinkTransfer
@@ -356,12 +358,12 @@
ld a, [wPlayerLinkAction]
add b
ld [hSerialSend], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
jr nz, .player_1
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
.player_1
@@ -388,12 +390,12 @@
; Let the other system know that the data has been received.
xor a
ld [hSerialSend], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
ret nz
- ld a, $1
+ ld a, (0 << rSC_ON) | 1
ld [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | 1
ld [rSC], a
ret
; 919
@@ -402,13 +404,13 @@
ld a, [wLinkMode]
and a
ret nz
- ld a, $2
+ ld a, USING_INTERNAL_CLOCK
ld [rSB], a
xor a
ld [hSerialReceive], a
- ld a, $0
+ ld a, 0 << rSC_ON
ld [rSC], a
- ld a, $80
+ ld a, 1 << rSC_ON
ld [rSC], a
ret
; 92e
--- a/home/video.asm
+++ b/home/video.asm
@@ -20,10 +20,10 @@
UpdateBGMapBuffer:: ; 15e3
-; Copy [hFFDC] 16x8 tiles from BGMapBuffer
+; Copy [hBGMapTileCount] 16x8 tiles from BGMapBuffer
; to bg map addresses in BGMapBufferPtrs.
-; [hFFDC] must be even since this is done in pairs.
+; [hBGMapTileCount] must be even since this is done in pairs.
; Return carry on success.
@@ -76,10 +76,10 @@
endr
; We've done 2 16x8 blocks
- ld a, [hFFDC]
+ ld a, [hBGMapTileCount]
dec a
dec a
- ld [hFFDC], a
+ ld [hBGMapTileCount], a
jr nz, .next
--- a/hram.asm
+++ b/hram.asm
@@ -39,7 +39,8 @@
hPrinter EQU $ffac
hGraphicStartTile EQU $ffad
hMoveMon EQU $ffae
-hMapObjectIndexBuffer EQU $ffaf
+
+hMapObjectIndexBuffer EQU $ffaf
hObjectStructIndexBuffer EQU $ffb0
hConnectionStripLength EQU $ffaf
@@ -88,13 +89,14 @@
hLCDCPointer EQU $ffc6
hLYOverrideStart EQU $ffc7
hLYOverrideEnd EQU $ffc8
-hMobileReceive EQU $ffc9
-hFFCA EQU $ffca
-hLinkPlayerNumber EQU $ffcb
-hFFCC EQU $ffcc
-hSerialSend EQU $ffcd
-hSerialReceive EQU $ffce
+hMobileReceive EQU $ffc9
+hSerialReceivedNewData EQU $ffca
+hSerialConnectionStatus EQU $ffcb
+hSerialIgnoringInitialData EQU $ffcc
+hSerialSend EQU $ffcd
+hSerialReceive EQU $ffce
+
hSCX EQU $ffcf
hSCY EQU $ffd0
hWX EQU $ffd1
@@ -108,7 +110,7 @@
hSPBuffer EQU $ffd9
hBGMapUpdate EQU $ffdb
-hFFDC EQU $ffdc
+hBGMapTileCount EQU $ffdc
hMapAnims EQU $ffde
hTileAnimFrame EQU $ffdf
@@ -125,5 +127,5 @@
hSGB EQU $ffe7
hDMATransfer EQU $ffe8
hMobile EQU $ffe9
-hFFEA EQU $ffea
+hSystemBooted EQU $ffea
hClockResetTrigger EQU $ffeb
--- a/mobile/mobile_40.asm
+++ b/mobile/mobile_40.asm
@@ -4056,7 +4056,7 @@
jr z, .got_link_player_number
ld a, 1
.got_link_player_number
- ld [hLinkPlayerNumber], a
+ ld [hSerialConnectionStatus], a
ret
; 101a21
@@ -4076,7 +4076,7 @@
xor a
ld [wDisableTextAcceleration], a
ld a, $ff
- ld [hLinkPlayerNumber], a
+ ld [hSerialConnectionStatus], a
pop af
ld [Options], a
ret
--- a/mobile/mobile_5b.asm
+++ b/mobile/mobile_5b.asm
@@ -4,7 +4,7 @@
and a
ret z
; Only do this once per boot cycle
- ld a, [hFFEA]
+ ld a, [hSystemBooted]
and a
ret z
; Set some flag, preserving the old state
@@ -19,9 +19,9 @@
call .RunJumptable
farcall DisableMobile
; Prevent this routine from running again
- ; until the next time the syatem is turned on
+ ; until the next time the system is turned on
xor a
- ld [hFFEA], a
+ ld [hSystemBooted], a
; Restore the flag state
pop af
ld [wcfbe], a