Skip to content

Commit 7eb499f

Browse files
committed
Initial serial link demo code
1 parent 7c1edc1 commit 7eb499f

File tree

3 files changed

+756
-9
lines changed

3 files changed

+756
-9
lines changed

src/part2/serial-link.md

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,22 @@ To do this we'll use the serial interrupt:
279279

280280
**TODO:**
281281

282+
/// building serial link test program, separate to unbricked main.asm?
283+
284+
/// Because we have an extra file (sio.asm) to compile now, the build commands will look a little different:
285+
```console
286+
$ rgbasm -L -o sio.o sio.asm
287+
$ rgbasm -L -o main.o main.asm
288+
$ rgblink -o unbricked.gb main.o sio.o
289+
$ rgbfix -v -p 0xFF unbricked.gb
290+
```
291+
292+
/// tiles
293+
294+
/// defs
295+
296+
/// init/reset
297+
282298
/// initialise Sio
283299
Before doing anything else with Sio, `SioInit` needs to be called.
284300

@@ -294,15 +310,56 @@ Before doing anything else with Sio, `SioInit` needs to be called.
294310
call SioTick
295311
```
296312

297-
/// set clock source
298-
```rgbasm
299-
ld a, SCF_SOURCE
300-
ldh [rSC], a
313+
---
314+
315+
### Handshake
316+
317+
/// Establish contact by trading magic numbers
318+
319+
/// Define the codes each device will send:
320+
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/main.asm:handshake-codes}}
321+
{{#include ../../unbricked/serial-link/main.asm:handshake-codes}}
301322
```
302323

303-
/// do handshakey thing?
304-
/// whoever presses KEY attempts to do a transfer as the clock provider
305-
```rgbasm
324+
///
325+
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/main.asm:handshake-state}}
326+
{{#include ../../unbricked/serial-link/main.asm:handshake-state}}
306327
```
307328

308-
---
329+
/// Routines to begin handshake sequence as either the internally or externally clocked device.
330+
331+
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/main.asm:handshake-begin}}
332+
{{#include ../../unbricked/serial-link/main.asm:handshake-begin}}
333+
```
334+
335+
/// Every frame, handshake update
336+
337+
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/main.asm:handshake-update}}
338+
{{#include ../../unbricked/serial-link/main.asm:handshake-update}}
339+
```
340+
341+
/// If `wHandshakeState` is zero, handshake is complete
342+
343+
/// If the user has pressed START, abort the current handshake and start again as the clock provider.
344+
345+
/// Monitor Sio. If the serial port is not busy, start the handshake, using the DIV register as a pseudorandom value to decide if we should be the clock or not.
346+
347+
:::tip The DIV register
348+
349+
/// is not particularly random...
350+
351+
/// but we just need the value to be different when each device reads it, and for the value to occasionally be an odd number
352+
353+
:::
354+
355+
/// If a transfer is complete (`SIO_DONE`), jump to `HandshakeMsgRx` (described below) to check the received value.
356+
357+
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/serial-link/main.asm:handshake-xfer-complete}}
358+
{{#include ../../unbricked/serial-link/main.asm:handshake-xfer-complete}}
359+
```
360+
361+
/// First byte must be `MSG_SHAKE`
362+
363+
/// Second byte must be `wHandshakeExpect`
364+
365+
/// If the received message is correct, set `wHandshakeState` to zero

0 commit comments

Comments
 (0)