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

PACKNR   DW 0
ANTBYTES DW 0
ANTBUF   DW 0
NAKNR    DW 0
KERMAREA DS 40
FIL      DS 15
         DB 0

;Ta emot en fil som s{nds med Kermit
RECEIVE  INP  58
         AND  2
         JPNZ TIMEOUT   Ingen b{rv}g
         LD   HL,KTXT1
         LD   BC,KTXT2-KTXT1
         CALL WRITE
         LD   IX,KERMAREA
         LD   (IX+0),0  Inga flaggor
         CALL RDEFAULT
         LD   A,(TIME)
         LD   (IX+12),A Default maxtid
         LD   (IX+8),1  Start CH-typ
         LD   (IX+23),0 Packetr{knare
         LD   (IX+26),"S"
RE1      LD   A,(RETRY)
         LD   (IX+20),A Max ant f|rs|k
         JR   RE3

RE2      DEC  (IX+20)
         JPZ  TIMEOUT   F|r m}nga f|rs|k
         LD   A,(IX+26)
         CP   "D"
         JRNZ RE3       V{ntar ej data
         LD   HL,(NAKNR) R{kna antalet
         INC  HL         felaktiga paket
         LD   (NAKNR),HL
         CALL SKRIVFIL
RE3      BIT  3,(IX+0)
         JPNZ TIMEOUT   ESCAPE
         LD   A,(IX+12)
         LD   (IX+21),A S{tt tid kvar
         LD   A,(IX+26)
         CP   "S"
         JPZ  RINIT     Kermitparametrar
         CP   "F"
         JPZ  RFIL      Filhuvud
         CP   "D"
         JPZ  RDATA     Data i fil
         RET            Klart

;V{nta p} Kermitparametrar
RINIT    CALL GPACK     Ta emot packet
         JPC  RE2       Fel, f|rs|k igen
         LD   A,(IX+25)
         CP   "E"
         JPZ  RERROR    Avbryt!
         CP   "S"
         JPNZ RE2       Fel packettyp
         LD   HL,KTXT2
         LD   BC,KTXT3-KTXT2
         CALL WRITE
         CALL RFIX
         LD   C,(IX+8)
         PUSH BC
         LD   (IX+8),1  CH-typ 1
         LD   (IX+24),9
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         POP  BC
         LD   (IX+8),C
         JPC  TIMEOUT   Time out
         LD   HL,KTXT4
         LD   BC,KTXT5-KTXT4
         CALL WRITE     Filrubrik
         LD   A,(IX+23)
         INC  A
         AND  63
         LD   (IX+23),A
         LD   (IX+26),"F"
         JP   RE1

;V{nta p} filhuvud
RFIL     CALL GPACK     Ta emot packet
         JPC  RE2       Fel, f|rs|k igen
         LD   A,(IX+25)
         CP   "S"
         JPZ  RFIL1     Oms{ndning
         CP   "Z"
         JPZ  RFIL5     Oms{ndning
         CP   "F"
         JPZ  RFIL6     Filnamn
         CP   "B"
         JPZ  RBREAK    Break
         CP   "E"
         JPZ  RERROR
         JP   FELPACK

;Behandla oms{ndning parametrar
RFIL1    LD   A,(IX+23)
         DEC  A
         CP   (IX+22)
         JPNZ RE2       Ej oms{ndning
         CALL RFIX
         LD   C,(IX+8)
         PUSH BC
         LD   (IX+8),1  CH-typ 1
         LD   (IX+24),9
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         POP  BC
         LD   (IX+8),C
         JPNC RE1
         JP   TIMEOUT

;Fixa parametrar att s{nda
RFIX     CALL RPARM
         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" Vi klarar av
         LD   A,(PARITET) ev QBIN
         AND  A
         JRZ  RFIX1     Paritet NONE
         LD   A,(IX+17)
         AND  A         S{ndare klarar
         JRZ  RFIX2     ej av QBIN
         LD   (IX+7),A
         CP   "Y"       S{ndare beg{r
         JRNZ RFIX2     QBIN
         LD   A,(QBIN)
         LD   (IX+7),A
         LD   (IX+17),A
         JR   RFIX2     Vi beg{r QBIN

RFIX1    LD   A,(IX+17)
         AND  A         S{ndare klarar
         JRZ  RFIX2     ej av QBIN
         LD   (IX+7),A
         CP   "Y"       S{ndare beg{r
         JRNZ RFIX2     QBIN
         LD   (IX+17),0 Ingen vill ha
RFIX2    LD   A,(CHECK)
         LD   (IX+8),A
         CP   (IX+18)
         JRZ  RFIX3     B}da har samma
         LD   (IX+8),1
         LD   (IX+18),1
RFIX3    LD   A,(IX+19)
         LD   (IX+9),A
         CALL SPARM
         RET

;Behandla oms{ndning EOF
RFIL5    LD   A,(IX+23)
         DEC  A
         CP   (IX+22)
         JPNZ RE2       Ej oms{ndning
         LD   (IX+24),0 Inget data
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         JPNC RE1
         JP   TIMEOUT

;Filnamn
RFIL6    LD   A,(IX+23)
         CP   (IX+22)
         JPNZ RE2       Fel packetnr
         LD   HL,0
         LD   (PACKNR),HL
         LD   (ANTBYTES),HL
         LD   (ANTBUF),HL
         LD   (NAKNR),HL
         CALL CRLF      Ny rad
         LD   HL,BUFF3  Peka p} data
         LD   DE,255:128
         LD   C,(IX+24) L{ngd filnamn
         LD   B,0
         PUSH HL
         PUSH DE
         CALL 0:65      Fixa filnamn
         POP  HL
         LD   BC,8
         CALL WRITE
         PUSH HL
         LD   HL,KTXT6
         LD   BC,1
         CALL WRITE     "."
         POP  HL
         LD   BC,3
         CALL WRITE     Extension
         CALL SKRIVFIL
         POP  HL
         LD   (IX+37),0 Ej CTRL-XZ {nnu
         SET  5,(IX+0)  Till}t CTRL-XZ
         PUSH HL
         LD   E,(IX+24) L{ngd data
         LD   D,0
         ADD  HL,DE
         LD   (HL),13   Slut p} filnamn
         POP  HL        Pekar p} filnamn
         LD   DE,(FILPEK)
         LD   A,(DE)
         CP   13
         JRZ  RFIL7
         EX   DE,HL     Lokalt filnamn
RFIL7    LD   A,(ACCESS)
         AND  A         Strunta i om
         JRZ  RFIL8     filen finns
         PUSH HL
         PUSH IX
         CALL OPEN
         POP  IX
         POP  HL        Pekar p} filnamn
         JRNC RFIL9     Filen finns!
         AND  127
         CP   21
         JPNZ FELFIL    Annat fel
RFIL8    PUSH IX
         CALL PREPARE
         POP  IX
         JPNC RFIL13    Det gick bra
         JP   FELFIL

RFIL9    LD   A,(ACCESS)
         CP   1
         JRZ  RFIL12    Kill
         LD   HL,RFIL10
         LD   BC,RFIL12-RFIL10
         CALL WRITE
         LD   HL,RFIL11
         LD   BC,RFIL12-RFIL11
         LD   (IX+24),C
         LD   DE,BUFF4
         LDIR
         LD   (IX+25),"E"
         CALL SPACK     Error!
         JP   AVBRYT

RFIL10   DM 13,10,7
RFIL11   DM "File already "
         DM "exists on disk"

RFIL12   PUSH HL
         PUSH IX
         LD   IX,FIL
         LD   DE,8:159
         CALL FILCONTR  Kill
         POP  IX
         POP  HL        Pekar p} filnamn
         JPNC RFIL8     Forts{tt...
         JP   FELFIL

RFIL13   LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
         CALL SKRIVFIL
         LD   (IX+29),0 Filtyp ok{nd
         LD   (IX+24),0
         LD   (IX+25),"Y"
         LD   A,(IX+37)
         AND  A
         JRZ  RFIL14    Ej CTRL-XZ
         LD   (BUFF4),A S{nd tkn i ACK!
         INC  (IX+24)   L{ngd ett
         SET  4,(IX+0)  V{nta p} Z-pack
         RES  5,(IX+0)  F|rbjud CTRL-XZ
RFIL14   CALL SPACK     Positivt svar
         JPC  TIMEOUT
         LD   HL,(PACKNR)
         INC  HL
         LD   (PACKNR),HL
         CALL SKRIVFIL
         LD   A,(IX+23)
         INC  A
         AND  63
         LD   (IX+23),A
         LD   (IX+26),"D"
         JP   RE1

;Break
RBREAK   LD   A,(IX+23)
         CP   (IX+22)
         JPNZ RE2       Fel packetnr
         LD   (IX+24),0
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         JPC  TIMEOUT
         LD   (IX+26),"C"
         JP   RE1

RDATA    CALL GPACK     Ta emot packet
         JPC  RE2       Fel, f|rs|k igen
         LD   A,(IX+25)
         CP   "Z"
         JPZ  REOF      EOF
         CP   "E"
         JPZ  RERROR
         BIT  4,(IX+0)  S{ndaren klarar
         JPNZ REOF1     inte av CTRL-XZ
         CP   "D"
         JPZ  RDATA1    Data
         CP   "F"
         JPZ  RDATA3    Oms{ndning
         JP   FELPACK

;Data i fil
RDATA1   LD   A,(IX+23)
         CP   (IX+22)
         JRNZ RDATA3    Fel packetnr
         CALL UNPACK    Packa upp data
         JPC  FELFIL    Fel vid skrivn
         LD   HL,(PACKNR)
         INC  HL
         LD   (PACKNR),HL
         CALL SKRIVFIL
         LD   (IX+24),0
         LD   (IX+25),"Y"
         LD   A,(IX+37)
         AND  A
         JRZ  RDATA2    Ej CTRL-XZ
         LD   (BUFF4),A S{nd tkn i ACK!
         INC  (IX+24)   L{ngd ett
         SET  4,(IX+0)  V{nta p} Z-pack
         RES  5,(IX+0)  F|rbjud CTRL-XZ
RDATA2   CALL SPACK     Positivt svar
         JPC  TIMEOUT
         LD   A,(IX+23)
         INC  A
         AND  63
         LD   (IX+23),A
         JP   RE1

;Behandla oms{ndning filnamn el data
RDATA3   LD   A,(IX+23)
         DEC  A
         CP   (IX+22)
         JPNZ RE2       Ej oms{ndning
         LD   (IX+24),0
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         JPNC RE1
         JP   TIMEOUT

;End Of File
REOF     LD   A,(IX+23)
         CP   (IX+22)
         JPNZ RE2       Fel packetnr
         RES  5,(IX+0)  F|rbjud CTRL-XZ
         BIT  4,(IX+0)
         JRZ  REOF2     Ej CTRL-XZ
         LD   A,(IX+24) L{ngd packet
         AND  A
         JRZ  REOF1     Tom
         DEC  A
         JRNZ REOF1     Mer {n ett tkn
         LD   A,(BUFF3) Data i packet
         CP   "D"
         JRNZ REOF1     Ej "avbryt"
         RES  4,(IX+0)  CTRL-XZ funkade
REOF1    PUSH IX
         LD   IX,FIL
         LD   DE,8:159
         CALL FILCONTR  Kill
         POP  IX
         JPC  FELFIL
         LD   A,32
         LD   (253:244),A
         LD   HL,KTXT7
         LD   BC,KTXT8-KTXT7
         CALL WRITE
         BIT  4,(IX+0)
         JRZ  REOF6     CTRL-XZ funkade
         LD   HL,REOF7
         LD   BC,REOF8-REOF7
         LD   DE,BUFF4
         LD   (IX+24),C
         LDIR
         LD   (IX+25),"E"
         CALL SPACK     Error!
         JP   AVBRYT

REOF2    LD   A,(IX+29) Typ av fil
         PUSH IX
         LD   IX,FIL
         CP   1
         JRZ  REOF3     Textfil
         BIT  7,(IX+14)
         JRZ  REOF4     Buffert tom
         CALL 8:156     Skriv buffert
         RES  7,(IX+14) Buffert tom
REOF3    LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
REOF4    LD   DE,8:127
         CALL FILCONTR  Close
         POP  IX
         JRNC REOF5
         CALL SKRIVERR
         JR   REOF6

REOF5    LD   A,32
         LD   (253:244),A
         LD   HL,KTXT5
         LD   BC,KTXT6-KTXT5
REOF6    CALL WRITE
         LD   (IX+24),0
         LD   (IX+25),"Y"
         CALL SPACK     Positivt svar
         JPC  TIMEOUT
         LD   A,(IX+23)
         INC  A
         AND  63
         LD   (IX+23),A
         LD   (IX+26),"F"
         JP   RE1

REOF7    DM "User interrupt"
REOF8

;Skriv data p} fil
PUTFIL   LD   A,(IX+29) Typ av fil
         CP   1
         JRZ  PF2       Textfil
         CP   2
         JRZ  PF5       Bin{rfil
         LD   A,(FILE)
         AND  A
         JRZ  PF1       Textfil
         CP   1
         JRZ  PF4       Bin{rfil
         LD   A,(HL)    Annars unders|k
         AND  A         byte i filen!
         JRZ  PF4       ABS-fil
         CP   128
         JRNC PF4       Annan bin{rfil
PF1      LD   (IX+29),1 Typ textfil
PF2      PUSH IX
         LD   IX,FIL
         LD   E,(IX+13)
         PUSH DE
         LD   DE,8:150
         CALL FILCONTR  Skriv rad
         POP  DE
         POP  IX
         RETC
         LD   A,(FIL+13)
         CP   E
         CCF
         RETNC
         LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
         AND  A
         RET

PF4      LD   (IX+29),2 Typ bin{rfil
         PUSH IX
         LD   IX,FIL
         INC  (IX+13)
         LD   A,(IX+9)
         POP  IX
         AND  A
         LD   A,52      ERR 52
         SCF
         RETZ           Buffert saknas
PF5      PUSH IX
         LD   IX,FIL
         LD   E,(IX+10) Position i buff
         LD   D,(IX+11)
PF6      LD   A,B
         OR   C
         JRZ  PF7
         LDI            L{gg tkn i buff
         SET  7,(IX+14) Data i buffert
         DEC  (IX+13)   Kvar i buffert
         JRNZ PF6       Buffert ej full
         PUSH HL
         PUSH BC
         CALL 8:156     Skriv buffert
         LD   HL,(ANTBUF)
         INC  HL
         LD   (ANTBUF),HL
         POP  BC
         POP  HL
         RES  7,(IX+14) Buffert tom
         INC  (IX+13)
         JR   PF6

PF7      LD   (IX+10),E
         LD   (IX+11),D
         POP  IX
         RET

KTXT1    DB 13,10
         DM "Waiting for init packet",13
KTXT2    DM "Init packet received   ",13
KTXT3    DB 13,10
         DM "Sending init packet",13
KTXT4    DM "filename      bad "
         DM "pkt bytes blk status"
KTXT5    DM "OK    "
KTXT6    DM "."
KTXT7    DM "FLUSH "
KTXT8    DM "ERR "
KTXT9    DM "("
KTXT10   DM ")"


;Visa text i errorpacket p} sk{rm
RERROR   LD   HL,RERR1
         LD   BC,RERR2-RERR1
         CALL WRITE
         LD   HL,BUFF3  Peka p} data
         LD   C,(IX+24) L{ngd data
         LD   B,0
         CALL WRITE     Skriv text
         JP   AVBRYT

RERR1    DM 13,10
         DM "Received message:"
         DM 13,10
RERR2    EQU  $

;Fel vid filhantering, s{nd error
FELFIL   PUSH AF
         CALL SKRIVERR  Skriv p} sk{rm
         POP  AF
         LD   (IX+24),0
         OR   128
         LD   HL,FELF4
         LD   BC,FELF5-FELF4
         CPIR
         JRNZ FELF3
         DEC  HL        Peka p} kod
FELF1    INC  HL
         BIT  7,(HL)
         JRNZ FELF1     Skippa highbyte
         PUSH HL
         LD   BC,0
FELF2    INC  HL
         INC  BC
         BIT  7,(HL)
         JRZ  FELF2
         POP  HL
         LD   (IX+24),C
         LD   DE,BUFF4
         LDIR
FELF3    LD   (IX+25),"E"
         CALL SPACK     Error!
         JP   AVBRYT

FELF4    DM 128+21
         DM "Can't find file"
         DM 128+35
         DM 128+36
         DM 128+37
         DM 128+38
         DM 128+44
         DM 128+45
         DM 128+46
         DM 128+47
         DM 128+48
         DM 128+49
         DM "I/O-error"
         DM 128+39
         DM 128+43
         DM "Writeprotection"
         DM 128+41
         DM "Disk full"
         DM 128+42
         DM "Disk not ready"
         DM 255
FELF5    EQU  $

FELPACK  LD   HL,FPK1
         LD   BC,FPK3-FPK1
         CALL WRITE
         LD   HL,FPK2
         LD   BC,FPK3-FPK1
         LD   (IX+24),C
         LD   DE,BUFF4
         LDIR
         LD   (IX+25),"E"
         CALL SPACK     S{nd error
         JR   AVBRYT

FPK1     DM 10,13
FPK2     DM "Wrong packet received"
FPK3     EQU  $

TIMEOUT  INP  58
         AND  2
         LD   HL,TIM2
         LD   BC,TIM3-TIM2
         JRNZ TIM1      Ingen b{rv}g
         BIT  3,(IX+0)
         LD   HL,TIM3
         LD   BC,TIM4-TIM3
         JRNZ TIM1      ESCAPE nedtryckt
         LD   HL,TIM4
         LD   BC,TIM5-TIM4
TIM1     CALL 0:11
AVBRYT   PUSH IX
         LD   IX,FIL
         LD   DE,8:127
         CALL FILCONTR  Close
         POP  IX
         CALLC SKRIVERR
         LD   (IX+26),"A"
         SCF
         RET

TIM2     DM 13,10,7,"No connection"
TIM3     DM 13,10,7,"Abort"
TIM4     DM 13,10,7,"Timeout"
TIM5     EQU $
