1 REM Ins{nd av Anders Franz`n <5258>    1988-01-01 13.42.30 (KERMIT)
; KERM6.ASM
; Anders Franz`n <5258>
; Stockholm 1987-09-13


;S{nd en eller flera fil med Kermit
SEND     INP  58
         AND  2
         JPNZ TIMEOUT   B{rv}g saknas
         LD   HL,KTXT3
         LD   BC,KTXT4-KTXT3
         CALL WRITE
         LD   IX,KERMAREA
         LD   (IX+0),0
         LD   (IX+23),0 Packetr{knare
         LD   A,(LENGTH)
         LD   (IX+1),A
         LD   A,(TIME)
         LD   (IX+2),A
         LD   A,(NUMPAD)
         LD   (IX+3),A
         LD   A,(PADCHAR)
         LD   (IX+4),A
         LD   A,(EOL)
         LD   (IX+5),A
         LD   A,(QCTL)
         LD   (IX+6),A
         LD   (IX+7),"Y"
         LD   A,(PARITET)
         AND  A
         JRZ  SE1       Paritet NONE
         LD   A,(QBIN)
         LD   (IX+7),A  Vi beg{r QBIN
SE1      LD   A,(CHECK)
         LD   (IX+8),A
         LD   A,(REPT)
         LD   (IX+9),A
         CALL RDEFAULT
         CALL SPARM
         LD   C,(IX+8)
         PUSH BC
         LD   (IX+8),1  CH-typ 1
         LD   (IX+24),9
         LD   (IX+25),"S"
         CALL PPACK     S{nd parametrar
         POP  BC
         LD   (IX+8),C
         JPC  TIMEOUT   Time out
         CALL RPARM
         LD   A,(PARITET)
         AND  A
         JRZ  SE2       Paritet NONE
         LD   A,(IX+17)
         CP   (IX+7)
         JRZ  SE3       B}da vill QBIN
         CP   "Y"
         JRZ  SE3
         LD   (IX+7),0  Mottagaren
         JR   SE3       klarar ej QBIN

SE2      LD   (IX+7),0
         LD   A,(IX+17)
         AND  A         S{ndare klarar
         JRZ  SE3       ej QBIN
         CP   "Y"
         JRZ  SE3       Ingen vill QBIN
         LD   (IX+7),A  S{ndare vill
SE3      LD   A,(IX+8)
         CP   (IX+18)
         JRZ  SE4
         LD   (IX+8),1
         LD   (IX+18),1
SE4      LD   A,(IX+9)
         CP   (IX+19)
         JRZ  SE5
         LD   (IX+9),0
         LD   (IX+19),0
SE5      LD   HL,KTXT4
         LD   BC,KTXT5-KTXT4
         CALL WRITE     Skriv filrubrik
SE6      CALL CRLF      Ny rad
         LD   HL,0
         LD   (PACKNR),HL
         LD   (ANTBYTES),HL
         LD   (ANTBUF),HL
         LD   (NAKNR),HL
         LD   A,(WILDFLG)
         AND  A
         JRZ  SE9       Ej wildcard
         BIT  7,(IX+0)
         JRNZ SE8       Drive initierad
         SET  7,(IX+0)
         XOR  A
         LD   (255:212),A Filstorlek
SE7      LD   BC,(WILDANT)
         DEC  B
         JPM  SE20      Slut p} drivear
         LD   A,C       Drivenummer
         INC  C
         LD   (WILDANT),BC
         CALL LIBINIT
         JRC  SE7
         CALL FIRSTFIL
         JRC  SE7
         JRZ  SE7       Drive tom
         JR   SE9

SE8      CALL NEXTFIL
         JRC  SE7
         JRZ  SE7       N{sta drive
SE9      LD   HL,255:132 Fil utan enhet
         LD   DE,BUFF4
         LD   (IX+24),-1 L{ngd data
         LD   BC,(FILPEK)
         LD   A,(BC)
         CP   13
         JRZ  SE10      Samma fil remote
         LD   L,C
         LD   H,B
SE10     RST  32
         INC  HL
         LD   (DE),A    Kopiera filnamn
         INC  DE
         INC  (IX+24)   R{kna antal tkn
         CP   13
         JRNZ SE10
         LD   HL,255:132 Fil utan enhet
         LD   BC,12     L{ngd filnamn
         CALL WRITE     Skriv filnamn
         CALL SKRIVFIL
         LD   A,(WILDFLG)
         AND  A
         JRZ  SE11      Ej wildcard
         LD   A,32+1
         LD   (253:244),A
         LD   HL,KTXT9
         LD   BC,1
         CALL WRITE     "("
         LD   A,(DEBUG)
         DEC  A
         LD   HL,(255:146)
         CALLNZ SKRIVTAL Filstorlek
         LD   HL,KTXT10
         LD   BC,1
         CALL WRITE     ")"
SE11     LD   HL,255:128 Filnamn
         RST  32
         PUSH IX
         CALL OPEN
         POP  IX
         JRNC SE12      Det gick bra
         CALL SKRIVERR
         LD   A,(WILDFLG)
         AND  A         Wildcard?
         JPNZ SE6       Ja, n{sta fil
         JP   SE20      S{nd BREAK

SE12     LD   (IX+37),0 Ej CTRL-XZ {nnu
         SET  5,(IX+0)  Till}t CTRL-XZ
         LD   (IX+27),0 Inget data l{st
         LD   (IX+29),0 Ok{nd filtyp
         LD   (IX+25),"F"
         CALL PPACK     S{nd filhuvud
         JPC  TIMEOUT   Time out
         LD   HL,1
         LD   (ANTBUF),HL
         CALL SKRIVFIL
SE13     LD   A,(IX+37)
         AND  A
         JPNZ SE18      Hoppa om CTRL-XZ
         CALL PACK      Packa data
         JRC  SE15      EOF eller ERROR
         LD   (IX+25),"D"
         CALL PPACK     S{nd data
         JPC  TIMEOUT   Time out
         CALL SKRIVFIL
         LD   A,(IX+24)
         AND  A
         JRZ  SE13      Inget data i ACK
         DEC  A
         JRNZ SE13      Mer {n ett tkn
         LD   A,(BUFF3)
         CP   "X"
         JRZ  SE14      X i ACK
         CP   "Z"
         JRNZ SE13      Ej Z i ACK
SE14     RES  5,(IX+0)  F|rbjud CTRL-XZ
         LD   (IX+37),A
         JP   SE18

SE15     AND  A
         JRZ  SE16      EOF
         CALL SKRIVERR  Annat fel
         JR   SE18

SE16     LD   A,(IX+24)
         AND  A
         JRZ  SE17      Inget mer data
         LD   (IX+25),"D"
         CALL PPACK     S{nd sista data
         JPC  TIMEOUT   Time out
         CALL SKRIVFIL
SE17     LD   A,32
         LD   (253:244),A
         LD   HL,KTXT5
         LD   BC,KTXT6-KTXT5
         CALL WRITE     Skriv sluttext
SE18     PUSH IX
         LD   IX,FIL
         LD   DE,8:127
         CALL FILCONTR  Close
         POP  IX
         CALLC SKRIVERR
         RES  5,(IX+0)  F|rbjud CTRL-XZ
         LD   (IX+24),0
         LD   (IX+25),"Z"
         LD   A,(IX+37)
         AND  A
         JRZ  SE19      Ej CTRL-XZ
         LD   A,32
         LD   (253:244),A
         LD   HL,KTXT7
         LD   BC,KTXT8-KTXT7
         CALL WRITE     Skriv avbryttext
         LD   A,"D"
         LD   (BUFF4),A
         INC  (IX+24)
SE19     CALL PPACK     S{nd EOF
         JPC  TIMEOUT
         LD   A,(IX+37)
         CP   "Z"
         JRZ  SE20      Skippa batch
         LD   A,(WILDFLG)
         AND  A         Wildcard?
         JPNZ SE6       Ja, n{sta fil
SE20     LD   (IX+24),0
         LD   (IX+25),"B"
         CALL PPACK     S{nd BRAKE
         JPC  TIMEOUT
         RET            Klart!

;H{mta ett tecken fr}n fil
;Carry s{tts om EOF eller ERROR
GETFIL   LD   A,(IX+27)
         AND  A
         JPNZ GF9       Data i buffert
         LD   A,(IX+29) Typ av fil
         CP   1
         JRZ  GF1       Textfil
         CP   2
         JPZ  GF7       Bin{rfil
         LD   A,(FILE)
         AND  A
         JRZ  GF1       Textfil
         CP   1
         JRZ  GF7       Bin{rfil
         PUSH IX        Annars unders|k
         LD   IX,FIL    filen...
         LD   L,(IX+10)
         PUSH HL
         LD   HL,BUFF7
         LD   BC,255
         LD   DE,8:147
         CALL FILCONTR  L{s rad
         POP  HL
         JRC  GF5       Kanske bin{rfil
         JR   GF2       Annars textfil!

GF1      PUSH IX
         LD   IX,FIL
         LD   L,(IX+10)
         PUSH HL
         LD   HL,BUFF7
         LD   BC,255
         LD   DE,8:147
         CALL FILCONTR  L{s rad
         POP  HL
         JRC  GF6       End of file
GF2      LD   A,L
         CP   (IX+10)
         JRC  GF3
         LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
GF3      POP  IX
         EX   DE,HL
         LD   A,(IX+20)
         AND  A
         JRZ  GF4       Hoppa om DUMP
         LD   (HL),10   Avsluta med LF
         INC  HL        om Kermit
GF4      LD   DE,BUFF7
         SBC  HL,DE
         EX   DE,HL
         LD   (IX+27),E L{ngd p} rad
         LD   (IX+29),1 Textfil
         LD   (IX+32),L
         LD   (IX+33),H
         JR   GF9

GF5      CP   186
         JRZ  GF8       Ogiltigt tecken
         AND  A
         JRZ  GF8       EOF dvs ABS-fil
GF6      POP  IX
         SCF
         RET            EOF eller ERROR

GF7      PUSH IX
         LD   IX,FIL
         LD   DE,8:153
         CALL FILCONTR  L{s buffert
         LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
         JRNC GF8       Inget fel
         POP  IX
         CP   38
         SCF
         RETNZ
         LD   A,0
         RET            Slut p} fil

GF8      LD   L,(IX+8)  Adress till
         LD   H,(IX+9)  buffert
         POP  IX
         LD   (IX+27),253 L{ngd p} rad
         LD   (IX+29),2 Bin{rfil
         LD   (IX+32),L
         LD   (IX+33),H
GF9      LD   L,(IX+32)
         LD   H,(IX+33)
         LD   A,(HL)
         INC  HL
         LD   (IX+32),L
         LD   (IX+33),H
         DEC  (IX+27)
         LD   HL,(ANTBYTES)
         INC  HL
         LD   (ANTBYTES),HL
         AND  A
         RET

;S{nd packet, om negativt svar sker
;oms{ndning. Om f|r m}nga f|rs|k eller
;timeout sker retur med carry.
PPACK    LD   A,(INBUFF)
         LD   (INPOS),A T|m inputbuffer
         LD   A,(RETRY)
         INC  A
         LD   (IX+20),A Max ant f|rs|k
         LD   A,(IX+24)
         LD   (IX+30),A L{ngd data
         LD   A,(IX+25)
         LD   (IX+31),A Typ att s{nda
PPK1     BIT  3,(IX+0)
         RETNZ          ESCAPE
         DEC  (IX+20)
         SCF
         RETZ           F|r m}nga f|rs|k
         LD   A,(IX+12)
         LD   (IX+21),A S{tt tid kvar
         LD   A,(IX+23)
         LD   (IX+22),A Packetnr
         LD   A,(IX+30)
         LD   (IX+24),A L{ngd data
         LD   A,(IX+31)
         LD   (IX+25),A Typ att s{nda
         CALL SPACK     S{nd packet
         JRC  PPK1      Fel, f|rs|k igen
         CALL RPACK     Ta emot svar
         JRC  PPK5      Fel, r{kna fel
         LD   A,(IX+25)
         CP   "Y"
         JRZ  PPK2
         CP   "N"
         JRZ  PPK4
         CP   "E"
         JPZ  RERROR
         JP   FELPACK

PPK2     LD   A,(IX+22)
         CP   (IX+23)
         JRNZ PPK5      Fel packetnr
PPK3     LD   A,(IX+23)
         INC  A
         AND  63        Clear carry!
         LD   (IX+23),A
         LD   HL,(PACKNR)
         INC  HL
         LD   (PACKNR),HL
         RET

PPK4     LD   A,(IX+22)
         DEC  A         NAK n+1 tolkas
         CP   (IX+23)   som ACK n!
         JRZ  PPK3
         INC  A
         CP   (IX+23)
         JPNZ FELPACK   Fel packetnr
PPK5     LD   A,(IX+31) Typ att s{nda
         CP   "F"
         JRZ  PPK6
         CP   "D"
         JRZ  PPK6
         CP   "Z"
         JPNZ PPK1
PPK6     LD   HL,(NAKNR)
         INC  HL
         LD   (NAKNR),HL
         CALL SKRIVFIL
         JP   PPK1      Oms{ndning

SKRIVFIL LD   A,(DEBUG)
         DEC  A
         RETZ
         LD   A,13
         LD   (253:244),A
         LD   HL,(NAKNR)
         LD   A,4
         LD   DE,SPACE
         CALL SKRTAL
         LD   HL,(PACKNR)
         LD   A,4
         LD   DE,SPACE
         CALL SKRTAL
         LD   HL,(ANTBYTES)
         LD   A,6
         LD   DE,SPACE
         CALL SKRTAL
         LD   HL,(ANTBUF)
         LD   A,4
         LD   DE,SPACE
         CALL SKRTAL
         RET

SKRIVERR AND  127
         LD   L,A
         LD   H,0
         LD   A,(DEBUG)
         DEC  A
         RETZ
         PUSH HL
         CALL SKRIVFIL
         LD   A,32
         LD   (253:244),A
         LD   HL,KTXT8
         LD   BC,KTXT9-KTXT8
         CALL 0:11      Skriv ERR
         POP  HL
         CALL SKRIVTAL  Skriv felkod
         RET

WRITE    LD   A,(DEBUG)
         DEC  A
         CALLNZ 0:11
         RET

CRLF     LD   A,(DEBUG)
         DEC  A
         CALLNZ 7:66
         RET

;filename      bad pkt bytes blk status
;L]NGRAD .BAC    0  12  1234  12 (15)
;L]NGRAD .BAC    0  13  1235  12 ERR 48
;L]NGRAD .TXT    0   5   234   4 OK
