Syscall 2: Firmware, home

DriverChapter 5: Half duplex

D9CHECKSUM

The task

Frame each host message with $C1, its length, and a sum check byte.

The station sends frames the receiver can check: noise that changes a byte should change the result of the check, so the receiver can throw the frame away.

For each host message, transmit $C1, then the number of bytes in the message, then the message, then one check byte: the sum of the message bytes, keeping only the low 8 bits. The check covers the message bytes only, not $C1 or the length.

An empty message is $C1 $00 $00. Messages are at most 12 bytes long, and this card has 16 cells of RAM.

The board

The line card of CHECKSUMThe line card: registers A B C D, 23 lines of ROM, 16 cells of RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSA B C DROM23 LINESRAM16 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

OUT transmits only the low byte of a register, so a sum that grows past 255 needs no trimming before it goes out.

Hints

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

Hint 1Show

As in Length Prefix, store the message and count it. Add each byte into a running sum in another register as it arrives.

Hint 2Show

Clear the sum in D at the start of each message. After the message, transmit $C1, the count, the stored bytes, and then D.

Hint 3Show

START: MOV B, 0, MOV D, 0. READ: HIN A, JEQ A, -1, SEND, MOV [B], A, ADD B, 1, ADD D, A, JMP READ. SEND: OUT $C1, OUT B, MOV C, 0. COPY: JEQ C, B, SUM, then the stored byte loop. SUM: OUT D.

Solution

Reveal the Full SolutionThe complete program for CHECKSUM. 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
        MOV D, 0
READ:   HIN A
        JEQ A, -1, SEND
        MOV [B], A
        ADD B, 1
        ADD D, A
        JMP READ
SEND:   OUT $C1
        OUT B
        MOV C, 0
COPY:   JEQ C, B, SUM
        MOV A, [C]
        OUT A
        ADD C, 1
        JMP COPY
SUM:    OUT D
TICKS
307
at par
LINES
17
at par