summaryrefslogtreecommitdiff
path: root/home/serial.asm
diff options
context:
space:
mode:
Diffstat (limited to 'home/serial.asm')
-rw-r--r--home/serial.asm169
1 files changed, 92 insertions, 77 deletions
diff --git a/home/serial.asm b/home/serial.asm
index bd51084e..0fb1750b 100644
--- a/home/serial.asm
+++ b/home/serial.asm
@@ -1,4 +1,6 @@
-Serial:: ; 6aa (0:06aa)
+Serial::
+; The serial interrupt.
+
push af
push bc
push de
@@ -9,18 +11,22 @@ Serial:: ; 6aa (0:06aa)
jr nz, .printer
ldh a, [hLinkPlayerNumber]
- inc a
- jr z, .init_player_number
+ inc a ; is it equal to CONNECTION_NOT_ESTABLISHED?
+ jr z, .establish_connection
+
ldh a, [rSB]
ldh [hSerialReceive], a
+
ldh a, [hSerialSend]
ldh [rSB], a
+
ldh a, [hLinkPlayerNumber]
- cp $2
+ cp USING_INTERNAL_CLOCK
jr z, .player2
- ld a, 0 << rSC_ON
+
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
- ld a, 1 << rSC_ON
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
jr .player2
@@ -28,22 +34,22 @@ Serial:: ; 6aa (0:06aa)
call PrinterReceive
jr .end
-.init_player_number
+.establish_connection
ldh a, [rSB]
- cp $1
+ cp USING_EXTERNAL_CLOCK
jr z, .player1
- cp $2
+ cp USING_INTERNAL_CLOCK
jr nz, .player2
.player1
ldh [hSerialReceive], a
ldh [hLinkPlayerNumber], a
- cp $2
+ cp USING_INTERNAL_CLOCK
jr z, ._player2
xor a
ldh [rSB], a
- ld a, $3
+ ld a, 3
ldh [rDIV], a
.wait_bit_7
@@ -51,18 +57,19 @@ Serial:: ; 6aa (0:06aa)
bit 7, a
jr nz, .wait_bit_7
- ; cycle the serial controller
- ld a, 0 << rSC_ON
+ ; Cycle the serial controller
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
- ld a, 1 << rSC_ON
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
jr .player2
._player2
xor a
ldh [rSB], a
+
.player2
- ld a, $1
+ ld a, TRUE
ldh [hFFCC], a
ld a, SERIAL_NO_DATA_BYTE
ldh [hSerialSend], a
@@ -74,13 +81,13 @@ Serial:: ; 6aa (0:06aa)
pop af
reti
-Function710:: ; 710
- ld a, $1
+Serial_ExchangeBytes::
+ ld a, 1
ldh [hFFCE], a
.loop
ld a, [hl]
ldh [hSerialSend], a
- call Function73b
+ call Serial_ExchangeByte
push bc
ld b, a
inc hl
@@ -109,27 +116,29 @@ Function710:: ; 710
jr nz, .loop
ret
-Function73b:: ; 73b (0:073b)
+Serial_ExchangeByte::
.loop
xor a
ldh [hFFCC], a
ldh a, [hLinkPlayerNumber]
- cp $2
- jr nz, .loop2
- ld a, (1 << rSC_CLOCK) | (0 << rSC_ON)
+ cp 2
+ jr nz, .not_player_2
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
- ld a, (1 << rSC_CLOCK) | (1 << rSC_ON)
+ ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
+
+.not_player_2
.loop2
ldh a, [hFFCC]
and a
jr nz, .reset_ffcc
ldh a, [hLinkPlayerNumber]
- cp $1
+ cp 1
jr nz, .not_player_1_or_wLinkTimeoutFrames_zero
- call CheckLinkTimeout
+ call CheckwLinkTimeoutFramesNonzero
jr z, .not_player_1_or_wLinkTimeoutFrames_zero
- call Serial15CycleDelay
+ call .delay_15_cycles
push hl
ld hl, wLinkTimeoutFrames + 1
inc [hl]
@@ -139,14 +148,14 @@ Function73b:: ; 73b (0:073b)
.no_rollover_up
pop hl
- call CheckLinkTimeout
+ call CheckwLinkTimeoutFramesNonzero
jr nz, .loop2
jp SerialDisconnected
.not_player_1_or_wLinkTimeoutFrames_zero
ldh a, [rIE]
- and $f
- cp $8
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp 1 << SERIAL
jr nz, .loop2
ld a, [wce5d]
dec a
@@ -157,10 +166,10 @@ Function73b:: ; 73b (0:073b)
ld [wce5d + 1], a
jr nz, .loop2
ldh a, [hLinkPlayerNumber]
- cp $1
+ cp 1
jr z, .reset_ffcc
-
- ld a, $ff
+
+ ld a, 255
.delay_255_cycles
dec a
jr nz, .delay_255_cycles
@@ -169,20 +178,21 @@ Function73b:: ; 73b (0:073b)
xor a
ldh [hFFCC], a
ldh 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 [wce5d], a
- ld a, 80
+ ld a, HIGH($5000)
ld [wce5d + 1], a
.rIE_not_equal_8
ldh a, [hSerialReceive]
cp SERIAL_NO_DATA_BYTE
ret nz
- call CheckLinkTimeout
- jr z, .link_timed_out
+ call CheckwLinkTimeoutFramesNonzero
+ jr z, .linkTimeoutFrames_zero
push hl
ld hl, wLinkTimeoutFrames + 1
ld a, [hl]
@@ -194,13 +204,13 @@ Function73b:: ; 73b (0:073b)
.no_rollover
pop hl
- call CheckLinkTimeout
+ call CheckwLinkTimeoutFramesNonzero
jr z, SerialDisconnected
-.link_timed_out
+.linkTimeoutFrames_zero
ldh a, [rIE]
- and $f
- cp $8
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp 1 << SERIAL
ld a, SERIAL_NO_DATA_BYTE
ret z
ld a, [hl]
@@ -208,14 +218,14 @@ Function73b:: ; 73b (0:073b)
call DelayFrame
jp .loop
-Serial15CycleDelay:: ; 7d6 (0:07d6)
- ld a, $f
-.loop
+.delay_15_cycles
+ ld a, 15
+.delay_cycles
dec a
- jr nz, .loop
+ jr nz, .delay_cycles
ret
-CheckLinkTimeout:: ; 7dc (0:07dc)
+CheckwLinkTimeoutFramesNonzero::
push hl
ld hl, wLinkTimeoutFrames
ld a, [hli]
@@ -223,28 +233,30 @@ CheckLinkTimeout:: ; 7dc (0:07dc)
pop hl
ret
-SerialDisconnected:: ; 7e4 (0:07e4)
- dec a
+SerialDisconnected::
+ dec a ; a is always 0 when this is called
ld [wLinkTimeoutFrames], a
ld [wLinkTimeoutFrames + 1], a
ret
-Function7ec:: ; 7ec
+; 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::
ld hl, wPlayerLinkAction
ld de, wOtherPlayerLinkMode
- ld c, $2
- ld a, $1
+ ld c, 2
+ ld a, TRUE
ldh [hFFCE], a
.asm_7f8
call DelayFrame
ld a, [hl]
ldh [hSerialSend], a
- call Function73b
+ call Serial_ExchangeByte
ld b, a
inc hl
ldh a, [hFFCE]
and a
- ld a, $0
+ ld a, 0
ldh [hFFCE], a
jr nz, .asm_7f8
ld a, b
@@ -254,26 +266,26 @@ Function7ec:: ; 7ec
jr nz, .asm_7f8
ret
-Function813:: ; 813
- call LoadTileMapToTempTileMap
+Serial_PrintWaitingTextAndSyncAndExchangeNybble::
+ call LoadTilemapToTempTilemap
callfar PlaceWaitingText
call WaitLinkTransfer
- jp Call_LoadTempTileMapToTileMap
+ jp SafeLoadTempTilemapToTilemap
-Function822:: ; 822
- call LoadTileMapToTempTileMap
+Serial_SyncAndExchangeNybble::
+ call LoadTilemapToTempTilemap
callfar PlaceWaitingText
jp WaitLinkTransfer
; One "giant" leap for machinekind
-WaitLinkTransfer:: ; 82e (0:082e)
+WaitLinkTransfer::
ld a, $ff
ld [wOtherPlayerLinkAction], a
.loop
call LinkTransfer
call DelayFrame
- call CheckLinkTimeout
+ call CheckwLinkTimeoutFramesNonzero
jr z, .check
push hl
ld hl, wLinkTimeoutFrames + 1
@@ -282,6 +294,7 @@ WaitLinkTransfer:: ; 82e (0:082e)
dec hl
dec [hl]
jr nz, .skip
+ ; We might be disconnected
pop hl
xor a
jp SerialDisconnected
@@ -312,7 +325,7 @@ WaitLinkTransfer:: ; 82e (0:082e)
ld [wOtherPlayerLinkMode], a
ret
-LinkTransfer:: ; 872 (0:0872)
+LinkTransfer::
push bc
ld b, SERIAL_TIMECAPSULE
ld a, [wLinkMode]
@@ -326,23 +339,24 @@ LinkTransfer:: ; 872 (0:0872)
ld b, SERIAL_BATTLE
.got_high_nybble
- call LinkTransferReceive
+ call .Receive
ld a, [wPlayerLinkAction]
add b
ldh [hSerialSend], a
ldh a, [hLinkPlayerNumber]
- cp $2
- jr nz, .asm_89f
- ld a, $1
+ cp USING_INTERNAL_CLOCK
+ jr nz, .player_1
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
-.asm_89f
- call LinkTransferReceive
+
+.player_1
+ call .Receive
pop bc
ret
-LinkTransferReceive:: ; 8a4 (0:08a4)
+.Receive:
ldh a, [hSerialReceive]
ld [wOtherPlayerLinkMode], a
and $f0
@@ -355,28 +369,29 @@ LinkTransferReceive:: ; 8a4 (0:08a4)
ld [wOtherPlayerLinkAction], a
ret
-LinkDataReceived:: ; 8b9 (0:08b9)
+LinkDataReceived::
+; Let the other system know that the data has been received.
xor a
ldh [hSerialSend], a
ldh a, [hLinkPlayerNumber]
- cp $2
+ cp USING_INTERNAL_CLOCK
ret nz
- ld a, $1
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
- ld a, $81
+ ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
ldh [rSC], a
ret
-Function8ca:: ; 8ca
+Unreferenced_Function8ca::
ld a, [wLinkMode]
and a
ret nz
- ld a, $2
+ ld a, USING_INTERNAL_CLOCK
ldh [rSB], a
xor a
ldh [hSerialReceive], a
- ld a, $0
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
- ld a, $80
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
ldh [rSC], a
ret