; Disassembly of the file "mz-1e14.rom"
; 
; CPU Type: Z80
; 
; Created with dZ80 2.1
; 
; on Saturday, 14 of March 2026 at 12:27 PM
; 
MMIO_E0     EQU     0E0H
SIO_CHA_D   EQU     0F4H
SIO_CHA_C   EQU     0F6H
SIO_CHB_C   EQU     0F7H
PPI_PORTB   EQU     0FEH
PPI_PORTC   EQU     0FFH
GETL        EQU     00003H
NL          EQU     00009H
PRNTS       EQU     0000CH
PRNT        EQU     00012H
MSG         EQU     00015H
MSGX        EQU     00018H
GETKY       EQU     0001BH
BRKEY       EQU     0001EH
XTEMP       EQU     00041H
MONSTART    EQU     0005BH
ERRORMSG    EQU     00147H
PRTHLA      EQU     002A6H
CLSSCR      EQU     00308H
PRTHX       EQU     003B1H
HLHEX       EQU     00410H
_2HEX       EQU     0041FH
WRINF       EQU     00436H
WRDAT       EQU     00475H
RDINF       EQU     004D8H
RDDAT       EQU     004F8H
VRFY        EQU     00588H
CLSDN       EQU     005FAH
?KEY        EQU     008CAH
PRNT4       EQU     00920H
SAVEDMSG    EQU     00942H
PRNT3       EQU     0096CH
FNAMMSG     EQU     0098BH
LOADINGMSG  EQU     009A0H
SPCCHK      EQU     00A32H
?ADCN       EQU     00BB9H
?BLNK       EQU     00DA6H
?DPCT       EQU     00DDCH
STACK2      EQU     010EEH
ATRB        EQU     010F0H
NAME        EQU     010F1H
SIZE        EQU     01102H
DTADR       EQU     01104H
EXADR       EQU     01106H
COMNT       EQU     01108H
QDCMD       EQU     01130H
QDSUBCMD    EQU     01131H
QDLOADADDR  EQU     01132H
QDBLKSIZE   EQU     01134H
QDRELOC     EQU     0113AH
QDDISPFLG   EQU     0113BH
QDADDRMARK  EQU     0113CH
QDEXPMARK   EQU     0113DH
QDWORK1     EQU     0113EH
QDWORK2     EQU     01140H
QDTOTBLKS   EQU     01141H
QDREMBLKS   EQU     01142H
QDMOTOR     EQU     01143H
QDRETRY     EQU     01144H
QDFLAGS     EQU     01146H
QDSAVESP    EQU     01147H
DSPXY       EQU     01171H
DPRNT       EQU     01194H
SWRK        EQU     0119DH
BUFER       EQU     011A3H
BUFER10     EQU     011ADH
USERRAM     EQU     01200H
PRGSTART    EQU     0E800H

            ORG     PRGSTART

            NOP                         ; MZ-1E14 QD Interface ROM - Entry point table
            JP      LE80A

            JP      LE884

LE807:      JP      LEDD1

LE80A:      LD      A,0C6H              ; Cold start: IPL boot from QD
            CALL    ?DPCT
            XOR     A
            LD      (DPRNT),A
            DI      
            XOR     A
            LD      DE,00000H
            CALL    CLSSCR
            LD      A,001H
            OUT     (SIO_CHB_C),A
            XOR     A
            OUT     (SIO_CHB_C),A
            CALL    GETKY
            CP      04DH
            JR      Z,LE880             ; (+057H)
            CP      051H
            JR      Z,LE835             ; (+008H)
            CALL    LEB22
            CALL    Z,LF006
            JR      LE835               ; (+000H)

LE835:      CALL    LEBD1
            CALL    NL
            CALL    LEC0C
            JR      C,LE868             ; (+028H)
            LD      A,00DH
            LD      (BUFER),A
            CALL    LED08
            LD      A,001H
            LD      (QDDISPFLG),A
            LD      HL,LE86B
            LD      SP,STACK2
            EX      (SP),HL
            CALL    LEBDC
            JP      C,LEBAC
            LD      A,(ATRB)
            CP      001H
            JR      NZ,LE871            ; (+010H)
            LD      DE,LEB27
            RST     018H
            JP      LEB7B

LE868:      LD      DE,LEB37
LE86B:      CALL    NL
            RST     018H
            JR      LE87D               ; (+00cH)

LE871:      LD      A,006H
            LD      (QDCMD),A
            CALL    LE807
            LD      DE,LED4C
            RST     018H
LE87D:      CALL    NL
LE880:      LD      DE,LEB45
            RST     018H
LE884:      CALL    NL                  ; Warm start: command prompt (* MONITOR 9Z-503M *)
            LD      A,02AH
            CALL    PRNT
            LD      DE,BUFER
            CALL    GETL
LE892:      LD      A,(DE)              ; Command parser: J=Jump, L=Load, F=Format, B=Bell, P=Print, M=Modify, S=Save, V=Verify, D=Dump, Q=QD sub-cmds
            INC     DE
            CP      00DH
            JR      Z,LE884             ; (-014H)
            CP      04AH
            JR      Z,LE910             ; (+074H)
            CP      04CH
            JR      Z,LE914             ; (+074H)
            CP      046H
            JR      Z,LE8DE             ; (+03aH)
            CP      042H
            JP      Z,LEA6D
            CP      023H
            JP      Z,LEA6A
            CP      050H
            JP      Z,LE968
            CP      04DH
            JP      Z,LE9D9
            CP      053H
            JP      Z,LEA0C
            CP      056H
            JP      Z,LEA5E
            CP      044H
            JP      Z,LEA78
            CP      051H
            JR      NZ,LE892            ; (-039H)
            LD      HL,00000H
            LD      (QDRELOC),HL
            LD      A,(DE)
            CP      04CH
            JP      Z,LEB5D
            CP      044H
            JP      Z,LEC18
LE8DC:      JR      LE884               ; (-05aH)

LE8DE:      LD      A,(DE)
            CP      00DH
            JR      NZ,LE8DC            ; (-007H)
            CALL    LEB22
            CALL    Z,LF006
            JR      LE8DC               ; (-00fH)

LE8EB:      CP      002H
            JR      Z,LE8DC             ; (-013H)
            CALL    NL
            LD      DE,ERRORMSG
            RST     018H
            JR      LE8DC               ; (-01cH)

LE8F8:      EX      (SP),HL
            POP     BC
            LD      DE,BUFER
            CALL    GETL
            LD      A,(DE)
            CP      01BH
            JR      Z,LE8DC             ; (-029H)
            JP      (HL)

LE906:      EX      (SP),IY
            POP     AF
            CALL    HLHEX
            JR      C,LE8DC             ; (-032H)
            JP      (IY)

LE910:      CALL    LE906
            JP      (HL)

LE914:      CALL    RDINF
            JR      C,LE8EB             ; (-02eH)
            CALL    NL
            LD      DE,LOADINGMSG
            RST     018H
            LD      DE,NAME
            RST     018H
            XOR     A
            LD      (BUFER),A
            LD      HL,(DTADR)
            LD      A,H
            OR      L
            JR      NZ,LE941            ; (+012H)
            LD      HL,(EXADR)
            LD      A,H
            OR      L
            JR      NZ,LE941            ; (+00bH)
            LD      A,PPI_PORTC
            LD      (BUFER),A
            LD      HL,USERRAM
            LD      (DTADR),HL
LE941:      CALL    RDDAT
            JR      C,LE8EB             ; (-05bH)
            LD      A,(BUFER)
            CP      PPI_PORTC
            JR      Z,LE954             ; (+007H)
            LD      BC,00100H
            LD      HL,(EXADR)
            JP      (HL)

LE954:      OUT     (MMIO_E0),A
            LD      HL,USERRAM
            LD      DE,00000H
            LD      BC,(SIZE)
            LDIR    
            LD      BC,00100H
            JP      00000H

LE968:      LD      A,(DE)
            CP      026H
            JR      NZ,LE983            ; (+016H)
LE96D:      INC     DE
            LD      A,(DE)
            CP      04CH
            JR      Z,LE989             ; (+016H)
            CP      053H
            JR      Z,LE98E             ; (+017H)
            CP      043H
            JR      Z,LE99E             ; (+023H)
            CP      047H
            JR      Z,LE997             ; (+018H)
            CP      054H
            JR      Z,LE993             ; (+010H)
LE983:      CALL    LE9B8
LE986:      JP      LE8DC

LE989:      LD      DE,00470H
            JR      LE983               ; (-00bH)

LE98E:      LD      DE,003D5H
            JR      LE983               ; (-010H)

LE993:      LD      A,004H
            JR      LE999               ; (+002H)

LE997:      LD      A,002H
LE999:      CALL    LE9A2
            JR      LE96D               ; (-031H)

LE99E:      LD      A,01DH
            JR      LE999               ; (-009H)

LE9A2:      LD      C,000H
            LD      B,A
            CALL    LE9C9
            LD      A,B
            OUT     (PPI_PORTC),A
            LD      A,080H
            OUT     (PPI_PORTB),A
            LD      C,001H
            CALL    LE9C9
            XOR     A
            OUT     (PPI_PORTB),A
            RET     

LE9B8:      PUSH    DE
            PUSH    BC
            PUSH    AF
LE9BB:      LD      A,(DE)
            CALL    LE9A2
            LD      A,(DE)
            INC     DE
            CP      00DH
            JR      NZ,LE9BB            ; (-00aH)
            POP     AF
            POP     BC
            POP     DE
            RET     

LE9C9:      IN      A,(PPI_PORTB)
            AND     00DH
            CP      C
            RET     Z
            CALL    BRKEY
            JR      NZ,LE9C9            ; (-00bH)
            LD      SP,ATRB
            JR      LE986               ; (-053H)

LE9D9:      CALL    LE906
LE9DC:      CALL    CLSDN
            CALL    PRTHX
            CALL    PRNT4
            CALL    LE8F8
            CALL    HLHEX
            JR      C,LEA08             ; (+01bH)
            CALL    PRTHLA
            INC     DE
            CALL    _2HEX
            JR      C,LE9DC             ; (-01aH)
            CP      (HL)
            JR      NZ,LE9DC            ; (-01dH)
            INC     DE
            LD      A,(DE)
            CP      00DH
            JR      Z,LEA05             ; (+006H)
            CALL    _2HEX
            JR      C,LE9DC             ; (-028H)
            LD      (HL),A
LEA05:      INC     HL
            JR      LE9DC               ; (-02cH)

LEA08:      LD      H,B
            LD      L,C
            JR      LE9DC               ; (-030H)

LEA0C:      CALL    LE906
            LD      (DTADR),HL
            LD      B,H
            LD      C,L
            CALL    PRTHLA
            CALL    LE906
            SBC     HL,BC
            INC     HL
            LD      (SIZE),HL
            CALL    PRTHLA
            CALL    LE906
            LD      (EXADR),HL
            CALL    NL
            LD      DE,FNAMMSG
            RST     018H
            CALL    LE8F8
            CALL    PRTHLA
            CALL    PRTHLA
            LD      HL,NAME
LEA3C:      INC     DE
            LD      A,(DE)
            LD      (HL),A
            INC     HL
            CP      00DH
            JR      NZ,LEA3C            ; (-008H)
            LD      A,001H
            LD      (ATRB),A
            CALL    WRINF
LEA4C:      JP      C,LE8EB
            CALL    WRDAT
            JR      C,LEA4C             ; (-008H)
            CALL    NL
            LD      DE,SAVEDMSG
            RST     018H
LEA5B:      JP      LE884

LEA5E:      CALL    VRFY
            JP      C,LE8EB
            LD      DE,SAVEDMSG
            RST     018H
            JR      LEA5B               ; (-00fH)

LEA6A:      JP      MONSTART

LEA6D:      LD      A,(SWRK)
            RRA     
            CCF     
            RLA     
            LD      (SWRK),A
LEA76:      JR      LEA5B               ; (-01dH)

LEA78:      CALL    LE906
            CALL    PRTHLA
            PUSH    HL
            CALL    HLHEX
            POP     DE
            JR      C,LEAD6             ; (+051H)
LEA85:      EX      DE,HL
LEA86:      LD      B,008H
            LD      C,017H
            CALL    CLSDN
LEA8D:      CALL    PRTHX
            INC     HL
            PUSH    AF
            LD      A,(DSPXY)
            ADD     A,C
            LD      (DSPXY),A
            POP     AF
            CP      020H
            JR      NC,LEAA0            ; (+002H)
            LD      A,02EH
LEAA0:      CALL    ?ADCN
            CALL    PRNT3
            LD      A,(DSPXY)
            INC     C
            SUB     C
            LD      (DSPXY),A
            DEC     C
            DEC     C
            DEC     C
            PUSH    HL
            SBC     HL,DE
            POP     HL
            JR      Z,LEAD3             ; (+01cH)
            LD      A,0F8H
            LD      (0E000H),A
            NOP     
            LD      A,(0E001H)
            CP      PPI_PORTB
            JR      NZ,LEAC7            ; (+003H)
            CALL    ?BLNK
LEAC7:      DJNZ    LEA8D               ; (-03cH)
LEAC9:      CALL    ?KEY
            OR      A
            JR      Z,LEAC9             ; (-006H)
            CALL    SPCCHK
            JR      NZ,LEA86            ; (-04eH)
            JR      LEA76               ; (-060H)

LEAD6:      LD      HL,000A0H
            ADD     HL,DE
            JR      LEA85               ; (-057H)

LEADC:      CALL    NL
            LD      DE,FNAMMSG
            RST     018H
            LD      DE,BUFER
            CALL    GETL
            LD      A,(DE)
            CP      01BH
            JR      NZ,LEAF3            ; (+005H)
            LD      HL,LE884
            EX      (SP),HL
            RET     

LEAF3:      LD      B,000H
            LD      DE,BUFER10
            LD      HL,BUFER
            LD      A,(DE)
            CP      00DH
            JR      Z,LEB20             ; (+020H)
LEB00:      CP      020H
            JR      NZ,LEB08            ; (+004H)
            INC     DE
            LD      A,(DE)
            JR      LEB00               ; (-008H)

LEB08:      CP      022H
            JR      Z,LEB14             ; (+008H)
LEB0C:      LD      (HL),A
            INC     HL
            INC     B
            LD      A,011H
            CP      B
            JR      Z,LEADC             ; (-038H)
LEB14:      INC     DE
            LD      A,(DE)
            CP      022H
            JR      Z,LEB1E             ; (+004H)
            CP      00DH
            JR      NZ,LEB0C            ; (-012H)
LEB1E:      LD      A,00DH
LEB20:      LD      (HL),A
            RET     

LEB22:      LD      A,(0F000H)          ; Check QD interface presence (read F000H)
            OR      A
            RET     

LEB27:      DB      "IPL IS LOADING "
            DB      00DH
LEB37:      DB      "MAKE READY QD"
            DB      00DH
LEB45:      DB      "**  MONITOR 9Z-503M  **"
            DB      00DH
LEB5D:      CALL    LEBD1               ; QL command: Load file from QD
            CALL    LEC0C
            JR      C,LEBAC             ; (+047H)
            CALL    LEADC
            CALL    LED08
            LD      DE,LOADINGMSG
            RST     018H
LEB6F:      CALL    LEBDC
            JR      C,LEBAC             ; (+038H)
            LD      A,(ATRB)
            CP      001H
            JR      NZ,LEB6F            ; (-00cH)
LEB7B:      LD      DE,NAME
            RST     018H
            LD      HL,(EXADR)
            LD      A,H
            OR      L
            JR      NZ,LEB8B            ; (+005H)
            LD      HL,(COMNT)
            LD      A,H
            OR      L
LEB8B:      JR      NZ,LEB97            ; (+00aH)
            LD      A,PPI_PORTC
            LD      (QDRELOC),A
            LD      HL,USERRAM
            JR      LEB9A               ; (+003H)

LEB97:      LD      HL,(EXADR)
LEB9A:      LD      (QDLOADADDR),HL
            LD      HL,(DTADR)
            LD      (QDBLKSIZE),HL
            LD      HL,00103H
            LD      (QDCMD),HL
            CALL    LE807
LEBAC:      JP      C,LEC51
            LD      A,(QDRELOC)
            CP      PPI_PORTC
            JR      Z,LEBBD             ; (+007H)
            LD      BC,00300H
            LD      HL,(COMNT)
            JP      (HL)

LEBBD:      OUT     (MMIO_E0),A
            LD      HL,USERRAM
            LD      DE,00000H
            LD      BC,(DTADR)
            LDIR    
            LD      BC,00300H
            JP      00000H

LEBD1:      XOR     A                   ; Init QD state variables (motor status, address marks)
            LD      (QDMOTOR),A
            LD      (QDWORK1),A
            LD      (QDWORK2),A
            RET     

LEBDC:      LD      HL,GETL             ; Read next header block from QD (mode 3)
            LD      (QDCMD),HL
            LD      HL,ATRB
            LD      (QDLOADADDR),HL
            LD      HL,00040H
            LD      (QDBLKSIZE),HL
LEBEE:      CALL    LE807
            RET     C
            LD      A,(BUFER)
            CP      00DH
            RET     Z
            LD      HL,BUFER
            LD      DE,NAME
            LD      B,011H
LEC00:      LD      A,(DE)
            CP      (HL)
            JR      NZ,LEBEE            ; (-016H)
            CP      00DH
            RET     Z
            INC     DE
            INC     HL
            DJNZ    LEC00               ; (-00bH)
            RET     

LEC0C:      XOR     A                   ; Start QD: motor on + status check (mode 1)
            LD      (QDSUBCMD),A
            INC     A
            LD      (QDCMD),A
            CALL    LE807
            RET     

LEC18:      CALL    LEBD1               ; QD command: Directory listing
            CALL    LEC0C
            JR      C,LEC51             ; (+031H)
            CALL    LED08
            LD      B,000H
            LD      DE,LED5D
            RST     018H
            LD      HL,0CD90H
LEC2C:      LD      (QDLOADADDR),HL
            LD      HL,GETL
            LD      (QDCMD),HL
            LD      HL,00040H
            LD      (QDBLKSIZE),HL
            PUSH    BC
            CALL    LE807
            POP     BC
            JR      C,LEC4C             ; (+00aH)
            INC     B
            LD      HL,(QDLOADADDR)
            LD      DE,PRNT
            ADD     HL,DE
            JR      LEC2C               ; (-020H)

LEC4C:      CP      028H
            JR      Z,LEC53             ; (+003H)
            SCF     
LEC51:      JR      C,LECD2             ; (+07fH)
LEC53:      LD      A,006H
            LD      (QDCMD),A
            PUSH    BC
            CALL    LE807
            POP     BC
            XOR     A
            CP      B
            JR      NC,LECC8            ; (+067H)
            CALL    NL
            LD      HL,0CD90H
LEC67:      LD      A,(HL)
            LD      DE,LED6E
            DEC     A
            JR      Z,LECA4             ; (+036H)
            LD      DE,LED79
            DEC     A
            JR      Z,LECA4             ; (+030H)
            LD      DE,LED84
            DEC     A
            JR      Z,LECA4             ; (+02aH)
            LD      DE,LED8F
            DEC     A
            JR      Z,LECA4             ; (+024H)
            LD      DE,LED9A
            DEC     A
            JR      Z,LECA4             ; (+01eH)
            DEC     A
            JR      Z,LECA1             ; (+018H)
            LD      DE,LEDA5
            DEC     A
            JR      Z,LECA4             ; (+015H)
            DEC     A
            JR      Z,LECA1             ; (+00fH)
            DEC     A
            JR      Z,LECA1             ; (+00cH)
            LD      DE,LEDB0
            DEC     A
            JR      Z,LECA4             ; (+009H)
            LD      DE,LEDBB
            DEC     A
            JR      Z,LECA4             ; (+003H)
LECA1:      LD      DE,LEDC6
LECA4:      RST     018H
            LD      A,022H
            CALL    PRNT
            INC     HL
            PUSH    HL
            POP     DE
            RST     018H
            LD      A,022H
            CALL    PRNT
            CALL    NL
            LD      DE,00011H
            ADD     HL,DE
LECBA:      CALL    ?KEY
            OR      A
            JR      Z,LECBA             ; (-006H)
            CALL    SPCCHK
            JP      Z,LE884
            DJNZ    LEC67               ; (-061H)
LECC8:      CALL    NL
            LD      DE,LED11
            RST     018H
            JP      LE884

LECD2:      LD      DE,LED15            ; Display QD error message based on error code in A
            CP      028H
            JR      Z,LECF1             ; (+018H)
            LD      DE,LED33
            CP      032H
            JR      Z,LECF1             ; (+011H)
            LD      DE,LED40
            CP      036H
            JR      Z,LECF1             ; (+00aH)
            LD      DE,LED14
            CP      000H
            JR      Z,LECF1             ; (+003H)
            LD      DE,LED27
LECF1:      LD      A,006H
            LD      (QDCMD),A
            CALL    LE807
            CALL    LED08
            LD      A,(QDDISPFLG)
            RRA     
            RET     C
            CALL    NL
            RST     018H
            JP      LE884

LED08:      LD      A,005H              ; Send motor off command (mode 5)
            LD      (QDCMD),A
            CALL    LE807
            RET     

LED11:      DB      "OK!"
LED14:      DB      00DH
LED15:      DB      "QD:FILE NOT FOUND"
            DB      00DH
LED27:      DB      "QD:HARD ERR"
            DB      00DH
LED33:      DB      "QD:NOT READY"
            DB      00DH
LED40:      DB      "QD:UNFORMAT"
            DB      00DH
LED4C:      DB      "QD:FILE MODE ERR"
            DB      00DH
LED5D:      DB      "DIRECTORY OF QD:"
            DB      00DH
LED6E:      DB      "    OBJ   "
            DB      00DH
LED79:      DB      "    BTX   "
            DB      00DH
LED84:      DB      "    BSD   "
            DB      00DH
LED8F:      DB      "    BRD   "
            DB      00DH
LED9A:      DB      "    RB    "
            DB      00DH
LEDA5:      DB      "    LIB   "
            DB      00DH
LEDB0:      DB      "    SYS   "
            DB      00DH
LEDBB:      DB      "    GR    "
            DB      00DH
LEDC6:      DB      "    ???   "
            DB      00DH
LEDD1:      LD      A,005H              ; QD block read service with retry (5 retries on error 29H)
            LD      (QDRETRY),A
LEDD6:      DI      
            CALL    LEDF8
            EI      
            RET     NC
            PUSH    AF
            CP      028H
            JR      Z,LEDF6             ; (+015H)
            CALL    LEF03
            POP     AF
            PUSH    AF
            CP      029H
            JR      NZ,LEDF6            ; (+00cH)
            LD      HL,QDRETRY
            DEC     (HL)
            JR      Z,LEDF3             ; (+003H)
            POP     AF
            JR      LEDD6               ; (-01dH)

LEDF3:      CALL    LEE0D
LEDF6:      POP     AF
            RET     

LEDF8:      LD      (QDSAVESP),SP       ; QD read dispatcher: mode 1=dir, 3=block read, 5/6=motor off
            LD      A,(QDCMD)
            DEC     A
            JR      Z,LEE14             ; (+012H)
            DEC     A
            DEC     A
            JR      Z,LEE1A             ; (+014H)
            DEC     A
            DEC     A
            JR      Z,LEE0D             ; (+003H)
            JP      LEF03

LEE0D:      PUSH    AF
            XOR     A
            LD      (QDADDRMARK),A
            POP     AF
            RET     

LEE14:      LD      A,(QDSUBCMD)        ; Mode 1: Directory scan (motor on + status)
            JP      LEE8B

LEE1A:      LD      A,(QDMOTOR)         ; Mode 3: Read block (init SIO, find block, read data)
            OR      A
            CALL    Z,LEEB7
            CALL    LEE51
            RET     C
            CALL    LEFEC
            CALL    LEFA8
            LD      C,A
            CALL    LEFA8
            LD      B,A
            LD      HL,(QDBLKSIZE)
            SBC     HL,BC
            JP      C,LEFC0
            LD      HL,(QDLOADADDR)
LEE3B:      CALL    LEFA8
            LD      (HL),A
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,LEE3B            ; (-00aH)
            CALL    LEF90
            LD      A,(QDSUBCMD)
            BIT     0,A
            JP      NZ,LEF03
            RET     

LEE51:      LD      HL,QDREMBLKS        ; Find next block: scan blocks, match address marks
            DEC     (HL)
            JR      Z,LEE87             ; (+030H)
            CALL    LEF18
            LD      C,A
            LD      A,(QDADDRMARK)
            LD      HL,QDEXPMARK
            CP      (HL)
            JR      NZ,LEE84            ; (+020H)
            INC     A
            LD      (QDADDRMARK),A
            LD      (HL),A
            LD      A,(QDSUBCMD)
            XOR     C
            RRA     
            RET     NC
LEE6F:      CALL    LEFA8
            LD      C,A
            CALL    LEFA8
            LD      B,A
LEE77:      CALL    LEFA8
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,LEE77            ; (-008H)
            CALL    LEF90
            JR      LEE51               ; (-033H)

LEE84:      INC     (HL)
            JR      LEE6F               ; (-018H)

LEE87:      LD      A,028H
            SCF     
            RET     

LEE8B:      LD      B,A                 ; Motor on + check drive status via SIO ChB
            LD      A,002H
            OUT     (SIO_CHB_C),A
            LD      A,081H
            OUT     (SIO_CHB_C),A
            LD      A,002H
            OUT     (SIO_CHB_C),A
            IN      A,(SIO_CHB_C)
            AND     081H
            CP      081H
            JP      NZ,LEFC6
            LD      A,010H
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            LD      C,A
            AND     008H
            JP      Z,LEFC6
            LD      A,B
            OR      A
            RET     Z
            LD      A,C
            AND     020H
            RET     NZ
            JP      LEFC3

LEEB7:      LD      HL,LEFE1            ; Full QD init: SIO reset, motor on, wait for DCD + sync
            LD      B,00BH
            CALL    LEED6
            CALL    LEF2E
            LD      (QDTOTBLKS),A
            INC     A
            LD      (QDREMBLKS),A
            CALL    LEF90
            LD      HL,QDFLAGS
            SET     3,(HL)
            XOR     A
            LD      (QDEXPMARK),A
            RET     

LEED6:      LD      C,SIO_CHA_C         ; Write SIO init table to ChA control + motor on via ChB WR5
            OTIR    
            LD      A,005H
            LD      (QDMOTOR),A
            OUT     (SIO_CHB_C),A
            LD      A,080H
            OUT     (SIO_CHB_C),A
LEEE5:      LD      A,010H
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            AND     008H
            JP      Z,LEFC6
            CALL    LEFEC
            LD      A,010H
            OUT     (SIO_CHB_C),A
            IN      A,(SIO_CHB_C)
            AND     008H
            JR      Z,LEEE5             ; (-018H)
            LD      BC,000E9H
            JP      LEFD4

LEF03:      PUSH    AF                  ; Motor off: ChA WR5=60H (RTS off), ChB WR5=00H
            LD      A,005H
            OUT     (SIO_CHA_C),A
            LD      A,060H
            OUT     (SIO_CHA_C),A
            LD      A,005H
            OUT     (SIO_CHB_C),A
            XOR     A
            LD      (QDMOTOR),A
            OUT     (SIO_CHB_C),A
            POP     AF
            RET     

LEF18:      LD      A,058H              ; Enter sync/hunt mode, wait for sync detection, read block type
            LD      B,00BH
            LD      HL,LEFE1
            CALL    LEF81
            LD      HL,QDFLAGS
            BIT     3,(HL)
            LD      BC,GETL
            JR      Z,LEF31             ; (+005H)
            RES     3,(HL)
LEF2E:      LD      BC,000A0H
LEF31:      CALL    LEFD4
            LD      A,005H
            OUT     (SIO_CHB_C),A
            LD      A,082H
            OUT     (SIO_CHB_C),A
            LD      A,003H
            OUT     (SIO_CHA_C),A
            LD      A,0D3H
            OUT     (SIO_CHA_C),A
            LD      BC,02CC0H
LEF47:      LD      A,010H
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            AND     010H
            JR      Z,LEF59             ; (+008H)
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,LEF47            ; (-00fH)
            JP      LEFCC

LEF59:      LD      A,003H
            OUT     (SIO_CHA_C),A
            LD      A,0C3H
            OUT     (SIO_CHA_C),A
            LD      B,09FH
LEF63:      LD      A,010H
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            AND     001H
            JR      NZ,LEF73            ; (+006H)
            DEC     B
            JR      NZ,LEF63            ; (-00dH)
            JP      LEFCC

LEF73:      LD      A,003H
            OUT     (SIO_CHA_C),A
            LD      A,0C9H
            OUT     (SIO_CHA_C),A
            CALL    LEFA8
            JP      LEFA8

LEF81:      LD      C,SIO_CHA_C         ; Write SIO register + OTIR init table to ChA control
            OUT     (C),A
            LD      A,005H
            OUT     (SIO_CHB_C),A
            LD      A,080H
            OUT     (SIO_CHB_C),A
            OTIR    
            RET     

LEF90:      LD      B,003H              ; Read 3 trailing bytes (CRC), check for Rx overrun
LEF92:      CALL    LEFA8
            DJNZ    LEF92               ; (-005H)
LEF97:      IN      A,(SIO_CHA_C)
            RRCA    
            JR      NC,LEF97            ; (-005H)
            LD      A,001H
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            AND     040H
            JR      NZ,LEFC0            ; (+01aH)
            OR      A
            RET     

LEFA8:      LD      A,010H              ; Read one byte from QD: check DCD, wait Rx ready, IN A,(F4H)
            OUT     (SIO_CHA_C),A
            IN      A,(SIO_CHA_C)
            AND     008H
            JP      Z,LEFC6
            IN      A,(SIO_CHA_C)
            RLCA    
            JR      C,LEFC0             ; (+008H)
            RRCA    
            RRCA    
            JR      NC,LEFA8            ; (-014H)
            IN      A,(SIO_CHA_D)
            OR      A
            RET     

LEFC0:      LD      A,029H              ; Error return chain: 29H=CRC, 2EH=?, 32H=not ready, 35H=?, 36H=unformat
            LD      HL,02E3EH
            LD      HL,0323EH
            LD      HL,0353EH
            LD      HL,0363EH
            LD      SP,(QDSAVESP)
            SCF     
            RET     

LEFD4:      PUSH    AF                  ; Delay loop: outer*inner (BC outer, 86H inner)
LEFD5:      LD      A,086H
LEFD7:      DEC     A
            JR      NZ,LEFD7            ; (-003H)
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,LEFD5            ; (-00aH)
            POP     AF
            RET     

LEFE1:      DB      058H                ; SIO init table: 58=ChReset, 04/10=WR4(x1,sync), 05/04=WR5(CRC16), 03/D0=WR3(Rx8bit,hunt), 06/16=WR6(sync1), 07/16=WR7(sync2)
            DB      004H
            DB      010H
            DB      005H
            DB      004H
            DB      003H
            DB      0D0H
            DB      006H
            DB      016H
            DB      007H
            DB      016H
LEFEC:      LD      A,0E8H              ; QD hardware presence check via memory-mapped I/O at E000/E001H
            LD      (0E000H),A
            NOP     
            LD      A,(0E001H)
            AND     081H
            RET     NZ
            LD      SP,(QDSAVESP)
            SCF     
            RET     

            DB      02EH                ; EFFE-EFFF: Residual data
            DB      041H
            DS      6, 0FFH             ; F000-F005: Unused (0xFF padding)
LF006:                                  ; F006-F7FF: Unused ROM space
            DS      2042, 0FFH          ; 0xFF padding to end of ROM
