Syscall 2: Firmware, home

DriverChapter 5: Half duplex

D11THE CRC ENGINE

The task

Wrap each host message in the house frame, with its CRC-16.

The house frame starts with two sync bytes, $C1 $67, so a stray $C1 in noise is not enough to start a frame. Then come a length byte, a flags byte, the payload, and a 16-bit CRC.

For each host message, transmit $C1, $67, the number of bytes in the message, the flags byte $01, the message, and then the CRC-16 of every byte from the first sync byte to the end of the message: high byte first, then low byte.

This card has the CRC engine. MOV CRC, $FFFF starts a new CRC, and CRC A folds the low byte of A into it. For example, the message HI! goes out as C1 67 03 01 48 49 21 07 9E. Messages are at most 16 bytes long, and this card has 24 cells of RAM.

The board

The line card of THE CRC ENGINEThe line card: registers A B C, 31 lines of ROM, 24 cells of RAM, a 16 byte receive buffer, a stack 8 deep, and CRC. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSA B CROM31 LINESRAM24 CELLSRX BUFFER16 BYTESSTACK8 DEEPUNITSCRCHOSTMESSAGES
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

Every byte you transmit before the CRC has to go through the engine too, sync bytes included. Miss one and the far end throws the frame away.

Hints

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

Hint 1Show

Store the message and count it, as in Length Prefix. Then transmit each byte of the frame and feed the same byte to the engine with CRC.

Hint 2Show

Start each frame with MOV CRC, $FFFF. When the payload is out, transmit the high byte of CRC first: MOV A, CRC, SHR A, 8, OUT A. Then OUT CRC sends the low byte.

Hint 3Show

A short way: store the message from cell 4 on, then put $C1, $67, the length, and $01 in cells 0 to 3. One loop then transmits and feeds every byte from cell 0 to the end, followed by the two CRC bytes.

Solution

Reveal the Full SolutionThe complete program for THE CRC ENGINE. 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, 4
READ:   HIN A
        JEQ A, -1, SEND
        MOV [B], A
        ADD B, 1
        JMP READ
SEND:   MOV [0], $C1
        MOV [1], $67
        MOV A, B
        SUB A, 4
        MOV [2], A
        MOV [3], 1
        MOV CRC, $FFFF
        MOV C, 0
COPY:   MOV A, [C]
        OUT A
        CRC A
        ADD C, 1
        JNE C, B, COPY
        MOV A, CRC
        SHR A, 8
        OUT A
        OUT CRC
TICKS
366
at par
LINES
23
at par