home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / trs80model2.zip / trsrecv.mac < prev    next >
Text File  |  1997-03-26  |  12KB  |  287 lines

  1.         title   krecv/mac reception unit
  2.         cseg
  3.         ;
  4.         ;
  5.         extrn   recptr,recbuf,rplus,mjump,rfjump,rdjump
  6.         extrn   spaket,rpaket,screen
  7.         extrn   rplus,sinit,state,byte,n,r
  8.         extrn   rpack,spack,abort,acsum,flush
  9.         extrn   fcb,writnx,open,rinit,close
  10.         extrn   lrecl
  11.         public  r_init,r_file,rf_b,rf_x,rf_f
  12.         public  r_data,rd_z,rd_d
  13.         ;
  14.         len     equ     0
  15.         seq     equ     1
  16.         type    equ     2
  17.         data    equ     3
  18.         quote   equ     '#'
  19.         _a      equ     1
  20.         _c      equ     2
  21.         _r      equ     3
  22.         _rf     equ     4
  23.         _rd     equ     5
  24.         ;
  25.         ;
  26.         subttl  macros used in this module
  27.         ;
  28.         ;prmes  to display messages
  29.         ;
  30.         prmes   macro   lab
  31.         .xlist
  32.         extrn   m_&lab,l_&lab
  33.         push    hl
  34.         push    bc
  35.         ld      hl,m_&lab
  36.         ld      bc,(l_&lab)
  37.         ld      c,13
  38.         ld      a,9
  39.         rst     8
  40.         pop     bc
  41.         pop     hl
  42.         .list
  43.         endm
  44.         ;movb
  45.         ;
  46.         movb    macro   value,loc
  47.         .xlist
  48.         push    af
  49.         ld      a,value
  50.         ld      (loc),a
  51.         pop     af
  52.         .list
  53.         endm
  54.         ;
  55.         ;blmov
  56.         ;
  57.         blmov   macro   source,dest,len
  58.         .xlist
  59.         local   $1,$2
  60.         push    hl
  61.         push    bc
  62.         push    de
  63.         ld      hl,source
  64.         ld      de,dest
  65.         ld      a,(len)
  66.         cp      0
  67.         jr      nz,$1
  68.         ld      b,1
  69.         ld      c,0
  70.         jp      $2
  71.         $1:
  72.         ld      b,0
  73.         ld      c,a
  74.         $2:
  75.         ldir
  76.         pop     de
  77.         pop     bc
  78.         pop     hl
  79.         .list
  80.         endm
  81.         ;
  82.         ;fack   to format an ack paket
  83.         ;
  84.         f_ack   macro
  85.         .xlist
  86.         ld      (iy+len),3
  87.         ld      a,(n)
  88.         add     a,' '
  89.         ld      (iy+seq),a
  90.         ld      (iy+type),'Y'
  91.         ld      hl,spaket
  92.         call    acsum
  93.         .list
  94.         endm
  95.         ;
  96.         ;nplus
  97.         ;
  98.         nplus   macro
  99.         .xlist
  100.         ld      hl,n
  101.         inc     (hl)
  102.         res     6,(hl)          ;not over 63
  103.         .list
  104.         endm
  105.         ;
  106.         subttl  receive initialize
  107.          ;
  108.         ;       receive init
  109.         ;
  110.         r_init:
  111.                 movb    0,n             ;set packet count to 0
  112.                 movb    0,r             ;and retry count to 0
  113.                 ld      ix,rpaket       ;ix will always point there
  114.                 call    flush           ;flush comm port
  115.                 call    rpack           ;and get a packet
  116.                 jp      c,rplus         ;no good, nack, r+
  117.                 ld      a,(ix+type)     ;get packet type
  118.                 cp      'S'             ;is it a send ?
  119.                 jp      nz,abort        ;nope, no good
  120.                 movb    10,byte         ;will move 10 bytes
  121.                 blmov   rpaket+data,sinit,byte
  122.                                         ;to send init buffer
  123.                 ld      hl,sinit+4      ;address of eol
  124.                 res     5,(hl)          ;sub 32 to get real eol
  125.                                         ;and prepare to ack
  126.                                         ;with our parameters
  127.                 ld      iy,spaket       ;iy will always point there
  128.                 ld      (iy+len),12     ;length
  129.                 ld      (iy+type),'Y'   ;ack
  130.                 ld      a,(n)           ;current packet number
  131.                 add     a,32            ;make printable
  132.                 ld      (iy+seq),a      ;save in ack packet
  133.                 blmov   rinit,spaket+data,byte
  134.                                         ;all the info
  135.                 ld      hl,spaket       ;hl points to send packet
  136.                 call    acsum           ;add checksum
  137.                 call    spack           ;and pray it gets there
  138.                 nplus                   ;increment n
  139.                 movb    0,r             ;set retry count to 0
  140.                 movb    _rf,state       ;to receive file
  141.                 jp      mjump           ;back
  142.         subttl  receive file
  143.         page
  144.         ;
  145.         ;       receive file
  146.         ;
  147.         r_file:
  148.                 call    rpack           ;get a packet
  149.                 jp      c,rplus         ;no good
  150.                 ld      a,(n)           ;packet number expected
  151.                 add     a,' '           ;make printable
  152.                 cp      (ix+seq)        ;equal to received packet
  153.                 jp      z,rfgood        ;yes
  154.                 call    spack           ;re-ack, it was lost
  155.                 jp      rplus           ;increment r, nak
  156.         rfgood:
  157.                 jp      rfjump
  158.                                         ;jump according to table
  159.         rf_b:
  160.                 ;case(break)
  161.                 f_ack                   ;format ack
  162.                 call    spack           ;and send it
  163.                 nplus
  164.                 movb    _c,state        ;set state to complete
  165.                 jp      mjump           ;and back
  166.          rf_x:
  167.                 ;case(type on screen)
  168.                 movb    1,screen        ;set flag on
  169.                 movb    _rd,state       ;set state to receive data
  170.                 f_ack                   ;format ack
  171.                 call    spack           ;and send it
  172.                 nplus                   ;increment packet count
  173.                 jp      mjump           ;and back
  174.          rf_f:
  175.                 ;case(file header)
  176.                 ld      a,(ix+len)      ;get lenght
  177.                 sub     ' '+3           ;minus seq,type, chksum
  178.                 ld      (ix+len),a      ;store back
  179.                 blmov   rpaket+data,fcb,rpaket
  180.                                         ;move filename to fcb
  181.                 ld      hl,fcb          ;start of filename
  182.                 ld      c,a             ;length
  183.                 ld      b,0             ;bc = length
  184.                 ld      a,'.'           ;to scan for dot
  185.                 cpir                    ;found dot
  186.                 dec     hl              ;adjust pointer
  187.                 ld      (hl),'/'        ;replace by '/'
  188.                 ld      a,0             ;clr a
  189.                 cp      c               ;c = 0 ?
  190.                 jp      z,r_f0          ;yes, put in cr
  191.                 ld      hl,fcb          ;first byte of filename
  192.                 ld      a,(rpaket)      ;length of filename
  193.                 add     a,l             ;add low byte to length
  194.                 ld      l,a             ;store back low byte
  195.                 ld      a,0             ;clear a
  196.                 adc     a,h             ;add high byte to carry
  197.                 ld      h,a             ;put back in h
  198.         r_f0:   ld      (hl),13         ;put in a carriage return
  199.                 call    open            ;and open file
  200.                 f_ack                   ;format an ack
  201.                 call    spack           ;and send it
  202.                 nplus                   ;increment packet count
  203.                 movb    _rd,state       ;set state to receive data
  204.                 jp      mjump           ;and back
  205.          subttl  receive data
  206.         page
  207.         ;
  208.         ;       receive data
  209.         ;
  210.         r_data:
  211.                 call    rpack           ;get a packet
  212.                 jp      c,rplus         ;no good
  213.                 ld      a,(n)           ;get expected packet count
  214.                 add     a,' '           ;make printable
  215.                 cp      (ix+seq)        ;equal to received ?
  216.                 jp      z,rdgood        ;yes, all ok
  217.                 call    spack           ;re-ack, it was lost
  218.                 jp      rplus           ;update retry count
  219.          rdgood:
  220.                 jp      rdjump
  221.          rd_z:
  222.                 ;case(end of file)
  223.                 call    writnx          ;flush buffer
  224.                 call    close           ;close file
  225.                 f_ack                   ;format an ack
  226.                 call    spack           ;and send it
  227.                 nplus                   ;increment packet count
  228.                 movb    _rf,state       ;set state to receive file
  229.                 jp      mjump           ;and back
  230.          rd_d:
  231.                 ;case(data)
  232.                 ld      hl,rpaket+data  ;start of data
  233.                 ld      a,(rpaket)      ;total length
  234.                 sub     ' '+3           ;convert to numeric
  235.                 cp      0               ;is it null ?
  236.                 jp      z,rd_d2         ;yes, finish
  237.                 ld      bc,(recptr)     ;pointer inside recbuf
  238.                 ld      b,0             ;turn off high byte
  239.                 push    hl              ;save temporarily
  240.                 ld      hl,recbuf       ;record address
  241.                 add     hl,bc           ;plus length
  242.                 ex      de,hl           ;pointer in de
  243.                 pop     hl              ;restore hl
  244.                 ;at this point :
  245.                 ;       hl = rpaket
  246.                 ;       de = inside recbuf
  247.                 ;       a = length of packet
  248.         rd_d1:
  249.                 push    af              ;save temporarily
  250.                 ld      a,(hl)          ;get current byte
  251.                 cp      quote           ;is it a quote ?
  252.                 jr      nz,rd_d3        ;no, go on
  253.                 inc     hl              ;point to next byte
  254.                 pop     af              ;restore a
  255.                 dec     a               ;decrement counter
  256.                 push    af              ;and save again
  257.                 ld      a,(hl)          ;get next byte
  258.                 cp      quote           ;is it a quote ?
  259.                 jr      z,rd_d3         ;yes, don't touch
  260.                 cp      quote or 128    ;quote and eight bit
  261.                 jr      z,rd_d3         ;yes don't touch either
  262.                 xor     64              ;uncontrollify
  263.                 ld      (hl),a          ;store back
  264.         rd_d3:  pop     af              ;restore
  265.                 ldi                     ;from rapket to recbuf
  266.                 dec     a               ;paket length minus one
  267.                 ld      bc,(recptr)     ;pointer inside recbuf
  268.                 inc     c               ;is incremented
  269.                 movb    c,recptr        ;and stored back
  270.                 push    af              ;save a
  271.                 ld      a,(lrecl)       ;get logical record length
  272.                 cp      c               ;compare to len(recbuf)
  273.                 jp      nz,rd_d0        ;no, do not update yet
  274.                 call    writnx          ;write next record
  275.                 movb    0,recptr        ;set pointer back to zero
  276.                 ld      de,recbuf       ;reset pointer to record buffer
  277.         rd_d0:
  278.                 pop     af              ;restore a
  279.                 cp      0               ;is packet empty ?
  280.                 jp      nz,rd_d1        ;no, get one more byte
  281.         rd_d2:
  282.                 f_ack                   ;format an ack
  283.                 call    spack           ;and send it
  284.                 nplus                   ;update packet counter
  285.                 jp      mjump           ;and back
  286.                 end
  287.