Skip to content

Commit b3f572d

Browse files
committed
Rewrite explanation of SioTick in sync with the code.
- move SioAbort to a separate anchor section
1 parent 8974060 commit b3f572d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/part2/serial-link.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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}}

unbricked/serial-link/sio.asm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ SioTick::
176176
ld a, SIO_IDLE
177177
ld [wSioState], a
178178
ret
179+
; ANCHOR_END: sio-tick
179180

180181

182+
; ANCHOR: sio-abort
181183
; Abort the ongoing transfer (if any) and enter the FAILED state.
182184
; @mut: AF
183185
SioAbort::
@@ -187,7 +189,7 @@ SioAbort::
187189
res SCB_START, a
188190
ldh [rSC], a
189191
ret
190-
; ANCHOR_END: sio-tick
192+
; ANCHOR_END: sio-abort
191193

192194

193195
; ANCHOR: sio-start-transfer

0 commit comments

Comments
 (0)