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

  1.         subttl  kutil/mac utilities and other odd routines
  2.         extrn   rlen,slen,csvc,rsvc,ssvc,r,n,mjump
  3.         extrn   spaket,rpaket,byte,recptr,sinit
  4.         extrn   fcb,lrecl,filbuf,recbuf,lrecl,paraml
  5.         public  flush,rplus,abort,exit,acsum,spack
  6.         public  open,close,writnx,readnx,kill
  7.         public  rpack,initcm
  8.         extrn   init,port,altsvc,nsvc
  9.         ;
  10.         ;       useful symbole
  11.         ;
  12.         soh     equ     1
  13.         tout    equ     10
  14.         len     equ     0
  15.         seq     equ     1
  16.         type    equ     2
  17.         data    equ     3
  18.         dfport  equ     'A'
  19.         ;
  20.         ;
  21.         ;timer  to interrupt a given routine after a number of seconds
  22.         ;       syntax  timer   routin,seconds
  23.         ;               where   routin is the interrupt handler
  24.         ;
  25.         timer   macro   routin,second
  26.         push    hl
  27.         push    bc
  28.         ld      hl,routin       ;routine to jump to
  29.         ld      bc,second       ;number of seconds
  30.         svc     25              ;timer call
  31.         pop     bc
  32.         pop     hl
  33.         endm
  34.         ;
  35.         ;svc    to make a trsdos supervisor call
  36.         ;       syntax  svc code
  37.         ;       where   code is the trsdos code
  38.         ;
  39.         svc     macro   code
  40.         ld      a,code
  41.         rst     8
  42.         endm
  43.         ;
  44.         ;
  45.         ;prmes  to print messages on the screen
  46.         ;       syntax  prmes  lab
  47.         ;       where   lab if the label as defined with mssg
  48.         ;
  49.         prmes   macro   lab
  50.         .xlist
  51.         extrn   m_&lab,l_&lab
  52.         push    hl
  53.         push    bc
  54.         ld      hl,m_&lab       ;get address of message
  55.         ld      bc,(l_&lab)     ;and length
  56.         ld      c,13            ;add a CR at end of ttyout
  57.         svc     9               ;call dos
  58.         pop     bc
  59.         pop     hl
  60.         .list
  61.         endm
  62.         ;
  63.         ;blmov  to move a block of text
  64.         ;       syntax  blmov source,destination,length
  65.         ;               if length is 0 then assume 256
  66.         ;
  67.         blmov   macro   source,dest,len
  68.         .xlist
  69.         local   $1,$2
  70.         push    hl
  71.         push    bc
  72.         push    de
  73.         ld      hl,source       ;address of source
  74.         ld      de,dest         ;address of destination
  75.         ld      a,(len)         ;get length
  76.         cp      0               ;is it zero ?
  77.         jr      nz,$1
  78.         ld      b,1             ;then set bc = 256
  79.         ld      c,0             ;(b=1 ; c=0)
  80.         jp      $2              ;go to start move
  81.         $1:
  82.         ld      b,0
  83.         ld      c,a             ;bc = length
  84.         $2:
  85.         ldir                    ;move and check if bc=0
  86.         pop     de
  87.         pop     bc
  88.         pop     hl
  89.         .list
  90.         endm
  91.         ;
  92.         ;readnx to read next record sequentially
  93.         ;       Returs with the record in recbuf
  94.         ;       And, at eof, will jump to sendeof
  95.         ;       (This macro will not save redisters)
  96.         ;
  97.         readnx:
  98.         ld      de,fcb          ;file control block
  99.         svc     34              ;read next svc
  100.         jp      nz,abort        ;bad read, abort
  101.         ld      a,(lrecl)       ;get logacal record length
  102.         cp      0               ;is it 256 ?
  103.         jp      nz,read0        ;no, all is ok
  104.         blmov   filbuf,recbuf,lrecl     ;move to recbuf
  105.         read0:
  106.         ret
  107.         ;
  108.         ;open   open a file according to fcb and paramlist
  109.         ;
  110.         open:
  111.         push    hl
  112.         push    de
  113.         ld      de,fcb          ;file control block
  114.         ld      hl,paraml       ;parameter list
  115.         svc     40              ;open call
  116.         jp      nz,abort        ;file not found
  117.                                 ;or file cannot create
  118.         pop     de
  119.         pop     hl
  120.         ret
  121.         ;
  122.         ;kill kill a file using current fcb
  123.         ;
  124.         kill:
  125.         push    de
  126.         ld      de,fcb          ;file control block
  127.         svc     41              ;kill call
  128.         jp      nz,abort        ;no good (password ?)
  129.         pop     de
  130.         ret
  131.         ;
  132.         ;close  file using current fcb
  133.         ;
  134.         close:
  135.         push    de
  136.         ld      de,fcb
  137.         svc     42
  138.         jp      nz,abort
  139.         xor     a               ;clr a
  140.         ld      (recptr),a      ;reset pointer to 0
  141.         pop     de
  142.         ret
  143.         ;
  144.         ;writnx write next sequential record
  145.         ;
  146.         writnx:
  147.         ld      a,(lrecl)       ;get logical record length
  148.         cp      0               ;is it 256 ?
  149.         jp      nz,writ0        ;no, go on
  150.         blmov   recbuf,filbuf,lrecl     ;get to filbuf
  151.         writ0:
  152.         push    de
  153.         ld      de,fcb          ;file control block
  154.         svc     43              ;write call
  155.         jp      nz,abort        ;no good
  156.         pop     de
  157.         ret
  158.         ;
  159.         ;delay  in seconds
  160.         ;
  161.         delay   macro   sec
  162.         .xlist
  163.         local   $1
  164.         push    bc
  165.         ld      bc,0            ;set for 426 milisecs
  166.         push    hl
  167.         ld      l,sec           ;number of seconds
  168.         $1:
  169.         svc     6               ;call for delay
  170.         svc     6               ;2 * 426 milisecs = 1 s.
  171.         dec     l               ;sec--
  172.         xor     a               ;a = 0
  173.         cp      l               ;sec = 0 ?
  174.         jr      nz,$1           ;no, play it again sam
  175.         pop     hl
  176.         pop     de
  177.         .list
  178.         endm
  179.         ;
  180.         ;jumptb jump according to a given table and a one byte code
  181.         ;
  182.         ;       syntax jumptb   table,code
  183.         ;
  184.         jumptb  macro   table,code
  185.         .xlist
  186.         local   $1
  187.         ld      hl,table        ;get jump table address
  188.         ld      bc,(code)       ;and code (note that c is messed up)
  189.         ld      a,c
  190.         ld      b,a
  191.         svc     28              ;lookup call
  192.         jr      z,$1            ;found
  193.         ld      hl,table+1      ;get abort address
  194.         $1:
  195.         jp      (hl)            ;bye ...
  196.         .list
  197.         endm
  198.         ;
  199.         ;initcm initalise comm channel A or B
  200.         ;       and set up correct svc communication calls
  201.         ;
  202.         initcm:
  203.         ld      a,(init)        ;get initial code
  204.         cp      0               ;should we init ?
  205.         jr      z,i1            ;no, go set up svc
  206.         ;
  207.         ld      hl,port         ;get port paramlist
  208.         ld      b,0             ;turn off port
  209.         svc     55              ;dos call
  210.         ld      b,1             ;turn on
  211.         svc     55              ;dos call
  212.         i1:
  213.         ld      a,(port)        ;get channel A or B
  214.         cp      dfport          ;is this default ?
  215.         jr      z,i2            ;yes, all ok
  216.         blmov   altsvc,init,nsvc;set up alternate svc's
  217.         i2:
  218.         ret
  219.         ;
  220.         ;xmitb  transmit a byte that is pointed to by hl
  221.         ;
  222.         xmitb   macro
  223.         .xlist
  224.         local   $1
  225.         $1:
  226.         ld      a,(ssvc)        ;get transmit svc
  227.         ld      b,(hl)          ;and byte to transmit
  228.         rst     8               ;dos call
  229.         jr      nz,$1           ;assume busy, try again
  230.         .list
  231.         endm
  232.         ;
  233.         ;rcvb  receive byte and return it in a
  234.         ;
  235.         rcvb    macro
  236.         .xlist
  237.         local   $1
  238.         push    bc
  239.         $1:
  240.         ld      a,(rsvc)        ;get receive svc
  241.         rst     8               ;dos call
  242.         jr      nz,$1           ;try it again
  243.         ld      a,b             ;store (might not be good)
  244.         pop     bc
  245.         .list
  246.         endm
  247.  
  248.         ;
  249.         ;nplus  to increment the packet number count
  250.         ;
  251.         nplus   macro
  252.         ld      hl,n
  253.         inc     (hl)
  254.         endm
  255.         ;
  256.         ;dec3   decrement three times a register or register pair
  257.         ;
  258.         dec3    macro   reg
  259.         dec     reg
  260.         dec     reg
  261.         dec     reg
  262.         endm
  263.         ;
  264.         ;addbc  to add a to bc in checksum computation
  265.         ;
  266.         addbc   macro
  267.         .xlist
  268.         add     a,c             ;c=c+1 (there might be a carry)
  269.         ld      c,a             ;back in c
  270.         ld      a,0             ;not xor a because we need the carry
  271.         adc     a,b             ;add the carry to b
  272.         ld      b,a             ;back in b
  273.         .list
  274.         endm                    ;bc=bc+a
  275.         ;
  276.         ;f_ack  to format ack using current n
  277.         ;
  278.         f_ack   macro
  279.         .xlist
  280.         ld      (iy+len),3      ;length=3
  281.         ld      a,(n)           ;current packet count
  282.         add     a,' '           ;make printable
  283.         ld      (iy+seq),a      ;put n in packet
  284.         ld      (iy+type),'Y'   ;type = ack
  285.         ld      hl,spaket       ;hl points to send packet
  286.         call    acsum           ;and add the checksum
  287.         .list
  288.         endm
  289.         ;
  290.         ;movb   to move a byte to memory
  291.         ;
  292.         movb    macro   value,loc
  293.         .xlist
  294.                 push    af      ;save
  295.                 ld      a,value ;get byte
  296.                 ld      (loc),a ;save
  297.                 pop     af      ;restore
  298.         .list
  299.                 endm
  300.         subttl  rpack - receive packet routine
  301.         page
  302.         ;
  303.         ;       rpack   receive packet routine
  304.         ;       call    rpack
  305.         ;               will discard soh on reception
  306.         ;               and will return with carry set
  307.         ;               if timout occured or cheksum wrong
  308.         ;
  309.         rpack:
  310.                 timer   rp0,tout        ;set timer handler
  311.         rp1:
  312.                 ld      hl,rpaket       ;set up hl
  313.                 rcvb                    ;get a byte
  314.                 cp      soh             ;is it a soh ?
  315.                 jr      nz,rp1          ;no, not yet, start over
  316.                 ld      b,0             ;for checksum bc=0
  317.                 ld      c,0             ;*****************
  318.         rp2:    ;len
  319.                 rcvb                    ;get a byte
  320.                 cp      soh             ;is it a soh ?
  321.                 jp      z,rp1           ;yes, re-sync
  322.                 ld      (hl),a          ;save in rpaket
  323.                 addbc                   ;add to bc for checksum
  324.                 ld      a,(hl)          ;get back byte
  325.                 inc     hl              ;point to next byte
  326.                 sub     ' '+3           ;convert to numeric
  327.                 ld      (rlen),a        ;and save
  328.         rp3:    ;packet number
  329.                 rcvb                    ;get a byte
  330.                 cp      soh             ;soh ?
  331.                 jp      z,rp1           ;yes, re-sync
  332.                 ld      (hl),a          ;save in rpaket
  333.                 inc     hl              ;update counter
  334.                 addbc                   ;add to bc for checksum
  335.         rp4:    ;type of packet
  336.                 rcvb                    ;get a byte
  337.                 cp      soh             ;soh ?
  338.                 jp      z,rp1           ;yes, re-sync
  339.                 ld      (hl),a          ;save in rapket
  340.                 inc     hl              ;update pointer
  341.                 addbc                   ;add to bc for checksum
  342.                 ld      a,(rlen)        ;get data length
  343.                 cp      0               ;is it null ?
  344.                 jp      z,rp6           ;yes, get checksum now
  345.         rp5:    ;data field
  346.                 rcvb                    ;get a byte
  347.                 cp      soh             ;soh ?
  348.                 jp      z,rp1           ;yes, re-sync
  349.                 ld      (hl),a          ;save
  350.                 inc     hl              ;update counter
  351.                 addbc                   ;add to bc for checksum
  352.                 ld      a,(rlen)        ;get length of packet
  353.                 dec     a               ;decrement
  354.                 ld      (rlen),a        ;ans store back
  355.                 cp      0               ;is it null ?
  356.                 jp      nz,rp5          ;no, get one more byte
  357.         rp6:    ;checksum
  358.                 rcvb                    ;get a byte
  359.                 cp      soh             ;soh ???
  360.                 jp      z,rp1           ;yes, re-sync
  361.                 sub     ' '             ;convert to numeric
  362.                 ld      (byte),a        ;save received checksum
  363.                 ld      a,c             ;get low byte
  364.                 and     300O            ;only two high bits
  365.                 rlca                    ;rotale left
  366.                 rlca                    ;twice
  367.                 add     a,c             ;add back to low byte
  368.                 and     077O            ;only six bits
  369.                 ld      c,a             ;computed checksum
  370.                 ld      a,(byte)        ;received checksum
  371.                 cp      c               ;equal ?
  372.                 jp      nz,rp0          ;no good
  373.                 timer   0,0             ;terminate timout handler
  374.                 scf                     ;ser carry to 1
  375.                 ccf                     ;back to 0
  376.                 ret                     ;and return
  377.         rp0:    timer   0,0             ;terminate timout handler
  378.                 scf                     ;set carry flag
  379.                 ret
  380.                 ;
  381.                 ;
  382.         subttl  flush - to reset communication port
  383.         page
  384.         ;
  385.         ;       flush   to reset internal communication buffer
  386.         ;               (mostly to get rid of stacked up naks)
  387.         flush:
  388.                 push    bc              ;save
  389.                 ld      b,6             ;code to reset buffer
  390.                 ld      a,(csvc)        ;control svc
  391.                 rst     8               ;dos call
  392.                 pop     bc              ;restore
  393.                 ret
  394.         ;
  395.         subttl  rplus - to increment retry count
  396.         page
  397.         ;       rplus   increment retry count and jump back
  398.         ;
  399.         rplus:
  400.                 ld      a,(r)           ;get retry count
  401.                 inc     a               ;increment it
  402.                 cp      tout            ;to maximum ?
  403.                 jp      z,abort         ;yes abort
  404.                 ld      (r),a           ;save back
  405.                 jp      mjump           ;and go back
  406.         ;
  407.         subttl  abort - end in disaster sending an error packet
  408.         page
  409.         ;       abort   end transmission and die...
  410.         ;
  411.         abort:
  412.                 prmes   a0              ;aborting ...
  413.                 ld      (iy+len),3      ;length = 3
  414.                 ld      a,(n)           ;get current packet seq
  415.                 cp      0               ;are we at beginning ?
  416.                 jp      z,ab0           ;yes, do not send error pak
  417.                 add     a,' '           ;make printable
  418.                 ld      (iy+seq),a      ;and store
  419.                 ld      (iy+type),'E'   ;type error packet
  420.                 ld      hl,spaket       ;set up hl
  421.                 call    acsum           ;compute checksum
  422.                 call    spack           ;and send packet
  423.         ab0:
  424.         exit:   prmes   e0              ;end of job
  425.                 rst     0               ;bye !
  426.         ;
  427.         subttl  acsum - add checksum to a packet
  428.         page
  429.         ;       acsum   compute and store checksum (hl)
  430.         ;
  431.         acsum:
  432.                 push    hl              ;save
  433.                 push    bc              ;save
  434.                 ld      b,0             ;initialize bc to 0
  435.                 ld      c,0             ;******************
  436.                 ld      a,(hl)          ;get length
  437.                 ld      (slen),a        ;save it
  438.                 add     a,' '           ;make printable
  439.                 ld      (hl),a          ;store back in packet
  440.         ac0:
  441.                 ld      a,(hl)          ;get a byte
  442.                 addbc                   ;add to bc for checksum
  443.                 inc     hl              ;increment pointer
  444.                 ld      a,(slen)        ;get length
  445.                 dec     a               ;decrement it
  446.                 ld      (slen),a        ;save it back
  447.                 cp      0               ;are we at end ?
  448.                 jp      nz,ac0          ;no, get one more byte
  449.                 ld      a,c             ;get low byte of sum
  450.                 and     300O            ;only 2 high bits
  451.                 rlca                    ;rotate left
  452.                 rlca                    ;twice
  453.                 add     a,c             ;add it back to low byte
  454.                 and     077O            ;mask off 2 high bits
  455.                 add     a,' '           ;and make pintable
  456.                 ld      (hl),a          ;store in packet
  457.                 pop     bc              ;restore
  458.                 pop     hl              ;restore
  459.                 ret
  460.         ;
  461.         subttl  spack - send a packet already formatted
  462.         page
  463.         ;       spack   send a packet already formatted
  464.         ;
  465.         spack:
  466.                 push    hl              ;save
  467.                 ld      a,(spaket)      ;get length
  468.                 sub     31              ;real length
  469.                 ld      (slen),a        ;save it
  470.                 movb    soh,byte        ;store a soh
  471.                 ld      hl,byte         ;set up hl
  472.                 xmitb                   ;transmit (hl)=soh
  473.                 ld      hl,spaket       ;packet address
  474.                 ld      a,(slen)        ;and length
  475.         sp1:
  476.                 push    af              ;save
  477.                 xmitb                   ;transmit (hl)
  478.                 pop     af              ;restore a
  479.                 dec     a               ;decrement length of packet
  480.                 inc     hl              ;update pointer
  481.                 cp      0               ;are we at end ?
  482.                 jp      nz,sp1          ;no, one more byte
  483.                 ;now send eol
  484.                 ld      hl,sinit+4      ;where eol is stored
  485.                 xmitb                   ;send it
  486.                 pop     hl              ;restore
  487.                 ret
  488.         ;
  489.         ;
  490.                 end
  491.  
  492.