; DISPATCH.ASM -- receive a three-byte station code and check its sum.
; Disk BASIC, MC6809 or HD6309. Run a personal copy from the assembly editor.
CHROUT      equ     $a002       ; indirect ROM character-output vector
            org     $3f00
start       leax    packet,pcr   ; X walks the incoming bytes
            leay    received,pcr ; Y walks a separate writable buffer
            ldb     #size
            bsr     receive
inspect     cmpa    expected,pcr ; stop here to inspect A and the copy
            bne     damaged
            leax    received,pcr
            bra     print
damaged     leax    badmsg,pcr
print       lda     ,x+
            beq     finish      ; both text paths end with a zero byte
            bsr     emit
            bra     print
finish      lda     #13         ; put the BASIC prompt on its own line
            bsr     emit
            rts

; Input: X source, Y destination, B positive byte count.
; Output: A = sum modulo 256. X and Y advance; B ends at zero.
receive     clra
next        adda    ,x          ; add this byte, leave X at it for the copy
            pshs    a           ; keep the running sum while moving the byte
            lda     ,x+
            sta     ,y+
            puls    a
            decb
            bne     next
            rts

emit        pshs    x           ; caller still needs its position after ROM I/O
            jsr     [CHROUT]
            puls    x,pc

packet      fcc     "ORB"
size        equ     *-packet
expected    fcb     $e3         ; $4F + $52 + $42 = $E3
received    fcb     0,0,0,0     ; fourth byte stays zero for printing
badmsg      fcc     "CHECK"
            fcb     0
            end     start
