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

         ORG  251:0

START    LD   A,(254:64)
         AND  A
         JRNZ ST1       Inga parametrar
         LD   HL,255:128
         LD   DE,PARMAREA
         LD   BC,PARMLEN
         LDIR
ST1      LD   SP,STACK
         LD   HL,KERMIT
         PUSH HL
         LD   (KERMSP),SP
         LD   SP,PGM
         LD   (254:39),SP S{nk stack
         CALL INIT      Init av terminal
         XOR  A
         LD   (FIL+5),A Fil ej |ppen!!
         LD   HL,TERMBILD
         LD   E,L
         LD   D,H
         LD   (HL),32
         INC  DE
         LD   BC,24*80-1
         LDIR
         LD   HL,KERMBILD
         LD   E,L
         LD   D,H
         LD   (HL),32
         INC  DE
         LD   BC,24*80-1
         LDIR
         LD   HL,TXT1
         LD   DE,TERMBILD
         LD   BC,TXT2-TXT1
         LDIR
         LD   DE,KERMBILD
         LD   BC,TXT3-TXT2
         LDIR
         LD   HL,(254:32) HEAP
         LD   DE,PGM
         SBC  HL,DE
         JRNC ST4       Program f|rst|rt
         LD   IX,(254:28) BOFA
         LD   HL,(254:28)
ST2      LD   A,(HL)
         CP   1
         JRZ  ST4       Slut p} program
         LD   C,A
         LD   A,(IX+3)
         CP   134
         JRNZ ST3
         LD   A,(IX+4)
         CP   145
         JRZ  ST5       CHAIN 'program'
ST3      ADD  IX,BC
         ADD  HL,BC
         JR   ST2

ST4      CALL 10:112    NEW
         LD   SP,STACK
         JP   KERMIT

ST5      LD   C,5
         ADD  HL,BC
         EX   DE,HL     DE --> uttryck
         LD   A,(13:63)
         CP   255
         JPZ  13:63     CHAIN
         JP   13:61

TXT1     DM "Terminal"
TXT2     DM "K E R M I T"
         DM "   for ABC80"
         DM "   Stockholm 1987"
TXT3

         ORG  PGM

         DEFM "5  KERMIT    "
         JP   TEDFIX    Retur fr}n TED
         JP   TEDFIX1   Retur fr}n BASIC
R[KNA    JP   192:0
TILLBUFF JP   192:3
SPARRAD  JP   192:6

TEDFIX   LD   A,(TEDFLG)
         AND  A
         JRNZ TEDFIX1   Retur till KERM
         POP  AF
         PUSH AF
         SUB  4
         LD   (R[KNA+2),A
         LD   (TILLBUFF+2),A
         LD   (SPARRAD+2),A
         LD   (TEDFLG),A TED i minnet!
         LD   IX,248:0  TED's LIB-rutin
         LD   HL,TEDLIB
         LD   (IX+14),L Ny LIB-rutin
         LD   (IX+15),H f|r TED!
         CALL INIT      Init av terminal
         LD   (TEDSP),SP
         LD   SP,STACK
         JP   KERMIT

TEDFIX1  LD   (TEDSP),SP
         LD   SP,(KERMSP)
         RET

TEDSP    DW 0
KERMSP   DW 0

TERMBILD DS 24*80
         DW 0:2
KERMBILD DS 24*80
         DW 0:2

VISABILD LD   IX,3:116
         LD   B,24
VB1      PUSH BC
         LD   E,(IX+0)
         LD   D,(IX+1)
         INC  IX
         INC  IX
         LD   A,(1:216) 40 eller 80
         LD   C,A
         LD   B,0
         LDIR
         CP   40
         JRNZ VB2
         LD   C,A
         ADD  HL,BC
VB2      POP  BC
         DJNZ VB1
         LD   E,(HL)
         INC  HL
         LD   D,(HL)
         LD   (253:243),DE Mark|r
         RET

SPARBILD LD   IX,3:116
         LD   B,24
SB1      PUSH BC
         LD   L,(IX+0)
         LD   H,(IX+1)
         INC  IX
         INC  IX
         LD   A,(1:216) 40 eller 80
         LD   C,A
         LD   B,0
         LDIR
         CP   40
         JRNZ SB2
         LD   L,E
         LD   H,D
         LD   (HL),32
         INC  DE
         LD   BC,39
         LDIR
         INC  HL
SB2      POP  BC
         DJNZ SB1
         LD   HL,(253:243) Mark|r
         EX   DE,HL
         LD   (HL),E
         INC  HL
         LD   (HL),D
         RET

;Filhantering, tar hand om ev fel
FILCONTR PUSH HL
         LD   HL,FILC3
         EX   (SP),HL
         LD   (254:54),SP RUN-timestack
         PUSH HL
         LD   HL,FILC2
         LD   (IY+30),L ONERRORGOTO
         LD   (IY+31),H
         LD   (IY+14),0 RUN-mode
         LD   (IY+15),0 NOTRACE
         LD   HL,FILC1
         EX   (SP),HL
         PUSH DE
         RET

FILC1    INC  SP
         INC  SP
         RET

FILC2    DB   4,0,1,145 BASIC-return

FILC3    LD   A,(IY+34) Errcode
         AND  127
         SCF
         RET

PREPARE  LD   A,1
         JR   OPEN1

PREPBAC  LD   A,4
         JR   OPEN1

OPEN     XOR  A
OPEN1    LD   IX,FIL
         LD   DE,0:62
         CALL FILCONTR
         RET

ERROR    AND  127
         LD   L,A
         LD   H,0
         PUSH HL
         LD   HL,ERROR1
         LD   BC,ERROR2-ERROR1
         CALL 0:11
         POP  HL
         CALL SKRIVTAL
         CALL 7:66
         RET

ERROR1   DM 13,10,7,"ERR "
ERROR2

;Omvandla str{ng i buffert till tal DE
G\RTAL   LD   A,(24:30)
         CP   126
         JPZ  24:30
         JP   24:28

;Omvandla tal till str{ng i buffert
SKRIVTA1 LD   DE,253:213 Tempbuffer
         PUSH DE
         LD   A,(24:96) Kolla checksumma
         CP   175
         PUSH AF
         CALLZ 24:96    \vers{tt till
         POP  AF        ASCII
         CALLNZ 24:98
         EX   DE,HL
         POP  DE
         AND  A         Ber{kna l{ngd
         SBC  HL,DE     p} str{ngen
         LD   C,L
         LD   B,H
         EX   DE,HL
         RET

;Skriv ut talet HL
SKRIVTAL CALL SKRIVTA1
         CALL 0:11      Skriv ut str{ng
         RET

;Skriver talet HL som max kan best}
;av x tecken. X lagras i A innan anrop
;varefter talet skrivs ut h|gerst{llt.
SKRTAL   PUSH DE        Spara utfyllnad
         PUSH AF
         CALL SKRIVTA1
         POP  AF
         EX   (SP),HL
         PUSH BC
         SUB  C
         LD   C,A
         CALLNZ 0:11    Skriv utfyllnad
         POP  BC
         POP  HL
         CALL 0:11
         RET

;Kollar om wildcard finns. Fixar
;i ordning filnamn.
DRIVEFIX RST  32
         LD   (WILDSTR),HL
         LD   BC,7:0
         LD   (WILDANT),BC
         PUSH HL
         LD   BC,0:255
         DEC  HL
DF1      LD   E,A       Spara f|reg tkn
         RST  24
         INC  C         R{kna antal tkn
         CP   "*"
         JRZ  DF2
         CP   "?"
         JRNZ DF3
DF2      INC  B         R{kna wildcards
DF3      CP   ","
         JRNZ DF4
         LD   A,13
         LD   (HL),A    Byt "," till CR
         INC  HL        Filnamn-2
DF4      CP   13
         JRNZ DF1
         RST  32
         LD   (FILPEK),HL Spara fil-2
         POP  HL
         LD   A,E       Sista tecknet
         CP   ":"
         JRZ  DF5       Endast enhet!
         LD   A,B
         LD   (WILDFLG),A
         AND  A
         JRZ  DF6       Inga wildcards!
DF5      LD   A,(HL)
         AND  95
         XOR  "D"
         RETNZ
         RST  24
         AND  95
         XOR  "R"
         RETNZ
         RST  24
         SUB  48
         CCF
         RETNC
         CP   8
         RETNC
         LD   C,A       Spara drive-nr
         RST  24
         XOR  ":"
         RETNZ
         RST  24
         LD   (WILDSTR),HL Skippa enhet
         LD   B,1          En drive
         LD   (WILDANT),BC
         AND  A
         RET

DF6      LD   DE,255:150 BC=l{ngd
         CALL 0:65      Fixa filnamn
         RETC           Fel!
         LD   HL,255:150+11
         LD   DE,255:128
         LD   BC,3
         LD   A,(HL)
         LDIR
         CP   32
         JRZ  DF7       Enhet saknas
         LD   A,":"
DF7      LD   (DE),A
         INC  DE
         LD   HL,255:150
         LD   C,8
         LDIR
         LD   A,"."
         LD   (DE),A
         INC  DE
         LD   C,3
         LDIR
         LD   A,13
         LD   (DE),A
         AND  A
         RET


;J{mf|r str{ng med wildcard, om str{ng
;passar s{tts carry. Rekursiv rutin.
;DE! str{ng
WILDCARD LD   HL,(WILDSTR)
WC1      RST  32
         EX   DE,HL
         RST  32
         EX   DE,HL
         CP   13
         JRNZ WC2
         LD   A,(HL)
         CP   13
         SCF
         RETZ
         XOR  "*"
         RETNZ
         PUSH HL
         RST  24
         POP  HL
         XOR  13
         RETNZ
         SCF
         RET

WC2      LD   A,(HL)
         CP   13
         RETZ
         LD   A,(DE)
         CP   96
         JRC  WC3
         AND  95
WC3      CP   (HL)
         JRNZ WC5
WC4      PUSH HL
         PUSH DE
         INC  HL
         INC  DE
         CALL WC1
         POP  DE
         POP  HL
         RET

WC5      LD   A,(HL)
         CP   "?"
         JRZ  WC4
         XOR  "*"
         RETNZ
         PUSH HL
         PUSH DE
         INC  HL
         CALL WC1
         POP  DE
         POP  HL
         RETC
         PUSH HL
         PUSH DE
         INC  DE
         CALL WC1
         POP  DE
         POP  HL
         RET

WILDSTR  DW 0   Pekar p} wildcard
WILDANT  DW 0   C=n{sta drive B=antal
WILDFLG  DB 0   Finns wildcards?
FILPEK   DW 0   Peka p} ev filnamn-2

;Leta efter ord i tabell
STRSCAN  LD   C,0
         PUSH DE
         CALL STR
         POP  DE
         RETZ           Hittat
         RETM           Finns ej
         PUSH HL
         PUSH DE
         LD   HL,STRSC1
         LD   BC,STRSC2-STRSC1
         CALL 0:11
         POP  DE
         POP  HL
         LD   C,128
         CALL STR
         CALL 7:66
         SCF
         RET

STRSC1   DM 13,10,7,"Be more specific"
STRSC2

;Leta i tabell
;IN HL! text
;   DE! tabell
;    C  0=leta 128=skriv alla ounika
;UT  Z  funnen
;    M  ej hittad
;    P  ej unikt
;    A  ev kod
;   HL  ev n{sta ord
STR      RST  32
         XOR  13
         DEC  A
         RETM           Tom textrad
         EXX
         LD   B,1       CRLF f|re f|rsta
         EXX
         EX   DE,HL     HL! tabell
STR1     LD   B,(HL)    Spara tabellkod
         LD   (STR8),HL Spara tabellord
         PUSH DE        Spara text
STR2     LD   A,(DE)    Tecken i text
         INC  DE
         CP   96
         JRC  STR3
         AND  95        G|r versal
STR3     INC  HL
         CP   (HL)      J{mf|r tkn
         JRZ  STR2      Lika
         CP   32
         JRZ  STR4
         CP   ","
         JRZ  STR4
         CP   13
         JRNZ STR6      Textord ej slut
STR4     DEC  DE        Peka p} tkn
         LD   (STR9),DE Spara n{sta ord
         LD   A,B
         EX   AF        Spara kod & NC!
         INC  C         R{kna tr{ffar
         BIT  7,C
         JRZ  STR6      Skriv ej ounika
         EXX
         DJNZ STR5
         CALL 7:66
         LD   B,3       Tre ord/rad
         LD   A,(1:216)
         CP   40
         JRZ  STR5
         LD   B,5
STR5     LD   HL,(STR8)
         LD   A,-10     Rak v{nsterkant
         LD   DE,SPACE  Avskiljare
         CALL SKRIVTAB  Skriv tabellord
         EXX
STR6     LD   A,(HL)
         INC  HL
         BIT  7,A
         JRZ  STR6      Leta tabordslut
         DEC  HL
         INC  A
         JRZ  STR7      Slut p} tabell
         POP  DE        Peka p} text
         JR   STR1

STR7     POP  DE        Text
         LD   HL,(STR9) N{sta ord
         RST  32
         EX   AF        ]terst{ll kod
         AND  A         Clear carry
         RES  7,C
         DEC  C
         RETZ           Hittad (NC)
         EX   DE,HL     HL! text
         RET

STR8     DW 0
STR9     DW 0


;Spara tecken i TED
TEDSPAR  LD   A,(TEDFLG)
         AND  A
         RETZ           TED ej i minnet
         LD   IX,TEDKOL
         LD   A,(SLASK)
         CP   32
         JRNC TS2
         CP   9
         JRZ  TS3
         CP   10
         JRZ  TS5
         CP   12
         JRZ  TS4
         CP   13
         JRZ  TS1
         CP   8
         RETNZ
         DEC  (IX+0)
         RETP
TS1      LD   (IX+0),0
         RET

TS2      LD   HL,FLAGG
         SET  5,(HL)    Data i buffer
         LD   HL,BUFFTED
         LD   E,(IX+0)
         LD   D,0
         ADD  HL,DE
         LD   (HL),A    Spara tecken
TS3      INC  (IX+0)    \ka kolumn
         LD   A,(IX+0)
         CP   160
         RETNZ          Rad ej full
TS4      LD   (IX+0),0
TS5      LD   DE,(TEDINRAD)
         PUSH DE
         LD   HL,(253:243)
         PUSH HL        Spara mark|r
         CALL SPARRAD
         POP  HL
         POP  DE
         JRC  TS6
         INC  DE
         LD   (TEDINRAD),DE
         CALL TILLBUFF
         LD   HL,FLAGG
         RES  5,(HL)    Buffer t|md
         RET

TS6      LD   (253:243),HL
         XOR  A
         LD   (TEDSAVE),A
         LD   DE,(3:162)
         CALL 2:103     Blanka rad 23
         LD   HL,FLAGG
         SET  0,(HL)    L{mna terminal
         SET  6,(HL)    TED full
         LD   A,3
         LD   (FELKOD),A
         RET

TEDSEND  LD   A,(TEDSAVE)
         XOR  2
         RETNZ
         LD   A,(TEDFLG)
         AND  A
         RETZ           TED ej i minnet
         LD   A,(TEDKOL)
         AND  A
         JRNZ TSE4      TED-buff ej tom
         LD   HL,(TEDANRAD)
         LD   A,H
         OR   L
         JRNZ TSE1      Fler rader kvar
         XOR  A
         LD   (TEDSAVE),A
         RET

TSE1     DEC  HL
         LD   (TEDANRAD),HL
         LD   HL,BUFFTED
         LD   (TEDUTPOS),HL
         LD   DE,(TEDUTRAD)
         INC  DE
         LD   (TEDUTRAD),DE
         CALL TILLBUFF
         LD   HL,BUFFTED+159
         LD   B,160
         LD   A,32
TSE2     CP   (HL)
         DEC  HL
         JRNZ TSE3
         DJNZ TSE2
         DEC  HL
TSE3     INC  HL
         INC  HL
         LD   (HL),13
         LD   A,B
         INC  A         R{kna med CR
TSE4     DEC  A
         LD   (TEDKOL),A
         LD   HL,(TEDUTPOS)
         LD   A,(HL)
         INC  HL
         LD   (TEDUTPOS),HL
         SCF            Flagga
         RET

TEDINRAD DW 0
TEDUTRAD DW 0
TEDANRAD DW 0
TEDUTPOS DW 0
TEDKOL   DB 0
