Syscall 2: Firmware, home

DriverChapter 12: Shared buses

D49GATEWAY

The task

Wrap every SLIP packet from the radio in a house frame for the trunk line.

The harbor radio and the trunk line use different framing, and this card sits between them. Packets arrive from the radio as SLIP frames: each ends with $C0, a $C0 inside is sent as $DB $DC, and a $DB as $DB $DD.

Transmit each packet on the trunk as a house frame: $C1 $67, the packet's length, the flags byte $00, the packet, and the CRC-16 of every byte before it, high byte first. Skip empty packets.

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

The board

The line card of GATEWAYThe line card: registers A B C, 40 lines of ROM, 24 cells of RAM, a 16 byte receive buffer, a stack 8 deep, and CRC. It talks to the line.LINETHE FAR ENDRXTXCARDREGISTERSA B CROM40 LINESRAM24 CELLSRX BUFFER16 BYTESSTACK8 DEEPUNITSCRC
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

Each framing finds the end of a packet its own way: SLIP with a marker, the house frame with a length.

Hints

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

Hint 1Show

This is Unstuff on the way in and Stop and Wait on the way out. Undo the escapes into RAM and count the bytes in B.

Hint 2Show

On $C0 with B above 0, the packet is complete and its length is known. Start the CRC, then transmit and feed the header, the stored bytes, and the CRC.

Hint 3Show

The bytes that need escaping on the radio go out on the trunk as they are: the house frame finds its end by the length, not by a marker.

Solution

Reveal the Full SolutionThe complete program for GATEWAY. 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
        MOV CRC, $FFFF
        OUT $C1
        CRC $C1
        OUT $67
        CRC $67
        OUT B
        CRC B
        OUT 0
        CRC 0
        MOV C, 0
BODY:   MOV A, [C]
        OUT A
        CRC A
        ADD C, 1
        JNE C, B, BODY
        MOV A, CRC
        SHR A, 8
        OUT A
        OUT CRC
TICKS
786
at par
LINES
32
at par