Av: Anders Franz`n <5258>    1985-05-19 00.38.35
; HEX.ASM
; Anders Franz`n <5258>
; Stockholm maj 1985


BUFF1    EQU  243:128
BUFF2    EQU  244:3
DEVROT   EQU  254:10
STACK    EQU  254:39


         ORG  240:0

START    LD   HL,START   Reservera area
         LD   (STACK),HL f|r programmet
         LD   HL,(DEVROT)
         LD   DE,L[NK
ST1      LD   A,H       Slut p}
         OR   L         enhets-lista ?
         JRZ  ST2       Ja, hoppa
         PUSH HL
         AND  A
         SBC  HL,DE     Finns HEX: redan
         POP  HL        i listan ?
         RETZ           Ja, till BASIC
         LD   A,(HL)    Adress till
         INC  HL        n{sta l{nk
         LD   H,(HL)
         LD   L,A
         JR   ST1       Leta vidare
ST2      LD   HL,(DEVROT)
         LD   (L[NK),HL Spara HEX: i
         LD   HL,L[NK   enhetslistan
         LD   (DEVROT),HL
         RET

L[NK     DEFW 0
         DEFM "HEX"
         DEFW JPTAB

JPTAB    JP   OPEN
         JP   PREPARE
         JP   CLOSE
         JP   0:21      INPUT blockning
         JP   0:27      PRINT blockning
         JP   BLOCKIN
         JP   BLOCKUT
         JP   7:39      DELETE finns ej
         JP   7:39      RENAME finns ej

PARMBLK  DEFS 15
CHSUMMA  DEFW 0

OPEN     CALL FILNAMN   Fixa filnamnet
         PUSH IX
         LD   IX,PARMBLK
         XOR  A         Kod f|r OPEN
         CALL 0:62      \ppna infilen
         LD   HL,BUFF1
         LD   BC,120
         CALL 8:147     L{s in en rad
         POP  IX
         JPC  ERROR     Hoppa om EOF
         LD   HL,OP2
         LD   BC,OP3-OP2
         CALL 0:11
         LD   HL,BUFF1
OP1      LD   BC,1
         CALL 0:11
         LD   A,(HL)
         CP   13
         JRNZ OP1
         CALL PEKARE
         RET
OP2      DEFM 13,10,"KONVERTERAR "
OP3      EQU  $


PREPARE  CALL FILNAMN   Fixa filnamnet
         PUSH HL
         LD   DE,BUFF1
         LD   BC,8
         LDIR           Spara filnamn
         LD   A,13      utan extension
         LD   (DE),A
         INC  DE
         LD   A,10
         LD   (DE),A
         POP  HL        HL --> filnamn
         PUSH IX
         LD   IX,PARMBLK
         LD   A,1       Kod f|r PREPARE
         CALL 0:62      \ppna filen
         LD   HL,BUFF1  HL --> filnamn
         LD   BC,10
         CALL 8:150     Skriv utrad
         POP  IX
         CALL PEKARE
         RET

FILNAMN  LD   A,(DE)    [r filnamn
         CP   32        angivet ?
         JRNZ FILN1     Ja, hoppa
         PUSH DE
         LD   HL,FILN5
         LD   BC,4
         LDIR           Kopiera in CAS:
         POP  DE
FILN1    LD   HL,8
         ADD  HL,DE     HL --> extension
         LD   A,(HL)
         CP   "B"       L{gg in default
         JRNZ FILN3     extension HEX
         INC  HL        om angiven
         LD   A,(HL)    extension {r
         CP   "A"       BAC eller BAS
         JRNZ FILN3
         INC  HL
         LD   A,(HL)
         CP   "C"
         JRZ  FILN2
         CP   "S"
         JRNZ FILN2
FILN2    LD   HL,FILN4  Ny extension
         CALL 8:189     Kopiera in HEX
FILN3    LD   HL,11
         ADD  HL,DE     L{gg ner CR
         LD   (HL),13   efter filnamn
         PUSH DE        Spara filnamn
         LD   E,L
         LD   D,H
         INC  DE
         LD   BC,4
         LDDR           G|r plats f|r
         LD   A,"."     punkten samt
         LD   (DE),A    l{gg ner den
         POP  HL        HL --> filnamn
         RET
FILN4    DEFM "HEX"     Default extens
FILN5    DEFM "CAS:"

PEKARE   LD   HL,0
         LD   (CHSUMMA),HL
         LD   HL,BUFF2  Buffertadress
         LD   (IX+7),120
         LD   (IX+8),L
         LD   (IX+9),H
         LD   (IX+10),L
         LD   (IX+11),H
         LD   (IX+13),253
         LD   (IX+14),0
         LD   (HL),3
         EX   DE,HL     DE --> buffer
         AND  A
         RET

CLOSE    LD   A,(IX+14) Har vi skrivit
         AND  A         p} filen ?
         PUSH IX
         LD   IX,PARMBLK
         JPNZ CL5       Ja, hoppa
         LD   HL,BUFF1
         LD   BC,120
         CALL 8:147     L{s rad i infil
         LD   HL,CL2
         LD   BC,CL3-CL2
         JRC  CL1       Hoppa om EOF
         LD   HL,BUFF1  HL --> inrad
         PUSH HL
         LD   BC,10     [r l{ngden p}
         LD   A,13      inraden st|rre
         CPIR           {n 10 tkn l}ng?
         POP  DE        DE --> inrad
         LD   HL,CL2
         LD   BC,CL3-CL2
         JRNZ CL1       Ja, hoppa
         EX   DE,HL     HL --> inrad
         LD   A,(24:65)
         CP   231
         PUSH AF
         CALLZ 24:65
         POP  AF
         CALLNZ 24:63
         LD   HL,(CHSUMMA)
         AND  A
         SBC  HL,DE
         LD   HL,CL3
         LD   BC,CL4-CL3
         JRNZ CL1       Checksumman fel
         LD   HL,CL4
         LD   BC,CL5-CL4
CL1      CALL 0:11
         CALL 8:127     St{ng infilen
         POP  IX
         RET
CL2      DEFB 7,13,10
         DEFM "CHECKSUMMA SAKNAS."
CL3      DEFB 7,13,10
         DEFM "CHECKSUMMAFEL "
         DEFM "I HEXFILEN."
CL4      DEFB 13,10
         DEFM "HEXFILEN KONVERTERAD."
CL5      LD   HL,(CHSUMMA)
         LD   DE,BUFF1
         PUSH DE
         LD   A,(24:85)
         CP   203
         PUSH AF
         CALLZ 24:85    G|r checksumman
         POP  AF        till en ASCII-
         CALLNZ 24:83   str{ng
         POP  HL
         EX   DE,HL
         LD   (HL),13
         INC  HL
         LD   (HL),10
         INC  HL
         AND  A
         SBC  HL,DE
         EX   DE,HL     HL --> rad
         LD   C,E       BC = l{ngd p}
         LD   B,D            raden
         CALL 8:150     Skriv ut rad
         CALL 8:127     St{ng utfilen
         POP  IX
         LD   HL,CL6
         LD   BC,CL7-CL6
         CALL 0:11
         RET
CL6      DEFB 13,10
         DEFM "HEXFIL SKAPAD."
CL7      EQU  $

BLOCKIN  LD   L,(IX+8)
         LD   H,(IX+9)  HL --> buffer
BLIN1    PUSH IX
         PUSH HL
         LD   IX,PARMBLK
         LD   HL,BUFF1
         LD   BC,120
         CALL 8:147     L{s rad i infil
         POP  HL
         POP  IX
         RETC           Retur om fel
         LD   DE,BUFF1  DE --> inrad
BLIN2    CALL BLIN4     Kolla n{sta tkn
         JPC  ERROR     Hoppa om fel
         RLC  A
         RLC  A         Multiplicera
         RLC  A         A med 16
         RLC  A
         LD   C,A       Spara resultat
         CALL BLIN4     Kolla n{sta tkn
         JPC  ERROR     Hoppa om fel
         ADD  C         Fixa v{rde
         LD   (HL),A    Spara byte
         PUSH HL
         LD   HL,(CHSUMMA)
         LD   C,A
         LD   B,0
         ADD  HL,BC
         LD   (CHSUMMA),HL
         POP  HL
         INC  L         Buffert full?
         JRZ  BLIN3     Ja, hoppa
         LD   A,(DE)
         CP   13        [r inrad slut?
         JRNZ BLIN2     Nej, n{sta byte
         JR   BLIN1     Ja, l{s ny inrad
BLIN3    LD   L,(IX+8)
         LD   H,(IX+9)  HL --> buffer
         RET
BLIN4    LD   A,(DE)    Tag tkn i inrad
         INC  DE        Uppdatera pos
         CP   "."       Punkt ?
         JRZ  BLIN4     Ja, skippa den
         SUB  48        ASCII --> bin{rt
         RETC           Fel om x < noll
         CP   10
         CCF
         RETNC          Ok om x < 10
         SUB  7
         RETC           Fel om x < "A"
         CP   16
         CCF            Fel om x > "F"
         RET            Ok annars !

ERROR    PUSH IX
         LD   IX,PARMBLK
         CALL 8:127     St{ng infilen
         POP  IX
         LD   HL,ERR1
         LD   BC,ERR2-ERR1
         CALL 0:11
         JP   0:201
ERR1     DEFM 7,13,10,"FELAKTIGT "
         DEFM "FORMAT P] HEXFILEN."
ERR2     EQU  $

BLOCKUT  LD   L,(IX+8)
         LD   H,(IX+9)  HL --> buffer
         PUSH IX
         LD   IX,PARMBLK
         LD   C,9       Nio rader
BLUT1    LD   B,26      26 tecken/rad
         CALL BLUT2     Skriv rad
         DEC  C         Fler rader
         JRNZ BLUT1     Ja, upprepa
         LD   B,19      Rad med 19 tkn
         CALL BLUT2     Skriv rad
         POP  IX
         LD   E,(IX+8)
         LD   D,(IX+9)  DE --> buffer
         LD   (IX+13),253 Kvar i buffer
         SET  0,(IX+14) Buffer utskriven
         RET
BLUT2    PUSH BC        Spara register
         LD   DE,BUFF1  DE --> plats f|r
         PUSH DE               utraden
BLUT3    LD   A,(HL)    Tecken i buffer
         PUSH HL
         PUSH BC
         LD   HL,(CHSUMMA)
         LD   C,A
         LD   B,0
         ADD  HL,BC
         LD   (CHSUMMA),HL
         POP  BC
         POP  HL
         RRC  A         Ber{kna A/16
         RRC  A
         RRC  A
         RRC  A
         CALL BLUT4     G|r ASCII
         LD   (DE),A    L{gg ner tecken
         INC  DE        i utraden
         LD   A,(HL)    Tecknet i buffer
         INC  HL
         CALL BLUT4     G|r ASCII
         LD   (DE),A    L{gg ner tecken
         INC  DE        i utraden
         DJNZ BLUT3     Upprepa
         LD   A,13      L{gg ner CR och
         LD   (DE),A    LF sist i utrad
         INC  DE
         LD   A,10
         LD   (DE),A
         INC  DE
         EX   (SP),HL   Spara buffer
         EX   DE,HL     DE --> utrad
         AND  A
         SBC  HL,DE
         LD   C,L       BC = l{ngd p}
         LD   B,H            utraden
         EX   DE,HL     HL --> utrad
         CALL 8:150     Skriv rad
         POP  HL        HL --> buffer
         POP  BC        ]terst{ll
         RET
BLUT4    PUSH HL
         PUSH DE
         LD   HL,BLUT5
         AND  15
         LD   E,A
         LD   D,0
         ADD  HL,DE
         LD   A,(HL)
         POP  DE
         POP  HL
         RET
BLUT5    DEFM "0123456789ABCDEF"

SLUT     END
