
* SEND PROM UTILITY FOR SK*DOS / 6809

* THIS UTILITY CONVERTS A BINARY S1-S9 DISK FILE
* TO INTEL FORMAT FOR SENDING TO THE HEATH PROGRAMMER.

* THIS VERSION IS DESIGNED FOR A 27128 TO BE USED IN A
* 80008 SYSTEM; I.E.,
*   A. ALL CONSECUTIVE BYTES GO INTO ONE PROM, RATHER THAN
*      BEING SPLIT INTO ALTERNATE PROMS, AS WOULD BE
*      REQUIRED WITH A 68000, 68010, ETC.
*   B. THE 27128 IS ASSUMED TO BEGIN AT $FFFFC000, BUT
*      THE EQUIVALENT PROM ADDRESSES BEGIN AT $0000. I.E.,
*      THE $FFFF IS DROPPED AND C CHANGED TO 0 ETC.

* COPYRIGHT (C) 1986 BY PETER A. STARK

* EQUATES

ACIA    EQU    $E010     ADDRESS OF PROM PORT ACIA
SYSFCB  EQU    $C840     SYSTEM FCB
WARMST  EQU    $CD03     WARM START
PUTCH   EQU    $CD18     OUTPUT CHARACTER
PDATA   EQU    $CD54
PCRLF   EQU    $CD24     PRINT RETURN / LINE FEED
GETNAM  EQU    $CD2D     GET FILE SPECIFICATION
DEFEXT  EQU    $CD33     DEFAULT EXTENSION
PERROR  EQU    $CD3F     REPORT ERROR NUMBER
FCSCLS  EQU    $D403     CLOSE FILES
FCS     EQU    $D406     CALL FCS

        ORG    $C100

SEND    BRA    START     GO TO START

VER     FCB    1         VERSION NUMBER

* START OF ACTUAL PROGRAM
START   JSR    PCRLF     START ON NEW LINE
        LDX    #SYSFCB   POINT TO SYSTEM FCB
        JSR    GETNAM    GET FILE SPEC INTO FCB
        LBCS   ERR21     IF FILE NAME IS NG

* FILE SPEC WAS OK; DEFAULT TO .BIN
        LDA    #0        DEFAULT EXTENSION CODE
        JSR    DEFEXT    DEFAULT TO .BIN

* NOW ACTUALLY OPEN THE FILE
        LDA    #1        OP CODE FOR OPEN FOR READ
        STA    0,X       SAVE OPERATION CODE
        JSR    FCS       AND GO OPEN THE FILE
        LBNE   ERROR     IF NOT ZERO (Z = 0)

* MAIN LOOP TO READ CHARS AND PUT THEM INTO LINE BUFFER
MLOOP   LDX    #$0400    TIME DELAY
WAIT    LEAX   -1,X
        BNE    WAIT      WAIT A BIT
        LDY    #LINE     POINT TO LINE
        LDX    #SYSFCB   POINT TO SYSTEM FCB

LLOOP   JSR    FCS       GO READ NEXT CHARACTER
        BNE    CHEROR    IF THERE WAS AN ERROR
        STA    0,Y+      STORE INTO LINE BUFFER
        CMPA   #$0D      IS IT CR?
        BNE    LLOOP     NO, REPEAT FOR MORE
        BRA    LINEOK    FINISHED READING A LINE

* IF THERE WAS AN ERROR, SEE IF END OF FILE
CHEROR  LDA    1,X       GET ERROR CODE
        CMPA   #8        COMPARE WITH END OF FILE (IE) ERROR
        LBNE   ERROR     NOT END OF FILE, SO REAL ERROR
        JMP    CLOSE     ON E-O-F, JUST CLOSE FILE AND QUIT

* LINE IS NOW IN BUFFER; PROCESS IT
LINEOK  LDD    LINE      GET FIRST
        CMPD   #$5339    IS IT S9?
        LBEQ   CLOSE     YES, CLOSE FILE AND QUIT
        CMPD   #$5338    IS IT S8?
        LBEQ   CLOSE     YES, CLOSE FILE AND QUIT
        CMPD   #$5337    IS IT S7?
        LBEQ   CLOSE     YES, CLOSE FILE AND QUIT
        CMPD   #$5331    IS IT S1?
        BEQ    S1        YES
        CMPD   #$5332    IS IT S2?
        BEQ    S2        YES
        CMPD   #$5333    IS IT S3?
        BEQ    S3        YES
        BRA    MLOOP     ELSE LOOK FOR NEXT LINE

S1      LDD    LINE+2    PICK UP COUNT
        JSR    ASTOBI    CONVERT ASCII TO BINARY
        SUBA   #3        SUBT 2-BYTE ADDRESS AND CHECKSUM
        LDU    #LINE+4   POINT TO BEG OF 4-BYTE ADDRESS
        BRA    DOREST    THEN GO DO THE REST

S2      LDD    LINE+2    PICK UP COUNT
        JSR    ASTOBI    CONVERT ASCII TO BINARY
        SUBA   #4        SUBT 3-BYTE ADDRESS AND CHECKSUM
        LDU    #LINE+6   POINT TO BEG OF 4-BYTE ADDRESS
        BRA    DOREST    THEN GO DO THE REST

S3      LDD    LINE+2    PICK UP COUNT
        JSR    ASTOBI    CONVERT ASCII TO BINARY
        SUBA   #5        SUBT 4-BYTE ADDRESS AND CHECKSUM
        LDU    #LINE+8   POINT TO BEG OF 4-BYTE ADDRESS

DOREST  STA    COUNT     STORE BYTE COUNT
        LDA    #$0D
        JSR    OUT1      OUTPUT CR TO PROM
        LDA    #$0A
        JSR    OUT1      OUTPUT LF
        LDA    #':
        JSR    OUT1      OUTPUT :
        CLR    CHKSUM    START WITH CHECKSUM = 0
        LDA    COUNT
        JSR    BITOAS    CONVERT COUNT TO ASCII
        JSR    OUT2      OUTPUT THE COUNT
        LDD    0,U++     GET LEFT BYTE OF 2-BYTE ADDRESS
        SUBA   #$13      CHANGE C TO 0 ETC.
        JSR    OUT2      OUTPUT THEM
        LDD    0,U++     GET RIGHT BYTE OF ADDRESS
        JSR    OUT2      OUTPUT THEM
        LDD    #$3030
        JSR    OUT2      OUTPUT '00' TYPE CODE
        LDB    COUNT
        CLR    A         CONSTRUCT COUNTER IN Y
        TFR    D,Y
BLOOP   LDD    0,U++     GET NEXT BYTE OF DATA
        JSR    OUT2      OUTPUT
        LEAY   -1,Y      DECREMENT COUNTER
        BNE    BLOOP     REPEAT UNTIL ALL DATA OUT
        LDA    CHKSUM
        NEGA             CONSTRUCT CHECKSUM
        JSR    BITOAS    CONVERT TO ASCII
        JSR    OUT2      OUTPUT IT
        JMP    MLOOP     AND REPEAT MAIN LOOP

* SUBROUTINE TO OUTPUT TWO CHARACTERS IN D TO PROM PORT
* AND ADD THEM TO THE CHECKSUM

OUT2    PSHS   D         SAVE THE TWO BYTES
        JSR    ASTOBI    CONVERT TO BINARY
        ADDA   CHKSUM
        STA    CHKSUM    UPDATE CHECKSUM
        PULS   A
        BSR    OUT1      OUTPUT FIRST
        PULS   A
        BSR    OUT1      OUTPUT SECOND
        RTS

* SUBROUTINE TO OUTPUT ONE CHARACTER IN A TO PROM PORT

OUT1    LDB    ACIA      CHECK TDRE FLAG OF ACIA
        LSRB
        LSRB
        BCC    OUT1      WAIT IF NOT READY TO OUTPUT
        STA    ACIA+1    THEN OUTPUT THE BYTE
        RTS

* SUBROUTINE TO CONVERT A PAIR OF ASCII CHARACTERS IN D
* INTO A BINARY NUMBER IN A

ASTOBI  BSR    CVTHEX    CONVERT FIRST TO HEX
        LSLA
        LSLA
        LSLA             MOVE INTO LEFT DIGIT
        LSLA
        EXG    A,B       SWAP WITH SECOND
        BSR    CVTHEX    CONVERT SECOND
        PSHS   B
        ADDA   0,S+      COMBINE THEM
        RTS

* SUBROUTINE TO CONVERT ONE ASCII HEX DIGIT IN A INTO BINARY

CVTHEX  SUBA   #'0       CVT FROM ASCII
        CMPA   #9
        BLS    CEXIT     DONE IF <=0
        SUBA   #7        ELSE SUBTRACT AN EXTRA 7
CEXIT   RTS              ABD RETURN

* SUBROUTINE TO CONVERT A BINARY NUMBER IN A INTO
* TWO ASCII CHARACTERS IN D FOR HEX OUTPUT

BITOAS  TFR    A,B       MOVE NUMBER INTO B
        ANDB   #$0F      RIGHT DIGIT IN B
        LSRA
        LSRA
        LSRA             LEFT DIGIT IN A
        LSRA
        ADDD   #$3030    CHANGE TO ASCII
        CMPA   #'9       CHECK FIRST DIGIT
        BLS    FRSTOK    FIRST OK AS IS
        ADDA   #7        BUT CHANGE 10 TO A
FRSTOK  CMPB   #'9       CHECK SECOND
        BLS    SECOK     OK AS IS
        ADDB   #7        BUT CHANGE 10 TO A
SECOK   RTS

ERR21   LDB    #21       ELSE IT'S ERROR 21 - FILE NAME
        STB    1,X       STORE ERROR CODE IN FCB

* ERROR ROUTINE

ERROR   JSR    PERROR    PRINT ERROR CODE
CLOSE   JSR    FCSCLS    CLOSE THE FILE
        LDX    #EOFSTR
SLOOP   LDA    0,X+      GET NEXT CHARACTER
        CMPA   #4        EOS?
        BEQ    SEXIT     YES
        JSR    OUT1      NO, OUTPUT IT
        BRA    SLOOP
SEXIT   JMP    WARMST    AND RETURN TO SK*DOS

* TEXT

EOFSTR  FCB    $0D,$0A
        FCC    ':00000001FF'
        FCB    $0D,$0A,4

* DATA

COUNT   RMB    1         BYTE COUNT TO BE OUTPUT
CHKSUM  RMB    0         CHECKSUM
LINE    RMB    256       LINE BUFFER

        END    SEND
