Syscall 2: Firmware, home

DriverChapter 12: Shared buses

D47GUARD TIME

The task

Pass received bytes to the host until +++ arrives alone between quiet spells; then answer OK.

In data mode a modem passes everything through, so it needs a safe signal to start listening for commands: three + characters with quiet time before and after, so a +++ inside the data does nothing.

Deliver every received byte to the host, except an escape: at least 30 quiet ticks, then exactly +++, then 30 more quiet ticks. For an escape, deliver none of the three + characters, and transmit OK $0D $0A once the quiet after it has passed.

Quiet means no byte arrives. Bytes either come within 8 ticks of each other or after at least 60 quiet ticks, and the line is quiet before the first byte. Anything that is not an escape is data, + characters included.

The board

The line card of GUARD TIMEThe line card: registers A B, 30 lines of ROM, no RAM, a 16 byte receive buffer, a stack 8 deep, and TIMER. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSA BROM30 LINESRAMNONERX BUFFER16 BYTESSTACK8 DEEPUNITSTIMERHOSTMESSAGES
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

Hold the + characters back until you know what they are. Once delivered, a byte cannot be taken back from the host.

Hints

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

Hint 1Show

Pass bytes through with TMR 30 and IN A, QUIET before each one. After quiet, the next byte decides: a + may start an escape, anything else is plain data.

Hint 2Show

Count the + characters in B as they come, with the timer running for each. A byte that is not +, or any byte after the third, means it was data: deliver the + characters you held back, then that byte.

Hint 3Show

If the timer runs out with B at 3, it was an escape: transmit OK $0D $0A. With fewer, deliver the held + characters. Either way the line is quiet now, so the next byte starts the check again.

Solution

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

QUIET:  IN A
        JNE A, '+', PASS
        MOV B, 1
PLUS:   TMR 30
        IN A, GAP
        JEQ B, 3, SPILL
        JNE A, '+', SPILL
        ADD B, 1
        JMP PLUS
GAP:    JNE B, 3, FLUSH
        OUT 'O'
        OUT 'K'
        OUT $0D
        OUT $0A
        JMP QUIET
FLUSH:  HOUT '+'
        DJN B, FLUSH
        JMP QUIET
SPILL:  HOUT '+'
        DJN B, SPILL
PASS:   HOUT A
DATA:   TMR 30
        IN A, QUIET
        HOUT A
        JMP DATA
TICKS
373
at par
LINES
25
at par