Syscall 2: Firmware, home

DriverChapter 9: Special nodes

D32UNSTUFF

The task

Deliver each SLIP frame's length, then its bytes with the escapes undone.

Packets come back from the harbor radio as SLIP frames: each ends with $C0, a $C0 inside is sent as $DB $DC, and a $DB inside as $DB $DD. After $DB the line only ever sends $DC or $DD.

For each frame, deliver to the host the number of bytes it holds once the escapes are undone, then those bytes. Skip a frame with no bytes: in $C0 $C0, the second $C0 delivers nothing.

A frame holds at most 16 bytes once the escapes are undone, and this card has 20 cells of RAM.

The board

The line card of UNSTUFFThe line card: registers A B C, 24 lines of ROM, 20 cells of RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSA B CROM24 LINESRAM20 CELLSRX BUFFER16 BYTESSTACK8 DEEPUNITSNONEHOSTMESSAGES
The line card this ticket gives you, with its registers, memory, buffers, and units. On a narrow screen, scroll the board sideways to see all of it.

Key idea

Undoing the escapes shortens the frame, so count the bytes you store, not the bytes you receive.

Hints

Each hint goes a step further than the one before. Open them one at a time.

Hint 1Show

Store the bytes in RAM and count them. When $C0 arrives the count is the length, which has to reach the host before the bytes.

Hint 2Show

On $DB, read the next byte at once: $DC stands for $C0 and $DD for $DB. Store what it stands for, never the escape itself.

Hint 3Show

On $C0: JEQ B, 0, NEXT, HOUT B, then deliver cells 0 to B and start again with B at 0. On $DB: IN A, then MOV A, $C0 if A is $DC, MOV A, $DB if not, then store A.

Solution

Reveal the Full SolutionThe complete program for UNSTUFF. Open it only when you want the answer.

The reference program

The game's own solution to this ticket. It passes every test and scores exactly par on every benchmark.

START:  MOV B, 0
NEXT:   IN A
        JEQ A, $C0, FRAME
        JNE A, $DB, STORE
        IN A
        JEQ A, $DC, WASC0
        MOV A, $DB
        JMP STORE
WASC0:  MOV A, $C0
STORE:  MOV [B], A
        ADD B, 1
        JMP NEXT
FRAME:  JEQ B, 0, NEXT
        HOUT B
        MOV C, 0
EMIT:   MOV A, [C]
        HOUT A
        ADD C, 1
        JNE C, B, EMIT
TICKS
430
at par
LINES
19
at par