summaryrefslogtreecommitdiff
path: root/home/serial.asm
diff options
context:
space:
mode:
Diffstat (limited to 'home/serial.asm')
-rw-r--r--home/serial.asm277
1 files changed, 146 insertions, 131 deletions
diff --git a/home/serial.asm b/home/serial.asm
index c57d77ee..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
@@ -8,64 +10,69 @@ Serial:: ; 6aa (0:06aa)
bit 0, a
jr nz, .printer
- ld a, [hLinkPlayerNumber]
- inc a
- jr z, .init_player_number
- ld a, [rSB]
- ld [hSerialReceive], a
- ld a, [hSerialSend]
- ld [rSB], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ldh a, [hLinkPlayerNumber]
+ 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 USING_INTERNAL_CLOCK
jr z, .player2
- ld a, 0 << rSC_ON
- ld [rSC], a
- ld a, 1 << rSC_ON
- ld [rSC], a
+
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
jr .player2
.printer
call PrinterReceive
jr .end
-.init_player_number
- ld a, [rSB]
- cp $1
+.establish_connection
+ ldh a, [rSB]
+ 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
+ ldh [hSerialReceive], a
+ ldh [hLinkPlayerNumber], a
+ cp USING_INTERNAL_CLOCK
jr z, ._player2
xor a
- ld [rSB], a
- ld a, $3
- ld [rDIV], a
+ ldh [rSB], a
+ ld a, 3
+ ldh [rDIV], a
.wait_bit_7
- ld a, [rDIV]
+ ldh a, [rDIV]
bit 7, a
jr nz, .wait_bit_7
- ; cycle the serial controller
- ld a, 0 << rSC_ON
- ld [rSC], a
- ld a, 1 << rSC_ON
- ld [rSC], a
+ ; Cycle the serial controller
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
jr .player2
._player2
xor a
- ld [rSB], a
+ ldh [rSB], a
+
.player2
- ld a, $1
- ld [hFFCC], a
+ ld a, TRUE
+ ldh [hFFCC], a
ld a, SERIAL_NO_DATA_BYTE
- ld [hSerialSend], a
+ ldh [hSerialSend], a
.end
pop hl
@@ -74,13 +81,13 @@ Serial:: ; 6aa (0:06aa)
pop af
reti
-Function710:: ; 710
- ld a, $1
- ld [hFFCE], a
+Serial_ExchangeBytes::
+ ld a, 1
+ ldh [hFFCE], a
.loop
ld a, [hl]
- ld [hSerialSend], a
- call Function73b
+ ldh [hSerialSend], a
+ call Serial_ExchangeByte
push bc
ld b, a
inc hl
@@ -88,7 +95,7 @@ Function710:: ; 710
.wait
dec a
jr nz, .wait
- ld a, [hFFCE]
+ ldh a, [hFFCE]
and a
ld a, b
pop bc
@@ -97,7 +104,7 @@ Function710:: ; 710
cp SERIAL_PREAMBLE_BYTE
jr nz, .loop
xor a
- ld [hFFCE], a
+ ldh [hFFCE], a
jr .loop
.load
@@ -109,27 +116,29 @@ Function710:: ; 710
jr nz, .loop
ret
-Function73b:: ; 73b (0:073b)
+Serial_ExchangeByte::
.loop
xor a
- ld [hFFCC], a
- ld a, [hLinkPlayerNumber]
- cp $2
- jr nz, .loop2
- ld a, (1 << rSC_CLOCK) | (0 << rSC_ON)
- ld [rSC], a
- ld a, (1 << rSC_CLOCK) | (1 << rSC_ON)
- ld [rSC], a
+ ldh [hFFCC], a
+ ldh a, [hLinkPlayerNumber]
+ cp 2
+ jr nz, .not_player_2
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
+ ldh [rSC], a
+ ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
+ ldh [rSC], a
+
+.not_player_2
.loop2
- ld a, [hFFCC]
+ ldh a, [hFFCC]
and a
jr nz, .reset_ffcc
- ld a, [hLinkPlayerNumber]
- cp $1
+ ldh a, [hLinkPlayerNumber]
+ 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
- ld a, [rIE]
- and $f
- cp $8
+ ldh a, [rIE]
+ and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
+ cp 1 << SERIAL
jr nz, .loop2
ld a, [wce5d]
dec a
@@ -156,33 +165,34 @@ Function73b:: ; 73b (0:073b)
dec a
ld [wce5d + 1], a
jr nz, .loop2
- ld a, [hLinkPlayerNumber]
- cp $1
+ ldh a, [hLinkPlayerNumber]
+ cp 1
jr z, .reset_ffcc
-
- ld a, $ff
+
+ ld a, 255
.delay_255_cycles
dec a
jr nz, .delay_255_cycles
.reset_ffcc
xor a
- ld [hFFCC], a
- ld a, [rIE]
- and $f
- sub $8
+ ldh [hFFCC], a
+ ldh a, [rIE]
+ 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
- ld a, [hSerialReceive]
+ 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,28 +204,28 @@ Function73b:: ; 73b (0:073b)
.no_rollover
pop hl
- call CheckLinkTimeout
+ call CheckwLinkTimeoutFramesNonzero
jr z, SerialDisconnected
-.link_timed_out
- ld a, [rIE]
- and $f
- cp $8
+.linkTimeoutFrames_zero
+ ldh 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 [hSerialSend], a
+ ldh [hSerialSend], a
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,29 +233,31 @@ 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 [hFFCE], a
+ ld c, 2
+ ld a, TRUE
+ ldh [hFFCE], a
.asm_7f8
call DelayFrame
ld a, [hl]
- ld [hSerialSend], a
- call Function73b
+ ldh [hSerialSend], a
+ call Serial_ExchangeByte
ld b, a
inc hl
- ld a, [hFFCE]
+ ldh a, [hFFCE]
and a
- ld a, $0
- ld [hFFCE], a
+ ld a, 0
+ ldh [hFFCE], a
jr nz, .asm_7f8
ld a, b
ld [de], a
@@ -254,26 +266,26 @@ Function7ec:: ; 7ec
jr nz, .asm_7f8
ret
-Function813:: ; 813
- call BackUpTilesToBuffer
+Serial_PrintWaitingTextAndSyncAndExchangeNybble::
+ call LoadTilemapToTempTilemap
callfar PlaceWaitingText
call WaitLinkTransfer
- jp ReloadTilesFromBuffer
+ jp SafeLoadTempTilemapToTilemap
-Function822:: ; 822
- call BackUpTilesToBuffer
+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,57 +339,59 @@ LinkTransfer:: ; 872 (0:0872)
ld b, SERIAL_BATTLE
.got_high_nybble
- call LinkTransferReceive
+ call .Receive
ld a, [wPlayerLinkAction]
add b
- ld [hSerialSend], a
- ld a, [hLinkPlayerNumber]
- cp $2
- jr nz, .asm_89f
- ld a, $1
- ld [rSC], a
- ld a, $81
- ld [rSC], a
-.asm_89f
- call LinkTransferReceive
+ ldh [hSerialSend], a
+ ldh a, [hLinkPlayerNumber]
+ cp USING_INTERNAL_CLOCK
+ jr nz, .player_1
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
+ ldh [rSC], a
+ ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
+ ldh [rSC], a
+
+.player_1
+ call .Receive
pop bc
ret
-LinkTransferReceive:: ; 8a4 (0:08a4)
- ld a, [hSerialReceive]
+.Receive:
+ ldh a, [hSerialReceive]
ld [wOtherPlayerLinkMode], a
and $f0
cp b
ret nz
xor a
- ld [hSerialReceive], a
+ ldh [hSerialReceive], a
ld a, [wOtherPlayerLinkMode]
and $f
ld [wOtherPlayerLinkAction], a
ret
-LinkDataReceived:: ; 8b9 (0:08b9)
+LinkDataReceived::
+; Let the other system know that the data has been received.
xor a
- ld [hSerialSend], a
- ld a, [hLinkPlayerNumber]
- cp $2
+ ldh [hSerialSend], a
+ ldh a, [hLinkPlayerNumber]
+ cp USING_INTERNAL_CLOCK
ret nz
- ld a, $1
- ld [rSC], a
- ld a, $81
- ld [rSC], a
+ ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
+ ldh [rSC], a
+ 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 [rSB], a
+ ld a, USING_INTERNAL_CLOCK
+ ldh [rSB], a
xor a
- ld [hSerialReceive], a
- ld a, $0
- ld [rSC], a
- ld a, $80
- ld [rSC], a
+ ldh [hSerialReceive], a
+ ld a, (0 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
+ ld a, (1 << rSC_ON) | (0 << rSC_CLOCK)
+ ldh [rSC], a
ret