Syscall 2: Firmware, home

DriverChapter 9: Special nodes

D31BYTE STUFFING

The task

Transmit each host message as a SLIP frame: escape $C0 and $DB, end with $C0.

The harbor radio marks the end of every packet with the byte $C0. When $C0 turns up inside a packet it has to be disguised, or the far end would cut the packet in two.

For each host message, transmit its bytes with two replacements, then $C0. A $C0 becomes $DB $DC, and a $DB becomes $DB $DD. Every other byte goes out as it is, and an empty message is the single byte $C0.

The board

The line card of BYTE STUFFINGThe line card: registers A, 16 lines of ROM, no RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSAROM16 LINESRAMNONERX 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

SLIP is this framing's name. Only two byte values ever change, so most packets go out untouched.

Hints

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

Hint 1Show

Compare each byte with $C0 and with $DB before you transmit it. Most bytes go straight out.

Hint 2Show

The escape byte $DB needs escaping too. Otherwise the far end could not tell a real $DB from the start of an escape.

Hint 3Show

NEXT: HIN A, JEQ A, -1, END, JEQ A, $C0, ESCC0, JEQ A, $DB, ESCDB, OUT A, JMP NEXT. ESCC0: OUT $DB, OUT $DC, JMP NEXT. ESCDB: OUT $DB, OUT $DD, JMP NEXT. END: OUT $C0.

Solution

Reveal the Full SolutionThe complete program for BYTE STUFFING. 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.

NEXT:   HIN A
        JEQ A, -1, END
        JEQ A, $C0, ESCC0
        JEQ A, $DB, ESCDB
        OUT A
        JMP NEXT
ESCC0:  OUT $DB
        OUT $DC
        JMP NEXT
ESCDB:  OUT $DB
        OUT $DD
        JMP NEXT
END:    OUT $C0
TICKS
160
at par
LINES
13
at par