        NAM     RCV$
        OPT     O,NOP
        SPC 1
*
* RECEIVE FILES TO MINI-DOS+ DISK FROM CONSOLE PORT
* AND HANDSHAKE WITH XON/XOFF.
*
* ROUTINE IS INTERRUPT DRIVEN AND REQUIRES
* IRQ VECTOR SET-UP.
*
        SPC     1
*
* This is version 2.0 which first, receives the buffer, then
* sends XOFF, writes the buffer to disk until reading $FF in 
* the input buffer, then sends XON and repeats until finding
* EOF (CNTRL-Z) in the input buffer.
*
* RCV should be invoked from MINIDOS with a filename specified
* in the command line.
*
        SPC     1
* BUFLEN is the length of the character buffer
BUFLEN  EQU     256
        SPC     1
* MINIDOS EQUATES
LINPTR  EQU     $A0A0
LINBUF  EQU     $A080
DIRTEM  EQU     $A0A6
TYPERR  EQU     $C01E
SEARCH  EQU     $C4BD
SEANXT  EQU     $C50F
TRKSEC  EQU     $01
FWDLNK  EQU     $5
BAKLNK  EQU     $3
FWDCAL  EQU     $C021
TRGTAD  EQU     $8
FILTYP  EQU     $A
BYTCNT  EQU     $7
TA      EQU     $14
TW      EQU     $16
WRTSEC  EQU     $C00F
TRK     EQU     $01
SCTR    EQU     $02
WTDIR   EQU     $C5F5
NXTOKN  EQU     $C792
MNDOS   EQU     $C000
        SPC     1
* OTHER EQUATES
PORT    EQU     $8004   ACIA IN SLOT 0
IRQVEC  EQU     $A000   IRQ VECTOR
XON     EQU     $11     START HOST
XOFF    EQU     $13     STOP HOST
CNTRLZ  EQU     $1A     END OF FILE
LF      EQU     $0A     LINEFEED
        SPC     1
        ORG     $100
        SPC     1
INIT    LDX     #LINBUF POINT TO DOS LINE BUFFER
        STX     LINPTR
        JSR     OPNENT  OPEN DISK FILE
        BCC     OPNGD
ERROR   JSR     TYPERR  PRINT ANY ERRORS
EXIT    JMP     MNDOS
OPNGD   LDX     DIRTEM  POINT TO FILE DATA IN DIR
        LDX     6,X
        STX     DSKPTR  GET BEGINNING TRK-SEC
        LDX     #BUFFER
        STX     BUFPTR
        CLR     BUFCNT
        CLR     EOFFLG
        CLR     BOFFLG
        INC     BOFFLG
        SPC     1
        LDX     #CHRBUF
        STX     BUFPTI  SET INTERRUPT ROUTINES POINTER
        LDA A   #$FF    SET THE LAST CHAR FLAG
        STA A   0,X
        SPC     1
        LDA A   #$13    RESET AND CONFIGURE ACIA
        STA A   PORT
        LDA A   #$95    IRQ, /16 BAUD, 8 BITS+1 STOP BIT
        STA A   PORT
        SPC     1
        LDX     #IRQRTN SET UP IRQ VECTOR
        STX     IRQVEC
        CLI             CLEAR INTERRUPT MASK
        SPC     1
START   CLR     BFLAG
        LDX     #CHRBUF
        STX     BUFPTI
        LDA A   #XON    TELL HOST TO SEND
        JSR     OUTCH
        SPC     1
WAIT    TST     BFLAG   CHECK FOR BUFFER FULL
        BEQ     WAIT
        LDA A   #XOFF   TELL HOST TO STOP SENDING
        JSR     OUTCH
        LDX     #CHRBUF
NXTCHR  LDA A   0,X     FETCH CHAR
        INX             BUMP X
        TST A           CHECK FOR NULLS
        BEQ     NXTCHR
        CMP A   #LF     OR LINEFEEDS
        BEQ     NXTCHR
        CMP A   #$FF    END OF BUFFER FLAG
        BEQ     START
        CMP A   #CNTRLZ END OF FILE CHAR?
        BNE     WRTCHR  NOPE, SO WRITE IT
        LDA A   #4      CHANGE TO EOF CODE
WRTCHR  JSR     PUTCHR  PUT THE CHAR TO DISK
        TST     EOFFLG  END OF FILE SET?
        BEQ     NXTCHR  NOPE, SO DO NEXT CHARACTER
        SPC     2
*
* CLSENT closes the disk file and returns to MINIDOS
*
CLSENT  LDX     DIRTEM
        LDA A   TRK
        STA A   8,X
        STA A   FWDLNK
        LDA A   SCTR
        STA A   9,X
        STA A   FWDLNK+1
        CLR     10,X    CLEAR BEGADD
        CLR     11,X
        CLR     12,X
        CLR     13,X
        LDA A   #$FF
        STA A   14,X
        STA A   15,X
        JSR     FWDCAL  CALCULATE NEXT AVAILABLE
        LDA A   FWDLNK
        STA A   22,X
        LDA A   FWDLNK+1
        STA A   23,X
        JSR     WTDIR
        JMP     EXIT
        SPC     2
*
* OPNENT opens the filename specified in the command line
* and returns error codes if the file already exists or
* cannot be opened for some other reason.
*
OPNENT  JSR     NXTOKN  POINT TO FILENAME
        BEQ     OPNEN5  MISSING FILENAME
        STX     LINPTR
        JSR     SEARCH  SEARCH TO SEE IF ITS THERE
        BCC     OPNEN6  YES, SO ERROR
        CMP A   #6
        BNE     OPNEN7  ONLY ERROR 6 ALLOWED
        JSR     SEANXT  GET NEXT AVAILABLE SPACE
        BCS     OPNEN7  ERROR
        STX     DIRTEM  X POINTS TO SPACE
        STX     XTEMP
        LDA B   #6
OPNEN3  LDX     LINPTR
        LDA A   0,X
        BEQ     OPNEN4
        CMP A   #'      SPACE IS ALSO DELIMITER
        BEQ     OPNEN4
        INX
        STX     LINPTR
        LDX     XTEMP
        STA A   0,X
        INX
        STX     XTEMP
        DEC B
        BNE     OPNEN3  MORE CHARS
OPNEN4  CLC
        RTS             GOOD EXIT, NO ERRORS
OPNEN5  LDA A   #8      WHAT? ERROR
        SEC
        RTS
OPNEN6  LDA A   #9      ALLOCATION ERROR
OPNEN7  SEC
        RTS
        SPC     2
*
* PUTCHR writes one character to disk
*
PUTCHR  STX     PUT4
        PSH B
        LDX     BUFPTR
        STA A   0,X
        INX
        STX     BUFPTR
        INC     BUFCNT
        CMP A   #4      EOF CODE?
        BNE     NEOF    NOPE
        INC     EOFFLG  SET EOF FLAG
        BRA     PUT1    WRITE THE WHOLE BUFFER
NEOF    TST     BUFCNT  BUFFER FULL?
        BNE     PUT3    NOPE, SKIP THE WRITE
        SPC     1
* WRITE OBJECT BUFFER TO DISK
PUT1    LDX     DSKPTR
        STX     TRKSEC
        STX     BAKLNK
        STX     FWDLNK
        JSR     FWDCAL
        LDX     #0
        STX     TRGTAD
        TST     BOFFLG  BEGINNING OF FILE?
        BEQ     NBOF    NOPE
        STX     BAKLNK  YES, CLEAR BAKLNK
        CLR     BOFFLG  CLEAR THE BOF FLAG
NBOF    TST     EOFFLG  END OF FILE SET?
        BEQ     PUT2    NOPE, USE NORMAL FWDLNK
        STX     FWDLNK  CLEAR THE FORWARD LINK
PUT2    LDA A   #$ED    EDITOR FILE SPEC
        STA A   FILTYP
        LDA A   BUFCNT
        STA A   BYTCNT
        LDX     #BUFFER
        STX     BUFPTR
        STX     TA
        JSR     WRTSEC
        BCC     GDWRT
        JMP     ERROR
GDWRT   LDX     FWDLNK
        STX     DSKPTR
PUT3    PUL     B
        LDX     PUT4
        RTS
        SPC     2
OUTCH   LDA B   PORT
        ASR B
        ASR B
        BCC     OUTCH
        STA A   PORT+1
        RTS
        SPC     2
IRQRTN  LDA A   PORT+1
        AND A   #$7F
        LDX     BUFPTI
        STA A   0,X
        INX
        CMP A   #CNTRLZ EOF CODE IS ALSO END OF BUFFER
        BEQ     YXOFF
        CPX     #CHRBUF+BUFLEN-4        END OF BUFFER LESS 4
        BNE     NEOB
YXOFF   INC     BFLAG   SET BUFFER FULL FLAG
NEOB    STX     BUFPTI  UPDATE POINTER
        LDA A   #$FF    SET LAST CHAR FLAG
        STA A   0,X
        RTI             RETURN FROM INTERRUPT
        SPC     1
* TEMPS FOR DISK
BOFFLG  RMB     1       BEGINNING OF FILE FLAG
EOFFLG  RMB     1       END OF FILE FLAG
BUFCNT  RMB     1       BUFFER COUNTER
XTEMP   RMB     2       SAVE X
PUT4    RMB     2       SAVE X
DSKPTR  RMB     2       POINTER TO THE DISKETTE
BUFPTR  RMB     2       POINTER INTO DISK BUFFER
BUFFER  RMB     256     DISK BUFFER
        SPC     1
* TEMPS FOR INTERRUPT AND PARSE ROUTINE
BFLAG   RMB     1       BUFFER FULL FLAG
BUFPTI  RMB     2       INTERRUPT ROUTINE BUFFER POINTER
CHRBUF  RMB     BUFLEN  CHARACTER BUFFER
        SPC     1
        END

