home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / trs80model4.zip / m4pkt.asm < prev    next >
Assembly Source File  |  1986-10-22  |  11KB  |  405 lines

  1. ;    m4pkt/asm
  2. ;
  3. ;    Most of the code in this file was derived directly from
  4. ;    the C-KERMIT program.  The code here represents a line by
  5. ;    line translation of the C-KERMIT code.
  6. ;
  7. MEMSTR    DW    0
  8. MAXSIZE    DB    0
  9. ;
  10. ;    Encode a packet from memory contents instead of from a file
  11. ;
  12. ENCSTR    EQU    $
  13.     PUSH    HL        ;Save the registers
  14.     PUSH    BC
  15.     PUSH    DE
  16.     LD    (MEMSTR),HL    ;Store the passed pointer
  17.     CALL    RSETPKT        ;Reinitiaize GETPKT() parameters
  18.     CALL    GETPKT        ;Get a packet
  19.     LD    HL,0        ;Reset the memory pointer
  20.     LD    (MEMSTR),HL
  21.     POP    DE        ;Restore the registers
  22.     POP    BC
  23.     POP    HL
  24.     RET            ;Return to the caller
  25. ;
  26. ;    Get a packet worth of data from the FILE indicated by the global
  27. ;    FCB.  Eighth bit quoting, and Repeat count prefixing are done
  28. ;    as indicated by the status of EBQFLAG, and RPTFLG respectively.
  29. ;
  30. GETPKT    EQU    $
  31.     PUSH    BC        ;Save the registers
  32.     PUSH    DE
  33.     PUSH    HL
  34.     LD    A,(CURCHK)    ;Calculate the size of a packet
  35.     SUB    '1'
  36.     LD    B,A
  37.     LD    A,(SPSIZ)    ;Get the user specified size
  38.     SUB    5        ;Minus the overhead
  39.     SUB    B
  40.     LD    (MAXSIZE),A    ;Save it for later use
  41.     LD    HL,(NEXT)    ;Get the next character, 16 bits
  42.     LD    BC,0        ;Subtract zero to set flags
  43.     OR    A        ;Reset the carry flag
  44.     SBC    HL,BC        ;Set the flags
  45.     JP    P,GETPKT1
  46.     CALL    GETCH        ;Get a character if no previous exists
  47.     LD    (CH),HL        ;Save it
  48. GETPKT1    EQU    $
  49.     LD    BC,0        ;Copy leftovers from previous call
  50. GETPKT2    EQU    $
  51.     LD    HL,LEFTOVER    ;Get the start
  52.     ADD    HL,BC        ;Index by BC
  53.     LD    A,(HL)        ;Get a byte
  54.     LD    HL,FILBUF    ;Get the destination
  55.     ADD    HL,BC        ;Index by BC
  56.     LD    (HL),A        ;Put a byte
  57.     IFZ    GETPKT3        ;Exit loop if zero byte found
  58.     INC    C        ;Point to next
  59.     JR    GETPKT2        ;Loop on
  60. GETPKT3    EQU    $
  61.     LD    A,C        ;Save the initial size of leftovers
  62.     LD    (SIZE),A
  63.     XOR    A        ;Nullify the string for next time
  64.     LD    (LEFTOVER),A
  65. GETPKT4    EQU    $
  66.     LD    HL,(CH)        ;Get the character
  67.     LD    BC,0        ;Subtract zero to set the flags
  68.     OR    A
  69.     SBC    HL,BC
  70.     JP    M,GETPKT20    ;Jump at end of file
  71.     CALL    GETCH        ;Get the next character
  72.     LD    (NEXT),HL    ;Save it
  73.     LD    A,(SIZE)    ;Get the current size
  74.     LD    (OSIZE),A    ;Save it in case we overflow the packet
  75.     LD    C,A        ;Make BC a 16 bit value of C
  76.     LD    B,0
  77.     LD    HL,FILBUF    ;Get the desination buffer
  78.     ADD    HL,BC        ;Index by BC
  79.     LD    (CBFPTR),HL    ;Save the buffer pointer
  80.     LD    HL,(CH)        ;Get the character
  81.     LD    A,L        ;Keep only 8 bits
  82.     CALL    ENCODE        ;Encode it and store the result
  83.     LD    HL,(CBFPTR)    ;Compute the new size
  84.     LD    BC,FILBUF    ;Get the start
  85.     OR    A        ;Reset carry
  86.     SBC    HL,BC        ;Compute lenght
  87.     LD    A,L        ;Keep only 8 bits
  88.     LD    (SIZE),A    ;Store the result
  89.     LD    HL,(NEXT)    ;Get the next character
  90.     LD    (CH),HL        ;Store it as the current character
  91.     LD    A,(SIZE)    ;Get the size and check the size
  92.     LD    C,A
  93.     LD    A,(MAXSIZE)    ;Get the maximum length
  94.     CP    C        ;Are they equal?
  95.     JP    Z,GETPKT20    ;Go return SIZE if equal
  96.     JP    P,GETPKT13    ;Jump if maxsize still bigger
  97.     LD    BC,0        ;Start from the beginning
  98. GETPKT11    EQU    $
  99.     LD    A,(OSIZE)    ;Get the oldsize index
  100.     PUSH    BC        ;Save the value of i
  101.     ADD    A,C        ;Compute osize+i
  102.     LD    C,A        ;Save the result
  103.     LD    HL,FILBUF    ;Get the start of the buffer
  104.     ADD    HL,BC        ;Compute &filbuf[osize+i]
  105.     LD    A,(HL)        ;Get the character stored there
  106.     POP    BC        ;Restore i
  107.     LD    HL,LEFTOVER    ;Get the address of the leftover buffer
  108.     ADD    HL,BC        ;Compute the absolute address
  109.     LD    (HL),A        ;Store the byte
  110.     OR    A        ;Check the value for zero
  111.     JR    Z,GETPKT12    ;Jump if end of string
  112.     INC    C        ;Compute i++
  113.     JR    GETPKT11    ;Go to the top of the loop
  114. GETPKT12    EQU    $
  115.     LD    A,(OSIZE)    ;Store the new pointer
  116.     LD    (SIZE),A
  117.     LD    C,A        ;Terminate the string with a NULL
  118.     LD    B,0        ;Get a 16 bit offset
  119.     LD    HL,FILBUF    ;Get the starting address
  120.     ADD    HL,BC        ;Compute the address
  121.     LD    (HL),0        ;Put in the NULL
  122.     JP    GETPKT20    ;Go to the return code
  123. GETPKT13    EQU    $
  124.     JP    GETPKT4
  125. GETPKT20    EQU    $
  126.     LD    A,(SIZE)    ;Get the size to return
  127.     POP    HL        ;Restore the registers
  128.     POP    DE
  129.     POP    BC
  130.     RET
  131. ;
  132. ;    Encode the character in the Accumlator and put it into
  133. ;    the packet buffer at CBFPTR.
  134. ;
  135. ENCODE    EQU    $
  136.     PUSH    BC        ;Save the regs
  137.     PUSH    DE
  138.     PUSH    HL
  139.     LD    C,A        ;Save the character to encode
  140.     LD    A,(RPTFLG)    ;Is repeat quoting in effect?
  141.     OR    A
  142.     JP    Z,ENCODE30
  143.     LD    HL,(NEXT)    ;Get the last character
  144.     LD    A,L
  145.     IFANOT    C,ENCODE3
  146.     LD    A,(RPTCNT)    ;Get the current repeat count
  147.     INC    A        ;Add one to it
  148.     LD    (RPTCNT),A    ;Store it back
  149.     CP    94        ;Too many characters?
  150.     JP    M,ENCODE50
  151.     LD    A,(RPTQ)
  152.     CALL    PUTCBF
  153.     LD    A,(RPTCNT)
  154.     TOCHAR
  155.     CALL    PUTCBF
  156.     XOR    A
  157.     LD    (RPTCNT),A
  158.     JP    ENCODE30
  159. ENCODE3    EQU    $
  160.     LD    A,(RPTCNT)
  161.     IFANOT    1,ENCODE7
  162.     XOR    A
  163.     LD    (RPTCNT),A
  164.     LD    A,C        ;Get the character back
  165.     CALL    ENCODE        ;Encode it again
  166.     LD    HL,(CBFPTR)    ;Get the buffer pointer
  167.     LD    DE,FILBUF    ;Get the start of the buffer
  168.     OR    A        ;Reset the carry
  169.     SBC    HL,DE        ;Compute the difference    in L
  170.     LD    A,(MAXSIZE)    ;Get MAXSIZE
  171.     CP    L        ;Is L <= A?
  172.     JP    M,ENCODE6
  173.     LD    A,L        ;Get SIZE
  174.     LD    (OSIZE),A    ;Store it in the old size
  175. ENCODE6    EQU    $
  176.     XOR    A
  177.     LD    (RPTCNT),A
  178.     LD    A,C        ;Get the character back
  179.     CALL    ENCODE        ;Encode it
  180.     JP    ENCODE50
  181. ENCODE7    EQU    $
  182.     LD    A,(RPTCNT)
  183.     CP    2
  184.     JP    M,ENCODE10
  185.     LD    A,(RPTQ)
  186.     CALL    PUTCBF
  187.     LD    A,(RPTCNT)    ;Get the count
  188.     ADD    A,' '+1        ;Uncontrolify it
  189.     CALL    PUTCBF
  190.     XOR    A
  191.     LD    (RPTCNT),A
  192. ENCODE10    EQU    $
  193. ENCODE30    EQU    $
  194.     LD    A,C        ;Get the character
  195.     AND    127        ;Turn off high bit
  196.     LD    B,A        ;Save it
  197.     LD    A,C        ;Get the character
  198.     AND    128        ;Leave only high bit
  199.     LD    D,A        ;Save it
  200.     IFZ    ENCODE32
  201.     LD    A,(EBQFLG)
  202.     IFZ    ENCODE32
  203.     LD    A,(EBQ)
  204.     CALL    PUTCBF
  205.     LD    C,B        ;Save only 7 bit version
  206. ENCODE32    EQU    $
  207.     LD    A,C        ;Get all bits for check
  208.     IFA    127,ENCODE33
  209.     IFAGE    ' ',ENCODE34
  210. ENCODE33    EQU    $
  211.     LD    A,(MYCTLQ)
  212.     CALL    PUTCBF
  213.     LD    A,C        ;Get the character
  214.     ADD    A,64        ;Uncontrolify it
  215.     AND    127        ;Keep only 7 bits
  216.     LD    C,A        ;Save the new value
  217. ENCODE34    EQU    $
  218.     LD    HL,(CBFPTR)    ;Do this ahead to save overhead
  219.     LD    A,(MYCTLQ)
  220.     IFANOT    B,ENCODE35
  221.     PUTHL    A        ;Store the byte
  222. ENCODE35    EQU    $
  223.     LD    A,(RPTFLG)
  224.     IFZ    ENCODE36
  225.     LD    A,(RPTQ)
  226.     IFANOT    B,ENCODE36
  227.     LD    A,(MYCTLQ)
  228.     PUTHL    A
  229. ENCODE36    EQU    $
  230.     LD    A,(EBQFLG)
  231.     IFZ    ENCODE37
  232.     LD    A,(EBQ)
  233.     IFANOT    B,ENCODE37
  234.     LD    A,(MYCTLQ)
  235.     PUTHL    A
  236. ENCODE37    EQU    $
  237.     PUTHL    C
  238.     LD    (CBFPTR),HL
  239.     LD    (HL),0
  240. ENCODE50    EQU    $
  241.     POP    HL        ;Restore the stack
  242.     POP    DE
  243.     POP    BC
  244.     RET
  245. ;
  246. ;    Decode the contents of the packet, and output them using
  247. ;    the function whose address is passed in HL
  248. ;
  249. DECODE    LD    (OUTADDR),HL    ;Save the output function
  250.     PUSH    BC        ;Save the registers that we use
  251.     PUSH    DE
  252.     PUSH    HL
  253.     LD    HL,DATA        ;Get the buffer address
  254.     LD    (DATPTR),HL    ;Set the address
  255.     LD    C,A        ;Get the length
  256.     LD    B,0        ;Make it 16 bits
  257.     PUSH    HL        ;Save DATPTR
  258.     ADD    HL,BC        ;Compute the end of the packet
  259.     LD    (HL),0        ;Put in the NULL byte
  260.     POP    HL        ;Get DATPTR back
  261. DECODE0    LD    A,1        ;Set the repeat count
  262.     LD    (RPTCNT),A
  263.     LD    A,(HL)        ;Get a character
  264.     INC    HL        ;Point to the next
  265.     LD    C,A        ;Store the value
  266.     OR    A        ;Is it NULL?
  267.     JP    Z,DECODE40    ;Quit the loop if it is
  268.     LD    A,(RPTFLG)    ;Is repeat prefixing on?
  269.     IFZ    DECODE1
  270.     LD    A,(RPTQ)    ;Get the quote character
  271.     IFANOT    C,DECODE1
  272.     LD    A,(HL)        ;Get the count + 32
  273.     SUB    ' '        ;Make it the real count
  274.     INC    HL        ;Point to next character
  275.     LD    (RPTCNT),A    ;Save the repeat count
  276.     LD    A,(HL)        ;Get a character
  277.     INC    HL        ;Point to the next
  278.     LD    C,A        ;Save the character
  279. DECODE1    XOR    A        ;Set the initial eighth bit as ZERO
  280.     LD    D,A
  281.     LD    A,(EBQFLG)    ;Is eighth bit quoting on?
  282.     IFZ    DECODE2
  283.     LD    A,(EBQ)        ;Get the eighth bit quote character
  284.     IFANOT    C,DECODE2
  285.     LD    D,128        ;Get an eighth bit
  286.     LD    A,(HL)        ;Get a character
  287.     INC    HL        ;Point to the next
  288.     LD    C,A        ;Save the character
  289. DECODE2    LD    A,(MYCTLQ)    ;Get the control character quote
  290.     IFANOT    C,DECODE5
  291.     LD    A,(HL)        ;Get a character
  292.     INC    HL        ;Point to the next
  293.     LD    C,A        ;Save the character
  294.     AND    127        ;Keep 7 bits
  295.     LD    E,A        ;Save only 7 bits worth
  296.     LD    A,E        ;Check lower bound of controls
  297.     CP    '@'
  298.     JP    M,DECODE3    ;Jump if less than
  299.     CP    96        ;Check upper bound of controls
  300.     JP    M,DECODE4
  301. DECODE3    IFANOT    '?',DECODE5
  302. DECODE4    LD    A,C        ;Get the character
  303.     SUB    64        ;Subtract 64 to uncontrolify it
  304.     AND    127
  305.     LD    C,A        ;Save the new value
  306. DECODE5    LD    A,D        ;Get the eighth bit
  307.     OR    C        ;Or in the character
  308.     LD    C,A        ;Save the new value
  309.     LD    A,(RPTCNT)    ;Get the repeat count
  310.     LD    B,A        ;Get it into the counter
  311. DECODE8    LD    IX,RTRANS    ;Get the pointer
  312.     CALL    INCKTRANS    ;Increment character counts
  313.     LD    A,C        ;Get the character
  314.     CALL    PUTCH
  315.     JR    NZ,DECODE50
  316.     DJNZ    DECODE8        ;Loop until done
  317.     JP    DECODE0
  318. DECODE40    EQU    $
  319.     POP    HL        ;Restore the stack
  320.     POP    DE
  321.     POP    BC
  322.     RET
  323. DECODE50    EQU    $
  324.     CALL    XERROR0        ;Print a system error message
  325.     LD    A,1        ;Set NZ status for return
  326.     OR    A
  327.     JR    DECODE40    ;Join the exit code
  328. ;
  329. ;    Get the next character from the input file.  We translate CR to
  330. ;    CRLF, for ASCII files.  Returns with (EOFLAG) set to 0FFH at end
  331. ;    of file.  The character retrieved is in HL.  HL will be 16bits
  332. ;    of -1 and EOF, but will otherwise only be 8bits wide.  H in this
  333. ;    case will always be 0, and L will hold the character.
  334. ;
  335. GETCH    LD    A,(FILTYPE)    ;Check for binary mode
  336.     OR    A        ;Set the flags
  337.     JR    NZ,GETCH1    ;Jump if it is binary
  338.     LD    A,(PREVCH)    ;Get the previous character
  339.     IFANOT    CR,GETCH1
  340.     LD    A,10        ;Get a LF character
  341.     LD    (PREVCH),A    ;Reset the previous character
  342.     LD    L,A        ;Put it into HL
  343.     LD    H,0
  344.     RET            ;Return to the caller
  345. GETCH1    EQU    $
  346.     LD    HL,(MEMSTR)
  347.     LD    A,H
  348.     OR    L
  349.     JR    Z,GETCH2
  350.     LD    A,(HL)
  351.     INC    HL
  352.     LD    (MEMSTR),HL
  353.     IFZ    GETCH4
  354.     JR    GETCH3
  355. GETCH2    EQU    $
  356.     LD    DE,FCB        ;Get the File Control Block
  357.     CALL    XGET        ;Get a character into A
  358.     JR    NZ,GETCH4    ;Jump to EOF on error
  359.     LD    IX,STRANS    ;Get the pointer
  360.     CALL    INCKTRANS    ;Increment the character count
  361. GETCH3    EQU    $
  362.     LD    (PREVCH),A    ;Set the previous character
  363.     LD    L,A        ;Put it into HL
  364.     LD    H,0
  365.     RET            ;Return
  366. GETCH4    EQU    $
  367.     LD    A,0FFH        ;Set the EOF flag
  368.     LD    (EOFLAG),A
  369.     LD    (PREVCH),A    ;Reset the previous character flag
  370.     LD    L,A        ;Make HL 16bits wide -1
  371.     LD    H,A
  372.     RET            ;Return to caller
  373. ;
  374. ;    Put the next character to the output file.  We convert CRLF to CR
  375. ;    IFF X packets are not active, and binary mode is not active
  376. ;
  377. PUTCH    PUSH    HL
  378.     PUSH    BC        ;Save the register
  379.     LD    C,A        ;Save the character to output
  380.     LD    A,(PREVCH)    ;Get the previous character
  381.     IFANOT    CR,PUTCH1
  382.     LD    A,(DISFLG)    ;Check if we are doing X packets
  383.     IFZ    PUTCH1
  384.     LD    A,(FILTYPE)    ;Doing D packets, so check file mode
  385.     OR    A
  386.     JR    NZ,PUTCH1
  387.     LD    A,C
  388.     IFA    10,PUTCH2
  389. PUTCH1    LD    A,C        ;Get the character to output
  390.     LD    (PREVCH),A    ;Make it the new previous
  391.     LD    HL,(OUTADDR)    ;Get the output routines address
  392.     CALL    CALLHL        ;Call the output routine
  393. PUTCH2    POP    BC        ;Restore the stack
  394.     POP    HL
  395.     RET            ;Return to caller
  396. ;
  397. ;    Put A into the location pointed to by CBFPTR, and increment the
  398. ;    pointer to point to the next location
  399. ;
  400. PUTCBF    LD    HL,(CBFPTR)    ;Get the pointer
  401.     PUTHL    A
  402.     LD    (CBFPTR),HL    ;Store the new pointer value
  403.     RET            ;Return
  404. ;end of file
  405.