Syscall 2: Firmware, home

DriverChapter 11: Capstones

D42FULL DUPLEX

The task

Transmit host bytes and deliver received bytes, both at once.

The order terminal and the depot talk at the same time. Bytes from the host go out on the line, bytes from the line go to the host, and neither side waits for the other.

Transmit every host byte; the end of a host message sends nothing. Deliver every received byte to the host. The receive buffer holds only 4 bytes, so the line cannot wait while the card waits for the host.

RXN reads how many received bytes are waiting. HIN A, LOOP works with the timer just as IN does: it jumps to LOOP if no host byte comes before the timer runs out.

The board

The line card of FULL DUPLEXThe line card: registers A, 14 lines of ROM, no RAM, a 4 byte receive buffer, a stack 8 deep, and TIMER. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSAROM14 LINESRAMNONERX BUFFER4 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

Serve the line first: its buffer is small, and the host keeps its bytes until they are read.

Hints

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

Hint 1Show

Never wait on one side while the other has something. JEQ RXN, 0, HOST checks the line without waiting for it.

Hint 2Show

HIN takes two ticks even when a byte is ready, so a timer of 1 or 2 always runs out first. TMR 3, then HIN A, LOOP gives the host a short chance.

Hint 3Show

LOOP: JEQ RXN, 0, HOST, IN A, HOUT A, JMP LOOP. HOST: TMR 3, HIN A, LOOP, JEQ A, -1, LOOP, OUT A, JMP LOOP.

Solution

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

LOOP:   JEQ RXN, 0, HOST
        IN A
        HOUT A
        JMP LOOP
HOST:   TMR 3
        HIN A, LOOP
        JEQ A, -1, LOOP
        OUT A
        JMP LOOP
TICKS
361
at par
LINES
9
at par

Another way: Host first

A second program that also passes. It beats par on LINES and gives some back on TICKS.

LOOP:   TMR 3
        HIN A, LINE
        JEQ A, -1, LINE
        OUT A
LINE:   JEQ RXN, 0, LOOP
        IN A
        HOUT A
        JMP LOOP
TICKS
368
over par
LINES
8
under par