Syscall 2: Firmware, home

DriverChapter 5: Half duplex

D12STOP AND WAIT

The task

Send each order as a frame and wait for the ACK.

Orders go out in the house frame format, and the far end answers every frame it receives. The order terminal is the host. It hands the card one order at a time, at most 12 bytes long.

Send each order as one frame: $C1 $67, the length of the order, a flags byte of $00, the order itself, and the CRC-16 of every byte before it, high byte first.

After the frame, wait for the far end's answer, $06 (ACK), and pass it to the terminal with HOUT. Only then read the next order.

This card has a CRC engine, 16 cells of RAM, and a receive buffer that holds only 2 bytes.

The board

The line card of STOP AND WAITThe line card: registers A B C D, 38 lines of ROM, 16 cells of RAM, a 2 byte receive buffer, a stack 8 deep, and CRC. It talks to the line and to a host.LINEAN ACKERRXTXCARDREGISTERSA B C DROM38 LINESRAM16 CELLSRX BUFFER2 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

Read each ACK before you read the next order. The receive buffer holds only 2 bytes, so a card that never reads the ACKs overflows it on the third order.

Hints

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

Hint 1Show

The length goes out before the order, so read the whole order into RAM first, counting its bytes in b.

Hint 2Show

Start each frame with MOV CRC, $FFFF and feed every byte you transmit to the engine with CRC. Transmit the high byte of CRC, then the low byte.

Hint 3Show

After the two CRC bytes, IN A waits for the ACK and HOUT A passes it on. The card then wraps back to the top to read the next order.

Solution

Reveal the Full SolutionThe complete program for STOP AND WAIT. 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
READ:   HIN A
        JEQ A, -1, SEND
        MOV [B], A
        ADD B, 1
        JMP READ
SEND:   MOV CRC, $FFFF
        OUT $C1
        CRC $C1
        OUT $67
        CRC $67
        OUT B
        CRC B
        OUT 0
        CRC 0
        MOV C, 0
        JEQ B, 0, FIN
COPY:   MOV A, [C]
        OUT A
        CRC A
        ADD C, 1
        JNE C, B, COPY
FIN:    MOV A, CRC
        SHR A, 8
        OUT A
        OUT CRC
        IN A
        HOUT A
TICKS
397
at par
LINES
28
at par