@@ -184,19 +184,35 @@ We'd better define the constants that set the catchup delay and timeout duration
184184```
185185
186186<!-- Tick -->
187- Implement ` SioTick ` to update the timeout and ` SioAbort ` to cancel the ongoing transfer :
187+ Implement the timeout logic in ` SioTick ` :
188188
189189``` rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/sio.asm:sio-tick}}
190190{{#include ../../unbricked/serial-link/sio.asm:sio-tick}}
191191```
192192
193- Check that a transfer has been started, and that the clock source is set to * external* .
194- Before * ticking* the timer, check that the timer hasn't already expired with ` and a, a ` .
195- Do nothing if the timer value is already zero.
196- Decrement the timer and save the new value before jumping to ` SioAbort ` if new value is zero.
193+ ` SioTick ` checks the current state (` wSioState ` ) and jumps to a state-specific subroutine (labelled ` *_tick ` ).
194+
195+ ** ` SIO_ACTIVE ` :** a transfer has been started, if the clock source is * external* , update the timeout timer.
196+
197+ The timer's state is an unsigned integer stored in ` wSioTimer ` .
198+ Check that the timer is active (has a non-zero value) with ` and a, a ` .
199+ Decrement the timer and write the new value back to memory.
200+ If the timer expired (the new value is zero) the transfer should be aborted.
201+ The ` dec ` instruction sets the zero flag in that case, so all we have to do is ` jr z, SioAbort ` .
202+
203+ ** ` SIO_RESET ` :** ` SioReset ` has been called, change state to ` SIO_IDLE ` .
204+ This causes a one tick delay after ` SioReset ` is called.
205+
206+ <!-- Abort -->
207+ ``` rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/sio.asm:sio-abort}}
208+ {{#include ../../unbricked/serial-link/sio.asm:sio-abort}}
209+ ```
210+
211+ ` SioAbort ` brings the serial port down and sets the current state to ` SIO_FAILED ` .
212+ The aborted transfer state is intentionally left intact (or as intact as it was, at least) so it can be used to inform error handling and debugging.
197213
198214<!-- PortEnd -->
199- The last part of the core implementation handles the end of a transfer:
215+ The last part of the core implementation handles the end of each byte transfer:
200216
201217``` rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/sio.asm:sio-port-end}}
202218{{#include ../../unbricked/serial-link/sio.asm:sio-port-end}}
0 commit comments