home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / trs80model4 / m4xfer.asm < prev   
Assembly Source File  |  2020-01-01  |  25KB  |  937 lines

  1. ;    M4XFER/ASM
  2. ;
  3. ;    FILE ROUTINES
  4. ;
  5. ;    Output the characters in a packet
  6. ;
  7. PTCHR    LD    HL,PUTFILE
  8.     CALL    DECODE
  9.     JP    Z,RSKP
  10.     RET
  11. ;
  12. ;    Get a packets worth of data
  13. ;
  14. GTCHR    LD    A,(EOFLAG)    ;Check for EOF
  15.     OR    A        ;Set or reset Z
  16.     RET    NZ        ;Return if EOF occured
  17.     CALL    GETPKT        ;Get a packets worth
  18.     JP    RSKP        ;Return saying that something is there
  19. ;
  20. ;    Put a character to a file
  21. ;
  22. PUTFILE    LD    DE,(RFIDCB)    ;Get the DCB address
  23.     CALL    XPUT        ;Output the character
  24.     RET    Z        ;Return if no error
  25.     CALL    XERROR0        ;Print an error message
  26.     LD    A,'X'        ;Set EOT flag
  27.     LD    (CZSEEN),A
  28.     RET            ;Return to caller
  29. ;
  30. ;    Put a character to the buffer
  31. ;
  32. PUTDATA    PUSH    HL        ;Save the pointer
  33.     LD    HL,(RMTPTR)    ;Get the pointer to the start
  34.     LD    (HL),A        ;Store the character
  35.     INC    HL        ;Point to next
  36.     LD    (HL),0        ;Terminate the string
  37.     LD    (RMTPTR),HL    ;Save the new pointer
  38.     POP    HL        ;Restore the pointer
  39.     CP    A        ;Set Z status
  40.     RET            ;Return
  41. ;
  42. ;    Get a character from the packet, doing all necessary decoding
  43. ;
  44. GETDATA    PUSH    HL        ;Save HL
  45.     LD    (RMTPTR),HL    ;Store as the place to but the data
  46.     LD    HL,PUTDATA    ;Get the address of the routine
  47.     CALL    DECODE        ;Decode it
  48.     POP    HL        ;Restore HL
  49.     RET            ;Return to caller
  50. ;
  51. ;    Call this address to call the address in HL
  52. ;
  53. CALLHL    JP    (HL)
  54.     RET
  55. ;
  56. ;    Get the next file name pointed to by MFNPTR and put it in FCB.
  57. ;    If no more names are available, then the Carry Flag will be
  58. ;    set apon return.  Otherwise, NC will exist, and FCB will
  59. ;    contain a valid TRSDOS filename.
  60. ;
  61. MFNAME    PUSH    BC        ;Save the registers
  62.     PUSH    DE
  63.     PUSH    HL
  64.     LD    B,32        ;Blank the FCB
  65.     LD    HL,MFREQ
  66.     PUSH    HL
  67.     LD    A,32
  68. MFN0A    LD    (HL),A
  69.     INC    HL
  70.     DJNZ    MFN0A
  71.     LD    HL,(MFNPTR)    ;Get the start of the next name
  72.     POP    DE        ;Get the destination
  73.     CALL    XFSPEC        ;Move and convert
  74.     LD    (MFNPTR),HL    ;Save start of next name
  75.     PUSH    AF        ;Save the returned flags
  76.     LD    HL,MFREQ    ;Get the source
  77.     PUSH    HL        ;Make a copy
  78.     LD    DE,TFILNAM    ;Get the destination
  79.     LD    BC,32        ;Get the byte count
  80.     LDIR            ;Move them
  81.     LD    HL,TFILNAM    ;Terminate the string with EOS
  82.     LD    A,3        ;Find the ETX
  83.     LD    BC,32        ;Only look this far
  84.     CPIR            ;Find it
  85.     DEC    HL        ;Back up to the ETX
  86.     LD    (HL),EOS    ;Put in an ETX
  87.     POP    HL        ;Restore the source address
  88.     LD    DE,FCB        ;Get the FCB destination
  89.     CALL    XFSPEC        ;Move a copy into FCB
  90.     POP    AF        ;Restore the return flags
  91.     SCF            ;Set initial return flag
  92.     JR    NZ,MFFIX1    ;Abort on error
  93.     CCF            ;Reset Carry to return valid name
  94. MFFIX1    POP    HL        ;Restore the registers
  95.     POP    DE
  96.     POP    BC
  97.     RET            ;Return to the caller
  98. ;
  99. ;    Open the filename in FCB for output
  100. ;
  101. GETFIL    LD    A,0FFH
  102.     LD    (FILFLG),A    ;No file open
  103.     XOR    A        ;Get zero
  104.     LD    (EOFLAG),A    ;Not the end of file
  105.     LD    (LSTCHR),A    ;No previous character
  106.     PUSH    HL
  107.     LD    DE,FCB        ;Get the FCB
  108.     LD    HL,BUFF        ;Get the data buffer
  109.     LD    B,0        ;Select LRL=256
  110.     CALL    XOPEN        ;Open the file (at least try)
  111.     POP    HL        ;Restore old HL
  112.     JR    Z,GETFIL1    ;Return if normal open
  113. ;
  114. ;    The following code handles files with LRL's different then
  115. ;    256.  The LRL open fault can be ignored in this case.
  116. ;
  117.     CP    42        ;Check for different LRL ERROR
  118.     JP    NZ,ERRORD    ;If not, then error
  119. GETFIL1    JP    RSKP        ;Ignore LRL open fault
  120. ;
  121. ;    PACKET ROUTINES
  122. ;
  123. ;    Send a packet out the comm port
  124. ;
  125. ;    This routine assembles a packet from the arguments given and
  126. ;    sends it out the communications port
  127. ;
  128. ;    Arguments:
  129. ;
  130. ;    A     - TYPE OF PACKET (D,Y,N,S,R,E,F,Z,T)
  131. ;    ARGBLK     - PACKET SEQUENCE NUMBER
  132. ;    ARGBLK+1 - NUMBER OF DATA CHARACTERS
  133. ;
  134. SPACK    LD    (ARGBLK+2),A    ;Save data for resend to use
  135.     LD    HL,PACKET    ;GET ADDRESS OF THE SEND PACKET.
  136.     LD    A,(SSOHCH)    ;GET THE START OF HEADER CHAR.
  137.     PUTHL    A        ;PUT IN THE PACKET.
  138.     LD    A,(CURCHK)    ;GET CURRENT CHECKSUM TYPE
  139.     SUB    '1'        ;DETERMINE EXTRA LENGTH OF CHECKSUM
  140.     LD    B,A        ;COPY LENGTH
  141.     LD    A,(ARGBLK+1)    ;GET THE NUMBER OF DATA CHARS.
  142.     ADD    A,' '+3        ;Real packet length made printable
  143.     ADD    A,B        ;DETERMINE OVERALL LENGTH
  144.     PUTHL    A        ;Put in the packet
  145.     LD    B,0        ;ZERO THE CHECKSUM AC.
  146.     LD    C,A        ;START THE CHECKSUM.
  147.     LD    A,(ARGBLK)    ;GET THE PACKET NUMBER.
  148.     TOCHAR            ;ADD A SPACE SO THE NUMBER IS PRINTABLE.
  149.     PUTHL    A        ;Put in the packet
  150.     CALL    NXTSUM        ;Get next checksum value
  151.     LD    A,(ARGBLK+2)    ;GET THE PACKET TYPE.
  152.     PUTHL    A        ;Put in the packet
  153.     CALL    NXTSUM
  154. SPACK2    LD    A,(ARGBLK+1)    ;GET THE PACKET SIZE.
  155.     IFZ    SPACK3
  156.     DEC    A        ;DECREMENT THE CHAR COUNT.
  157.     LD    (ARGBLK+1),A    ;PUT IT BACK.
  158.     LD    A,(HL)        ;GET THE NEXT CHAR.
  159.     INC    HL        ;POINT TO NEXT CHAR.
  160.     CALL    NXTSUM        ;Compute next checksum
  161.     JR    SPACK2        ;GO TRY AGAIN.
  162. SPACK3    LD    A,(CURCHK)    ;GET THE CURRENT CHECKSUM TYPE
  163.     IFA    '2',SPACK4
  164.     JR    NC,SPACK5    ;Go do CRC if '3'
  165.     LD    A,C        ;GET THE CHARACTER TOTAL.
  166.     AND    0C0H        ;TURN OFF ALL BUT THE TWO HIGH ORDER BITS
  167.     RLCA            ;TWO LEFT ROTATES SAME AS 6 RIGHTS
  168.     RLCA            ;.  .    .
  169.     ADD    A,C        ;ADD IT TO THE OLD BITS.
  170.     AND    3FH        ;TURN OFF THE TWO HIGH ORDER BITS.
  171.     TOCHAR            ;ADD A SPACE SO THE NUMBER IS PRINTABLE.
  172.     PUTHL    A        ;Put in the packet
  173.     JP    SPACK7        ;GO STORE EOL CHARACTER
  174. ;
  175. ;HERE FOR 3 CHARACTER CRC-CCITT
  176. ;
  177. SPACK5    LD    (HL),0        ;STORE A NULL FOR CURRENT END
  178.     PUSH    HL        ;SAVE H
  179.     LD    HL,PACKET+1    ;POINT TO FIRST CHECKSUMED CHARACTER
  180.     CALL    CRCCLC        ;CALCULATE THE CRC
  181.     POP    HL        ;RESTORE THE POINTER
  182.     LD    C,E        ;GET LOW ORDER HALF FOR LATER
  183.     LD    B,D        ;COPY THE HIGH ORDER
  184.     LD    A,D        ;GET THE HIGH ORDER PORTION
  185.     RLCA            ;SHIFT OFF LOW 4 BITS
  186.     RLCA            ;.  .    .
  187.     RLCA            ;.  .    .
  188.     RLCA            ;.  .    .
  189.     AND    0FH        ;KEEP ONLY LOW 4 BITS
  190.     TOCHAR            ;PUT INTO PRINTING RANGE
  191.     LD    (HL),A        ;STORE THE CHARACTER
  192.     INC    HL        ;POINT TO NEXT POSITION
  193. ;
  194. ;HERE FOR TWO CHARACTER CHECKSUM
  195. ;
  196. SPACK4    LD    A,B        ;GET HIGH ORDER PORTION
  197.     AND    0FH        ;ONLY KEEP LAST FOUR BITS
  198.     RLCA            ;SHIFT UP TWO BITS
  199.     RLCA            ;. .  .
  200.     LD    B,A        ;COPY BACK INTO SAFE PLACE
  201.     LD    A,C        ;GET LOW ORDER HALF
  202.     RLCA            ;SHIFT HIGH TWO BITS
  203.     RLCA            ;TO LOW TWO BITS
  204.     AND    03H        ;KEEP ONLY TWO LOW BITS
  205.     OR    B        ;GET HIGH ORDER PORTION IN
  206.     TOCHAR            ;CONVERT TO PRINTING CHARACTER RANGE
  207.     PUTHL    A        ;Store the character
  208.     LD    A,C        ;GET LOW ORDER PORTION
  209.     AND    3FH        ;KEEP ONLY SIX BITS
  210.     TOCHAR            ;CONVERT TO PRINTING RANGE
  211.     PUTHL    A        ;Store the character
  212. SPACK7    LD    A,(SEOL)    ;GET THE EOL THE OTHER HOST WANTS.
  213.     PUTHL    A        ;Store the character
  214.     PUTHL    0        ;End with a NULL
  215.     LD    A,(DBFLG)
  216.     OR    A
  217.     JR    Z,SPACK8    ;debug is off
  218.     PUTHL    EOS        ;Add terminator
  219. SPACK8    CALL    OUTPKT        ;CALL THE SYSTEM DEPENDENT ROUTINE.
  220.     JP    QUIT
  221.     JP    RSKP
  222. ;
  223. ;    WRITE OUT A PACKET.
  224. ;
  225. OUTPKT    LD    A,(SPAD)    ;GET THE NUMBER OF PADDING CHARS.
  226.     LD    B,A
  227. OUTPK2    DEC    B
  228.     JP    M,OUTPK4
  229.     LD    A,(SPADCH)    ;GET THE PADDING CHAR.
  230.     LD    E,A        ;PUT THE CHAR IN RIGHT AC.
  231.     CALL    OUTCHR        ;OUTPUT IT.
  232.     JR    OUTPK2
  233. OUTPK4    LD    A,(DBFLG)
  234.     IFZ    OUTPK5        ;If not on, then check for logfile
  235.     STROUT    SPPOS        ;Print the SPACK=> message
  236.     STROUT    PACKET+1    ;Print the data
  237. OUTPK5    LD    A,(DEBLOG)    ;See if logging in effect
  238.     IFZ    OUTPK7        ;If not, then finish up
  239.     LD    DE,DFCB        ;Get the debug FCB
  240.     TRLOG    SPPOS,OUTPK6    ;Log the SPACK=> message
  241.     TRLOG    PACKET,OUTPK6    ;Log the packet data
  242.     JR    OUTPK7
  243. OUTPK6    XOR    A
  244.     LD    (DEBLOG),A
  245.     LD    DE,DFCB
  246.     CALL    XCLOSE
  247. OUTPK7    LD    HL,PACKET    ;POINT TO THE PACKET.
  248. OUTPK10    LD    A,(HL)        ;GET THE NEXT CHARACTER.
  249.     IFZ    OUTPK11        ;Return success if EOS found
  250.     LD    E,A        ;PUT THE CHAR IN RIGHT AC.
  251.     CALL    OUTCHR        ;OUTPUT THE CHARACTER.
  252.     INC    HL        ;INCREMENT THE CHAR POINTER.
  253.     JR    OUTPK10
  254. OUTPK11    LD    A,(STURN)    ;Is turn around needed?
  255.     IFZ    OUTPK12        JUMP IF NOT NEEDED
  256.     LD    E,A        ;Get the character
  257.     CALL    OUTCHR        ;Output it
  258. OUTPK12    JP    RSKP        ;Return no error
  259. ;
  260. ;    Compute next checksum
  261. ;
  262. NXTSUM    ADD    A,C
  263.     LD    C,A
  264.     LD    A,0        ;Must use load to preserve Carry flag
  265.     ADC    A,B
  266.     LD    B,A
  267.     RET
  268. ;
  269. ;THIS ROUTINE WAITS FOR A PACKET TO ARRIVE FROM THE HOST. IT READS
  270. ;CHARACTERS UNTIL IT FINDS THE SOH. IT THEN READS THE PACKET INTO PACKET.
  271. ;
  272. ;RETURNS +1 FAILURE (IF THE CHECKSUM IS WRONG OR THE PACKET TRASHED)
  273. ;    +3 SUCCESS WITH A        - MESSAGE TYPE
  274. ;            ARGBLK - MESSAGE NUMBER
  275. ;            ARGBLK+1 - LENGTH OF DATA
  276. ;
  277. RPACK    LD    A,(RTIME)    ;Get the timeout value
  278.     LD    HL,0
  279.     IFZ    RPACK1        ;If zero, then no timeout
  280.     LD    L,A        ;Get the value as 16 bits
  281.     LD    C,30        ;Get the factor to extend it by
  282.     CALL    XMUL16        ;Do the multiplication
  283.     LD    H,L        ;Slide the 24 bit result down
  284.     LD    L,A
  285. RPACK1    LD    (SVTIMER),HL    ;Save it as the timer value
  286. RPACK2    CALL    STARTTIMER    ;Start the timer for receive timeout
  287.     CALL    INPKT        ;READ UP TO A CARRIAGE RETURN.
  288.     JP    QUIT        ;RETURN BAD.
  289.     CALL    STOPTIMER    ;Stop the timeout countdown
  290. RPACK3    CALL    GETCHR        ;GET A CHARACTER.
  291.     JP    RPACK2        ;HIT A CR;NULL LINE; JUST START OVER.
  292.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  293.     JR    NZ,RPACK3    ;NO, GO UNTIL IT IS.
  294. RPACK4    CALL    GETCHR        ;GET A CHARACTER.
  295.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  296.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  297.     JR    Z,RPACK4    ;YES, THEN GO START OVER.
  298.     LD    (PACKET+1),A    ;STORE IN PACKET ALSO
  299.     LD    C,A        ;START THE CHECKSUM.
  300.     LD    A,(CURCHK)    ;GET BLOCK CHECK TYPE
  301.     SUB    '1'        ;DETERMINE EXTRA LENGTH OF BLOCK CHECK
  302.     LD    B,A        ;GET A COPY
  303.     LD    A,C        ;GET BACK LENGTH CHARACTER
  304.     SUB    ' '+3        ;GET THE REAL DATA COUNT.
  305.     SUB    B        ;GET TOTAL LENGTH
  306.     LD    (ARGBLK+1),A
  307.     LD    B,0        ;CLEAR HIGH ORDER HALF OF CHECKSUM
  308.     CALL    GETCHR        ;GET A CHARACTER.
  309.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  310.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  311.     JR    Z,RPACK4    ;YES, THEN GO START OVER.
  312.     LD    (ARGBLK),A
  313.     LD    (PACKET+2),A    ;SAVE ALSO IN PACKET
  314.     CALL    NXTSUM        ;ADD THE CHARACTER TO THE CHECKSUM
  315.     LD    A,(ARGBLK)
  316.     SUB    ' '        ;GET THE REAL PACKET NUMBER.
  317.     LD    (ARGBLK),A
  318.     CALL    GETCHR        ;GET A CHARACTER.
  319.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  320.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  321.     JR    Z,RPACK4    ;YES, THEN GO START OVER.
  322.     LD    (TEMP1),A    ;SAVE THE MESSAGE TYPE.
  323.     LD    (PACKET+3),A    ;SAVE IN PACKET
  324.     LD    (RECTYP),A
  325.     CALL    NXTSUM        ;ADD THE CHARACTER TO THE CHECKSUM.
  326.     LD    A,(ARGBLK+1)    ;GET THE NUMBER OF DATA CHARACTERS.
  327.     LD    (TEMP2),A
  328.     LD    HL,DATA        ;POINT TO THE DATA BUFFER.
  329.     LD    (DATPTR),HL
  330. RPACK5    LD    A,(TEMP2)
  331.     DEC    A        ;ANY DATA CHARACTERS?
  332.     JP    M,RPACK6    ;IF NOT GO GET THE CHECKSUM.
  333.     LD    (TEMP2),A
  334.     CALL    GETCHR        ;GET A CHARACTER.
  335.     JP    QUIT        ;HIT THE END-OF-LINE, RETURN BAD.
  336.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  337.     JR    Z,RPACK4    ;YES, THEN GO START OVER.
  338.     LD    HL,(DATPTR)
  339.     PUTHL    A        ;Store the character
  340.     LD    (DATPTR),HL
  341.     CALL    NXTSUM        ;ADD THE CHARACTER TO THE CHECKSUM.
  342.     JR    RPACK5        ;GO GET ANOTHER.
  343. RPACK6    CALL    CHKECHO        ;See if only echo of previous
  344.     JP    RPACK3        ;Yes, restart
  345.     CALL    GETCHR        ;Get a character
  346.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  347.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  348.     JP    Z,RPACK4    ;YES, THEN GO START OVER.
  349.     SUB    ' '        ;TURN THE CHAR BACK INTO A NUMBER.
  350.     LD    (TEMP3),A
  351. ;DETERMINE TYPE OF CHECKSUM
  352.     LD    A,(CURCHK)    ;GET THE CURRENT CHECKSUM TYPE
  353.     IFA    '2',RPACK9    ;Jump if 2 character
  354.     JR    NC,RPACK8    ;Jump if 3 character
  355. ;
  356. ;    1 character checksum
  357. ;
  358.     LD    A,C        ;Get the character count
  359.     AND    0C0H        ;Keep 2 MSB's
  360.     RLCA            ;Move them to 2 LSB's
  361.     RLCA
  362.     ADD    A,C        ;Add the 2 low bits to bottom
  363.     AND    3FH        ;Remove 2 high bits after add
  364.     LD    B,A
  365.     LD    A,(TEMP3)    ;GET THE REAL RECEIVED CHECKSUM.
  366.     IFA    B,RPACK10    ;Jump checksum OK
  367. RPACK7    CALL    UPDRTR        ;If checksum bad, update retries
  368.     RET            ;Return error
  369. ;
  370. ;    Here for three character CRC-CCITT
  371. ;
  372. RPACK8    LD    HL,(DATPTR)    ;GET THE ADDRESS OF THE DATA
  373.     LD    (HL),0        ;Store a zero in the buffer as terminator
  374.     LD    HL,PACKET+1    ;POINT AT START OF CHECKSUMMED REGION
  375.     CALL    CRCCLC        ;CALCULATE THE CRC
  376.     LD    C,E        ;SAVE LOW ORDER HALF FOR LATER
  377.     LD    B,D        ;ALSO COPY HIGH ORDER
  378.     LD    A,D        ;GET HIGH BYTE
  379.     RLCA            ;WANT HIGH FOUR BITS
  380.     RLCA            ;.  .    .
  381.     RLCA            ;AND SHIFT TWO MORE
  382.     RLCA            ;.  .    .
  383.     AND    0FH        ;KEEP ONLY 4 BITS
  384.     LD    D,A        ;BACK INTO D
  385.     LD    A,(TEMP3)    ;GET FIRST VALUE BACK
  386.     IFANOT    D,RPACK7    ;Jump if not correct
  387.     CALL    GETCHR        ;GET A CHARACTER.
  388.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  389.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  390.     JP    Z,RPACK4    ;YES, THEN GO START OVER.
  391.     SUB    ' '        ;REMOVE SPACE OFFSET
  392.     LD    (TEMP3),A    ;STORE FOR LATER CHECK
  393. ;
  394. ;    Here for a two character checksum and last two characters of CRC
  395. ;
  396. RPACK9    LD    A,B        ;GET HIGH ORDER PORTION
  397.     AND    0FH        ;ONLY FOUR BITS
  398.     RLCA            ;Shift up 2 bits
  399.     RLCA
  400.     LD    B,A        ;Save back into B
  401.     LD    A,C        ;Get low order byte
  402.     RLCA            ;Move the 2 MSB's to 2 LSB's
  403.     RLCA
  404.     AND    03H        ;Save only low 2 bits
  405.     OR    B        ;Get other 4 bits
  406.     LD    B,A        ;Save back into B
  407.     LD    A,(TEMP3)    ;Get this portion of the checksum
  408.     IFANOT    B,RPACK7    ;If wrong, then give up
  409.     CALL    GETCHR        ;GET A CHARACTER.
  410.     JP    QUIT        ;HIT THE CARRIAGE RETURN, RETURN BAD.
  411.     CALL    CPRSOH        ;IS THE CHAR THE START OF HEADER CHAR?
  412.     JP    Z,RPACK4    ;YES, THEN GO START OVER.
  413.     SUB    ' '        ;REMOVE SPACE OFFSET
  414.     LD    B,A        ;SAVE IN SAFE PLACE
  415.     LD    A,C        ;GET LOW 8 BITS OF CHECKSUM
  416.     AND    3FH        ;KEEP ONLY 6 BITS
  417.     IFANOT    B,RPACK7    ;Jump if bad value
  418. RPACK10    LD    HL,(DATPTR)
  419.     PUTHL    0        ;End with a NULL
  420.     LD    A,(TEMP1)    ;GET THE TYPE.
  421.     JP    RSKP
  422. ;
  423. ;    Input a packet
  424. ;
  425. INPKT    LD    HL,RECPKT    ;POINT TO THE BEGINNING OF THE PACKET.
  426.     LD    (PKTPTR),HL
  427. INPKT2    CALL    INCHR        ;GET A CHARACTER.
  428.     JP    INPKT7        ;Skip out if key typed that is valid
  429.     LD    HL,(PKTPTR)    ;Get the packet position
  430.     PUTHL    A        ;Store the character
  431.     LD    (PKTPTR),HL    ;Save the pointer
  432.     CALL    CPREOL        ;IS IT THE EOL CHARACTER?
  433.     JR    NZ,INPKT2
  434.     PUTHL    EOS        ;Put in the terminator
  435.     LD    A,(DBFLG)
  436.     IFZ    INPKT3        ;Jump if debug off
  437.     STROUT    RPPOS        ;Output RPACK=> message
  438.     STROUT    RECPKT+1    ;Print the received packet
  439. INPKT3    LD    A,(DEBLOG)    ;See if debug log in on
  440.     IFZ    INPKT6        ;Jump if not enabled
  441.     LD    DE,DFCB        ;Get the FCB
  442.     TRLOG    RPPOS,OUTPK6    ;Log the RPACK=> message
  443.     TRLOG    RECPKT+1,OUTPK6    ;Log the packet
  444. INPKT6    LD    HL,RECPKT
  445.     LD    (PKTPTR),HL    ;SAVE THE PACKET POINTER.
  446.     LD    A,(RTURN)    ;Get the turn around character
  447.     CALL    WAITT        ;Go wait for it.
  448.     JP    RSKP        ;Return no error
  449. ;
  450. ;    Error return processing.  This code deals with the keystrokes
  451. ;    that are recognized in INCHR().
  452. ;
  453. INPKT7    LD    A,(CZSEEN)
  454.     IFANOT    'A',INPKT8    ;Status
  455.     CALL    SHOTRANS    ;Show the transfer status
  456.     JR    INPKT16
  457. INPKT8    IFANOT    'B',INPKT9    ;Cancel Batch?
  458.     LD    A,'Z'        ;Get the packet type
  459.     LD    (CZSEEN),A
  460.     LD    DE,CBATCH    ;Get the cancel message
  461.     CALL    CONDIS        ;Print it if not doing REMOTE commands
  462.     JP    INPKT2
  463. INPKT9    IFANOT    'F',INPKT10    ;Cancel FILE?
  464.     LD    A,'X'        ;Get the PACKET type
  465.     LD    (CZSEEN),A
  466.     LD    DE,CFILE    ;Get the cancel message
  467.     CALL    CONDIS        ;Print it if not doing REMOTE commands
  468.     JP    INPKT2
  469. INPKT10    IFANOT    'E',INPKT12    ;Send error, and ABORT?
  470.     XOR    A
  471.     LD    (CZSEEN),A    ;Reset the key pressed flag
  472.     CALL    STOPTIMER    ;Stop the timer
  473.     LD    A,ABRTMSGLEN    ;Get length of message
  474.     LD    (ARGBLK+1),A    ;Save the length
  475.     LD    HL,ABRTMSG    ;Get the message
  476.     LD    DE,DATA        ;Where to put it
  477.     LD    C,A        ;Get the length in BC
  478.     LD    B,0
  479.     LDIR            ;Move the message
  480.     LD    A,'E'        ;Get the packet type
  481.     CALL    SPACK        ;Send the error packet
  482.     JP    ABORT        ;Give up on an error
  483.     JP    KERMIT        ;Give up completely
  484. INPKT12    IFANOT    'C',INPKT14    ;Cancel transfer, immediately
  485.     XOR    A
  486.     LD    (CZSEEN),A    ;Zap the old key pressed
  487.     CALL    STOPTIMER    ;Stop the timer
  488.     JP    KERMIT        ;Jump to restart
  489. INPKT14    IFANOT    'D',INPKT18    ;Toggle debug mode?
  490.     LD    A,(DBFLG)    ;Get the debug flag
  491.     XOR    1        ;Toggle value
  492.     LD    (DBFLG),A    ;Store it back
  493. INPKT16    XOR    A        ;Finish up handling, reset key pressed
  494.     LD    (CZSEEN),A    ;Reset the flag
  495.     JP    INPKT2        ;Get the next character
  496. INPKT18    IFANOT    'H',INPKT20    ;Help?
  497.     STROUT    HELPMSG        ;Print the message
  498.     JR    INPKT16        ;Finish up
  499. INPKT20    XOR    A        ;Otherwise, return no input to force
  500.     LD    (CZSEEN),A    ;resend to occur...
  501.     RET            ;Return no input packet
  502. ;
  503. GETCHR    LD    HL,(PKTPTR)    ;GET THE PACKET POINTER.
  504.     LD    A,(HL)        ;GET THE CHAR.
  505.     INC    HL
  506.     LD    (PKTPTR),HL
  507.     CALL    CPREOL        ;IS IT THE END OF LINE
  508.     JP    NZ,RSKP        ;IF NOT RETURN RETSKP.
  509.     RET            ;IF SO RETURN FAILURE.
  510. ;
  511. ;    Assorted comparison routines
  512. ;
  513. CPSSOH    PUSH    HL
  514.     LD    HL,SSOHCH
  515. CPSH10    CP    (HL)
  516.     POP    HL
  517.     RET
  518. CPRSOH    PUSH    HL
  519.     LD    HL,RSOHCH
  520.     JR    CPSH10
  521. CPSEOL    PUSH    HL
  522.     LD    HL,SEOL
  523.     JR    CPSH10
  524. CPREOL    PUSH    HL
  525.     LD    HL,REOL
  526.     JR    CPSH10
  527. CPSPAD    PUSH    HL
  528.     LD    HL,SPADCH
  529.     JR    CPSH10
  530. CPRPAD    PUSH    HL
  531.     LD    HL,RPADCH
  532.     JR    CPSH10
  533. CPSTME    PUSH    HL
  534.     LD    HL,STIME
  535.     JR    CPSH10
  536. CPRTME    PUSH    HL
  537.     LD    HL,RTIME
  538.     JR    CPSH10
  539. CPSQTE    PUSH    HL
  540.     LD    HL,SQUOTE
  541.     JR    CPSH10
  542. CPRQTE    PUSH    HL
  543.     LD    HL,RQUOTE
  544.     JR    CPSH10
  545. ;
  546. ;THIS ROUTINE WILL CALCULATE A CRC USING THE CCITT POLYNOMIAL FOR
  547. ;A STRING.
  548. ;
  549. ;USAGE
  550. ;    HL/ ADDRESS OF STRING
  551. ;    A/ LENGTH OF STRING
  552. ;    CALL    CRCCLC
  553. ;
  554. ;16-BIT CRC VALUE IS RETURNED IN DE.
  555. ;
  556. ;REGISTERS BC AND HL ARE PRESERVED.
  557. ;
  558. CRCCLC    PUSH    HL        ;SAVE HL
  559.     PUSH    BC        ;AND BC
  560.     LD    DE,0        ;INITIAL CRC VALUE IS 0
  561. CRCCL0    LD    A,(HL)        ;GET A CHARACTER
  562.     OR    A        ;CHECK IF ZERO
  563.     JP    Z,CRCCL1    ;IF SO, ALL DONE
  564.     PUSH    HL        ;SAVE THE POINTER
  565.     XOR    E        ;ADD IN WITH PREVIOUS VALUE
  566.     AND    0FH        ;Keep only 4 bits
  567.     LD    B,0
  568.     GETCRCTAB
  569.     CALL    SRLDE4        ;Shift CRC right by 4
  570.     XORATHL    E
  571.     INC    HL
  572.     XORATHL    D
  573.     POP    HL
  574.     PUSH    HL
  575.     LD    A,(HL)
  576.     SRL    A
  577.     SRL    A
  578.     SRL    A
  579.     SRL    A
  580.     XOR    E
  581.     AND    0FH
  582.     GETCRCTAB
  583.     CALL    SRLDE4
  584.     XORATHL    E
  585.     INC    HL
  586.     XORATHL    D
  587.     POP    HL        ;AND H
  588.     INC    HL        ;POINT TO NEXT CHARACTER
  589.     JP    CRCCL0        ;GO GET NEXT CHARACTER
  590. CRCCL1    POP    BC        ;RESTORE B
  591.     POP    HL        ;AND HL
  592.     RET            ;AND RETURN, DE=CRC-CCITT
  593. ;
  594. ;    Shift DE right by 4 bits
  595. ;
  596. SRLDE4    SRL    D
  597.     RR    E
  598.     SRL    D
  599.     RR    E
  600.     SRL    D
  601.     RR    E
  602.     SRL    D
  603.     RR    E
  604.     RET
  605. ;
  606. CRCTAB    DW    00000H
  607.     DW    01081H
  608.     DW    02102H
  609.     DW    03183H
  610.     DW    04204H
  611.     DW    05285H
  612.     DW    06306H
  613.     DW    07387H
  614.     DW    08408H
  615.     DW    09489H
  616.     DW    0A50AH
  617.     DW    0B58BH
  618.     DW    0C60CH
  619.     DW    0D68DH
  620.     DW    0E70EH
  621.     DW    0F78FH
  622. ;
  623. GETNPNT    LD    A,(HL)
  624.     INC    HL
  625.     LD    (OUTPNT),HL
  626.     RET
  627. ;
  628. ;    Check if RFIDCB points at FCB.
  629. ;
  630. CHKFCB    LD    DE,(RFIDCB)    ;Get the current FCB/DCB pointer
  631.     LD    HL,FCB        ;Get the Address of FCB
  632.     OR    A        ;Reset the carry
  633.     SBC    HL,DE        ;Compute the difference
  634.     RET            ;Return the flags
  635. ;
  636. INCKTRANS    PUSH    HL    ;Save the regs
  637.     PUSH    AF
  638.     LD    HL,(KTRANS)    ;Get the current count
  639.     INC    HL        ;Add one to it
  640.     LD    A,H        ;Check for overflow
  641.     OR    L        ;Set the flags
  642.     LD    A,1        ;Set the ok to change curtrans flag
  643.     CALL    Z,ADD64K    ;Add 64K if overflow
  644.     LD    (KTRANS),HL    ;Save the new counter
  645.     LD    (CURTRANS),HL    ;Store as current count too
  646.     POP    AF        ;Restore the regs
  647.     POP    HL
  648.     RET            ;Return to caller
  649. ;
  650. ;    Add 64k to the current counter (RTRANS, or STRANS, as well as
  651. ;    CURTRANS which is used for TRANSACTION logs).
  652. ;
  653. ADD64K    EQU    $
  654.     PUSH    HL        ;Save the regs
  655.     LD    L,(IX)        ;Get the current Kbyte counter
  656.     LD    H,(IX+1)
  657.     LD    BC,64        ;Add 64 to it
  658.     ADD    HL,BC
  659.     LD    (IX),L        ;Store it back
  660.     LD    (IX+1),H
  661.     IFZ    ADD64K_1
  662.     LD    HL,(CURTRANS+2)    ;Get the current value
  663.     ADD    HL,BC        ;Add 64
  664.     LD    (CURTRANS+2),HL    ;Store new value
  665. ADD64K_1    POP    HL        ;Restore HL
  666.     RET            ;Return
  667. ;
  668. ;    Wait for the turn around character in A
  669. ;
  670. TTURN    DB    0
  671. ;
  672. WAITT    OR    A        ;Check for no turn around
  673.     RET    Z        ;return on ZERO
  674.     LD    (TTURN),A    ;Save the character
  675. WAITT1    CALL    INCHR        ;Get a character
  676.     JP    INPKT7        ;Process keyboard character
  677.     LD    C,A        ;Save the character
  678.     LD    A,(TTURN)    ;Check for turn around
  679.     CP    C        ;Is the character received it?
  680.     JR    NZ,WAITT1    ;No, get another
  681.     RET
  682. ;
  683. SHOTRANS    EQU    $
  684.     STROUT    NPKTSTR        ;Print number of packets
  685.     LD    HL,(NUMPKT)
  686.     CALL    NOUT
  687.     STROUT    NRTRSTR        ;Print retries
  688.     LD    HL,(NUMRTR)
  689.     CALL    NOUT
  690.     STROUT    NCHRSTR        ;Print number of characters
  691.     LD    HL,(KTRANS)
  692.     CALL    NOUT
  693.     STROUT    NRECCH        ;Number K of characters recvd
  694.     LD    HL,(RTRANS)
  695.     CALL    NOUT
  696.     LD    A,'K'
  697.     CALL    CONOUT
  698.     STROUT    NSNDCH        ;Print number of characters sent
  699.     LD    HL,(STRANS)
  700.     CALL    NOUT
  701.     LD    A,'K'
  702.     CALL    CONOUT
  703.     JP    NEWLIN
  704. ;
  705. CHKECHO    PUSH    BC
  706.     LD    A,(SNDTYP)
  707.     LD    C,A
  708.     LD    A,(RECTYP)
  709.     CP    C
  710.     POP    BC
  711.     RET    Z
  712.     JP    RSKP
  713. ;
  714. ;    Convert KERMIT filename to TRSDOS filename
  715. ;
  716. GOFIL    LD    HL,RMTDATA    ;Where to put the name
  717.     LD    A,(ARGBLK+1)    ;Number of characters
  718.     CALL    GETDATA        ;Get the name in RMTDATA (HL saved)
  719.     LD    DE,MFREQ    ;Destination
  720.     LD    BC,8        ;Max characters for first field
  721. GOFIL1    EQU    $
  722.     LD    A,(HL)        ;At end of field?
  723.     IFA    '.',GOFIL8    ;Jump if at separator
  724.     IFALT    ' ',GOFIL15    ;Stop if at end of string
  725.     LD    A,C        ;Check range
  726.     IFANOT    8,GOFIL2    ;If not first character, then skip
  727.     LD    A,(HL)        ;Get the character
  728.     CALL    ISALPHA        ;Is it alphabetic?
  729.     JR    Z,GOFIL5    ;Jump if it is
  730.     LD    (HL),'Z'    ;Change name
  731.     JR    GOFIL5        ;Join code
  732. GOFIL2    EQU    $        ;Second or later character...
  733.     LD    A,(HL)
  734.     CALL    ISALNUM        ;Is it alphanumeric?
  735.     JR    Z,GOFIL5    ;Jump if so
  736.     INC    HL        ;Skip this one
  737.     JR    GOFIL1        ;Go to the next character
  738. GOFIL5    EQU    $
  739.     LDI            ;Move the character
  740.     LD    A,B        ;Check the remaining count
  741.     OR    C
  742.     JR    NZ,GOFIL1    ;Jump if still OK
  743. GOFIL7    EQU    $
  744.     LD    A,(HL)        ;Are we at the end yet
  745.     IFA    '.',GOFIL8    ;Jump if at the separator
  746.     IFALT    ' ',GOFIL15    ;Stop it at the end
  747.     INC    HL        ;Point to next source character
  748.     JR    GOFIL7        ;Loop
  749. GOFIL8    EQU    $
  750.     LD    A,C        ;Check the count of characters moved
  751.     IFANOT    8,GOFIL9    ;Jump if at least one moved
  752.     LD    A,'X'        ;Use this as the first character
  753.     LD    (DE),A        ;Put it in
  754.     INC    DE        ;Point to next slot
  755. GOFIL9    LD    (HL),'/'    ;Put in separator
  756.     LDI            ;Move it too
  757.     LD    BC,3        ;Length of next field
  758. GOFIL10    EQU    $
  759.     LD    A,(HL)        ;Get the character
  760.     IFALT    ' ',GOFIL15    ;Jump if at the end
  761.     LD    A,C        ;Check ranges
  762.     IFANOT    3,GOFIL12    ;Jump if not first character
  763.     LD    A,(HL)        ;Get the character (Must be alphabetic)
  764.     CALL    ISALPHA        ;Is it alphbetic?
  765.     JR    Z,GOFIL13    ;Jump if it is
  766.     LD    (HL),'Z'    ;Make it alphabetic
  767.     JR    GOFIL13        ;Join other code
  768. GOFIL12    EQU    $        ;Second or third character in extension
  769.     LD    A,(HL)        ;Get the character back
  770.     CALL    ISALNUM        ;Is it alphanumeric?
  771.     JR    Z,GOFIL13    ;Jump if so
  772.     INC    HL        ;Skip it
  773.     JR    GOFIL10        ;Check the next one
  774. GOFIL13    EQU    $
  775.     LDI            ;Move a char
  776.     LD    A,B        ;Check the count
  777.     OR    C
  778.     JR    NZ,GOFIL10    ;Loop if OK
  779. GOFIL15    EQU    $
  780.     PUSH    DE        ;Put dest into HL
  781.     POP    HL
  782.     PUTHL    ':'        ;Add the default drive
  783.     LD    A,(DEFDSK)
  784.     PUTHL    A        ;Add the drive number
  785.     LD    (HL),EOS    ;Add the end of string byte
  786.     PUSH    HL        ;Save that address
  787.     LD    HL,MFREQ    ;Move the string to the save area
  788.     LD    DE,TFILNAM    ;Get the destination
  789.     LD    BC,32
  790.     LDIR            ;Move the bytes
  791.     POP    HL
  792.     LD    (HL),3        ;Put in FSPEC terminator
  793.     LD    (FCBPTR),HL    ;Save the end
  794.     LD    HL,MFREQ    ;Validate the filespec
  795.     LD    DE,FCB
  796.     CALL    XFSPEC        ;Call TRSDOS
  797.     JR    Z,GOFIL18    ;Jump if OK
  798.     LD    HL,(FCBPTR)    ;Get end of string
  799.     LD    (HL),EOS    ;Put in print terminator
  800.     STROUT    FCB        ;Print the name
  801.     JP    XERROR0        ;Print system error message and return
  802. GOFIL18    LD    HL,FCB        ;Get start
  803.     LD    BC,40        ;Maximum to look
  804.     LD    A,':'        ;Find drive spec to index off of
  805.     CPIR            ;Look for it.  MUST be there
  806.     INC    HL        ;Point after for terminator
  807.     LD    (FCBPTR),HL    ;Save it
  808.     DEC    HL        ;Back up to just after extension
  809.     DEC    HL
  810.     LD    (DATPTR),HL    ;Save it for renaming
  811.     LD    A,(FLWFLG)    ;Is file warning on?
  812.     IFZ    GOFIL30        ;Jump if not
  813.     LD    DE,FCB        ;Get the file name
  814.     LD    HL,BUFF        ;Buffer address
  815.     LD    B,0        ;LRL=256
  816.     CALL    XOPEN        ;Is it there?
  817.     JR    NZ,GOFIL30    ;Jump if not
  818.     LD    DE,INFMS5    ;Print renaming message
  819.     CALL    ERROR3
  820.     LD    DE,FCB        ;Close the file
  821.     CALL    XCLOSE        ;Close restores the filename with @FNAME
  822. GOFIL20    LD    HL,(DATPTR)    ;Get the pointer to extension
  823.     LD    DE,FCB        ;Get the start
  824.     OR    A        ;Reset the carry
  825.     PUSH    HL        ;Save it
  826.     SBC    HL,DE        ;At the beginning yet
  827.     POP    HL        ;Restore it
  828.     JR    NZ,GOFIL21    ;Jump if not
  829.     LD    DE,ERMS16    ;Oops, Can't rename it, stop
  830.     JP    PRTSTR        ;Print, and return
  831. ;
  832. GOFIL21    DEC    HL        ;Point to previous character
  833.     LD    (DATPTR),HL    ;Save the new pointer
  834. GOFIL24    LD    HL,(DATPTR)    ;Get the pointer
  835.     LD    A,(HL)        ;Get the character
  836.     IFALT    'B',GOFIL20    ;Jump if we can't change it
  837.     DEC    (HL)        ;Change the file name
  838.     LD    HL,FCB        ;Get the source address
  839.     PUSH    HL        ;Save it for later
  840.     LD    DE,TFILNAM    ;Get the destination
  841.     LD    BC,32        ;Number to move
  842.     LDIR            ;Move them
  843.     POP    DE        ;Restore stack and get FCB address
  844.     LD    HL,BUFF
  845.     LD    B,0
  846.     CALL    XOPEN        ;Is it there?
  847.     JR    NZ,GOFIL27    ;Jump if not
  848.     LD    DE,FCB        ;Close it up
  849.     CALL    XCLOSE
  850.     JR    GOFIL24        ;Try another name
  851. ;
  852. GOFIL27    LD    HL,(FCBPTR)    ;Get the end of the file name
  853.     LD    (HL),EOS    ;Add the print terminator
  854.     STROUT    FCB        ;Print the new name
  855.     LD    (HL),3        ;Put the @OPEN terminator back
  856. GOFIL30    LD    DE,FCB        ;Get the FCB
  857.     LD    HL,BUFF        ;Get the buffer
  858.     LD    B,0        ;LRL=256
  859.     CALL    XINIT        ;Create it or zap old file
  860.     JP    Z,RSKP        ;Return on success +4
  861.     PUSH    AF        ;Save error code
  862.     LD    DE,ERMS11    ;Print the error message
  863.     CALL    ERROR3
  864.     POP    AF        ;Get the error code back
  865.     JP    ERRORD        ;Print a system error
  866. ;
  867. ;    Restart timer for receive packet timeout
  868. ;
  869. STARTTIMER    PUSH    HL    ;Save the clobbered regs
  870.     PUSH    DE
  871.     PUSH    BC
  872.     LD    C,0        ;Get no timer flag for next test
  873.     LD    HL,(SVTIMER)    ;Get the timer value
  874.     LD    A,H        ;Check if not timeout wanted
  875.     OR    L
  876.     JR    Z,STARTT3    ;Don't start timer if none needed
  877.     LD    (TIMER),HL    ;Set the real counter
  878.     LD    C,8        ;Get the task slot number
  879. STARTT1    CALL    XCKTSK        ;Check is slot in use
  880.     JR    NZ,STARTT2
  881.     LD    DE,RECTIME    ;Get the TCB
  882.     PUSH    BC
  883.     CALL    XADTSK        ;Try to add the task
  884.     POP    BC        ;Restore the task slot
  885.     JR    STARTT3        ;Go save slot used
  886. STARTT2    INC    C        ;Get next possible slot
  887.     IFANOT    11,STARTT1    ;Loop if not at max task slot
  888.     STROUT    NOTIMER        ;Print error message
  889.     LD    HL,1
  890.     LD    (SVTIMER),HL    ;Zap timer
  891.     LD    C,0        ;Make sure no timer flag is on
  892. STARTT3    LD    A,C        ;Save task to stop
  893.     LD    (TASKSLOT),A
  894.     POP    BC        ;Restore registers
  895.     POP    DE
  896.     POP    HL
  897.     RET
  898. ;
  899. ;    Stop the timeout task
  900. ;
  901. STOPTIMER    PUSH    HL    ;Save the registers
  902.     PUSH    DE
  903.     PUSH    BC
  904.     LD    C,8        ;Get the task slot number
  905. TASKSLOT    EQU    $-1
  906.     CALL    XRMTSK
  907.     LD    HL,0        ;Zero out the timer
  908.     LD    (TIMER),HL
  909.     POP    BC        ;Restore the registers
  910.     POP    DE
  911.     POP    HL
  912.     RET
  913. ;
  914. ;    Check if character in A is alphabetic.  Z status means YES,
  915. ;    NZ status means NO
  916. ;
  917. ISALPHA    CP    'A'        ;Check upper case
  918.     RET    C        ;Return if less than
  919.     CP    'Z'+1        ;If less than or equal to Z, then A-Z
  920.     JR    C,ISAL_1    ;Jump to TRUE return
  921.     CP    'a'        ;Check lower case
  922.     RET    C        ;Return less than
  923.     CP    'z'+1        ;Check for a-z
  924.     RET    NC        ;NC means greater than 'z'
  925. ISAL_1    CP    A        ;Set Z status
  926.     RET            ;Return
  927. ;
  928. ;    Check if character in A is alphanumeric
  929. ;
  930. ISALNUM    CP    '0'        ;Check digits
  931.     RET    C        ;Return too small
  932.     CP    '9'+1        ;Check max+1
  933.     JR    NC,ISALPHA    ;If too big, go try alphabetic
  934.     CP    A        ;Set Z status
  935.     RET            ;return it
  936. ; end of file
  937.