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


;Ny LIB-rutin f|r TED
TEDLIB   LD   HL,TL4
         LD   BC,TL5-TL4
         CALL 0:11
TL1      CALL 0:2
         LD   HL,TL6
         LD   BC,8
         CPIR
         JRZ  TL2       Siffra 0-7
         LD   C,8
         CPIR
         RETNZ          Ej shift+siffra
         XOR  A         Visa filstorlek
TL2      LD   (255:212),A
         LD   HL,TL7
         LD   (WILDSTR),HL
         LD   A,C       \nskad drive
         OR   128       Flagga TED-lib
         CALL DIR
         JRNC TEDLIB
         LD   HL,TL5
         LD   BC,TL6-TL5
         CALL 0:11
         JR   TL1

TL4      DB 27,61,32+23,32+0,7
         DM "*  \NSKAD DRIVE ?"
TL5      DB 27,61,32+23,32+0,7
         DM "*  SKIVAN EJ KLAR."
TL6      DM "76543210"
         DM '/&%$#"!='
TL7      DB "*",13

;Huvudrutin f|r LIB
DIRH     RST  32
         CP   ","
         JRNZ DIRH1
         INC  HL
         RST  32
         AND  95
         CP   "S"
         JRNZ DIRH1
         RST  24
         XOR  A         Filstorlek visas
DIRH1    LD   (255:212),A Spara flagga
         CALL DRIVEFIX
         LD   HL,(WILDSTR)
         LD   A,(HL)
         CP   13
         JRNZ DIRH2     Argument finns
         LD   (HL),"*"  Default alla
         INC  HL
         LD   (HL),A
DIRH2    CALL 7:66
         LD   BC,(WILDANT)
DIRH3    LD   A,C
         PUSH BC
         CALL DIR
         POP  BC
         INC  C
         DJNZ DIRH3
         RET

DIR      LD   C,A
         AND  127
         PUSH BC
         CALL LIBINIT
         POP  BC
         RETC
         BIT  7,C
         LD   HL,DIR19
         LD   BC,DIR20-DIR19
         CALLNZ 0:11    Skriv om TED-lib
         LD   DE,0:32*7 Sp}r 0 sektor 7
         CALL LIBL[S    L{s sektor
         RETC           Felkod >0
         LD   HL,245:0  Inl{st sektor
         LD   E,L
         LD   D,L       DE <-- noll
         LD   B,160     Antal bytes
         XOR  A
DIR1     CP   (HL)      Unders|k byte
         INC  HL
         JRNZ DIR5      Ej noll
         PUSH HL
         LD   HL,8
         ADD  HL,DE     \ka antalet
         EX   DE,HL     sektorer med 8
         POP  HL
DIR5     DJNZ DIR1      Upprepa
         LD   (255:200),DE Ant sektorer
         LD   DE,0:32*0 Sp}r 0 sektor 0
         CALL LIBL[S    L{s sektor
         RETC           Felkod > 0
         LD   HL,DIR15
         LD   BC,DIR17-DIR15
         CALL 0:11      Skriv "DRx:"
         LD   HL,245:135
         LD   BC,120:0  Max 120 tecken
DIR6     LD   A,(HL)
         INC  HL
         CP   10        Volymnamn slut ?
         JRZ  DIR7      Ja, hoppa
         CP   13
         JRZ  DIR7      Ja, hoppa
         CP   32        Finns volymnamn?
         JRC  DIR8      Nej, hoppa
         CP   128
         JRNC DIR8      Nej, hoppa
         INC  C         \ka r{knare
         DJNZ DIR6      Upprepa om fler
DIR7     LD   A,C
         AND  A         Fanns volymnamn?
         JRZ  DIR8      Nej, hoppa
         LD   B,0       BC=l{ngd p} text
         LD   HL,245:135
         CALL 0:11      Skriv volymnamn
DIR8     CALL 7:66
         CALL FIRSTFIL
         RETC
         JRZ  DIR14
         LD   B,1
DIR9     DJNZ DIR11
         CALL 7:66
         LD   A,(1:216)
         CP   40
         LD   B,2
         JRZ  DIR10
         LD   B,4
DIR10    LD   A,(255:212)
         AND  A
         JRZ  DIR11     Storlek visas
         INC  B
DIR11    PUSH BC
         LD   HL,255:132
         LD   BC,12
         CALL 0:11      Skriv filnamn
         LD   BC,1
         LD   A,(255:212) Ska filstorlek
         AND  A           visas ?
         JRNZ DIR12     Nej
         LD   HL,(255:146)
         LD   A,4
         LD   DE,SPACE
         CALL SKRTAL
         LD   BC,3
DIR12    LD   HL,SPACE
         CALL 0:11      Skriv space
         POP  BC
         CALL PAUS
         RETC           CTRL-C
         PUSH BC
         CALL NEXTFIL
         POP  BC
         RETC
         JRNZ DIR9
DIR14    CALL 7:66
         CALL 7:66
         LD   HL,(255:202) Lediga sekt
         CALL SKRIVTAL
         LD   HL,DIR17
         LD   BC,DIR18-DIR17
         CALL 0:11
         LD   HL,(255:200) Ant sektorer
         CALL SKRIVTAL
         LD   HL,DIR18
         LD   BC,DIR19-DIR18
         CALL 0:11
         RET

DIR15    DM 13,10,"DR"
DIR16    DM "x:  "
DIR17    DM " of "
DIR18    DM " sectors free."
         DM 13,10
DIR19    DB 12,27,61,32+3,32+0
         DM "LIB",13,10
         DM "====="
         DB 27,61,32+6,32+0
DIR20

LIBINIT  LD   (253:1),A Selektera drive
         ADD  48
         LD   (DIR16),A
         LD   DE,0:32*6 Sp}r 0 sektor 6
         CALL LIBL[S    L{s sektor
         RETC           Felkod > 0
         LD   HL,245:0  Inl{st sektor
         LD   E,L
         LD   D,L       DE <-- noll
         LD   C,160     Antal byte
LIB1     LD   B,8       ]tta bitar
LIB2     RR   (HL)      Testa bit
         JRC  LIB3      Upptagen
         INC  DE        \ka r{knare
LIB3     DJNZ LIB2      Repetera
         INC  HL        N{sta byte
         DEC  C         Slut ?
         JRNZ LIB1      Nej, upprepa
         LD   (255:202),DE Lediga sekt
         LD   HL,245:239 Kopiera vilka
         LD   DE,255:204 sektorer i
         LD   C,8        biblioteket
         LDIR            som {r anv{nda
         AND  A
         RET

FIRSTFIL LD   DE,2:0    Sp}r 2 sektor 0
         JR   NX2

NEXTFIL  LD   HL,(DRFIL)
         LD   DE,(DRSEKT)
         LD   A,L
         ADD  16        N{sta fil i
         LD   L,A       biblioteket
         LD   (DRFIL),HL
         AND  A         Sektor slut ?
         JRNZ NX3       Nej, kolla fil
NX1      LD   A,E
         ADD  32        N{sta sektor
         LD   E,A       p} sp}r tv}
         AND  A         Sektorer slut ?
         RETZ           Ja, retur!
NX2      LD   (DRSEKT),DE
         PUSH DE        Spara sektor-nr
         LD   HL,255:204
         SRL  E         Dela E med 32
         SRL  E
         SRL  E
         SRL  E
         SRL  E
         LD   D,0
         ADD  HL,DE     Sl} i tabell
         LD   A,(HL)    [r sektorn
         CP   2         anv{nd ?
         POP  DE
         JRC  NX1       Nej, tag n{sta
         CALL LIBL[S    L{s sektor
         RETC           Felkod > 0
         LD   HL,245:16 Biblioteket
         LD   DE,252:16 Kassettbuffer 2
         LD   BC,240
         PUSH DE
         LDIR
         POP  HL        Filbeskrivning
         LD   (DRFIL),HL
NX3      LD   A,(HL)
         AND  A         Finns fil ?
         JRZ  NEXTFIL   Nej, hoppa
         INC  A
         JRZ  NEXTFIL
         PUSH HL
         LD   DE,4
         ADD  HL,DE     Peka p} filnamn
         EX   DE,HL
         LD   HL,255:128
         LD   (HL),"D"
         INC  HL
         LD   (HL),"R"
         INC  HL
         LD   A,(DIR16)
         LD   (HL),A
         INC  HL
         LD   (HL),":"
         INC  HL
         EX   DE,HL
         PUSH DE
         LD   BC,8
         LDIR           Spara filnamn
         LD   A,"."
         LD   (DE),A
         INC  DE
         LD   BC,3
         LDIR           Spara extension
         LD   A,13
         LD   (DE),A
         POP  DE
         CALL WILDCARD  Kolla filnamn
         POP  HL
         JPNC NEXTFIL   Passar ej
         LD   A,(255:212) Ska filstorlek
         AND  A           visas ?
         RETNZ          Nej, retur
         LD   D,(HL)    Sp}r segmrecord
         INC  HL
         LD   A,(HL)
         AND  224       Ta bort status
         LD   E,A       Sektor*32
         CALL LIBL[S    L{s segmrecord
         RETC           Felkod > 0
         LD   DE,245:4  Segmbeskrivning
         LD   HL,0      R{knare f|r
         LD   B,L       filstorleken
NX4      LD   A,(DE)
         INC  A         Fler segment ?
         JRZ  NX5       Nej, hoppa
         INC  DE        Skippa sp}radr
         LD   A,(DE)
         INC  DE
         AND  31        Maska fram antal
         INC  A         sektorer i segm
         LD   C,A
         ADD  HL,BC     \ka r{knare
         JR   NX4       N{sta segment

NX5      LD   (255:146),HL
         OR   1
         RET

DRSEKT   DW 0
DRFIL    DW 0

; LIBL[S l{ser en sektor p} skivan.
LIBL[S   PUSH DE
         PUSH BC
         PUSH HL
         CALL 96:102    L{s sektor
         POP  HL
         POP  BC
         POP  DE
         LD   A,(253:21)
         AND  A         Felkod > 0 ?
         RETZ
         SCF
         RET

TYPE     PUSH HL
         CALL CLOSE
         POP  HL
         CALL OPEN
         JPC  ERROR
         CALL 7:66
TYP1     LD   HL,BUFF6
         LD   BC,255
         LD   DE,8:147
         PUSH HL
         CALL FILCONTR  L{s in en rad
         POP  HL
         JRC  TYP3      EOF, BAC el ERR
         EX   DE,HL
         LD   (HL),10
         INC  HL
         SBC  HL,DE
         LD   C,L
         LD   B,H
         EX   DE,HL
         CALL 0:11
         CALL PAUS
         JRNC TYP1      Ej CTRL-C
TYP3     CP   186
         JRZ  TYP4      Kanske BAC-fil
         AND  A
         JPNZ ERROR     Ej EOF
         LD   DE,8:144
         CALL FILCONTR  Close
         JPC  ERROR
         CALL 7:66
         RET

TYP4     LD   L,(IX+8)
         LD   H,(IX+9)
         LD   A,(HL)
         INC  HL
         CP   130
         JRZ  TYP5
         CP   131
         JRNZ TYP3      Ej BAC-fil
TYP5     LD   A,(HL)
         AND  A
         JRZ  TYP6      Forts n{sta blk
         DEC  A
         JRZ  TYP3      Slut p} pgm
         LD   DE,BUFF6
         PUSH DE
         PUSH IX
         CALL 17:149    \vers{tt BAC-rad
         POP  IX
         EX   (SP),HL
         EX   DE,HL
         LD   (HL),10   LF sist i rad
         INC  HL
         AND  A
         SBC  HL,DE
         LD   C,L
         LD   B,H
         EX   DE,HL
         CALL 0:11
         CALL PAUS
         POP  HL        N{sta rad
         JRNC TYP5
         JR   TYP3

TYP6     LD   DE,8:153
         CALL FILCONTR  L{s n{sta block
         JRNC TYP5
         JR   TYP3

DELETE   PUSH HL
         CALL CLOSE
         POP  HL
         CALL DRIVEFIX
         LD   A,21
         JPC  ERROR
DEL1     LD   A,(WILDFLG)
         AND  A
         JRZ  DEL4      Ej wildcard
         BIT  7,A
         JRNZ DEL3      Drive initierad
         SET  7,A
         LD   (WILDFLG),A
DEL2     LD   BC,(WILDANT)
         DEC  B
         RETM           Slut p} drivear
         LD   A,C       Drivenummer
         INC  C
         LD   (WILDANT),BC
         CALL LIBINIT
         JRC  DEL2
         CALL FIRSTFIL
         JRC  DEL2
         JRZ  DEL2      Drive tom
         JR   DEL4

DEL3     CALL NEXTFIL
         JRC  DEL2
         JRZ  DEL2      N{sta drive
DEL4     LD   HL,DEL6
         LD   BC,DEL7-DEL6
         CALL 0:11
         LD   HL,255:128 Filnamn
         LD   BC,16
         PUSH HL
         CALL 0:11
         POP  HL
         RST  32
         CALL OPEN
         JRC  DEL5
         LD   DE,8:159
         CALL FILCONTR  Delete fil
DEL5     CALLC ERROR
         CALL PAUS
         RETC           CTRL-C
         LD   A,(WILDFLG)
         AND  A
         JRNZ DEL1
         RET

DEL6     DM   13,10,"delete "
DEL7

CLOSE    LD   A,(LOG)
         AND  A
         JRNZ CL1
         LD   A,(DMP)
         AND  A
         RETZ
CL1      LD   HL,CL2
         LD   BC,CL3-CL2
         CALL 0:11
         XOR  A
         LD   (LOG),A
         LD   (DMP),A
         LD   IX,FIL
         LD   DE,8:127
         CALL FILCONTR  Close
         JPC  ERROR
         CALL 7:66
         RET

CL2      DB 13,10
         DM "Closing DUMP/LOG file"
CL3

LOGG     PUSH HL
         CALL CLOSE
         POP  HL
         CALL PREPARE
         JPC  ERROR
         LD   HL,BUFF3
         LD   (LOGBUFF),HL
         LD   IX,KERMAREA
         LD   (IX+29),1 Textfil
         LD   A,255
         LD   (LOG),A
         RET

DUMP     PUSH HL
         CALL CLOSE
         POP  HL
         CALL OPEN
         JPC  ERROR
         LD   IX,KERMAREA
         XOR  A
         LD   (IX+20),A Kommando DUMP
         LD   (IX+27),A Inget data l{st
         LD   (IX+29),1 Textfil
         DEC  A
         LD   (DMP),A
         RET

SAVE     PUSH HL
         CALL CLOSE
         POP  HL
         CALL PREPBAC
         JPC  ERROR
         LD   E,(IX+8)
         LD   D,(IX+9)
         LD   A,D
         AND  A
         LD   A,52
         JPZ  ERROR     Buffer saknas
         LD   HL,SAV2
         LD   BC,SAV3-SAV2
         LDIR
         EX   DE,HL
         LD   DE,PARMAREA
         LD   B,PARMLEN
SAV1     LD   (HL),","
         INC  HL
         LD   (HL),199
         INC  HL
         LD   A,(DE)
         INC  DE
         LD   (HL),A
         INC  HL
         LD   (HL),0
         INC  HL
         LD   (HL),186
         INC  HL
         DJNZ SAV1
         EX   DE,HL
         LD   HL,SAV3
         LD   BC,SAV8-SAV3
         LDIR
         CALL 8:156     Skriv block
         CALL 8:127     St{ng filen
         RET

SAV2     DB 130         BASIC-pgm

         DB 5*1+10
         DB 10,0,134,129
         DB 199,64,254,186
         DB ",",199,0,0,186
         DB 13

         DB 5*PARMLEN+10
         DB 20,0,134,129
         DB 199,128,255,186

SAV3     DB 13

SAV4     DB SAV7-SAV4
         DB 30,0,134,145        CHAIN
         DB 203,34,SAV6-SAV5    str{ng
SAV5     DB "KERM.BAC"
SAV6     DB 187,13
SAV7     DB 1           Slut p} pgm
SAV8

PAUS     CALL 3:62
         SCF
         RETNZ          CTRL-C
         PUSH IX
         INP  56
         AND  127
         CP   32
         JRZ  PAUS1     Tangentbroms
         LD   A,(253:245)
         AND  128
         JRZ  PAUS2     Tangent ej nere
         CALL 0:2
PAUS1    CALL 0:2
PAUS2    POP  IX
         AND  A
         RET
