; DOS 1 DiskError Handler 
; Written by Jurgen Kramer - 11/06/93 -

CHPUT:  EQU   &HA2
CHGET:  EQU   &H9F
CR:     EQU   &H0D
LF:     EQU   &H0A
EOL:    EQU   &H00
BDOS:   EQU   &HF37D
ABRTH:  EQU   &HF1E6
ERRHA:  EQU   &HF323
IGNORE: EQU   0
RETRY:  EQU   1
ABORT:  EQU   2

        ORG   &HC000

INIT:   PUSH  BC
        PUSH  DE
        PUSH  HL
        LD    HL,(ERRHA)
        LD    (OLDH),HL       ; Save old Error Handler
        LD    HL,(ABRTH)
        LD    (OLDA),HL       ; Save old Abort Handler

        LD    HL,ERRNT
        LD    (ERRHA),HL
        LD    HL,MYABRT
        LD    (ABRTH),HL

;       LD    HL,BDOS
;       PUSH  HL
;       LD    DE,BDOSAV
;       LD    BC,3
;       LDIR
;       POP   HL
;       LD    DE,NWBDOS
;       LD    (HL),&HC3
;       INC   HL
;       LD    (HL),E
;       INC   HL
;       LD    (HL),D
        POP   HL
        POP   DE
        POP   BC
        RET

ST:     PUSH  AF
        PUSH  DE
        PUSH  HL
        LD    DE,EIGEN
        PUSH  DE
        LD    HL,DATA1
        LD    DE,&H8000
        CALL  DISKLD
        POP   DE
        CALL  PRINT
        POP   HL
        POP   DE
        POP   AF
        CALL  CHGET
        LD    C,0
        CALL  BDOS
        RET

ERRNT:  DEFW  MYERR
OLDH:   DEFW  0
OLDA:   DEFW  0

; New Error Handler        

MYERR:
        PUSH  AF              ; Save drive number
        PUSH  BC              ; Save Error Code [C]        
        BIT   7,C             ; Bad FAT?
        JR    NZ,BADFAT
        LD    A,C
        AND   &B00001110
        LD    E,A
        LD    D,0             ; DE = 2 * error code
        LD    HL,ERRPNT
        ADD   HL,DE           ; HL = Error Pointer
        LD    E,(HL)
        INC   HL
        LD    D,(HL)
        CALL  PRINT           ; Print error message        
DRIVE:  POP   BC              ; Return error code
        POP   AF              ; Restore drive #
        ADD   A,"A"           ; Scale drive number to ASCII
        LD    (T_WR+14),A
        LD    (T_RD+14),A
        BIT   0,C             ; Read Error?
        LD    DE,T_RD
        JR    Z,DO_PRT
        LD    DE,T_WR
DO_PRT: CALL  PRINT
WRONG:  LD    DE,RETXT
        CALL  PRINT           ; Print Retry text        
        CALL  CHGET           ; Wait for any key
        PUSH  AF
        LD    A,CR
        CALL  CHPUT
        LD    A,LF
        CALL  CHPUT
        POP   AF
        CP    "A"
        JR    Z,ABRT
        CP    "a"
        JR    Z,ABRT
        CP    "R"
        JR    Z,RETR
        CP    "r"
        JR    Z,RETR
        CP    "I"
        JR    Z,IGN
        CP    "i"
        JR    NZ,WRONG        ; No right choice was made

IGN:    LD    C,IGNORE
        RET
RETR:   LD    C,RETRY
        RET
ABRT:   LD    C,ABORT
        RET

BADFAT: LD    DE,BADTXT
        CALL  PRINT
        JR    DRIVE

PRINT:  LD    A,(DE)
        OR    A
        RET   Z
        INC   DE
        CALL  CHPUT
        JR    PRINT

MYABRT:
        LD    DE,DISKAB
        CALL  PRINT
        LD    SP,(ERRSTK)
        RET

NWBDOS:
;       LD    (ERRSTK),SP

BDOSAV:
        DEFB  0,0,0
ERRSTK: DEFW  0

; Error pointers

ERRPNT: DEFW  ERR1,ERR2,ERR3,ERR4,ERR5,ERR6,ERR7

; ErrorMessages

ERR1:   DEFB  CR,LF,"Write protected disk ",0
ERR2:   DEFB  CR,LF,"Not ready ",0
ERR3:   DEFB  CR,LF,"CRC error ",0
ERR4:   DEFB  CR,LF,"Seek error ",0
ERR5:   DEFB  CR,LF,"Record not found ",0
ERR6:   DEFB  CR,LF,"Write error ",0
ERR7:   DEFB  CR,LF,"Disk error ",0
T_WR:   DEFB  "writing drive  :",CR,LF,0
T_RD:   DEFB  "reading drive  :",CR,LF,0
RETXT:  DEFB  "Abort, Retry or Ignore (A/R/I)? ",0
BADTXT: DEFB  CR,LF,"Unsupported media ",0
DISKAB: DEFB  CR,LF,"*** Disk operation aborted",CR,LF,0

; DISK LOAD, LAADT FILES IN
;
; INPUT:   HL=adres waar data zich bevindt
;          DE=adres waar de geladen file komt
;
; data ziet er als volgt uit: DB drivenr,"filenaamext"
;
; (C) JURGEN KRAMER, FCS 1991, KMCS 1991
;

DISKLD: LD    (ERRSTK),SP
        PUSH  HL
        LD    C,&H1A          ; SET DMA adres
        CALL  BDOS
        LD    HL,FCB+0        ; CLEAR FCB
        LD    DE,FCB+1
        LD    BC,37-1
        LD    (HL),0
        LDIR
        POP   HL
        LD    DE,FCB+0        ; file naam
        LD    BC,12
        LDIR
        LD    DE,FCB          ; open file
        LD    C,&H0F
        CALL  BDOS

        LD    DE,FILEN
        OR    A               ; voor de error routine
        JR    NZ,ERROR

        LD    HL,0            ; random records = 0
        LD    (FCB+33),HL
        LD    (FCB+35),HL
        INC   L               ; record size = 1
        LD    (FCB+14),HL
        LD    HL,(FCB+16)     ; HL=file grootte
        LD    DE,FCB
        LD    C,&H27
        CALL  BDOS

        LD    DE,REDERR
        OR    A               ; voor error routine
        JR    NZ,ERROR

        LD    C,&H10          ; Close file
        LD    DE,FCB
        CALL  BDOS
        LD    DE,CLOSE
        OR    A
        JR    NZ,ERROR
        RET

ERROR:  CALL  PRINT
        RET

FILEN:  DEFB  "File not found!",CR,LF,EOL
REDERR: DEFB  "File could not be loaded!",CR,LF,EOL
CLOSE:  DEFB  "File could not be closed!",CR,LF,EOL
EIGEN:  DEFB  "Je moet wel in je eigen programma blijven, anders gaat het
        DEFB  " fout!",CR,LF,0
DATA1:  DEFB  0,"FILENAMEEXT"

FCB:    DEFS  37
e eigen programma blijven, anders gaat het
        DEFB  " fout!",CR,LF,0
DATA1:  DEFB  0,"FILENAMEEXT"

FCB