Syscall 2: Firmware, home

DriverChapter 12: Shared buses

D48HEX RECORDS

The task

Transmit each host message as a hex record with its address and check byte.

The chip programmer loads firmware as text records: a colon, then hex digit pairs for the data length, a 16-bit address high byte first, the record type $00, the data, and a check byte, then $0D $0A.

The check byte is 0 minus the sum of every byte of the record before it, length through data, keeping the low 8 bits. The data 01 02 03 at address 0000 goes out as :03000000010203F7 and $0D $0A.

Each host message is one record's data, 1 to 16 bytes. The first record goes to address $0000 and each later one straight after the one before. Hex digits are '0' to '9' and 'A' to 'F', and this card has 20 cells of RAM.

The board

The line card of HEX RECORDSThe line card: registers A B C D, 48 lines of ROM, 20 cells of RAM, a 16 byte receive buffer, a stack 4 deep. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSA B C DROM48 LINESRAM20 CELLSRX BUFFER16 BYTESSTACK4 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

The type byte is part of the sum too, but it is 0, so it adds nothing.

Hints

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

Hint 1Show

Read the data into RAM first: its length goes out before it. Keep the address in C for the whole run and the running sum in D.

Hint 2Show

Write a routine that transmits the low byte of A as two hex digits, as in Hex Dump, and call it for every byte of the record. Add each byte to D as it goes; the check is then 0 minus D.

Hint 3Show

CH and CL are the address bytes. The next record starts at this one's address plus its length: ADD C, B once the address has gone out. JNE B, [18], DATA compares B with a RAM cell.

Solution

Reveal the Full SolutionThe complete program for HEX RECORDS. 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 [18], B
        OUT ':'
        MOV D, B
        MOV A, B
        CALL HEX
        MOV A, CH
        ADD D, A
        CALL HEX
        MOV A, CL
        ADD D, A
        CALL HEX
        MOV A, 0
        CALL HEX
        ADD C, B
        MOV B, 0
DATA:   MOV A, [B]
        ADD D, A
        CALL HEX
        ADD B, 1
        JNE B, [18], DATA
        MOV A, 0
        SUB A, D
        CALL HEX
        OUT $0D
        OUT $0A
        JMP START
HEX:    MOV [19], A
        SHR A, 4
        AND A, $0F
        CALL DIGIT
        MOV A, [19]
        AND A, $0F
DIGIT:  JLT A, 10, NUM
        ADD A, 7
NUM:    ADD A, '0'
        OUT A
        RET
TICKS
1259
at par
LINES
43
at par