Syscall 2: Firmware, home

DriverChapter 8: Scarcity

D25ODOMETER

The task

Keep a 24-bit running total of the received bytes and transmit it after each one.

Each turnstile sends a byte every so often: how many people went through since the last one. The office keeps a running total, which soon outgrows 16 bits.

When the card starts, the host hands it the total so far as one message of three bytes, high byte first. After each received byte, add it to the total and transmit the total as three bytes, high byte first. The total never passes $FFFFFF.

The board

The line card of ODOMETERThe line card: registers A B C D, 20 lines of ROM, no RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSA B C DROM20 LINESRAMNONERX 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

A carry is just a byte that went past 255. Taking 256 off it and adding 1 to the next byte up is what long addition does by hand.

Hints

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

Hint 1Show

Keep each byte of the total in its own register, as a number from 0 to 255. Read the starting total into them first, then add each new byte to the lowest one.

Hint 2Show

If the low byte went past 255, subtract 256 and add 1 to the middle byte. If that went past 255, clear it and add 1 to the high byte.

Hint 3Show

After ADD D, A: JLT D, 256, SEND, SUB D, 256, ADD C, 1, and the same for C into B. SEND: OUT B, OUT C, OUT D, then jump back to the IN, not to the top, where HIN would wait forever.

Solution

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

        HIN B
        HIN C
        HIN D
        HIN A
TOP:    IN A
        ADD D, A
        JLT D, 256, SEND
        SUB D, 256
        ADD C, 1
        JLT C, 256, SEND
        MOV C, 0
        ADD B, 1
SEND:   OUT B
        OUT C
        OUT D
        JMP TOP
TICKS
103
at par
LINES
16
at par