ref: 08b939375ec2eac1512c4dbddd184de6bbca8b98
parent: 1a69d6b888f9372060dad1af867c4a5c4113b40e
author: U-Daniel-PC\Daniel <corrnondacqb@yahoo.com>
date: Mon Jun 1 18:13:46 EDT 2015
EOL Windows->UNIX
--- a/constants/connection_constants.asm
+++ b/constants/connection_constants.asm
@@ -1,5 +1,5 @@
-; connection directions
-EAST EQU 1
-WEST EQU 2
-SOUTH EQU 4
-NORTH EQU 8
\ No newline at end of file
+; connection directions
+EAST EQU 1
+WEST EQU 2
+SOUTH EQU 4
+NORTH EQU 8
--- a/constants/evolution_constants.asm
+++ b/constants/evolution_constants.asm
@@ -1,4 +1,4 @@
-; Evolution types
-EV_LEVEL EQU 1
-EV_ITEM EQU 2
-EV_TRADE EQU 3
\ No newline at end of file
+; Evolution types
+EV_LEVEL EQU 1
+EV_ITEM EQU 2
+EV_TRADE EQU 3
--- a/constants/list_constants.asm
+++ b/constants/list_constants.asm
@@ -1,14 +1,14 @@
-; list menu ID's
-PCPOKEMONLISTMENU EQU $00 ; PC pokemon withdraw/deposit lists
-MOVESLISTMENU EQU $01 ; XXX where is this used?
-PRICEDITEMLISTMENU EQU $02 ; Pokemart buy menu / Pokemart buy/sell choose quantity menu
-ITEMLISTMENU EQU $03 ; Start menu Item menu / Pokemart sell menu
-SPECIALLISTMENU EQU $04 ; list of special "items" e.g. floor list in elevators / list of badges
-
-MONSTER_NAME EQU 1
-MOVE_NAME EQU 2
-; ???_NAME EQU 3
-ITEM_NAME EQU 4
-PLAYEROT_NAME EQU 5
-ENEMYOT_NAME EQU 6
-TRAINER_NAME EQU 7
\ No newline at end of file
+; list menu ID's
+PCPOKEMONLISTMENU EQU $00 ; PC pokemon withdraw/deposit lists
+MOVESLISTMENU EQU $01 ; XXX where is this used?
+PRICEDITEMLISTMENU EQU $02 ; Pokemart buy menu / Pokemart buy/sell choose quantity menu
+ITEMLISTMENU EQU $03 ; Start menu Item menu / Pokemart sell menu
+SPECIALLISTMENU EQU $04 ; list of special "items" e.g. floor list in elevators / list of badges
+
+MONSTER_NAME EQU 1
+MOVE_NAME EQU 2
+; ???_NAME EQU 3
+ITEM_NAME EQU 4
+PLAYEROT_NAME EQU 5
+ENEMYOT_NAME EQU 6
+TRAINER_NAME EQU 7
--- a/constants/oam_constants.asm
+++ b/constants/oam_constants.asm
@@ -1,9 +1,9 @@
-; OAM flags used by this game
-OAMFLAG_ENDOFDATA EQU %00000001 ; pseudo OAM flag, only used by game logic
-OAMFLAG_CANBEMASKED EQU %00000010 ; pseudo OAM flag, only used by game logic
-OAMFLAG_VFLIPPED EQU %00100000 ; OAM flag flips the sprite vertically.
-; Used for making left facing sprites face right and to alternate between left and right foot animation when walking up or down
-
-; OAM attribute flags
-OAM_HFLIP EQU %00100000 ; horizontal flip
-OAM_VFLIP EQU %01000000 ; vertical flip
\ No newline at end of file
+; OAM flags used by this game
+OAMFLAG_ENDOFDATA EQU %00000001 ; pseudo OAM flag, only used by game logic
+OAMFLAG_CANBEMASKED EQU %00000010 ; pseudo OAM flag, only used by game logic
+OAMFLAG_VFLIPPED EQU %00100000 ; OAM flag flips the sprite vertically.
+; Used for making left facing sprites face right and to alternate between left and right foot animation when walking up or down
+
+; OAM attribute flags
+OAM_HFLIP EQU %00100000 ; horizontal flip
+OAM_VFLIP EQU %01000000 ; vertical flip
--- a/constants/sprite_constants.asm
+++ b/constants/sprite_constants.asm
@@ -1,97 +1,97 @@
-; pokemon's overworld sprites
-SPRITE_MON EQU $0
-SPRITE_BALL_M EQU $1
-SPRITE_HELIX EQU $2
-SPRITE_FAIRY EQU $3
-SPRITE_BIRD_M EQU $4
-SPRITE_WATER EQU $5
-SPRITE_BUG EQU $6
-SPRITE_GRASS EQU $7
-SPRITE_SNAKE EQU $8
-SPRITE_QUADRUPED EQU $9
-
-; overworld sprites
-SPRITE_RED EQU $01
-SPRITE_BLUE EQU $02
-SPRITE_OAK EQU $03
-SPRITE_BUG_CATCHER EQU $04
-SPRITE_SLOWBRO EQU $05
-SPRITE_LASS EQU $06
-SPRITE_BLACK_HAIR_BOY_1 EQU $07
-SPRITE_LITTLE_GIRL EQU $08
-SPRITE_BIRD EQU $09
-SPRITE_FAT_BALD_GUY EQU $0a
-SPRITE_GAMBLER EQU $0b
-SPRITE_BLACK_HAIR_BOY_2 EQU $0c
-SPRITE_GIRL EQU $0d
-SPRITE_HIKER EQU $0e
-SPRITE_FOULARD_WOMAN EQU $0f
-SPRITE_GENTLEMAN EQU $10
-SPRITE_DAISY EQU $11
-SPRITE_BIKER EQU $12
-SPRITE_SAILOR EQU $13
-SPRITE_COOK EQU $14
-SPRITE_BIKE_SHOP_GUY EQU $15
-SPRITE_MR_FUJI EQU $16
-SPRITE_GIOVANNI EQU $17
-SPRITE_ROCKET EQU $18
-SPRITE_MEDIUM EQU $19
-SPRITE_WAITER EQU $1a
-SPRITE_ERIKA EQU $1b
-SPRITE_MOM_GEISHA EQU $1c
-SPRITE_BRUNETTE_GIRL EQU $1d
-SPRITE_LANCE EQU $1e
-SPRITE_OAK_SCIENTIST_AIDE EQU $1f
-SPRITE_OAK_AIDE EQU $20
-SPRITE_ROCKER EQU $21
-SPRITE_SWIMMER EQU $22
-SPRITE_WHITE_PLAYER EQU $23
-SPRITE_GYM_HELPER EQU $24
-SPRITE_OLD_PERSON EQU $25
-SPRITE_MART_GUY EQU $26
-SPRITE_FISHER EQU $27
-SPRITE_OLD_MEDIUM_WOMAN EQU $28
-SPRITE_NURSE EQU $29
-SPRITE_CABLE_CLUB_WOMAN EQU $2a
-SPRITE_MR_MASTERBALL EQU $2b
-SPRITE_LAPRAS_GIVER EQU $2c
-SPRITE_WARDEN EQU $2d
-SPRITE_SS_CAPTAIN EQU $2e
-SPRITE_FISHER2 EQU $2f
-SPRITE_BLACKBELT EQU $30
-SPRITE_GUARD EQU $31
-;SPRITE_COP_GUARD EQU $32
-SPRITE_MOM EQU $33
-SPRITE_BALDING_GUY EQU $34
-SPRITE_YOUNG_BOY EQU $35
-SPRITE_GAMEBOY_KID EQU $36
-SPRITE_GAMEBOY_KID_COPY EQU $37
-SPRITE_CLEFAIRY EQU $38
-SPRITE_AGATHA EQU $39
-SPRITE_BRUNO EQU $3a
-SPRITE_LORELEI EQU $3b
-SPRITE_SEEL EQU $3c
-SPRITE_BALL EQU $3d
-SPRITE_OMANYTE EQU $3e
-SPRITE_BOULDER EQU $3f
-SPRITE_PAPER_SHEET EQU $40
-SPRITE_BOOK_MAP_DEX EQU $41
-SPRITE_CLIPBOARD EQU $42
-SPRITE_SNORLAX EQU $43
-SPRITE_OLD_AMBER_COPY EQU $44
-SPRITE_OLD_AMBER EQU $45
-SPRITE_LYING_OLD_MAN_UNUSED_1 EQU $46
-SPRITE_LYING_OLD_MAN_UNUSED_2 EQU $47
-SPRITE_LYING_OLD_MAN EQU $48
-
-; different kinds of people events
-ITEM EQU $80
-TRAINER EQU $40
-
-BOULDER_MOVEMENT_BYTE_2 EQU $10
-
-; sprite facing directions
-SPRITE_FACING_DOWN EQU $00
-SPRITE_FACING_UP EQU $04
-SPRITE_FACING_LEFT EQU $08
-SPRITE_FACING_RIGHT EQU $0C
\ No newline at end of file
+; pokemon's overworld sprites
+SPRITE_MON EQU $0
+SPRITE_BALL_M EQU $1
+SPRITE_HELIX EQU $2
+SPRITE_FAIRY EQU $3
+SPRITE_BIRD_M EQU $4
+SPRITE_WATER EQU $5
+SPRITE_BUG EQU $6
+SPRITE_GRASS EQU $7
+SPRITE_SNAKE EQU $8
+SPRITE_QUADRUPED EQU $9
+
+; overworld sprites
+SPRITE_RED EQU $01
+SPRITE_BLUE EQU $02
+SPRITE_OAK EQU $03
+SPRITE_BUG_CATCHER EQU $04
+SPRITE_SLOWBRO EQU $05
+SPRITE_LASS EQU $06
+SPRITE_BLACK_HAIR_BOY_1 EQU $07
+SPRITE_LITTLE_GIRL EQU $08
+SPRITE_BIRD EQU $09
+SPRITE_FAT_BALD_GUY EQU $0a
+SPRITE_GAMBLER EQU $0b
+SPRITE_BLACK_HAIR_BOY_2 EQU $0c
+SPRITE_GIRL EQU $0d
+SPRITE_HIKER EQU $0e
+SPRITE_FOULARD_WOMAN EQU $0f
+SPRITE_GENTLEMAN EQU $10
+SPRITE_DAISY EQU $11
+SPRITE_BIKER EQU $12
+SPRITE_SAILOR EQU $13
+SPRITE_COOK EQU $14
+SPRITE_BIKE_SHOP_GUY EQU $15
+SPRITE_MR_FUJI EQU $16
+SPRITE_GIOVANNI EQU $17
+SPRITE_ROCKET EQU $18
+SPRITE_MEDIUM EQU $19
+SPRITE_WAITER EQU $1a
+SPRITE_ERIKA EQU $1b
+SPRITE_MOM_GEISHA EQU $1c
+SPRITE_BRUNETTE_GIRL EQU $1d
+SPRITE_LANCE EQU $1e
+SPRITE_OAK_SCIENTIST_AIDE EQU $1f
+SPRITE_OAK_AIDE EQU $20
+SPRITE_ROCKER EQU $21
+SPRITE_SWIMMER EQU $22
+SPRITE_WHITE_PLAYER EQU $23
+SPRITE_GYM_HELPER EQU $24
+SPRITE_OLD_PERSON EQU $25
+SPRITE_MART_GUY EQU $26
+SPRITE_FISHER EQU $27
+SPRITE_OLD_MEDIUM_WOMAN EQU $28
+SPRITE_NURSE EQU $29
+SPRITE_CABLE_CLUB_WOMAN EQU $2a
+SPRITE_MR_MASTERBALL EQU $2b
+SPRITE_LAPRAS_GIVER EQU $2c
+SPRITE_WARDEN EQU $2d
+SPRITE_SS_CAPTAIN EQU $2e
+SPRITE_FISHER2 EQU $2f
+SPRITE_BLACKBELT EQU $30
+SPRITE_GUARD EQU $31
+;SPRITE_COP_GUARD EQU $32
+SPRITE_MOM EQU $33
+SPRITE_BALDING_GUY EQU $34
+SPRITE_YOUNG_BOY EQU $35
+SPRITE_GAMEBOY_KID EQU $36
+SPRITE_GAMEBOY_KID_COPY EQU $37
+SPRITE_CLEFAIRY EQU $38
+SPRITE_AGATHA EQU $39
+SPRITE_BRUNO EQU $3a
+SPRITE_LORELEI EQU $3b
+SPRITE_SEEL EQU $3c
+SPRITE_BALL EQU $3d
+SPRITE_OMANYTE EQU $3e
+SPRITE_BOULDER EQU $3f
+SPRITE_PAPER_SHEET EQU $40
+SPRITE_BOOK_MAP_DEX EQU $41
+SPRITE_CLIPBOARD EQU $42
+SPRITE_SNORLAX EQU $43
+SPRITE_OLD_AMBER_COPY EQU $44
+SPRITE_OLD_AMBER EQU $45
+SPRITE_LYING_OLD_MAN_UNUSED_1 EQU $46
+SPRITE_LYING_OLD_MAN_UNUSED_2 EQU $47
+SPRITE_LYING_OLD_MAN EQU $48
+
+; different kinds of people events
+ITEM EQU $80
+TRAINER EQU $40
+
+BOULDER_MOVEMENT_BYTE_2 EQU $10
+
+; sprite facing directions
+SPRITE_FACING_DOWN EQU $00
+SPRITE_FACING_UP EQU $04
+SPRITE_FACING_LEFT EQU $08
+SPRITE_FACING_RIGHT EQU $0C
--- a/constants/status_constants.asm
+++ b/constants/status_constants.asm
@@ -1,32 +1,32 @@
-; non-volatile statuses
-SLP EQU %111 ; sleep counter
-PSN EQU 3
-BRN EQU 4
-FRZ EQU 5
-PAR EQU 6
-
-; volatile statuses 1
-StoringEnergy EQU 0 ; Bide
-ThrashingAbout EQU 1 ; e.g. Thrash
-AttackingMultipleTimes EQU 2 ; e.g. Double Kick, Fury Attack
-Flinched EQU 3
-ChargingUp EQU 4 ; e.g. Solar Beam, Fly
-UsingTrappingMove EQU 5 ; e.g. Wrap
-Invulnerable EQU 6 ; charging up Fly/Dig
-Confused EQU 7
-
-; volatile statuses 2
-UsingXAccuracy EQU 0
-ProtectedByMist EQU 1
-GettingPumped EQU 2 ; Focus Energy
-; EQU 3 ; unused?
-HasSubstituteUp EQU 4
-NeedsToRecharge EQU 5 ; Hyper Beam
-UsingRage EQU 6
-Seeded EQU 7
-
-; volatile statuses 3
-BadlyPoisoned EQU 0
-HasLightScreenUp EQU 1
-HasReflectUp EQU 2
-Transformed EQU 3
+; non-volatile statuses
+SLP EQU %111 ; sleep counter
+PSN EQU 3
+BRN EQU 4
+FRZ EQU 5
+PAR EQU 6
+
+; volatile statuses 1
+StoringEnergy EQU 0 ; Bide
+ThrashingAbout EQU 1 ; e.g. Thrash
+AttackingMultipleTimes EQU 2 ; e.g. Double Kick, Fury Attack
+Flinched EQU 3
+ChargingUp EQU 4 ; e.g. Solar Beam, Fly
+UsingTrappingMove EQU 5 ; e.g. Wrap
+Invulnerable EQU 6 ; charging up Fly/Dig
+Confused EQU 7
+
+; volatile statuses 2
+UsingXAccuracy EQU 0
+ProtectedByMist EQU 1
+GettingPumped EQU 2 ; Focus Energy
+; EQU 3 ; unused?
+HasSubstituteUp EQU 4
+NeedsToRecharge EQU 5 ; Hyper Beam
+UsingRage EQU 6
+Seeded EQU 7
+
+; volatile statuses 3
+BadlyPoisoned EQU 0
+HasLightScreenUp EQU 1
+HasReflectUp EQU 2
+Transformed EQU 3
--- a/constants/type_constants.asm
+++ b/constants/type_constants.asm
@@ -1,16 +1,16 @@
-; Elemental types
-NORMAL EQU $00
-FIGHTING EQU $01
-FLYING EQU $02
-POISON EQU $03
-GROUND EQU $04
-ROCK EQU $05
-BUG EQU $07
-GHOST EQU $08
-FIRE EQU $14
-WATER EQU $15
-GRASS EQU $16
-ELECTRIC EQU $17
-PSYCHIC EQU $18
-ICE EQU $19
-DRAGON EQU $1A
\ No newline at end of file
+; Elemental types
+NORMAL EQU $00
+FIGHTING EQU $01
+FLYING EQU $02
+POISON EQU $03
+GROUND EQU $04
+ROCK EQU $05
+BUG EQU $07
+GHOST EQU $08
+FIRE EQU $14
+WATER EQU $15
+GRASS EQU $16
+ELECTRIC EQU $17
+PSYCHIC EQU $18
+ICE EQU $19
+DRAGON EQU $1A
--- a/home/serial.asm
+++ b/home/serial.asm
@@ -1,312 +1,312 @@
-Serial:: ; 2125 (0:2125)
- push af
- push bc
- push de
- push hl
- ld a, [hSerialConnectionStatus]
- inc a
- jr z, .connectionNotYetEstablished
- ld a, [rSB]
- ld [hSerialReceiveData], a
- ld a, [hSerialSendData]
- ld [rSB], a
- ld a, [hSerialConnectionStatus]
- cp USING_INTERNAL_CLOCK
- jr z, .done
-; using external clock
- ld a, START_TRANSFER_EXTERNAL_CLOCK
- ld [rSC], a
- jr .done
-.connectionNotYetEstablished
- ld a, [rSB]
- ld [hSerialReceiveData], a
- ld [hSerialConnectionStatus], a
- cp USING_INTERNAL_CLOCK
- jr z, .usingInternalClock
-; using external clock
- xor a
- ld [rSB], a
- ld a, $3
- ld [rDIV], a
-.waitLoop
- ld a, [rDIV]
- bit 7, a
- jr nz, .waitLoop
- ld a, START_TRANSFER_EXTERNAL_CLOCK
- ld [rSC], a
- jr .done
-.usingInternalClock
- xor a
- ld [rSB], a
-.done
- ld a, $1
- ld [hSerialReceivedNewData], a
- ld a, SERIAL_NO_DATA_BYTE
- ld [hSerialSendData], a
- pop hl
- pop de
- pop bc
- pop af
- reti
-
-; hl = send data
-; de = receive data
-; bc = length of data
-Serial_ExchangeBytes:: ; 216f (0:216f)
- ld a, 1
- ld [hSerialIgnoringInitialData], a
-.loop
- ld a, [hl]
- ld [hSerialSendData], a
- call Serial_ExchangeByte
- push bc
- ld b, a
- inc hl
- ld a, 48
-.waitLoop
- dec a
- jr nz, .waitLoop
- ld a, [hSerialIgnoringInitialData]
- and a
- ld a, b
- pop bc
- jr z, .storeReceivedByte
- dec hl
- cp SERIAL_PREAMBLE_BYTE
- jr nz, .loop
- xor a
- ld [hSerialIgnoringInitialData], a
- jr .loop
-.storeReceivedByte
- ld [de], a
- inc de
- dec bc
- ld a, b
- or c
- jr nz, .loop
- ret
-
-Serial_ExchangeByte:: ; 219a (0:219a)
- xor a
- ld [hSerialReceivedNewData], a
- ld a, [hSerialConnectionStatus]
- cp USING_INTERNAL_CLOCK
- jr nz, .asm_21a7
- ld a, START_TRANSFER_INTERNAL_CLOCK
- ld [rSC], a
-.asm_21a7
- ld a, [hSerialReceivedNewData]
- and a
- jr nz, .asm_21f1
- ld a, [hSerialConnectionStatus]
- cp USING_EXTERNAL_CLOCK
- jr nz, .asm_21cc
- call IsUnknownCounterZero
- jr z, .asm_21cc
- call WaitLoop_15Iterations
- push hl
- ld hl, wUnknownSerialCounter + 1
- inc [hl]
- jr nz, .asm_21c3
- dec hl
- inc [hl]
-.asm_21c3
- pop hl
- call IsUnknownCounterZero
- jr nz, .asm_21a7
- jp SetUnknownCounterToFFFF
-.asm_21cc
- ld a, [rIE]
- and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
- cp (1 << SERIAL)
- jr nz, .asm_21a7
- ld a, [wUnknownSerialCounter2]
- dec a
- ld [wUnknownSerialCounter2], a
- jr nz, .asm_21a7
- ld a, [wUnknownSerialCounter2 + 1]
- dec a
- ld [wUnknownSerialCounter2 + 1], a
- jr nz, .asm_21a7
- ld a, [hSerialConnectionStatus]
- cp USING_EXTERNAL_CLOCK
- jr z, .asm_21f1
- ld a, 255
-.waitLoop
- dec a
- jr nz, .waitLoop
-.asm_21f1
- xor a
- ld [hSerialReceivedNewData], a
- ld a, [rIE]
- and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
- sub (1 << SERIAL)
- jr nz, .asm_2204
- ld [wUnknownSerialCounter2], a
- ld a, $50
- ld [wUnknownSerialCounter2 + 1], a
-.asm_2204
- ld a, [hSerialReceiveData]
- cp SERIAL_NO_DATA_BYTE
- ret nz
- call IsUnknownCounterZero
- jr z, .asm_221f
- push hl
- ld hl, wUnknownSerialCounter + 1
- ld a, [hl]
- dec a
- ld [hld], a
- inc a
- jr nz, .asm_2219
- dec [hl]
-.asm_2219
- pop hl
- call IsUnknownCounterZero
- jr z, SetUnknownCounterToFFFF
-.asm_221f
- ld a, [rIE]
- 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 [hSerialSendData], a
- call DelayFrame
- jp Serial_ExchangeByte
-
-WaitLoop_15Iterations:: ; 2231 (0:2231)
- ld a, 15
-.waitLoop
- dec a
- jr nz, .waitLoop
- ret
-
-IsUnknownCounterZero:: ; 2237 (0:2237)
- push hl
- ld hl, wUnknownSerialCounter
- ld a, [hli]
- or [hl]
- pop hl
- ret
-
-; a is always 0 when this is called
-SetUnknownCounterToFFFF:: ; 223f (0:223f)
- dec a
- ld [wUnknownSerialCounter], a
- ld [wUnknownSerialCounter + 1], a
- ret
-
-; 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:: ; 2247 (0:2247)
- ld hl, wLinkMenuSelectionSendBuffer
- ld de, wLinkMenuSelectionReceiveBuffer
- ld c, 2 ; number of bytes to save
- ld a, 1
- ld [hSerialIgnoringInitialData], a
-.loop
- call DelayFrame
- ld a, [hl]
- ld [hSerialSendData], a
- call Serial_ExchangeByte
- ld b, a
- inc hl
- ld a, [hSerialIgnoringInitialData]
- and a
- ld a, 0
- ld [hSerialIgnoringInitialData], a
- jr nz, .loop
- ld a, b
- ld [de], a
- inc de
- dec c
- jr nz, .loop
- ret
-
-Serial_PrintWaitingTextAndSyncAndExchangeNybble:: ; 226e (0:226e)
- call SaveScreenTilesToBuffer1
- callab PrintWaitingText
- call Serial_SyncAndExchangeNybble
- jp LoadScreenTilesFromBuffer1
-
-Serial_SyncAndExchangeNybble:: ; 227f (0:227f)
- ld a, $ff
- ld [wSerialExchangeNybbleReceiveData], a
-.loop1
- call Serial_ExchangeNybble
- call DelayFrame
- call IsUnknownCounterZero
- jr z, .next1
- push hl
- ld hl, wUnknownSerialCounter + 1
- dec [hl]
- jr nz, .next2
- dec hl
- dec [hl]
- jr nz, .next2
- pop hl
- xor a
- jp SetUnknownCounterToFFFF
-.next2
- pop hl
-.next1
- ld a, [wSerialExchangeNybbleReceiveData]
- inc a
- jr z, .loop1
- ld b, 10
-.loop2
- call DelayFrame
- call Serial_ExchangeNybble
- dec b
- jr nz, .loop2
- ld b, 10
-.loop3
- call DelayFrame
- call Serial_SendZeroByte
- dec b
- jr nz, .loop3
- ld a, [wSerialExchangeNybbleReceiveData]
- ld [wSerialSyncAndExchangeNybbleReceiveData], a
- ret
-
-Serial_ExchangeNybble:: ; 22c3 (0:22c3)
- call .doExchange
- ld a, [wSerialExchangeNybbleSendData]
- add $60
- ld [hSerialSendData], a
- ld a, [hSerialConnectionStatus]
- cp USING_INTERNAL_CLOCK
- jr nz, .doExchange
- ld a, START_TRANSFER_INTERNAL_CLOCK
- ld [rSC], a
-.doExchange
- ld a, [hSerialReceiveData]
- ld [wSerialExchangeNybbleTempReceiveData], a
- and $f0
- cp $60
- ret nz
- xor a
- ld [hSerialReceiveData], a
- ld a, [wSerialExchangeNybbleTempReceiveData]
- and $f
- ld [wSerialExchangeNybbleReceiveData], a
- ret
-
-Serial_SendZeroByte:: ; 22ed (0:22ed)
- xor a
- ld [hSerialSendData], a
- ld a, [hSerialConnectionStatus]
- cp USING_INTERNAL_CLOCK
- ret nz
- ld a, START_TRANSFER_INTERNAL_CLOCK
- ld [rSC], a
- ret
-
-Serial_TryEstablishingExternallyClockedConnection:: ; 22fa (0:22fa)
- ld a, ESTABLISH_CONNECTION_WITH_EXTERNAL_CLOCK
- ld [rSB], a
- xor a
- ld [hSerialReceiveData], a
- ld a, START_TRANSFER_EXTERNAL_CLOCK
- ld [rSC], a
- ret
\ No newline at end of file
+Serial:: ; 2125 (0:2125)
+ push af
+ push bc
+ push de
+ push hl
+ ld a, [hSerialConnectionStatus]
+ inc a
+ jr z, .connectionNotYetEstablished
+ ld a, [rSB]
+ ld [hSerialReceiveData], a
+ ld a, [hSerialSendData]
+ ld [rSB], a
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
+ jr z, .done
+; using external clock
+ ld a, START_TRANSFER_EXTERNAL_CLOCK
+ ld [rSC], a
+ jr .done
+.connectionNotYetEstablished
+ ld a, [rSB]
+ ld [hSerialReceiveData], a
+ ld [hSerialConnectionStatus], a
+ cp USING_INTERNAL_CLOCK
+ jr z, .usingInternalClock
+; using external clock
+ xor a
+ ld [rSB], a
+ ld a, $3
+ ld [rDIV], a
+.waitLoop
+ ld a, [rDIV]
+ bit 7, a
+ jr nz, .waitLoop
+ ld a, START_TRANSFER_EXTERNAL_CLOCK
+ ld [rSC], a
+ jr .done
+.usingInternalClock
+ xor a
+ ld [rSB], a
+.done
+ ld a, $1
+ ld [hSerialReceivedNewData], a
+ ld a, SERIAL_NO_DATA_BYTE
+ ld [hSerialSendData], a
+ pop hl
+ pop de
+ pop bc
+ pop af
+ reti
+
+; hl = send data
+; de = receive data
+; bc = length of data
+Serial_ExchangeBytes:: ; 216f (0:216f)
+ ld a, 1
+ ld [hSerialIgnoringInitialData], a
+.loop
+ ld a, [hl]
+ ld [hSerialSendData], a
+ call Serial_ExchangeByte
+ push bc
+ ld b, a
+ inc hl
+ ld a, 48
+.waitLoop
+ dec a
+ jr nz, .waitLoop
+ ld a, [hSerialIgnoringInitialData]
+ and a
+ ld a, b
+ pop bc
+ jr z, .storeReceivedByte
+ dec hl
+ cp SERIAL_PREAMBLE_BYTE
+ jr nz, .loop
+ xor a
+ ld [hSerialIgnoringInitialData], a
+ jr .loop
+.storeReceivedByte
+ ld [de], a
+ inc de
+ dec bc
+ ld a, b
+ or c
+ jr nz, .loop
+ ret
+
+Serial_ExchangeByte:: ; 219a (0:219a)
+ xor a
+ ld [hSerialReceivedNewData], a
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
+ jr nz, .asm_21a7
+ ld a, START_TRANSFER_INTERNAL_CLOCK
+ ld [rSC], a
+.asm_21a7
+ ld a, [hSerialReceivedNewData]
+ and a
+ jr nz, .asm_21f1
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
+ jr nz, .asm_21cc
+ call IsUnknownCounterZero
+ jr z, .asm_21cc
+ call WaitLoop_15Iterations
+ push hl
+ ld hl, wUnknownSerialCounter + 1
+ inc [hl]
+ jr nz, .asm_21c3
+ dec hl
+ inc [hl]
+.asm_21c3
+ pop hl
+ call IsUnknownCounterZero
+ jr nz, .asm_21a7
+ jp SetUnknownCounterToFFFF
+.asm_21cc
+ ld a, [rIE]
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp (1 << SERIAL)
+ jr nz, .asm_21a7
+ ld a, [wUnknownSerialCounter2]
+ dec a
+ ld [wUnknownSerialCounter2], a
+ jr nz, .asm_21a7
+ ld a, [wUnknownSerialCounter2 + 1]
+ dec a
+ ld [wUnknownSerialCounter2 + 1], a
+ jr nz, .asm_21a7
+ ld a, [hSerialConnectionStatus]
+ cp USING_EXTERNAL_CLOCK
+ jr z, .asm_21f1
+ ld a, 255
+.waitLoop
+ dec a
+ jr nz, .waitLoop
+.asm_21f1
+ xor a
+ ld [hSerialReceivedNewData], a
+ ld a, [rIE]
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ sub (1 << SERIAL)
+ jr nz, .asm_2204
+ ld [wUnknownSerialCounter2], a
+ ld a, $50
+ ld [wUnknownSerialCounter2 + 1], a
+.asm_2204
+ ld a, [hSerialReceiveData]
+ cp SERIAL_NO_DATA_BYTE
+ ret nz
+ call IsUnknownCounterZero
+ jr z, .asm_221f
+ push hl
+ ld hl, wUnknownSerialCounter + 1
+ ld a, [hl]
+ dec a
+ ld [hld], a
+ inc a
+ jr nz, .asm_2219
+ dec [hl]
+.asm_2219
+ pop hl
+ call IsUnknownCounterZero
+ jr z, SetUnknownCounterToFFFF
+.asm_221f
+ ld a, [rIE]
+ 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 [hSerialSendData], a
+ call DelayFrame
+ jp Serial_ExchangeByte
+
+WaitLoop_15Iterations:: ; 2231 (0:2231)
+ ld a, 15
+.waitLoop
+ dec a
+ jr nz, .waitLoop
+ ret
+
+IsUnknownCounterZero:: ; 2237 (0:2237)
+ push hl
+ ld hl, wUnknownSerialCounter
+ ld a, [hli]
+ or [hl]
+ pop hl
+ ret
+
+; a is always 0 when this is called
+SetUnknownCounterToFFFF:: ; 223f (0:223f)
+ dec a
+ ld [wUnknownSerialCounter], a
+ ld [wUnknownSerialCounter + 1], a
+ ret
+
+; 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:: ; 2247 (0:2247)
+ ld hl, wLinkMenuSelectionSendBuffer
+ ld de, wLinkMenuSelectionReceiveBuffer
+ ld c, 2 ; number of bytes to save
+ ld a, 1
+ ld [hSerialIgnoringInitialData], a
+.loop
+ call DelayFrame
+ ld a, [hl]
+ ld [hSerialSendData], a
+ call Serial_ExchangeByte
+ ld b, a
+ inc hl
+ ld a, [hSerialIgnoringInitialData]
+ and a
+ ld a, 0
+ ld [hSerialIgnoringInitialData], a
+ jr nz, .loop
+ ld a, b
+ ld [de], a
+ inc de
+ dec c
+ jr nz, .loop
+ ret
+
+Serial_PrintWaitingTextAndSyncAndExchangeNybble:: ; 226e (0:226e)
+ call SaveScreenTilesToBuffer1
+ callab PrintWaitingText
+ call Serial_SyncAndExchangeNybble
+ jp LoadScreenTilesFromBuffer1
+
+Serial_SyncAndExchangeNybble:: ; 227f (0:227f)
+ ld a, $ff
+ ld [wSerialExchangeNybbleReceiveData], a
+.loop1
+ call Serial_ExchangeNybble
+ call DelayFrame
+ call IsUnknownCounterZero
+ jr z, .next1
+ push hl
+ ld hl, wUnknownSerialCounter + 1
+ dec [hl]
+ jr nz, .next2
+ dec hl
+ dec [hl]
+ jr nz, .next2
+ pop hl
+ xor a
+ jp SetUnknownCounterToFFFF
+.next2
+ pop hl
+.next1
+ ld a, [wSerialExchangeNybbleReceiveData]
+ inc a
+ jr z, .loop1
+ ld b, 10
+.loop2
+ call DelayFrame
+ call Serial_ExchangeNybble
+ dec b
+ jr nz, .loop2
+ ld b, 10
+.loop3
+ call DelayFrame
+ call Serial_SendZeroByte
+ dec b
+ jr nz, .loop3
+ ld a, [wSerialExchangeNybbleReceiveData]
+ ld [wSerialSyncAndExchangeNybbleReceiveData], a
+ ret
+
+Serial_ExchangeNybble:: ; 22c3 (0:22c3)
+ call .doExchange
+ ld a, [wSerialExchangeNybbleSendData]
+ add $60
+ ld [hSerialSendData], a
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
+ jr nz, .doExchange
+ ld a, START_TRANSFER_INTERNAL_CLOCK
+ ld [rSC], a
+.doExchange
+ ld a, [hSerialReceiveData]
+ ld [wSerialExchangeNybbleTempReceiveData], a
+ and $f0
+ cp $60
+ ret nz
+ xor a
+ ld [hSerialReceiveData], a
+ ld a, [wSerialExchangeNybbleTempReceiveData]
+ and $f
+ ld [wSerialExchangeNybbleReceiveData], a
+ ret
+
+Serial_SendZeroByte:: ; 22ed (0:22ed)
+ xor a
+ ld [hSerialSendData], a
+ ld a, [hSerialConnectionStatus]
+ cp USING_INTERNAL_CLOCK
+ ret nz
+ ld a, START_TRANSFER_INTERNAL_CLOCK
+ ld [rSC], a
+ ret
+
+Serial_TryEstablishingExternallyClockedConnection:: ; 22fa (0:22fa)
+ ld a, ESTABLISH_CONNECTION_WITH_EXTERNAL_CLOCK
+ ld [rSB], a
+ xor a
+ ld [hSerialReceiveData], a
+ ld a, START_TRANSFER_EXTERNAL_CLOCK
+ ld [rSC], a
+ ret