; Disassembly of the file "mz5z008.bin"
; 
; CPU Type: Z80
; 
; Created with dZ80 2.1
; 
; on Monday, 16 of March 2026 at 07:23 PM
; 
LSD0        EQU     0D0H
LSD1        EQU     0D1H
LSD2        EQU     0D2H
LSD3        EQU     0D3H
LSE0        EQU     0E0H
LSE1        EQU     0E1H
LSE2        EQU     0E2H
LSE3        EQU     0E3H
LSE4        EQU     0E4H
LSE5        EQU     0E5H
LSE6        EQU     0E6H
QDPA        EQU     0F2H
QDPB        EQU     0F3H
QDPC        EQU     0F4H
QDPD        EQU     0F5H
FDPA        EQU     0F8H
FDPB        EQU     0F9H
FDPC        EQU     0FAH
FDPD        EQU     0FBH
LSFC        EQU     0FCH
TMCNT       EQU     01195H
CSMDT       EQU     01199H
TEMPW       EQU     0119EH
KEY262      EQU     012AAH
KEY264      EQU     012ACH
KEY266      EQU     012AEH
FUNBUF      EQU     012B2H
INBUFC      EQU     01352H
INBUFL      EQU     01353H
INBUF       EQU     01354H
LINLIM      EQU     01364H
KEYDAT      EQU     01365H
AMPM        EQU     01366H
SECD        EQU     01367H
XS          EQU     01369H
XE          EQU     0136AH
CWIDTH      EQU     0136BH
CSIZE       EQU     0136DH
YS          EQU     0136FH
YE          EQU     01370H
YW          EQU     01371H
SOF         EQU     01372H
SW          EQU     01374H
SSA         EQU     01375H
SEA         EQU     01376H
SSW         EQU     01377H
CRTMD1      EQU     01379H
CRTMD2      EQU     0137AH
SELCOL      EQU     0137BH
PAIWED      EQU     0137CH
PALBK       EQU     0137EH
PALAD       EQU     0137FH
PALTBL      EQU     01381H
PAL2T       EQU     01385H
PAL4T       EQU     01389H
PAL16T      EQU     0138DH
CURDT1      EQU     01391H
CURDT2      EQU     01399H
CURDT3      EQU     013A1H
SCRNT0      EQU     013A9H
EMFLG       EQU     013C3H
EMPTR       EQU     013C4H
EMWP0       EQU     013C6H
EMWP1       EQU     013C8H
PBCN        EQU     013CEH
HERRF       EQU     013D9H
CTABLE      EQU     013DAH
FONTBF      EQU     01000H
TEXTBF      EQU     02000H
BITBUF      EQU     08000H
IBUFE       EQU     010F0H
ERRTXT      EQU     0FDA0H
VRAM        EQU     0D000H
ARAM        EQU     0D800H
MMIO        EQU     0E000H
TEMPO_      EQU     0E008H
PPI_PA      EQU     0E010H
PPI_PB      EQU     0E011H
PPI_PC      EQU     0E012H
PPI_CTL     EQU     0E013H
PRGSTART    EQU     00000H

; RFS (ROM Filing System) constants for picoZ80
PORT_LODRAM EQU     0E0H                ; OUT: map 0000-0FFF to DRAM
PORT_DRAM   EQU     0E1H                ; OUT: map D000-FFFF to DRAM
PORT_LOROM  EQU     0E2H                ; OUT: map 0000-0FFF to Monitor ROM
PORT_MMIO   EQU     0E3H                ; OUT: map D000-FFFF to VRAM/MMIO/ROM
BNKCTRLRST  EQU     0EFF8H              ; RFS bank control reset register
BNKCTRL     EQU     0EFFFH              ; Bank Control register (CS, CLK bits)
HWSPIDATA   EQU     0EFFBH              ; Hardware SPI Data register (read/write)
HWSPISTART  EQU     0EFFCH              ; Start SPI transfer register
; SECTORBUF relocated to RFS data area — was EQU 0CE00H (overlapped BASIC stack)
SDCS_ACT    EQU     001H                ; BNKCTRL: CS LOW (active), BBCLK enable
SDCS_DEACT  EQU     003H                ; BNKCTRL: CS HIGH (inactive), BBCLK enable
HDRLEN      EQU     00040H              ; File header length (64 bytes)

            ORG     PRGSTART

L0000:      JP      L0CAF               ; Cold start entry - JP to initialization

L0003:      JP      L028C

L0006:      JP      L05EC

            JP      L05E8

L000C:      JP      L0BFD

L000F:      JP      L05F1

L0012:      JP      L0BFF

L0015:      JP      L0BEE

SVC_ENTRY:  JP      SVC_DISP            ; RST 18H - SVC (Supervisor Call) dispatcher

            JP      L041C

BRKCHK:     JP      L05C1

            JP      L30CB

            JP      L30D2

L0027:      JP      L3114

            JP      L311B

            JP      L3153

L0030:      JP      L14BC

            JP      L0955

L0036:      LD      B,B
            NOP     
RST38:      JP      BREAKZ              ; RST 38H - Interrupt handler

            JP      L098F

L003E:      JP      L0D3F

L0041:      JP      L0D30

            JP      L0D05

            JP      L0D20

            JP      L028C

L004D:      DB      000H                ; System status / work area
L004E:      DB      000H
L004F:      DB      0FFH
L0050:      DB      000H
            DB      0C3H
            DB      0EEH
            DB      00BH
L0054:      DS      2, 000H
L0056:      DB      000H
L0057:      DB      018H
            DB      0C3H
            DB      08AH
            DB      003H
L005B:      DB      000H
            DB      027H
L005D:      DB      071H
            DB      000H
L005F:      DB      000H
L0060:      DB      0EFH
            DB      0C3H
            DB      037H
            DB      00AH
L0064:      DB      000H
L0065:      DB      000H
L0066:      DB      000H
L0067:      DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      0ADH
            DB      007H
            DB      0F1H
            DB      008H
L0071:      DB      015H
            DB      008H
            DB      012H
            DB      008H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
L0080:      DB      000H
L0081:      DB      0ADH
            DB      007H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      01EH
            DB      008H
            DB      0CCH
            DB      007H
            DB      0F0H
            DB      007H
            DB      LSD1
            DB      007H
            DB      QDPD
            DB      007H
            DB      054H
            DB      007H
            DB      074H
            DB      007H
            DB      018H
            DB      008H
            DB      0BAH
            DB      008H
            DB      012H
            DB      008H
            DB      LSE5
            DB      000H
            DB      0ADH
            DB      007H
            DB      LSE5
L00A0:      DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
.NOP:       DB      000H
.HL:        DS      11, 000H
.RET:       DS      8, 000H
PUSHRA:     DS      6, 000H
L00C0:      DS      7, 000H
PUSHR:      DS      18, 000H
L00D9:      DB      000H
L00DA:      DI      
            LD      SP,L0000
            IM      1
            OUT     (LSE1),A
            JP      L1800

BREAKZ:     RET     

BREAKX:     DI      
            OUT     (LSE3),A
L00E9:      LD      A,(HL)
            OUT     (LSE1),A
            EI      
ERRORJ:     RET     

POPRA:      DI      
            OUT     (LSE3),A
            LD      (HL),A
            OUT     (LSE1),A
            EI      
            RET     

            JP      L026A

L00F9:      NOP     
L00FA:      JP      (HL)

IOOUT:      EX      (SP),IX
            PUSH    HL
            PUSH    BC
            PUSH    DE
L0100:      PUSH    AF
L0101:      PUSH    HL
            LD      HL,L0114
            EX      (SP),HL
            JP      (IX)

SKPDE:      EX      (SP),IX
            PUSH    HL
            PUSH    BC
            PUSH    DE
DEVASC:     PUSH    HL
            LD      HL,L0115
            EX      (SP),HL
            JP      (IX)

L0114:      POP     AF
L0115:      POP     DE
            POP     BC
            POP     HL
            POP     IX
            RET     

CHKACC:     EX      (SP),HL
            PUSH    BC
            LD      B,(HL)
L011E:      INC     HL
            CP      (HL)
            JR      Z,L0127             ; (+005H)
            DJNZ    L011E               ; (-006H)
            INC     HL
            JR      L012A               ; (+003H)

L0127:      INC     HL
            DJNZ    L0127               ; (-003H)
L012A:      POP     BC
            EX      (SP),HL
            RET     

LDDEMI:     LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            RET     

LDDEMD:     LD      E,(HL)
            INC     HL
            LD      D,(HL)
            DEC     HL
            RET     

?CLRHL:     XOR     A
?SETHL:     LD      (HL),A
            INC     HL
            DJNZ    ?SETHL              ; (-004H)
            RET     

?CLRDE:     XOR     A
?SETDE:     LD      (DE),A
            INC     DE
L0140:      DJNZ    ?SETDE              ; (-004H)
            RET     

LDDEHL:     LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            DJNZ    LDDEHL              ; (-006H)
            RET     

LDHLDE:     LD      A,(DE)
            LD      (HL),A
            INC     HL
            INC     DE
            DJNZ    LDHLDE              ; (-006H)
            RET     

INDRCT:     PUSH    AF
            LD      A,(HL)
            INC     HL
            LD      H,(HL)
            LD      L,A
            POP     AF
            RET     

ADDHLA:     ADD     A,L
            LD      L,A
            RET     NC
            INC     H
            RET     

INCHLF:     INC     HL
HLFTCH:     LD      A,(HL)
            CP      020H
            JR      Z,INCHLF            ; (-006H)
            RET     

TEST1:      CALL    HLFTCH
            EX      (SP),HL
L0168:      CP      (HL)
            INC     HL
            EX      (SP),HL
            RET     NZ
            INC     HL
            RET     

TESTX:      CALL    HLFTCH
            EX      (SP),HL
            CP      (HL)
            INC     HL
            EX      (SP),HL
            INC     HL
            RET     Z
            LD      A,001H
            JR      L0182               ; (+007H)

L017B:      XOR     A
            LD      HL,L803E
            LD      HL,L80F6
L0182:      PUSH    AF
            CALL    L0D20
            POP     AF
            JP      L0000

; ---------------------------------------------------------------------------
; SVC Dispatch Table (L018A - ~0x0201)
;
; This is a lookup table of 2-byte handler addresses indexed by SVC number.
; SVC_DISP uses: handler = word[L018A + SVC*2].
; NOTE: The bytes in this table also serve as executable code — several
; labels within the table (L01A2, L01CE, L01D9, L01E0) are jump targets
; from nearby code. This dual-purpose layout prevents conversion to DW.
;
; Key entries:
;   SVC #00=3D9D #02=05E8 #05=0908 #20=0D3E #29=1EDB #2A=1F1D
;   SVC #2E=19E0 #33=1AE1 #39=1D1B #3A=14DD
; ---------------------------------------------------------------------------
L018A:      SBC     A,L
            DEC     A
            CALL    PE,0E805H
            DEC     B
            RST     038H
            DEC     BC
            LD      B,E
            INC     C
            EX      AF,AF'
            ADD     HL,BC
            XOR     C
            RRCA    
            LD      A,00FH
            RET     Z
            DEC     BC
            JP      Z,0D80BH
            DEC     BC
            AND     00BH
L01A2:      ADC     A,H
            LD      (BC),A
            ADC     A,D
            INC     BC
            POP     BC
            DEC     B
            AND     B
            INC     D
            ADD     A,A
            LD      A,(BC)
            ADC     A,C
            LD      A,(BC)
            SCF     
            LD      A,(BC)
            LD      B,(HL)
            INC     D
            HALT    
            INC     D
            SUB     C
            INC     D
            LD      B,014H
            LD      E,H
            LD      (BC),A
            POP     HL
            INC     C
            RET     PE
            INC     C
            RST     028H
            INC     C
            PUSH    HL
            INC     D
            AND     H
            INC     DE
            LD      (HL),D
            INC     DE
            LD      C,E
            LD      B,LSE5
            NOP     
            LD      A,00DH
            JR      NC,L01CE            ; (+000H)
L01CE:      PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            ADC     A,A
            ADD     HL,BC
            LD      D,L
            ADD     HL,BC
            SUB     B
L01D9:      DEC     C
            RET     PE
            DEC     DE
            IN      A,(01EH)
            DEC     E
            RRA     
L01E0:      POP     AF
            LD      E,03DH
            LD      A,(DE)
            CP      B
            LD      A,(DE)
            RET     PO
            ADD     HL,DE
            LD      (HL),C
            DEC     DE
            OR      H
            INC     E
            RET     C
            INC     E
            RET     

            INC     E
            POP     HL
            LD      A,(DE)
            RET     NC
            JR      L01D9               ; (-01cH)

            JR      00243H               ; (+04cH)

            ADD     HL,DE
            RRCA    
            JR      L01A2               ; (-059H)

            DEC     DE
            DEC     DE
            DEC     E
            DB      0DDH
            INC     D
L0200:      PUSH    HL
            NOP     
            LD      E,B
            LD      E,067H
            INC     L
            OR      (HL)
            INC     L
            LD      B,(HL)
            INC     L
            NOP     
            ADD     HL,HL
            SBC     A,L
            LD      HL,(02925H)
            XOR     E
            RRA     
            CALL    M,L742A
            RLCA    
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
            PUSH    HL
            NOP     
SVC_DISP:   EX      (SP),HL
            PUSH    AF
            LD      A,(HL)
            INC     HL
            PUSH    HL
            LD      HL,L018A
            ADD     A,A
            ADD     A,L
            JR      NC,L0237            ; (+001H)
            INC     H
L0237:      LD      L,A
            LD      A,(HL)
            INC     HL
            LD      H,(HL)
            LD      L,A
            LD      (00243H),HL
            POP     HL
            POP     AF
            EX      (SP),HL
            JP      L0000

IOCALL:     PUSH    HL
            PUSH    DE
            LD      (00251H),IX
            LD      IX,00180H
            OR      A
            CALL    L0000
            POP     DE
            POP     HL
            RET     NC
            OR      A
            SCF     
L0258:      RET     Z
            JP      00180H

            PUSH    DE
            LD      B,000H
L025F:      LD      A,(DE)
            OR      A
            JR      Z,L0268             ; (+005H)
            INC     DE
            INC     B
            JR      NZ,L025F            ; (-008H)
            DEC     B
L0268:      POP     DE
            RET     

L026A:      PUSH    HL
            LD      HL,(L0054)
            PUSH    HL
            PUSH    DE
            CALL    PUSHW
            POP     DE
            LD      (HL),000H
            POP     HL
            CALL    L028C
            JR      C,L028A             ; (+00eH)
            LD      A,(L005B)
            SUB     L
            JR      NC,L028A            ; (+008H)
            NEG     
            LD      L,A
            LD      H,000H
            ADD     HL,DE
            EX      DE,HL
            OR      A
L028A:      POP     HL
            RET     

L028C:      PUSH    BC
            PUSH    HL
            PUSH    DE
L028F:      CALL    L0391
            CP      00DH
            JP      Z,L0311
            CP      01BH
            JR      Z,L02E8             ; (+04dH)
            PUSH    AF
            CP      020H
DEASC:      JR      C,L02CD             ; (+02dH)
            JR      L02C8               ; (+026H)

L02A2:      LD      A,(L004D)
            OR      A
            JR      NZ,L02C8            ; (+020H)
            LD      A,(00B18H)
            RRCA    
            JR      C,L02C8             ; (+01aH)
            LD      A,(L004E)
            OR      A
            JR      NZ,L02C8            ; (+014H)
            POP     AF
            LD      HL,L06C1
            CP      011H
            JR      Z,L02C3             ; (+007H)
            LD      HL,L06E7
            CP      012H
            JR      NZ,L02C9            ; (+006H)
L02C3:      CALL    L00FA
            JR      L028F               ; (-039H)

L02C8:      POP     AF
L02C9:      RST     018H
            DB      003H
            JR      L028F               ; (-03eH)

L02CD:      LD      A,(L0060)
            CP      0FFH
            JR      NZ,L02A2            ; (-032H)
            POP     AF
            CP      017H
            JR      NC,L02C9            ; (-010H)
            CP      011H
            JR      C,L02C9             ; (-014H)
            LD      HL,(00B18H)
            BIT     6,L
            JR      Z,L02C9             ; (-01bH)
            RST     018H
            DB      004H
            JR      L028F               ; (-059H)

L02E8:      SCF     
CKHEX:      PUSH    AF
            LD      A,00DH
            RST     018H
            DB      003H
            POP     AF
            POP     DE
            POP     HL
            POP     BC
            RET     

L02F3:      CALL    PUSHW
L02F6:      LD      A,(HL)
            OR      A
MHALT:      RET     Z
            DEC     HL
            DEC     E
            LD      A,(L0056)
            CP      E
            JR      C,L02F6             ; (-00bH)
            RET     

L0302:      CALL    PUSHW
L0305:      INC     E
            INC     HL
            LD      A,(HL)
            OR      A
            RET     Z
            LD      A,(L0057)
            CP      E
            JR      NC,L0305            ; (-00bH)
            RET     

L0311:      LD      HL,(L0054)
SETDFL:     CALL    L02F3
            LD      D,E
            CALL    L0305
            LD      A,(L005B)
            LD      L,A
            LD      A,E
            SUB     D
            LD      E,A
            LD      H,D
ADDP1:      LD      D,L
            RST     018H
            DB      01EH
            LD      A,(0005CH)
            SUB     D
ADDP2:      INC     A
            LD      D,A
            POP     BC
            PUSH    BC
            EXX     
            PUSH    HL
            LD      HL,(L004F)
            EXX     
L0334:      PUSH    DE
            PUSH    HL
L0336:      CALL    BREAKX
            RST     018H
            DB      01CH
            INC     HL
            LD      (BC),A
            INC     BC
L033E:      EXX     
            DEC     L
ERRX:       JR      Z,L0354             ; (+012H)
            EXX     
            DEC     D
            JR      NZ,L0336            ; (-010H)
            POP     HL
            PUSH    BC
            LD      BC,00028H
            ADD     HL,BC
            POP     BC
            POP     DE
            DEC     E
            JR      NZ,L0334            ; (-01dH)
            PUSH    DE
            PUSH    HL
            EXX     
L0354:      EXX     
            POP     HL
            POP     DE
            EXX     
            POP     HL
            EXX     
            XOR     A
            LD      (BC),A
            LD      L,C
            LD      H,B
            POP     DE
            PUSH    DE
            PUSH    HL
            OR      A
            SBC     HL,DE
            LD      B,L
            POP     HL
            LD      A,020H
            LD      D,A
            JP      Z,CKHEX
            DEC     HL
L036D:      LD      A,(HL)
            OR      A
            JR      Z,L0376             ; (+005H)
            CP      D
            JR      NZ,L037D            ; (+009H)
            LD      (HL),000H
L0376:      DEC     HL
            DJNZ    L036D               ; (-00cH)
            OR      A
            JP      CKHEX

L037D:      LD      A,(HL)
            OR      A
            JR      NZ,L0383            ; (+002H)
            LD      (HL),020H
L0383:      DEC     HL
            DJNZ    L037D               ; (-009H)
            OR      A
            JP      CKHEX

            INC     A
            JR      Z,L0406             ; (+079H)
            DEC     A
            JP      Z,L041C
L0391:      CALL    L0426
            CALL    SKPDE
SETDNM:     CALL    L0648
            CALL    BREAKX
            LD      D,A
            LD      A,(L0060)
            LD      E,A
            CALL    POPRA
            XOR     A
            DI      
            OUT     (LSE3),A
            LD      (MMIO),A
            CPL     
            LD      (MMIO),A
            OUT     (LSE1),A
            EI      
            LD      C,000H
            EX      AF,AF'
            PUSH    AF
            CALL    L0459
            LD      A,000H
L03BC:      EX      AF,AF'
L03BD:      LD      B,010H
L03BF:      CALL    L043C
            CALL    L0459
            BIT     1,A
            JR      NZ,L03BD            ; (-00cH)
            DJNZ    L03BF               ; (-00cH)
            BIT     0,A
            JR      Z,L03DB             ; (+00cH)
            BIT     2,A
            JR      NZ,L03EC            ; (+019H)
            EX      AF,AF'
            DEC     A
            JR      NZ,L03BC            ; (-01bH)
            LD      A,006H
            JR      L03EE               ; (+013H)

L03DB:      PUSH    HL
            PUSH    DE
            PUSH    BC
            LD      HL,INKEY1
            LD      DE,L0B17
            LD      BC,0000AH
L03E7:      LDIR    
            POP     BC
            POP     DE
            POP     HL
L03EC:      LD      A,040H
L03EE:      LD      (003BBH),A
L03F1:      CALL    L043C
            CALL    L0494
            OR      A
            JR      Z,L03F1             ; (-009H)
            LD      (L005F),A
            LD      C,A
            LD      A,D
            CALL    POPRA
            POP     AF
            EX      AF,AF'
            LD      A,C
            RET     

L0406:      CALL    L0426
            PUSH    HL
            CALL    L0494
            LD      HL,L005F
            OR      A
            JR      Z,L0419             ; (+006H)
            CP      (HL)
            JR      NZ,L0419            ; (+003H)
            POP     HL
            XOR     A
            RET     

L0419:      LD      (HL),A
            POP     HL
            RET     

L041C:      CALL    L0426
            CALL    L0494
            LD      (L005F),A
            RET     

L0426:      PUSH    HL
            LD      HL,(L1360)
            LD      A,L
            CP      H
            POP     HL
            RET     Z
            EX      (SP),HL
            INC     A
            LD      (L1360),A
            LD      HL,L1361
            CALL    ADDHLA
            LD      A,(HL)
            POP     HL
            RET     

L043C:      DI      
            OUT     (LSE3),A
            LD      A,(0E002H)
            OUT     (LSE1),A
            EI      
            RLCA    
            RLCA    
            LD      A,C
            JR      C,L0451             ; (+007H)
            OR      A
            RET     Z
            LD      C,000H
            LD      A,D
DPALBK:     JR      L0456               ; (+005H)

L0451:      DEC     A
            RET     Z
            LD      C,001H
            LD      A,E
L0456:      JP      POPRA

L0459:      CALL    SKPDE
            LD      DE,INKEY1
            LD      HL,L0B17
            LD      BC,L0A00
            DI      
            OUT     (LSE3),A
L0468:      LD      A,B
            ADD     A,0EFH
            LD      (MMIO),A
            CP      FDPA
            LD      A,(0E001H)
            JR      NZ,L0477            ; (+002H)
            OR      07FH
L0477:      CP      0FFH
            JR      Z,L047D             ; (+002H)
            SET     0,C
L047D:      EX      DE,HL
            CP      (HL)
            LD      (HL),A
            EX      DE,HL
            JR      Z,L0485             ; (+002H)
            SET     1,C
L0485:      CPL     
            AND     (HL)
            JR      Z,L048B             ; (+002H)
            SET     2,C
L048B:      INC     HL
            INC     DE
            DJNZ    L0468               ; (-027H)
            LD      A,C
L0490:      OUT     (LSE1),A
            EI      
DGCOL:      RET     

L0494:      CALL    SKPDE
            LD      HL,L0B17
            LD      DE,INKEY1
            PUSH    HL
            PUSH    DE
            LD      BC,0000AH
            LDIR    
            POP     HL
            POP     DE
            LD      BC,L0AF9
            DI      
            OUT     (LSE3),A
            LD      A,C
            LD      (MMIO),A
            NOP     
            LD      A,(0E001H)
            LD      (DE),A
L04B5:      LD      A,C
            LD      (MMIO),A
            CP      FDPA
            LD      A,(0E001H)
            LD      (DE),A
            JR      Z,L04E4             ; (+023H)
            CPL     
            AND     (HL)
L04C3:      LD      (HL),A
            INC     DE
            INC     HL
            DEC     C
            DJNZ    L04B5               ; (-014H)
            OUT     (LSE1),A
            EI      
            LD      BC,L0A00
L04CF:      DEC     HL
            LD      A,(HL)
            OR      A
            JR      NZ,L0507            ; (+033H)
            INC     C
            DJNZ    L04CF               ; (-008H)
            LD      B,00AH
L04D9:      DEC     DE
            LD      A,(DE)
            CP      0FFH
            JR      NZ,L04E7            ; (+008H)
L04DF:      DJNZ    L04D9               ; (-008H)
L04E1:      XOR     A
            JR      L0490               ; (-054H)

L04E4:      XOR     A
            JR      L04C3               ; (-024H)

L04E7:      LD      A,B
            CP      002H
            JR      NZ,L04F5            ; (+009H)
L04EC:      LD      A,(DE)
            AND     081H
DWIDTH:     JR      NZ,L04DF            ; (-012H)
            LD      A,01BH
            JR      L0490               ; (-065H)

L04F5:      CP      001H
            JR      Z,L04E1             ; (-018H)
            CP      000H
            JR      NZ,L04DF            ; (-01eH)
            LD      A,(DE)
            AND     001H
            JR      NZ,L04DF            ; (-023H)
            LD      A,(L005F)
            JR      L0490               ; (-077H)

L0507:      PUSH    AF
            LD      A,B
            LD      (004FAH),A
            LD      A,(HL)
            LD      (004FFH),A
            POP     AF
            DEC     B
            JR      NZ,L054A            ; (+036H)
            CALL    L05B7
            LD      A,(00B18H)
            BIT     6,A
            JR      Z,L04E1             ; (-03dH)
            BIT     0,A
            LD      A,B
            JR      NZ,L0525            ; (+002H)
            ADD     A,005H
L0525:      CP      00AH
            JR      NC,L04E1            ; (-048H)
            LD      L,A
            LD      H,000H
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,HL
            LD      BC,L12C0
            ADD     HL,BC
            LD      A,(HL)
            OR      A
            JR      Z,L04E1             ; (-057H)
            LD      DE,L1360
            LD      A,001H
            LD      (DE),A
L053E:      INC     DE
            LD      BC,00010H
            LDIR    
            LD      A,(01362H)
            JP      L0490

L054A:      CALL    L05B7
            LD      A,C
            ADD     A,A
            ADD     A,A
            ADD     A,A
            ADD     A,B
            LD      L,A
            LD      H,000H
            LD      A,(00B18H)
            BIT     6,A
            LD      BC,L0B2B
            JR      Z,L058E             ; (+02fH)
            PUSH    AF
            LD      A,(L0060)
            CP      043H
            JR      NZ,L056B            ; (+004H)
            POP     AF
            XOR     001H
            PUSH    AF
L056B:      POP     AF
            BIT     0,A
            JR      NZ,L0573            ; (+003H)
            LD      BC,L0B3D
L0573:      LD      A,(L0060)
            CP      0FFH
            JR      NZ,L0587            ; (+00dH)
            LD      BC,L0B4F
            LD      A,(00B18H)
            BIT     0,A
            JR      Z,L0587             ; (+003H)
            LD      BC,L0B61
L0587:      CALL    L13E0
            LD      A,C
            JP      L0490

L058E:      CALL    L13E0
            LD      A,C
            CP      020H
            JP      C,L0490
            LD      HL,L0B73
            LD      B,005H
L059C:      CP      (HL)
            JR      Z,L05B1             ; (+012H)
            INC     HL
            DJNZ    L059C               ; (-006H)
            CP      040H
            JP      C,L04E1
            CP      05BH
            JP      NC,L04E1
            SUB     040H
            JP      L0490

L05B1:      LD      A,020H
            SUB     B
            JP      L0490

L05B7:      LD      B,008H
L05B9:      RRCA    
            JR      C,L05BF             ; (+003H)
            DJNZ    L05B9               ; (-005H)
            RET     

L05BF:      DEC     B
            RET     

L05C1:      LD      A,0E8H
            DI      
            OUT     (LSE3),A
            LD      (MMIO),A
            NOP     
            LD      A,(0E001H)
            OUT     (LSE1),A
            EI      
            AND     081H
            RET     Z
            RLCA    
            RET     C
            JR      L05C1               ; (-016H)

L05D7:      CALL    SKPDE
            LD      HL,(L0054)
            LD      A,(L005B)
            CP      L
            RET     C
            CALL    PUSHW
            LD      A,(HL)
            OR      A
            RET     

L05E8:      CALL    L05D7
            RET     Z
L05EC:      LD      A,00DH
            JP      L0BFF

L05F1:      CALL    L0BFD
            LD      A,(L0054)
L05F7:      SUB     00AH
LBOUND:     JR      NC,L05F7            ; (-004H)
            ADD     A,00AH
            RET     Z
            JR      L05F1               ; (-00fH)

L0600:      CALL    IOOUT
            LD      HL,(L0054)
            PUSH    HL
            PUSH    AF
            RST     018H
            DB      01EH
            POP     AF
            RST     018H
            DB      01DH
            CALL    POPRA
            SET     3,H
            LD      DE,(0005DH)
            LD      A,E
            OR      D
            CALL    POPRA
            POP     HL
            INC     L
HCLSW:      LD      A,(0005CH)
            CP      L
            JP      NC,L07EC
L0624:      LD      A,(L005B)
            LD      L,A
            INC     H
            PUSH    HL
            CALL    PUSHW
            LD      (HL),001H
            POP     HL
            LD      A,(L0057)
            CP      H
            JP      NC,L07EC
            LD      H,A
            PUSH    HL
            CALL    L0730
            POP     HL
            JP      L07EC

PUSHW:      LD      E,H
            LD      D,000H
            LD      HL,.HL
            ADD     HL,DE
            RET     

L0648:      LD      HL,(L0054)
            PUSH    DE
            PUSH    AF
            LD      D,LSD0
            LD      E,L
            LD      A,H
            ADD     A,A
HCLS:       ADD     A,A
            ADD     A,H
            LD      L,A
            LD      H,000H
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,DE
L065B:      POP     AF
            POP     DE
            RET     

L065E:      ADD     A,A
            LD      HL,L0067
            CALL    ADDHLA
            CALL    INDRCT
            JP      (HL)

L0669:      LD      B,D
            LD      C,E
L066B:      LD      DE,00028H
            EX      DE,HL
            ADD     HL,DE
            DEC     B
            JR      Z,L067E             ; (+00bH)
            PUSH    HL
            PUSH    BC
            LD      B,000H
            CALL    L069E
            POP     BC
            POP     HL
            JR      L066B               ; (-013H)

L067E:      EX      DE,HL
            LD      E,C
L0680:      LD      A,(L005D)
            LD      D,A
            LD      A,(0005EH)
            OR      D
            LD      D,A
L0689:      DI      
            OUT     (LSE3),A
            LD      (HL),000H
            SET     3,H
            LD      (HL),D
            RES     3,H
            OUT     (LSE1),A
            EI      
            INC     HL
            DEC     E
            JR      NZ,L0689            ; (-011H)
            RET     

L069B:      LD      BC,00028H
L069E:      LD      A,0B0H
            JR      L06A4               ; (+002H)

L06A2:      LD      A,0B8H
L06A4:      LD      (006BCH),A
            PUSH    HL
            PUSH    DE
            PUSH    BC
            RES     3,H
            RES     3,D
            CALL    L06B8
            POP     BC
            POP     DE
            POP     HL
            SET     3,H
            SET     3,D
L06B8:      DI      
            OUT     (LSE3),A
            LDIR    
            OUT     (LSE1),A
            EI      
            RET     

L06C1:      LD      HL,VRAM
            LD      DE,0D7D0H
            CALL    L069B
            LD      HL,0D028H
            LD      DE,VRAM
            LD      B,032H
L06D2:      PUSH    BC
            CALL    L069B
            POP     BC
            DJNZ    L06D2               ; (-007H)
            LD      HL,.HL
            LD      D,H
            LD      E,L
            LD      A,(HL)
            INC     HL
            LD      BC,00031H
            LDIR    
            LD      (DE),A
            RET     

L06E7:      LD      HL,0D7CFH
            LD      DE,0D7F7H
            LD      B,032H
L06EF:      PUSH    BC
            LD      BC,00028H
            CALL    L06A2
            POP     BC
            DJNZ    L06EF               ; (-00aH)
            LD      HL,0D7D0H
            LD      DE,VRAM
            CALL    L069B
            LD      HL,L00D9
            LD      D,H
            LD      E,L
            DEC     HL
            LD      A,(DE)
            LD      BC,00031H
            LDDR    
            LD      (DE),A
            RET     

L0710:      LD      HL,0D3E8H
            XOR     A
            CALL    POPRA
            SET     3,H
            LD      A,(L005D)
            CALL    POPRA
            LD      DE,0D3E9H
            LD      BC,L0027
            CALL    L069E
            CALL    L06C1
            XOR     A
            LD      (L00C0),A
            RET     

L0730:      LD      A,(L004E)
            OR      A
            JR      Z,L0710             ; (-026H)
            CALL    L075F
            CALL    L0669
            LD      A,(L0056)
            LD      H,A
            CALL    PUSHW
            LD      A,(L0057)
            SUB     E
            LD      B,A
            LD      (HL),000H
            INC     HL
            LD      D,H
            LD      E,L
            INC     DE
            CALL    NZ,LDHLDE
            LD      (HL),000H
            RET     

L0754:      LD      A,(L005B)
            LD      L,A
            LD      A,(L0056)
            LD      H,A
            JP      L07EC

L075F:      LD      HL,(L005B)
            LD      DE,(00056H)
            LD      A,D
L0767:      SUB     E
            INC     A
            LD      D,A
            LD      A,H
            SUB     L
            INC     A
L076D:      LD      E,A
            CALL    L0754
            JP      L0648

            CALL    SKPDE
            LD      A,(L004E)
L077A:      OR      A
            JR      NZ,L078B            ; (+00eH)
            LD      A,031H
            LD      (L0057),A
            CALL    L078B
            LD      A,018H
            LD      (L0057),A
            RET     

L078B:      CALL    L075F
L078E:      PUSH    DE
            PUSH    HL
            CALL    L0680
            POP     HL
            LD      BC,00028H
            ADD     HL,BC
            POP     DE
            DEC     D
            JR      NZ,L078E            ; (-00eH)
            LD      A,(L0056)
            LD      H,A
            CALL    PUSHW
            LD      A,(L0057)
            SUB     E
            ADD     A,002H
            LD      B,A
            JP      ?CLRHL

            XOR     A
            LD      (0091FH),A
            LD      HL,(L0054)
            PUSH    HL
            INC     H
            CALL    PUSHW
            LD      A,(L0057)
            LD      D,A
L07BD:      LD      A,(HL)
            OR      A
            JR      Z,L07C7             ; (+006H)
            INC     HL
            INC     E
            LD      A,D
            CP      E
            JR      NC,L07BD            ; (-00aH)
L07C7:      DEC     E
            POP     HL
            LD      H,E
            JR      L07DB               ; (+00fH)

            LD      HL,(L0054)
            JR      L07DF               ; (+00eH)

            LD      HL,(L0054)
            INC     L
            LD      A,(0005CH)
            CP      L
            JR      NC,L07EC            ; (+011H)
L07DB:      LD      A,(L005B)
            LD      L,A
L07DF:      INC     H
            LD      A,(L0057)
            CP      H
            JR      NC,L07EC            ; (+006H)
            LD      H,A
            PUSH    HL
            CALL    L0730
            POP     HL
L07EC:      LD      (L0054),HL
            RET     

            LD      HL,(L0054)
            JR      L0802               ; (+00dH)

            LD      HL,(L0054)
            LD      A,(L005B)
            CP      L
            JR      C,0080DH             ; (+00fH)
            LD      A,(0005CH)
            LD      L,A
L0802:      LD      A,(L0056)
            CP      H
            JR      C,0080FH             ; (+007H)
            LD      A,(L0056)
            LD      H,A
            LD      A,02DH
            LD      A,025H
            JR      L07EC               ; (-026H)

            LD      A,0EFH
            LD      HL,L433E
            LD      HL,0FF3EH
            LD      (L0060),A
            RET     

            LD      HL,(L0054)
            LD      BC,(0005BH)
            LD      A,L
            DEC     L
            INC     C
            CP      C
            JR      NC,L0837            ; (+00cH)
            PUSH    HL
            CALL    PUSHW
            LD      A,(HL)
            POP     HL
            INC     L
            OR      A
            JR      Z,L0874             ; (+03fH)
            LD      L,B
            DEC     H
L0837:      LD      DE,(00054H)
            LD      (L0054),HL
L083E:      EX      DE,HL
            LD      A,(L005B)
            CP      L
            JR      C,L086D             ; (+028H)
L0845:      PUSH    HL
            CALL    PUSHW
            LD      A,(HL)
            POP     HL
            OR      A
            JR      Z,L085D             ; (+00fH)
            CALL    L0882
            PUSH    HL
            LD      A,(0005CH)
            LD      L,A
            DEC     H
            CALL    L089A
            POP     HL
            JR      L0874               ; (+017H)

L085D:      LD      A,(0005CH)
            LD      L,A
            DEC     H
L0862:      LD      DE,(0005DH)
            LD      A,E
            OR      D
            LD      E,A
            LD      D,000H
            JR      L089A               ; (+02dH)

L086D:      CALL    L0882
            DEC     BC
            CALL    L0895
L0874:      INC     L
            LD      A,(0005CH)
            CP      L
            JR      NC,L086D            ; (-00eH)
            LD      A,(L005B)
            LD      L,A
            INC     H
            JR      L0845               ; (-03dH)

L0882:      PUSH    HL
            RST     018H
            DB      01EH
            SET     3,H
            CALL    BREAKX
            LD      E,A
            RES     3,H
            CALL    BREAKX
            LD      D,A
            LD      B,H
            LD      C,L
            POP     HL
            RET     

L0895:      PUSH    HL
            LD      H,B
            LD      L,C
            JR      L089D               ; (+003H)

L089A:      PUSH    HL
            RST     018H
            DB      01EH
L089D:      SET     3,H
            LD      A,E
            CALL    POPRA
            RES     3,H
            LD      A,D
            CALL    POPRA
            LD      B,H
            LD      C,L
            POP     HL
            RET     

L08AD:      LD      HL,(L0054)
            CALL    L0302
            DEC     E
            LD      H,E
            LD      A,(0005CH)
            LD      L,A
            RET     

            CALL    L08AD
            CALL    L0882
            LD      A,0F0H
            CP      D
            JR      Z,L08C8             ; (+003H)
            XOR     A
            CP      D
            RET     NZ
L08C8:      LD      BC,(0005BH)
            EX      DE,HL
            LD      HL,(L0054)
            OR      A
            SBC     HL,DE
            EX      DE,HL
            JR      Z,L0862             ; (-074H)
            LD      A,C
            CP      L
            JR      NC,L08E4            ; (+00aH)
            DEC     L
            CALL    L0882
            INC     BC
            CALL    L0895
            JR      L08C8               ; (-01cH)

L08E4:      PUSH    HL
            LD      L,B
            DEC     H
            CALL    L0882
            EX      (SP),HL
            CALL    L089A
            POP     HL
            JR      L08C8               ; (-029H)

            LD      HL,L0071
            LD      (L005D),HL
L08F7:      LD      HL,L1800
            LD      (L0056),HL
            LD      HL,L2700
            LD      (L005B),HL
            XOR     A
            LD      (L004E),A
            RET     

            CALL    SKPDE
L090B:      LD      A,(DE)
            INC     DE
            OR      A
            RET     Z
            LD      C,A
            CP      005H
            JR      Z,L0933             ; (+01fH)
            CP      006H
            JR      Z,L0932             ; (+01aH)
            SUB     041H
            CP      01AH
            JR      NC,HCURON           ; (+00dH)
            LD      A,000H
            OR      A
            JR      Z,HCURON            ; (+008H)
            LD      A,C
            LD      HL,L08F7
            CALL    ADDHLA
            LD      C,(HL)
HCURON:     LD      A,C
            RST     018H
            DB      003H
            CP      00DH
            JR      NZ,L090B            ; (-027H)
L0932:      XOR     A
L0933:      LD      (0091FH),A
            JR      L090B               ; (-02dH)

            AND     C
            SBC     A,D
            SBC     A,A
HCUROF:     SBC     A,H
            SUB     D
            XOR     D
            SUB     A
            SBC     A,B
            AND     (HL)
            XOR     A
            XOR     C
            CP      B
            OR      E
FLASH:      OR      B
            OR      A
            SBC     A,(HL)
            AND     B
            SBC     A,L
            AND     H
            SUB     (HL)
            AND     L
CURMOV:     XOR     E
            AND     E
            SBC     A,E
            CP      L
            AND     D
            NOP     
            NOP     
L0954:      NOP     
L0955:      PUSH    DE
            PUSH    HL
            LD      (L0954),A
            LD      (00952H),DE
            LD      HL,0E007H
            DI      
            OUT     (LSE3),A
            LD      (HL),0B4H
            LD      (HL),074H
            DEC     HL
            LD      (HL),0C0H
            LD      (HL),0A8H
            DEC     HL
            LD      (HL),002H
            LD      (HL),000H
L0972:      LD      HL,0E007H
            LD      (HL),080H
PONTCB:     DEC     HL
            LD      E,(HL)
            LD      D,(HL)
PONTB:      LD      HL,0A8C0H
            OR      A
            SBC     HL,DE
            JR      NZ,L0972            ; (-010H)
            LD      HL,0E005H
            LD      (HL),0F6H
            LD      (HL),079H
            OUT     (LSE1),A
            EI      
            POP     HL
            POP     DE
            RET     

L098F:      PUSH    BC
            PUSH    HL
            DI      
            OUT     (LSE3),A
            LD      HL,0E007H
            LD      (HL),080H
            LD      (HL),040H
            DEC     HL
            LD      E,(HL)
            LD      D,(HL)
            DEC     HL
            LD      C,(HL)
            LD      B,(HL)
            OUT     (LSE1),A
            EI      
            PUSH    BC
            LD      A,D
L09A6:      OR      E
            JR      NZ,L09AC            ; (+003H)
            LD      DE,0A8C0H
L09AC:      LD      HL,0A8C0H
            LD      A,(L0954)
            OR      A
            SBC     HL,DE
            LD      DE,L5460
            CALL    L09EB
            OR      A
            LD      DE,(00952H)
            RR      D
            RR      E
            PUSH    AF
            ADD     HL,DE
            LD      DE,L5460
            CALL    L09EB
            LD      C,A
            ADD     HL,HL
            EX      DE,HL
            POP     AF
            LD      A,C
            POP     BC
            PUSH    AF
            OR      A
            LD      HL,L3CFB
            SBC     HL,BC
            JR      C,L09DC             ; (+001H)
            INC     DE
L09DC:      POP     AF
            JR      NC,L09E0            ; (+001H)
            INC     DE
L09E0:      EX      DE,HL
            LD      DE,0A8C0H
            CALL    L09EB
            EX      DE,HL
            POP     HL
            POP     BC
            RET     

L09EB:      OR      A
            SBC     HL,DE
            JR      NC,L09F2            ; (+002H)
            ADD     HL,DE
            RET     

L09F2:      XOR     001H
            RET     

L09F5:      LD      A,04EH
            DEC     L
            RET     Z
            LD      A,056H
            DEC     L
            RET     Z
            LD      A,05EH
            DEC     L
L0A00:      RET     Z
L0A01:      LD      A,066H
            DEC     L
            RET     

L0A05:      PUSH    DE
            LD      (00A23H),A
            LD      DE,0FFFFH
            LD      HL,0E002H
            DI      
            OUT     (LSE3),A
L0A12:      BIT     7,(HL)
            JR      Z,L0A12             ; (-004H)
L0A16:      BIT     7,(HL)
            JR      NZ,L0A16            ; (-004H)
L0A1A:      LD      L,008H
            LD      A,011H
L0A1E:      DEC     A
            JR      NZ,L0A1E            ; (-003H)
L0A21:      INC     DE
            BIT     0,(HL)
            JR      Z,L0A21             ; (-005H)
            OUT     (LSE1),A
            EI      
            LD      A,D
            OR      A
            LD      A,E
            JR      Z,L0A30             ; (+002H)
            LD      A,0FFH
L0A30:      LD      (L0066),A
            POP     DE
            POP     HL
            SCF     
            RET     

            INC     A
            PUSH    HL
            LD      L,A
            CALL    L09F5
            JR      Z,L0A05             ; (-03aH)
            CALL    L09F5
            LD      (00A75H),A
            LD      (00A55H),A
            LD      HL,0E002H
            DI      
            OUT     (LSE3),A
L0A4E:      BIT     7,(HL)
            JR      Z,L0A4E             ; (-004H)
            LD      L,008H
            BIT     0,(HL)
            LD      A,L
            JR      Z,L0A5A             ; (+001H)
            XOR     A
L0A5A:      LD      (00A7CH),A
            OUT     (LSE1),A
            LD      A,009H
L0A61:      PUSH    AF
            XOR     A
L0A63:      DEC     A
            JR      NZ,L0A63            ; (-003H)
            POP     AF
BINPUT:     DEC     A
            JR      NZ,L0A61            ; (-009H)
            OUT     (LSE3),A
            LD      L,002H
L0A6E:      BIT     7,(HL)
            JR      Z,L0A6E             ; (-004H)
            LD      L,008H
            BIT     0,(HL)
            LD      A,L
            JR      Z,L0A7A             ; (+001H)
            XOR     A
L0A7A:      LD      L,A
            LD      A,000H
            AND     L
            LD      (L0065),A
            OUT     (LSE1),A
            EI      
GETL:       POP     HL
            OR      A
            RET     

            DI      
            RET     

            EI      
            RET     

            DS      110, 000H
L0AF9:      DS      30, 000H
L0B17:      DS      3, 000H
INKEY$:     DS      7, 000H
INKEY1:     DS      10, 000H
L0B2B:      JP      PE,0900BH
            RLA     
            CALL    M,00019H
            DEC     SP
            LD      A,(L180D)
            DJNZ    L0B4A               ; (+012H)
            LD      DE,01413H
            CCF     
            CPL     
L0B3D:      LD      HL,(0900CH)
            RLA     
            EI      
            DEC     B
            NOP     
            DEC     HL
            LD      HL,(0160DH)
            DEC     D
            LD      (DE),A
L0B4A:      LD      DE,01413H
            ADD     A,05FH
L0B4F:      JP      (HL)

            INC     C
            SUB     B
            RLA     
            LD      L,B
            DEC     B
            NOP     
            ADD     A,H
            JP      (HL)

            DEC     C
            LD      D,015H
            LD      (DE),A
            LD      DE,01413H
            ADC     A,A
            ADC     A,E
L0B61:      LD      L,D
            INC     C
            SUB     B
            RLA     
            LD      L,H
            ADD     HL,DE
            NOP     
            CP      089H
            DEC     C
            LD      D,015H
            LD      (DE),A
            LD      DE,01413H
            ADC     A,D
            LD      A,E
L0B73:      LD      E,E
            LD      E,H
            LD      E,L
            LD      E,(HL)
            CPL     
$CRT:       DB      091H,00BH,"CRT",000H,08AH ; $CRT - CRT device descriptor
            DS      2, 000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
INKEYF:     DB      0AAH
            DB      00BH
            DB      0B5H
            DB      00BH
            DB      0C4H
            DB      00BH
$KB_:       DB      000H                ; $KB - Keyboard device descriptor
            DB      00FH
            DB      04BH
            DB      042H
            DS      2, 000H
            DB      081H
            DS      2, 000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
INKEY0:     DB      000H
            DB      LSE5
            DB      000H
            DB      0AAH
            DB      00BH
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            RST     018H
            DB      00CH
            LD      A,080H
            RET     C
            JP      C,0017DH
            RST     018H
            DB      017H
            RET     

            EX      AF,AF'
            LD      HL,L0BFF
L0BB9:      LD      A,(DISPX)
            OR      A
            JR      Z,L0BC2             ; (+003H)
            LD      HL,L0C43
L0BC2:      EX      AF,AF'
            JP      (HL)

            LD      A,(L0054)
            RET     

            LD      A,00DH
L0BCA:      PUSH    AF
            LD      A,(FILOUT)
L0BCE:      OR      A
            JR      NZ,L0BD4            ; (+003H)
            POP     AF
            JR      L0BFF               ; (+02bH)

L0BD4:      POP     AF
            JP      L0F3E

            PUSH    AF
            LD      A,(FILOUT)
            OR      A
            JR      NZ,L0BE2            ; (+003H)
            POP     AF
            JR      L0C43               ; (+061H)

L0BE2:      POP     AF
            JP      L0F2E

            CALL    SKPDE
            LD      HL,L0BCA
            JR      L0BF4               ; (+006H)

L0BEE:      CALL    SKPDE
            LD      HL,L0BFF
L0BF4:      LD      A,(DE)
            OR      A
            RET     Z
            CALL    L00FA
            INC     DE
            JR      L0BF4               ; (-009H)

L0BFD:      LD      A,020H
L0BFF:      CALL    IOOUT
L0C02:      LD      C,A
            LD      A,(L004D)
            OR      A
            JR      Z,L0C3A             ; (+031H)
            LD      A,C
            CP      020H
            JR      NC,L0C37            ; (+029H)
            LD      DE,(00054H)
            LD      HL,(L005B)
            CP      014H
            JR      Z,L0C21             ; (+008H)
            CP      012H
            JR      NZ,L0C26            ; (+009H)
            LD      HL,(L0056)
            LD      E,D
L0C21:      LD      A,L
            CP      E
            JP      NC,L0D3F
L0C26:      LD      A,C
            LD      HL,L0C8F
            ADD     A,L
            LD      L,A
            JR      NC,L0C2F            ; (+001H)
            INC     H
L0C2F:      LD      A,(HL)
            INC     A
            JR      Z,L0C3A             ; (+007H)
            DEC     A
            JP      Z,L0D3F
L0C37:      CALL    L0C60
L0C3A:      LD      A,C
            CP      020H
            JP      C,L065E
            JP      L0600

L0C43:      CALL    IOOUT
            LD      C,A
            CP      00DH
            JR      Z,L0C02             ; (-049H)
            CALL    L0600
            LD      A,(L004D)
            OR      A
            RET     Z
            LD      A,C
            CP      011H
            JR      C,L0C8A             ; (+032H)
            CP      017H
            JR      C,L0C8C             ; (+030H)
            CP      020H
            JR      C,L0C8A             ; (+02aH)
L0C60:      CP      060H
            JR      C,L0C8C             ; (+028H)
            CALL    CHKACC
            ADD     HL,BC
            ADD     A,B
            LD      A,E
            ADD     A,08BH
            RST     010H
            RST     008H
            CALL    M,0FFFBH
            JR      Z,L0C8C             ; (+019H)
            CP      092H
            JR      C,L0C8A             ; (+013H)
            CALL    CHKACC
            ADD     HL,BC
            SBC     A,C
            SUB     L
            XOR     H
            AND     A
            OR      H
            OR      C
            OR      (HL)
            OR      L
            CP      H
            JR      Z,L0C8A             ; (+004H)
            CP      0BFH
            JR      C,L0C8C             ; (+002H)
L0C8A:      LD      A,02EH
L0C8C:      JP      L0FA9

; ---------------------------------------------------------------------------
; Control character translation table (indexed by char code 0x00-0x1F)
; Used by L0C26: HL = L0C8F + C, A = (HL).
;   0x00 → handle via L0D3F,  0xFF → skip,  other → translate via L0C60
; ---------------------------------------------------------------------------
L0C8F:      DB      000H,000H,000H,000H ; 00-03: NUL,SOH,STX,ETX → L0D3F
            DB      0FFH,0FFH,0FFH,01DH ; 04-07: EOT,ENQ,ACK → skip; BEL → 0x1D
            DB      000H,000H,000H,000H ; 08-0B: BS,HT,LF,VT → L0D3F
            DB      000H,00DH,000H,000H ; 0C-0F: FF → L0D3F; CR → 0x0D; SO,SI → L0D3F
            DB      000H,00AH,003H,020H ; 10-13: DLE → L0D3F; DC1 → 0x0A; DC2 → 0x03; DC3 → 0x20
            DB      00EH,000H,000H,0FFH ; 14-17: DC4 → 0x0E; NAK,SYN → L0D3F; ETB → skip
            DB      000H,0FFH,0FFH,00DH ; 18-1B: CAN → L0D3F; EM,SUB → skip; ESC → 0x0D
            DB      0FFH,000H,000H,000H ; 1C-1F: FS → skip; GS,RS,US → L0D3F
L0CAF:      DI      
            XOR     A
            LD      (L004D),A
            LD      (FILOUT),A
            LD      SP,L0000
            OUT     (LSE1),A
            LD      A,004H
            RST     018H
            DB      003H
L0CC0:      LD      HL,L1B18
            XOR     A
            LD      (L0CC0),A
            LD      D,A
            LD      E,A
            RST     018H
            DB      026H
            LD      DE,$CMT
            LD      A,B
            DEC     A
            JR      Z,L0CDB             ; (+009H)
            LD      DE,$FD
            DEC     A
            JR      Z,L0CDB             ; (+003H)
            LD      DE,$QD
L0CDB:      LD      A,C
            RST     018H
            DB      03AH
            JP      L00DA

            LD      HL,(TEXTED)
            ADD     HL,DE
            LD      (TEXTED),HL
            LD      HL,(VARST)
            ADD     HL,DE
            LD      (VARST),HL
            LD      HL,(STRST)
            ADD     HL,DE
            LD      (STRST),HL
            LD      HL,(VARED)
            ADD     HL,DE
            LD      (VARED),HL
            LD      HL,(TMPEND)
            ADD     HL,DE
            LD      (TMPEND),HL
            RET     

L0D05:      LD      HL,(00B15H)
L0D08:      LD      A,H
            OR      L
            JR      Z,L0D20             ; (+014H)
            PUSH    DE
            EX      DE,HL
            LD      HL,0E004H
            DI      
            OUT     (LSE3),A
            LD      (HL),E
            LD      (HL),D
            LD      A,001H
            LD      (TEMPO_),A
            OUT     (LSE1),A
            EI      
            POP     DE
            RET     

L0D20:      LD      A,036H
BRKEY:      DI      
            OUT     (LSE3),A
            LD      (0E007H),A
            XOR     A
            LD      (TEMPO_),A
            OUT     (LSE1),A
            EI      
            RET     

L0D30:      PUSH    AF
            PUSH    BC
            AND     00FH
            LD      B,A
            LD      A,008H
            SUB     B
            LD      (0119EH),A
            POP     BC
            POP     AF
            RET     

L0D3E:      XOR     A
L0D3F:      PUSH    HL
            LD      HL,L04EC
            JR      Z,L0D4E             ; (+009H)
            CP      001H
L0D47:      JR      Z,L0D59             ; (+010H)
            CALL    L0D08
            POP     HL
            RET     

L0D4E:      CALL    L0D08
            LD      HL,L2710
L0D54:      DEC     HL
            LD      A,H
            OR      L
            JR      NZ,L0D54            ; (-005H)
L0D59:      CALL    L0D20
            POP     HL
            RET     

; ---------------------------------------------------------------------------
; $USR Device Descriptor (User-defined device)
;
; Handlers pointing to 0x00E5 (monitor ROM) are stubs (no-op/error).
; ZRO and ZWO share the same handler (USR_RO).
; ZINP and ZOUT share the same handler (USR_IO).
; ZWDIR and ZFREE are not emitted explicitly — USR_RO code at 0x0D77
; overlaps these 2 DW fields (the first 4 bytes of USR_RO: 21 01 10 DF
; encode ZWDIR=0x0121, ZFREE=0xDF10). Neither is called (flags1 bit7=1).
; ---------------------------------------------------------------------------
$USR:       DW      00000H              ; Next device = NULL (end of chain)
L0D60:      DB      "USR",000H          ; Device name
            DB      09FH                ; flags1: bit7=1 (no dir/free), bit4-0=0x1F
            DB      000H                ; flags2
            DB      000H                ; dirmx: 0
            DW      000E5H              ; ZINIT (monitor ROM stub)
            DW      USR_RO              ; ZRO handler (read/write open)
            DW      USR_RO              ; ZWO handler (same as ZRO)
            DW      000E5H              ; ZCL (monitor ROM stub)
            DW      000E5H              ; ZKL (monitor ROM stub)
            DW      USR_IO              ; ZINP handler (user I/O dispatch)
            DW      USR_IO              ; ZOUT handler (same as ZINP)
            DW      000E5H              ; ZDELT (monitor ROM stub)
; ZWDIR/ZFREE implicit — next 4 bytes are USR_RO code, read as DW pointers
;
; ---------------------------------------------------------------------------
; USR_RO - User device open (ZRO/ZWO handler)
;
; Calls SVC #13H to find the USR device entry point, stores it in ZWRK1.
; ---------------------------------------------------------------------------
USR_RO:     LD      HL,ELMD1
            RST     018H
            DB      013H
            LD      A,D
            OR      E
            JP      Z,01F9AH
            LD      (ZWRK1),DE
            RET
;
; ---------------------------------------------------------------------------
; USR_IO - User device I/O (ZINP/ZOUT handler)
;
; Dispatches to the user-supplied routine address stored in ZWRK1.
; ---------------------------------------------------------------------------
USR_IO:     LD      HL,(ZWRK1)
            JP      (HL)

            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            LD      A,(ZFLAG1)
            BIT     7,A
            JP      NZ,01F97H
            JP      L1989

L0D9B:      LD      B,(IY+000H)
            CALL    L1C39
            LD      A,(ELMD)
            CP      B
            JP      NZ,01F9DH
            RET     

L0DA9:      LD      A,(L004D)
            OR      A
            LD      A,(LPOSB)
            RET     Z
            LD      A,(L0054)
            RET     

            DS      40, 000H
EQTBL:      DS      25, 000H
$KB:        DS      10, 000H
L0E00:      DS      16, 000H
L0E10:      DS      46, 000H
L0E3E:      DS      35, 000H
CRT1C:      DS      82, 000H
PLTOTX:     DS      77, 000H
$LPT:       DB      0F7H,"/LPT",000H,08AH ; $LPT - Line printer device descriptor
            DS      2, 000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DB      000H
            DB      LSE5
            DS      3, 000H
            DB      027H
            DB      00FH
            DB      019H
            DB      00FH
            JP      L0DA9

            RET     

L0F1D:      LD      A,00DH
            CALL    L0FA9
            XOR     A
            LD      (LPOSB),A
            RET     

            LD      HL,DISPX
            BIT     0,(HL)
            JR      Z,L0F3E             ; (+010H)
L0F2E:      CALL    IOOUT
            LD      B,A
            LD      A,(L004D)
            OR      A
            LD      A,B
            LD      E,0FFH
            JR      Z,L0F4E             ; (+013H)
            RST     018H
            DB      004H
            RET     

L0F3E:      CALL    IOOUT
            LD      B,A
            LD      A,(L004D)
            OR      A
            LD      A,B
            LD      E,000H
            JR      Z,L0F4E             ; (+003H)
            RST     018H
            DB      003H
            RET     

L0F4E:      LD      A,(PNMODE)
            CP      001H
            LD      A,044H
            JP      NZ,00180H
            LD      A,B
            CP      00DH
            JR      Z,L0F1D             ; (-040H)
            LD      HL,LPOSB
            INC     (HL)
            SUB     005H
            CP      002H
            JR      C,L0F7C             ; (+015H)
            LD      A,B
            CP      020H
            JR      NC,L0FB0            ; (+044H)
            BIT     0,E
            JR      Z,L0F80             ; (+010H)
            SUB     011H
            CP      006H
            JR      C,L0FB1             ; (+03bH)
            SUB     QDPC
            CP      002H
            JR      NC,L0FAE            ; (+032H)
L0F7C:      LD      A,02EH
            JR      L0FB0               ; (+030H)

L0F80:      LD      HL,LPOSB
            DEC     (HL)
            LD      A,B
            CP      015H
            LD      B,00FH
            JR      Z,L0FB1             ; (+026H)
            CP      011H
            LD      B,009H
            JR      Z,L0FB1             ; (+020H)
            CP      012H
            LD      B,00BH
            JR      Z,L0FB1             ; (+01aH)
            CP      016H
            JR      NZ,L0FB0            ; (+015H)
            LD      A,00CH
            CALL    L0FB0
            LD      A,00AH
            CALL    L0FB0
            LD      A,003H
            JR      L0FB0               ; (+007H)

L0FA9:      CALL    IOOUT
            JR      L0FB0               ; (+002H)

L0FAE:      LD      A,020H
L0FB0:      LD      B,A
L0FB1:      CALL    L0FCA
            LD      A,B
            DI      
            CALL    L0FBB
            EI      
            RET     

L0FBB:      OUT     (0FFH),A
            LD      A,080H
            OUT     (0FEH),A
            LD      A,001H
            CALL    L0FCB
L0FC6:      XOR     A
            OUT     (0FEH),A
            RET     

L0FCA:      XOR     A
L0FCB:      CALL    SKPDE
            LD      C,A
            LD      B,00EH
L0FD1:      LD      HL,0E678H
L0FD4:      IN      A,(0FEH)
            AND     00DH
            CP      C
            RET     Z
            DEC     HL
            LD      A,H
            OR      L
            NOP     
            NOP     
            JR      NZ,L0FD4            ; (-00dH)
            DJNZ    L0FD1               ; (-012H)
            XOR     A
            LD      (L004D),A
            CALL    L0FC6
            LD      A,041H
            JP      L0182

            DS      17, 000H
; ===========================================================================
; BASIC System Workspace (0x1000-0x109F)
; ===========================================================================
;
; --- ELMD: File header / element data (0x1000-0x103F) ---
ELMD:       DB      000H                ; File type (0=obj, 1=BTX, 2=BASIC, 3=data, 5=MZF)
ELMD1:      DS      17, 000H            ; Filename (17 bytes, null-padded)
ELMD18:     DW      00000H              ; File size (bytes)
ELMD20:     DW      00000H              ; Load address / BASIC program size
ELMD22:     DW      00000H              ; Execution address
ELMD24:     DW      00000H              ; Reserved
ELMD26:     DW      00000H              ; Reserved
            DS      2, 000H
ELMD30:     DW      00000H              ; Reserved
ELMD32:     DW      00000H              ; Reserved
            DS      6, 000H
SDDRIVENO:  DB      000H                ; SD drive number (numeric 0-9)
CMTFILENO:  DB      000H                ; Sequential file counter
            DS      20, 000H
L103E:      DS      2, 000H
;
; --- Device workspace (0x1040-0x106F) ---
ZTOP:       DW      00002H              ; Top of device chain pointer
ZLOG:       DB      000H                ; Logical unit number
ZRWX:       DB      000H                ; Read/write mode
ZEQT:       DW      00000H              ; Equipment/device type
ZCH:        DB      000H                ; Channel number
ZEOF:       DB      000H                ; End of file flag
ZWRK1:      DW      00000H              ; Work area 1 (16-bit)
ZNXT:       DW      00000H              ; Next byte pointer
ZDEVNM:     DS      4, 000H             ; Active device name (4 bytes)
ZFLAG1:     DB      001H                ; Active device flags1
ZFLAG2:     DB      000H                ; Active device flags2
ZDIRMX:     DB      000H                ; Active device max dir entries
ZINIT:      DW      00000H              ; Active ZINIT function pointer
ZRO:        DW      00000H              ; Active ZRO function pointer
ZWO:        DW      00000H              ; Active ZWO function pointer
ZCL:        DW      00000H              ; Active ZCL function pointer
ZKL:        DW      00000H              ; Active ZKL function pointer
ZINP:       DW      00000H              ; Active ZINP function pointer
ZOUT:       DW      00000H              ; Active ZOUT function pointer
ZDELT:      DW      00000H              ; Active ZDELT function pointer
ZWDIR:      DW      00000H              ; Active ZWDIR function pointer
ZFREE:      DW      00000H              ; Active ZFREE function pointer
            DS      2, 000H
DCHAN:      DB      000H                ; Default channel
DDEV:       DW      00000H              ; Default device descriptor pointer
_CRT:       DW      00B78H              ; CRT device descriptor pointer
_LPT:       DW      00F00H              ; LPT device descriptor pointer
;
; --- BASIC memory pointers (0x1070-0x109F) ---
TEXTST:     DW      00000H              ; Start of BASIC text
TEXTED:     DW      00000H              ; End of BASIC text
VARST:      DW      00000H              ; Start of variables
STRST:      DW      00000H              ; Start of string space
VARED:      DW      00000H              ; End of variables
TMPEND:     DW      00000H              ; End of temporary area
INTFAC:     DW      00000H              ; Integer accumulator
MEMLMT:     DW      00000H              ; Memory limit
MEMMAX:     DW      0FF00H              ; Maximum memory
CURXY:      DW      00000H              ; Cursor X,Y position
POSADR:     DW      00000H              ; Cursor VRAM address
BITADR:     DW      00000H              ; Bit address
POINTX:     DW      00000H              ; Graphics point X
POINTY:     DW      00000H              ; Graphics point Y
CURFLG:     DB      000H                ; Cursor flag
CURMAK:     DB      000H                ; Cursor make flag
CMTMSG:     DB      000H                ; CMT message flag
INPFLG:     DB      000H                ; Input flag
DISPX:      DB      000H                ; Display X position
FILOUT:     DB      000H                ; File output flag
PSEL:       DB      001H                ; Printer select
PCRLF:      DB      000H                ; Printer CR/LF mode
LPT_TM:     DB      00EH                ; LPT timeout
LPOSB:      DB      000H                ; LPT position
PSMAL:      DB      000H                ; Printer small flag
PNMODE:     DB      001H                ; Printer mode
DMD:        DB      000H                ; Display mode
MEMOP:      DB      000H                ; Memory operation flag
PWMODE:     DB      000H                ; Power mode
CMODE:      DB      000H                ; Color mode
CPLANE:     DB      000H                ; Color plane
MAXCF:      DB      000H                ; Max color flag
PMASK:      DB      000H                ; Plane mask
GMODE:      DB      000H                ; Graphics mode
            DS      80, 000H            ; 0x10A0-0x10EF (padding to L10F0)
L10F0:      DS      78, 000H
L113E:      DS      84, 000H
            RST     028H
            NOP     
            NOP     
            NOP     
            NOP     
SUMDT:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            LD      BC,00504H
            DS      3, 000H
KEYBM1:      DB      000H
KEYBUF:      DS      154, 000H
L123E:      DS      57, 000H
L1277:      DS      73, 000H
; ---------------------------------------------------------------------------
; Function key definition table (F1-F10 defaults, stored in RAM)
; Each entry: 1 byte length + string + zero-padded to 16 bytes total.
; Loaded at startup, modifiable via DEF KEY statement.
; ---------------------------------------------------------------------------
L12C0:      DB      007H,"RUN",020H,020H,020H,00DH  ; F1: RUN   <CR>
            DS      8, 000H
            DB      005H,"LIST",020H                  ; F2: LIST
            DS      10, 000H
            DB      005H,"AUTO",020H                  ; F3: AUTO
            DS      10, 000H
            DB      006H,"RENUM",020H                 ; F4: RENUM
            DS      9, 000H
            DB      004H,"DIR",020H                   ; F5: DIR
            DS      11, 000H
            DB      005H,"CHR$("                      ; F6: CHR$(
            DS      10, 000H
            DB      008H,"DEF KEY("                   ; F7: DEF KEY(
            DS      7, 000H
            DB      004H,"CONT"                       ; F8: CONT
            DS      9, 000H
L133E:      DS      2, 000H                           ; (last 2 bytes of F8 entry, used as workspace)
            DB      006H,"SAVE",020H,020H             ; F9: SAVE
            DS      9, 000H
            DB      006H,"LOAD",020H,020H             ; F10: LOAD
            DS      9, 000H
L1360:      DB      000H                              ; F11: (empty/unused)
L1361:      DS      15, 000H                          ; (F11 padding, used as workspace)
            DB      000H                              ; End marker
L1371:      DB      000H                              ; End marker
            CALL    L13D8
            JR      Z,L1385             ; (+00eH)
L1377:      CP      010H
            JR      C,L139B             ; (+020H)
            CP      080H
            JR      Z,L13A1             ; (+022H)
            CP      0C0H
            JR      Z,L13A1             ; (+01eH)
            JR      L1391               ; (+00cH)

L1385:      CP      003H
            JR      Z,L139B             ; (+012H)
            CP      005H
            JR      Z,L139E             ; (+011H)
            CP      006H
            JR      Z,L139E             ; (+00dH)
L1391:      DI      
            OUT     (LSE2),A
            CALL    L0BB9
            OUT     (LSE0),A
            EI      
            RET     

L139B:      LD      A,0F0H
            RET     

L139E:      ADD     A,0C9H
            RET     

L13A1:      SUB     040H
            RET     

L13A4:      CP      0F0H
            JR      NC,L13CF            ; (+027H)
            CP      073H
            JR      Z,L139B             ; (-011H)
            CALL    L13D8
            JR      Z,L13BB             ; (+00aH)
            CP      040H
            JR      Z,L13D5             ; (+020H)
            CP      080H
            JR      Z,L13D5             ; (+01cH)
            JR      L13C3               ; (+008H)

L13BB:      CP      0CEH
            JR      Z,L13D2             ; (+013H)
            CP      0CFH
            JR      Z,L13D2             ; (+00fH)
L13C3:      DI      
            OUT     (LSE2),A
            CALL    L0BCE
            OUT     (LSE0),A
            EI      
            CP      0F0H
            RET     NZ
L13CF:      LD      A,020H
            RET     

L13D2:      SUB     0C9H
            RET     

L13D5:      ADD     A,040H
            RET     

L13D8:      PUSH    HL
            LD      HL,L0036
            BIT     6,(HL)
            POP     HL
            RET     

L13E0:      PUSH    AF
            LD      A,L
            SUB     008H
            JR      C,L13EA             ; (+004H)
            SUB     030H
            JR      C,L13F1             ; (+007H)
L13EA:      ADD     A,00AH
            LD      L,A
            ADD     HL,BC
            LD      C,(HL)
            POP     AF
            RET     

L13F1:      LD      A,(BC)
            PUSH    AF
            INC     BC
            LD      A,(BC)
            LD      B,A
            POP     AF
            LD      C,A
            ADD     HL,BC
            DI      
            OUT     (LSE2),A
            LD      A,(HL)
            OUT     (LSE0),A
            EI      
            CALL    L13A4
            LD      C,A
            POP     AF
            RET     

            PUSH    HL
            PUSH    BC
            PUSH    DE
            LD      DE,L2710
            CALL    L142C
            LD      DE,003E8H
            CALL    L142C
            LD      DE,L0064
            CALL    L142C
            LD      DE,0000AH
            CALL    L142C
            LD      A,L
            POP     DE
            OR      030H
            LD      (DE),A
            INC     DE
            XOR     A
            LD      (DE),A
            POP     BC
            POP     HL
            RET     

L142C:      LD      A,0FFH
L142E:      INC     A
            OR      A
            SBC     HL,DE
            JR      NC,L142E            ; (-006H)
            ADD     HL,DE
            OR      A
            JR      NZ,L143B            ; (+003H)
            OR      B
            RET     Z
            XOR     A
L143B:      LD      B,001H
            OR      030H
            POP     DE
            EX      (SP),HL
            LD      (HL),A
            INC     HL
            EX      (SP),HL
            PUSH    DE
            RET     

            CALL    TEST1
            DB      024H
            JR      Z,L1476             ; (+02aH)
            LD      DE,L0000
L144F:      CALL    HLFTCH
            SUB     030H
            CP      00AH
            RET     NC
            INC     HL
            PUSH    HL
            LD      H,D
            LD      L,E
            ADD     HL,HL
            JR      C,L1471             ; (+013H)
            ADD     HL,HL
            JR      C,L1471             ; (+010H)
            ADD     HL,DE
            JR      C,L1471             ; (+00dH)
            ADD     HL,HL
            JR      C,L1471             ; (+00aH)
            LD      E,A
            LD      D,000H
            ADD     HL,DE
            JR      C,L1471             ; (+004H)
            EX      DE,HL
            POP     HL
            JR      L144F               ; (-022H)

L1471:      LD      A,002H
            JP      L0182

L1476:      LD      DE,L0000
L1479:      LD      A,(HL)
            CALL    L1491
            RET     C
            INC     HL
            EX      DE,HL
            ADD     HL,HL
            JR      C,L1471             ; (-012H)
            ADD     HL,HL
            JR      C,L1471             ; (-015H)
            ADD     HL,HL
            JR      C,L1471             ; (-018H)
            ADD     HL,HL
            JR      C,L1471             ; (-01bH)
            ADD     A,L
            LD      L,A
            EX      DE,HL
            JR      L1479               ; (-018H)

L1491:      SUB     030H
            CP      00AH
            CCF     
            RET     NC
            SUB     011H
            CP      006H
            CCF     
            RET     C
            ADD     A,00AH
            RET     

            CALL    L14AD
            CP      020H
            RET     NZ
L14A6:      CALL    L14AD
            OR      A
            JR      Z,L14A6             ; (-006H)
            RET     

L14AD:      RST     018H
            DB      00EH
            JR      Z,L14B8             ; (+007H)
            LD      A,0FFH
            RST     018H
            DB      00DH
            CP      01BH
            RET     NZ
L14B8:      JP      0017DH

            RET     

L14BC:      CALL    SKPDE
            LD      HL,KEYBUF
            LD      B,000H
            PUSH    HL
            CALL    LDHLDE
            POP     DE
            LD      (014DAH),SP
            LD      SP,L10F0
            DI      
            OUT     (LSE4),A
            RST     030H
            OUT     (LSE0),A
            OUT     (LSE1),A
            EI      
            LD      SP,L0000
            RET     

            LD      (DDEV),DE
            LD      (DCHAN),A
            RET     

            LD      C,A
            RST     018H
            DB      020H
            RST     018H
            DB      002H
            BIT     7,C
            JR      Z,L14F8             ; (+00aH)
            LD      HL,KEYBUF
            PUSH    HL
            CALL    L1535
            POP     DE
            RST     018H
            DB      005H
L14F8:      LD      A,C
            AND     07FH
            JR      L14FF               ; (+002H)

L14FD:      LD      A,045H
L14FF:      LD      DE,L1554
L1502:      DEC     A
            JR      Z,L150F             ; (+00aH)
            EX      AF,AF'
L1506:      LD      A,(DE)
            INC     DE
            OR      A
            JP      P,L1506
            EX      AF,AF'
            JR      L1502               ; (-00dH)

L150F:      LD      A,(DE)
            CP      080H
            JR      Z,L14FD             ; (-017H)
L1514:      LD      A,(DE)
            OR      A
            JP      M,L151F
            CALL    L152A
            INC     DE
            JR      L1514               ; (-00bH)

L151F:      AND     07FH
            CALL    L152A
            LD      DE,L179E
            RST     018H
            DB      005H
            RET     

L152A:      PUSH    DE
            LD      DE,L1533
            LD      (DE),A
            RST     018H
            DB      005H
            POP     DE
            RET     

L1533:      NOP     
            NOP     
L1535:      LD      DE,(ZEQT)
            INC     DE
            INC     DE
            RST     018H
            DB      017H
            CALL    LDHLDE
            LD      A,(ZCH)
            ADD     A,031H
            LD      (HL),A
            LD      A,(01051H)
            AND     00FH
            JR      Z,L154E             ; (+001H)
            INC     HL
L154E:      LD      (HL),03AH
            INC     HL
            LD      (HL),000H
            RET     

L1554:      DB      053H,005H,"YNTA",0D8H,04FH,005H,"VER FLO",0D7H,049H,005H ; Error message table (bit-7 terminated)
            DS      2, 04CH
            DB      "EGAL DAT",0C1H,054H,005H,"YPE MISMATC",0C8H,053H,005H,"TRING LENGT",0C8H,04DH,005H,"EMORY CAPACIT",0D9H,041H,005H
            DS      2, 052H
            DB      "AY DEF",0AEH,04CH,005H,"INELENGT",0C8H,080H,"GOSUB ",005H,"NESTIN",0C7H,"FOR-NEX",0D4H,"DEF FN ",005H,"NESTIN",0C7H,"NEX",0D4H,"RETUR",0CEH,055H,005H,"N DEF. FUNCTIO",0CEH,055H,005H,"N DEF. LIN",0C5H,043H,005H,"AN'T ",006H,"CON",0D4H,04DH,005H,"EMORY PROTECTIO",0CEH,049H,005H,"NSTRUCTIO",0CEH,043H,005H,"AN'T ",006H,"RESUM",0C5H,"RESUM",0C5H
            DS      2, 080H
            DB      "REA",0C4H,"SWAP ",005H,"LEVE",0CCH
            DS      2, 080H
            DB      053H,005H,"YSTEM I",0C4H,046H,005H,"RAMIN",0C7H,04FH,005H,056H,045H
            DS      2, 052H
            DB      055H,0CEH,050H,005H,"ARIT",0D9H
            DS      8, 080H
            DB      046H,005H,"ILE NOT FOUN",0C4H,048H,005H,"ARDWAR",0C5H,041H,005H,"LREADY EXIS",0D4H,041H,005H,"LREADY OPE",0CEH,04EH,005H,"OT OPE",0CEH,080H,057H,005H,"RITE PROTEC",0D4H
            DS      3, 080H
            DB      04EH,005H,"OT READ",0D9H,054H,005H
            DS      2, 04FH
            DB      " MANY FILE"
            DB      LSD3
            DB      044H,005H,"ISK MISMATC",0C8H,04EH,005H,"O FILE SPAC",0C5H,055H,005H,"NFORMA",0D4H,054H,005H
            DS      2, 04FH
            DB      " LONG FIL",0C5H
            DS      2, 080H
            DB      044H,005H,"EV. NAM",0C5H,043H,005H,"AN'T EXECUT",0C5H,049H,005H
            DS      2, 04CH
            DB      "EGAL FILENAM",0C5H,049H,005H
            DS      2, 04CH
            DB      "EGAL FILEMOD",0C5H,080H,04FH,005H,"UT OF FIL",0C5H,04CH,005H,"OGICAL NUMBE"
            DB      LSD2
            DB      "LPT:"
L1772:      DB      04EH,005H,"OT READ",0D9H
            DS      2, 080H
            DB      044H,005H,"EV. MOD",0C5H,055H,005H,"NPRINTABL",0C5H,043H,005H,"HECK SU",0CDH
L179E:      DB      020H
            DB      005H
            DB      045H
            DS      2, 052H
            DB      04FH
            DB      052H
            DS      91, 000H
L1800:      JP      L4000

L1803:      LD      A,(ZLOG)
            CP      088H
            RET     

L1809:      NOP     
            NOP     
L180B:      NOP     
            NOP     
L180D:      NOP     
            NOP     
            CALL    L181A
            CALL    L1821
L1815:      XOR     A
            LD      (L1FC0),A
            RET     

L181A:      LD      A,(ZLOG)
            LD      (L1FC0),A
            RET     

L1821:      CALL    SKPDE
            LD      A,(ZFLAG1)
            BIT     7,A
            LD      HL,L1848
L182C:      JR      Z,L183D             ; (+00fH)
            BIT     3,A
            LD      HL,L1835
            JR      NZ,L183D            ; (+008H)
L1835:      LD      IX,(ZOUT)
            CALL    IOCALL
            RET     

L183D:      LD      A,B
L183E:      OR      A
            RET     Z
            LD      A,(DE)
            CALL    L00FA
            INC     DE
            DEC     B
            JR      L183D               ; (-00bH)

L1848:      CALL    SKPDE
            LD      E,(IY+040H)
            LD      D,(IY+041H)
            LD      HL,(L180B)
            ADD     HL,DE
            LD      (HL),A
            INC     DE
            LD      HL,(ZKL)
            OR      A
            SBC     HL,DE
            CALL    Z,L1871
            LD      (IY+040H),E
            LD      (IY+041H),D
            INC     (IY+014H)
            RET     NZ
            INC     (IY+015H)
            RET     NZ
            JP      01F91H

L1871:      PUSH    BC
            LD      B,002H
L1874:      CALL    L1D15
            LD      IX,L1835
            JR      Z,L1881             ; (+004H)
            LD      IX,L2907
L1881:      LD      A,B
            CALL    L18B6
            CALL    L1890
            INC     (IY+04EH)
            LD      DE,L0000
            POP     BC
            RET     

L1890:      JP      (IX)

L1892:      LD      A,01AH
            LD      E,(IY+040H)
            LD      D,(IY+041H)
L189A:      LD      HL,(L180B)
            ADD     HL,DE
            LD      (HL),A
            INC     DE
            LD      HL,(ZKL)
            XOR     A
            SBC     HL,DE
            JR      NZ,L189A            ; (-00eH)
            PUSH    BC
            LD      HL,(L180B)
            DEC     HL
            LD      (HL),0FFH
            DEC     HL
            LD      (HL),0FFH
            LD      B,006H
            JR      L1874               ; (-042H)

L18B6:      LD      BC,(ZKL)
            INC     BC
            INC     BC
            LD      HL,(L180B)
            DEC     HL
            DEC     HL
            LD      DE,(01809H)
            RET     

L18C6:      OR      0AFH
            LD      IX,(ZCL)
            CALL    IOCALL
            RET     

            CALL    L1803
            RET     NZ
            PUSH    DE
            LD      DE,KEYBUF
            CALL    L026A
            LD      (0193CH),DE
            POP     DE
            RET     NC
            JP      0017DH

            PUSH    HL
            PUSH    DE
            CALL    L181A
            LD      HL,(ZTOP)
            INC     HL
            BIT     7,(HL)
            PUSH    HL
            LD      B,000H
            SCF     
            CALL    Z,L1913
            POP     HL
            CALL    C,L1908
            POP     DE
            PUSH    AF
            CALL    L1815
            LD      L,B
            LD      H,000H
            ADD     HL,DE
            LD      (HL),000H
            POP     AF
            POP     HL
            RET     

L1908:      SET     7,(HL)
            LD      HL,01051H
            BIT     4,(HL)
            RET     Z
            JP      01FA0H

L1913:      CALL    L1803
            JR      Z,L193B             ; (+023H)
            LD      A,(ZFLAG1)
            BIT     7,A
            LD      HL,L1989
            JR      Z,L1975             ; (+053H)
            BIT     2,A
            LD      HL,L1929
            JR      NZ,L1975            ; (+04cH)
L1929:      LD      IX,(ZINP)
            CALL    IOCALL
            RET     

L1931:      LD      A,03FH
            RST     018H
            DB      003H
            LD      A,020H
            RST     018H
            DB      003H
            RST     018H
            DB      034H
L193B:      LD      HL,L0000
L193E:      CALL    HLFTCH
            OR      A
            JR      Z,L1931             ; (-013H)
            LD      C,000H
            RST     018H
            DB      036H
            LD      (0193CH),HL
            RET     

            LD      B,000H
            CALL    TEST1
            DB      022H
            JR      NZ,L1967            ; (+013H)
L1954:      LD      A,(HL)
            OR      A
            RET     Z
            INC     HL
            CP      022H
            JR      Z,L1961             ; (+005H)
            LD      (DE),A
            INC     DE
            INC     B
            JR      L1954               ; (-00dH)

L1961:      CALL    TEST1
            DB      02CH
            OR      A
            RET     

L1967:      LD      A,(HL)
            OR      A
            RET     Z
            CP      C
            RET     Z
            INC     HL
            CP      02CH
            RET     Z
            LD      (DE),A
            INC     DE
            INC     B
            JR      L1967               ; (-00eH)

L1975:      LD      (0197BH),HL
            LD      B,000H
L197A:      CALL    L0000
            RET     C
            CP      00DH
            RET     Z
            LD      (DE),A
            INC     DE
            INC     B
            JR      NZ,L197A            ; (-00cH)
            JP      01F76H

L1989:      CALL    SKPDE
            LD      E,(IY+040H)
            LD      D,(IY+041H)
            LD      HL,(ZKL)
            OR      A
            SBC     HL,DE
            CALL    Z,L19B4
            RET     C
            LD      L,(IY+04EH)
            LD      H,(IY+04FH)
            XOR     A
            SBC     HL,DE
            SCF     
            RET     Z
            LD      HL,(L180B)
            ADD     HL,DE
            LD      A,(HL)
            INC     DE
            LD      (IY+040H),E
            LD      (IY+041H),D
            RET     

L19B4:      CALL    L1D15
            JP      NZ,L2A4C
            OR      0AFH
            CALL    L18B6
            CALL    L1929
            RET     C
            LD      A,(HL)
            INC     HL
            AND     (HL)
            LD      (HL),0FFH
            INC     A
            LD      DE,L0000
            RET     NZ
            PUSH    HL
            LD      HL,(L180D)
            LD      BC,(ZKL)
L19D5:      DEC     HL
            DEC     BC
            LD      A,(HL)
            OR      A
            JR      Z,L19D5             ; (-006H)
            POP     HL
            LD      (HL),B
            DEC     HL
            LD      (HL),C
            RET     

            CALL    SKPDE
            LD      HL,L1A2D
            CP      088H
            JR      Z,L19F4             ; (+00aH)
            LD      HL,L1A35
            CP      089H
            JR      Z,L19F4             ; (+003H)
            RST     018H
            DB      029H
            RET     C
L19F4:      LD      (ZTOP),HL
            LD      DE,ZLOG
            LD      BC,00008H
            LDIR    
            LD      (L1809),HL
            PUSH    HL
            POP     IY
            LD      DE,ELMD
            CALL    L1C54
            PUSH    HL
            LD      HL,(ZEQT)
            LD      DE,ZNXT
            LD      BC,0001DH
            LDIR    
            POP     HL
            LD      BC,00010H
            ADD     HL,BC
            LD      (L180B),HL
            LD      BC,(ZKL)
            ADD     HL,BC
            LD      (L180D),HL
            LD      A,(ZRWX)
            AND     00FH
            RET     

L1A2D:      ADC     A,B
            INC     BC
            LD      A,B
            DEC     BC
            NOP     
            NOP     
            NOP     
            NOP     
L1A35:      ADC     A,C
            LD      (BC),A
            NOP     
            RRCA    
            NOP     
            NOP     
            NOP     
            NOP     
            LD      HL,KEYBUF
            PUSH    HL
            LD      A,B
            OR      A
            CALL    NZ,LDHLDE
            LD      (HL),000H
            LD      HL,$CRT
            JR      L1A50               ; (+003H)

L1A4D:      LD      HL,L0000
L1A50:      LD      A,L
            OR      H
            JR      Z,L1A89             ; (+035H)
            LD      (01A84H),HL
            CALL    LDDEMI
            LD      (01A4EH),DE
            LD      DE,KEYBUF
            EX      DE,HL
L1A62:      LD      A,(DE)
            OR      A
            JR      Z,L1A6D             ; (+007H)
            CP      (HL)
            JR      NZ,L1A4D            ; (-01cH)
            INC     HL
            INC     DE
            JR      L1A62               ; (-00bH)

L1A6D:      LD      A,(HL)
            INC     HL
            CP      03AH
            LD      C,000H
            JR      Z,L1A82             ; (+00dH)
            SUB     031H
            CP      009H
            JR      NC,L1A4D            ; (-02eH)
            LD      C,A
            LD      A,(HL)
            INC     HL
            CP      03AH
            JR      NZ,L1A4D            ; (-035H)
L1A82:      EX      (SP),HL
            LD      HL,L0000
            LD      A,C
            JR      L1A9B               ; (+012H)

L1A89:      POP     HL
            PUSH    HL
L1A8B:      LD      A,(HL)
            INC     HL
            CP      03AH
            JP      Z,01F94H
            OR      A
            JR      NZ,L1A8B            ; (-00aH)
            LD      HL,(DDEV)
            LD      A,(DCHAN)
L1A9B:      LD      (ZEQT),HL
            PUSH    HL
            LD      (ZCH),A
            LD      DE,ZNXT
            LD      BC,0001DH
            LDIR    
            LD      B,A
            LD      A,(01051H)
            AND     007H
            CP      B
            JP      C,01F94H
            LD      A,B
            POP     DE
            POP     HL
            RET     

            CALL    SKPDE
            RST     018H
            DB      02CH
            EX      DE,HL
            LD      HL,ELMD1
            LD      B,01FH
            CALL    ?CLRHL
            LD      HL,ELMD
            RST     018H
            DB      017H
            CALL    L1EB4
            LD      HL,ZFLAG1
            BIT     4,(HL)
            RET     Z
            INC     HL
            BIT     7,(HL)
            RET     NZ
            LD      A,(ELMD1)
            CP      00DH
            JP      Z,01F9AH
            RET     

            CALL    SKPDE
            CALL    L1AEF
            LD      B,000H
            CALL    L1B97
            JP      L2D08

L1AEF:      LD      A,(ZLOG)
            RST     018H
            DB      029H
            JP      NC,01F7CH
            CALL    L1C5B
            LD      HL,L005D
            LD      A,(ZFLAG1)
            LD      DE,(ZKL)
            INC     DE
            INC     DE
            BIT     7,A
            JR      NZ,L1B0B            ; (+001H)
            ADD     HL,DE
L1B0B:      LD      A,(ZRWX)
            BIT     2,A
            JR      Z,L1B13             ; (+001H)
            ADD     HL,DE
L1B13:      EX      DE,HL
            LD      A,(ZLOG)
            RST     018H
L1B18:      DB      02AH
            LD      (L1FC0),A
            LD      (ZTOP),HL
            EX      DE,HL
            LD      HL,ZLOG
            LD      BC,00008H
            LDIR    
            LD      (01809H),DE
            PUSH    DE
            POP     IY
            LD      HL,ELMD
            CALL    L1C54
            LD      HL,00010H
            ADD     HL,DE
            LD      (L180B),HL
            LD      DE,(ZKL)
            ADD     HL,DE
            LD      (L180D),HL
            CALL    L1D15
            JP      NZ,L2B7F
            LD      A,(ZRWX)
            BIT     2,A
            JP      NZ,01F97H
            BIT     1,A
            LD      IX,(ZWO)
            JR      NZ,L1B65            ; (+00bH)
            LD      A,(ZFLAG1)
            BIT     7,A
            JR      Z,L1B6B             ; (+00aH)
            LD      IX,(ZRO)
L1B65:      LD      HL,ELMD
            JP      IOCALL

L1B6B:      CALL    L0D9B
            JP      019BBH

            CALL    SKPDE
            LD      IY,L0100
            LD      (ZLOG),IY
            LD      IY,ELMD
            CALL    L1D15
            LD      HL,L2B7A
            JR      NZ,L1B8B            ; (+003H)
            LD      HL,L1C39
L1B8B:      CALL    L00FA
            LD      B,080H
            CALL    L1B97
            LD      A,(ELMD)
            RET     

L1B97:      LD      A,(ELMD)
            CP      005H
            RET     C
            LD      A,(ELMD18)
            AND     080H
            CP      B
            RET     Z
            JP      01F9DH

L1BA7:      CALL    SKPDE
            OR      A
            JR      Z,L1BED             ; (+040H)
            CALL    L1BB3
            JP      L2D08

L1BB3:      RST     018H
            DB      02EH
            RET     C
            CALL    L181A
            PUSH    AF
            CALL    L1D15
            JR      NZ,L1BE1            ; (+022H)
            BIT     7,A
            LD      A,B
            JR      Z,L1BD4             ; (+010H)
            OR      A
            LD      IX,(ZKL)
            JR      Z,L1BCF             ; (+004H)
            LD      IX,(ZCL)
L1BCF:      CALL    IOCALL
            JR      L1BE4               ; (+010H)

L1BD4:      OR      A
            JR      Z,L1BE4             ; (+00dH)
            LD      A,(ZRWX)
            BIT     1,A
            CALL    NZ,L1892
            JR      L1BE4               ; (+003H)

L1BE1:      CALL    L2BEC
L1BE4:      POP     AF
            RST     018H
            DB      02BH
            RET     

            CALL    SKPDE
            LD      B,000H
L1BED:      LD      C,08EH
L1BEF:      LD      A,C
            PUSH    BC
            RST     018H
            DB      029H
            CALL    NC,L1BB3
            POP     BC
            DEC     C
            JR      NZ,L1BEF            ; (-00bH)
            JP      L1FAB

L1BFD:      CALL    SKPDE
            LD      A,(ZFLAG1)
            BIT     6,A
            JP      Z,01F97H
            CALL    018C7H
            LD      A,(ZDIRMX)
            LD      B,A
L1C0F:      LD      HL,KEYBUF
            PUSH    BC
            LD      IX,(ZRO)
            CALL    IOCALL
            POP     BC
            SET     0,A
            RET     C
            PUSH    IY
            POP     DE
            CALL    L1E9D
            LD      A,(HL)
            RET     Z
            DJNZ    L1C0F               ; (-019H)
            XOR     A
            SCF     
            RET     

L1C2B:      CALL    L1C5B
            CALL    L1BFD
            JP      NC,01F79H
            OR      A
            JP      Z,01F85H
            RET     

L1C39:      CALL    L1C5B
            CALL    L1BFD
            JP      C,01F73H
            CALL    SKPDE
            LD      HL,KEYBUF
            PUSH    IY
            POP     DE
            PUSH    HL
            CALL    L1C54
            POP     HL
            LD      DE,ELMD
            LD      A,(HL)
L1C54:      LD      BC,00040H
            LDIR    
            OR      A
            RET     

L1C5B:      LD      IX,L1C9D
L1C5F:      LD      (01C99H),IX
            CALL    SKPDE
            LD      A,(ZLOG)
            LD      C,A
            LD      HL,(TEXTED)
            PUSH    HL
L1C6E:      POP     HL
            LD      A,(HL)
            OR      A
            RET     Z
            LD      B,A
            INC     HL
            CALL    LDDEMI
            PUSH    HL
            ADD     HL,DE
            EX      (SP),HL
            CP      08FH
            JR      NC,L1C6E            ; (-010H)
            CP      C
            JR      Z,L1C6E             ; (-013H)
            INC     HL
            LD      A,(HL)
            EX      AF,AF'
            INC     HL
            LD      DE,ZEQT
            PUSH    BC
            LD      BC,00300H
L1C8C:      LD      A,(DE)
            SUB     (HL)
            OR      C
            LD      C,A
            INC     DE
            INC     HL
            DJNZ    L1C8C               ; (-008H)
            POP     BC
            JR      NZ,L1C6E            ; (-029H)
            LD      A,B
            CALL    L0000
            JR      L1C6E               ; (-02fH)

L1C9D:      LD      A,(01051H)
            BIT     6,A
            JP      NZ,01F7CH
            BIT     5,A
            RET     Z
            EX      AF,AF'
            LD      B,A
            LD      A,(ZRWX)
            AND     B
            BIT     1,A
            RET     Z
            JP      01F7CH

            CALL    L1D15
            JP      NZ,L2AD2
            LD      BC,(ELMD20)
            PUSH    BC
            XOR     A
            LD      IX,(ZINP)
            CALL    IOCALL
            POP     BC
            RET     

            LD      A,(01051H)
            BIT     7,A
            JP      Z,01F97H
            LD      BC,(ELMD20)
            JP      L313A

            LD      A,(ELMD)
            CP      005H
            JR      C,L1CE4             ; (+005H)
            LD      A,080H
            LD      (ELMD18),A
L1CE4:      CALL    L1D15
            JP      NZ,L2AE0
            BIT     7,A
            JP      NZ,01F97H
            PUSH    DE
            LD      HL,L0200
            LD      (ZLOG),HL
            CALL    L1C5B
            LD      HL,ELMD
            PUSH    HL
            POP     IY
            LD      IX,(ZWO)
            CALL    IOCALL
            LD      BC,(ELMD20)
            POP     HL
            LD      A,004H
            LD      IX,(ZOUT)
            CALL    IOCALL
            RET     

L1D15:      LD      A,(ZFLAG1)
            BIT     5,A
            RET     

            CALL    SKPDE
            OR      A
            JR      NZ,L1D6B            ; (+04aH)
            LD      HL,L0100
            LD      (ZLOG),HL
            CALL    DBG_L1C5B_WRAP
            CALL    DBG_FLAGS_TEST
            ;
            ; V1.1Z: Restore original ZFLAG1/ZFLAG2 bit tests.
            ; DBG_FLAGS_TEST returns HL=ZFLAG1 for non-$RFS devices
            ; (and already handles the ZFLAG1 bit7 error check).
            ; Original code: BIT 7,ZFLAG1 / BIT 5,ZFLAG1 / INC HL /
            ; BIT 7,ZFLAG2.  bit5 of ZFLAG1 (not ZFLAG2!) flags block
            ; devices ($FD, $QD) for the ZINP-based DIR path at 02F38H.
            ;
            BIT     5,(HL)          ; ZFLAG1 bit5 = block device
            JP      NZ,02F38H       ; Block device DIR path (ZINP-based)
            INC     HL              ; HL = ZFLAG2
            BIT     7,(HL)          ; ZFLAG2 bit7 = error
            JP      NZ,01F97H
            NOP
            NOP
            NOP
            NOP
            NOP
            LD      HL,L2000
            LD      BC,00008H
L1D43:      CALL    ?CLRHL
            DEC     C
            JR      NZ,L1D43            ; (-006H)
            CALL    018C7H
            LD      A,(ZDIRMX)
            LD      B,A
            LD      HL,L2000
L1D53:      PUSH    BC
            LD      IX,(ZRO)
            CALL    IOCALL
            PUSH    AF
            LD      BC,00020H
            ADD     HL,BC
            LD      (HL),000H
            POP     AF
            POP     BC
            JR      C,L1D68             ; (+002H)
            DJNZ    L1D53               ; (-015H)
L1D68:      JP      L18C6

L1D6B:      LD      (01E13H),A
            XOR     A
            LD      (DISPX),A
            LD      HL,KEYBUF
            PUSH    HL
            LD      DE,L1E1D
            LD      B,00EH
            CALL    LDHLDE
            CALL    L1535
            LD      (HL),020H
            INC     HL
            LD      (HL),020H
            INC     HL
            EX      DE,HL
            CALL    L1D15
            LD      IX,(ZFREE)
            JR      Z,L1D95             ; (+004H)
            LD      IX,L2F4B
L1D95:      CALL    IOCALL
            JR      C,L1DA8             ; (+00eH)
            LD      H,B
            LD      L,C
            LD      B,000H
            RST     018H
            DB      016H
            LD      HL,L1E2B
            LD      B,009H
            CALL    LDDEHL
L1DA8:      EX      DE,HL
            LD      (HL),00DH
            INC     HL
            LD      (HL),000H
            POP     DE
            CALL    L1E12
            LD      B,040H
            LD      HL,L2000
L1DB7:      CALL    L1DC3
            LD      DE,00020H
            ADD     HL,DE
            DJNZ    L1DB7               ; (-009H)
            JP      L2D08

L1DC3:      CALL    SKPDE
            LD      A,(HL)
            OR      A
            RET     Z
            RET     M
            LD      DE,KEYBUF
            PUSH    DE
            LD      A,020H
            LD      B,026H
            CALL    ?SETDE
            LD      A,(HL)
            CP      00CH
            JR      C,L1DDC             ; (+002H)
            LD      A,00CH
L1DDC:      PUSH    HL
            POP     IY
            POP     DE
            PUSH    DE
            INC     DE
            LD      HL,L1E31
            LD      BC,L0003
L1DE8:      ADD     HL,BC
            DEC     A
            JR      NZ,L1DE8            ; (-004H)
            LDIR    
            EX      DE,HL
            BIT     0,(IY+012H)
            JR      Z,L1DF7             ; (+002H)
            LD      (HL),02AH
L1DF7:      INC     HL
            INC     HL
            LD      (HL),022H
            INC     HL
L1DFC:      LD      A,(IY+001H)
            CP      00DH
            JR      Z,L1E09             ; (+006H)
            LD      (HL),A
            INC     IY
            INC     HL
            JR      L1DFC               ; (-00dH)

L1E09:      LD      (HL),022H
            INC     HL
            LD      (HL),00DH
            INC     HL
            LD      (HL),000H
            POP     DE
L1E12:      LD      A,000H
            RST     018H
            DB      02EH
            RST     018H
            DB      017H
            RST     018H
            DB      037H
            RST     018H
            DB      00FH
            RET     

L1E1D:      DB      00DH,"DIRECTORY OF " ; Directory/file type strings
L1E2B:      DB      " KB FR"
L1E31:      DS      2, 045H
            DB      ".OBJBTXBSDBRDRB"
            DS      2, 020H
            DB      "? LIB ?"
            DS      2, 020H
            DB      "? SYSGR"
            DS      2, 020H
            DB      03FH
            DB      020H
; ---------------------------------------------------------------------------
; Subroutine at 0x1E58: Opens device I/O channel (saves/restores SP and IX)
; ---------------------------------------------------------------------------
            PUSH    HL                  ; E5
L1E59:      XOR     A                   ; AF
            LD      (01042H),A          ; 32 42 10 — clear I/O flag
            LD      IX,01E71H           ; DD 21 71 1E — IX = workspace
            LD      (01E72H),SP         ; ED 73 72 1E — save stack pointer
            CALL    L1C5F               ; CD 5F 1C — open channel
            POP     HL                  ; E1
            LD      IX,(01053H)         ; DD 2A 53 10 — restore IX
            CALL    IOCALL              ; CD 45 02
            RET                         ; C9
; Workspace area (0x1E71-0x1E73, used by above subroutine)
            DB      031H                ; 0x1E71 data (IX-based structure)
            DS      2, 000H             ; 0x1E72-73 SP save area
; ---------------------------------------------------------------------------
; Subroutine at 0x1E74: Confirm prompt — SVC #38 + "OK ? [Y/N]"
; ---------------------------------------------------------------------------
            LD      B,000H              ; 06 00
            RST     018H                ; DF
            DB      038H                ; SVC #38
            JR      L1E59               ; 18 DF — loop back to XOR A at 0x1E59
; ---------------------------------------------------------------------------
; Subroutine at 0x1E7A: Ask "OK ? [Y/N]" and return Z if 'Y'
; ---------------------------------------------------------------------------
L1E7A:      CALL    00164H              ; CD 64 01
            DB      059H                ; inline param 'Y' for CALL 0164H
            RET     Z                   ; C8 — return if match
            LD      DE,01E8EH           ; 11 8E 1E — DE → "OK ? [Y/N]" string
            RST     018H                ; DF
            DB      005H                ; SVC #5 — display string
            LD      A,001H              ; 3E 01
            RST     018H                ; DF
            DB      00DH                ; SVC #0D — read key
            CP      059H                ; FE 59 — compare with 'Y'
            RET     Z                   ; C8 — return Z if 'Y'
            JP      0017DH              ; C3 7D 01 — jump to error/cancel handler
            DB      "OK ? [Y/N]",019H,000H  ; Inline string at 0x1E8E
; ---------------------------------------------------------------------------
; L1E9D: Filename comparison — compares entry name (HL) with search name (DE).
; Skips ATRB byte in both, compares up to 17 chars, CR terminates/matches.
; ---------------------------------------------------------------------------
L1E9A:      LD      DE,01000H
L1E9D:      CALL    SKPDE               ; Skip DE past device name
            INC     HL                  ; Skip ATRB byte in directory entry
            INC     DE                  ; Skip ATRB byte in search name
            LD      A,(DE)
            CP      00DH
            RET     Z
            LD      B,011H
L1EA8:      LD      A,(DE)
            CP      (HL)
            RET     NZ
            CP      00DH
            RET     Z
            INC     HL
            INC     DE
            DJNZ    L1EA8               ; (-00aH)
            OR      A
            RET     

L1EB4:      INC     HL
            LD      C,010H
L1EB7:      LD      A,B
            OR      A
            JR      Z,L1ECF             ; (+014H)
            LD      A,(DE)
            INC     DE
            DEC     B
            OR      A
            JR      Z,L1ECF             ; (+00eH)
            CP      022H
            JR      Z,L1EB7             ; (-00eH)
            CP      03AH
            JP      Z,01F9AH
            LD      (HL),A
            INC     HL
            DEC     C
            JR      NZ,L1EB7            ; (-018H)
L1ECF:      LD      (HL),00DH
            INC     HL
L1ED2:      LD      A,C
            OR      A
            RET     Z
            LD      (HL),020H
            INC     HL
            DEC     C
            JR      L1ED2               ; (-009H)

            LD      HL,(TEXTED)
L1EDE:      INC     (HL)
            DEC     (HL)
            SCF     
            RET     Z
            CP      (HL)
            INC     HL
            JR      Z,L1EEE             ; (+008H)
            PUSH    DE
            CALL    LDDEMI
            ADD     HL,DE
            POP     DE
            JR      L1EDE               ; (-010H)

L1EEE:      INC     HL
            INC     HL
            RET     

L1EF1:      CALL    SKPDE
            RST     018H
            DB      029H
            RET     C
            DEC     HL
            LD      B,(HL)
            DEC     HL
            LD      C,(HL)
            DEC     HL
            LD      D,H
            LD      E,L
            INC     BC
            INC     BC
            INC     BC
            PUSH    BC
            ADD     HL,BC
            PUSH    HL
            LD      B,H
            LD      C,L
            LD      HL,(TMPEND)
            OR      A
            SBC     HL,BC
            LD      B,H
            LD      C,L
            POP     HL
            LDIR    
            POP     DE
            LD      HL,L0000
            OR      A
            SBC     HL,DE
            EX      DE,HL
            RST     018H
            DB      019H
            OR      A
            RET     

            PUSH    AF
            PUSH    BC
            PUSH    DE
            PUSH    DE
            INC     DE
            INC     DE
            INC     DE
            LD      HL,(TMPEND)
            EX      DE,HL
            ADD     HL,DE
            EX      DE,HL
            JP      C,01F6DH
            PUSH    HL
            LD      HL,0FE00H
            ADD     HL,SP
            SBC     HL,DE
            JR      C,01F6DH             ; (+037H)
            LD      HL,(MEMLMT)
            DEC     H
            DEC     H
            SBC     HL,DE
            JR      C,01F6DH             ; (+02eH)
            POP     HL
            PUSH    HL
            LD      BC,(VARST)
            OR      A
            SBC     HL,BC
            LD      B,H
            LD      C,L
            POP     HL
            INC     BC
            LDDR    
            POP     DE
            LD      (HL),A
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            PUSH    HL
            INC     DE
            PUSH    DE
L1F58:      LD      (HL),000H
            INC     HL
            DEC     DE
            LD      A,D
            OR      E
            JR      NZ,L1F58            ; (-008H)
            POP     DE
            INC     DE
            INC     DE
            RST     018H
            DB      019H
            POP     HL
            POP     DE
            POP     BC
            POP     AF
            RET     

L1F6A:      LD      A,003H
            LD      HL,0063EH
            LD      HL,09C3EH
            LD      HL,0A83EH
            LD      HL,0A93EH
            LD      HL,0AA3EH
            LD      HL,0AB3EH
            LD      HL,0AE3EH
            LD      HL,0B23EH
            LD      HL,0B33EH
            LD      HL,0B43EH
            LD      HL,0B53EH
            LD      HL,0B63EH
            LD      HL,0B73EH
            LD      HL,L3A3E
            LD      HL,0BB3EH
            LD      HL,0BC3EH
            LD      HL,0BD3EH
            LD      HL,0BF3EH
            LD      HL,L403E
            LD      HL,0C43EH
            JP      L0182

L1FAB:      LD      A,(L1FC0)
            OR      A
            LD      B,000H
            CALL    NZ,L1BA7
            CALL    L368D
            CALL    L38FA
            CALL    L2D08
            JP      L0D20

L1FC0:      DS      64, 000H
L2000:      DS      38, 000H
L2026:      DS      263, 000H
L212D:      DS      211, 000H
L2200:      DS      294, 000H
L2326:      DS      986, 000H
L2700:      DS      16, 000H
L2710:      DS      240, 000H
L2800:      DS      256, 000H
            LD      (02905H),DE
            RET     

L2905:      NOP     
            NOP     
L2907:      LD      BC,L0000
            LD      DE,00001H
            BIT     2,A
            CALL    Z,L2ED0
            LD      HL,(L180D)
            LD      A,(HL)
            LD      (HL),C
            LD      C,A
            INC     HL
            LD      A,(HL)
            LD      (HL),B
            LD      B,A
            LD      HL,(L180B)
            LD      DE,L0100
            JP      L2FCF

            CALL    SKPDE
            CALL    L181A
            LD      HL,KEYBUF
            PUSH    HL
            LD      A,B
            OR      A
            CALL    NZ,LDHLDE
            LD      B,020H
            LD      A,020H
            CALL    ?SETHL
            LD      A,(IY+012H)
            OR      A
            JP      NZ,01F7FH
            CALL    L29DF
            JR      NC,L2964            ; (+01dH)
            LD      (IY+014H),E
            LD      (IY+015H),D
            LD      (IY+046H),001H
            JR      Z,L2964             ; (+011H)
            CALL    L29C2
            CALL    L2A08
            CALL    L297C
            LD      (IY+040H),L
            LD      (IY+041H),H
            JR      L296A               ; (+006H)

L2964:      CALL    L297C
            CALL    L29AB
L296A:      POP     HL
            CALL    L2E24
            LD      (IY+044H),001H
L2972:      LD      HL,(L2905)
            INC     HL
            LD      (L2905),HL
            JP      L1815

L297C:      LD      HL,(L2905)
            DEC     HL
            PUSH    HL
            ADD     HL,HL
            LD      E,H
            LD      D,000H
            LD      HL,(L180B)
            ADD     HL,DE
            ADD     HL,DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            POP     HL
            LD      A,L
            PUSH    AF
            RRA     
            RRA     
            RRA     
            AND     00FH
            LD      L,A
            LD      H,000H
            ADD     HL,DE
            POP     AF
            AND     007H
            ADD     A,A
            ADD     A,A
            ADD     A,A
            ADD     A,A
            ADD     A,A
            LD      DE,(0180BH)
            INC     D
            ADD     A,E
            LD      E,A
            RET     NC
            INC     D
            RET     

L29AB:      LD      A,(IY+040H)
            CP      L
            JR      NZ,L29B6            ; (+005H)
            LD      A,(IY+041H)
            CP      H
            RET     Z
L29B6:      CALL    L29C2
            LD      (IY+040H),L
            LD      (IY+041H),H
            XOR     A
            JR      L29CC               ; (+00aH)

L29C2:      XOR     A
            CP      (IY+044H)
            RET     Z
            LD      (IY+044H),A
            LD      A,003H
L29CC:      CALL    SKPDE
            LD      C,(IY+040H)
            LD      B,(IY+041H)
            LD      DE,L0100
            LD      HL,(L180B)
            INC     H
            JP      L2FD1

L29DF:      LD      L,(IY+014H)
            LD      H,(IY+015H)
            LD      DE,(02905H)
            LD      A,H
            OR      L
            JR      NZ,L29F1            ; (+004H)
            OR      0FFH
            SCF     
            RET     

L29F1:      CALL    L2FC9
            RET     NC
            CALL    SKPDE
            DEC     HL
            DEC     DE
            LD      A,L
            AND     080H
            LD      L,A
            LD      A,E
            AND     080H
            LD      E,A
            CALL    L2FC9
            RET     C
            SCF     
            RET     

L2A08:      CALL    SKPDE
            LD      HL,(L2905)
            DEC     HL
            ADD     HL,HL
            LD      B,H
            INC     B
            LD      HL,(L180B)
L2A15:      LD      A,(HL)
            INC     HL
            OR      (HL)
            CALL    Z,L2A23
            INC     HL
            DJNZ    L2A15               ; (-009H)
            LD      (IY+046H),001H
            RET     

L2A23:      CALL    SKPDE
            PUSH    HL
            LD      DE,00010H
            CALL    L2ED0
            POP     HL
            LD      (HL),B
            DEC     HL
            LD      (HL),C
            LD      HL,(L180B)
            INC     H
            XOR     A
L2A36:      LD      (HL),020H
            INC     HL
            DEC     A
            JR      NZ,L2A36            ; (-006H)
            DEC     H
            LD      DE,L0100
            LD      A,010H
L2A42:      PUSH    AF
            CALL    L2FCF
            POP     AF
            INC     BC
            DEC     A
            JR      NZ,L2A42            ; (-009H)
            RET     

L2A4C:      LD      L,(IY+014H)
            LD      H,(IY+015H)
            LD      A,H
            OR      L
            SCF     
            RET     Z
            LD      DE,0FF02H
            PUSH    HL
            ADD     HL,DE
            JR      C,L2A60             ; (+003H)
            LD      HL,L0000
L2A60:      LD      (IY+014H),L
            LD      (IY+015H),H
            POP     DE
            LD      HL,(L180B)
            DEC     HL
            LD      (HL),D
            DEC     HL
            LD      (HL),E
            CALL    L2A88
            OR      A
            LD      A,(ZRWX)
            BIT     2,A
            RET     NZ
            LD      HL,(L180D)
            LD      A,(HL)
            LD      (IY+01EH),A
            INC     HL
            LD      A,(HL)
            LD      (IY+01FH),A
            LD      DE,L0000
            RET     

L2A88:      LD      C,(IY+01EH)
            LD      B,(IY+01FH)
            LD      A,B
            OR      C
            JP      Z,01F76H
            LD      DE,L0100
            LD      HL,(L180B)
            JP      02FD0H

            RET     

            XOR     A
            LD      (DE),A
            PUSH    HL
            PUSH    DE
            PUSH    DE
            CALL    L29DF
            POP     DE
            JR      C,L2AC7             ; (+01fH)
            CALL    L181A
            PUSH    DE
            CALL    L297C
            CALL    L29AB
            EX      DE,HL
            POP     DE
            CALL    L2E24
            XOR     A
            LD      (DE),A
            CALL    L2972
            LD      B,020H
            XOR     A
            LD      HL,(ZTOP)
            INC     HL
            RES     7,(HL)
            JR      L2ACF               ; (+008H)

L2AC7:      XOR     A
            LD      B,A
            LD      HL,(ZTOP)
            INC     HL
            SET     7,(HL)
L2ACF:      POP     DE
            POP     HL
            RET     

L2AD2:      LD      DE,(ELMD20)
            LD      BC,(ELMD30)
            CALL    02FD0H
            JP      L2D08

L2AE0:      CALL    L2AEA
            LD      IY,ELMD
            JP      L2C07

L2AEA:      PUSH    DE
            CALL    L2DD0
            CALL    L2ED0
            LD      (ELMD30),BC
            LD      D,E
            LD      E,000H
            POP     HL
            JP      L2FCF

            OR      A
            JR      NZ,L2B34            ; (+035H)
            PUSH    DE
            CALL    L2B6A
            LD      A,000H
            CALL    Z,L2B51
            POP     DE
            CALL    L2AEA
            LD      HL,ELMD
            OR      0FFH
            CALL    L2B51
            CALL    L2E2A
            LD      HL,L2000
            LD      (HL),081H
            PUSH    HL
            POP     IY
            LD      HL,(ELMD20)
            LD      (IY+014H),L
            LD      (IY+015H),H
            LD      HL,(ELMD30)
            LD      (IY+01EH),L
            LD      (IY+01FH),H
            JP      L2F37

L2B34:      PUSH    HL
            CALL    L2B6A
            JP      NZ,01F88H
            DEC     (HL)
            LD      DE,ELMD
            CALL    L2E24
            CALL    L2F37
L2B45:      XOR     A
            LD      HL,L2000
            CALL    L2B51
            CALL    L2E2A
            POP     HL
            RET     

L2B51:      CALL    SKPDE
            PUSH    AF
            LD      DE,00014H
            ADD     HL,DE
            CALL    LDDEMI
            LD      BC,00008H
            ADD     HL,BC
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            CALL    L2DD8
            POP     AF
            JP      L2E77

L2B6A:      CALL    L1D15
            JP      Z,01F97H
            CALL    L2E36
            CALL    02F38H
            LD      A,(HL)
            CP      081H
            RET     

L2B7A:      LD      A,009H
            LD      (ZRWX),A
L2B7F:      CALL    L2F6E
            XOR     B
            DEC     HL
            LD      A,(ZRWX)
            BIT     0,A
            JP      NZ,01F73H
            PUSH    AF
            LD      DE,00001H
            CALL    L2ED0
            LD      (IY+01EH),C
            LD      (IY+01FH),B
            LD      (IY+046H),001H
            POP     AF
            BIT     1,A
            RET     Z
            LD      HL,(L180D)
            LD      (HL),C
            INC     HL
            LD      (HL),B
            RET     

            RET     M
            RET     Z
            CALL    L1E9A
            RET     NZ
            POP     AF
            LD      A,(ZRWX)
            BIT     1,A
            JP      NZ,01F79H
            PUSH    HL
            CALL    L2E24
            LD      B,020H
            CALL    ?CLRDE
            POP     HL
            LD      A,(HL)
            PUSH    IY
            POP     DE
            CALL    L2E24
            LD      HL,ZRWX
            BIT     3,(HL)
            RET     NZ
            BIT     0,(HL)
            JP      NZ,L2A4C
            CALL    L2E5E
            CALL    L2A88
            LD      D,H
            LD      E,L
L2BDB:      LD      A,(HL)
            INC     HL
            OR      (HL)
            INC     HL
            JR      NZ,L2BDB            ; (-006H)
            DEC     HL
            DEC     HL
            SBC     HL,DE
            LD      (IY+042H),L
            LD      (IY+043H),H
            RET     

L2BEC:      LD      A,B
            OR      A
            RET     Z
            LD      A,(ZRWX)
            BIT     0,A
            RET     NZ
            BIT     2,A
            JR      NZ,L2C20            ; (+027H)
            LD      A,006H
            CALL    L2907
            PUSH    IY
            POP     HL
            LD      DE,ELMD
            CALL    L2E24
L2C07:      CALL    L2F6E
            RLA     
            INC     L
            JP      Z,01F85H
L2C0F:      LD      A,001H
            LD      B,(IY+000H)
            JP      L2D6A

            RET     M
            RET     Z
            CALL    L1E9A
            RET     NZ
            JP      01F79H

L2C20:      CALL    L29C2
            LD      A,(IY+046H)
            OR      A
            RET     Z
            CALL    L2F6E
            LD      (0CA2CH),A
            ADD     A,L
            RRA     
            JR      L2C0F               ; (-023H)

            RET     M
            RET     Z
            CALL    L1E9A
            RET     NZ
            POP     AF
            LD      (IY+045H),001H
            LD      A,(HL)
            CP      (IY+000H)
            JP      NZ,01F9DH
            JR      L2C0F               ; (-037H)

            CALL    SKPDE
            LD      (02C60H),A
            CALL    L2F6E
            LD      D,H
            INC     L
            JP      01F73H

            RET     Z
            RET     M
            CALL    L1E9A
            RET     NZ
            POP     AF
            LD      BC,L0012
            ADD     HL,BC
            LD      (HL),0FFH
L2C61:      CALL    L2F37
            JP      L2D08

            CALL    SKPDE
            CALL    L2F6E
            LD      (HL),D
            INC     L
            JP      01F73H

            RET     Z
            RET     M
            CALL    L1E9A
            RET     NZ
            POP     AF
            CALL    L2CFB
            PUSH    HL
            LD      DE,ELMD
            CALL    L2E24
            XOR     A
            LD      (ZLOG),A
            LD      IX,L2C9B
            CALL    L1C5F
            XOR     A
            LD      (ELMD),A
            POP     HL
L2C93:      LD      B,(HL)
            LD      IY,ELMD
            JP      L2D6A

L2C9B:      INC     HL
            INC     HL
            INC     HL
            CALL    L1E9A
            JP      Z,01F7CH
L2CA4:      RET     

L2CA5:      DS      16, 000H
            DEC     C
            CALL    SKPDE
            LD      HL,L2CA4
            CALL    L1EB4
            XOR     A
L2CC0:      LD      (02CE3H),A
            CALL    L2F6E
            JP      Z,L182C
            JR      L2C93               ; (-038H)

            RET     M
            LD      DE,L2CA4
            CALL    L1E9D
            JP      Z,01F79H
            CALL    L1E9A
            RET     NZ
            LD      (02CE9H),HL
            LD      A,001H
            LD      (02CE3H),A
            RET     

            LD      A,0FFH
            OR      A
            JP      Z,01F73H
            LD      HL,0FFFFH
            CALL    L2CFB
            LD      DE,L2CA5
            INC     HL
            EX      DE,HL
            LD      BC,00011H
            LDIR    
            JP      L2C61

L2CFB:      CALL    SKPDE
            LD      DE,L0012
            ADD     HL,DE
            LD      A,(HL)
            OR      A
            RET     Z
            JP      01F7FH

L2D08:      CALL    IOOUT
            XOR     A
            LD      (L1FC0),A
            LD      A,LSE0
L2D11:      PUSH    AF
            RST     018H
            DB      02BH
            POP     AF
            INC     A
            JR      NZ,L2D11            ; (-007H)
            LD      HL,(ZEQT)
            LD      DE,$FD
            CALL    L2FC9
            RET     NZ
            LD      A,(ZCH)
            EXX     
            LD      H,A
            LD      D,000H
            LD      B,003H
            EXX     
            XOR     A
            LD      (ZLOG),A
L2D30:      EXX     
            LD      A,B
            LD      (ZCH),A
            ADD     A,090H
            LD      L,A
            LD      E,000H
            EXX     
            LD      IX,L2D5C
            CALL    L1C5F
            EXX     
            BIT     7,E
            LD      A,L
            EXX     
            CALL    Z,L1EF1
            EXX     
            DEC     B
            EXX     
            JP      P,L2D30
            EXX     
            LD      A,H
            LD      (ZCH),A
            BIT     7,D
            EXX     
            CALL    Z,L368D
            RET     

L2D5C:      EXX     
            LD      DE,0FFFFH
            EXX     
            RET     

L2D62:      LD      A,(ZCH)
            ADD     A,090H
            RST     018H
            DB      029H
            RET     

L2D6A:      LD      (L2DCF),A
            LD      A,B
            PUSH    AF
            EX      DE,HL
            LD      HL,ELMD
            CALL    L2E24
            CALL    L2E36
            POP     AF
            CP      005H
            JR      C,L2D88             ; (+00aH)
            LD      A,(ELMD18)
            RLA     
            LD      A,003H
            JR      NC,L2D88            ; (+002H)
            LD      A,001H
L2D88:      LD      HL,02D9CH
            ADD     A,A
            CALL    ADDHLA
            CALL    INDRCT
            CALL    L00FA
            CALL    L2F37
            CALL    L2E2A
            JP      L2D08

            ADD     A,02DH
            ADD     A,02DH
            AND     (HL)
            DEC     L
            JP      PO,L212D
            JR      NZ,02DB9H            ; (+010H)
L2DA9:      DEC     HL
            LD      B,(HL)
            DEC     HL
            LD      C,(HL)
            LD      A,B
            OR      C
            RET     Z
            LD      DE,L0100
            LD      HL,KEYBUF
            PUSH    HL
            PUSH    DE
            CALL    02FD0H
            LD      DE,00001H
            CALL    L2DC9
            POP     DE
            POP     HL
            ADD     HL,DE
            JR      L2DA9               ; (-01dH)

            CALL    L2DD0
L2DC9:      LD      A,(L2DCF)
            JP      L2E77

L2DCF:      NOP     
L2DD0:      LD      DE,(ELMD20)
            LD      BC,(ELMD30)
L2DD8:      LD      A,E
            OR      D
            JR      Z,L2DDD             ; (+001H)
            DEC     DE
L2DDD:      INC     D
            LD      E,D
            LD      D,000H
            RET     

            LD      DE,L0100
            LD      BC,(ELMD30)
            LD      A,(L2DCF)
            OR      A
            LD      HL,KEYBUF
            JR      Z,L2DF5             ; (+003H)
            LD      HL,(L180B)
L2DF5:      PUSH    HL
            PUSH    AF
            CALL    L2FD1
            POP     AF
            PUSH    AF
            JR      Z,L2E02             ; (+004H)
            LD      A,(IY+045H)
            OR      A
L2E02:      LD      DE,00001H
            CALL    Z,L2DC9
            POP     AF
            POP     HL
            JR      Z,L2E13             ; (+007H)
            LD      E,(IY+042H)
            LD      D,(IY+043H)
            ADD     HL,DE
L2E13:      LD      C,(HL)
            INC     HL
            LD      B,(HL)
            INC     HL
            LD      A,B
            OR      C
            RET     Z
            PUSH    HL
            LD      DE,00010H
            CALL    L2DC9
            POP     HL
            JR      L2E13               ; (-011H)

L2E24:      LD      BC,00020H
            LDIR    
            RET     

L2E2A:      LD      HL,L2800
            LD      DE,L0100
            LD      BC,L000F
            JP      L2FCF

L2E36:      LD      HL,L2800
            CALL    L2E67
            PUSH    HL
            CALL    L2D62
            EX      DE,HL
            POP     HL
            RET     C
            PUSH    HL
            LD      A,(DE)
            CP      (HL)
            JP      NZ,01F88H
            LD      BC,L0006
            ADD     HL,BC
            EX      DE,HL
            ADD     HL,BC
            EX      DE,HL
            LD      B,FDPC
L2E52:      LD      A,(DE)
            CPL     
            AND     (HL)
            JP      NZ,01F88H
            INC     HL
            INC     DE
            DJNZ    L2E52               ; (-00aH)
            POP     HL
            RET     

L2E5E:      CALL    L2D62
            RET     NC
            LD      DE,L0100
            RST     018H
            DB      02AH
L2E67:      LD      BC,L000F
            LD      DE,L0100
            XOR     A
            CALL    L2FD4
            LD      A,(HL)
            OR      A
            JP      M,01F70H
            RET     

L2E77:      LD      HL,L2800
            CALL    L2E84
            OR      A
            RET     NZ
            CALL    L2D62
            RET     C
            XOR     A
L2E84:      CALL    IOOUT
            OR      A
            PUSH    HL
            LD      HL,0B600H
            LD      A,013H
            JR      NZ,L2E95            ; (+005H)
            LD      HL,0A62FH
            LD      A,01BH
L2E95:      LD      (L2EB2),A
            LD      (L2EC8),HL
            POP     HL
L2E9C:      CALL    L2EA4
            INC     BC
            DEC     E
            JR      NZ,L2E9C            ; (-007H)
            RET     

L2EA4:      CALL    SKPDE
            INC     HL
            LD      A,C
            SUB     (HL)
            LD      C,A
            JR      NC,L2EAE            ; (+001H)
            DEC     B
L2EAE:      INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
L2EB2:      NOP     
            DEC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            INC     HL
            INC     HL
            LD      A,C
            LD      E,003H
L2EBD:      SRL     B
            RR      C
            DEC     E
            JR      NZ,L2EBD            ; (-007H)
            ADD     HL,BC
            CALL    L2F2B
L2EC8:      NOP     
            NOP     
            CP      (HL)
            LD      (HL),A
            RET     NZ
            JP      01F88H

L2ED0:      PUSH    DE
            CALL    L2E5E
            POP     DE
            PUSH    HL
            CALL    L2EDE
            POP     HL
            LD      A,001H
            JR      L2E84               ; (-05aH)

L2EDE:      LD      A,E
            EXX     
            LD      E,A
            LD      D,A
            LD      B,001H
            EXX     
            INC     HL
            LD      E,(HL)
            LD      D,000H
            INC     HL
            INC     HL
            INC     HL
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            DEC     BC
            LD      (02EFCH),BC
            INC     HL
            PUSH    HL
            EXX     
            POP     HL
            EXX     
            LD      B,D
            LD      C,E
L2EFB:      LD      HL,L0000
            OR      A
            SBC     HL,DE
            JP      C,01F8BH
            EXX     
            LD      A,B
            AND     (HL)
            EXX     
            CALL    L2F15
            INC     DE
            EXX     
            RLC     B
            JR      NC,L2F12            ; (+001H)
            INC     HL
L2F12:      EXX     
            JR      L2EFB               ; (-01aH)

L2F15:      JR      NZ,L2F24            ; (+00dH)
            EXX     
            DEC     E
            EXX     
            RET     NZ
            POP     AF
            EXX     
            LD      A,D
            EXX     
            LD      E,A
            LD      D,000H
            OR      A
            RET     

L2F24:      EXX     
            LD      E,D
            EXX     
            LD      B,D
            LD      C,E
            INC     BC
            RET     

L2F2B:      PUSH    BC
            AND     007H
            INC     A
            LD      B,A
            XOR     A
            SCF     
L2F32:      ADC     A,A
            DJNZ    L2F32               ; (-003H)
            POP     BC
            RET     

L2F37:      OR      0AFH
            LD      HL,L2000
            LD      DE,00800H
            LD      BC,00010H
            CALL    L2FD1
            LD      A,(HL)
            OR      A
            JP      P,01F70H
            RET     

L2F4B:      PUSH    HL
            PUSH    DE
            CALL    L2E36
            INC     HL
            INC     HL
            CALL    LDDEMI
            CALL    INDRCT
            XOR     A
            SBC     HL,DE
            JP      C,01F70H
            SRL     H
            RR      L
            SRL     H
            RR      L
            LD      B,H
            LD      C,L
            POP     DE
            POP     HL
            OR      A
            JP      L368D

L2F6E:      CALL    L1D15
            JP      Z,01F97H
            POP     HL
            CALL    LDDEMI
            LD      (02FB1H),HL
            LD      (02F97H),DE
            XOR     A
            LD      (L2FC3),A
            CALL    02F38H
            LD      (L2FC7),HL
            LD      A,040H
            LD      (L2FC6),A
L2F8E:      LD      HL,(L2FC7)
            LD      A,(HL)
            OR      A
            CALL    Z,L2FB3
            CALL    0FFFFH
            LD      HL,(L2FC7)
            LD      BC,00020H
            ADD     HL,BC
            LD      (L2FC7),HL
            LD      HL,L2FC6
            DEC     (HL)
            JR      NZ,L2F8E            ; (-01bH)
            LD      A,(L2FC3)
            LD      HL,(L2FC4)
            OR      A
            JP      0FFFFH

L2FB3:      PUSH    AF
            LD      A,(L2FC3)
            OR      A
            JR      NZ,L2FC1            ; (+007H)
            INC     A
            LD      (L2FC3),A
            LD      (L2FC4),HL
L2FC1:      POP     AF
            RET     

L2FC3:      NOP     
L2FC4:      NOP     
            NOP     
L2FC6:      NOP     
L2FC7:      NOP     
            NOP     
L2FC9:      PUSH    HL
            OR      A
            SBC     HL,DE
            POP     HL
            RET     

L2FCF:      OR      0AFH
L2FD1:      CALL    L2FE8
L2FD4:      CALL    SKPDE
            OR      A
            LD      IX,(ZINP)
            JR      Z,L2FE2             ; (+004H)
            LD      IX,(ZOUT)
L2FE2:      LD      A,(ZCH)
            JP      IOCALL

L2FE8:      CALL    IOOUT
            LD      A,(L35B5)
            OR      A
            RET     M
            CALL    L2D62
            RET     C
            JP      L2E36

; ---------------------------------------------------------------------------
; $CMT - Cassette Tape device descriptor (same format as $RFS, $FD, etc.)
; ---------------------------------------------------------------------------
$CMT:       DW      03359H              ; Next device = $RS
            DB      "CMT",000H          ; Device name
            DB      05FH                ; flags1: bit5=0, bit6=1
L2FFE:      DB      0C0H                ; flags2 (bit 4 toggled by CMT handler)
            DB      000H                ; dirmx = 0
            DW      03014H              ; ZINIT
            DW      03064H              ; ZRO
            DW      03022H              ; ZWO
            DW      000E5H              ; ZCL (= NOP stub)
            DW      00100H              ; ZKL
            DW      030AAH              ; ZINP
            DW      0305DH              ; ZOUT
            DW      01F97H              ; ZDELT (= error handler)
            DW      01F97H              ; ZWDIR (= error handler)
            DW      01F97H              ; ZFREE (= error handler)
            CALL    TEST1
            DB      054H
            LD      HL,L2FFE
            SET     4,(HL)
            RET     Z
            RES     4,(HL)
            OR      A
            RET     

            CALL    SKPDE
            LD      DE,L10F0
            LD      A,(HL)
            LD      C,005H
            CP      002H
            JR      Z,L303D             ; (+00eH)
            LD      C,004H
L3031:      CP      003H
            JR      Z,L303D             ; (+008H)
            LD      C,A
L3036:      SUB     004H
            CP      002H
            JP      C,01F9DH
L303D:      LD      A,C
            LD      (DE),A
            INC     HL
            INC     DE
            LD      BC,00011H
            LDIR    
            INC     HL
            INC     HL
            LD      BC,L0006
            LDIR    
            LD      B,068H
            CALL    ?CLRDE
            LD      HL,L10F0
            LD      BC,L0080
            CALL    L30CB
            JR      L3060               ; (+003H)

            CALL    L30D2
L3060:      JP      C,L017B
            RET     

            LD      A,037H
            LD      (L30AA),A
            PUSH    HL
            LD      HL,L10F0
            LD      BC,L0080
            CALL    L3114
            JR      C,L30C1             ; (+04cH)
            LD      DE,L3332
            CALL    L3301
            POP     DE
            LD      A,(HL)
            LD      C,002H
            CP      005H
            JR      Z,L3091             ; (+00eH)
            LD      C,003H
            CP      004H
            JR      Z,L3091             ; (+008H)
            LD      C,A
            SUB     002H
            CP      002H
            JP      C,01F9DH
L3091:      LD      A,C
            LD      (DE),A
            INC     HL
            INC     DE
            LD      BC,00011H
            LDIR    
            XOR     A
            LD      B,002H
            CALL    ?CLRDE
            LD      BC,L0006
            LDIR    
            LD      B,006H
            JP      ?CLRDE

L30AA:      XOR     A
            JR      NC,L30BD            ; (+010H)
            LD      A,0AFH
            LD      (L30AA),A
            PUSH    HL
            LD      HL,ELMD
            LD      DE,L333F
            CALL    L3301
            POP     HL
L30BD:      CALL    L311B
            RET     NC
L30C1:      CP      002H
            JP      NZ,L017B
            LD      A,0C6H
            JP      L0182

L30CB:      CALL    L32D4
            LD      A,0CCH
            JR      L30D4               ; (+002H)

L30D2:      LD      A,053H
L30D4:      LD      (03109H),SP
            LD      SP,L10F0
            PUSH    DE
            LD      E,A
            LD      D,0D7H
            LD      A,B
            OR      C
            JR      Z,L3107             ; (+024H)
            CALL    L32CB
            CALL    L3174
            JR      C,L3101             ; (+016H)
            LD      A,E
            CP      0CCH
            JR      NZ,L30F8            ; (+008H)
            PUSH    DE
            LD      DE,L3325
            CALL    L3301
            POP     DE
L30F8:      DI      
            OUT     (LSE4),A
            CALL    00780H
            CALL    NC,L31DD
L3101:      DI      
            OUT     (LSE4),A
            CALL    00700H
L3107:      POP     DE
            LD      SP,L0000
            OUT     (LSE0),A
            OUT     (LSE1),A
            PUSH    AF
            RST     018H
            DB      011H
            POP     AF
            RET     

L3114:      CALL    L32D4
            LD      A,0CCH
            JR      L311D               ; (+002H)

L311B:      LD      A,053H
L311D:      LD      (03109H),SP
            LD      SP,L10F0
            PUSH    DE
            LD      D,LSD2
            LD      E,A
            LD      A,B
            OR      C
            JR      Z,L3107             ; (-025H)
            CALL    L3174
            DI      
            OUT     (LSE4),A
            CALL    NC,L065B
            CALL    NC,L3220
            JR      L3101               ; (-039H)

L313A:      PUSH    HL
            LD      DE,L334C
            LD      HL,ELMD
            CALL    L3301
            POP     HL
            CALL    L3153
            RET     NC
            CP      002H
            JP      NZ,L017B
            LD      A,083H
            JP      L0182

L3153:      LD      (03109H),SP
            LD      SP,L10F0
            PUSH    DE
            LD      D,LSD2
            LD      E,053H
            LD      A,B
            OR      C
            JR      Z,L3107             ; (-05cH)
            CALL    RFS_LOAD_HOOK
            NOP
            NOP
            NOP
            DI
            OUT     (LSE4),A
            CALL    NC,L065B
            CALL    NC,L3274
            JR      L3101               ; (-073H)

L3174:      CALL    SKPDE
            RST     018H
            DB      010H
            LD      HL,MMIO
            LD      A,FDPA
            CALL    POPRA
            LD      B,00AH
L3183:      LD      HL,0E002H
            CALL    BREAKX
            AND     010H
            JR      Z,L319A             ; (+00dH)
L318D:      LD      B,0FFH
            OUT     (LSE2),A
L3191:      CALL    L09A6
            DJNZ    L3191               ; (-005H)
            OUT     (LSE0),A
            XOR     A
            RET     

L319A:      LD      HL,0E003H
            LD      A,006H
            CALL    POPRA
            INC     A
            CALL    POPRA
            DJNZ    L3183               ; (-025H)
            LD      A,(L0050)
            OR      A
            JR      NZ,L31C7            ; (+019H)
            RST     018H
            DB      002H
            LD      A,07FH
            RST     018H
            DB      004H
            LD      A,020H
            RST     018H
            DB      003H
            LD      A,D
            CP      0D7H
            LD      DE,L32BC
            JR      Z,L31C3             ; (+003H)
            LD      DE,L32C3
L31C3:      RST     018H
            DB      005H
            RST     018H
            DB      002H
L31C7:      LD      HL,0E002H
            CALL    BREAKX
            AND     010H
            JR      NZ,L318D            ; (-044H)
            LD      HL,0E001H
            CALL    BREAKX
            AND     080H
            JR      NZ,L31C7            ; (-014H)
            SCF     
            RET     

L31DD:      PUSH    DE
            PUSH    BC
            PUSH    HL
            LD      D,002H
            LD      A,FDPA
            LD      (MMIO),A
L31E7:      OUT     (LSE1),A
            LD      A,(HL)
            OUT     (LSE3),A
            CALL    L32C8
            LD      A,(0E001H)
            AND     080H
            SCF     
            JR      Z,L321C             ; (+025H)
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L31E7            ; (-016H)
            LD      HL,(SUMDT)
            LD      A,H
            CALL    L32C8
            LD      A,L
            CALL    L32C8
            CALL    L0D60
            XOR     A
            DEC     D
            JR      Z,L321C             ; (+00dH)
            LD      B,000H
L3211:      CALL    L0D47
            DJNZ    L3211               ; (-005H)
            POP     HL
            POP     BC
            PUSH    BC
            PUSH    HL
            JR      L31E7               ; (-035H)

L321C:      POP     HL
            POP     BC
            POP     DE
            RET     

L3220:      PUSH    DE
            PUSH    BC
            PUSH    HL
            LD      H,002H
L3225:      LD      BC,0E001H
            LD      DE,0E002H
L322B:      CALL    00601H
            JR      C,L3267             ; (+037H)
            CALL    00A4AH
            LD      A,(DE)
            AND     020H
            JR      Z,L322B             ; (-00dH)
            LD      D,H
            LD      HL,L0000
            LD      (SUMDT),HL
            POP     HL
            POP     BC
            PUSH    BC
            PUSH    HL
L3243:      CALL    L0624
            JR      C,L3267             ; (+01fH)
            OUT     (LSE1),A
            LD      (HL),A
            OUT     (LSE3),A
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3243            ; (-010H)
            LD      HL,(SUMDT)
            CALL    L0624
            JR      C,L3267             ; (+00cH)
            CP      H
            JR      NZ,L326B            ; (+00dH)
            CALL    L0624
            JR      C,L3267             ; (+004H)
            CP      L
            JR      NZ,L326B            ; (+005H)
L3266:      XOR     A
L3267:      POP     HL
            POP     BC
            POP     DE
            RET     

L326B:      DEC     D
            LD      H,D
            JR      NZ,L3225            ; (-04aH)
L326F:      LD      A,002H
            SCF     
            JR      L3267               ; (-00dH)

L3274:      PUSH    DE
            PUSH    BC
            PUSH    HL
            LD      H,002H
L3279:      LD      BC,0E001H
            LD      DE,0E002H
L327F:      CALL    00601H
            JR      C,L3267             ; (-01dH)
            CALL    00A4AH
            LD      A,(DE)
            AND     020H
            JR      Z,L327F             ; (-00dH)
            LD      D,H
            POP     HL
            POP     BC
            PUSH    BC
            PUSH    HL
L3291:      CALL    L0624
            JR      C,L3267             ; (-02fH)
            OUT     (LSE1),A
            CP      (HL)
            OUT     (LSE3),A
            JR      NZ,L326F            ; (-02eH)
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3291            ; (-012H)
            LD      HL,(CSMDT)
            CALL    L0624
            JR      C,L3267             ; (-044H)
            CP      H
            JR      NZ,L326F            ; (-03fH)
            CALL    L0624
            JR      C,L3267             ; (-04cH)
            CP      L
            JR      NZ,L326F            ; (-047H)
            DEC     D
            LD      H,D
            JR      NZ,L3279            ; (-041H)
            JR      L3266               ; (-056H)

L32BC:      DB      "RECORD." ; CMT status strings (RECORD/PLAY)
L32C3:      DB      "PLAY",000H
L32C8:      JP      L076D

L32CB:      DI      
            OUT     (LSE2),A
            CALL    0071AH
            OUT     (LSE0),A
            RET     

L32D4:      CALL    SKPDE
            LD      A,0C9H
            LD      (L32D4),A
            LD      HL,L0036
            BIT     6,(HL)
            RET     Z
            LD      HL,L0767
            LD      (032C9H),HL
            LD      HL,L077A
            LD      (030FCH),HL
            LD      HL,00996H
            LD      (03192H),HL
            LD      HL,L0A01
            LD      (03212H),HL
            LD      HL,L0A1A
            LD      (03209H),HL
            RET     

L3301:      LD      A,(L0050)
            OR      A
            RET     NZ
            RST     018H
            DB      002H
            RST     018H
            DB      005H
            PUSH    HL
            INC     HL
            LD      A,022H
            RST     018H
            DB      003H
            LD      D,010H
L3312:      LD      A,(HL)
            CP      00DH
            JR      Z,L331D             ; (+006H)
            RST     018H
            DB      003H
            INC     HL
            DEC     D
            JR      NZ,L3312            ; (-00bH)
L331D:      LD      A,022H
            RST     018H
            DB      003H
            RST     018H
            DB      002H
            POP     HL
            RET     

L3325:      DB      057H,005H,"RITING" ; CMT status strings (WRITING/FOUND/LOADING/VERIFYING)
            DS      3, 020H
            DB      006H
            DB      000H
L3332:      DB      046H,005H,"OUND"
            DS      5, 020H
            DB      006H
            DB      000H
L333F:      DB      04CH,005H,"OADING"
            DS      3, 020H
            DB      006H
            DB      000H
L334C:      DB      056H,005H,"ERIFYING ",006H
            NOP     
; ---------------------------------------------------------------------------
; $RS - RS-232C device descriptor
; flags1 bit7=1: ZWDIR/ZFREE not used, overlap with RS_INIT code below.
; ---------------------------------------------------------------------------
$RS:        DW      $FD                 ; Next device = $FD (0x358D)
            DB      "RS",000H,000H      ; Device name
            DB      08FH                ; flags1: bit7=1 (no dir/free)
            DB      001H                ; flags2
            DB      000H                ; dirmx = 0
            DW      03372H              ; ZINIT
            DW      033A4H              ; ZRO
            DW      033A4H              ; ZWO (same handler as ZRO)
            DW      033B5H              ; ZCL
            DW      033B5H              ; ZKL (same handler as ZCL)
            DW      033C6H              ; ZINP
            DW      033DBH              ; ZOUT
            DW      000E5H              ; ZDELT (= monitor ROM stub)
; ZWDIR/ZFREE implicit — next 4 bytes are ZINIT code, read as DW pointers
; (flags1 bit 7=1 → ZWDIR/ZFREE never called)
RS_INIT:    RET     C
            PUSH    IY
            CALL    L33EC
            CALL    L3380
            CALL    L3435
            JR      L33D8               ; (+058H)

L3380:      RST     018H
            DB      013H
            LD      (IY-003H),E
            CALL    TEST1
            DB      02CH
            JP      NZ,L1F6A
            RST     018H
            DB      013H
            LD      (IY-002H),E
            LD      (IY-008H),00DH
            CALL    TEST1
            DB      000H
            RET     Z
            CALL    TEST1
            DB      02CH
            RST     018H
            DB      013H
            LD      (IY-008H),E
            RET     

            PUSH    IY
            CALL    L33EC
            LD      A,(IY-001H)
            INC     (IY-001H)
            OR      A
            CALL    Z,L3497
            JR      L33D8               ; (+023H)

            PUSH    IY
            CALL    L33EC
            DEC     (IY-001H)
            LD      A,(IY-001H)
            OR      A
            CALL    Z,L347F
            JR      L33D7               ; (+011H)

            PUSH    IY
            CALL    L33EC
            CALL    L34E6
            JR      C,L33D8             ; (+008H)
            CP      (IY-008H)
            JR      NZ,L33D7            ; (+002H)
            LD      A,00DH
L33D7:      OR      A
L33D8:      POP     IY
            RET     

            PUSH    IY
            CALL    L33EC
            CP      00DH
            JR      NZ,L33E7            ; (+003H)
            LD      A,(IY-008H)
L33E7:      CALL    L352B
            JR      L33D8               ; (-014H)

L33EC:      PUSH    AF
            PUSH    DE
            LD      A,(ZCH)
            INC     A
            LD      IY,033F7H
            LD      DE,L0015
L33F9:      ADD     IY,DE
            DEC     A
            JR      NZ,L33F9            ; (-005H)
            LD      C,(IY+000H)
            POP     DE
            POP     AF
            RET     

            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            OR      C
            OR      B
            NOP     
            DJNZ    L3421               ; (+010H)
            INC     B
            LD      B,B
            INC     BC
            RET     NZ
            DEC     B
            LD      H,B
            JR      NC,L341C            ; (+003H)
            NOP     
            NOP     
            NOP     
L341C:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
L3421:      OR      E
            OR      D
            NOP     
            DJNZ    03436H               ; (+010H)
            INC     B
            LD      B,B
            INC     BC
            RET     NZ
            DEC     B
            LD      H,B
            JR      NC,L3431            ; (+003H)
L342E:      CALL    BRKCHK
L3431:      RET     NZ
            JP      0017DH

L3435:      LD      A,018H
            OUT     (C),A
            LD      A,030H
            OUT     (C),A
            LD      A,(IY-002H)
            AND     00CH
            JR      NZ,L3448            ; (+004H)
            SET     2,(IY-002H)
L3448:      LD      A,(IY-002H)
            LD      B,A
            AND     00FH
            OR      040H
            LD      (IY+006H),A
            LD      A,B
            AND     080H
            OR      040H
            LD      (IY+008H),A
            RRA     
            AND     07FH
            OR      00AH
            LD      (IY+00AH),A
            LD      A,B
            OR      07FH
            LD      (IY+002H),A
            CALL    L348B
L346C:      IN      A,(C)
            RRCA    
            RET     NC
            DEC     C
            IN      A,(C)
            INC     C
            LD      A,001H
            OUT     (C),A
            IN      A,(C)
            AND     070H
            JR      Z,L346C             ; (-012H)
            RET     

L347F:      RES     0,(IY+008H)
            RES     7,(IY+00AH)
            LD      (IY-001H),000H
L348B:      LD      B,00AH
            PUSH    IY
            POP     HL
            LD      DE,L0003
            ADD     HL,DE
            OTIR    
            RET     

L3497:      LD      A,030H
            OUT     (C),A
            RET     

L349C:      SET     0,(IY+008H)
            LD      A,013H
            OUT     (C),A
            LD      A,(IY+008H)
            OUT     (C),A
            LD      A,035H
            OUT     (C),A
            LD      A,(IY+00AH)
            OR      088H
            LD      (IY+00AH),A
            OUT     (C),A
            RET     

            LD      A,003H
            OUT     (C),A
            RES     0,(IY+008H)
            LD      A,(IY+008H)
            OUT     (C),A
L34C5:      RES     7,(IY+00AH)
            JR      L34DB               ; (+010H)

L34CB:      SET     1,(IY+00AH)
            JR      L34DB               ; (+00aH)

L34D1:      RES     1,(IY+00AH)
            JR      L34DB               ; (+004H)

            SET     7,(IY+00AH)
L34DB:      LD      A,005H
            OUT     (C),A
            LD      A,(IY+00AH)
            OUT     (C),A
            OR      A
            RET     

L34E6:      CALL    SKPDE
            LD      C,(IY+000H)
L34EC:      CALL    L342E
            CALL    L3564
            JR      C,L34EC             ; (-008H)
            CALL    L349C
L34F7:      CALL    L342E
            IN      A,(C)
            RRCA    
            JR      NC,L34F7            ; (-008H)
            DEC     C
            IN      A,(C)
            INC     C
            AND     (IY+002H)
            PUSH    AF
            LD      A,001H
            OUT     (C),A
            IN      A,(C)
            AND     070H
            JR      NZ,L3516            ; (+005H)
            CALL    L34C5
            POP     AF
            RET     

L3516:      LD      B,A
            POP     AF
            PUSH    BC
            CALL    L3435
            POP     BC
            LD      A,01DH
            RLC     B
            RLC     B
            RET     C
            INC     A
            RLC     B
            RET     C
            INC     A
            SCF     
            RET     

L352B:      CALL    SKPDE
            LD      C,(IY+000H)
            LD      D,A
            CALL    L34CB
L3535:      CALL    L342E
            CALL    L356B
            CALL    NC,L357C
L353E:      JR      C,L3535             ; (-00bH)
            IN      A,(C)
            BIT     2,A
            JR      Z,L3535             ; (-011H)
            BIT     7,(IY-003H)
            JR      Z,L3555             ; (+009H)
            LD      A,001H
            OUT     (C),A
            IN      A,(C)
            RRCA    
            JR      NC,L3535            ; (-020H)
L3555:      DEC     C
            OUT     (C),D
            INC     C
            BIT     6,(IY-003H)
            JR      Z,L3562             ; (+003H)
            CALL    L34D1
L3562:      OR      A
            RET     

L3564:      OR      A
            BIT     0,(IY-003H)
            JR      L3570               ; (+005H)

L356B:      OR      A
            BIT     1,(IY-003H)
L3570:      RET     Z
            LD      A,010H
            OUT     (C),A
            IN      A,(C)
            AND     008H
            RET     NZ
            SCF     
            RET     

L357C:      OR      A
            BIT     2,(IY-003H)
            RET     Z
            LD      A,010H
            OUT     (C),A
            IN      A,(C)
            AND     020H
            RET     NZ
            SCF     
            RET     

; $FD descriptor (original, restored)
$FD:        DW      $QD                 ; Next device = $QD
            DB      "FD",000H,000H      ; Device name
            DB      03FH                ; ZFLAG1 (original, bit5=1 = ZINP-based DIR)
            DB      003H                ; ZFLAG2 (original)
            DB      03FH                ; ZDIRMX (original)
            DW      0368DH              ; func[0] ZINIT
            DW      0000FH              ; func[1] ZRO (original)
            DW      00100H              ; func[2] ZWO
            DW      00030H              ; func[3] ZCL
            DW      000FEH              ; func[4] ZKL
            DW      03705H              ; func[5] ZINP (original restored)
            DW      03708H              ; func[6] ZOUT
            DW      01F97H              ; func[7] ZDELT
            DW      01F97H              ; func[8] ZWDIR
            DW      01F97H              ; func[9] ZFREE
            DB      000H
L35AB:      DB      000H
L35AC:      DB      000H
L35AD:      DS      3, 000H
L35B0:      DB      000H
L35B1:      DS      2, 000H
L35B3:      DB      000H
L35B4:      DB      000H
L35B5:      DB      000H
L35B6:      DB      000H
L35B7:      DS      4, 000H
L35BB:      DS      4, 000H
L35BF:      DB      000H
L35C0:      PUSH    BC
            LD      A,L
            LD      B,004H
L35C4:      SRL     H
            RR      L
            DJNZ    L35C4               ; (-006H)
            AND     00FH
            INC     A
            LD      H,A
            POP     BC
            RET     

L35D0:      LD      A,H
            LD      H,000H
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,HL
            DEC     A
            ADD     A,L
            LD      L,A
            RET     

L35DB:      CALL    SKPDE
            LD      HL,L35B5
            BIT     7,(HL)
            JR      NZ,L35EB            ; (+006H)
            CALL    L367C
            CALL    L3898
L35EB:      LD      A,(L35AC)
            BIT     2,A
            SET     2,A
            JR      Z,L35F6             ; (+002H)
            SET     5,A
L35F6:      SET     7,A
            LD      HL,L35B5
            BIT     6,(HL)
            JR      Z,L3601             ; (+002H)
            SET     6,A
L3601:      CALL    L361C
            LD      (HL),A
            LD      HL,L35B7
            CALL    L3614
            BIT     0,(HL)
            CALL    Z,L3695
            RET     C
            SET     0,(HL)
            RET     

L3614:      LD      A,(L35AC)
            AND     003H
            JP      ADDHLA

L361C:      OUT     (0DCH),A
            PUSH    AF
            RLCA    
            RLCA    
            OUT     (0DDH),A
            RLCA    
            OUT     (0DEH),A
            POP     AF
            RET     

L3628:      CALL    SKPDE
            LD      D,000H
            LD      A,(L35B3)
            SRL     A
            CPL     
            OUT     (0DBH),A
            LD      B,A
            LD      A,(L35B5)
            RES     6,A
            BIT     5,A
            JR      Z,L3640             ; (+001H)
            CCF     
L3640:      JR      C,L3644             ; (+002H)
            SET     6,A
L3644:      CALL    L361C
            LD      HL,L35B5
            CP      (HL)
            LD      (HL),A
            JR      Z,L364F             ; (+001H)
            INC     D
L364F:      LD      HL,L35BB
            CALL    L3614
            LD      A,(HL)
            OUT     (0D9H),A
            CP      B
            JR      Z,L3666             ; (+00bH)
            INC     D
            CALL    L3673
            PUSH    AF
            IN      A,(0D9H)
            LD      (HL),A
            POP     AF
            RET     C
            RET     NZ
L3666:      LD      A,(L35B4)
            CPL     
            OUT     (0DAH),A
            LD      A,D
            OR      A
            CALL    NZ,L388D
            XOR     A
            RET     

L3673:      LD      A,018H
            CALL    L36C2
            RET     C
            AND     099H
            RET     

L367C:      LD      HL,L35B5
            SET     7,(HL)
            LD      A,(HL)
            CALL    L361C
            LD      HL,L35B7
            LD      B,004H
            JP      ?CLRHL

L368D:      XOR     A
            CALL    L361C
            LD      (L35B5),A
            RET     

L3695:      CALL    SKPDE
            LD      HL,L35BB
            CALL    L3614
            LD      (HL),0FFH
            LD      A,008H
            CALL    L36C2
            RET     C
            AND     085H
            XOR     004H
            RET     Z
            JP      L385A

L36AE:      PUSH    AF
            LD      A,LSD0
            CALL    L36BB
            JR      C,L36B8             ; (+002H)
            POP     AF
            RET     

L36B8:      POP     HL
            POP     HL
            RET     

L36BB:      CALL    SKPDE
            LD      D,000H
            JR      L36DC               ; (+01aH)

L36C2:      CALL    SKPDE
            LD      B,A
            LD      A,(035AAH)
            CPL     
            AND     003H
            OR      B
            LD      D,000H
            JR      L36D6               ; (+005H)

L36D1:      CALL    SKPDE
            LD      D,001H
L36D6:      CALL    L36AE
            LD      (L35B6),A
L36DC:      CPL     
            OUT     (0D8H),A
            CALL    L3887
            LD      E,007H
L36E4:      LD      HL,L2710
L36E7:      DEC     HL
            LD      A,H
            OR      L
            JP      Z,L36F9
            IN      A,(0D8H)
            CPL     
            LD      B,A
            AND     081H
            CP      D
            JR      NZ,L36E7            ; (-00fH)
            LD      A,B
            OR      A
            RET     

L36F9:      DEC     E
            JP      Z,L385A
            LD      A,(L35B6)
            CPL     
            OUT     (0D8H),A
            JR      L36E4               ; (-021H)

            DB      037H
            JR      L3709               ; (+001H)

            OR      A
L3709:      EX      AF,AF'
            PUSH    AF
            EX      AF,AF'
            LD      (L35B1),HL
            PUSH    HL
            LD      HL,L35AB
            RL      (HL)
            RRC     (HL)
            LD      H,(HL)
            LD      L,A
            AND     007H
            LD      (L35AC),A
            CALL    L35DB
            JR      NC,L3728            ; (+005H)
            POP     HL
            EX      AF,AF'
            POP     AF
            EX      AF,AF'
            RET     

L3728:      BIT     7,H
            LD      A,L
            POP     HL
            PUSH    HL
            CALL    Z,L37E7
            LD      (035AFH),DE
            PUSH    DE
            OR      A
            PUSH    AF
            LD      H,B
            LD      L,C
            CALL    P,L35C0
            LD      (L35AD),HL
            LD      A,009H
            LD      (L35BF),A
            PUSH    IY
            CALL    L375C
            EX      AF,AF'
            POP     IY
            LD      HL,(L35B3)
            POP     AF
            CALL    P,L35D0
            LD      B,H
            LD      C,L
            POP     DE
            POP     HL
            POP     AF
            EX      AF,AF'
            JP      L37E7

L375C:      LD      E,000H
            LD      HL,(035AFH)
            LD      A,(L35B5)
            BIT     5,A
            JR      Z,L376B             ; (+003H)
            ADD     HL,HL
            LD      E,080H
L376B:      LD      A,L
            OR      A
            JR      Z,L3770             ; (+001H)
            INC     H
L3770:      LD      A,H
            LD      (L35B0),A
L3774:      CALL    L37FA
            CALL    NC,L3628
            RET     C
            JR      NZ,L37E1            ; (+064H)
            DI      
            LD      A,(L35AB)
            BIT     7,A
            LD      A,090H
            LD      IY,0A2EDH
            JR      NZ,L3791            ; (+006H)
            LD      A,0B4H
            LD      IY,0A3EDH
L3791:      LD      (L37A8),IY
            CALL    L36D1
            RET     C
L3799:      LD      B,E
            PUSH    DE
            LD      DE,0FE7CH
L379E:      IN      A,(0D8H)
            OR      E
            CP      D
            JR      Z,L379E             ; (-006H)
            CP      LSFC
            JR      NZ,L37AD            ; (+005H)
L37A8:      NOP     
            NOP     
            JP      NZ,L379E
L37AD:      POP     DE
            IN      A,(0D8H)
            RRA     
            JP      C,L385A
            LD      A,(L35B4)
            INC     A
            LD      (L35B4),A
            CP      011H
            JR      Z,L37C4             ; (+005H)
            DEC     D
            JR      NZ,L3799            ; (-029H)
            JR      L37C5               ; (+001H)

L37C4:      DEC     D
L37C5:      CALL    L36AE
            EI      
            IN      A,(0D8H)
            CPL     
            OR      A
            JR      NZ,L37E1            ; (+012H)
            LD      A,(L35AB)
            OR      A
            CALL    Z,L3830
            JR      C,L37E1             ; (+009H)
            CALL    L3818
            LD      A,D
            OR      A
            JR      NZ,L3774            ; (-06bH)
            XOR     A
            RET     

L37E1:      CALL    L380B
            RET     C
            JR      L3774               ; (-073H)

L37E7:      CALL    IOOUT
            LD      A,(L35B5)
            BIT     5,A
            RET     Z
L37F0:      LD      A,(HL)
            CPL     
            LD      (HL),A
            INC     HL
            DEC     DE
            LD      A,D
            OR      E
            JR      NZ,L37F0            ; (-009H)
            RET     

L37FA:      LD      HL,(L35AD)
            LD      (L35B3),HL
            LD      HL,(L35B1)
            LD      A,(L35B0)
            LD      D,A
            LD      C,0DBH
            OR      A
            RET     

L380B:      CALL    SKPDE
            LD      HL,L35BF
            DEC     (HL)
            JP      Z,L385A
            JP      L3695

L3818:      LD      (L35B1),HL
            LD      A,D
            LD      (L35B0),A
            LD      HL,(L35B3)
            LD      A,H
            CP      011H
            RET     NZ
            LD      H,001H
            INC     L
            LD      (L35AD),HL
            LD      (L35B3),HL
            RET     

L3830:      LD      A,(035AEH)
            CPL     
            OUT     (0DAH),A
            LD      A,090H
            CALL    L36D1
            RET     C
            LD      A,(L35B4)
            LD      B,A
L3840:      IN      A,(0D8H)
            CPL     
            AND     081H
            CP      001H
            JR      NZ,L3852            ; (+009H)
            IN      A,(0DAH)
            CPL     
            CP      B
            JR      NZ,L3840            ; (-00fH)
            CALL    L36AE
L3852:      IN      A,(0D8H)
            CPL     
            AND     099H
            RET     Z
            SCF     
            RET     

L385A:      LD      A,(L35B6)
            RLA     
            IN      A,(0D8H)
            CPL     
            JR      NC,L3878            ; (+015H)
            RLA     
            JR      C,L387B             ; (+015H)
            RLA     
            JR      NC,L3870            ; (+007H)
            LD      HL,L35AB
            BIT     7,(HL)
            JR      Z,L387F             ; (+00fH)
L3870:      AND     0C0H
            JR      NZ,L3883            ; (+00fH)
L3874:      LD      A,029H
            SCF     
            RET     

L3878:      RLA     
            JR      NC,L3874            ; (-007H)
L387B:      LD      A,032H
            SCF     
            RET     

L387F:      LD      A,02EH
            SCF     
            RET     

L3883:      LD      A,036H
            SCF     
            RET     

L3887:      PUSH    DE
            LD      DE,L000F
            JR      L3891               ; (+004H)

L388D:      PUSH    DE
            LD      DE,L2026
L3891:      DEC     DE
            LD      A,E
            OR      D
            JR      NZ,L3891            ; (-005H)
            POP     DE
            RET     

L3898:      PUSH    BC
            LD      B,011H
L389B:      CALL    L388D
            DJNZ    L389B               ; (-005H)
            POP     BC
            RET     

; ---------------------------------------------------------------------------
; $QD Device Descriptor (Quick Disk)
; ---------------------------------------------------------------------------
$QD:        DW      $RFS                ; Next device = $RFS (was $USR at 0D5EH)
            DB      "QD",000H,000H      ; Device name
            DB      05FH                ; flags1: bit5=1 ZINP-based DIR, bit6=1 ZRO enum
            DB      040H                ; flags2
            DB      020H                ; dirmx: 32 (max directory entries)
            DW      QDINIT              ; ZINIT handler
            DW      QDRO                ; ZRO handler
            DW      QDWO                ; ZWO handler
            DW      QDCL                ; ZCL handler
            DW      00400H              ; ZKL (constant)
            DW      QDINP               ; ZINP handler
            DW      QDOUT               ; ZOUT handler
            DW      00000H              ; ZDELT (not supported)
            DW      00000H              ; ZWDIR (not supported)
            DW      QDFREE              ; ZFREE handler
;
; ---------------------------------------------------------------------------
; QDFREE - Free space query (returns carry = not supported)
; ---------------------------------------------------------------------------
QDFREE:     XOR     A
            SCF
            RET
;
; ---------------------------------------------------------------------------
; QDINIT - Quick Disk initialization
; ---------------------------------------------------------------------------
QDINIT:     RET     C
            CALL    TEST1
            DB      059H                ; Test for 'Y'
            JR      Z,L38D0             ; (+007H)
            OR      A
            JP      NZ,L1F6A
            CALL    L1E7A
L38D0:      CALL    L3924
            RET     C
            RST     018H
            DB      010H
            LD      C,002H
            JR      L3919               ; (+03fH)
;
; ---------------------------------------------------------------------------
; QDCL - Quick Disk close
; ---------------------------------------------------------------------------
QDCL:       OR      A
            JR      NZ,L38FA            ; (+01dH)
            RST     018H
            DB      010H
            XOR     A
            LD      (L395E),A
            LD      C,005H
            CALL    L38EB
            LD      BC,00001H
L38EB:      LD      (L3954),HL
            LD      (03956H),DE
            LD      HL,L3953
            LD      (HL),B
            DEC     HL
            LD      (HL),C
            JR      L3968               ; (+06eH)

L38FA:      CALL    SKPDE
            LD      C,006H
            JR      L3919               ; (+018H)
;
; ---------------------------------------------------------------------------
; QDRO - Quick Disk read open (ZRO handler)
; ---------------------------------------------------------------------------
QDRO:       LD      BC,L0003
            LD      DE,00040H
            CALL    L38EB
            RET     NC
            CP      028H
            SCF
            RET     NZ
            LD      A,000H
            RET
;
; ---------------------------------------------------------------------------
; QDINP - Quick Disk input (ZINP handler)
; ---------------------------------------------------------------------------
QDINP:      RST     018H
            DB      010H
            LD      D,B
            LD      E,C
            LD      BC,00103H
L3919:      CALL    L38EB
            RST     018H
            DB      011H
            RET
;
; ---------------------------------------------------------------------------
; QDWO - Quick Disk write open (ZWO handler)
; ---------------------------------------------------------------------------
QDWO:       LD      A,037H
            LD      (L392C),A
L3924:      LD      BC,L0101
            JR      L38EB               ; (-03eH)
;
; ---------------------------------------------------------------------------
; QDOUT - Quick Disk output (ZOUT handler)
; ---------------------------------------------------------------------------
QDOUT:      PUSH    AF
            RST     018H
            DB      010H
L392C:      XOR     A
            JR      NC,L3943            ; (+014H)
            LD      A,0AFH
            LD      (L392C),A
            CALL    L1C2B
            LD      (L3958),HL
            LD      HL,ELMD
            LD      DE,00040H
            POP     AF
            JR      L3949               ; (+006H)

L3943:      LD      (L3958),HL
            POP     AF
            SET     0,A
L3949:      LD      (0395AH),BC
            LD      B,A
            LD      C,004H
            JR      L3919               ; (-039H)

            DB      000H
L3953:      DB      000H
L3954:      DS      4, 000H
L3958:      DS      4, 000H
L395C:      DB      000H
L395D:      DB      000H
L395E:      DB      000H
L395F:      DB      000H
L3960:      DB      000H
L3961:      DB      000H
L3962:      DB      000H
L3963:      DB      000H
L3964:      DB      000H
L3965:      DS      3, 000H
L3968:      LD      A,005H
            LD      (L3964),A
L396D:      DI      
            CALL    L39BC
            EI      
            RET     NC
            PUSH    AF
            CP      028H
            JR      Z,L39BA             ; (+042H)
            CALL    L3C37
            LD      A,(03952H)
            CP      004H
            JR      NZ,L399F            ; (+01dH)
            LD      A,(L3960)
            OR      A
            JR      Z,L399F             ; (+017H)
            XOR     A
            LD      (L3960),A
            LD      A,(L3961)
            PUSH    HL
            LD      (03966H),SP
            POP     HL
            DI      
            CALL    L3BB7
            EI      
            JR      C,L39B4             ; (+018H)
            CALL    L3C37
L399F:      POP     AF
            PUSH    AF
            CP      029H
            JR      NZ,L39B4            ; (+00fH)
            LD      HL,L3964
            DEC     (HL)
            JR      Z,L39B4             ; (+009H)
            POP     AF
            LD      A,(L395F)
            LD      (L395E),A
            JR      L396D               ; (-047H)

L39B4:      CALL    L3B29
            CALL    L39D5
L39BA:      POP     AF
            RET     

L39BC:      LD      (03966H),SP
            LD      A,(03952H)
            DEC     A
            JR      Z,L39DC             ; (+016H)
            DEC     A
            JR      Z,L39E2             ; (+019H)
            DEC     A
            JR      Z,L3A2C             ; (+060H)
            DEC     A
            JP      Z,L3A9C
            DEC     A
            JR      Z,L39D5             ; (+002H)
            JR      L3A25               ; (+050H)

L39D5:      PUSH    AF
            XOR     A
            LD      (L395C),A
            POP     AF
            RET     

L39DC:      LD      A,(L3953)
            JP      L3B8B

L39E2:      XOR     A
            CALL    L3BB7
            CALL    L3CB5
            LD      BC,0EFFFH
            LD      A,0AAH
L39EE:      CPL     
            LD      D,A
            CALL    L3D2C
            DEC     BC
            LD      A,B
            OR      C
            JR      Z,L39FB             ; (+003H)
            LD      A,D
            JR      L39EE               ; (-00dH)

L39FB:      CALL    L3D02
            CALL    L3C37
            CALL    L3BEA
            LD      A,(L3962)
            DEC     A
            JR      NZ,L3A28            ; (+01eH)
            CALL    L3C4C
            LD      BC,0EFFFH
            LD      E,055H
L3A12:      CP      E
            JR      NZ,L3A28            ; (+013H)
            DEC     BC
            LD      A,B
            OR      C
            JR      Z,L3A22             ; (+008H)
            LD      A,E
            CPL     
            LD      E,A
            CALL    L3D42
            JR      L3A12               ; (-010H)

L3A22:      CALL    L3D14
L3A25:      JP      L3C37

L3A28:      LD      A,029H
            SCF     
            RET     

L3A2C:      LD      A,(L3963)
            OR      A
            CALL    Z,L3BEA
            CALL    L3A62
            RET     C
            CALL    L3D87
            CALL    L3D42
            LD      C,A
L3A3E:      CALL    L3D42
            LD      B,A
            LD      HL,(03956H)
            SBC     HL,BC
            JP      C,L3B33
            LD      HL,(L3954)
L3A4D:      CALL    L3D42
            LD      (HL),A
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3A4D            ; (-00aH)
            CALL    L3D14
            LD      A,(L3953)
            BIT     0,A
            JR      NZ,L3A25            ; (-03cH)
            RET     

L3A62:      LD      HL,L3962
            DEC     (HL)
            JR      Z,L3A98             ; (+030H)
            CALL    L3C4C
            LD      C,A
            LD      A,(L395C)
            LD      HL,L395D
            CP      (HL)
            JR      NZ,L3A95            ; (+020H)
            INC     A
            LD      (L395C),A
            LD      (HL),A
            LD      A,(L3953)
            XOR     C
            RRA     
            RET     NC
L3A80:      CALL    L3D42
            LD      C,A
            CALL    L3D42
            LD      B,A
L3A88:      CALL    L3D42
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3A88            ; (-008H)
            CALL    L3D14
            JR      L3A62               ; (-033H)

L3A95:      INC     (HL)
            JR      L3A80               ; (-018H)

L3A98:      LD      A,028H
            SCF     
            RET     

L3A9C:      LD      A,(L395E)
            LD      (L395F),A
            LD      A,(L3963)
            OR      A
            JR      NZ,L3ABF            ; (+017H)
            CALL    L3BEA
            LD      A,(L395E)
            LD      HL,L3962
            ADD     A,(HL)
            LD      (L3962),A
            INC     A
            LD      (L395C),A
            CALL    L3A62
            JP      NC,L3B33
L3ABF:      LD      A,(L3953)
            LD      B,A
            AND     001H
            JR      NZ,L3AD3            ; (+00cH)
            LD      DE,L3954
            LD      A,B
            RES     2,A
            CALL    L3B3C
            CALL    L3D87
L3AD3:      LD      DE,L3958
            LD      A,(L3953)
            SET     0,A
            CALL    L3B3C
            CALL    L3C37
            CALL    L3D87
            LD      A,(L3953)
            AND     004H
            JR      Z,L3B37             ; (+04cH)
            LD      A,(L3961)
            LD      HL,L395E
            ADD     A,(HL)
            CALL    L3BB7
            LD      A,001H
            LD      (L3960),A
L3AFA:      LD      A,(L3961)
            LD      HL,L395F
            ADD     A,(HL)
            INC     A
            LD      (L3962),A
            INC     A
            LD      (L395C),A
            CALL    L3A62
            JR      NC,L3B33            ; (+025H)
            LD      A,(L3953)
            AND     001H
            JR      NZ,L3B1C            ; (+007H)
            LD      DE,L3954
            CALL    L3B6A
            RET     C
L3B1C:      LD      DE,L3958
            CALL    L3B6A
            RET     C
            LD      A,(L3960)
            OR      A
            JR      Z,L3B30             ; (+007H)
L3B29:      XOR     A
            LD      (L395E),A
            LD      (L3960),A
L3B30:      JP      L3C37

L3B33:      LD      A,029H
            SCF     
            RET     

L3B37:      CALL    L3BEA
            JR      L3AFA               ; (-042H)

L3B3C:      PUSH    AF
            LD      HL,L395E
            INC     (HL)
            CALL    L3CB5
            POP     AF
            CALL    L3D2C
            CALL    L3B60
            LD      A,C
            CALL    L3D2C
            LD      A,B
            CALL    L3D2C
L3B53:      LD      A,(HL)
            CALL    L3D2C
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3B53            ; (-00aH)
            JP      L3D02

L3B60:      EX      DE,HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            EX      DE,HL
            RET     

L3B6A:      CALL    L3C4C
            CALL    L3B60
            CALL    L3D42
            CP      C
            JR      NZ,L3B33            ; (-043H)
            CALL    L3D42
            CP      B
            JR      NZ,L3B33            ; (-049H)
L3B7C:      CALL    L3D42
            CP      (HL)
            JR      NZ,L3B33            ; (-04fH)
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3B7C            ; (-00cH)
            JP      L3D14

L3B8B:      LD      B,A
            LD      A,002H
            OUT     (0F7H),A
            LD      A,081H
            OUT     (0F7H),A
            LD      A,002H
            OUT     (0F7H),A
            IN      A,(0F7H)
            AND     0F0H
            CP      080H
            JP      NZ,03D58H
            LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            LD      C,A
            AND     008H
            JP      Z,03D58H
            LD      A,B
            OR      A
            RET     Z
            LD      A,C
            AND     020H
            RET     NZ
            JP      03D55H

L3BB7:      PUSH    AF
            LD      HL,03D7EH
            LD      B,009H
            CALL    L3C27
L3BC0:      LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     008H
            JP      Z,03D58H
            LD      A,010H
            OUT     (0F7H),A
            IN      A,(0F7H)
            AND     008H
            JR      Z,L3BC0             ; (-015H)
            LD      BC,L00E9
            CALL    L3D66
            CALL    L3CEA
            CALL    L3CCE
            POP     AF
            CALL    L3D2C
            CALL    L3D02
            JR      L3C1D               ; (+033H)

L3BEA:      LD      HL,L3D73
            LD      B,00BH
            CALL    L3C27
L3BF2:      LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     008H
            JP      Z,03D58H
            CALL    L3D87
            LD      A,010H
            OUT     (0F7H),A
            IN      A,(0F7H)
            AND     008H
            JR      Z,L3BF2             ; (-018H)
            LD      BC,L00E9
            CALL    L3D66
            CALL    L3C62
            LD      (L3961),A
            INC     A
            LD      (L3962),A
            CALL    L3D14
L3C1D:      LD      HL,L3965
            SET     3,(HL)
            XOR     A
            LD      (L395D),A
            RET     

L3C27:      LD      C,0F6H
            OTIR    
            LD      A,005H
            LD      (L3963),A
            OUT     (0F7H),A
            LD      A,080H
            OUT     (0F7H),A
            RET     

L3C37:      PUSH    AF
            LD      A,005H
            OUT     (0F6H),A
            LD      A,060H
            OUT     (0F6H),A
            LD      A,005H
            OUT     (0F7H),A
            XOR     A
            LD      (L3963),A
            OUT     (0F7H),A
            POP     AF
            RET     

L3C4C:      LD      A,058H
            LD      B,00BH
            LD      HL,L3D73
            CALL    L3CF3
            LD      HL,L3965
            BIT     3,(HL)
            LD      BC,L0003
            JR      Z,L3C65             ; (+005H)
            RES     3,(HL)
L3C62:      LD      BC,L00A0
L3C65:      CALL    L3D66
            LD      A,005H
            OUT     (0F7H),A
            LD      A,082H
            OUT     (0F7H),A
            LD      A,003H
            OUT     (0F6H),A
            LD      A,LSD3
            OUT     (0F6H),A
            LD      BC,L2CC0
L3C7B:      LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     010H
            JR      Z,L3C8D             ; (+008H)
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3C7B            ; (-00fH)
            JP      03D5EH

L3C8D:      LD      A,003H
            OUT     (0F6H),A
            LD      A,0C3H
            OUT     (0F6H),A
            LD      B,09FH
L3C97:      LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     001H
            JR      NZ,L3CA7            ; (+006H)
            DEC     B
            JR      NZ,L3C97            ; (-00dH)
            JP      03D5EH

L3CA7:      LD      A,003H
            OUT     (0F6H),A
            LD      A,0C9H
            OUT     (0F6H),A
            CALL    L3D42
            JP      L3D42

L3CB5:      LD      A,098H
            LD      B,009H
            LD      HL,03D7EH
            CALL    L3CF3
            CALL    L3CEA
            LD      HL,L3965
            BIT     3,(HL)
            LD      BC,0001DH
            JR      Z,L3CD1             ; (+005H)
            RES     3,(HL)
L3CCE:      LD      BC,L0140
L3CD1:      CALL    L3D66
            LD      A,005H
            OUT     (0F6H),A
            LD      A,0EFH
            OUT     (0F6H),A
            LD      BC,00001H
            CALL    L3D66
            LD      A,0C0H
            OUT     (0F6H),A
            LD      A,0A5H
            JR      L3D2C               ; (+042H)

L3CEA:      LD      A,005H
            OUT     (0F6H),A
            LD      A,0FFH
            OUT     (0F6H),A
            RET     

L3CF3:      LD      C,0F6H
            OUT     (C),A
            LD      A,005H
            OUT     (0F7H),A
L3CFB:      LD      A,080H
            OUT     (0F7H),A
            OTIR    
            RET     

L3D02:      LD      BC,00001H
            CALL    L3D66
            LD      A,010H
            OUT     (0F7H),A
            IN      A,(0F7H)
            AND     008H
            RET     NZ
            JP      03D5BH

L3D14:      LD      B,003H
L3D16:      CALL    L3D42
            DJNZ    L3D16               ; (-005H)
L3D1B:      IN      A,(0F6H)
            RRCA    
            JR      NC,L3D1B            ; (-005H)
            LD      A,001H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     040H
            JR      NZ,L3D52            ; (+028H)
            OR      A
            RET     

L3D2C:      PUSH    AF
L3D2D:      IN      A,(0F6H)
            AND     004H
            JR      Z,L3D2D             ; (-006H)
            POP     AF
            OUT     (QDPC),A
L3D36:      LD      A,010H
            OUT     (0F6H),A
            IN      A,(0F6H)
            AND     008H
            JP      Z,03D58H
            RET     

L3D42:      CALL    L3D36
            IN      A,(0F6H)
            RLCA    
            JR      C,L3D52             ; (+008H)
            RRCA    
            RRCA    
            JR      NC,L3D42            ; (-00cH)
            IN      A,(QDPC)
            OR      A
            RET     

L3D52:      LD      A,029H
            LD      HL,02E3EH
            LD      HL,0323EH
            LD      HL,L353E
            LD      HL,0363EH
            LD      SP,(03966H)
            SCF     
            RET     

L3D66:      PUSH    AF
L3D67:      LD      A,096H
L3D69:      DEC     A
            JR      NZ,L3D69            ; (-003H)
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L3D67            ; (-00aH)
            POP     AF
            RET     

L3D73:      LD      E,B
            INC     B
            DJNZ    L3D7C               ; (+005H)
            INC     B
            INC     BC
            RET     NC
            LD      B,016H
L3D7C:      RLCA    
            LD      D,098H
            INC     B
            DJNZ    03D88H               ; (+006H)
            LD      D,007H
            LD      D,005H
            LD      L,L
L3D87:      LD      A,0E8H
            OUT     (LSE3),A
            LD      (MMIO),A
            NOP     
            LD      A,(0E001H)
            OUT     (LSE1),A
            AND     081H
            RET     NZ
            CALL    L3B29
            JP      L017B

            PUSH    HL
            LD      DE,(00188H)
            PUSH    DE
            LD      DE,L3DE4
            LD      (00188H),DE
            LD      A,(L004F)
            PUSH    AF
            LD      A,064H
            LD      (L004F),A
            LD      (03E5BH),SP
            XOR     A
            LD      (FILOUT),A
L3DBB:      LD      SP,L0000
L3DBE:      LD      BC,L3DBE
            PUSH    BC
            RST     018H
            DB      002H
            LD      A,02AH
            RST     018H
            DB      003H
L3DC8:      CALL    L3E67
            JR      NC,L3DC8            ; (-005H)
            LD      A,(DE)
            CP      02AH
            RET     NZ
            INC     DE
            LD      A,(DE)
            INC     DE
            EXX     
            LD      HL,L3DF6
            LD      B,00AH
L3DDA:      CP      (HL)
            INC     HL
            JR      Z,L3DF0             ; (+012H)
            INC     HL
            INC     HL
            DJNZ    L3DDA               ; (-008H)
            EXX     
            RET     

L3DE4:      LD      C,A
            AND     07FH
            JR      Z,L3DBB             ; (-02eH)
            LD      A,C
            RST     018H
            DB      01BH
            RST     018H
            DB      043H
            JR      L3DBB               ; (-035H)

L3DF0:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            PUSH    DE
            EXX     
            RET     

L3DF6:      LD      B,H
            CALL    C,L4D3E
            LD      C,(HL)
            CCF     
            LD      D,B
            INC     D
            LD      A,047H
            RST     010H
            LD      A,046H
            LD      (HL),D
            CCF     
            LD      D,D
L3E06:      LD      E,D
            LD      A,053H
            DEC     E
            LD      A,04CH
            INC     (HL)
            LD      A,056H
            LD      D,C
            LD      A,054H
            JP      NZ,03A3FH
            SUB     C
            DJNZ    L3E06               ; (-012H)
            LD      BC,09132H
            DJNZ    03DE6H               ; (-037H)
            CALL    L3FAB
            RET     C
            EXX     
            CALL    L3FD9
            EXX     
            LD      (ELMD20),BC
            LD      (ELMD22),DE
            LD      (ELMD24),HL
            RST     018H
            DB      031H
            RET     

            CALL    L3E88
            PUSH    HL
            PUSH    AF
            CALL    L3E46
            POP     AF
            POP     HL
            JR      NC,L3E43            ; (+003H)
            LD      HL,(ELMD22)
L3E43:      RST     018H
            DB      030H
            RET     

L3E46:      CALL    L3FD9
            RST     018H
            DB      02FH
            CP      001H
            RET     Z
            JP      01F9DH

            CALL    L3E46
            LD      HL,(ELMD22)
            RST     018H
            DB      032H
            RET     

            LD      SP,L0000
            POP     AF
            LD      (L004F),A
            POP     HL
            LD      (00188H),HL
            POP     HL
            RET     

L3E67:      LD      DE,0FF00H
            RST     018H
            DB      00CH
            JR      C,L3E86             ; (+018H)
            LD      A,(DE)
            CP      03AH
            SCF     
            RET     NZ
            INC     DE
            CALL    L3E88
            RET     C
            LD      A,(DE)
            INC     DE
            XOR     03DH
            RET     NZ
L3E7D:      CALL    L3EAD
            CCF     
            RET     NC
            LD      (HL),A
            INC     HL
            JR      L3E7D               ; (-009H)

L3E86:      LD      (DE),A
            RET     

L3E88:      PUSH    HL
            CALL    L3EA2
            PUSH    DE
            CALL    L3EB4
            JR      C,L3E9D             ; (+00bH)
            LD      H,A
            CALL    L3EB4
            JR      C,L3E9D             ; (+005H)
            LD      L,A
            POP     AF
            POP     AF
            XOR     A
            RET     

L3E9D:      POP     DE
            POP     HL
            SCF     
            RET     

L3EA1:      INC     DE
L3EA2:      LD      A,(DE)
            CP      020H
            JR      Z,L3EA1             ; (-006H)
            RET     

L3EA8:      LD      A,(DE)
            RST     018H
            DB      015H
            INC     DE
            RET     

L3EAD:      CALL    L3EA2
            CP      03BH
            JR      Z,L3ED2             ; (+01eH)
L3EB4:      PUSH    BC
            PUSH    DE
            CALL    L3EA8
            JR      C,L3ECE             ; (+013H)
            LD      C,A
            CALL    L3EA8
            JR      C,L3ECE             ; (+00dH)
            LD      B,A
            LD      A,C
            RLCA    
            RLCA    
            RLCA    
            RLCA    
            ADD     A,B
            LD      C,A
            LD      A,C
            POP     BC
            POP     BC
            OR      A
            RET     

L3ECE:      POP     DE
            POP     BC
            SCF     
            RET     

L3ED2:      INC     DE
            LD      A,(DE)
            INC     DE
            OR      A
            RET     

            CALL    L3E88
            RET     C
            JP      (HL)

            CALL    L3E88
            JR      C,L3EEC             ; (+00bH)
            PUSH    HL
            CALL    L3E88
            JR      C,L3EEB             ; (+004H)
            POP     DE
            EX      DE,HL
            JR      L3EF2               ; (+007H)

L3EEB:      POP     HL
L3EEC:      EX      DE,HL
            LD      HL,L0080
            ADD     HL,DE
            EX      DE,HL
L3EF2:      LD      C,008H
            CALL    L3EFF
            RET     C
            PUSH    HL
            SBC     HL,DE
            POP     HL
            RET     NC
            JR      L3EF2               ; (-00dH)

L3EFF:      CALL    L3F27
            LD      B,C
            PUSH    HL
L3F04:      LD      A,(HL)
            CALL    L3F38
            INC     HL
            LD      A,020H
            RST     018H
            DB      009H
            DJNZ    L3F04               ; (-00bH)
            POP     HL
            LD      A,02FH
            RST     018H
            DB      009H
            LD      B,C
L3F15:      LD      A,(HL)
            CP      020H
            JR      NC,L3F1C            ; (+002H)
            LD      A,02EH
L3F1C:      RST     018H
            DB      009H
            INC     HL
            DJNZ    L3F15               ; (-00cH)
            RST     018H
            DB      008H
            RST     018H
            DB      00FH
            OR      A
            RET     

L3F27:      LD      A,03AH
            RST     018H
            DB      009H
            LD      A,H
            CALL    L3F38
            LD      A,L
            CALL    L3F38
            LD      A,03DH
            RST     018H
            DB      009H
            RET     

L3F38:      PUSH    AF
            RLCA    
            RLCA    
            RLCA    
            RLCA    
            CALL    L3F41
            POP     AF
L3F41:      AND     00FH
            ADD     A,030H
            CP      03AH
            JR      C,L3F4B             ; (+002H)
            ADD     A,007H
L3F4B:      RST     018H
            DB      009H
            RET     

            CALL    L3E88
            LD      A,(FILOUT)
            PUSH    AF
            XOR     A
            LD      (FILOUT),A
L3F59:      RST     018H
            DB      002H
            CALL    L3F27
            LD      A,(HL)
            CALL    L3F38
            LD      A,014H
            RST     018H
            DB      003H
            RST     018H
            DB      003H
            CALL    L3E67
            JR      NC,L3F59            ; (-014H)
            POP     AF
            LD      (FILOUT),A
            RET     

            CALL    L3E88
            RET     C
            PUSH    HL
            CALL    L3E88
            POP     BC
            RET     C
            PUSH    HL
            PUSH    BC
            LD      HL,0FF00H
            CALL    L3E7D
            LD      DE,0FF00H
            OR      A
            SBC     HL,DE
            LD      C,L
            POP     HL
            PUSH    HL
            EXX     
            POP     HL
            POP     DE
            EXX     
            RET     Z
L3F92:      CALL    L3FEC
            JR      NZ,L3F9B            ; (+004H)
            CALL    L3EFF
            RET     C
L3F9B:      RST     018H
            DB      00EH
            RET     Z
            EXX     
            INC     HL
            PUSH    HL
            SCF     
            SBC     HL,DE
            POP     HL
            RET     NC
            PUSH    HL
            EXX     
            POP     HL
            JR      L3F92               ; (-019H)

L3FAB:      CALL    L3E88
            PUSH    HL
            CALL    NC,L3E88
            POP     BC
            RET     C
            SBC     HL,BC
            INC     HL
            PUSH    HL
            PUSH    BC
            CALL    L3E88
            PUSH    HL
            EXX     
            POP     HL
            POP     DE
            POP     BC
            RET     

            CALL    L3FAB
            RET     C
            EX      DE,HL
            PUSH    HL
            SBC     HL,DE
            POP     HL
            JR      C,L3FD0             ; (+003H)
            LDIR    
            RET     

L3FD0:      ADD     HL,BC
            DEC     HL
            EX      DE,HL
            ADD     HL,BC
            DEC     HL
            EX      DE,HL
            LDDR    
            RET     

L3FD9:      LD      A,(DE)
            OR      A
            JR      Z,L3FE2             ; (+005H)
            INC     DE
            CP      03AH
            JR      NZ,L3FD9            ; (-009H)
L3FE2:      RST     018H
            DB      017H
            RST     018H
            DB      02DH
            LD      A,001H
            LD      (ELMD),A
            RET     

L3FEC:      LD      A,(DE)
            CP      (HL)
            RET     NZ
            PUSH    BC
            PUSH    DE
            PUSH    HL
            LD      B,C
L3FF3:      LD      A,(DE)
            CP      (HL)
            JR      NZ,L3FFC            ; (+005H)
            INC     DE
            INC     HL
            DJNZ    L3FF3               ; (-008H)
            XOR     A
L3FFC:      POP     HL
            POP     DE
            POP     BC
            RET     

L4000:      LD      HL,RFS_END          ; TEXTST: moved past RFS module (was 085FFH)
            LD      (TEXTST),HL
            LD      HL,L4046
            LD      (000E3H),HL
            LD      A,B
            OR      A
            JR      Z,L401B             ; (+00bH)
            LD      HL,L4036
            LD      DE,L1361
            LD      BC,00010H
            LDIR    
L401B:      LD      DE,L84E7
            RST     018H
            DB      005H
            JP      MEMCLI_PATCH        ; MEMCLI: call SVC 020H then set HL=RFS_END (was SVC 020H + EX DE,HL)
L4023:      LD      (HL),000H
            INC     HL
            LD      A,H
            CP      0FFH
            JR      C,L4023             ; (-008H)
L402B:      CALL    L4902
            CALL    L4914
            CALL    L5E1D
            JR      L404A               ; (+014H)

; ---------------------------------------------------------------------------
; Auto-run command string (16 bytes, copied into F-key slot at startup)
; L403E overlaps: referenced as "TO RUN"\r (suffix of "AUTO RUN"\r)
; ---------------------------------------------------------------------------
L4036:      DB      00FH,"RUN ",022H     ; Length (15) + 'RUN "'
L403E:      DB      "AUTO RUN",022H,00DH ; 'AUTO RUN"\r' (also read as 'TO RUN"\r' via L403E+2)
; ---------------------------------------------------------------------------
; Hook snippet installed at 0x00E3 — executed in-place as Z80 code
; ---------------------------------------------------------------------------
L4046:      LD      A,016H              ; 3E 16
            RST     018H                ; DF
            DB      003H                ; SVC #3
L404A:      LD      HL,L4B85
            LD      (00188H),HL
L4050:      RST     018H
            DB      002H
            LD      DE,L4B72
            RST     018H
            DB      005H
            RST     018H
            DB      001H
L4059:      LD      A,(L4B78)
            OR      A
            JR      NZ,L406B            ; (+00cH)
            LD      SP,(INTFAC)
            LD      HL,0FFFFH
            PUSH    HL
            LD      (08496H),SP
L406B:      LD      HL,L0000
            LD      (L8483),HL
            XOR     A
            LD      (L0050),A
            CALL    L48B1
            RST     018H
            DB      043H
            LD      DE,KEYBUF
            RST     018H
            DB      00CH
            JR      NC,L408D            ; (+00cH)
L4081:      LD      HL,L48B0
            LD      A,(HL)
            OR      A
            LD      (HL),000H
            JR      NZ,L4050            ; (-03aH)
            JR      L4059               ; (-033H)

L408C:      INC     DE
L408D:      LD      A,(DE)
            CP      020H
            JR      Z,L408C             ; (-006H)
            OR      A
            JR      Z,L4059             ; (-03cH)
            CALL    L5EC4
            JP      NC,L473E
            LD      HL,L84E7
            PUSH    HL
            CALL    L66EF
            INC     HL
            LD      (L847D),HL
            CALL    L4952
            POP     HL
            JR      L40AF               ; (+003H)

L40AC:      CALL    05E48H
L40AF:      LD      (08496H),SP
L40B3:      LD      DE,L40AC
            PUSH    DE
L40B7:      LD      (L8498),HL
            CALL    BRKCHK
            JP      Z,0017DH
L40C0:      LD      A,(HL)
            INC     HL
            CP      080H
            JR      NC,L411A            ; (+054H)
            CP      027H
            JP      Z,L4F8A
            CP      020H
            JR      Z,L40C0             ; (-00fH)
            CP      03AH
            JR      Z,L40B7             ; (-01cH)
            OR      A
            JR      Z,L40E1             ; (+00bH)
            DEC     HL
            SUB     041H
            CP      01AH
            JP      C,L4C99
            JP      L4AEC

L40E1:      LD      HL,(L847D)
L40E4:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      A,D
            OR      E
            JR      Z,L4103             ; (+018H)
            LD      (L0050),A
            EX      DE,HL
            ADD     HL,DE
            DEC     HL
            LD      (L847D),HL
            EX      DE,HL
            INC     HL
            CALL    LDDEMI
            LD      (08483H),DE
            CALL    L4971
            POP     DE
            JR      L40AF               ; (-054H)

L4103:      POP     HL
            CALL    L4146
            JP      Z,L4050
            XOR     A
            LD      (L4B78),A
            LD      A,(L8486)
            CP      002H
            JP      Z,04B19H
            PUSH    HL
            JP      L484D

L411A:      CP      0FFH
            JP      Z,L4CCB
            LD      DE,L44AC
            CP      0FEH
            JR      NZ,L412A            ; (+004H)
            LD      A,(HL)
            INC     HL
            JR      L4132               ; (+008H)

L412A:      CP      LSE0
            JP      NC,L4AEC
            LD      DE,L43EC
L4132:      ADD     A,A
            PUSH    HL
            EX      DE,HL
            LD      E,A
            LD      D,000H
            ADD     HL,DE
            LD      A,(HL)
            INC     HL
            LD      H,(HL)
            LD      L,A
            EX      (SP),HL
L413E:      CALL    HLFTCH
L4141:      OR      A
            RET     Z
            CP      03AH
            RET     

L4146:      PUSH    HL
            LD      HL,(L8483)
            LD      A,L
            OR      H
            POP     HL
            RET     

            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
STMTWORDTBL:DB      "GOT", "O" | 080H ; BASIC statement keyword text table
            DB      "GOSU", "B" | 080H
            DB      080H
            DB      "RU", "N" | 080H
            DB      "RETUR", "N" | 080H
            DB      "RESTOR", "E" | 080H
            DB      "RESUM", "E" | 080H
            DB      "LIS", "T" | 080H
            DB      080H
            DB      "DELET", "E" | 080H
            DB      "RENU", "M" | 080H
            DB      "AUT", "O" | 080H
            DB      "EDI", "T" | 080H
L4193:      DB      "FO", "R" | 080H
            DB      "NEX", "T" | 080H
            DB      "PRIN", "T" | 080H
            DB      080H
            DB      "INPU", "T" | 080H
            DB      080H
            DB      "I", "F" | 080H
            DB      "DAT", "A" | 080H
            DB      "REA", "D" | 080H
            DB      "DI", "M" | 080H
            DB      "RE", "M" | 080H
            DB      "EN", "D" | 080H
            DB      "STO", "P" | 080H
            DB      "CON", "T" | 080H
            DB      "CL", "S" | 080H
            DB      080H
            DB      "O", "N" | 080H
            DB      "LE", "T" | 080H
            DB      "NE", "W" | 080H
            DB      "POK", "E" | 080H
            DB      "OF", "F" | 080H
            DB      "MOD", "E" | 080H
            DB      "SKI", "P" | 080H
            DB      "PLO", "T" | 080H
            DB      "LIN", "E" | 080H
            DB      "RLIN", "E" | 080H
            DB      "MOV", "E" | 080H
            DB      "RMOV", "E" | 080H
            DB      "TRO", "N" | 080H
            DB      "TROF", "F" | 080H
            DB      "INP", "@" | 080H
            DB      "DEFAUL", "T" | 080H
            DB      "GE", "T" | 080H
            DB      "PCOLO", "R" | 080H
            DB      "PHOM", "E" | 080H
            DB      "HSE", "T" | 080H
            DB      "GPRIN", "T" | 080H
            DB      "KE", "Y" | 080H
            DB      "AXI", "S" | 080H
            DB      "LOA", "D" | 080H
            DB      "SAV", "E" | 080H
            DB      "MERG", "E" | 080H
            DB      "CHAI", "N" | 080H
            DB      "CONSOL", "E" | 080H
            DB      "SEARC", "H" | 080H
            DB      "OUT", "@" | 080H
            DB      "CIRCL", "E" | 080H
            DB      "TES", "T" | 080H
            DB      "PAG", "E" | 080H
            DB      "WAI", "T" | 080H
            DB      "SWA", "P" | 080H
            DB      080H
            DB      "ERRO", "R" | 080H
            DB      "ELS", "E" | 080H
            DB      "US", "R" | 080H
            DB      "BY", "E" | 080H
            DB      080H
            DB      080H
            DB      "DE", "F" | 080H
            DB      080H
            DB      080H
            DB      "LABE", "L" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      "WOPE", "N" | 080H
            DB      "CLOS", "E" | 080H
            DB      "ROPE", "N" | 080H
            DB      "XOPE", "N" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      "DI", "R" | 080H
            DB      080H
            DB      080H
            DB      "RENAM", "E" | 080H
            DB      "KIL", "L" | 080H
            DB      "LOC", "K" | 080H
            DB      "UNLOC", "K" | 080H
            DB      "INI", "T" | 080H
            DB      080H
            DB      080H
            DB      "RMO", "V" | 080H
            DB      "T", "O" | 080H
            DB      "STE", "P" | 080H
            DB      "THE", "N" | 080H
            DB      "USIN", "G" | 080H
            DB      080H
            DB      080H
            DB      "TA", "B" | 080H
            DB      "SP", "C" | 080H
            DB      080H
            DB      080H
            DB      ".XO", "R" | 080H
            DB      ".O", "R" | 080H
            DB      ".AN", "D" | 080H
            DB      ".NO", "T" | 080H
            DB      ">", "<" | 080H
            DB      "<", ">" | 080H
            DB      "=", "<" | 080H
            DB      "<", "=" | 080H
            DB      "=", ">" | 080H
            DB      ">", "=" | 080H
            DB      "=" | 080H
            DB      ">" | 080H
            DB      "<" | 080H
            DB      "+" | 080H
            DB      "-" | 080H
            DB      080H
            DB      080H
            DB      "/" | 080H
            DB      "*" | 080H
            DB      "^" | 080H
            DB      0FFH
EXTWORDTBL: DB      080H ; BASIC extended keyword text table
            DB      "SE", "T" | 080H
            DB      "RESE", "T" | 080H
            DB      "COLO", "R" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "BEE", "P" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "MUSI", "C" | 080H
            DB      "TEMP", "O" | 080H
            DB      "CURSO", "R" | 080H
            DB      "VERIF", "Y" | 080H
            DB      "CL", "R" | 080H
L433E:      DB      "LIMI", "T" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "BOO", "T" | 080H
            DB      0FFH
FUNCWORDTBL:DB      "IN", "T" | 080H ; BASIC function keyword text table
            DB      "AB", "S" | 080H
            DB      "SI", "N" | 080H
            DB      "CO", "S" | 080H
            DB      "TA", "N" | 080H
            DB      "L", "N" | 080H
            DB      "EX", "P" | 080H
            DB      "SQ", "R" | 080H
            DB      "RN", "D" | 080H
            DB      "PEE", "K" | 080H
            DB      "AT", "N" | 080H
            DB      "SG", "N" | 080H
            DB      "LO", "G" | 080H
            DB      "FRA", "C" | 080H
            DB      "PA", "I" | 080H
            DB      "RA", "D" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "JO", "Y" | 080H
            DB      080H
            DB      "CHR", "$" | 080H
            DB      "STR", "$" | 080H
            DB      "HEX", "$" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "SPACE", "$" | 080H
            DB      080H
            DB      080H
            DB      "AS", "C" | 080H
            DB      "LE", "N" | 080H
            DB      "VA", "L" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "ER", "N" | 080H
            DB      "ER", "L" | 080H
            DB      "SIZ", "E" | 080H
            DB      "CSR", "H" | 080H
            DB      "CSR", "V" | 080H
            DB      080H
            DB      080H
            DB      "LEFT", "$" | 080H
            DB      "RIGHT", "$" | 080H
            DB      "MID", "$" | 080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      080H
            DB      "TI", "$" | 080H
            DB      080H
            DB      "EO", "F" | 080H
            DB      "F", "N" | 080H
            DB      0FFH
L43EC:      DW      L5147               ; Keyword dispatch address table
            DW      L50C2
            DW      L4AEC
            DW      L47FF
            DW      L5091
            DW      L5828
            DW      L4C38
            DW      052C2H
            DW      L4AEC
            DW      L4999
            DW      L4A38
            DW      L485E
            DW      L48B9
            DW      L4D8E
            DW      L4E25
            DW      L561E
            DW      L4AEC
            DW      L575B
            DW      L4AEC
            DW      L515A
            DW      L4F8A
            DW      L5754
            DW      L76AC
            DW      L4F8A
            DW      L484D
            DW      L4BEA
            DW      L4C1B
            DW      L5537
            DW      L4AEC
            DW      L50EA
            DW      L4C99
            DW      L4922
            DW      L5024
            DW      L4AEC
            DW      L6A24
            DW      L6A87
            DW      L6E22
            DW      L6AD4
            DW      06AD7H
            DW      06ADAH
            DW      06ADDH
            DW      L4959
            DW      L496C
            DW      L4FAB
            DW      L58CA
            DW      L4FEF
            DW      L6B3F
            DW      L6B71
            DW      L6B7B
            DW      L6B85
            DW      L53A9
            DW      L6BEA
            DW      L5986
            DW      L5C88
            DW      L59EA
            DW      L59D5
            DW      L5260
            DW      L52C0
            DW      L4F96
            DW      L6C2A
            DW      L6DF8
            DW      L6E03
            DW      L4F7A
            DW      L5D65
            DW      L4AEC
            DW      L4B7A
            DW      L517C
            DW      L4F30
            DW      L5258
            DW      L4AEC
            DW      L4AEC
            DW      L5456
            DW      L4AEC
            DW      L4AEC
            DW      L4F8A
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      058E2H
            DW      L5913
            DW      L58DF
            DW      058E5H
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L5CE3
            DW      L4AEC
            DW      L4AEC
            DW      L5D0A
            DW      05914H
            DW      05CD7H
            DW      L5CD5
            DW      L58D0
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
L44AC:      DW      L4AEC
            DW      L51EE
            DW      051EFH
            DW      L54A6
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L5255
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L54FD
            DW      L551F
            DW      L4FC7
            DW      L5C71
            DW      L4928
            DW      L504D
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L52BA
L450A:      DW      L7E8B
            DW      L7E86
            DW      L7FD2
            DW      L7FC2
            DW      L80B1
            DW      L8320
            DW      L81FC
            DW      L7EE8
            DW      L81B7
            DW      L81A3
            DW      L7F06
            DW      L8169
            DW      L8314
            DW      L608D
            DW      L8181
            DW      L817C
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L792C
            DW      L4AEC
            DW      L4AEC
            DW      L7237
            DW      L71F6
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L7219
            DW      L4AEC
            DW      L4AEC
            DW      L7272
            DW      L727C
            DW      L7284
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L7180
            DW      L718E
            DW      L7164
            DW      L7176
            DW      L717B
            DW      L4AEC
            DW      L4AEC
            DW      L7296
            DW      L72B3
            DW      L72D0
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L7326
            DW      L4AEC
            DW      L7245
            DW      L789F
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
            DW      L4AEC
L45AA:      CALL    TEST1
            DB      00CH
            JR      NZ,L45B6            ; (+006H)
            CALL    LDDEMI
            OR      0FFH
            RET     

L45B6:      INC     HL
            CP      00BH
            JR      NZ,L45DA            ; (+01fH)
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            LD      A,E
            OR      D
            RET     Z
            EX      DE,HL
            CALL    L4609
            JP      C,04B0DH
            EX      DE,HL
            DEC     HL
            LD      (HL),D
            DEC     HL
            LD      (HL),E
            DEC     HL
            LD      (HL),00CH
            INC     HL
            INC     HL
            INC     HL
            OR      0FFH
            LD      (L473D),A
            RET     

L45DA:      CP      022H
            JP      NZ,L4AEC
            LD      (04650H),HL
            LD      B,000H
L45E4:      LD      A,(HL)
            OR      A
            JR      Z,L45F0             ; (+008H)
            INC     HL
            CP      022H
            JR      Z,L45F0             ; (+003H)
            INC     B
            JR      L45E4               ; (-00cH)

L45F0:      LD      A,B
            OR      A
            JP      Z,L4AEC
            LD      (0464EH),A
            EX      DE,HL
            CALL    L4603
            JP      C,04B0DH
            EX      DE,HL
            OR      0FFH
            RET     

L4603:      PUSH    BC
            LD      BC,L463C
            JR      L460D               ; (+004H)

L4609:      PUSH    BC
            LD      BC,L4630
L460D:      LD      (04623H),BC
            PUSH    DE
            EX      DE,HL
            LD      HL,(TEXTST)
L4616:      LD      C,(HL)
            INC     HL
            LD      B,(HL)
            LD      A,B
            OR      C
            SCF     
            JR      Z,L462D             ; (+00fH)
            DEC     HL
            PUSH    HL
            ADD     HL,BC
            EX      (SP),HL
            CALL    L0000
            JR      C,L462C             ; (+005H)
            JR      Z,L462C             ; (+003H)
            POP     HL
            JR      L4616               ; (-016H)

L462C:      POP     DE
L462D:      POP     DE
            POP     BC
            RET     

L4630:      INC     HL
            INC     HL
            INC     HL
            LD      A,D
            CP      (HL)
            RET     NZ
            DEC     HL
            LD      A,E
            CP      (HL)
            DEC     HL
            DEC     HL
            RET     

L463C:      PUSH    HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            CALL    TEST1
            DB      0CAH
            JR      NZ,L4660            ; (+019H)
            CALL    TEST1
            DB      022H
            JR      NZ,L4660            ; (+013H)
            LD      B,000H
            LD      DE,L0000
L4652:      LD      A,(DE)
            CP      (HL)
            JR      NZ,L4660            ; (+00aH)
            INC     HL
            INC     DE
            DJNZ    L4652               ; (-008H)
            LD      A,(HL)
            CP      022H
            JR      Z,L4660             ; (+001H)
            OR      A
L4660:      SCF     
            CCF     
            POP     HL
            RET     

L4664:      LD      DE,L0000
            LD      BC,0FFFFH
            CALL    L46A6
            RET     Z
            CP      02DH
            JR      Z,L468E             ; (+01cH)
            CP      02EH
            LD      DE,(0847FH)
            JR      Z,L4681             ; (+007H)
            CALL    TESTX
            DB      00BH
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
L4681:      INC     HL
            CALL    L46A6
            JR      Z,L468B             ; (+004H)
            CP      02DH
            JR      Z,L468E             ; (+003H)
L468B:      LD      C,E
            LD      B,D
            RET     

L468E:      INC     HL
            CALL    L46A6
            RET     Z
            CP      02EH
            JR      NZ,L469D            ; (+006H)
            LD      BC,(0847FH)
            INC     HL
            RET     

L469D:      CALL    TESTX
            DB      00BH
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            INC     HL
            RET     

L46A6:      CALL    L413E
            RET     Z
            CP      02CH
            RET     

L46AD:      CALL    SKPDE
            LD      A,0FFH
            LD      (L473D),A
            LD      HL,L4703
            JR      L46CC               ; (+012H)

L46BA:      CALL    SKPDE
            CALL    L4836
L46C0:      LD      A,(L473D)
            OR      A
            RET     Z
            XOR     A
            LD      (L473D),A
            LD      HL,L46E9
L46CC:      LD      (046E7H),HL
            LD      HL,(TEXTST)
            DEC     HL
L46D3:      INC     HL
            LD      A,(HL)
            INC     HL
            OR      (HL)
            RET     Z
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      (04732H),DE
L46E0:      CALL    L51A2
            OR      A
            JR      Z,L46D3             ; (-013H)
            JP      L0000

L46E9:      CP      00CH
            JR      NZ,L46E0            ; (-00dH)
            DEC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            PUSH    HL
            EX      DE,HL
            INC     HL
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            POP     HL
            LD      (HL),D
            DEC     HL
            LD      (HL),E
            DEC     HL
            LD      (HL),00BH
            INC     HL
            INC     HL
            JR      L46E0               ; (-023H)

L4703:      CP      00BH
            JR      NZ,L46E0            ; (-027H)
            DEC     HL
            PUSH    HL
            CALL    INDRCT
            LD      E,L
            LD      D,H
            LD      A,L
            OR      H
            JR      Z,L4739             ; (+027H)
            CALL    L4609
            JR      C,L4722             ; (+00bH)
            EX      DE,HL
            POP     HL
            DEC     HL
            LD      (HL),00CH
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            JR      L46E0               ; (-042H)

L4722:      PUSH    DE
            LD      A,010H
            RST     018H
            DB      01BH
            LD      A,020H
            RST     018H
            DB      003H
            POP     HL
            CALL    L5E66
            RST     018H
            DB      005H
            LD      HL,L0000
            CALL    L4B58
            RST     018H
            DB      002H
L4739:      POP     HL
            INC     HL
            JR      L46E0               ; (-05dH)

L473D:      NOP     
L473E:      RST     018H
            DB      028H
            CALL    L46BA
            CALL    L6A11
            LD      A,B
            OR      C
            JP      Z,L4059
            LD      (0847FH),BC
            LD      A,(DE)
            CP      020H
            JR      NZ,L4755            ; (+001H)
            INC     DE
L4755:      PUSH    AF
            LD      HL,L84E7
            CALL    L66EF
            PUSH    HL
            LD      HL,(L847F)
            LD      E,L
            LD      D,H
            CALL    L49B9
            POP     HL
            POP     AF
            OR      A
            JR      Z,L477C             ; (+012H)
            LD      DE,L84E7
            OR      A
            SBC     HL,DE
            LD      DE,00005H
            ADD     HL,DE
            LD      B,H
            LD      C,L
            LD      HL,L84E7
            CALL    L4794
L477C:      LD      A,(L48B0)
            OR      A
            JP      Z,L4059
            LD      DE,(08481H)
            LD      HL,(L847F)
            ADD     HL,DE
            LD      (L847F),HL
            JP      NC,L4059
            JP      L4081

L4794:      LD      (047F6H),HL
            PUSH    BC
            LD      BC,(0847FH)
            LD      HL,(TEXTST)
            JR      L47A5               ; (+004H)

L47A1:      CALL    LDDEMD
            ADD     HL,DE
L47A5:      CALL    LDDEMD
            LD      A,D
            OR      E
            JR      Z,L47B9             ; (+00dH)
            INC     HL
            INC     HL
            CALL    LDDEMD
            EX      DE,HL
            SBC     HL,BC
            DEC     DE
            DEC     DE
            EX      DE,HL
            JR      C,L47A1             ; (-018H)
L47B9:      POP     DE
            PUSH    HL
            PUSH    DE
            LD      HL,00028H
            ADD     HL,DE
            LD      BC,(VARED)
            LD      (TMPEND),BC
            ADD     HL,BC
            JP      C,L4B2C
            EX      DE,HL
            CALL    L74E8
            POP     DE
            RST     018H
            DB      018H
            POP     HL
            PUSH    DE
            PUSH    BC
            EX      (SP),HL
            POP     BC
            PUSH    HL
            OR      A
            SBC     HL,BC
            LD      B,H
            LD      C,L
            POP     HL
            LD      DE,(VARED)
            INC     BC
            LDDR    
            INC     HL
            POP     BC
            LD      (HL),C
            INC     HL
            LD      (HL),B
            INC     HL
            LD      DE,(0847FH)
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            EX      DE,HL
            LD      HL,L84E7
            DEC     BC
            DEC     BC
            DEC     BC
            DEC     BC
            LDIR    
            RET     

L47FF:      JR      Z,L480A             ; (+009H)
            CALL    L5176
            JP      Z,L5147
            JP      L5AED

L480A:      CALL    L4928
L480D:      CALL    L4821
            LD      DE,(TEXTST)
            LD      SP,(INTFAC)
            LD      HL,0FFFFH
            PUSH    HL
            PUSH    HL
            EX      DE,HL
            JP      L40E4

L4821:      PUSH    HL
            PUSH    DE
            CALL    L4836
            XOR     A
            LD      (L48B0),A
            LD      HL,0000AH
            LD      (L847F),HL
            LD      (L8481),HL
            POP     DE
            POP     HL
            RET     

L4836:      CALL    L5825
            LD      HL,L0000
            LD      (L8487),HL
            XOR     A
            LD      (L4B78),A
            LD      (L8486),A
            LD      (L8485),A
            LD      (L849A),A
            RET     

L484D:      LD      A,(L849A)
            OR      A
            JP      NZ,L5DCB
            RST     018H
            DB      028H
            XOR     A
            LD      (L4B78),A
            POP     BC
            JP      L4050

L485E:      CALL    L5C08
            LD      DE,0000AH
            LD      BC,0000AH
            JR      Z,L4897             ; (+02eH)
            CP      02CH
            JR      NZ,L4878            ; (+00bH)
            INC     HL
            CALL    L6E5F
            LD      B,D
            LD      C,E
            LD      DE,0000AH
            JR      L4897               ; (+01fH)

L4878:      CP      02EH
            LD      DE,(0847FH)
            JR      Z,L4889             ; (+009H)
            CP      00BH
            JP      NZ,L4AEC
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
L4889:      INC     HL
            CALL    TEST1
            DB      02CH
            JR      NZ,L4897            ; (+007H)
            PUSH    DE
            CALL    L6E5F
            LD      C,E
            LD      B,D
            POP     DE
L4897:      CALL    05E48H
            LD      A,C
            OR      B
            JP      Z,04AF2H
            LD      (0847FH),DE
            LD      (08481H),BC
            LD      A,001H
            LD      (L48B0),A
            POP     AF
            JP      L4059

L48B0:      NOP     
L48B1:      LD      A,(L48B0)
            OR      A
            RET     Z
            XOR     A
            JR      L48BF               ; (+006H)

L48B9:      CALL    L48BF
            JP      L4059

L48BF:      LD      DE,(0847FH)
            CALL    NZ,L4664
            PUSH    DE
            EX      DE,HL
            CALL    L4609
            POP     DE
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            JR      NC,L48D5            ; (+003H)
            LD      HL,L00F9
L48D5:      EX      DE,HL
            PUSH    DE
            LD      (L847F),HL
            CALL    L5E66
            RST     018H
            DB      005H
            LD      A,020H
            RST     018H
            DB      003H
            POP     HL
            LD      DE,KEYBUF
            PUSH    DE
            CALL    L68E0
            POP     DE
            LD      B,000H
L48EE:      LD      A,(DE)
            OR      A
            JR      Z,L48F8             ; (+006H)
            INC     B
            RST     018H
            DB      004H
            INC     DE
            JR      L48EE               ; (-00aH)

L48F8:      LD      A,B
            OR      A
            RET     Z
            LD      A,014H
            RST     018H
            DB      003H
            DJNZ    048FCH               ; (-005H)
            RET     

L4902:      PUSH    DE
            LD      DE,0FFF0H
            ADD     HL,DE
            POP     DE
            LD      (MEMLMT),HL
            DEC     H
            LD      (INTFAC),HL
            XOR     A
            LD      (L849A),A
            RET     

L4914:      LD      HL,(TEXTST)
            CALL    L4952
            LD      (TEXTED),HL
            CALL    L4821
            JR      L4928               ; (+006H)

L4922:      CALL    L4914
            JP      L404A

L4928:      PUSH    HL
            CALL    L4939
            POP     HL
            RST     018H
            DB      028H
            RET     

L4930:      LD      HL,(TEXTST)
            CALL    L4952
            LD      (TEXTED),HL
L4939:      LD      HL,(TEXTED)
            LD      (HL),000H
            INC     HL
            LD      (VARST),HL
            LD      (HL),000H
            INC     HL
            LD      (STRST),HL
            CALL    L4952
            LD      (VARED),HL
            LD      (TMPEND),HL
            RET     

L4952:      LD      (HL),000H
            INC     HL
            LD      (HL),000H
            INC     HL
            RET     

L4959:      CALL    L413E
            LD      A,001H
            JR      Z,L496D             ; (+00dH)
            CALL    TESTX
            DB      FDPD
            CALL    TESTX
            DB      050H
            LD      A,002H
            JR      L496D               ; (+001H)

L496C:      XOR     A
L496D:      LD      (04972H),A
            RET     

L4971:      LD      A,000H
            OR      A
            RET     Z
            DEC     A
            LD      (FILOUT),A
            JR      Z,L4982             ; (+007H)
            LD      A,(PNMODE)
            CP      002H
            JR      Z,L4994             ; (+012H)
L4982:      PUSH    HL
            LD      A,05BH
            RST     018H
            DB      009H
            LD      HL,(L8483)
            CALL    L5E66
            RST     018H
            DB      00BH
            LD      A,05DH
            RST     018H
            DB      009H
            POP     HL
L4994:      XOR     A
            LD      (FILOUT),A
            RET     

L4999:      CALL    L46A6
            JP      Z,L4AEC
            CALL    L5176
            JR      Z,L49AD             ; (+009H)
            CP      02DH
            JR      Z,L49AD             ; (+005H)
            CP      02EH
            JP      NZ,L5D02
L49AD:      CALL    L4664
            EX      DE,HL
            LD      E,C
            LD      D,B
            CALL    L49B9
            JP      L4050

L49B9:      PUSH    AF
            PUSH    BC
            PUSH    HL
            PUSH    DE
            CALL    L46BA
            LD      C,L
            LD      B,H
            LD      HL,(TEXTST)
L49C5:      CALL    LDDEMI
            LD      A,E
            OR      D
            JR      NZ,L49D4            ; (+008H)
L49CC:      POP     DE
            POP     HL
            POP     BC
            POP     AF
            RET     

L49D1:      POP     DE
            JR      L49CC               ; (-008H)

L49D4:      EX      DE,HL
            ADD     HL,DE
            DEC     HL
            DEC     HL
            EX      DE,HL
            PUSH    DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            EX      DE,HL
            LD      (049EEH),HL
            SBC     HL,BC
            POP     HL
            JR      C,L49C5             ; (-021H)
            DB      01BH
            DEC     DE
            DEC     DE
            POP     BC
            PUSH    BC
            PUSH    DE
            PUSH    HL
            LD      HL,L0000
            SBC     HL,BC
            POP     HL
            JR      Z,L4A13             ; (+01eH)
            JR      NC,L49D1            ; (-026H)
L49F7:      CALL    LDDEMI
            LD      A,D
            OR      E
            JR      Z,L4A11             ; (+013H)
            EX      DE,HL
            ADD     HL,DE
            EX      DE,HL
            DEC     DE
            DEC     DE
            PUSH    DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            EX      DE,HL
            SBC     HL,BC
            POP     HL
            JR      C,L49F7             ; (-016H)
            JR      Z,L4A13             ; (+004H)
            EX      DE,HL
            DEC     HL
L4A11:      DEC     HL
            DEC     HL
L4A13:      POP     DE
            PUSH    DE
            PUSH    HL
            OR      A
            EX      DE,HL
            SBC     HL,DE
            EX      DE,HL
            LD      BC,(VARED)
            RST     018H
            DB      018H
            POP     DE
            LD      H,B
            LD      L,C
            OR      A
            SBC     HL,DE
            LD      B,H
            LD      C,L
            EX      DE,HL
            POP     DE
            LDIR    
            JR      L49CC               ; (-063H)

L4A2F:      CALL    L6E5F
            LD      A,D
            OR      E
            RET     NZ
            JP      04AF2H

L4A38:      CALL    L5C08
            LD      DE,0000AH
            LD      (04AB7H),DE
            LD      (04AC9H),DE
            LD      E,000H
            LD      (04AA6H),DE
            JR      Z,L4A81             ; (+033H)
            CP      02CH
            JR      Z,L4A62             ; (+010H)
            CALL    L4A2F
            LD      (04AB7H),DE
            CALL    L413E
            JR      Z,L4A81             ; (+023H)
            CALL    L5E70
            DEC     HL
L4A62:      CALL    INCHLF
            CP      02CH
            JR      Z,L4A79             ; (+010H)
            CALL    L4A2F
            LD      (04AA6H),DE
            CALL    L413E
            JR      Z,L4A81             ; (+00cH)
            CALL    L5E70
            DEC     HL
L4A79:      INC     HL
            CALL    L4A2F
            LD      (04AC9H),DE
L4A81:      PUSH    HL
            LD      HL,(04AA6H)
            EX      DE,HL
            LD      HL,(04AB7H)
            OR      A
            SBC     HL,DE
            JP      C,04AF2H
            CALL    L46AD
            LD      HL,(TEXTST)
L4A95:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      A,D
            OR      E
            JR      Z,L4AD3             ; (+037H)
            EX      DE,HL
            ADD     HL,DE
            DEC     HL
            EX      DE,HL
            INC     HL
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            PUSH    HL
            LD      HL,L0000
            OR      A
            SBC     HL,BC
            POP     HL
            JR      Z,L4AB3             ; (+005H)
            JR      C,L4AB3             ; (+003H)
            EX      DE,HL
            JR      L4A95               ; (-01eH)

L4AB3:      DEC     HL
            DEC     HL
            DEC     HL
            LD      BC,L0000
L4AB9:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      A,D
            OR      E
            JR      Z,L4AD3             ; (+013H)
            EX      DE,HL
            ADD     HL,DE
            DEC     HL
            EX      DE,HL
            INC     HL
            LD      (HL),C
            INC     HL
            LD      (HL),B
            LD      HL,L0000
            ADD     HL,BC
            JR      C,L4AD8             ; (+00aH)
            LD      C,L
            LD      B,H
            EX      DE,HL
            JR      L4AB9               ; (-01aH)

L4AD3:      CALL    L46C0
            POP     HL
            RET     

L4AD8:      LD      HL,0000AH
            LD      (04AC9H),HL
            LD      (04AB7H),HL
            LD      L,000H
            LD      (04AA6H),HL
            CALL    L4A81
            JP      04AF2H

L4AEC:      LD      A,001H
            LD      HL,0023EH
            LD      HL,L033E
            LD      HL,0043EH
            LD      HL,L053E
            LD      HL,0063EH
            LD      HL,0073EH
            LD      HL,L083E
            LD      HL,L0D3E
            LD      HL,L0E3E
            LD      HL,L0F3E
            LD      HL,L103E
            LD      HL,L113E
            LD      HL,L123E
            LD      HL,L133E
            LD      HL,0143EH
            LD      HL,0153EH
            LD      HL,L183E
            LD      HL,L193E
            LD      HL,L3A3E
            LD      HL,L403E
            JR      L4B3A               ; (+00eH)

L4B2C:      LD      A,006H
L4B2E:      LD      SP,(INTFAC)
            LD      HL,0FFFFH
            PUSH    HL
            LD      (08496H),SP
L4B3A:      JR      L4B55               ; (+019H)

L4B3C:      LD      HL,(_LPT)
            LD      IX,(_CRT)
            LD      (ZEQT),HL
            XOR     A
            LD      (01051H),A
            LD      A,0C4H
            LD      HL,0BB3EH
            LD      HL,0BC3EH
            LD      HL,0BD3EH
L4B55:      JP      L0182

L4B58:      LD      A,L
            OR      H
            RET     Z
            LD      DE,L4B66
            RST     018H
            DB      005H
            CALL    L5E66
            RST     018H
            DB      005H
            RET     

L4B66:      JR      NZ,04BB1H            ; (+049H)
            LD      C,(HL)
            JR      NZ,L4B6B            ; (+000H)
L4B6B:      LD      B,D
            DEC     B
            LD      D,D
            LD      B,L
            LD      B,C
            LD      C,E
            NOP     
L4B72:      LD      D,D
            DEC     B
            LD      B,L
            LD      B,C
            LD      B,H
            LD      E,C
L4B78:      NOP     
            NOP     
L4B7A:      CALL    L6E4B
            DEC     A
            CP      046H
            JR      C,L4B84             ; (+002H)
            LD      A,044H
L4B84:      INC     A
L4B85:      LD      SP,(08496H)
            PUSH    AF
            RST     018H
            DB      043H
            CALL    L5C44
            POP     AF
            OR      A
            JR      Z,L4BE5             ; (+052H)
            CP      080H
            JR      Z,L4BE3             ; (+04cH)
            LD      C,A
            LD      HL,L0000
            LD      (L7928),HL
            CALL    L4146
            LD      A,C
            JR      Z,L4BCE             ; (+02aH)
            LD      HL,(L8483)
            LD      (L8487),HL
            LD      (L847F),HL
            LD      HL,(L847D)
            LD      (L8489),HL
            LD      HL,(L8498)
            LD      (L848B),HL
            AND     07FH
            LD      (L8485),A
            LD      A,(L8486)
            INC     A
            CP      002H
            JR      Z,L4BD9             ; (+013H)
            XOR     A
            LD      (L4B78),A
            LD      (L849A),A
            LD      A,C
L4BCE:      RST     018H
            DB      01BH
L4BD0:      LD      HL,(L8483)
            CALL    L4B58
            JP      L4050

L4BD9:      LD      (L8486),A
            LD      HL,(0848DH)
            PUSH    HL
            JP      L40E4

L4BE3:      LD      A,02EH
L4BE5:      LD      HL,(L8498)
            JR      L4BED               ; (+003H)

L4BEA:      LD      A,02EH
            POP     DE
L4BED:      PUSH    AF
            PUSH    HL
            RST     018H
            DB      002H
            RST     018H
            DB      020H
            LD      DE,L4B6B
            RST     018H
            DB      005H
            POP     HL
            CALL    L4146
            JR      Z,L4C17             ; (+019H)
            LD      (04C33H),HL
            LD      HL,(L847D)
            LD      (04C2DH),HL
            LD      HL,(L8483)
            LD      (04C27H),HL
            LD      (L847F),HL
            POP     AF
            LD      (L4B78),A
            JP      L4BD0

L4C17:      POP     AF
            JP      L4050

L4C1B:      POP     DE
            LD      HL,L4B78
            LD      A,(HL)
            OR      A
            JP      Z,04B10H
            LD      (HL),000H
            LD      HL,L0000
            LD      (L8483),HL
            LD      HL,L0000
            LD      (L847D),HL
            LD      HL,L0000
            JP      L40AF

L4C38:      LD      A,(L8486)
            CP      002H
            JP      C,04B1CH
            DEC     A
            LD      (L8486),A
            CALL    L413E
            EX      DE,HL
            LD      HL,(L8487)
            LD      (L8483),HL
            LD      HL,(L8489)
            LD      (L847D),HL
            LD      HL,(L848B)
            JR      NZ,L4C5D            ; (+004H)
            POP     BC
            JP      L40B3

L4C5D:      CP      08EH
            JP      Z,L4F8A
            EX      DE,HL
            JP      L5147

L4C66:      CALL    TEST1
            DB      080H
            JR      Z,L4C74             ; (+008H)
            CALL    TESTX
            DB      082H
            CALL    TESTX
            DB      LSE0
L4C74:      CALL    L45AA
            JR      Z,L4C83             ; (+00aH)
            LD      (0848DH),DE
            LD      A,001H
L4C7F:      LD      (L8486),A
            RET     

L4C83:      LD      A,(L8486)
            DEC     A
            JR      Z,L4C7F             ; (-00aH)
            XOR     A
            LD      (L8486),A
            LD      HL,(L8487)
            LD      (L8483),HL
            LD      A,(L8485)
            JP      L4B85

L4C99:      CALL    TEST1
            DB      0FFH
            JP      Z,L4CCB
            CALL    L7436
            PUSH    BC
            PUSH    BC
            PUSH    AF
            CALL    TESTX
            DB      QDPC
            CALL    L6E8A
            POP     BC
            LD      A,(L84CE)
            CP      B
            JP      NZ,04AF5H
            EX      (SP),HL
            EX      DE,HL
            CP      005H
            JR      Z,L4CC3             ; (+008H)
            PUSH    BC
            CALL    L4CD6
            POP     AF
            POP     HL
            POP     BC
            RET     

L4CC3:      LD      C,A
            LD      B,000H
            LDIR    
            POP     HL
            POP     BC
            RET     

L4CCB:      CALL    TESTX
            DB      0C4H
            JP      L73C2

L4CD2:      PUSH    DE
            EX      DE,HL
            JR      L4CDA               ; (+004H)

L4CD6:      PUSH    DE
            CALL    L6E7E
L4CDA:      LD      HL,KEYBM1
            LD      (HL),A
            LD      B,A
            LD      C,A
            INC     HL
            CALL    LDHLDE
            POP     HL
            LD      A,(HL)
            CP      C
            JR      Z,L4D2B             ; (+042H)
            PUSH    HL
            OR      A
            CALL    NZ,L4D3B
            POP     HL
            LD      A,(KEYBM1)
            OR      A
            JR      Z,L4D28             ; (+033H)
            PUSH    HL
            LD      BC,(VARST)
            SBC     HL,BC
            EX      DE,HL
            LD      HL,(VARED)
            DEC     HL
            DEC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            LD      BC,(STRST)
            OR      A
            POP     DE
            PUSH    HL
            SBC     HL,BC
            EX      DE,HL
            LD      (HL),A
            LD      B,A
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            POP     HL
            LD      DE,KEYBM1
            INC     DE
            CALL    L74DC
            CALL    L4952
            LD      (TMPEND),HL
            LD      (VARED),HL
            RET     

L4D28:      LD      (HL),000H
            RET     

L4D2B:      INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            ADD     HL,DE
            LD      DE,KEYBM1
            LD      B,C
            INC     DE
            JP      L74DC

L4D3B:      LD      C,(HL)
            LD      B,000H
L4D3E:      INC     BC
            INC     BC
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            ADD     HL,DE
            DEC     HL
            DEC     HL
            LD      E,L
            LD      D,H
            ADD     HL,BC
            PUSH    BC
            PUSH    DE
            EX      DE,HL
            LD      HL,(VARED)
            OR      A
            SBC     HL,DE
            LD      C,L
            LD      B,H
            EX      DE,HL
            POP     DE
            PUSH    DE
            JR      Z,L4D5F             ; (+002H)
            LDIR    
L4D5F:      POP     DE
            POP     BC
            LD      HL,(VARED)
            OR      A
            SBC     HL,BC
            LD      (VARED),HL
            EX      DE,HL
L4D6B:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      A,D
            OR      E
            RET     Z
            LD      HL,(VARST)
            ADD     HL,DE
            LD      A,(HL)
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            OR      A
            EX      DE,HL
            SBC     HL,BC
            EX      DE,HL
            LD      (HL),D
            DEC     HL
            LD      (HL),E
            PUSH    BC
            LD      C,A
            LD      B,000H
            LD      HL,(STRST)
            ADD     HL,DE
            ADD     HL,BC
            POP     BC
            JR      L4D6B               ; (-023H)

L4D8E:      POP     BC
            LD      (04E23H),BC
            CALL    L4C99
            LD      IX,L0000
            ADD     IX,SP
            LD      (04E05H),HL
            CALL    L4EFE
            LD      (04E01H),BC
L4DA6:      LD      E,(IX+000H)
            LD      D,(IX+001H)
            LD      HL,0FF12H
            OR      A
            SBC     HL,DE
            JR      NZ,L4DCE            ; (+01aH)
            LD      E,(IX+006H)
            LD      D,(IX+007H)
            EX      DE,HL
            OR      A
            SBC     HL,BC
            JR      Z,L4DC7             ; (+007H)
            LD      DE,L0012
            ADD     IX,DE
            JR      L4DA6               ; (-021H)

L4DC7:      LD      DE,00012H

            ADD     IX,DE
            LD      SP,IX
L4DCE:      LD      HL,(04E05H)
            CALL    TESTX
            DB      LSE0
            CALL    L6E8A
            PUSH    AF
            PUSH    HL
            EX      DE,HL
            LD      DE,L4F0A
            CALL    L5E93
            POP     HL
            POP     AF
            CP      LSE1
            LD      DE,L6112
            JR      NZ,L4DEE            ; (+004H)
            INC     HL
            CALL    L6E8A
L4DEE:      LD      (04E05H),HL
            LD      HL,0FFF6H
            ADD     HL,SP
            LD      SP,HL
            EX      DE,HL
            CALL    L5E93
            LD      HL,L4F0A
            CALL    L5E93
            LD      HL,L0000
            PUSH    HL
            LD      HL,L0000
            PUSH    HL
            LD      HL,(L847D)
            PUSH    HL
            LD      HL,0FF12H
            PUSH    HL
            LD      HL,0FE00H
            ADD     HL,SP
            LD      DE,(TMPEND)
            SBC     HL,DE
            LD      A,00BH
            JP      C,L4B2E
            LD      HL,(04E05H)
            JP      L0000

L4E25:      LD      A,005H
            LD      (L84CE),A
            POP     BC
            LD      (04E9AH),BC
L4E2F:      PUSH    AF
            POP     BC
            LD      (04E05H),HL
            LD      IX,L0000
            ADD     IX,SP
            LD      (04E89H),IX
            LD      E,(IX+000H)
            LD      D,(IX+001H)
            LD      HL,0FF12H
            OR      A
            SBC     HL,DE
            JP      NZ,04B04H
            PUSH    BC
            POP     AF
            JR      NZ,L4EB0            ; (+05fH)
            LD      E,(IX+004H)
            LD      D,(IX+005H)
            EX      DE,HL
            LD      (04E97H),HL
            LD      E,(IX+006H)
            LD      D,(IX+007H)
            LD      HL,(VARST)
            ADD     HL,DE
            LD      BC,00008H
            ADD     IX,BC
            PUSH    IX
            POP     DE
            CALL    L7985
            INC     DE
            LD      A,(DE)
            LD      IX,(04E89H)
            LD      DE,0000DH
            ADD     IX,DE
            PUSH    IX
            POP     DE
            BIT     7,A
            JR      NZ,L4E83            ; (+001H)
            EX      DE,HL
L4E83:      CALL    L7A1C
            JR      C,L4E9C             ; (+014H)
            LD      SP,L0000
            LD      HL,00002H
            ADD     HL,SP
            LD      A,(HL)
            INC     HL
            LD      H,(HL)
            LD      L,A
            LD      (L847D),HL
            LD      HL,L0000
L4E99:      JP      L0000

L4E9C:      LD      DE,L0012
            LD      HL,(04E89H)
            ADD     HL,DE
            LD      SP,HL
            LD      HL,(04E05H)
            CALL    TEST1
            DB      02CH
            JR      NZ,L4E99            ; (-014H)
            LD      (04E05H),HL
L4EB0:      LD      IX,L0000
            ADD     IX,SP
            LD      (04E89H),IX
            LD      HL,(04E05H)
            CALL    TEST1
            DB      02CH
            JP      Z,L4E2F
            CALL    L7436
            LD      (04E05H),HL
            CALL    L4EFE
            LD      IX,(04E89H)
L4ED1:      LD      E,(IX+000H)
            LD      D,(IX+001H)
            LD      HL,0FF12H
            OR      A
            SBC     HL,DE
            JP      NZ,04B04H
            LD      L,(IX+006H)
            LD      H,(IX+007H)
            OR      A
            SBC     HL,BC
            LD      HL,(04E05H)
            JP      Z,L4E2F
            LD      DE,L0012
            ADD     IX,DE
            LD      (04E89H),IX
            LD      SP,(04E89H)
            JR      L4ED1               ; (-02dH)

L4EFE:      LD      H,B
            LD      L,C
            LD      BC,(VARST)
            OR      A
            SBC     HL,BC
            LD      B,H
            LD      C,L
            RET     

L4F0A:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
            NOP     
L4F13:      CALL    L51A2
            OR      A
            RET     NZ
            INC     HL
            PUSH    DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      A,D
            OR      E
            INC     HL
            LD      (04F12H),DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      (04F10H),DE
            POP     DE
            SCF     
            RET     Z
            JR      L4F13               ; (-01dH)

L4F30:      CALL    L5E7B
            CALL    L6E5F
            LD      (04F69H),DE
            CALL    TEST1
            DB      02CH
            JR      NZ,L4F5B            ; (+01bH)
            CALL    L6E8A
            CALL    L7916
            LD      (04F5EH),DE
            CALL    TEST1
            DB      02CH
            JR      NZ,L4F5B            ; (+00bH)
            CALL    L7436
            CALL    L7916
            LD      (04F71H),BC
            XOR     A
L4F5B:      PUSH    HL
            PUSH    AF
            LD      HL,L0000
            CALL    L6E7E
            LD      IX,L0182
            XOR     A
            CALL    L0000
            POP     AF
            JR      NZ,L4F76            ; (+008H)
            LD      A,B
            EX      DE,HL
            LD      DE,L0000
            CALL    L4CD2
L4F76:      POP     HL
            JP      L5E7F

L4F7A:      CALL    L6E5F
L4F7D:      LD      A,D
            OR      E
            RET     Z
            LD      B,000H
L4F82:      DJNZ    L4F82               ; (-002H)
            RST     018H
            DB      00EH
            RET     Z
            DEC     DE
            JR      L4F7D               ; (-00dH)

L4F8A:      DEC     HL
L4F8B:      CALL    L51A2
            OR      A
            SCF     
            RET     Z
            CP      03AH
            RET     Z
            JR      L4F8B               ; (-00bH)

L4F96:      CALL    L6E4B
            SUB     LSE0
            CP      007H
            JP      C,04AF2H
            CALL    L5E70
            PUSH    DE
            CALL    L6E4B
            POP     BC
            OUT     (C),A
            RET     

L4FAB:      CALL    L6E4B
            CALL    L5E70
            PUSH    DE
            CALL    L7436
            CP      005H
            JP      NZ,04AF5H
            EX      (SP),HL
            PUSH    BC
            EX      (SP),HL
            POP     BC
            IN      E,(C)
            LD      D,000H
            CALL    L5E99
            POP     HL
            RET     

L4FC7:      CALL    L4FCF
            LD      (00054H),DE
            RET     

L4FCF:      LD      B,018H
            LD      C,027H
            CALL    L4FDA
            JP      C,04AF2H
            RET     

L4FDA:      PUSH    BC
            CALL    L6E4B
            PUSH    AF
            CALL    L5E70
            CALL    L6E4B
            LD      D,E
            POP     AF
            LD      E,A
            POP     BC
            LD      A,C
            CP      E
            RET     C
            LD      A,B
            CP      D
            RET     

L4FEF:      CALL    L7436
            LD      (L84CE),A
            CP      005H
            JR      Z,L500E             ; (+015H)
            PUSH    HL
            PUSH    BC
            LD      A,0FFH
            RST     018H
            DB      00DH
            OR      A
            JR      Z,L5008             ; (+006H)
            LD      HL,(TMPEND)
            LD      (HL),A
            LD      A,001H
L5008:      POP     DE
            CALL    L4CD2
            POP     HL
            RET     

L500E:      PUSH    HL
            PUSH    BC
            LD      A,0FFH
            RST     018H
            DB      00DH
            SUB     030H
            CP      00AH
            JR      C,L501B             ; (+001H)
            XOR     A
L501B:      LD      E,A
            LD      D,000H
            POP     HL
            CALL    L5E99
            POP     HL
            RET     

L5024:      CP      040H
            LD      BC,POPRA
            JR      NZ,L502F            ; (+004H)
            INC     HL
            LD      BC,L504B
L502F:      LD      (05040H),BC
            CALL    L6E5F
            CALL    L5E73
L5039:      PUSH    DE
            CALL    L6E4B
            POP     DE
            EX      DE,HL
            CALL    POPRA
            EX      DE,HL
            INC     DE
            CALL    TEST1
            DB      02CH
            RET     NZ
            JR      L5039               ; (-012H)

L504B:      LD      (HL),A
            RET     

L504D:      PUSH    HL
            CALL    TEST1
            DB      04DH
            JR      NZ,L5066            ; (+012H)
            CALL    TEST1
            DB      041H
            JR      NZ,L5066            ; (+00cH)
            CALL    TEST1
            DB      058H
            JR      NZ,L5066            ; (+006H)
            EX      (SP),HL
            LD      HL,(MEMMAX)
            JR      L5082               ; (+01cH)

L5066:      POP     HL
            CALL    L6E5F
            PUSH    HL
            LD      HL,(MEMMAX)
            OR      A
            SBC     HL,DE
            JP      C,L4B2C
            LD      HL,(TMPEND)
            INC     H
            INC     H
            INC     H
            INC     H
            OR      A
            SBC     HL,DE
            JP      NC,04AF2H
            EX      DE,HL
L5082:      CALL    L4902
            POP     HL
            POP     DE
            LD      SP,(INTFAC)
            LD      BC,0FFFFH
            PUSH    BC
            PUSH    DE
            RET     

L5091:      POP     IX
L5093:      POP     BC
            PUSH    BC
            LD      A,B
            INC     A
            JP      NZ,04B07H
            LD      A,C
            CP      012H
            JR      Z,L50B9             ; (+01aH)
            CP      0FEH
            JP      NZ,04B07H
            POP     BC
            CALL    L413E
            EX      DE,HL
            POP     HL
            LD      (L8483),HL
            POP     HL
            LD      (L847D),HL
            POP     HL
            PUSH    IX
            RET     Z
            EX      DE,HL
            JP      L5147

L50B9:      EX      DE,HL
            LD      HL,L0012
            ADD     HL,SP
            LD      SP,HL
            EX      DE,HL
            JR      L5093               ; (-02fH)

L50C2:      PUSH    HL
            CALL    L4F8A
            EX      DE,HL
            POP     HL
            POP     BC
            PUSH    DE
            EXX     
            LD      HL,(L847D)
            PUSH    HL
            LD      HL,(L8483)
            PUSH    HL
            LD      HL,0FFFEH
            PUSH    HL
            LD      HL,0FE00H
            ADD     HL,SP
            LD      DE,(TMPEND)
            SBC     HL,DE
            LD      A,00AH
            JP      C,L4B2E
            EXX     
            PUSH    BC
            JR      L5147               ; (+05dH)

L50EA:      CALL    TEST1
            DB      0C1H
            JP      Z,L4C66
            CALL    L6E5F
            LD      C,E
            LD      B,D
            CP      087H
            JP      NC,L4AEC
            CP      082H
            JR      NZ,L510F            ; (+010H)
            CALL    INCHLF
            LD      E,081H
            CP      LSE4
            JR      Z,L510E             ; (+006H)
            CP      LSE0
            JP      NZ,L4AEC
            DEC     E
L510E:      LD      A,E
L510F:      PUSH    HL
            LD      HL,L43EC
            SUB     080H
            ADD     A,A
            LD      E,A
            LD      D,000H
            ADD     HL,DE
            CALL    INDRCT
            EX      (SP),HL
            INC     HL
            LD      A,B
            OR      A
            JR      NZ,L5143            ; (+020H)
            LD      A,C
            OR      A
            JR      Z,L5143             ; (+01cH)
            LD      B,A
L5128:      DEC     B
            RET     Z
            CALL    HLFTCH
            CALL    L5173
            JP      NZ,L4AEC
            CALL    L51A8
            OR      A
            RET     Z
            INC     HL
            CALL    TEST1
            DB      02CH
            JR      Z,L5128             ; (-017H)
            POP     DE
            JP      05E48H

L5143:      POP     DE
            JP      L4F8A

L5147:      CALL    L45AA
            EX      DE,HL
            JR      NZ,L5150            ; (+003H)
            LD      HL,(TEXTST)
L5150:      LD      (L847D),HL
            XOR     A
            LD      (L4B78),A
            JP      L40E4

L515A:      CALL    L6E8A
            EX      AF,AF'
            LD      A,(DE)
            OR      A
            JR      Z,L5182             ; (+020H)
            EX      AF,AF'
            CP      LSE2
            JR      NZ,L516F            ; (+008H)
L5167:      CALL    INCHLF
            CALL    L5173
            JR      Z,L5147             ; (-028H)
L516F:      POP     AF
            JP      L40AF

L5173:      CP      022H
            RET     Z
L5176:      CP      00BH
            RET     Z
            CP      00CH
            RET     

L517C:      CALL    L518B
            JR      NC,L517C            ; (-005H)
            RET     

L5182:      DEC     HL
            CALL    L518B
            JR      NC,L5167            ; (-021H)
            DB      0C3H
            POP     HL
            LD      B,B
L518B:      CALL    L51A2
            OR      A
            SCF     
            RET     Z
            CP      0C2H
            RET     Z
            CP      093H
            JR      NZ,L518B            ; (-00dH)
            CALL    L518B
            RET     C
            JR      L518B               ; (-013H)

L519E:      INC     HL
            LD      A,(HL)
            JR      L51A8               ; (+006H)

L51A2:      INC     HL
            LD      A,(HL)
            CP      094H
            JR      Z,L51C7             ; (+01fH)
L51A8:      OR      A
            RET     Z
            CP      022H
            JR      Z,L51E5             ; (+037H)
            CP      0FEH
            JR      NC,L51C5            ; (+013H)
            CP      097H
            JR      Z,L51C7             ; (+011H)
            CP      027H
            JR      Z,L51C7             ; (+00dH)
            CP      020H
            RET     NC
            CP      00BH
            RET     C
            CP      015H
            JR      NC,L51DC            ; (+018H)
            INC     HL
L51C5:      INC     HL
            RET     

L51C7:      LD      A,(HL)
            OR      A
            RET     Z
            CP      03AH
            RET     Z
            CP      022H
            JR      Z,L51D4             ; (+003H)
            INC     HL
            JR      L51C7               ; (-00dH)

L51D4:      CALL    L51E5
            OR      A
            RET     Z
            INC     HL
            JR      L51C7               ; (-015H)

L51DC:      AND     00FH
            ADD     A,L
            LD      L,A
            LD      A,020H
            RET     NC
            INC     H
            RET     

L51E5:      INC     HL
            LD      A,(HL)
            OR      A
            RET     Z
            CP      022H
            RET     Z
            JR      L51E5               ; (-009H)

L51EE:      OR      0AFH
            PUSH    AF
            LD      B,031H
            LD      C,04FH
            CALL    L4FDA
            PUSH    AF
            CALL    L523F
            POP     AF
            JR      C,L523D             ; (+03eH)
            LD      A,C
            LD      (0522BH),A
            LD      B,001H
            SRL     E
            JR      NC,L520B            ; (+002H)
            SLA     B
L520B:      SRL     D
            JR      NC,L5213            ; (+004H)
            SLA     B
            SLA     B
L5213:      EX      DE,HL
            RST     018H
            DB      01EH
            CALL    BREAKX
            CP      0F0H
            JR      NC,L521F            ; (+002H)
            LD      A,0F0H
L521F:      LD      C,A
            POP     AF
            JR      Z,L5235             ; (+012H)
            SET     3,H
            CALL    BREAKX
            AND     08FH
            OR      000H
            CALL    POPRA
            RES     3,H
            LD      A,C
            OR      B
            JR      L5238               ; (+003H)

L5235:      LD      A,B
            CPL     
            AND     C
L5238:      CALL    POPRA
            EX      DE,HL
            RET     

L523D:      POP     AF
            RET     

L523F:      LD      A,(L005D)
            AND     070H
            LD      C,A
            CALL    TEST1
            DB      02CH
            RET     NZ
            PUSH    DE
            CALL    L552C
            POP     DE
            RLCA    
            RLCA    
            RLCA    
            RLCA    
            LD      C,A
            RET     

L5255:      RST     018H
            DB      020H
            RET     

L5258:      CALL    05E48H
            RST     018H
            DB      028H
            RST     018H
            DB      000H
            RET     

L5260:      JR      Z,L5285             ; (+023H)
            LD      A,001H
            LD      (L004E),A
            LD      A,(L0056)
            LD      B,019H
            CALL    L5298
            LD      (00056H),BC
            CALL    TEST1
            DB      02CH
            RET     NZ
            LD      B,028H
            LD      A,(L005B)
            CALL    L5298
            LD      (0005BH),BC
            RET     

L5285:      XOR     A
            LD      (L004E),A
            LD      BC,L1800
            LD      (00056H),BC
            LD      BC,L2700
            LD      (0005BH),BC
            RET     

L5298:      LD      D,000H
            LD      E,A
            CALL    TEST1
            DB      02CH
            JR      Z,L52A9             ; (+008H)
            PUSH    BC
            CALL    L6E4B
            POP     BC
            CALL    L5E70
L52A9:      LD      C,E
            PUSH    BC
            CALL    L6E4B
            POP     BC
            DEC     A
            JP      M,04AF2H
            ADD     A,C
            CP      B
            JP      NC,04AF2H
            LD      B,A
            RET     

L52BA:      DI      
            OUT     (LSE4),A
            JP      L0000

L52C0:      XOR     A
            LD      BC,?SETDE
            LD      (L53A8),A
            PUSH    AF
            CALL    L5882
            RST     018H
            DB      02EH
            JP      C,04B28H
            BIT     1,A
            JP      Z,04B28H
            CALL    TEST1
            DB      02CH
            POP     AF
            OR      A
            JR      NZ,L52F2            ; (+015H)
            CALL    L6E6F
            LD      A,B
            LD      (L53A3),A
            LD      (053A4H),DE
            CALL    05E48H
            JR      L52F2               ; (+005H)

L52ED:      LD      A,002H
            LD      (L53A8),A
L52F2:      CALL    L4664
            LD      (05319H),DE
            LD      (05327H),BC
            LD      A,0FFH
            LD      (DISPX),A
            CALL    SKPDE
            LD      HL,(TEXTST)
L5308:      CALL    LDDEMI
            LD      A,D
            OR      E
            RET     Z
            EX      DE,HL
            ADD     HL,DE
            DEC     HL
            DEC     HL
            EX      DE,HL
            PUSH    DE
            CALL    LDDEMI
            PUSH    HL
            LD      HL,L0000
            OR      A
            SBC     HL,DE
            JR      C,L5326             ; (+006H)
            JR      Z,L5326             ; (+004H)
            POP     HL
            POP     HL
            JR      L5308               ; (-01eH)

L5326:      LD      HL,L0000
            OR      A
            SBC     HL,DE
            JR      NC,L5331            ; (+003H)
            POP     HL
            POP     HL
            RET     

L5331:      EX      DE,HL
            CALL    L5E66
            RST     018H
            DB      017H
            LD      HL,KEYBUF
            CALL    LDHLDE
            LD      (HL),020H
            INC     HL
            EX      DE,HL
            POP     HL
            CALL    L68E0
            LD      A,(L53A8)
            OR      A
            JR      NZ,L5350            ; (+005H)
            CALL    L5366
            JR      NC,L535A            ; (+00aH)
L5350:      LD      DE,KEYBUF
            RST     018H
            DB      017H
            RST     018H
            DB      037H
            CALL    L5733
L535A:      POP     HL
            LD      A,(L53A8)
            CP      002H
            JR      Z,L5308             ; (-05aH)
            RST     018H
            DB      00FH
            JR      L5308               ; (-05eH)

L5366:      EX      DE,HL
L5367:      PUSH    HL
            CALL    L5371
            POP     HL
            RET     C
            RET     Z
            INC     HL
            JR      L5367               ; (-00aH)

L5371:      LD      A,(L53A3)
            LD      B,A
            LD      DE,(053A4H)
L5379:      LD      A,(HL)
            INC     HL
            OR      A
            RET     Z
            CP      005H
            JR      Z,L5385             ; (+004H)
            CP      006H
            JR      NZ,L5387            ; (+002H)
L5385:      JR      L5379               ; (-00eH)

L5387:      PUSH    HL
            LD      C,A
L5389:      LD      A,(DE)
            INC     DE
            CP      005H
            JR      Z,L5393             ; (+004H)
            CP      006H
            JR      NZ,L539A            ; (+007H)
L5393:      DEC     B
            SCF     
            POP     HL
            RET     Z
            PUSH    HL
            JR      L5389               ; (-011H)

L539A:      SUB     C
            POP     HL
            OR      A
            RET     NZ
            DEC     B
            SCF     
            RET     Z
            JR      L5379               ; (-02aH)

L53A3:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
L53A8:      NOP     
L53A9:      CALL    TESTX
            DB      087H
            CALL    L5E4F
            PUSH    HL
            LD      C,000H
L53B3:      RST     018H
            DB      008H
            LD      A,C
            ADD     A,031H
            LD      D,A
            LD      E,020H
            CP      03AH
            JR      NZ,L53C2            ; (+003H)
            LD      DE,L3031
L53C2:      LD      (05445H),DE
            LD      DE,L543D
            RST     018H
            DB      00BH
            LD      A,C
            CALL    L549C
            LD      B,(HL)
            INC     HL
            PUSH    BC
            CALL    L53E0
            POP     BC
            INC     C
            LD      A,C
            CP      00AH
            JR      NZ,L53B3            ; (-029H)
            RST     018H
            DB      008H
            POP     HL
            RET     

L53E0:      LD      A,B
            OR      A
            LD      C,000H
            JR      NZ,L53ED            ; (+007H)
            LD      A,022H
            RST     018H
            DB      00AH
            RST     018H
            DB      00AH
            RET     

L53ED:      LD      A,(HL)
            CP      020H
            JR      C,L5412             ; (+020H)
            CP      022H
            JR      Z,L5412             ; (+01cH)
            LD      A,C
            CP      001H
            JR      Z,L5405             ; (+00aH)
            OR      A
            LD      DE,L544A
            JR      NZ,L5403            ; (+002H)
            INC     DE
            INC     DE
L5403:      RST     018H
            DB      00BH
L5405:      LD      A,(HL)
            RST     018H
            DB      00AH
            INC     HL
            LD      C,001H
            DJNZ    L53ED               ; (-020H)
            LD      A,022H
L540F:      RST     018H
            DB      00AH
            RET     

L5412:      LD      A,C
            CP      0FFH
            JR      Z,L5437             ; (+020H)
            OR      A
            LD      DE,0544EH
            JR      NZ,L541F            ; (+002H)
            INC     DE
            INC     DE
L541F:      RST     018H
            DB      00BH
L5421:      PUSH    BC
            PUSH    HL
            LD      L,(HL)
            LD      H,000H
            CALL    L5E66
            RST     018H
            DB      00BH
            POP     HL
            POP     BC
            INC     HL
            LD      C,0FFH
            DJNZ    L53ED               ; (-045H)
            LD      A,029H
            JP      L540F

L5437:      LD      A,02CH
            RST     018H
            DB      00AH
            JR      L5421               ; (-01cH)

L543D:      LD      B,H
            LD      B,L
            LD      B,(HL)
            JR      NZ,L548D            ; (+04bH)
            LD      B,L
            LD      E,C
            JR      Z,L5446             ; (+000H)
L5446:      NOP     
            ADD     HL,HL
            DEC     A
            NOP     
L544A:      ADD     HL,HL
            DEC     HL
            LD      (L2200),HL
            DEC     HL
            LD      B,E
            LD      C,B
            LD      D,D
            INC     H
            JR      Z,L5456             ; (+000H)
L5456:      CALL    TEST1
            DB      0B2H
            JR      Z,L5467             ; (+00bH)
            CALL    TESTX
            DB      0FFH
L5460:      CALL    TESTX
            DB      0C7H
            JP      L7824

L5467:      CALL    TESTX
            DB      028H
            CALL    L6E4B
            CALL    TESTX
            DB      029H
            CALL    TESTX
            DB      QDPC
            LD      A,E
            DEC     A
            CP      00AH
            JP      NC,04AF2H
            PUSH    HL
            CALL    L549C
            EX      (SP),HL
            CALL    L6E6F
            EX      (SP),HL
            LD      A,B
            CP      010H
            JR      C,L548D             ; (+002H)
            LD      A,00FH
L548D:      LD      (HL),A
            OR      A
            JR      Z,L5499             ; (+008H)
            LD      B,A
            INC     HL
L5493:      LD      A,(DE)
            LD      (HL),A
            INC     DE
            INC     HL
            DJNZ    L5493               ; (-006H)
L5499:      POP     HL
            EI      
            RET     

L549C:      ADD     A,A
            ADD     A,A
            ADD     A,A
            ADD     A,A
            LD      HL,L12C0
            JP      ADDHLA

L54A6:      CALL    TEST1
            DB      02CH
            JR      NZ,L54B4            ; (+008H)
            CALL    L5E70
            LD      DE,L005D
            JR      L54C0               ; (+00cH)

L54B4:      CALL    L4FCF
            CALL    L5E70
            EX      DE,HL
            RST     018H
            DB      01EH
            SET     3,H
            EX      DE,HL
L54C0:      EX      DE,HL
            CALL    BREAKX
            EX      DE,HL
            PUSH    DE
            CALL    L54D0
            POP     DE
            EX      DE,HL
            CALL    POPRA
            EX      DE,HL
            RET     

L54D0:      LD      C,A
            CALL    TEST1
            DB      02CH
            JR      Z,L54E9             ; (+012H)
            CALL    L552C
            ADD     A,A
            ADD     A,A
            ADD     A,A
            ADD     A,A
            XOR     C
            AND     070H
            XOR     C
            LD      C,A
            CALL    TEST1
            DB      02CH
            JR      NZ,L54FA            ; (+011H)
L54E9:      CALL    L413E
            JR      Z,L54FA             ; (+00cH)
            CP      05DH
            JR      Z,L54FA             ; (+008H)
            CALL    L552C
            XOR     C
            AND     007H
            XOR     C
            RET     

L54FA:      LD      A,C
            RET     

L54FC:      INC     HL
L54FD:      CALL    L6E6F
            LD      A,B
            OR      A
            JR      Z,L5513             ; (+00fH)
            PUSH    HL
            LD      HL,KEYBUF
            PUSH    HL
            CALL    LDHLDE
            LD      (HL),00DH
            POP     DE
            CALL    L0030
            POP     HL
L5513:      CALL    HLFTCH
            CP      03BH
            JR      Z,L54FC             ; (-01eH)
            CP      02CH
            JR      Z,L54FC             ; (-022H)
            RET     

L551F:      CALL    L6E4B
            DEC     A
            CP      007H
            JP      NC,04AF2H
            INC     A
            JP      L0041

L552C:      PUSH    BC
            CALL    L6E4B
            POP     BC
            CP      008H
            JP      NC,04AF2H
            RET     

L5537:      RST     018H
            DB      045H
            RET     

            DS      198, 000H
L5600:      LD      A,(ZLOG)
            CP      088H
            RET     

L5606:      CALL    L6E8A
            LD      A,(L84CE)
            CP      003H
            PUSH    HL
            EX      DE,HL
            JR      Z,L5619             ; (+007H)
            CALL    L6028
            POP     HL
            RST     018H
            DB      017H
            RET     

L5619:      CALL    L6E7E
            POP     HL
            RET     

L561E:      LD      A,(L005D)
            LD      (0574DH),A
            XOR     A
            LD      (DISPX),A
            CALL    L5882
            RST     018H
            DB      02EH
            JP      C,L57B4
            BIT     2,A
            JP      NZ,L5D18
            BIT     1,A
            JP      Z,057B7H
            CALL    L58C2
            CALL    L5600
            JR      NZ,L5655            ; (+013H)
            CALL    TEST1
            DB      05BH
            JR      NZ,L5655            ; (+00dH)
            LD      A,(0574DH)
            CALL    L54D0
            LD      (0574DH),A
            CALL    TESTX
            DB      05DH
L5655:      CALL    L413E
            JP      Z,L5733
L565B:      LD      A,(HL)
            CP      LSE3
            JR      Z,L56DA             ; (+07aH)
            LD      BC,L5674
            PUSH    BC
            CP      03BH
            RET     Z
            CP      02CH
            RET     Z
            CP      LSE6
            JR      Z,L56AA             ; (+03cH)
            CALL    L5606
            JP      L573E

L5674:      CALL    L413E
            JP      Z,L5733
            CP      02CH
            JR      NZ,L569A            ; (+01cH)
            CALL    L5600
            JR      C,L5697             ; (+014H)
            LD      IX,(ZDELT)
            CALL    IOCALL
            LD      B,A
L568B:      SUB     00AH
            JR      NC,L568B            ; (-004H)
            NEG     
            LD      B,A
            CALL    L56CC
            JR      L569A               ; (+003H)

L5697:      CALL    L5733
L569A:      LD      A,(HL)
            CP      02CH
            JR      Z,L56A1             ; (+002H)
            CP      03BH
L56A1:      JR      NZ,L56A4            ; (+001H)
            INC     HL
L56A4:      CALL    L413E
            RET     Z
            JR      L565B               ; (-04fH)

L56AA:      CALL    L5600
            JP      C,057B7H
            CALL    L5E77
            CALL    L6E4B
            CALL    L5E7F
            LD      IX,(ZDELT)
            CALL    IOCALL
            SUB     E
            RET     NC
            NEG     
            LD      B,A
            CALL    L5600
            LD      A,013H
            JR      Z,L56CE             ; (+002H)
L56CC:      LD      A,020H
L56CE:      LD      DE,KEYBUF
            PUSH    BC
            PUSH    DE
            CALL    ?SETDE
            POP     DE
            POP     BC
            JR      L573E               ; (+064H)

L56DA:      INC     HL
            CALL    L6E6F
            LD      A,B
            OR      A
            JP      Z,04AF2H
            PUSH    HL
            LD      HL,KEYBUF
            PUSH    HL
            PUSH    BC
            CALL    LDHLDE
            POP     BC
            LD      A,0F0H
            LD      E,B
            LD      D,000H
            INC     DE
            RST     018H
            DB      02AH
            LD      (L6306),HL
            LD      (L6308),HL
            POP     DE
            CALL    LDHLDE
            LD      (HL),000H
            POP     HL
L5702:      CALL    L413E
            JR      Z,L5729             ; (+022H)
            INC     HL
            CP      02CH
            JR      Z,L5711             ; (+005H)
            CP      03BH
            JP      NZ,L4AEC
L5711:      CALL    L413E
            JR      Z,L572C             ; (+016H)
            CALL    L6E92
            PUSH    HL
            LD      BC,(TMPEND)
            PUSH    BC
            CALL    L630A
            POP     DE
            CALL    L573C
            POP     HL
            JR      L5702               ; (-027H)

L5729:      CALL    L5733
L572C:      LD      A,0F0H
            RST     018H
            DB      02BH
            RET     

L5731:      DEC     C
            NOP     
L5733:      LD      A,(L005D)
            LD      (0574DH),A
            LD      DE,L5731
L573C:      RST     018H
            DB      017H
L573E:      CALL    L5600
            JR      Z,L5746             ; (+003H)
            RST     018H
            DB      037H
            RET     

L5746:      PUSH    HL
            PUSH    BC
            LD      HL,L005D
            LD      C,(HL)
            LD      (HL),070H
            RST     018H
            DB      037H
            LD      (HL),C
            POP     BC
            POP     HL
            RET     

L5754:      LD      A,08AH
            LD      (ZLOG),A
            JR      L5792               ; (+037H)

L575B:      CALL    L5882
            RST     018H
            DB      02EH
            JP      C,L57B4
            BIT     2,A
            JP      NZ,L5D29
            BIT     0,A
            JP      Z,057B7H
            CALL    L58C2
            CALL    L5600
            JR      NZ,L5792            ; (+01dH)
            CALL    HLFTCH
            CP      022H
            LD      DE,L57BC
            LD      B,002H
            JR      NZ,L5788            ; (+007H)
            CALL    L6E6F
            CALL    TESTX
            DB      03BH
L5788:      LD      A,B
            OR      A
            JR      Z,L5792             ; (+006H)
L578C:      LD      A,(DE)
            INC     DE
            RST     018H
            DB      003H
            DJNZ    L578C               ; (-006H)
L5792:      LD      (057EBH),SP
            LD      DE,(VARED)
            LD      (TMPEND),DE
L579E:      LD      DE,(TMPEND)
            CALL    L74E8
            CALL    L7436
            PUSH    AF
            PUSH    BC
            CALL    L413E
            JR      Z,057BEH             ; (+00fH)
            CALL    L5E73
            JR      L579E               ; (-016H)

L57B4:      LD      A,02CH
            LD      HL,0BB3EH
            JP      L0182

L57BC:      CCF     
            JR      NZ,0576EH            ; (-051H)
            PUSH    AF
            PUSH    HL
            RST     018H
            DB      034H
            LD      HL,(057EBH)
            DEC     HL
L57C7:      LD      A,(HL)
            OR      A
            JR      Z,L57E9             ; (+01eH)
            DEC     HL
            DEC     HL
            LD      B,(HL)
            DEC     HL
            LD      C,(HL)
            DEC     HL
            PUSH    HL
            PUSH    AF
            PUSH    BC
            LD      DE,(TMPEND)
            CALL    L57EE
            LD      H,000H
            LD      L,B
            ADD     HL,DE
            LD      (HL),000H
            POP     DE
            POP     AF
            CALL    L57F8
            POP     HL
            JR      L57C7               ; (-022H)

L57E9:      POP     HL
            LD      SP,L0000
            RET     

L57EE:      LD      A,(ZLOG)
            CP      08AH
            JR      Z,L5839             ; (+044H)
            RST     018H
            DB      035H
            RET     

L57F8:      CP      003H
            JR      NZ,L5803            ; (+007H)
            LD      HL,(TMPEND)
            LD      A,B
$START:     JP      L4CD2               ; $START - BASIC warm start / system entry

L5803:      PUSH    DE
            LD      HL,(INTFAC)
            LD      DE,(TMPEND)
            EX      DE,HL
            CALL    HLFTCH
            CP      045H
            JP      Z,04AF2H
            EX      DE,HL
            CALL    L5EDC
            EX      DE,HL
            CALL    TEST1
            DB      000H
            JP      NZ,04AF2H
            EX      DE,HL
            POP     DE
            JP      L5E93

L5825:      XOR     A
            JR      L587E               ; (+056H)

L5828:      CALL    L5825
            CALL    L413E
            CALL    NZ,L45AA
            EX      DE,HL
            CALL    NZ,L5869
            EX      DE,HL
            JP      L4F8A

L5839:      PUSH    HL
            PUSH    DE
            CALL    L5847
            POP     DE
            POP     HL
            RET     

L5841:      LD      HL,(TEXTST)
            CALL    L5869
L5847:      LD      A,(L848F)
            CP      001H
            JP      Z,04B1FH
            JR      C,L5841             ; (-010H)
            LD      HL,(L8490)
            LD      C,03AH
            RST     018H
            DB      036H
            LD      (L8490),HL
            CALL    L413E
            SCF     
            CCF     
            RET     NZ
            DEC     HL
L5862:      CALL    L519E
            OR      A
            JR      NZ,L5874            ; (+00cH)
            INC     HL
L5869:      LD      A,(HL)
            INC     HL
            OR      (HL)
            LD      A,001H
            JR      Z,L587E             ; (+00eH)
            INC     HL
            INC     HL
            JR      L5862               ; (-012H)

L5874:      CP      094H
            JR      NZ,L5862            ; (-016H)
            INC     HL
            LD      (L8490),HL
            LD      A,002H
L587E:      LD      (L848F),A
            RET     

L5882:      CALL    TEST1
            DB      023H
            JR      NZ,L58AD            ; (+025H)
            CALL    HLFTCH
            CP      020H
            JP      NC,L4AEC
            PUSH    DE
            PUSH    BC
            LD      DE,L84CF
            PUSH    DE
            CALL    L705A
            EX      (SP),HL
            CALL    L60B7
            LD      A,H
            OR      A
            JP      NZ,04B28H
            OR      L
            JP      Z,04B28H
            JP      M,04B28H
            POP     HL
            POP     BC
            POP     DE
            RET     

L58AD:      CALL    TEST1
            DB      FDPD
            LD      A,088H
            RET     NZ
            CALL    TEST1
            DB      050H
            LD      A,089H
            RET     Z
            CALL    TESTX
            DB      054H
            LD      A,080H
            RET     

L58C2:      LD      A,(ZLOG)
            OR      A
            RET     M
            JP      L5E70

L58CA:      JP      DBG_DIRCMD1         ; Debug hook: DIR handler 1 (via L593A)
            NOP
            NOP
            NOP

L58D0:      JP      DBG_DIRCMD2         ; Debug hook: DIR handler 2 (via L6E6F)
            NOP
            NOP
            NOP
            NOP
            NOP
L58D8:      PUSH    HL
            RST     018H
            DB      02CH
            RST     018H
            DB      03CH
            POP     HL
            RET     

L58DF:      LD      A,001H
            LD      BC,0023EH
            LD      BC,0043EH
            PUSH    AF
            LD      (ZRWX),A
            CALL    L5882
            CP      088H
            JR      NZ,L58F4            ; (+002H)
            LD      A,080H
L58F4:      LD      (ZLOG),A
            CALL    L58C2
            CALL    L592D
            POP     AF
            CP      004H
            JR      Z,L5904             ; (+002H)
L5902:      LD      A,003H
L5904:      LD      (ELMD),A
            PUSH    AF
            RST     018H
            DB      033H
            LD      A,(ELMD)
            POP     BC
            CP      B
            JP      NZ,04B53H
            RET     

L5913:      OR      0AFH
            LD      B,A
            CALL    L413E
            JR      Z,L5929             ; (+00eH)
L591B:      CALL    L5882
            CP      088H
            RET     NC
            RST     018H
            DB      038H
            CALL    TEST1
            DB      02CH
            JR      L591B               ; (-00eH)

L5929:      XOR     A
            RST     018H
            DB      038H
            RET     

L592D:      CALL    L46A6
            LD      B,000H
            CALL    NZ,L6E6F
            PUSH    HL
            RST     018H
            DB      02DH
            POP     HL
            RET     

L593A:      PUSH    HL
            CALL    HLFTCH
            LD      DE,L5982
            LD      B,001H
            CALL    L5975
            CALL    NC,L5975
            JR      C,L5962             ; (+017H)
            CALL    L5975
            CALL    L5EC4
            CALL    L597B
            CALL    L413E
            JR      NZ,L5962            ; (+009H)
            LD      A,03AH
            LD      (DE),A
            POP     AF
            JP      RFS_DIR_FIX
            NOP
            NOP

L5962:      POP     HL
            CALL    L413E
            LD      B,000H
            CALL    NZ,L6E6F
L596B:      JP      DBG_DIR             ; Debug hook: trace SVC #2C for DIR
            NOP                         ; (was: PUSH HL / RST 018H / DB 02CH / INC(HL) / DEC(HL) / JP NZ / POP HL / RET)
            NOP
            NOP
            NOP
            NOP
            NOP
            NOP

L5975:      CP      041H
            RET     C
            CP      05BH
            CCF     
L597B:      RET     C
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            INC     B
            RET     

L5982:      NOP     
            NOP     
            NOP     
            NOP     
L5986:      CALL    L592D
            CALL    TEST1
            DB      02CH
            JP      Z,L5A47
            PUSH    HL
            CALL    L5BDC
            DEC     A
            JR      Z,L59A9             ; (+012H)
            DEC     A
            JP      NZ,04B53H
            CALL    L5C08
            CALL    L5BE5
            CALL    L5C11
            CALL    L5C44
            POP     HL
            RET     

L59A9:      LD      HL,(ELMD22)
            PUSH    HL
            LD      DE,(MEMLMT)
            CALL    L59CF
            LD      DE,(ELMD20)
            LD      BC,(MEMMAX)
            CALL    NC,L59CA
            JP      C,04B13H
            POP     HL
            RST     018H
            DB      030H
            CALL    L5C44
            POP     HL
            RET     

L59CA:      ADD     HL,DE
            RET     C
            EX      DE,HL
            LD      H,B
            LD      L,C
L59CF:      PUSH    HL
            OR      A
            SBC     HL,DE
            POP     HL
            RET     

L59D5:      CALL    L592D
            CALL    L5BDC
            CP      002H
            JP      NZ,04B53H
            LD      A,(L849A)
            OR      A
            JP      Z,L5B0D
            JP      L5DB2

L59EA:      CALL    L5C08
            CALL    L592D
            CALL    TEST1
            DB      02CH
            JR      Z,05A4DH             ; (+057H)
            RST     018H
            DB      02FH
            CP      002H
            JP      NZ,04B53H
            PUSH    HL
            LD      HL,(VARED)
            LD      (TMPEND),HL
            LD      BC,003E8H
            ADD     HL,BC
            JP      C,L4B2C
            PUSH    HL
            LD      BC,(ELMD20)
            INC     B
            ADD     HL,BC
            JP      C,L4B2C
            SBC     HL,SP
            JP      NC,L4B2C
            POP     HL
            PUSH    HL
            RST     018H
            DB      030H
            POP     HL
            CALL    L5A24
            POP     HL
            RET     

L5A24:      CALL    LDDEMI
            LD      A,D
            OR      E
            RET     Z
            PUSH    DE
            CALL    LDDEMI
            PUSH    HL
            LD      H,D
            LD      L,E
            LD      (L847F),HL
            CALL    L49B9
            POP     HL
            POP     BC
            PUSH    BC
            PUSH    HL
            CALL    L4794
            POP     HL
            POP     BC
            ADD     HL,BC
            DEC     HL
            DEC     HL
            DEC     HL
            DEC     HL
            JR      L5A24               ; (-023H)

L5A47:      CALL    L5C08
            LD      A,001H
            LD      BC,L003E
            LD      BC,0023EH
            PUSH    AF
            CALL    TESTX
            DB      041H
            CALL    05E48H
            LD      A,001H
            LD      (ZRWX),A
            LD      A,081H
            LD      (ZLOG),A
            CALL    L5902
            POP     AF
            PUSH    AF
            PUSH    HL
            LD      HL,L0000
            LD      DE,0FFFFH
            OR      A
            CALL    NZ,L49B9
            LD      A,081H
            RST     018H
            DB      02EH
            LD      HL,(VARED)
            LD      (TMPEND),HL
            LD      BC,003E8H
            ADD     HL,BC
            JP      C,04AFBH
            PUSH    HL
            LD      (L5AEB),HL
L5A89:      LD      HL,0FE00H
            ADD     HL,SP
            LD      DE,(05AEBH)
            SBC     HL,DE
            JP      C,04AFBH
            LD      DE,(TMPEND)
            RST     018H
            DB      035H
            LD      A,B
            OR      A
            JR      Z,L5AD6             ; (+036H)
            CALL    L6A11
            LD      A,B
            OR      C
            JP      Z,04AF2H
            LD      HL,(L5AEB)
            PUSH    HL
            INC     HL
            INC     HL
            LD      (HL),C
            INC     HL
            LD      (HL),B
            INC     HL
            PUSH    HL
            LD      H,D
            LD      L,E
            CALL    TEST1
            DB      000H
            JP      Z,04AF2H
            POP     HL
            LD      A,(DE)
            CP      020H
            JR      NZ,L5AC3            ; (+001H)
            INC     DE
L5AC3:      CALL    L66EF
            LD      (HL),000H
            INC     HL
            LD      (L5AEB),HL
            POP     DE
            OR      A
            SBC     HL,DE
            EX      DE,HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            JR      L5A89               ; (-04dH)

L5AD6:      LD      HL,(L5AEB)
            CALL    L4952
            CALL    L4928
            POP     HL
            CALL    L5A24
            POP     HL
            POP     AF
            CP      002H
            RET     NZ
            JP      L480D

L5AEB:      NOP     
            NOP     
L5AED:      CALL    L592D
            PUSH    HL
            CALL    TEST1
            DB      02CH
            JR      NZ,L5AFF            ; (+008H)
            CALL    HLFTCH
            CP      041H
            JP      Z,05A50H
L5AFF:      CALL    L5BDC
            POP     HL
            DEC     A
            JR      Z,L5B16             ; (+010H)
            DEC     A
            JP      NZ,04B53H
            CALL    L5BE5
L5B0D:      CALL    L5C11
            CALL    L5C44
            JP      L480D

L5B16:      LD      D,0C9H
            LD      BC,0FF00H
            CALL    TEST1
            DB      02CH
            JR      NZ,L5B2A            ; (+009H)
            CALL    TESTX
            DB      052H
            LD      D,000H
            LD      BC,0CF00H
L5B2A:      LD      A,D
            LD      (L5B93),A
            LD      HL,(ELMD20)
            LD      DE,(ELMD22)
            PUSH    HL
            CALL    L59CA
            POP     DE
            LD      HL,(TEXTST)
            LD      BC,0FF00H
            CALL    NC,L59CA
            JP      C,L4B2C
            LD      SP,L0000
            CALL    L4930
            XOR     A
            LD      (L5C07),A
            LD      HL,(ELMD24)
            PUSH    HL
            LD      DE,(ELMD20)
            PUSH    DE
            LD      HL,(TMPEND)
            RST     018H
            DB      030H
            LD      A,004H
            RST     018H
            DB      003H
            LD      A,016H
            RST     018H
            DB      003H
            POP     BC
            PUSH    BC
            LD      DE,(ELMD22)
            CALL    L59CF
            LD      A,0B0H
            JR      NC,L5B7B            ; (+008H)
            EX      DE,HL
            ADD     HL,BC
            DEC     HL
            EX      DE,HL
            ADD     HL,BC
            DEC     HL
            LD      A,0B8H
L5B7B:      LD      (05B92H),A
            DI      
            PUSH    HL
            PUSH    DE
            LD      BC,00016H
            LD      HL,L5B8E
            LD      DE,0FF80H
            PUSH    DE
            LDIR    
            RET     

L5B8E:      POP     DE
            POP     HL
            POP     BC
            LDIR    
L5B93:      RET     

            POP     BC
            LD      SP,VRAM
            EX      DE,HL
            PUSH    BC
            PUSH    HL
            LD      (HL),LSD3
            INC     HL
            LD      (HL),LSE4
            INC     HL
            LD      (HL),0C9H
            RET     

L5BA4:      LD      HL,(VARED)
            LD      (TMPEND),HL
            LD      DE,(TEXTED)
            LD      (05C01H),DE
            OR      A
            SBC     HL,DE
            LD      (L5C03),HL
            LD      HL,0FF00H
            ADD     HL,SP
            LD      (L5C05),HL
            LD      DE,(VARED)
            PUSH    HL
            OR      A
            SBC     HL,DE
            JP      C,L4B2C
            EX      (SP),HL
            EX      DE,HL
            LD      BC,(05C03H)
            INC     BC
            LDDR    
            POP     DE
            RST     018H
            DB      018H
            LD      A,001H
            LD      (L5C07),A
            RET     

L5BDC:      CALL    L5BA4
            RST     018H
            DB      02FH
            LD      A,(ELMD)
            RET     

L5BE5:      LD      HL,(VARED)
            XOR     A
            DEC     HL
            LD      (HL),A
            DEC     HL
            LD      (HL),A
            LD      (STRST),HL
            DEC     HL
            LD      (HL),A
            LD      (VARST),HL
            DEC     HL
            LD      (HL),A
            LD      (TEXTED),HL
            LD      HL,00004H
            LD      (L5C03),HL
            RET     

L5C01:      NOP     
            NOP     
L5C03:      NOP     
            NOP     
L5C05:      NOP     
            NOP     
L5C07:      NOP     
L5C08:      PUSH    AF
            CALL    L4146
            JP      NZ,04B16H
            POP     AF
            RET     

L5C11:      LD      BC,(ELMD20)
            PUSH    BC
            LD      HL,(TEXTED)
            LD      DE,(TEXTST)
            OR      A
            SBC     HL,DE
            LD      L,000H
            SBC     HL,BC
            JP      C,L4B2C
            LD      HL,L0000
            LD      (L5C01),HL
            CALL    L4821
            LD      HL,L0000
            LD      (L8483),HL
            LD      HL,(TEXTST)
            RST     018H
            DB      030H
            POP     BC
            LD      HL,(TEXTST)
            ADD     HL,BC
            LD      (L5C01),HL
            RET     

L5C44:      LD      A,081H
            LD      B,000H
            RST     018H
            DB      038H
            LD      HL,L5C07
            LD      A,(HL)
            OR      A
            RET     Z
            LD      (HL),000H
            LD      HL,(L5C01)
            LD      A,H
            OR      L
            JR      NZ,L5C5F            ; (+006H)
            LD      HL,(TEXTST)
            CALL    L4952
L5C5F:      EX      DE,HL
            LD      HL,(TEXTED)
            LD      BC,(05C03H)
            LDIR    
            EX      DE,HL
            OR      A
            SBC     HL,DE
            EX      DE,HL
            RST     018H
            DB      018H
            RET     

L5C71:      PUSH    HL
            CALL    L46C0
            POP     HL
            CALL    L592D
            PUSH    HL
            RST     018H
            DB      02FH
            CP      002H
            JP      NZ,04B53H
            LD      HL,(TEXTST)
            RST     018H
            DB      032H
            POP     HL
            RET     

L5C88:      PUSH    HL
            CALL    L46C0
            POP     HL
            CALL    L592D
            CALL    TEST1
            DB      02CH
            JR      Z,L5CB5             ; (+01fH)
            PUSH    HL
            LD      A,002H
            LD      (ELMD),A
            LD      HL,(TEXTED)
            LD      DE,(TEXTST)
            OR      A
            SBC     HL,DE
            LD      (ELMD20),HL
            LD      A,(ELMD1)
            CP      00DH
            JP      Z,04B50H
            RST     018H
            DB      031H
            POP     HL
            RET     

L5CB5:      CALL    TESTX
            DB      041H
            PUSH    HL
            LD      A,002H
            LD      (ZRWX),A
            LD      A,081H
            LD      (ZLOG),A
            CALL    L5902
            POP     HL
            CALL    L52ED
            CALL    L5733
            LD      B,001H
            LD      A,081H
            RST     018H
            DB      038H
            RET     

L5CD5:      XOR     A
            LD      BC,?SETDE
            PUSH    AF
            CALL    L6E6F
            RST     018H
            DB      02DH
            POP     AF
            RST     018H
            DB      03FH
            RET     

L5CE3:      JP      DBG_DIR3            ; Debug hook: trace full DIR handler
            PUSH    AF
            RST     018H
            DB      02EH
            JP      C,L57B4
            BIT     1,A
            JP      Z,057B7H
            CALL    L58C2
            CALL    L593A
            LD      B,A
            XOR     A
            RST     018H
            DB      039H
            LD      A,B
            RST     018H
            DB      03AH
            POP     AF
            RST     018H
            DB      039H
            RET     

L5D02:      CALL    L6E6F
            RST     018H
            DB      02DH
            RST     018H
            DB      03DH
            RET     

L5D0A:      CALL    L6E6F
            RST     018H
            DB      02DH
            CALL    L5E70
            CALL    L6E6F
            RST     018H
            DB      03EH
            RET     

L5D18:      CALL    L5D4E
L5D1B:      CALL    L5606
            RST     018H
            DB      042H
            CALL    L413E
            RET     Z
            CALL    L5E73
            JR      L5D1B               ; (-00eH)

L5D29:      CALL    L5D4E
            LD      DE,(TMPEND)
            CALL    L74E8
L5D33:      CALL    L7436
            PUSH    HL
            PUSH    AF
            PUSH    BC
            LD      DE,(TMPEND)
            RST     018H
            DB      041H
            POP     DE
            POP     AF
            CALL    L57F8
            POP     HL
            CALL    L413E
            RET     Z
            CALL    L5E73
            JR      L5D33               ; (-01bH)

L5D4E:      CALL    TEST1
            DB      028H
            RET     NZ
            CALL    L6E5F
            LD      A,D
            OR      E
            JP      Z,04AF2H
            RST     018H
            DB      040H
            CALL    L5E7F
            CALL    TEST1
            DB      02CH
            RET     

L5D65:      LD      A,(L849A)
            OR      A
            JP      NZ,04B22H
            PUSH    HL
            LD      B,000H
            RST     018H
            DB      02CH
            LD      (05E1AH),DE
            LD      (L5E1C),A
            LD      HL,(TEXTED)
            LD      DE,(TEXTST)
            XOR     A
            SBC     HL,DE
            LD      (ELMD20),HL
            RST     018H
            DB      044H
            POP     HL
            CALL    L592D
            CALL    05E48H
            LD      A,(ZFLAG1)
            BIT     5,A
            JP      Z,057B7H
            PUSH    HL
            LD      HL,(08494H)
            ADD     HL,SP
            LD      SP,HL
            EX      DE,HL
            LD      HL,L847D
            LD      BC,(08492H)
            LDIR    
            CALL    L5BDC
            CP      002H
            JP      NZ,04B53H
            LD      (05DB3H),SP
L5DB2:      LD      SP,L0000
            CALL    L5C11
            CALL    L5C44
            LD      HL,0FFFDH
            PUSH    HL
            PUSH    HL
            LD      A,001H
            LD      (L849A),A
            LD      HL,(TEXTST)
            JP      L40E4

L5DCB:      XOR     A
            LD      (L849A),A
            POP     IX
L5DD1:      POP     BC
            LD      A,B
            CP      0FFH
            JP      NZ,04B22H
            LD      A,C
            CP      0FDH
            JR      Z,L5DF0             ; (+013H)
            CP      0FEH
            LD      HL,00004H
            JR      Z,L5DEC             ; (+008H)
            CP      012H
            LD      HL,00010H
            JP      NZ,04B22H
L5DEC:      ADD     HL,SP
            LD      SP,HL
            JR      L5DD1               ; (-01fH)

L5DF0:      LD      DE,(05E1AH)
            LD      A,(L5E1C)
            RST     018H
            DB      03AH
            LD      B,000H
            RST     018H
            DB      02CH
            CALL    L5BA4
            OR      0FFH
            RST     018H
            DB      044H
            CALL    L5C11
            CALL    L5C44
            LD      HL,L0000
            ADD     HL,SP
            LD      DE,L847D
            LD      BC,(08492H)
            LDIR    
            LD      SP,HL
            POP     HL
            RET     

            NOP     
            NOP     
L5E1C:      NOP     
L5E1D:      LD      DE,L5E37
            LD      B,005H
            CALL    L58D8
            LD      A,031H
            CALL    L5E2C
            LD      A,032H
L5E2C:      LD      (05E3EH),A
            LD      DE,05E3CH
            LD      B,00CH
            JP      L58D8

L5E37:      DB      "CMT:TRS?:0,$8C,13",0CDH; XCMT device config string
            LD      A,041H
            RET     Z
            JP      L4AEC

L5E4F:      XOR     A
            LD      (FILOUT),A
            CALL    TEST1
            DB      FDPD
            RET     NZ
            CALL    TESTX
            DB      050H
            CALL    L6AC6
            LD      A,050H
            LD      (FILOUT),A
            CP      A
            RET     

L5E66:      LD      DE,L849C
            LD      B,000H
            PUSH    DE
            RST     018H
            DB      016H
            POP     DE
            RET     

L5E70:      CALL    HLFTCH
L5E73:      CP      02CH
            JR      L5E84               ; (+00dH)

L5E77:      INC     HL
L5E78:      CALL    HLFTCH
L5E7B:      CP      028H
            JR      L5E84               ; (+005H)

L5E7F:      CALL    HLFTCH
L5E82:      CP      029H
L5E84:      INC     HL
            RET     Z
            JP      L4AEC

L5E89:      LD      A,(DE)
            CP      020H
            INC     DE
            JR      Z,L5E89             ; (-006H)
            RET     

L5E90:      LD      HL,L6112
L5E93:      LD      BC,00005H
            LDIR    
            RET     

L5E99:      CALL    L7967
            LD      A,E
            OR      D
            RET     Z
            BIT     7,D
            LD      A,07FH
            JR      Z,L5EAE             ; (+009H)
            LD      A,D
            CPL     
            LD      D,A
            LD      A,E
            CPL     
            LD      E,A
            INC     DE
            LD      A,0FFH
L5EAE:      LD      B,091H
L5EB0:      DEC     B
            BIT     7,D
            JR      NZ,L5EBB            ; (+006H)
            RL      E
            RL      D
            JR      L5EB0               ; (-00bH)

L5EBB:      LD      (HL),B
            INC     HL
            AND     D
            LD      (HL),A
            INC     HL
            LD      (HL),E
            DEC     HL
            DEC     HL
            RET     

L5EC4:      CP      030H
            RET     C
            CP      03AH
            CCF     
            RET     

L5ECB:      CP      05FH
            RET     Z
            CP      030H
            RET     C
            CP      05BH
            CCF     
            RET     C
            CP      03AH
            CCF     
            RET     NC
            CP      041H
            RET     

L5EDC:      CALL    L7967
            LD      (L6301),A
            LD      (L6302),A
            LD      (L6303),A
            LD      (05F83H),A
            LD      A,005H
            LD      (L84CE),A
L5EF0:      CALL    L5E89
            CP      02BH
            JR      Z,L5EF0             ; (-007H)
            CP      02DH
            JR      NZ,L5F01            ; (+006H)
            CALL    L5EF0
            JP      L7972

L5F01:      CP      024H
            JR      NZ,L5F11            ; (+00cH)
            PUSH    HL
            EX      DE,HL
            RST     018H
            DB      014H
            EX      (SP),HL
            CALL    L5E99
            POP     DE
            LD      A,005H
            RET     

L5F11:      CP      030H
            JR      NZ,L5F1B            ; (+006H)
            LD      A,(DE)
            INC     DE
            JR      L5F11               ; (-008H)

L5F19:      LD      A,(DE)
            INC     DE
L5F1B:      CP      020H
            JR      Z,L5F19             ; (-006H)
            CP      02EH
            JR      Z,L5F3E             ; (+01bH)
            CALL    L5EC4
            JR      C,L5F78             ; (+050H)
            SUB     030H
            CALL    L60AA
            CALL    L6014
            LD      A,001H
            LD      (L6302),A
            LD      A,(L6301)
            INC     A
            LD      (L6301),A
            JR      L5F19               ; (-025H)

L5F3E:      LD      A,001H
            LD      (05F83H),A
            LD      C,A
L5F44:      LD      A,(DE)
            INC     DE
            CP      020H
            JR      Z,L5F44             ; (-006H)
            CALL    L5EC4
            JR      C,L5F70             ; (+021H)
            INC     C
            SUB     030H
            JR      Z,L5F5B             ; (+007H)
            PUSH    AF
            LD      A,001H
            LD      (L6302),A
            POP     AF
L5F5B:      PUSH    AF
            LD      A,(L6302)
            LD      B,A
            LD      A,(L6301)
            ADD     A,B
            LD      (L6301),A
            POP     AF
            CALL    L60AA
            CALL    L6014
            JR      L5F44               ; (-02cH)

L5F70:      DEC     C
            JR      Z,L5F78             ; (+005H)
            CALL    L5FFE
            JR      L5F70               ; (-008H)

L5F78:      CP      045H
            JR      Z,L5F89             ; (+00dH)
L5F7C:      DEC     DE
            LD      A,(L6303)
            OR      A
            RET     NZ
            LD      A,000H
            OR      A
            RET     NZ
            LD      A,005H
            RET     

L5F89:      LD      A,(DE)
            CP      02DH
            JR      Z,L5F97             ; (+009H)
            CP      02BH
            JR      Z,L5F97             ; (+005H)
            CALL    L5EC4
            JR      C,L5F7C             ; (-01bH)
L5F97:      LD      A,001H
            LD      (05F83H),A
            PUSH    HL
            LD      HL,L7BB3
            LD      (05FF7H),HL
            LD      HL,L0000
            LD      A,(DE)
            INC     DE
            CP      02BH
            JR      Z,L5FB8             ; (+00cH)
            CP      02DH
            JR      NZ,L5FBA            ; (+00aH)
            PUSH    HL
            LD      HL,L7C9E
            LD      (05FF7H),HL
            POP     HL
L5FB8:      LD      A,(DE)
            INC     DE
L5FBA:      SUB     030H
            JR      C,L5FDA             ; (+01cH)
            CP      00AH
            JR      NC,L5FDA            ; (+018H)
            PUSH    DE
            CALL    L600F
            LD      E,L
            LD      D,H
            CALL    L600F
            CALL    L600F
            CALL    L600B
            LD      E,A
            LD      D,000H
            CALL    L600B
            POP     DE
            JR      L5FB8               ; (-022H)

L5FDA:      LD      A,H
            OR      A
            JR      NZ,L6011            ; (+033H)
            LD      A,L
            POP     HL
            PUSH    DE
            PUSH    BC
            PUSH    HL
            LD      DE,L84CF
            PUSH    DE
            CALL    L5E90
            POP     HL
            LD      B,A
            INC     B
            JR      L5FF2               ; (+003H)

L5FEF:      CALL    L60AA
L5FF2:      DJNZ    L5FEF               ; (-005H)
            EX      DE,HL
            POP     HL
            CALL    L0000
            POP     BC
            POP     DE
            JP      L5F7C

L5FFE:      PUSH    AF
            PUSH    BC
            PUSH    DE
            LD      DE,L610D
            CALL    L7C9E
            POP     DE
            POP     BC
            POP     AF
            RET     

L600B:      ADD     HL,DE
            RET     NC
            JR      L6011               ; (+002H)

L600F:      ADD     HL,HL
            RET     NC
L6011:      JP      04AEFH

L6014:      PUSH    DE
            PUSH    HL
            LD      HL,L84CF
            LD      E,A
            LD      D,000H
            CALL    L5E99
            EX      DE,HL
            POP     HL
            PUSH    BC
            CALL    L7985
            POP     BC
            POP     DE
            RET     

L6028:      LD      A,005H
            LD      (L84CE),A
            LD      A,(HL)
            OR      A
            JR      Z,L6035             ; (+004H)
            INC     HL
            LD      A,(HL)
            DEC     HL
            RLCA    
L6035:      LD      A,020H
            JR      NC,L603E            ; (+005H)
            CALL    L7972
            LD      A,02DH
L603E:      PUSH    AF
            CALL    L61AD
            CALL    L623A
            POP     AF
            DEC     DE
            LD      (DE),A
            RET     

L6049:      PUSH    BC
            CALL    L7A1C
            POP     BC
            RET     

L604F:      CALL    SKPDE
            LD      A,(HL)
            CP      081H
            JP      C,L7967
            LD      A,(HL)
            CP      0A0H
            RET     NC
            INC     HL
            CALL    L6086
            PUSH    HL
            LD      L,(HL)
            LD      B,000H
L6064:      SRL     E
            RR      D
            RR      C
            RR      L
            INC     A
            INC     B
            CP      0A0H
            JR      NZ,L6064            ; (-00eH)
L6072:      SLA     L
            RL      C
            RL      D
            RL      E
            DJNZ    L6072               ; (-00aH)
            LD      A,L
            POP     HL
            LD      (HL),A
            DEC     HL
            LD      (HL),C
            DEC     HL
            LD      (HL),D
            DEC     HL
            LD      (HL),E
            RET     

L6086:      LD      E,(HL)
            INC     HL
L6088:      LD      D,(HL)
            INC     HL
            LD      C,(HL)
            INC     HL
            RET     

L608D:      LD      A,(HL)
            CP      081H
            RET     C
            PUSH    DE
            PUSH    HL
            LD      DE,L60A5
            PUSH    DE
            CALL    L5E93
            POP     HL
            CALL    L604F
            EX      DE,HL
            POP     HL
            CALL    L797C
            POP     DE
            RET     

L60A5:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
L60AA:      PUSH    AF
            PUSH    BC
            PUSH    DE
            LD      DE,L610D
            CALL    L7BB3
            POP     DE
            POP     BC
            POP     AF
            RET     

L60B7:      INC     HL
            BIT     7,(HL)
            JR      Z,L60C7             ; (+00bH)
            CALL    L60C7
            LD      A,H
            CPL     
            LD      H,A
            LD      A,L
            CPL     
            LD      L,A
            INC     HL
            RET     

L60C7:      DEC     HL
            LD      A,(HL)
            CP      091H
            JP      NC,04AEFH
            CP      081H
            JR      C,L60E5             ; (+013H)
            PUSH    AF
            INC     HL
            LD      A,(HL)
            INC     HL
            LD      L,(HL)
            LD      H,A
            POP     AF
            SET     7,H
L60DB:      CP      090H
            RET     Z
            INC     A
            SRL     H
            RR      L
            JR      L60DB               ; (-00aH)

L60E5:      CCF     
            LD      HL,L0000
            RET     

L60EA:      SBC     A,E
            LD      A,0BCH
            JR      NZ,L60EF            ; (+000H)
L60EF:      SBC     A,B
            JR      L6088               ; (-06aH)

            ADD     A,B
            NOP     
            SUB     H
            LD      (HL),H
            INC     H
            NOP     
            NOP     
            SUB     C
            LD      B,E
            LD      D,B
            NOP     
            NOP     
            ADC     A,(HL)
            INC     E
            LD      B,B
            NOP     
            NOP     
            ADC     A,D
            LD      A,D
            NOP     
            NOP     
            NOP     
            ADD     A,A
            LD      C,B
            NOP     
            NOP     
            NOP     
L610D:      ADD     A,H
            JR      NZ,L6110            ; (+000H)
L6110:      NOP     
            NOP     
L6112:      ADD     A,C
            NOP     
            NOP     
            NOP     
            NOP     
            LD      A,L
            LD      C,H
            CALL    Z,0CDCCH
L611C:      LD      H,(HL)
            DEC     HL
            CALL    Z,L1277
L6121:      LD      HL,(L6304)
            LD      B,000H
L6126:      LD      DE,L610D
            CALL    L6049
            JR      C,L6134             ; (+006H)
            CALL    L5FFE
            INC     B
            JR      L6126               ; (-00eH)

L6134:      LD      DE,L6112
            CALL    L6049
            JR      NC,L6142            ; (+006H)
            CALL    L60AA
            DEC     B
            JR      L6134               ; (-00eH)

L6142:      PUSH    BC
            CALL    L61AD
            CALL    L623A
            POP     BC
            PUSH    DE
            EX      DE,HL
L614C:      LD      A,(HL)
            OR      A
            JR      Z,L6153             ; (+003H)
            INC     HL
            JR      L614C               ; (-007H)

L6153:      DEC     HL
            LD      A,(HL)
            INC     HL
            CP      030H
            JR      NZ,L615C            ; (+002H)
            INC     B
            DEC     HL
L615C:      LD      A,045H
            LD      (HL),A
            INC     HL
            LD      A,B
            LD      B,02BH
            BIT     7,A
            JR      Z,L616B             ; (+004H)
            NEG     
            LD      B,02DH
L616B:      LD      (HL),B
            INC     HL
            LD      (HL),030H
L616F:      SUB     00AH
            JR      C,L6176             ; (+003H)
            INC     (HL)
            JR      L616F               ; (-007H)

L6176:      ADD     A,03AH
            INC     HL
            LD      (HL),A
            INC     HL
            LD      (HL),000H
            POP     DE
            RET     

L617F:      PUSH    HL
            CALL    L60B7
            LD      DE,L84A7
            PUSH    DE
            LD      B,001H
            RST     018H
            DB      016H
            POP     HL
            LD      A,030H
            LD      B,005H
L6190:      CP      (HL)
            JR      NZ,L6198            ; (+005H)
            INC     HL
            DJNZ    L6190               ; (-006H)
            JR      L61A1               ; (+009H)

L6198:      LD      A,B
            LD      (L6301),A
            LD      A,001H
            LD      (L6302),A
L61A1:      LD      A,02EH
            LD      (L84AC),A
            POP     HL
            CALL    L608D
            JP      L61FD

L61AD:      LD      (L6304),HL
            XOR     A
            LD      (L6301),A
            LD      (L6302),A
            PUSH    HL
            LD      HL,L84A3
            LD      (HL),0FFH
            LD      B,021H
            LD      A,030H
L61C1:      INC     HL
            LD      (HL),A
            DJNZ    L61C1               ; (-004H)
            LD      A,02EH
            LD      (L84AC),A
            POP     HL
            LD      A,(HL)
            OR      A
            RET     Z
            LD      DE,L60EA
            CALL    L7A1C
            CCF     
            RET     C
            LD      DE,L611C
            CALL    L7A1C
            RET     C
            LD      DE,L84D7
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      A,(HL)
            CP      081H
            JR      C,L61FD             ; (+013H)
            CP      090H
            JP      C,L617F
            LD      IX,L84A4
            LD      DE,L60EF
            CALL    L62C4
            CALL    L62F8
            RET     NC
L61FD:      LD      IX,L84AD
L6201:      LD      DE,L60EA
            PUSH    BC
            PUSH    IX
            CALL    L7BB3
            POP     IX
            POP     BC
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            CALL    L62C4
            CALL    L62F8
            JR      C,L6201             ; (-019H)
            RET     

L621B:      LD      A,005H
            LD      (L84CE),A
            PUSH    HL
            LD      DE,L84DF
            PUSH    DE
            CALL    L5E93
            POP     HL
            CALL    L61AD
            CALL    L6237
            POP     HL
            RET     

L6231:      LD      DE,L84AC
            LD      (DE),A
            DEC     DE
            RET     

L6237:      JP      C,L6121
L623A:      LD      HL,(L6304)
            LD      A,(HL)
            OR      A
            JR      Z,L6231             ; (-010H)
            LD      DE,L84A4
            DEC     DE
            EX      DE,HL
            LD      DE,00001H
L6249:      INC     HL
            LD      A,(HL)
            CP      02EH
            JR      NZ,L6254            ; (+005H)
            LD      DE,L0000
            JR      L6249               ; (-00bH)

L6254:      CP      030H
            JR      Z,L6249             ; (-00fH)
            ADD     HL,DE
            LD      DE,00008H
            ADD     HL,DE
            LD      A,(HL)
L625E:      LD      (HL),030H
            CP      035H
            JR      C,L6273             ; (+00fH)
L6264:      DEC     HL
            LD      A,(HL)
            CP      02EH
            JR      Z,L6264             ; (-006H)
            INC     A
            JR      Z,L62AD             ; (+040H)
            LD      (HL),A
            CP      03AH
            JR      Z,L625E             ; (-014H)
            INC     HL
L6273:      LD      DE,L84AC
            EX      DE,HL
            OR      A
            SBC     HL,DE
            EX      DE,HL
            JR      C,L6282             ; (+005H)
            LD      HL,L84AC
            JR      L6299               ; (+017H)

L6282:      DEC     HL
            LD      A,(HL)
            CP      02EH
            DEC     HL
            JR      Z,L6298             ; (+00fH)
            INC     HL
            CP      030H
            JR      Z,L6273             ; (-01bH)
            PUSH    HL
            LD      DE,L84B5
            SBC     HL,DE
            POP     HL
            JP      NC,L6121
L6298:      INC     HL
L6299:      LD      (HL),000H
            LD      DE,L84A4
L629E:      LD      A,(DE)
            CP      030H
            JR      NZ,L62A6            ; (+003H)
            INC     DE
            JR      L629E               ; (-008H)

L62A6:      OR      A
            RET     NZ
            DEC     DE
            LD      A,030H
            LD      (DE),A
            RET     

L62AD:      LD      HL,L849C
            LD      DE,L62BE
            PUSH    BC
            LD      BC,L0006
            LDIR    
            POP     BC
            LD      DE,L849C
            RET     

L62BE:      LD      SP,L2B45
            JR      NC,L62FB            ; (+038H)
            NOP     
L62C4:      LD      A,(DE)
            CP      07DH
            RET     Z
L62C8:      CALL    L7A1C
            JR      C,L62E0             ; (+013H)
            INC     (IX+000H)
            PUSH    IX
            PUSH    BC
            CALL    L797C
            POP     BC
            POP     IX
            LD      A,001H
            LD      (L6302),A
            JR      L62C8               ; (-018H)

L62E0:      INC     IX
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            LD      A,(L6302)
            LD      B,A
            LD      A,(L6301)
            ADD     A,B
            LD      (L6301),A
            CALL    L62F8
            RET     NC
            JR      L62C4               ; (-034H)

L62F8:      LD      A,(HL)
            OR      A
            RET     Z
L62FB:      LD      A,(L6301)
            CP      009H
            RET     

L6301:      NOP     
L6302:      NOP     
L6303:      NOP     
L6304:      NOP     
            NOP     
L6306:      NOP     
            NOP     
L6308:      NOP     
            NOP     
L630A:      LD      HL,(L6308)
            PUSH    DE
L630E:      LD      A,(HL)
            OR      A
            JP      Z,04AF2H
            CALL    L63AC
            JR      Z,L631D             ; (+005H)
            LD      (BC),A
            INC     BC
            INC     HL
            JR      L630E               ; (-00fH)

L631D:      EX      AF,AF'
            LD      A,(L84CE)
            CP      003H
            JP      NZ,L63D5
            EX      AF,AF'
            CP      021H
            JP      Z,L6334
            CP      026H
            JP      Z,L635B
            JP      04AF5H

L6334:      EX      (SP),HL
            PUSH    BC
            CALL    L6E7E
            OR      A
            LD      A,020H
            JR      Z,L633F             ; (+001H)
            LD      A,(DE)
L633F:      POP     BC
            LD      (BC),A
            INC     BC
L6342:      POP     HL
            INC     HL
L6344:      LD      A,(HL)
            OR      A
            JR      Z,L6352             ; (+00aH)
            CALL    L63AC
            JR      Z,L6355             ; (+008H)
            LD      (BC),A
            INC     BC
            INC     HL
            JR      L6344               ; (-00eH)

L6352:      LD      HL,(L6306)
L6355:      LD      (L6308),HL
            XOR     A
            LD      (BC),A
            RET     

L635B:      LD      D,002H
L635D:      INC     HL
            LD      A,(HL)
            CP      026H
            JR      Z,L636B             ; (+008H)
            INC     D
            CP      020H
            JR      Z,L635D             ; (-00bH)
            JP      04AF2H

L636B:      EX      (SP),HL
            LD      A,(HL)
            CP      D
            JR      C,L6385             ; (+015H)
            INC     HL
            CALL    INDRCT
            PUSH    BC
            LD      BC,(STRST)
            ADD     HL,BC
            POP     BC
L637B:      LD      A,(HL)
            LD      (BC),A
            INC     BC
            INC     HL
            DEC     D
            JR      NZ,L637B            ; (-007H)
            JP      L6342

L6385:      LD      E,(HL)
            INC     HL
            CALL    INDRCT
            LD      A,E
            OR      A
            JR      Z,L63A2             ; (+014H)
            PUSH    BC
            LD      BC,(STRST)
            ADD     HL,BC
            POP     BC
L6395:      LD      A,(HL)
            LD      (BC),A
            INC     BC
            INC     HL
            DEC     E
            JR      Z,L63A6             ; (+00aH)
            DEC     D
            JR      NZ,L6395            ; (-00aH)
            JP      L6342

L63A2:      LD      A,020H
            LD      (BC),A
            INC     BC
L63A6:      DEC     D
            JR      NZ,L63A2            ; (-007H)
            JP      L6342

L63AC:      CALL    CHKACC
            INC     B
            LD      HL,L2326
            DEC     HL
            RET     Z
            LD      E,A
            CP      02AH
            JR      Z,L63CF             ; (+015H)
            CALL    L65AC
            JR      Z,L63CF             ; (+010H)
            CP      02EH
            LD      E,023H
            JR      Z,L63CF             ; (+00aH)
            CP      0C4H
            RET     NZ
            INC     HL
            LD      A,(HL)
            OR      A
            JP      Z,04AF2H
            RET     

L63CF:      INC     HL
            LD      A,(HL)
            CP      E
            DEC     HL
            LD      A,(HL)
            RET     

L63D5:      XOR     A
            LD      (0651CH),A
            LD      (0656CH),A
            LD      (L6554),A
            LD      (06512H),A
            LD      (064CCH),A
            LD      (064F9H),A
            LD      (06517H),A
            LD      (064C5H),A
            DEC     A
            LD      (064C2H),A
            EX      AF,AF'
            LD      D,000H
            CP      023H
            JP      Z,L6431
            CP      02AH
            JP      Z,L6412
            CALL    L65AC
            JP      Z,L6423
            CP      02EH
            JP      Z,L6433
            CP      02BH
            JP      Z,L642C
            JP      04AF5H

L6412:      LD      A,001H
            LD      (0656CH),A
            INC     HL
            INC     D
            INC     HL
            INC     D
            LD      A,(HL)
            CALL    L65AC
            JR      NZ,L6433            ; (+012H)
            JR      L6425               ; (+002H)

L6423:      INC     HL
            INC     D
L6425:      INC     HL
            INC     D
            LD      (L6554),A
            JR      L6433               ; (+007H)

L642C:      LD      A,001H
            LD      (0651CH),A
L6431:      INC     HL
            INC     D
L6433:      EX      DE,HL
            DEC     H
L6435:      INC     H
            LD      A,(DE)
            INC     DE
            CP      023H
            JR      Z,L6435             ; (-007H)
            CP      02CH
            JR      NZ,L6447            ; (+007H)
            LD      A,001H
            LD      (064CCH),A
            JR      L6435               ; (-012H)

L6447:      CP      02EH
            JR      Z,L646E             ; (+023H)
            CP      02DH
            JR      NZ,L6457            ; (+008H)
            LD      A,001H
            LD      (06512H),A
            INC     DE
            JR      L6467               ; (+010H)

L6457:      CP      02BH
            JR      NZ,L6467            ; (+00cH)
            LD      A,(0651CH)
            OR      A
            JR      NZ,L6467            ; (+006H)
            LD      A,001H
            LD      (06517H),A
            INC     DE
L6467:      DEC     DE
            LD      A,H
            LD      (064F9H),A
            JR      L64A1               ; (+033H)

L646E:      LD      A,H
            LD      (064F9H),A
            LD      H,0FFH
L6474:      INC     H
            LD      A,(DE)
            INC     DE
            CP      023H
            JR      Z,L6474             ; (-007H)
            CP      02DH
            JR      NZ,L6487            ; (+008H)
            LD      A,001H
            LD      (06512H),A
            INC     DE
            JR      L6497               ; (+010H)

L6487:      CP      02BH
            JR      NZ,L6497            ; (+00cH)
            LD      A,(0651CH)
            OR      A
            JR      NZ,L6497            ; (+006H)
            LD      A,001H
            LD      (06517H),A
            INC     DE
L6497:      DEC     DE
            LD      A,H
            LD      (064C2H),A
            JR      L64A1               ; (+003H)

L649E:      POP     DE
            JR      L64B3               ; (+012H)

L64A1:      LD      H,004H
            PUSH    DE
L64A4:      LD      A,(DE)
            INC     DE
            CP      05EH
            JR      NZ,L649E            ; (-00cH)
            DEC     H
            JR      NZ,L64A4            ; (-009H)
            POP     AF
            LD      A,001H
            LD      (064C5H),A
L64B3:      POP     HL
            PUSH    DE
            INC     HL
            LD      A,(HL)
            RES     7,(HL)
            DEC     HL
            LD      (06549H),A
            LD      A,(064F9H)
            LD      D,A
            LD      A,000H
            LD      E,A
            LD      A,000H
            PUSH    BC
            CALL    L65BE
            POP     BC
            LD      A,000H
            OR      A
            JR      Z,L6511             ; (+041H)
            PUSH    BC
            PUSH    DE
            LD      A,(064F9H)
            LD      L,A
            LD      H,000H
            ADD     HL,DE
            LD      DE,L849C
            LD      C,000H
            DEC     HL
L64DF:      LD      B,003H
L64E1:      LD      A,(HL)
            CP      020H
            JR      Z,L64F8             ; (+012H)
            INC     C
            LD      (DE),A
            INC     DE
            DEC     HL
            DJNZ    L64E1               ; (-00bH)
            LD      A,(HL)
            CP      020H
            JR      Z,L64F8             ; (+007H)
            LD      A,02CH
            LD      (DE),A
            INC     DE
            INC     C
            JR      L64DF               ; (-019H)

L64F8:      LD      A,000H
            CP      C
            JP      C,04AEFH
            LD      B,C
            LD      L,A
            LD      H,000H
            POP     DE
            PUSH    DE
            ADD     HL,DE
            LD      DE,L849C
            DEC     HL
L6509:      LD      A,(DE)
            LD      (HL),A
            DEC     HL
            INC     DE
            DJNZ    L6509               ; (-006H)
            POP     DE
            POP     BC
L6511:      LD      A,000H
            OR      A
            JR      NZ,L6553            ; (+03dH)
            LD      A,000H
            OR      A
            JR      NZ,L6553            ; (+038H)
            LD      A,000H
            OR      A
            JR      NZ,L6548            ; (+028H)
            LD      A,(06549H)
            RLCA    
            JR      NC,L6553            ; (+02dH)
            LD      A,(DE)
            CP      020H
            LD      H,02DH
            JR      Z,L6532             ; (+005H)
            CP      030H
            JP      NZ,04AEFH
L6532:      PUSH    DE
L6533:      LD      A,(DE)
            INC     DE
            CP      020H
            JR      Z,L6533             ; (-006H)
            CP      030H
            JR      Z,L6533             ; (-00aH)
            OR      A
            JR      NZ,L6541            ; (+001H)
            DEC     DE
L6541:      DEC     DE
            DEC     DE
            LD      A,H
            LD      (DE),A
            POP     DE
            JR      L6553               ; (+00bH)

L6548:      LD      A,000H
            RLCA    
            LD      H,02BH
            JR      NC,L6532            ; (-01dH)
            LD      H,02DH
            JR      L6532               ; (-021H)

L6553:      DB      03EH                ; LD A,nn opcode (self-modifying: operand at L6554)
L6554:      DB      000H                ; Operand byte — patched by LD (L6554),A
            OR      A
            JR      Z,L656B             ; (+013H)
            LD      A,(DE)
            CP      020H
            JR      NZ,L656B            ; (+00eH)
            PUSH    DE
L655E:      LD      A,(DE)
            INC     DE
            CP      020H
            JR      Z,L655E             ; (-006H)
            DEC     DE
            DEC     DE
            LD      A,(L6554)
            LD      (DE),A
            POP     DE
L656B:      LD      A,000H
            OR      A
            JR      Z,L657D             ; (+00dH)
            PUSH    DE
L6571:      LD      A,(DE)
            CP      020H
            JR      NZ,L657C            ; (+006H)
            LD      A,02AH
            LD      (DE),A
            INC     DE
            JR      L6571               ; (-00bH)

L657C:      POP     DE
L657D:      LD      A,(DE)
            OR      A
            JR      Z,L6586             ; (+005H)
            LD      (BC),A
            INC     BC
            INC     DE
            JR      L657D               ; (-009H)

L6586:      LD      A,(06517H)
            OR      A
            JR      Z,L659A             ; (+00eH)
            LD      A,(06549H)
            RLCA    
            LD      A,02BH
L6592:      JR      NC,L6596            ; (+002H)
            LD      A,02DH
L6596:      LD      (BC),A
            INC     BC
            JR      L65A8               ; (+00eH)

L659A:      LD      A,(06512H)
            OR      A
            JR      Z,L65A8             ; (+008H)
            LD      A,(06549H)
            RLCA    
            LD      A,020H
            JR      L6592               ; (-016H)

L65A8:      POP     HL
            JP      L6344

L65AC:      PUSH    HL
            LD      HL,L0036
            BIT     6,(HL)
            POP     HL
            JR      NZ,L65B8            ; (+003H)
            CP      07DH
            RET     

L65B8:      CP      FDPD
            RET     Z
            CP      024H
            RET     

L65BE:      OR      A
            JP      Z,L6649
            PUSH    DE
            LD      A,(HL)
            OR      A
            JR      Z,L6606             ; (+03fH)
            PUSH    HL
            LD      A,D
            LD      DE,L84D7
            PUSH    AF
            CALL    L5E90
            POP     AF
            OR      A
            JR      Z,L65DF             ; (+00bH)
            LD      B,A
            LD      HL,L84D7
            JR      L65DD               ; (+003H)

L65DA:      CALL    L60AA
L65DD:      DJNZ    L65DA               ; (-005H)
L65DF:      POP     HL
            LD      B,000H
L65E2:      LD      DE,L84D7
            CALL    L6049
            JR      C,L65F0             ; (+006H)
            CALL    L5FFE
            INC     B
            JR      L65E2               ; (-00eH)

L65F0:      PUSH    HL
            LD      HL,L84D7
            CALL    L5FFE
            POP     HL
L65F8:      LD      DE,L84D7
            CALL    L6049
            JR      NC,L6606            ; (+006H)
            CALL    L60AA
            DEC     B
            JR      L65F8               ; (-00eH)

L6606:      POP     DE
            PUSH    BC
            CALL    L6663
            POP     BC
            PUSH    DE
            LD      A,(DE)
            CP      02EH
            JR      NZ,L6624            ; (+012H)
            LD      DE,L84AC
            DEC     DE
            LD      A,(DE)
            CP      031H
            POP     DE
            PUSH    DE
            JR      NZ,L6640            ; (+023H)
            EX      DE,HL
            INC     HL
            LD      (HL),031H
            DEC     HL
            JR      L663E               ; (+01aH)

L6624:      LD      A,(DE)
            CP      031H
            JR      NZ,L6640            ; (+017H)
            EX      DE,HL
            INC     HL
            LD      A,(HL)
            DEC     HL
            CP      02EH
            LD      A,030H
            JR      Z,L6635             ; (+002H)
            LD      A,020H
L6635:      LD      (HL),A
L6636:      INC     HL
            LD      A,(HL)
            CP      02EH
            JR      Z,L6636             ; (-006H)
            LD      (HL),031H
L663E:      EX      DE,HL
            INC     B
L6640:      LD      A,(DE)
            INC     DE
            OR      A
            JR      NZ,L6640            ; (-005H)
            DEC     DE
            JP      L615C

L6649:      PUSH    DE
            CALL    L6663
            POP     AF
            OR      A
            RET     NZ
            PUSH    HL
            LD      HL,L84AC
            DEC     HL
            LD      A,030H
            CP      (HL)
L6658:      JP      NZ,04AEFH
            DEC     HL
            LD      A,020H
            CP      (HL)
            JR      NZ,L6658            ; (-009H)
            POP     HL
            RET     

L6663:      PUSH    DE
            CALL    L61AD
            JP      C,04AEFH
            POP     HL
            PUSH    HL
            LD      H,000H
            INC     L
            JR      Z,L6672             ; (+001H)
            DEC     L
L6672:      LD      DE,L84AD
            ADD     HL,DE
            LD      A,(HL)
            LD      (HL),000H
            LD      DE,L3036
            CP      035H
            JR      C,L668F             ; (+00fH)
L6680:      DEC     HL
            LD      A,(HL)
            CP      02EH
            JR      Z,L6680             ; (-006H)
            INC     A
            JP      Z,04AEFH
            LD      (HL),A
            CP      03AH
            JR      Z,0667AH             ; (-015H)
L668F:      LD      HL,KEYBM1
            LD      DE,L2000
L6695:      LD      (HL),D
            INC     HL
            DEC     E
            JR      NZ,L6695            ; (-005H)
            POP     HL
            PUSH    HL
            LD      E,H
            LD      D,000H
            LD      HL,KEYBUF
            ADD     HL,DE
            PUSH    HL
            LD      HL,L84A3
            LD      (HL),020H
            INC     HL
            LD      D,007H
L66AC:      LD      A,(HL)
            CP      030H
            JR      NZ,L66B7            ; (+006H)
            LD      (HL),020H
            INC     HL
            DEC     D
            JR      NZ,L66AC            ; (-00bH)
L66B7:      POP     HL
            PUSH    HL
            LD      B,E
            LD      A,B
            OR      A
            JR      Z,L66D5             ; (+017H)
            LD      DE,L84AC
L66C1:      DEC     HL
            DEC     DE
            LD      A,(DE)
            LD      (HL),A
            CP      020H
            JR      Z,L66D5             ; (+00cH)
            DJNZ    L66C1               ; (-00aH)
            DEC     DE
            LD      A,(DE)
            CP      020H
            JR      Z,L66D5             ; (+004H)
            INC     A
            JP      NZ,04AEFH
L66D5:      POP     HL
            POP     DE
            INC     E
            JR      Z,L66E9             ; (+00fH)
            LD      B,E
            LD      DE,L84AD
            LD      (HL),02EH
L66E0:      INC     HL
            DEC     B
            JR      Z,L66E9             ; (+005H)
            LD      A,(DE)
            INC     DE
            LD      (HL),A
            JR      L66E0               ; (-009H)

L66E9:      LD      (HL),000H
            LD      DE,KEYBUF
            RET     

L66EF:      PUSH    DE
            PUSH    BC
            LD      C,000H
            DEC     DE
L66F4:      INC     DE
L66F5:      CALL    L687B
            OR      A
            JR      Z,L672C             ; (+031H)
            CP      0FFH
            JR      Z,L6730             ; (+031H)
            CP      060H
            JP      NC,L4AEC
            CP      020H
            JR      C,L66F4             ; (-014H)
            LD      IX,L66F5
            PUSH    IX
            CP      022H
            JR      Z,L674B             ; (+039H)
            CP      027H
            JR      Z,L673E             ; (+028H)
            CP      03FH
            JR      Z,L6737             ; (+01dH)
            CP      02EH
            JP      Z,L689E
            CP      024H
            JP      Z,L68B8
            CALL    L5EC4
            JR      C,L6773             ; (+04aH)
            JP      L6885

L672C:      LD      (HL),A
            POP     BC
            POP     DE
            RET     

L6730:      LD      (HL),LSE4
            CALL    L6757
            JR      L66F5               ; (-042H)

L6737:      LD      A,08FH
            CALL    L6756
            JR      L67B4               ; (+076H)

L673E:      LD      (HL),03AH
            CALL    L6757
            LD      (HL),027H
            CALL    L6758
            JP      L681B

L674B:      LD      (HL),A
            CALL    L6757
L674F:      LD      A,(DE)
            OR      A
            RET     Z
            CP      022H
            JR      NZ,L674B            ; (-00bH)
L6756:      LD      (HL),A
L6757:      INC     DE
L6758:      INC     HL
L6759:      INC     C
            RET     NZ
            JP      04B01H

L675E:      POP     BC
            LD      A,(DE)
            CALL    L5ECB
            JR      C,L6756             ; (-00fH)
L6765:      CALL    L6756
            LD      A,(DE)
            CP      024H
            JR      Z,L6756             ; (-017H)
            CALL    L5EC4
            RET     C
            JR      L6765               ; (-00eH)

L6773:      PUSH    BC
            LD      BC,STMTWORDTBL
            CALL    L682A
            JR      NC,L67AA            ; (+02eH)
            LD      BC,EXTWORDTBL
            CALL    L682A
            LD      C,0FEH
            JR      NC,L6790            ; (+00aH)
            LD      BC,FUNCWORDTBL
            CALL    L682A
            LD      C,0FFH
            JR      C,L675E             ; (-032H)
L6790:      LD      (HL),C
            INC     HL
            LD      (HL),A
            POP     BC
            CALL    L6759
            CALL    L6758
            CP      0B4H
            RET     NZ
            CALL    L687B
            CP      03DH
            RET     NZ
            LD      (HL),QDPC
            CALL    L6757
            JR      L67F0               ; (+046H)

L67AA:      POP     BC
            CP      0DFH
            DB      020H
            LD      (BC),A
            LD      (HL),0A8H
            CALL    L6758
L67B4:      CP      097H
            JR      Z,L681B             ; (+063H)
            CP      094H
            JR      Z,L681B             ; (+05fH)
            CP      0C2H
            JR      Z,L6811             ; (+051H)
            CP      LSE2
            JR      Z,L67F0             ; (+02cH)
            CP      LSE0
            RET     NC
            PUSH    AF
            CALL    L687B
            CP      02FH
            JR      NZ,L67DC            ; (+00dH)
            LD      (HL),FDPD
            CALL    L6757
            CALL    L6A1D
            CALL    L6756
            JR      L67EC               ; (+010H)

L67DC:      CP      023H
            JR      NZ,L67EC            ; (+00cH)
            CALL    L6756
            CALL    L6A1D
            CALL    L5EC4
            CALL    NC,L6885
L67EC:      POP     AF
            CP      08DH
            RET     NC
L67F0:      CALL    L687B
            CP      022H
            JR      NZ,L67FC            ; (+005H)
            CALL    L674B
            JR      L67F0               ; (-00cH)

L67FC:      CP      02CH
            RET     C
            CP      02FH
            JR      C,L680C             ; (+009H)
            CALL    L5EC4
            RET     C
            CALL    L68AF
            JR      L67F0               ; (-01cH)

L680C:      CALL    L6756
            JR      L67F0               ; (-021H)

L6811:      DEC     HL
            LD      (HL),03AH           ; Store ':' at previous position
            INC     HL
            LD      (HL),A
            CALL    L6758
            JR      L67F0               ; (-02bH)

L681B:      LD      A,(DE)
            CALL    L4141
            RET     Z
            CALL    L6756
            CP      022H
            CALL    Z,L674F
            JR      L681B               ; (-00fH)

L682A:      PUSH    HL
            PUSH    DE
            LD      H,B
            LD      L,C
            LD      B,07FH
L6830:      POP     DE
            PUSH    DE
            INC     B
            LD      A,(HL)
            CP      0FFH
            JR      NZ,L683C            ; (+004H)
            POP     DE
            POP     HL
            SCF     
            RET     

L683C:      CP      02EH
            JR      NZ,L6849            ; (+009H)
            INC     HL
            DEC     DE
            LD      A,(DE)
            INC     DE
            CALL    L5ECB
            JR      NC,L6868            ; (+01fH)
L6849:      LD      A,(DE)
            CP      020H
            JR      NZ,L685A            ; (+00cH)
            LD      A,(HL)
            AND     07FH
            SUB     041H
            CP      01AH
            JR      C,L6868             ; (+011H)
            CALL    L6A1C
L685A:      LD      C,(HL)
            INC     HL
            INC     DE
            CP      02EH
            JR      Z,L6870             ; (+00fH)
            SUB     C
            JR      Z,L6849             ; (-01bH)
            CP      080H
            JR      Z,L6876             ; (+00eH)
L6868:      DEC     HL
L6869:      BIT     7,(HL)
            INC     HL
            JR      Z,L6869             ; (-005H)
            JR      L6830               ; (-040H)

L6870:      LD      A,B
            CP      0E8H
            JR      NC,L6868            ; (-00dH)
            CCF     
L6876:      POP     HL
            POP     HL
            LD      (HL),B
            LD      A,B
            RET     

L687B:      LD      A,(DE)
            CP      020H
            RET     NZ
            LD      (HL),A
            CALL    L6757
            JR      L687B               ; (-00aH)

L6885:      EX      AF,AF'
            PUSH    DE
            CALL    L6A1C
            POP     DE
            CALL    L5EC4
            JR      NC,L689E            ; (+00eH)
            CP      02EH
            JR      Z,L689E             ; (+00aH)
            CP      045H
            JR      Z,L689E             ; (+006H)
            EX      AF,AF'
            SUB     02FH
            JP      L689E

L689E:      PUSH    BC
            LD      (HL),015H
            INC     HL
            PUSH    HL
            CALL    L5EDC
            POP     HL
            LD      BC,00005H
            ADD     HL,BC
            LD      A,006H
            JR      L68D2               ; (+023H)

L68AF:      PUSH    BC
            CALL    L6A11
            LD      (HL),00BH
            INC     HL
            JR      L68CC               ; (+014H)

L68B8:      LD      (HL),A
            INC     DE
            LD      A,(DE)
            RST     018H
            DB      015H
            JP      C,L6758
            PUSH    BC
            LD      (HL),011H
            INC     HL
            PUSH    HL
            EX      DE,HL
            RST     018H
            DB      014H
            LD      B,D
            LD      C,E
            EX      DE,HL
            POP     HL
L68CC:      LD      A,003H
            LD      (HL),C
            INC     HL
            LD      (HL),B
            INC     HL
L68D2:      POP     BC
            ADD     A,C
            JP      C,04B01H
            LD      C,A
L68D8:      DEC     DE
            LD      A,(DE)
            CP      020H
            JR      Z,L68D8             ; (-006H)
            INC     DE
            RET     

L68E0:      PUSH    HL
            PUSH    DE
            PUSH    BC
            EXX     
            LD      B,000H
            EXX     
            LD      C,000H
L68E9:      LD      A,(HL)
            OR      A
            JR      Z,L691D             ; (+030H)
            LD      BC,L68E9
            PUSH    BC
            CP      027H
            JR      Z,L6937             ; (+042H)
            INC     HL
            LD      BC,STMTWORDTBL
            CP      020H
            JR      C,L695F             ; (+062H)
            CP      022H
            JR      Z,L6948             ; (+047H)
            CP      03AH
            JR      Z,L6955             ; (+050H)
            CP      097H
            JR      Z,L6933             ; (+02aH)
            CP      094H
            JR      Z,L6933             ; (+026H)
            CP      LSE4
            JR      Z,L6922             ; (+011H)
            CP      0FEH
            JR      NC,L6927            ; (+012H)
            CP      080H
            JP      NC,L69A3
            JP      L69C0

L691D:      LD      (DE),A
            POP     BC
            POP     DE
            POP     HL
            RET     

L6922:      LD      A,0FFH
            JP      L69C0

L6927:      LD      BC,FUNCWORDTBL
            JR      NZ,L692F            ; (+003H)
            LD      BC,EXTWORDTBL
L692F:      LD      A,(HL)
            INC     HL
            JR      L69A3               ; (+070H)

L6933:      CALL    L69A3
            RET     Z
L6937:      LD      A,(HL)
            CALL    L4141
            RET     Z
            CALL    L69C0
            LD      A,(HL)
            INC     HL
            CP      022H
            CALL    Z,L694B
            JR      L6937               ; (-011H)

L6948:      CALL    L69C0
L694B:      LD      A,(HL)
            OR      A
            RET     Z
            INC     HL
            CP      022H
            JR      NZ,L6948            ; (-00bH)
            JR      L69C0               ; (+06bH)

L6955:      LD      (DE),A
            LD      A,(HL)
            CP      0C2H
            RET     Z
            CP      027H
            RET     Z
            JR      L69C3               ; (+064H)

L695F:      CP      015H
            JR      Z,L6993             ; (+030H)
            CP      00BH
            JR      NC,L696C            ; (+005H)
            DEC     A
            OR      030H
            JR      L69C0               ; (+054H)

L696C:      PUSH    DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            PUSH    HL
            CP      012H
            JR      Z,L6982             ; (+00cH)
            CP      00CH
            JR      C,L6982             ; (+008H)
            JR      NZ,L69CF            ; (+053H)
            EX      DE,HL
            INC     HL
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
L6982:      EX      DE,HL
            CALL    L5E66
            LD      B,D
            LD      C,E
            POP     HL
            POP     DE
L698A:      LD      A,(BC)
            OR      A
            RET     Z
            CALL    L69C0
            INC     BC
            JR      L698A               ; (-009H)

L6993:      PUSH    HL
            PUSH    DE
            CALL    L621B
            LD      B,D
            LD      C,E
            POP     DE
            POP     HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            JR      L698A               ; (-019H)

L69A3:      CP      080H
            JR      Z,L69B1             ; (+00aH)
            EX      AF,AF'
L69A8:      LD      A,(BC)
            RLCA    
            INC     BC
            JR      NC,L69A8            ; (-005H)
            EX      AF,AF'
            DEC     A
            JR      L69A3               ; (-00eH)

L69B1:      LD      A,(BC)
            BIT     7,A
            JR      NZ,L69BE            ; (+008H)
            CP      02EH
            CALL    NZ,L69C0
            INC     BC
            JR      L69B1               ; (-00dH)

L69BE:      AND     07FH
L69C0:      LD      (DE),A
            OR      A
            RET     Z
L69C3:      INC     DE
            EXX     
            INC     B
            EXX     
            RET     NZ
            XOR     A
            LD      (DE),A
            DEC     DE
            EXX     
            DEC     B
            EXX     
            RET     

L69CF:      LD      A,024H
            EX      AF,AF'
            EX      DE,HL
            CALL    L69E0
            LD      B,D
            LD      C,E
            POP     HL
            POP     DE
            EX      AF,AF'
            CALL    L69C0
            JR      L698A               ; (-056H)

L69E0:      LD      DE,L84A8
            PUSH    DE
            LD      A,H
            CALL    L69F9
            LD      A,L
            CALL    L69F9
            XOR     A
            LD      (DE),A
            POP     DE
            LD      B,003H
L69F1:      LD      A,(DE)
            CP      030H
            RET     NZ
            INC     DE
            DJNZ    L69F1               ; (-007H)
            RET     

L69F9:      PUSH    AF
            RRCA    
            RRCA    
            RRCA    
            RRCA    
            AND     00FH
            CALL    L6A06
            POP     AF
            AND     00FH
L6A06:      ADD     A,030H
            CP      03AH
            JR      C,L6A0E             ; (+002H)
            ADD     A,007H
L6A0E:      LD      (DE),A
            INC     DE
            RET     

L6A11:      PUSH    HL
            EX      DE,HL
            RST     018H
            DB      013H
            LD      B,D
            LD      C,E
            EX      DE,HL
            POP     HL
            JP      L68D8

L6A1C:      INC     DE
L6A1D:      LD      A,(DE)
            CP      020H
            JR      Z,L6A1C             ; (-006H)
            RET     

L6A23:      LD      C,(HL)
L6A24:      CALL    L6A2C
            XOR     A
            LD      (LPOSB),A
            RET     

L6A2C:      LD      A,(L004D)
            OR      A
            JP      NZ,L4B3C
            CALL    TEST1
            DB      047H
            JR      Z,L6A7C             ; (+043H)
            CALL    TESTX
            DB      054H
            LD      A,001H
            LD      (PNMODE),A
            CALL    TEST1
            DB      04EH
            JR      Z,L6A52             ; (+00aH)
            CALL    TEST1
            DB      04CH
            JR      Z,L6A52             ; (+004H)
            CALL    TESTX
            DB      053H
L6A52:      LD      (L6A23),A
            CALL    05E48H
            CALL    L6A74
L6A5B:      LD      A,001H
            RST     018H
            DB      006H
            LD      A,(L6A23)
            CP      04EH
            RET     Z
            CP      04CH
            LD      A,00BH
            JR      Z,L6A71             ; (+006H)
            LD      A,009H
            RST     018H
            DB      006H
            RST     018H
            DB      006H
L6A71:      RST     018H
            DB      006H
            RET     

L6A74:      LD      A,00AH
            RST     018H
            DB      006H
            LD      A,003H
            JR      L6A71               ; (-00bH)

L6A7C:      INC     HL
            CALL    05E48H
            LD      A,002H
            LD      (PNMODE),A
            JR      L6A71               ; (-016H)

L6A87:      CALL    L6AC6
            CALL    L6E5F
            LD      A,E
            OR      A
            RET     Z
            CP      0ECH
            JR      NC,L6A99            ; (+005H)
            CP      015H
            JP      NC,04AF2H
L6A99:      CALL    05E48H
            BIT     7,E
            JR      NZ,L6AA8            ; (+008H)
L6AA0:      LD      A,00AH
            RST     018H
            DB      006H
            DEC     E
            JR      NZ,L6AA0            ; (-007H)
            RET     

L6AA8:      LD      A,003H
            RST     018H
            DB      006H
            INC     E
            JR      NZ,L6AA8            ; (-007H)
            RET     

L6AB0:      PUSH    HL
            LD      HL,L03E7
            JR      L6ABA               ; (+004H)

L6AB6:      PUSH    HL
            LD      HL,L01E0
L6ABA:      PUSH    HL
            ADD     HL,DE
            POP     HL
            JR      C,L6AC4             ; (+005H)
            SBC     HL,DE
            JP      C,04AF2H
L6AC4:      POP     HL
            RET     

L6AC6:      LD      B,001H
            JR      L6ACC               ; (+002H)

L6ACA:      LD      B,002H
L6ACC:      LD      A,(PNMODE)
            CP      B
            RET     Z
            JP      L4B3C

L6AD4:      LD      C,044H
            LD      DE,04A0EH
            LD      DE,04D0EH
            LD      DE,0520EH
            CALL    L6ACA
            LD      A,C
            LD      (06B26H),A
            CP      04DH
            JR      NC,L6B0C            ; (+022H)
            CALL    TEST1
            DB      025H
            JR      NZ,L6B0C            ; (+01cH)
            CALL    L6E4B
            LD      A,E
            DEC     A
            CP      010H
            JP      NC,04AF2H
            DEC     DE
            LD      A,04CH
            RST     018H
            DB      006H
            CALL    L6DE2
            CALL    L6B68
            CALL    L413E
            RET     Z
            CALL    L5E73
L6B0C:      CALL    L6E5F
            CALL    L5E73
            CALL    L6AB6
            PUSH    DE
            CALL    L6E5F
            CP      02CH
            JR      Z,L6B20             ; (+003H)
            CALL    05E48H
L6B20:      CALL    L6AB0
            POP     BC
            PUSH    DE
            LD      A,000H
            RST     018H
            DB      006H
            LD      E,C
            LD      D,B
            CALL    L6DE2
            CALL    L6B6C
            POP     DE
            CALL    L6DE2
            CALL    L6B68
            CALL    L413E
            RET     Z
            INC     HL
            JR      L6B0C               ; (-033H)

L6B3F:      CALL    L6E4B
            LD      A,E
            CP      004H
            JP      NC,04AF2H
            CALL    05E48H
            LD      A,(PNMODE)
            CP      002H
            JR      Z,L6B5F             ; (+00dH)
            CALL    L6A74
            LD      A,002H
            RST     018H
            DB      006H
            CALL    L6B5F
            JP      L6A5B

L6B5F:      LD      A,043H
            RST     018H
            DB      006H
            LD      A,E
            OR      030H
            RST     018H
            DB      006H
L6B68:      LD      A,00DH
            JR      L6B6E               ; (+002H)

L6B6C:      LD      A,02CH
L6B6E:      RST     018H
            DB      006H
            RET     

L6B71:      CALL    L6ACA
            CALL    05E48H
            LD      A,048H
            JR      L6B6E               ; (-00dH)

L6B7B:      CALL    L6ACA
            CALL    05E48H
            LD      A,049H
            JR      L6B6E               ; (-017H)

L6B85:      CALL    L6ACA
            CALL    TEST1
            DB      05BH
            JR      NZ,L6BC6            ; (+038H)
            CALL    L6E4B
            CP      040H
            JP      NC,04AF2H
            PUSH    DE
            CALL    L5E70
            CALL    L6E4B
            CP      004H
            JP      NC,04AF2H
            PUSH    DE
            CALL    TESTX
            DB      05DH
            POP     BC
            POP     DE
            PUSH    BC
            LD      A,053H
            RST     018H
            DB      006H
            CALL    L6DE2
            CALL    L6B6C
            POP     DE
            LD      A,051H
            RST     018H
            DB      006H
            CALL    L6DE2
            CALL    L6B68
            CALL    L413E
            RET     Z
            CALL    L5E73
L6BC6:      CALL    L6E6F
            CALL    L413E
            JR      Z,L6BD2             ; (+004H)
            CALL    L5E73
            DEC     HL
L6BD2:      LD      A,B
            OR      A
            JR      Z,L6BE3             ; (+00dH)
            LD      A,050H
            RST     018H
            DB      006H
L6BDA:      LD      A,(DE)
            RST     018H
            DB      006H
            INC     DE
            DJNZ    L6BDA               ; (-006H)
            CALL    L6B68
L6BE3:      CALL    L413E
            RET     Z
            INC     HL
            JR      L6BC6               ; (-024H)

L6BEA:      CALL    L6ACA
            CALL    L6E4B
            CP      002H
            JP      NC,04AF2H
            PUSH    AF
            CALL    L5E70
            CALL    L6E5F
            PUSH    DE
            CALL    L5E73
            CALL    L6E4B
            OR      A
            JP      Z,04AF2H
            CALL    05E48H
            LD      A,058H
            RST     018H
            DB      006H
            POP     BC
            POP     AF
            PUSH    DE
            PUSH    BC
            OR      030H
            RST     018H
            DB      006H
            CALL    L6B6C
            POP     DE
            CALL    L6AB0
            CALL    L6DE2
            CALL    L6B6C
            POP     DE
            CALL    L6DE2
            JP      L6B68

L6C2A:      CALL    L6ACA
            PUSH    HL
            LD      DE,L0000
            LD      HL,L6DC4
            CALL    L5E99
            LD      DE,L0168
            LD      HL,L6DC9
            CALL    L5E99
            LD      HL,L610D
            LD      DE,L6DCE
            CALL    L5E93
            POP     HL
            CALL    L6E8A
            CALL    L5E73
            PUSH    HL
            LD      HL,L6DB5
            EX      DE,HL
            CALL    L5E93
            POP     HL
            CALL    L6E8A
            CALL    L5E73
            PUSH    HL
            LD      HL,L6DBA
            EX      DE,HL
            CALL    L5E93
            POP     HL
            CALL    L6E8A
            PUSH    HL
            PUSH    AF
            LD      HL,L6DBF
            EX      DE,HL
            CALL    L5E93
            LD      A,(06DC0H)
            RLCA    
            JP      C,04AF2H
            POP     AF
            CP      02CH
            JR      NZ,L6CBA            ; (+03aH)
            POP     HL
            INC     HL
            CALL    L6E8A
            PUSH    HL
            PUSH    AF
            LD      HL,L6DC4
            EX      DE,HL
            CALL    L5E93
            POP     AF
            CP      02CH
            JR      NZ,L6CBA            ; (+027H)
            POP     HL
            INC     HL
            CALL    L6E8A
            PUSH    HL
            PUSH    AF
            LD      HL,L6DC9
            EX      DE,HL
            CALL    L5E93
            POP     AF
            CP      02CH
            JR      NZ,L6CBA            ; (+014H)
            POP     HL
            INC     HL
            CALL    L6E8A
            PUSH    HL
            LD      HL,L6DCE
            EX      DE,HL
            CALL    L5E93
            LD      A,(06DCFH)
            RLCA    
            JP      C,04AF2H
L6CBA:      POP     HL
            CALL    05E48H
            PUSH    HL
            LD      HL,L6DC9
            LD      DE,L6DC4
            LD      A,(L6DCE)
            OR      A
            CALL    NZ,L7A1C
            JP      C,04AF2H
            CALL    L6D6B
            LD      HL,L6DD3
            LD      (06D43H),HL
            LD      HL,L6DD8
            LD      (06D4EH),HL
            CALL    06D3FH
            LD      A,(L6DCE)
            OR      A
            JR      Z,L6D19             ; (+032H)
L6CE7:      LD      HL,L6DC4
            LD      DE,L6DCE
            CALL    L7985
            LD      DE,L6DC9
            CALL    L7A1C
            JR      NC,L6D04            ; (+00cH)
            CALL    L6D6B
            CALL    L6D3C
            RST     018H
            DB      00EH
            JR      NZ,L6CE7            ; (-01bH)
            POP     HL
            RET     

L6D04:      CALL    L6D0C
            CALL    L6D3C
            POP     HL
            RET     

L6D0C:      LD      HL,L6DC9
            LD      DE,L6DC4
            LD      BC,00005H
            LDIR    
            JR      L6D6B               ; (+052H)

L6D19:      LD      HL,L6DB5
            LD      (06D43H),HL
            LD      HL,L6DBA
            LD      (06D4EH),HL
            CALL    L6D3C
            CALL    L6D0C
            LD      HL,L6DD3
            LD      (06D43H),HL
            LD      HL,L6DD8
            LD      (06D4EH),HL
            CALL    L6D3C
            POP     HL
            RET     

L6D3C:      LD      A,044H
            LD      HL,L4D3E
            PUSH    AF
            LD      HL,L6DB5
            CALL    L60B7
            PUSH    HL
            EX      DE,HL
            CALL    L6AB0
            LD      HL,L6DBA
            CALL    L60B7
            PUSH    HL
            EX      DE,HL
            CALL    L6AB0
            POP     HL
            POP     DE
            POP     AF
            RST     018H
            DB      006H
            PUSH    HL
            CALL    L6DE2
            CALL    L6B6C
            POP     DE
            CALL    L6DE2
            JP      L6B68

L6D6B:      LD      DE,(INTFAC)
            LD      HL,L6DC4
            CALL    L5E93
            LD      HL,(INTFAC)
            CALL    L817C
            CALL    L7FC2
            LD      DE,L6DBF
            CALL    L7BB3
            LD      DE,L6DB5
            CALL    L7985
            LD      DE,L6DD3
            CALL    L5E93
            LD      DE,(INTFAC)
            LD      HL,L6DC4
            CALL    L5E93
            LD      HL,(INTFAC)
            CALL    L817C
            CALL    L7FD2
            LD      DE,L6DBF
            CALL    L7BB3
            LD      DE,L6DBA
            CALL    L7985
            LD      DE,L6DD8
            JP      L5E93

L6DB5:      DS      5, 000H
L6DBA:      DS      5, 000H
L6DBF:      DS      5, 000H
L6DC4:      DS      5, 000H
L6DC9:      DS      5, 000H
L6DCE:      DS      5, 000H
L6DD3:      DS      5, 000H
L6DD8:      DS      10, 000H
L6DE2:      PUSH    AF
            PUSH    HL
            LD      HL,(INTFAC)
            CALL    L5E99
            CALL    L6028
            RST     018H
            DB      017H
L6DEF:      LD      A,(DE)
            RST     018H
            DB      006H
            INC     DE
            DJNZ    L6DEF               ; (-006H)
            POP     HL
            POP     AF
            RET     

L6DF8:      CALL    L6AC6
            CALL    05E48H
            LD      A,004H
            RST     018H
            DB      006H
            RET     

L6E03:      CALL    L6AC6
            CALL    L6E5F
            LD      A,E
            OR      A
            JP      Z,04AF2H
            CP      049H
            JP      NC,04AF2H
            CALL    05E48H
            LD      A,009H
            RST     018H
            DB      006H
            RST     018H
            DB      006H
            CALL    L6DE2
            JP      L6B68

L6E22:      CP      09DH
            JR      Z,L6E31             ; (+00bH)
            CP      0A1H
            JP      NZ,L4AEC
            XOR     A
L6E2C:      LD      (L004D),A
            INC     HL
            RET     

L6E31:      CALL    L6AC6
            LD      A,(L6A23)
            CP      04EH
            JP      NZ,L4B3C
            LD      A,(L004D)
            OR      A
            JR      NZ,L6E2C            ; (-016H)
            CALL    L5285
            LD      A,016H
            RST     018H
            DB      003H
            JR      L6E2C               ; (-01fH)

L6E4B:      CALL    L6E5F
L6E4E:      LD      A,D
            OR      A
            JP      NZ,04AF2H
            LD      A,E
            RET     

L6E55:      CALL    L6E9C
            DEC     DE
            DEC     DE
            DEC     DE
            DEC     DE
            DEC     DE
            JR      L6E62               ; (+003H)

L6E5F:      CALL    L6E8A
L6E62:      PUSH    AF
            PUSH    HL
            EX      DE,HL
            CALL    L791F
            CALL    L60B7
            EX      DE,HL
            POP     HL
            POP     AF
            RET     

L6E6F:      CALL    L6E8A
            PUSH    AF
            CALL    L7916
            PUSH    HL
            EX      DE,HL
            CALL    L6E7E
            POP     HL
            POP     AF
            RET     

L6E7E:      LD      B,(HL)
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            ADD     HL,DE
            EX      DE,HL
            LD      A,B
            RET     

L6E8A:      LD      DE,(VARED)
            LD      (TMPEND),DE
L6E92:      LD      DE,(INTFAC)
            PUSH    DE
            CALL    L6E9C
            POP     DE
            RET     

L6E9C:      PUSH    DE
            LD      DE,(TMPEND)
            CALL    L74E8
            POP     DE
            CALL    L6EC0
L6EA8:      CP      0EAH
            RET     NZ
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            CALL    L6EC0
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    07E65H
            POP     HL
            POP     AF
            JR      L6EA8               ; (-018H)

L6EC0:      CALL    L6EDB
L6EC3:      CP      0EBH
            RET     NZ
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            CALL    L6EDB
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    07E62H
            POP     HL
            POP     AF
            JR      L6EC3               ; (-018H)

L6EDB:      CALL    L6EF6
L6EDE:      CP      0ECH
            RET     NZ
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            CALL    L6EF6
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7E5F
            POP     HL
            POP     AF
            JR      L6EDE               ; (-018H)

L6EF6:      CALL    TEST1
            DB      0EDH
            JR      NZ,L6F0B            ; (+00fH)
            CALL    L6F0B
            PUSH    AF
            PUSH    HL
            LD      HL,0FFFBH
            ADD     HL,DE
            CALL    L7E4F
            POP     HL
            POP     AF
            RET     

L6F0B:      CALL    L6F75
L6F0E:      CP      0EEH
            RET     C
            PUSH    AF
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            CALL    L6F75
            POP     BC
            EX      (SP),HL
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7A1C
            EX      AF,AF'
            POP     AF
            CP      0F6H
            JR      NZ,L6F48            ; (+01eH)
            EX      AF,AF'
            JR      C,L6F43             ; (+016H)
L6F2D:      LD      BC,L0000
L6F30:      LD      (HL),C
            INC     HL
            LD      (HL),B
            INC     HL
            XOR     A
            LD      (HL),A
            INC     HL
            LD      (HL),A
            INC     HL
            LD      (HL),A
            LD      A,005H
            LD      (L84CE),A
            POP     AF
            POP     HL
            JR      L6F0E               ; (-035H)

L6F43:      LD      BC,08081H
            JR      L6F30               ; (-018H)

L6F48:      CP      QDPD
            JR      NZ,L6F53            ; (+007H)
            EX      AF,AF'
            JR      Z,L6F2D             ; (-022H)
            JR      C,L6F2D             ; (-024H)
            JR      L6F43               ; (-010H)

L6F53:      CP      QDPC
            JR      NZ,L6F5C            ; (+005H)
            EX      AF,AF'
            JR      Z,L6F43             ; (-017H)
            JR      L6F2D               ; (-02fH)

L6F5C:      CP      QDPA
            JR      C,L6F65             ; (+005H)
            EX      AF,AF'
            JR      NC,L6F43            ; (-020H)
            JR      L6F2D               ; (-038H)

L6F65:      CP      0F0H
            JR      C,L6F70             ; (+007H)
            EX      AF,AF'
            JR      Z,L6F43             ; (-029H)
            JR      C,L6F43             ; (-02bH)
            JR      L6F2D               ; (-043H)

L6F70:      EX      AF,AF'
            JR      Z,L6F2D             ; (-046H)
            JR      L6F43               ; (-032H)

L6F75:      CALL    L6FA0
L6F78:      CP      0F7H
            RET     C
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            JR      Z,L6F92             ; (+010H)
            CALL    L6FA0
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L797C
L6F8E:      POP     HL
            POP     AF
            JR      L6F78               ; (-01aH)

L6F92:      CALL    L6FA0
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7985
            JR      L6F8E               ; (-012H)

L6FA0:      CALL    L6FCB
L6FA3:      CP      FDPB
            RET     C
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            JR      Z,L6FBD             ; (+010H)
            CALL    L6FCB
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7EBB
L6FB9:      POP     HL
            POP     AF
            JR      L6FA3               ; (-01aH)

L6FBD:      CALL    L6FCB
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7EDA
            JR      L6FB9               ; (-012H)

L6FCB:      CALL    L6FF6
L6FCE:      CP      FDPD
            RET     C
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            JR      Z,L6FE8             ; (+010H)
            CALL    L6FF6
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7BB3
L6FE4:      POP     HL
            POP     AF
            JR      L6FCE               ; (-01aH)

L6FE8:      CALL    L6FF6
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7C9E
            JR      L6FE4               ; (-012H)

L6FF6:      CALL    TEST1
            DB      0F7H
            JR      Z,L6FF6             ; (-006H)
            CP      FDPA
            JR      NZ,L7006            ; (+006H)
            INC     HL
            CALL    L7006
            JR      L702F               ; (+029H)

L7006:      CALL    L703B
L7009:      CP      0FDH
            RET     NZ
            LD      A,(L84CE)
            PUSH    AF
            INC     HL
            CALL    L7021
            POP     BC
            PUSH    AF
            PUSH    HL
            CALL    L74C5
            CALL    L7DD3
            POP     HL
            POP     AF
            JR      L7009               ; (-018H)

L7021:      CALL    TEST1
            DB      0F7H
            JR      Z,L7021             ; (-006H)
            CP      FDPA
            JR      NZ,L703B            ; (+010H)
            INC     HL
            CALL    L703B
L702F:      PUSH    AF
            PUSH    HL
            LD      HL,0FFFBH
            ADD     HL,DE
            CALL    L796F
            POP     HL
            POP     AF
            RET     

L703B:      CALL    L413E
            JP      Z,L4AEC
            CALL    L7047
            JP      HLFTCH

L7047:      PUSH    HL
            LD      HL,(MEMLMT)
            SCF     
            SBC     HL,DE
            JP      C,04AFBH
            POP     HL
            CP      LSE4
            JR      Z,L7064             ; (+00eH)
            CP      020H
            JR      NC,L70A4            ; (+04aH)
L705A:      INC     HL
            CP      015H
            JR      C,L7073             ; (+014H)
            CALL    L5E93
            JR      L706D               ; (+009H)

L7064:      INC     HL
            PUSH    HL
            LD      HL,0818FH
            CALL    L5E93
            POP     HL
L706D:      LD      A,005H
L706F:      LD      (L84CE),A
            RET     

L7073:      CP      00BH
            JR      NC,L7080            ; (+009H)
            DEC     A
            JP      M,L4AEC
            LD      B,000H
            LD      C,A
            JR      L7091               ; (+011H)

L7080:      LD      C,(HL)
            INC     HL
            LD      B,(HL)
            INC     HL
            CP      00CH
            JR      NZ,L7091            ; (+009H)
            INC     BC
            INC     BC
            LD      A,(BC)
            INC     BC
            EX      AF,AF'
            LD      A,(BC)
            LD      B,A
            EX      AF,AF'
            LD      C,A
L7091:      PUSH    HL
            PUSH    DE
            EX      DE,HL
            LD      E,C
            LD      D,B
            CALL    L5E99
            POP     DE
            POP     HL
            LD      A,005H
L709D:      INC     DE
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            JR      L706F               ; (-035H)

L70A4:      CP      022H
            JR      NZ,L70D7            ; (+02fH)
            INC     HL
            PUSH    HL
            CALL    L74D0
            EX      (SP),HL
            PUSH    HL
            LD      HL,(TMPEND)
            PUSH    BC
            LD      BC,(STRST)
            OR      A
            SBC     HL,BC
            POP     BC
            EX      DE,HL
            LD      (HL),B
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            INC     HL
            INC     HL
            POP     DE
            PUSH    HL
            LD      HL,(TMPEND)
            CALL    L74DC
            LD      (TMPEND),HL
            POP     DE
            POP     HL
            LD      A,003H
            LD      (L84CE),A
            RET     

L70D7:      CP      028H
            JR      NZ,L70E2            ; (+007H)
            INC     HL
            CALL    L6E9C
            JP      L5E82

L70E2:      OR      A
            JP      P,L76F5
            CP      0E7H
            INC     HL
            JR      NZ,L70F0            ; (+005H)
            LD      A,0A8H
            PUSH    AF
            JR      L7125               ; (+035H)

L70F0:      CP      0FFH
            JP      NZ,L4AEC
            LD      A,(HL)
            INC     HL
            CP      0A0H
            JP      Z,L71A5
            CP      0C8H
            JP      NC,L4AEC
            CP      0BAH
            JR      NC,L7155            ; (+050H)
            PUSH    AF
            CP      0B3H
            JR      NC,L7146            ; (+03cH)
            LD      B,003H
            CP      0AAH
            JR      Z,L7125             ; (+015H)
            CP      0ABH
            JR      NC,L7125            ; (+011H)
            LD      B,005H
            CP      088H
            JR      Z,L7140             ; (+026H)
            CP      089H
            JR      NZ,L7125            ; (+007H)
            CALL    TEST1
            DB      040H
            LD      (081A8H),A
L7125:      CALL    L5E78
L7128:      PUSH    BC
            CALL    L6E9C
            CALL    L5E82
            POP     AF
            CALL    L7912
L7133:      POP     AF
            PUSH    DE
            PUSH    HL
            LD      HL,0FFFBH
            ADD     HL,DE
            CALL    L7155
            POP     HL
            POP     DE
            RET     

L7140:      CALL    TEST1
            DB      028H
            JR      Z,L7128             ; (-01eH)
L7146:      LD      A,005H
            LD      (L84CE),A
            PUSH    HL
            LD      HL,L6112
            CALL    L5E93
            POP     HL
            JR      L7133               ; (-022H)

L7155:      PUSH    HL
            ADD     A,A
            LD      L,A
            LD      H,000H
            LD      BC,L450A
            ADD     HL,BC
            LD      A,(HL)
            INC     HL
            LD      H,(HL)
            LD      L,A
            EX      (SP),HL
            RET     

L7164:      EX      DE,HL
            LD      HL,0FDF1H
            ADD     HL,SP
            LD      BC,(TMPEND)
            OR      A
            SBC     HL,BC
            EX      DE,HL
            JR      NC,L7186            ; (+013H)
            XOR     A
            JR      L7183               ; (+00dH)

L7176:      LD      A,(L0054)
            JR      L7183               ; (+008H)

L717B:      LD      A,(00055H)
            JR      L7183               ; (+003H)

L7180:      LD      A,(L8485)
L7183:      LD      E,A
            LD      D,000H
L7186:      LD      A,005H
            LD      (L84CE),A
            JP      L5E99

L718E:      LD      DE,(08487H)
            CALL    L5E99
            INC     HL
            BIT     7,(HL)
            DEC     HL
            RET     Z
            LD      DE,L71A0
            JP      L7985

L71A0:      SUB     C
            NOP     
            NOP     
            NOP     
            NOP     
L71A5:      CALL    L5E78
            LD      B,000H
L71AA:      PUSH    BC
            PUSH    DE
            CALL    L6E55
            CALL    L6E4E
            POP     DE
            POP     BC
            PUSH    AF
            INC     B
            CALL    TEST1
            DB      02CH
            JR      Z,L71AA             ; (-012H)
            CALL    L5E82
            LD      A,B
            EXX     
            LD      B,A
            LD      HL,(TMPEND)
            CALL    ADDHLA
            LD      D,H
            LD      E,L
            CALL    L74E8
L71CD:      DEC     HL
            POP     AF
            LD      (HL),A
            DJNZ    L71CD               ; (-005H)
            EXX     
            LD      A,B
            EXX     
            LD      B,A
L71D6:      LD      (TMPEND),DE
            LD      DE,(STRST)
            OR      A
            SBC     HL,DE
            PUSH    HL
            LD      A,B
            EXX     
            EX      DE,HL
            LD      (HL),A
            INC     HL
            POP     BC
            LD      (HL),C
            INC     HL
            LD      (HL),B
            LD      BC,L0003
            ADD     HL,BC
L71EF:      EX      DE,HL
            LD      A,003H
            LD      (L84CE),A
            RET     

L71F6:      PUSH    HL
            CALL    L60B7
            CALL    L69E0
            RST     018H
            DB      017H
L71FF:      LD      A,B
            OR      A
            JR      Z,L7213             ; (+010H)
            LD      HL,(TMPEND)
            PUSH    HL
            PUSH    BC
            CALL    L74DC
            POP     BC
            EX      DE,HL
            POP     HL
            EXX     
            POP     DE
            EXX     
            JR      L71D6               ; (-03dH)

L7213:      POP     HL
            CALL    L7967
            JR      L71EF               ; (-02aH)

L7219:      PUSH    HL
            CALL    L722A
            LD      B,A
            LD      C,A
            OR      A
            LD      A,020H
            PUSH    DE
            CALL    NZ,?SETDE
            POP     DE
            LD      B,C
            JR      L71FF               ; (-02bH)

L722A:      CALL    L60B7
            LD      DE,KEYBUF
            LD      A,H
            OR      A
            JP      NZ,04AF2H
            LD      A,L
            RET     

L7237:      PUSH    HL
            CALL    L6028
            LD      A,(DE)
            CP      020H
            JR      NZ,L7241            ; (+001H)
            INC     DE
L7241:      RST     018H
            DB      017H
            JR      L71FF               ; (-046H)

L7245:      CALL    L5E78
            CALL    L5882
            LD      B,A
            CALL    L5E7F
            LD      A,B
            PUSH    HL
            PUSH    DE
            RST     018H
            DB      029H
            LD      DE,0FFFFH
            JR      C,L7261             ; (+008H)
            INC     HL
            BIT     7,(HL)
            JR      NZ,L7261            ; (+003H)
            LD      DE,L0000
L7261:      POP     HL
            CALL    L5E99
            LD      A,005H
            LD      (L84CE),A
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            EX      DE,HL
            POP     HL
            RET     

L7272:      PUSH    HL
            CALL    L6E7E
            OR      A
            JR      Z,L727A             ; (+001H)
            LD      A,(DE)
L727A:      JR      L7280               ; (+004H)

L727C:      PUSH    HL
            CALL    L6E7E
L7280:      POP     HL
            JP      L7183

L7284:      PUSH    HL
            CALL    L6E7E
            LD      HL,(TMPEND)
            PUSH    HL
            CALL    L74DC
            LD      (HL),000H
            POP     DE
            POP     HL
            JP      L5EDC

L7296:      CALL    L7312
            CALL    L5E82
            CALL    L730B
            EX      DE,HL
            LD      A,(HL)
            CP      C
            JR      NC,L72A5            ; (+001H)
            LD      C,A
L72A5:      LD      (HL),C
            LD      BC,00005H
L72A9:      LD      A,003H
            ADD     HL,BC
            EX      DE,HL
            LD      (L84CE),A
            JP      HLFTCH

L72B3:      CALL    L7312
            CALL    L5E82
            CALL    L730B
            EX      DE,HL
            LD      A,(HL)
            SUB     C
            JR      NC,L72C3            ; (+002H)
            XOR     A
            LD      C,(HL)
L72C3:      LD      (HL),C
            INC     HL
            ADD     A,(HL)
            LD      (HL),A
            INC     HL
            JR      NC,L72CB            ; (+001H)
            INC     (HL)
L72CB:      LD      BC,L0003
            JR      L72A9               ; (-027H)

L72D0:      CALL    L7312
            CALL    L730B
            OR      A
            JP      Z,04AF2H
            PUSH    AF
            CALL    TEST1
            DB      029H
            LD      A,0FFH
            JR      Z,L72F7             ; (+014H)
            CALL    L5E70
            PUSH    DE
            EX      DE,HL
            LD      BC,00005H
            ADD     HL,BC
            EX      DE,HL
            CALL    L6E55
            CALL    L5E82
            CALL    L6E4E
            POP     DE
L72F7:      POP     BC
            LD      C,A
            EX      DE,HL
            LD      A,(HL)
            SUB     B
            JR      C,L7307             ; (+009H)
            INC     A
            CP      C
            JR      NC,L7303            ; (+001H)
            LD      C,A
L7303:      LD      A,B
            DEC     A
            JR      L72C3               ; (-044H)

L7307:      XOR     A
            LD      C,A
            JR      L72C3               ; (-048H)

L730B:      LD      A,B
            OR      A
            JP      NZ,04AF2H
            LD      A,C
            RET     

L7312:      CALL    L5E78
            PUSH    DE
            CALL    L6E9C
            CALL    L5E73
            CALL    L7916
            CALL    L6E55
            LD      C,E
            LD      B,D
            POP     DE
            RET     

L7326:      PUSH    HL
            PUSH    DE
            LD      HL,(TMPEND)
            PUSH    HL
            LD      A,030H
            LD      B,006H
            CALL    ?SETHL
            RST     018H
            DB      025H
            POP     HL
            OR      A
            JR      Z,L733E             ; (+005H)
            INC     (HL)
            INC     HL
            INC     (HL)
            INC     (HL)
            DEC     HL
L733E:      EX      DE,HL
            LD      BC,08CA0H
            CALL    L73B5
            LD      BC,L0E10
            CALL    L73B5
            CP      03AH
            JR      C,L7359             ; (+00aH)
            SUB     00AH
            DEC     DE
            LD      (DE),A
            DEC     DE
            LD      A,(DE)
            INC     A
            LD      (DE),A
            INC     DE
            INC     DE
L7359:      DEC     DE
            DEC     DE
            LD      A,(DE)
            INC     DE
            LD      B,A
            LD      A,(DE)
            INC     DE
            LD      C,A
            LD      A,B
            CP      032H
            JR      NZ,L7377            ; (+011H)
            LD      A,C
            CP      034H
            JR      NZ,L7377            ; (+00cH)
            LD      HL,(TMPEND)
            LD      A,030H
            LD      B,006H
            CALL    ?SETHL
            JR      L738D               ; (+016H)

L7377:      LD      BC,L0258
            CALL    L73B5
            LD      BC,0003CH
            CALL    L73B5
            LD      BC,0000AH
            CALL    L73B5
            LD      A,030H
            ADD     A,L
            LD      (DE),A
L738D:      LD      HL,(TMPEND)
            LD      DE,L0006
            EX      DE,HL
            ADD     HL,DE
            LD      (TMPEND),HL
            EX      DE,HL
            LD      DE,(STRST)
            OR      A
            SBC     HL,DE
            EX      DE,HL
            POP     HL
            LD      (HL),006H
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            INC     HL
            INC     HL
            EX      DE,HL
            POP     HL
            LD      A,003H
            LD      (L84CE),A
            JP      HLFTCH

L73B5:      OR      A
            SBC     HL,BC
            JR      C,L73BF             ; (+005H)
            LD      A,(DE)
            INC     A
            LD      (DE),A
            JR      L73B5               ; (-00aH)

L73BF:      ADD     HL,BC
            INC     DE
            RET     

L73C2:      CALL    TESTX
            DB      QDPC
            CALL    L6E6F
            LD      A,B
            CP      006H
            JP      NZ,04AF2H
            PUSH    HL
            PUSH    DE
            LD      HL,L0000
            CALL    L7403
            CP      018H
            JP      NC,04AF2H
            CP      00CH
            LD      A,000H
            JR      C,L73EB             ; (+009H)
            PUSH    DE
            LD      DE,L000C
            OR      A
            SBC     HL,DE
            POP     DE
            INC     A
L73EB:      PUSH    AF
            CALL    L7403
            CP      03CH
            JP      NC,04AF2H
            CALL    L7403
            CP      03CH
            JP      NC,04AF2H
            POP     AF
            EX      DE,HL
            RST     018H
            DB      026H
            POP     DE
            POP     HL
            RET     

L7403:      PUSH    DE
            LD      D,H
            LD      E,L
            ADD     HL,HL
            ADD     HL,HL
            ADD     HL,DE
            LD      D,H
            LD      E,L
            ADD     HL,HL
            ADD     HL,DE
            ADD     HL,HL
            ADD     HL,HL
            POP     DE
            LD      A,(DE)
            INC     DE
            SUB     030H
            JP      C,04AF2H
            CP      00AH
            JP      NC,04AF2H
            PUSH    BC
            LD      C,A
            ADD     A,A
            ADD     A,A
            ADD     A,C
            ADD     A,A
            LD      C,A
            LD      A,(DE)
            INC     DE
            SUB     030H
            JP      C,04AF2H
L742A:      CP      00AH
            JP      NC,04AF2H
            ADD     A,C
            LD      C,A
            LD      B,000H
            ADD     HL,BC
            POP     BC
            RET     

L7436:      LD      DE,(VARED)
            LD      (TMPEND),DE
            LD      DE,(INTFAC)
L7442:      PUSH    DE
            CALL    L7495
L7446:      LD      A,(HL)
            CP      028H
            JP      Z,L74FB
            PUSH    HL
            CALL    L77B6
            JR      NC,L7458            ; (+006H)
            LD      A,C
            LD      C,L
            LD      B,H
            POP     HL
            POP     DE
            RET     

L7458:      LD      A,(DE)
            ADD     A,C
            ADD     A,002H
            PUSH    BC
            EX      DE,HL
            LD      HL,(TMPEND)
            PUSH    HL
            OR      A
            SBC     HL,DE
            INC     HL
            LD      C,L
            LD      B,H
            POP     HL
            PUSH    DE
            EX      DE,HL
            LD      L,A
            LD      H,000H
            ADD     HL,DE
            EX      DE,HL
            CALL    L74E8
            LDDR    
            LD      E,A
            LD      D,000H
            RST     018H
            DB      01AH
            POP     HL
            POP     BC
            LD      DE,KEYBUF
            LD      (HL),C
            INC     HL
            SCF     
            SBC     A,C
            LD      B,A
L7484:      LD      A,(DE)
            LD      (HL),A
            INC     DE
            INC     HL
            DJNZ    L7484               ; (-006H)
            PUSH    HL
            LD      B,C
            CALL    ?CLRHL
            LD      (HL),A
            LD      A,C
            POP     BC
            POP     HL
            POP     DE
            RET     

L7495:      CALL    HLFTCH
            SUB     041H
            CP      01AH
            JP      NC,L4AEC
            LD      DE,KEYBUF
            LD      B,000H
L74A4:      INC     DE
            LD      A,(HL)
            CALL    L5ECB
            JR      C,L74B6             ; (+00bH)
            LD      (DE),A
            INC     B
            LD      A,B
            CP      003H
            JR      C,L74B3             ; (+001H)
            DEC     B
L74B3:      INC     HL
            JR      L74A4               ; (-012H)

L74B6:      LD      DE,KEYBUF
            EX      DE,HL
            LD      (HL),B
            EX      DE,HL
            LD      C,005H
            CP      024H
            RET     NZ
            LD      C,003H
            INC     HL
            RET     

L74C5:      LD      HL,0FFFBH
            EX      DE,HL
            ADD     HL,DE
            EX      DE,HL
            ADD     HL,DE
            LD      A,B
            JP      L7912

L74D0:      LD      B,0FFH
L74D2:      INC     B
            LD      A,(HL)
            OR      A
            RET     Z
            INC     HL
            CP      022H
            RET     Z
            JR      L74D2               ; (-00aH)

L74DC:      LD      A,B
            OR      A
            RET     Z
            CALL    LDHLDE
            EX      DE,HL
            CALL    L74E8
            EX      DE,HL
            RET     

L74E8:      PUSH    HL
            LD      HL,0FE00H
            ADD     HL,SP
            SBC     HL,DE
            POP     HL
            RET     NC
            JP      L4B2C

L74F4:      LD      DE,(INTFAC)
            PUSH    DE
            JR      L7503               ; (+008H)

L74FB:      XOR     A
            LD      (L76F4),A
            INC     HL
            LD      (L76F2),HL
L7503:      LD      B,000H
            EXX     
            POP     HL
            PUSH    HL
            PUSH    HL
            LD      HL,(TMPEND)
            LD      DE,KEYBUF
            PUSH    HL
            LD      A,(DE)
            LD      (HL),A
            INC     DE
            INC     HL
            LD      B,A
            CALL    L74DC
            POP     DE
            LD      (TMPEND),HL
            LD      HL,(STRST)
            EX      DE,HL
            OR      A
            SBC     HL,DE
            EX      (SP),HL
            PUSH    HL
            EXX     
L7526:      POP     DE
            PUSH    DE
            PUSH    BC
            LD      BC,(076F2H)
            LD      A,(L76F4)
            PUSH    BC
            PUSH    AF
            LD      BC,(0849CH)
            PUSH    BC
            CALL    L6E55
            BIT     7,D
            JP      NZ,04AF2H
            EX      AF,AF'
            POP     BC
            LD      (0849CH),BC
            POP     AF
            LD      (L76F4),A
            POP     BC
            LD      (076F2H),BC
            EX      AF,AF'
            INC     HL
            CP      029H
            JR      Z,L756C             ; (+018H)
            CP      02CH
            JP      NZ,L4AEC
            POP     BC
            INC     B
            LD      A,B
            CP      004H
            JP      NC,04AF2H
            EXX     
            POP     HL
            POP     DE
            EXX     
            PUSH    DE
            EXX     
            PUSH    DE
            PUSH    HL
            EXX     
            JR      L7526               ; (-046H)

L756C:      POP     BC
            EXX     
            POP     HL
            POP     DE
            EXX     
            INC     B
            PUSH    DE
            PUSH    HL
            EXX     
            LD      HL,(STRST)
            ADD     HL,DE
            LD      B,(HL)
            INC     B
            LD      DE,KEYBUF
            CALL    LDDEHL
            EXX     
            SET     7,C
            CALL    L77B6
            RES     7,C
            JR      NC,L75E1            ; (+056H)
            LD      A,B
            CP      (HL)
            JP      NZ,04AFEH
            INC     HL
            EX      DE,HL
            POP     HL
            LD      (L849C),HL
            EXX     
            LD      HL,L0000
            EXX     
            EX      DE,HL
L759C:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            EX      (SP),HL
            PUSH    DE
            EXX     
            POP     DE
            CALL    L76C8
            EXX     
            PUSH    HL
            OR      A
            SBC     HL,DE
            JP      NC,04AF2H
            EXX     
            POP     DE
            CALL    L76C4
            EXX     
            POP     HL
            DJNZ    L759C               ; (-01cH)
            PUSH    HL
            EXX     
            LD      A,C
            LD      D,B
            BIT     6,A
            JR      NZ,L75D4            ; (+014H)
            AND     00FH
            LD      E,A
            PUSH    AF
            CALL    L76C8
            PUSH    HL
            EXX     
            POP     BC
            POP     AF
            POP     HL
            ADD     HL,BC
            LD      C,L
            LD      B,H
L75CF:      LD      HL,(L849C)
            POP     DE
            RET     

L75D4:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            POP     HL
            LD      C,L
            LD      B,H
            LD      (HL),E
            INC     HL
            LD      (HL),D
            AND     00FH
            JR      L75CF               ; (-012H)

L75E1:      EXX     
            LD      A,(L76F4)
            OR      A
            JP      Z,04AF2H
            POP     HL
            LD      (L849C),HL
            EXX     
            LD      DE,(TMPEND)
            LD      L,B
            LD      H,000H
            ADD     HL,HL
            ADD     HL,DE
            EX      DE,HL
            INC     DE
            CALL    L74E8
            LD      (HL),B
            INC     HL
            EXX     
            LD      HL,00001H
            EXX     
L7603:      POP     DE
            LD      A,(L76F4)
            OR      A
            JR      NZ,L7616            ; (+00cH)
            PUSH    HL
            EX      DE,HL
            LD      DE,0000AH
            SCF     
            SBC     HL,DE
            JP      NC,04AF2H
            POP     HL
L7616:      INC     DE
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            PUSH    DE
            EXX     
            POP     DE
            CALL    L76C8
            EXX     
            DJNZ    L7603               ; (-021H)
            DB      059H
            LD      D,000H
            PUSH    BC
            PUSH    DE
            EXX     
            POP     DE
            CALL    L76C8
            PUSH    HL
            EXX     
            POP     BC
            PUSH    BC
            EX      DE,HL
            LD      HL,(TMPEND)
            LD      L,(HL)
            LD      H,000H
            ADD     HL,HL
            LD      A,(KEYBUF)
            ADD     A,005H
            ADD     A,L
            LD      L,A
            LD      A,000H
            ADC     A,H
            LD      H,A
            JR      C,L764C             ; (+005H)
            ADD     HL,BC
            JR      C,L764C             ; (+002H)
            PUSH    HL
            ADD     HL,DE
L764C:      JP      C,L4B2C
            EX      DE,HL
            CALL    L74E8
            PUSH    HL
            EXX     
            POP     HL
            LD      BC,(STRST)
            DEC     BC
            OR      A
            SBC     HL,BC
            PUSH    HL
            EXX     
            POP     BC
            LDDR    
            POP     DE
            RST     018H
            DB      01AH
            POP     BC
            PUSH    DE
            EXX     
            POP     DE
            LD      H,B
            LD      L,C
            POP     BC
            LD      A,C
            OR      080H
            LD      (HL),A
            INC     HL
            DEC     DE
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            LD      DE,KEYBUF
            LD      A,(DE)
            INC     A
            LD      B,A
            CALL    LDHLDE
            LD      DE,(TMPEND)
            LD      A,(DE)
            LD      (HL),A
            INC     HL
            INC     DE
            ADD     A,A
            LD      B,A
            CALL    LDHLDE
            PUSH    HL
            EXX     
            POP     HL
L7690:      XOR     A
            LD      (HL),A
            INC     HL
            DEC     BC
            LD      A,B
            OR      C
            JR      NZ,L7690            ; (-008H)
            LD      (HL),A
            EXX     
            POP     DE
            LD      A,(L76F4)
            OR      A
            JR      Z,L76A5             ; (+004H)
            LD      HL,(L849C)
            RET     

L76A5:      LD      HL,(L76F2)
            PUSH    DE
            JP      L7503

L76AC:      LD      A,0FFH
            LD      (L76F4),A
L76B1:      CALL    L7495
            LD      A,(HL)
            CALL    L5E7B
            CALL    L74F4
            CALL    HLFTCH
            CP      02CH
            RET     NZ
            INC     HL
            JR      L76B1               ; (-013H)

L76C4:      ADD     HL,DE
            RET     NC
            JR      L76EF               ; (+027H)

L76C8:      PUSH    BC
            EX      DE,HL
            LD      C,L
            LD      A,H
            LD      HL,L0000
            CALL    L76D8
            LD      A,C
            CALL    L76D8
            POP     BC
            RET     

L76D8:      OR      A
            JR      Z,L76E9             ; (+00eH)
            LD      B,008H
L76DD:      ADD     HL,HL
            JR      C,L76EF             ; (+00fH)
            RLCA    
            JR      NC,L76E6            ; (+003H)
            ADD     HL,DE
            JR      C,L76EF             ; (+009H)
L76E6:      DJNZ    L76DD               ; (-00bH)
            RET     

L76E9:      LD      A,H
            LD      H,L
            LD      L,000H
            OR      A
            RET     Z
L76EF:      JP      04AFBH

L76F2:      NOP     
            NOP     
L76F4:      NOP     
L76F5:      SUB     041H
            CP      01AH
            JP      NC,L4AEC
            LD      BC,(07928H)
            LD      A,B
            OR      C
            JR      NZ,L7716            ; (+012H)
            CALL    L7442
L7707:      PUSH    DE
            PUSH    HL
            LD      L,C
            LD      H,B
            LD      B,A
            LD      C,A
            CALL    LDDEHL
            POP     HL
            LD      A,C
            POP     DE
            JP      L709D

L7716:      PUSH    DE
            CALL    L7495
            PUSH    HL
            INC     B
            LD      DE,KEYBUF
            LD      HL,(TMPEND)
            CALL    L74DC
            LD      B,000H
            LD      HL,(L7928)
            CALL    HLFTCH
            CP      QDPC
            JR      Z,L7760             ; (+02fH)
            CP      028H
            JR      NZ,L775D            ; (+028H)
L7735:      INC     HL
            INC     B
            PUSH    BC
            CALL    L7495
            LD      A,C
            POP     DE
            CP      E
            LD      C,E
            JR      NZ,L7752            ; (+011H)
            PUSH    DE
            LD      C,B
            INC     C
            PUSH    HL
            LD      HL,(TMPEND)
            LD      DE,KEYBUF
            CALL    L7953
            POP     HL
            POP     BC
            JR      Z,L7776             ; (+024H)
L7752:      CALL    HLFTCH
            CP      029H
            JR      Z,L7760             ; (+007H)
            CP      02CH
            JR      Z,L7735             ; (-028H)
L775D:      JP      L4AEC

L7760:      LD      HL,(TMPEND)
            LD      DE,KEYBUF
            LD      A,(HL)
            LD      (DE),A
            LD      B,A
L7769:      INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            DJNZ    L7769               ; (-006H)
            POP     HL
            POP     DE
            CALL    L77B2
            JR      L7707               ; (-06fH)

L7776:      LD      HL,(L792A)
            CALL    L5E78
            DEC     B
            JR      Z,L7797             ; (+018H)
            DEC     HL
L7780:      PUSH    BC
L7781:      CALL    L51A2
            CALL    L413E
            JP      Z,L4AEC
            CP      029H
            JP      Z,L4AEC
            CP      02CH
            JR      NZ,L7781            ; (-012H)
            POP     BC
            DJNZ    L7780               ; (-016H)
            INC     HL
L7797:      EX      DE,HL
            POP     HL
            EX      (SP),HL
            EX      DE,HL
            LD      BC,(07928H)
            PUSH    BC
            LD      BC,L0000
            LD      (07928H),BC
            CALL    L6E9C
            POP     HL
            LD      (L7928),HL
            POP     HL
            JP      HLFTCH

L77B2:      PUSH    DE
            JP      L7446

L77B6:      LD      HL,(VARST)
L77B9:      LD      DE,KEYBUF
            LD      A,(HL)
            OR      A
            RET     Z
            CP      040H
            JR      NC,L77EA            ; (+027H)
            CP      C
            JR      NZ,L77DF            ; (+019H)
            INC     HL
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L77DD            ; (+012H)
            LD      B,A
L77CC:      INC     DE
            INC     HL
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L77D7            ; (+005H)
            DJNZ    L77CC               ; (-008H)
            INC     HL
            SCF     
            RET     

L77D7:      INC     HL
            DJNZ    L77D7               ; (-003H)
            LD      A,C
            JR      L77E4               ; (+007H)

L77DD:      LD      A,C
            DEC     HL
L77DF:      AND     00FH
            INC     HL
            ADD     A,(HL)
            INC     HL
L77E4:      LD      E,A
            LD      D,000H
            ADD     HL,DE
            JR      L77B9               ; (-031H)

L77EA:      CP      C
            JR      Z,L7801             ; (+014H)
            INC     HL
L77EE:      LD      E,(HL)
            INC     HL
            LD      D,(HL)
            DEC     HL
            ADD     HL,DE
            JR      L77B9               ; (-03cH)

L77F5:      LD      A,C
            SUB     B
            CPL     
            LD      C,A
            LD      B,0FFH
            ADD     HL,BC
            POP     BC
L77FD:      DEC     HL
            DEC     HL
            JR      L77EE               ; (-013H)

L7801:      LD      A,(HL)
            EXX     
            LD      C,A
            LD      B,000H
            EXX     
            INC     HL
            PUSH    DE
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            INC     HL
            EX      DE,HL
            ADD     HL,DE
            EX      DE,HL
            POP     DE
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L77FD            ; (-018H)
            PUSH    BC
            LD      B,A
            LD      C,A
L7818:      INC     HL
            INC     DE
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L77F5            ; (-029H)
            DJNZ    L7818               ; (-008H)
            INC     HL
            POP     BC
            SCF     
            RET     

L7824:      CALL    L7495
            SET     6,C
            PUSH    HL
            CALL    L77B6
            JP      C,04AFEH
            LD      (HL),C
            EX      (SP),HL
            PUSH    HL
            DEC     HL
L7834:      CALL    L51A2
            OR      A
            JR      Z,L783E             ; (+004H)
            CP      03AH
            JR      NZ,L7834            ; (-00aH)
L783E:      POP     DE
            PUSH    HL
            INC     HL
            SBC     HL,DE
            LD      A,(KEYBUF)
            ADD     A,004H
            LD      C,A
            LD      B,000H
            LD      A,L
            ADD     HL,BC
            LD      B,A
            PUSH    HL
            EXX     
            POP     BC
            PUSH    BC
            PUSH    HL
            PUSH    DE
            PUSH    BC
            LD      HL,L0000
            ADD     HL,SP
            LD      DE,(TMPEND)
            DEC     H
            OR      A
            SBC     HL,DE
            LD      A,00CH
            JP      C,L4B2E
            POP     BC
            POP     DE
            POP     HL
            LD      HL,(TMPEND)
            PUSH    HL
            ADD     HL,BC
            EX      (SP),HL
            PUSH    HL
            LD      DE,(STRST)
            OR      A
            SBC     HL,DE
            LD      C,L
            LD      B,H
            POP     HL
            POP     DE
            LDDR    
            POP     DE
            RST     018H
            DB      01AH
            DEC     DE
            POP     HL
            EX      (SP),HL
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            LD      DE,KEYBUF
            LD      A,(DE)
            LD      (HL),A
            LD      B,A
L788E:      INC     DE
            INC     HL
            LD      A,(DE)
            LD      (HL),A
            DJNZ    L788E               ; (-006H)
            INC     HL
            PUSH    HL
            EXX     
            POP     HL
            CALL    LDHLDE
            LD      (HL),000H
            POP     HL
            RET     

L789F:      PUSH    DE
            CALL    L7495
            POP     IX
            PUSH    BC
            SET     6,C
            LD      DE,(0792AH)
            LD      (L792A),HL
            PUSH    DE
            CALL    L77B6
            JP      NC,04B0AH
            LD      DE,(07928H)
            LD      (L7928),HL
            PUSH    DE
            PUSH    IX
            DEC     HL
L78C1:      CALL    L51A2
            CALL    L413E
            JP      Z,L4AEC
            CP      QDPC
            JR      NZ,L78C1            ; (-00dH)
            INC     HL
            POP     DE
            PUSH    DE
            CALL    L6E9C
            POP     DE
            LD      HL,(L792A)
            POP     BC
            LD      (07928H),BC
            POP     BC
            LD      (0792AH),BC
            EX      DE,HL
            POP     BC
            LD      A,C
            CALL    L7912
            LD      BC,00005H
            ADD     HL,BC
            EX      DE,HL
            CALL    HLFTCH
            CP      028H
            RET     NZ
            PUSH    DE
            LD      B,001H
L78F6:      PUSH    BC
            CALL    L51A2
            POP     BC
            CALL    L4141
            JP      Z,L4AEC
            CP      028H
            JR      NZ,L7906            ; (+001H)
            INC     B
L7906:      CP      029H
            JR      NZ,L78F6            ; (-014H)
            DJNZ    L78F6               ; (-016H)
            INC     HL
            CALL    HLFTCH
            POP     DE
            RET     

L7912:      CP      003H
            JR      NZ,L791F            ; (+009H)
L7916:      LD      A,(L84CE)
            CP      003H
            RET     Z
            JP      04AF5H

L791F:      LD      A,(L84CE)
            CP      003H
            RET     NZ
            JP      04AF5H

L7928:      NOP     
            NOP     
L792A:      NOP     
            NOP     
L792C:      PUSH    HL
            CALL    L60B7
            LD      A,H
            OR      A
            JP      NZ,04AF2H
            LD      A,L
            CP      008H
            JP      NC,04AF2H
            RST     018H
            DB      012H
            POP     HL
            JR      C,L794E             ; (+00eH)
            JR      Z,L794E             ; (+00cH)
            LD      DE,0FFFFH
L7945:      CALL    L5E99
            LD      A,005H
            LD      (L84CE),A
            RET     

L794E:      LD      E,A
            LD      D,000H
            JR      L7945               ; (-00eH)

L7953:      LD      A,(DE)
            CP      (HL)
            RET     NZ
            PUSH    BC
            PUSH    DE
            PUSH    HL
            LD      B,C
L795A:      LD      A,(DE)
            CP      (HL)
            JR      NZ,L7963            ; (+005H)
            INC     DE
            INC     HL
            DJNZ    L795A               ; (-008H)
            XOR     A
L7963:      POP     HL
            POP     DE
            POP     BC
            RET     

L7967:      PUSH    HL
            LD      B,005H
            CALL    ?CLRHL
            POP     HL
            RET     

L796F:      CALL    L791F
L7972:      LD      A,(HL)
            OR      A
            RET     Z
            INC     HL
            LD      A,(HL)
            XOR     080H
            LD      (HL),A
            DEC     HL
            RET     

L797C:      CALL    L791F
            LD      C,A
            LD      A,080H
            JP      L798F

L7985:      LD      A,(L84CE)
            CP      003H
            JP      Z,L79D9
            LD      C,A
            XOR     A
L798F:      LD      (L7D89),HL
            PUSH    DE
            PUSH    HL
            LD      (07D8CH),SP
            INC     HL
            INC     DE
            LD      B,(HL)
            XOR     (HL)
            EX      DE,HL
            XOR     (HL)
            DEC     HL
            DEC     DE
            EX      DE,HL
            RLCA    
            LD      A,B
            LD      (L7D8B),A
            JP      C,L7A9A
            XOR     A
            CP      (HL)
            JP      Z,L79CE
            LD      A,(DE)
            OR      A
            JR      Z,L79B6             ; (+004H)
            SUB     (HL)
            JP      L7ADD

L79B6:      LD      SP,(07D8CH)
            POP     HL
            POP     DE
            EI      
            LD      A,(HL)
            OR      A
            JP      Z,L7E86
            LD      A,(L7D8B)
            AND     080H
            INC     HL
            RES     7,(HL)
            OR      (HL)
            LD      (HL),A
            DEC     HL
            RET     

L79CE:      LD      B,000H
            LD      A,005H
            LD      C,A
            EX      DE,HL
            LDIR    
            JP      L79B6

L79D9:      LD      B,(HL)
            LD      A,(DE)
            ADD     A,B
            JP      C,04AF8H
            LD      C,A
            PUSH    DE
            PUSH    HL
            PUSH    DE
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            ADD     HL,DE
            EX      DE,HL
            LD      HL,(TMPEND)
            CALL    L74DC
            EX      (SP),HL
            LD      B,(HL)
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            ADD     HL,DE
            EX      DE,HL
            POP     HL
            CALL    L74DC
            LD      A,C
            LD      DE,(TMPEND)
            LD      BC,(STRST)
            EX      DE,HL
            OR      A
            SBC     HL,BC
            EX      DE,HL
            LD      (TMPEND),HL
            POP     HL
            LD      (HL),A
            INC     HL
            LD      (HL),E
            INC     HL
            LD      (HL),D
            DEC     HL
            DEC     HL
            POP     DE
            RET     

L7A1C:      LD      A,(L84CE)
            CP      003H
            JR      NZ,L7A57            ; (+034H)
            PUSH    DE
            PUSH    HL
            LD      A,(DE)
            OR      (HL)
            JR      Z,L7A54             ; (+02bH)
            LD      A,(DE)
            CP      (HL)
            JR      C,L7A2E             ; (+001H)
            LD      A,(HL)
L7A2E:      OR      A
            JR      Z,L7A4E             ; (+01dH)
            INC     HL
            LD      C,(HL)
            INC     HL
            LD      B,(HL)
            EX      DE,HL
            INC     HL
            LD      E,(HL)
            INC     HL
            LD      D,(HL)
            LD      HL,(STRST)
            EX      DE,HL
            ADD     HL,DE
            EX      DE,HL
            ADD     HL,BC
            EX      DE,HL
            LD      B,A
            OR      A
            JR      Z,L7A54             ; (+00eH)
L7A46:      LD      A,(DE)
            CP      (HL)
            JR      NZ,L7A54            ; (+00aH)
            INC     DE
            INC     HL
            DJNZ    L7A46               ; (-008H)
L7A4E:      POP     DE
            POP     HL
            LD      A,(DE)
            CP      (HL)
            EX      DE,HL
            RET     

L7A54:      POP     HL
            POP     DE
            RET     

L7A57:      INC     DE
            INC     HL
            LD      A,(DE)
            DEC     DE
            XOR     (HL)
            RLCA    
            JR      NC,L7A63            ; (+004H)
            LD      A,(HL)
            DEC     HL
            RLCA    
            RET     

L7A63:      LD      A,(HL)
            DEC     HL
            RLCA    
            JR      NC,L7A6E            ; (+006H)
            CALL    L7A6E
            RET     Z
            CCF     
            RET     

L7A6E:      PUSH    DE
            PUSH    HL
            EX      DE,HL
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L7A8F            ; (+01aH)
            INC     DE
            INC     HL
            LD      A,(HL)
            OR      080H
            LD      B,A
            LD      A,(DE)
            OR      080H
            CP      B
            JR      NZ,L7A8F            ; (+00eH)
            LD      A,(L84CE)
            LD      B,A
            DEC     B
            DEC     B
L7A87:      INC     DE
            INC     HL
            LD      A,(DE)
            CP      (HL)
            JR      NZ,L7A8F            ; (+002H)
            DJNZ    L7A87               ; (-008H)
L7A8F:      POP     HL
            POP     DE
            RET     

L7A92:      POP     HL
            PUSH    HL
            CALL    L7967
            JP      L79B6

L7A9A:      CALL    L7A6E
            JR      Z,L7A92             ; (-00dH)
            JR      NC,L7AAA            ; (+009H)
            LD      A,(L7D8B)
            XOR     080H
            LD      (L7D8B),A
            SCF     
L7AAA:      EX      AF,AF'
            LD      A,(HL)
            OR      A
            JP      Z,L79CE
            LD      A,(DE)
            OR      A
            JP      Z,L79B6
            SUB     (HL)
            JR      C,L7ABF             ; (+007H)
            CP      020H
            JP      NC,L79CE
            JR      L7AC6               ; (+007H)

L7ABF:      NEG     
            CP      020H
            JP      NC,L79B6
L7AC6:      EX      AF,AF'
            JR      C,L7ACA             ; (+001H)
            EX      DE,HL
L7ACA:      EX      AF,AF'
            JP      L7B64

L7ACE:      LD      SP,(07D8CH)
            EI      
            POP     HL
            POP     DE
            LD      A,(07D93H)
            OR      A
            JP      Z,04AEFH
            RET     

L7ADD:      JR      NC,L7AE9            ; (+00aH)
            NEG     
            CP      020H
            JP      NC,L79B6
            EX      DE,HL
            JR      L7AEE               ; (+005H)

L7AE9:      CP      020H
            JP      NC,L79CE
L7AEE:      CALL    L7B24
            LD      A,H
            EXX     
            ADC     A,H
            EXX     
            LD      H,A
            LD      A,L
            EXX     
            ADC     A,L
            EXX     
            LD      L,A
            LD      A,D
            EXX     
            ADC     A,D
            EXX     
            LD      D,A
            LD      A,E
            EXX     
            ADC     A,E
            EXX     
            JR      NC,L7B11            ; (+00bH)
            RRA     
            RR      D
            RR      L
            RR      H
            INC     C
            JP      Z,L7ACE
L7B11:      LD      E,A
            LD      A,C
            EXX     
            LD      BC,00005H
            LD      HL,(L7D89)
            LD      (HL),A
            ADD     HL,BC
            DI      
            LD      SP,HL
            EXX     
            PUSH    HL
            PUSH    DE
            JP      L79B6

L7B24:      DI      
            LD      (07D8EH),SP
            EX      AF,AF'
            INC     HL
            LD      SP,HL
            EXX     
            POP     DE
            SET     7,E
            POP     HL
            OR      A
L7B32:      EX      AF,AF'
            CP      008H
            JR      C,L7B43             ; (+00cH)
            SUB     008H
            EX      AF,AF'
            RL      H
            LD      H,L
            LD      L,D
            LD      D,E
            LD      E,000H
            JR      L7B32               ; (-011H)

L7B43:      OR      A
            JR      Z,L7B54             ; (+00eH)
L7B46:      EX      AF,AF'
            OR      A
            RR      E
            RR      D
            RR      L
            RR      H
            EX      AF,AF'
            DEC     A
            JR      NZ,L7B46            ; (-00eH)
L7B54:      EXX     
            EX      DE,HL
            LD      C,(HL)
            INC     HL
            LD      SP,HL
            POP     DE
            SET     7,E
            POP     HL
            EX      AF,AF'
            LD      SP,(07D8EH)
            EI      
            RET     

L7B64:      CP      020H
            JP      NC,L79B6
            CALL    L7B24
            LD      A,H
            EXX     
            SBC     A,H
            EXX     
            LD      H,A
            LD      A,L
            EXX     
            SBC     A,L
            EXX     
            LD      L,A
            LD      A,D
            EXX     
            SBC     A,D
            EXX     
            LD      D,A
            LD      A,E
            EXX     
            SBC     A,E
            EXX     
L7B7F:      OR      A
            JR      Z,L7B98             ; (+016H)
L7B82:      BIT     7,A
            JR      NZ,L7B94            ; (+00eH)
            RL      H
            RL      L
            RL      D
            RLA     
            DEC     C
            JP      NZ,L7B82
            JP      L7A92

L7B94:      LD      E,A
            JP      L7B11

L7B98:      LD      A,C
            SUB     008H
            LD      C,A
            LD      A,D
            LD      D,L
            LD      L,H
            LD      H,000H
            JR      Z,L7BA5             ; (+002H)
            JR      NC,L7B7F            ; (-026H)
L7BA5:      JP      L7A92

L7BA8:      LD      C,A
            INC     HL
            INC     DE
            LD      A,(DE)
            XOR     (HL)
            LD      (L7D8B),A
            DEC     HL
            DEC     DE
            RET     

L7BB3:      CALL    L791F
            CALL    L7BA8
            PUSH    DE
            PUSH    HL
            LD      (07D8CH),SP
            LD      A,(HL)
            OR      A
            JP      Z,L7A92
            LD      A,(DE)
            OR      A
            JP      Z,L7A92
            INC     DE
            PUSH    DE
            POP     IX
            ADD     A,(HL)
            LD      E,A
            LD      A,000H
            ADC     A,A
            LD      D,A
            LD      (07D91H),DE
            INC     HL
            XOR     A
            LD      (L7D90),A
            LD      D,(HL)
            SET     7,D
            INC     HL
            LD      E,(HL)
            INC     HL
            PUSH    HL
            LD      H,A
            LD      L,A
            EXX     
            POP     HL
            LD      D,(HL)
            INC     HL
            LD      E,(HL)
            LD      B,A
            LD      C,A
            LD      H,A
            LD      L,A
            EXX     
            LD      A,(IX+000H)
            OR      080H
            LD      C,004H
L7BF6:      LD      B,008H
            OR      A
            JP      Z,L7C8B
L7BFC:      RLCA    
            JR      NC,L7C11            ; (+012H)
            EX      AF,AF'
            EXX     
            LD      A,C
            ADD     A,B
            LD      C,A
            ADC     HL,DE
            EXX     
            ADC     HL,DE
            JR      NC,L7C10            ; (+005H)
            LD      A,001H
            LD      (L7D90),A
L7C10:      EX      AF,AF'
L7C11:      SRL     D
            RR      E
            EXX     
            RR      D
            RR      E
            RR      B
            EXX     
            DJNZ    L7BFC               ; (-023H)
L7C1F:      INC     IX
            LD      A,(IX+000H)
            DEC     C
            JR      NZ,L7BF6            ; (-031H)
            EXX     
            LD      A,(L7D90)
            OR      A
            JR      Z,L7C44             ; (+016H)
            LD      DE,(07D91H)
            INC     DE
            LD      (07D91H),DE
            EXX     
            SCF     
            RR      H
            RR      L
            EXX     
            RR      H
            RR      L
            RR      C
L7C44:      BIT     7,C
            JR      Z,L7C62             ; (+01aH)
            LD      DE,00001H
            ADD     HL,DE
            EXX     
            LD      DE,L0000
            ADC     HL,DE
            EXX     
            JR      NC,L7C62            ; (+00dH)
            LD      DE,(07D91H)
            INC     DE
            LD      (07D91H),DE
            EXX     
            LD      H,080H
            EXX     
L7C62:      POP     IX
            PUSH    IX
            LD      (IX+004H),L
            LD      (IX+003H),H
            EXX     
            LD      (IX+002H),L
            LD      (IX+001H),H
            LD      HL,(07D91H)
            OR      A
            LD      DE,L0081
            SBC     HL,DE
            LD      A,H
            ADD     A,000H
            JP      M,L7C97
            JP      NZ,L7ACE
            LD      (IX+000H),L
            JP      L79B6

L7C8B:      LD      A,E
            EXX     
            LD      B,E
            LD      E,D
            LD      D,A
            EXX     
            LD      E,D
            LD      D,000H
            JP      L7C1F

L7C97:      LD      SP,(07D8CH)
            JP      L7A92

L7C9E:      CALL    L791F
            CALL    L7BA8
            PUSH    DE
            PUSH    HL
            LD      (07D8CH),SP
            LD      A,(DE)
            OR      A
            JP      Z,04AEFH
            EX      AF,AF'
            LD      A,(HL)
            OR      A
            JP      Z,L7A92
            EXX     
            ADD     A,081H
            LD      B,A
            LD      A,000H
            ADC     A,A
            EX      AF,AF'
            LD      C,A
            LD      A,B
            SUB     C
            LD      C,A
            EX      AF,AF'
            LD      B,A
            EX      AF,AF'
            LD      A,B
            SBC     A,000H
            JP      C,L7C97
            JP      NZ,L7ACE
            LD      A,C
            PUSH    AF
            EXX     
            INC     DE
            INC     HL
            LD      B,(HL)
            SET     7,B
            INC     HL
            LD      C,(HL)
            INC     HL
            PUSH    HL
            EX      DE,HL
            LD      D,(HL)
            SET     7,D
            INC     HL
            LD      E,(HL)
            INC     HL
            PUSH    HL
            LD      H,B
            LD      L,C
            EXX     
            POP     HL
            LD      D,(HL)
            INC     HL
            LD      E,(HL)
            POP     HL
            LD      A,(HL)
            INC     HL
            LD      L,(HL)
            LD      H,A
            EXX     
            LD      C,005H
L7CF0:      LD      B,008H
            XOR     A
L7CF3:      BIT     7,H
            JR      NZ,L7D07            ; (+010H)
            OR      A
L7CF8:      RLA     
            EXX     
            ADD     HL,HL
            EXX     
            ADC     HL,HL
            DJNZ    L7CF3               ; (-00dH)
            PUSH    AF
            DEC     C
            JR      NZ,L7CF0            ; (-014H)
            JP      L7D41

L7D07:      OR      A
            EXX     
            SBC     HL,DE
            EXX     
            SBC     HL,DE
            CCF     
            JR      C,L7CF8             ; (-019H)
            EXX     
            ADD     HL,DE
            EXX     
            ADC     HL,DE
            OR      A
            RLA     
            EXX     
            ADD     HL,HL
            EXX     
            ADC     HL,HL
            DJNZ    L7D26               ; (+007H)
            PUSH    AF
            LD      B,008H
            DEC     C
            JP      Z,L7D41
L7D26:      EXX     
            OR      A
            SBC     HL,DE
            EXX     
            SBC     HL,DE
            SCF     
            RLA     
            DJNZ    L7D37               ; (+006H)
            PUSH    AF
            LD      B,008H
            DEC     C
            JR      Z,L7D41             ; (+00aH)
L7D37:      EXX     
            ADD     HL,HL
            EXX     
            ADC     HL,HL
            JR      C,L7D26             ; (-018H)
            JP      L7CF3

L7D41:      POP     AF
            LD      H,A
            EXX     
            POP     AF
            LD      E,A
            POP     AF
            LD      D,A
            POP     AF
            LD      C,A
            POP     AF
            LD      B,A
            POP     AF
L7D4D:      BIT     7,B
            JR      NZ,L7D64            ; (+013H)
            EXX     
            SLA     H
            EXX     
            RL      E
            RL      D
            RL      C
            RL      B
            DEC     A
            JP      NZ,L7D4D
            JP      L7A92

L7D64:      EXX     
            RL      H
            EXX     
            JR      NC,L7D7B            ; (+011H)
            LD      HL,00001H
            ADD     HL,DE
            EX      DE,HL
            LD      HL,L0000
            ADC     HL,BC
            LD      B,H
            LD      C,L
            JR      NC,L7D7B            ; (+003H)
            LD      B,080H
            INC     A
L7D7B:      POP     HL
            PUSH    HL
            LD      (HL),A
            INC     HL
            LD      (HL),B
L7D80:      INC     HL
            LD      (HL),C
            INC     HL
            LD      (HL),D
            INC     HL
            LD      (HL),E
            JP      L79B6

L7D89:      NOP     
            NOP     
L7D8B:      NOP     
            NOP     
            NOP     
            NOP     
            NOP     
L7D90:      NOP     
            NOP     
            NOP     
            NOP     
L7D94:      INC     HL
            BIT     7,(HL)
            DEC     HL
            RET     

L7D99:      INC     (HL)
            RET     NZ
            JP      04AEFH

L7D9E:      LD      A,(HL)
            OR      A
            RET     Z
            DEC     (HL)
            RET     NZ
            JP      L7967

L7DA6:      PUSH    HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            INC     HL
            EX      DE,HL
            LD      HL,(MEMMAX)
            DEC     HL
            SBC     HL,DE
            JP      C,04AFBH
            POP     HL
            RET     

L7DB8:      PUSH    HL
            LD      (L8471),HL
            CALL    L7DA6
            EX      DE,HL
            LD      (L8473),HL
            CALL    L7DA6
            EX      DE,HL
            LD      (L8475),HL
            CALL    L7DA6
            LD      (08477H),DE
            POP     HL
            RET     

L7DD3:      CALL    L791F
            EX      DE,HL
            LD      A,(HL)
            OR      A
            JP      Z,L7E35
            EX      DE,HL
            LD      A,(HL)
            OR      A
            JP      Z,L7967
            CALL    L7D94
            JR      Z,L7E0F             ; (+028H)
            CALL    L7972
            EX      DE,HL
            PUSH    DE
            PUSH    HL
            LD      DE,L84D7
            CALL    L5E93
            LD      HL,L84D7
            CALL    L608D
            LD      A,(HL)
            OR      A
            JP      NZ,04AF2H
            POP     HL
            PUSH    HL
            CALL    L60B7
            BIT     0,L
            POP     DE
            POP     HL
            JR      Z,L7E0F             ; (+006H)
            CALL    L7E0F
            JP      L7972

L7E0F:      EX      DE,HL
            LD      A,(HL)
            OR      A
            JR      Z,L7E35             ; (+021H)
            CALL    L7D94
            JR      Z,L7E3B             ; (+022H)
            CALL    L7972
            CALL    L7E3B
            PUSH    DE
            PUSH    HL
            LD      DE,L84D7
            CALL    L5E90
            LD      HL,L84D7
            POP     DE
            CALL    L7C9E
            PUSH    DE
            CALL    L5E93
            POP     HL
            POP     DE
            RET     

L7E35:      PUSH    DE
            CALL    L5E90
            POP     HL
            RET     

L7E3B:      PUSH    DE
            LD      DE,L84D7
            CALL    L5E93
            POP     HL
            CALL    L8320
            LD      DE,L84D7
            CALL    L7BB3
            JP      L81FC

L7E4F:      CALL    L791F
            PUSH    DE
            PUSH    HL
            CALL    L60B7
            LD      A,L
            CPL     
            LD      E,A
            LD      A,H
            CPL     
            LD      D,A
            JR      L7E80               ; (+021H)

L7E5F:      LD      A,0A2H
            LD      BC,0B23EH
            LD      BC,0AA3EH
            LD      (L7E7B),A
            INC     A
            LD      (L7E7E),A
            CALL    L791F
            PUSH    DE
            PUSH    HL
            CALL    L60B7
            EX      DE,HL
            CALL    L60B7
            LD      A,H
L7E7B:      AND     D
            LD      D,A
            LD      A,L
L7E7E:      AND     E
            LD      E,A
L7E80:      POP     HL
            CALL    L5E99
            POP     DE
            RET     

L7E86:      INC     HL
            RES     7,(HL)
            DEC     HL
            RET     

L7E8B:      CALL    L7D94
            JP      Z,L604F
            CALL    L7EB0
            CALL    L604F
            LD      DE,L84D7
            CALL    L7A1C
            RET     Z
L7E9E:      LD      DE,L6112
            JP      L797C

L7EA4:      LD      DE,L6112
            JP      L7985

L7EAA:      LD      DE,L6112
            JP      L7A1C

L7EB0:      PUSH    HL
            PUSH    DE
            LD      DE,L84D7
            CALL    L5E93
            POP     DE
            POP     HL
            RET     

L7EBB:      CALL    L604F
            EX      DE,HL
            CALL    L604F
            EX      DE,HL
            CALL    L7EB0
            CALL    L7C9E
            CALL    L604F
            CALL    L7BB3
            PUSH    DE
            LD      DE,L84D7
            CALL    L797C
            POP     DE
            JP      L7972

L7EDA:      CALL    L604F
            EX      DE,HL
            CALL    L604F
            EX      DE,HL
            CALL    L7C9E
            JP      L604F

L7EE8:      LD      A,(HL)
            OR      A
            RET     Z
            CALL    L7D94
            JP      NZ,04AF2H
            CALL    L8320
            CALL    L7D9E
            JP      L81FC

L7EFA:      PUSH    HL
            EX      DE,HL
            CALL    L5E90
            POP     HL
            POP     BC
            RET     

L7F02:      LD      A,(HL)
            LD      C,H
L7F04:      ADD     A,B
            DEC     HL
L7F06:      PUSH    BC
            INC     HL
            LD      A,(HL)
            RES     7,(HL)
            DEC     HL
            PUSH    AF
            CALL    L7F17
            POP     AF
            POP     BC
            RLCA    
            RET     NC
            JP      L7972

L7F17:      CALL    L7EAA
            JR      C,L7F3A             ; (+01eH)
            CALL    L7DA6
            PUSH    DE
            PUSH    HL
            CALL    L5E93
            POP     DE
            PUSH    DE
            CALL    L5E90
            POP     HL
            POP     DE
            CALL    L7C9E
            CALL    L7F3A
            LD      DE,08194H
            CALL    L797C
            JP      L7E86

L7F3A:      LD      A,0FFH
            LD      (L80B0),A
            LD      DE,L7F02
            CALL    L7A1C
            JR      C,07F96H             ; (+04fH)
            LD      DE,L7F04
            CALL    L7A1C
            PUSH    AF
            CALL    L7DA6
            POP     AF
            PUSH    HL
            PUSH    DE
            CALL    L5E93
            POP     HL
            JR      C,L7F6E             ; (+014H)
            CALL    L7EA4
            EX      (SP),HL
            CALL    L797C
            POP     DE
            CALL    L7C9E
            CALL    07F96H
            LD      DE,08199H
            JP      L7985

L7F6E:      LD      DE,L7F91
            CALL    L7BB3
            CALL    L7EA4
            EX      (SP),HL
            LD      DE,L7F91
            CALL    L797C
            POP     DE
            CALL    L7C9E
            CALL    07F96H
            CALL    L7D99
            LD      DE,08199H
            CALL    L7985
            JP      L7D9E

L7F91:      LD      A,A
            LD      D,H
            INC     DE
            CALL    Z,0C5D0H
            PUSH    HL
            CALL    L7DB8
            POP     HL
            PUSH    HL
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      E,L
            LD      D,H
            CALL    L7BB3
            LD      DE,(08475H)
            POP     HL
            PUSH    HL
            CALL    L5E93
            LD      A,(L84CE)
            DEC     A
            CP      004H
            LD      B,A
            LD      HL,08137H
            JP      Z,L803D
            LD      B,00AH
            JP      L803D

L7FC2:      PUSH    BC
            LD      A,(HL)
            OR      A
            JP      Z,L7EFA
            LD      DE,08194H
            CALL    L797C
            CALL    L7972
            POP     BC
L7FD2:      PUSH    BC
            INC     HL
            LD      A,(HL)
            RES     7,(HL)
            AND     080H
            CPL     
            LD      (L80B0),A
            DEC     HL
            LD      DE,L818A
            PUSH    HL
            CALL    L7A1C
            JR      C,L7FF0             ; (+009H)
            CALL    L7C9E
            CALL    L608D
            CALL    L7BB3
L7FF0:      LD      DE,0818FH
            CALL    L7A1C
            JR      C,L8003             ; (+00bH)
            CALL    L797C
            LD      A,(L80B0)
            XOR     080H
            LD      (L80B0),A
L8003:      LD      DE,08194H
            CALL    L7A1C
            JR      C,L8014             ; (+009H)
            LD      DE,0818FH
            CALL    L797C
            CALL    L7E86
L8014:      CALL    L7DB8
            POP     HL
            PUSH    DE
            LD      DE,08199H
            CALL    L7A1C
            JR      NC,L8086            ; (+065H)
            POP     DE
            PUSH    HL
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      E,L
            LD      D,H
            CALL    L7BB3
            LD      DE,(08475H)
            POP     HL
            PUSH    HL
            CALL    L5E93
            LD      A,(L84CE)
            LD      B,A
            LD      HL,L80E7
L803D:      PUSH    BC
L803E:      PUSH    HL
            LD      HL,(L8475)
            LD      DE,(08477H)
            CALL    L7BB3
            POP     HL
            PUSH    HL
            LD      DE,(08473H)
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      DE,(08475H)
            CALL    L7BB3
            EX      DE,HL
            LD      HL,(L8471)
            CALL    L7985
            POP     HL
            LD      DE,00005H
            ADD     HL,DE
            POP     BC
            DJNZ    L803D               ; (-02dH)
            POP     HL
            POP     BC
            LD      A,(L80B0)
            INC     HL
            XOR     (HL)
            CPL     
            LD      (HL),A
            DEC     HL
            LD      A,(L84CE)
            CP      008H
            LD      A,(HL)
            JR      Z,L8082             ; (+006H)
            CP      05CH
L807E:      RET     NC
            JP      L7967

L8082:      CP      04DH
            JR      L807E               ; (-008H)

L8086:      LD      DE,08194H
            CALL    L797C
            CALL    L7E86
            POP     DE
            PUSH    HL
            PUSH    DE
            CALL    L5E93
L8095:      POP     HL
            LD      E,L
            LD      D,H
            CALL    L7BB3
            LD      DE,(08475H)
            CALL    L5E90
L80A2:      POP     DE
            PUSH    DE
            CALL    L5E90
            LD      A,(L84CE)
            LD      B,A
            LD      HL,L810F
            JR      L803D               ; (-073H)

L80B0:      NOP     
L80B1:      PUSH    BC
            PUSH    HL
            CALL    L7DA6
            EX      DE,HL
            LD      (L8479),HL
            CALL    L7DA6
            LD      (0847BH),DE
            POP     HL
            PUSH    HL
            CALL    L5E93
            LD      HL,(L8479)
            EX      DE,HL
            POP     HL
L80CB:      PUSH    HL
            CALL    L5E93
            LD      HL,(0847BH)
            CALL    L7FD2
            POP     DE
            PUSH    DE
            CALL    L5E93
            LD      HL,(L8479)
            CALL    L7FC2
            EX      DE,HL
            POP     HL
            CALL    L7C9E
            POP     BC
            RET     

L80E7:      LD      A,(HL)
L80E8:      XOR     D
            XOR     D
            XOR     D
            XOR     E
            LD      A,D
            EX      AF,AF'
            ADC     A,B
            ADC     A,B
            ADC     A,C
            LD      (HL),H
            RET     NC
            DEC     C
            NOP     
            RET     NC
L80F6:      LD      L,(HL)
            JR      C,L80E8             ; (-011H)
            DEC     E
            DEC     HL
            LD      H,A
            RST     010H
            LD      (L402B),A
            LD      H,B
            JR      NC,L8095            ; (-06eH)
            JR      NC,L80A2            ; (-063H)
            LD      E,B
            RST     010H
            CCF     
            SBC     A,A
            LD      A,(04A50H)
            SUB     (HL)
            DEC     SP
            ADD     A,D
L810F:      ADD     A,B
            ADD     A,B
            NOP     
            NOP     
            NOP     
            LD      A,H
            LD      HL,(0AAAAH)
            XOR     E
            LD      (HL),A
            OR      (HL)
            DEC     BC
            LD      H,B
            OR      (HL)
            LD      (HL),C
            LD      D,B
            DEC     C
            NOP     
            RET     NC
            LD      L,E
            SUB     E
            JP      P,0BC7DH
            LD      H,H
            RRCA    
            HALT    
            RST     000H
L812C:      ADD     A,B
            LD      E,H
            RET     

L812F:      RES     4,L
            LD      B,(HL)
            LD      D,H
            LD      D,A
            CCF     
            SBC     A,A
            LD      A,(0AA7FH)
            XOR     D
            XOR     D
            XOR     E
            LD      A,(HL)
            LD      C,H
            CALL    Z,0CDCCH
            LD      A,(HL)
            SUB     D
            LD      C,C
            INC     H
            SUB     D
            LD      A,L
            LD      H,E
            ADC     A,(HL)
            JR      C,L812F             ; (-01cH)
            LD      A,L
            CP      D
            LD      L,08BH
            AND     E
            LD      A,L
            DEC     E
            ADC     A,C
            RET     C
            SBC     A,(HL)
            LD      A,L
            ADC     A,B
            ADC     A,B
            ADC     A,B
            ADC     A,C
            LD      A,H
            LD      (HL),B
            RET     P
            RET     P
            POP     AF
            LD      A,H
            RST     010H
            SUB     H
            DEC     (HL)
            PUSH    HL
            LD      A,H
            LD      B,E
            INC     C
            JR      NC,L812C            ; (-03dH)
L8169:      LD      DE,L0000
            LD      A,(HL)
            OR      A
            JR      Z,L8178             ; (+008H)
            CALL    L7D94
            INC     DE
            JR      Z,L8178             ; (+002H)
            DEC     DE
            DEC     DE
L8178:      CALL    L5E99
            RET     

L817C:      LD      DE,0819EH
            JR      L8184               ; (+003H)

L8181:      LD      DE,0818FH
L8184:      PUSH    BC
            CALL    L7BB3
            POP     BC
            RET     

L818A:      ADD     A,E
            LD      C,C
            RRCA    
            JP      C,L82A2
            LD      C,C
            RRCA    
            JP      C,L81A2
            LD      C,C
            RRCA    
            JP      C,L80A2
            LD      C,C
            RRCA    
            JP      C,07BA2H
            LD      C,FDPC
            DEC     (HL)
L81A2:      INC     DE
L81A3:      PUSH    HL
            CALL    L60B7
            LD      A,000H
            CP      040H
            LD      A,(HL)
            CALL    NZ,BREAKX
            LD      E,A
            LD      D,000H
            POP     HL
            CALL    L5E99
            RET     

L81B7:      LD      A,(HL)
            OR      A
            JR      Z,L81C0             ; (+005H)
            CALL    L7D94
            JR      Z,L81CB             ; (+00bH)
L81C0:      PUSH    HL
            LD      HL,L4193
            LD      (L81FA),HL
            POP     HL
            XOR     A
            LD      R,A
L81CB:      PUSH    BC
            LD      DE,(081FAH)
            LD      A,R
            XOR     D
            RRC     A
            RRC     A
            RRC     A
            LD      D,A
            LD      A,R
            XOR     E
            RLC     A
            RLC     A
            LD      E,D
            LD      D,A
            LD      (081FAH),DE
            PUSH    HL
            INC     HL
            RES     7,D
            LD      (HL),D
            INC     HL
            LD      (HL),E
            INC     HL
            LD      A,R
            LD      (HL),A
            POP     HL
            LD      (HL),081H
            CALL    L7E9E
            POP     BC
            RET     

L81FA:      SUB     E
            LD      B,C
L81FC:      PUSH    BC
            LD      A,(HL)
            OR      A
            JP      Z,L7EFA
            INC     HL
            LD      A,(HL)
            LD      (L8312),A
            RES     7,(HL)
            DEC     HL
            LD      DE,L8467
            CALL    L7BB3
            PUSH    HL
            CALL    L7DA6
            PUSH    DE
            CALL    L5E93
            POP     HL
            CALL    L604F
            PUSH    HL
            CALL    L60B7
            XOR     A
            CP      H
            JP      NZ,04AEFH
            LD      A,L
            LD      (L8311),A
            POP     DE
            POP     HL
            PUSH    HL
            CALL    L797C
            PUSH    DE
            PUSH    HL
            CALL    L5E90
            POP     DE
            POP     HL
            CALL    L7D9E
            EX      DE,HL
            XOR     A
            LD      B,008H
L823D:      PUSH    BC
            PUSH    AF
            CALL    L7A1C
            JR      C,L824B             ; (+007H)
            CALL    L797C
            POP     AF
            SET     7,A
            PUSH    AF
L824B:      POP     AF
            RLC     A
            EX      DE,HL
            PUSH    AF
            CALL    L7D9E
            POP     AF
            EX      DE,HL
            POP     BC
            DJNZ    L823D               ; (-01bH)
            LD      (L8313),A
            PUSH    DE
            LD      DE,L845D
            LD      A,(L84CE)
            BIT     3,A
            JR      NZ,L8269            ; (+003H)
            LD      DE,L8462
L8269:      CALL    L7BB3
            POP     DE
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      A,(L84CE)
            BIT     3,A
            JP      Z,L8301
            CALL    L7D99
            LD      DE,L610D
            CALL    L7985
            CALL    L7D9E
            POP     DE
            PUSH    DE
            CALL    L7BB3
            LD      DE,L610D
            CALL    L7D9E
            CALL    L7985
            CALL    L7D99
            POP     DE
            PUSH    DE
            CALL    L7BB3
            LD      DE,L8458
            CALL    L7BB3
L82A2:      CALL    L7D99
            CALL    L7EA4
            CALL    L7D9E
            POP     DE
            PUSH    DE
            CALL    L7BB3
            CALL    L7EA4
            POP     DE
            PUSH    DE
            CALL    L7BB3
L82B8:      CALL    L7EA4
            EX      (SP),HL
            EX      DE,HL
            PUSH    DE
            CALL    L5E90
            POP     HL
            LD      DE,L8408
            LD      A,(L8313)
            LD      B,008H
L82CA:      RLC     A
            JR      NC,L82D5            ; (+007H)
            PUSH    AF
            PUSH    BC
            CALL    L7BB3
            POP     BC
            POP     AF
L82D5:      INC     DE
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            DJNZ    L82CA               ; (-012H)
            LD      A,(L8311)
            ADD     A,(HL)
            JP      C,04AEFH
            LD      (HL),A
            POP     DE
            CALL    L7BB3
            POP     BC
            LD      A,(L8312)
            RLC     A
            RET     NC
            PUSH    BC
            PUSH    DE
            PUSH    HL
            CALL    L5E93
            POP     DE
            PUSH    DE
            CALL    L5E90
            POP     HL
            POP     DE
            CALL    L7C9E
            POP     BC
            RET     

L8301:      POP     DE
            PUSH    DE
            CALL    L7BB3
            LD      A,(HL)
            OR      A
            CALL    NZ,L7D9E
            CALL    L7985
            JP      L82B8

L8311:      NOP     
L8312:      NOP     
L8313:      NOP     
L8314:      PUSH    BC
            CALL    L8320
            LD      DE,L83F9
            CALL    L7BB3
            POP     BC
            RET     

L8320:      PUSH    BC
            CALL    L7D94
            JP      NZ,04AF2H
            LD      A,(HL)
            OR      A
            JP      Z,04AF2H
            SUB     081H
            LD      (L83F8),A
            LD      (HL),081H
            XOR     A
            LD      B,008H
            LD      DE,L8408
L8339:      PUSH    BC
            PUSH    AF
            CALL    L7A1C
            JR      C,L834F             ; (+00fH)
            PUSH    HL
            LD      HL,00028H
            ADD     HL,DE
            EX      DE,HL
            EX      (SP),HL
            CALL    L7BB3
            POP     DE
            POP     AF
            SET     7,A
            PUSH    AF
L834F:      POP     AF
            RLC     A
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            INC     DE
            POP     BC
            DJNZ    L8339               ; (-021H)
            DB      022H
            LD      (HL),C
            ADD     A,H
            CALL    L7DA6
            EX      DE,HL
            LD      E,A
            LD      D,000H
            CALL    L5E99
            LD      A,(HL)
            OR      A
            JR      Z,L836E             ; (+003H)
            SUB     008H
            LD      (HL),A
L836E:      LD      A,(L83F8)
            CP      080H
            JR      C,L8377             ; (+002H)
            NEG     
L8377:      PUSH    HL
            CALL    L7DA6
            EX      DE,HL
            LD      (L8473),HL
            LD      E,A
            LD      D,000H
            CALL    L5E99
            LD      A,(L83F8)
            AND     080H
            INC     HL
            OR      (HL)
            LD      (HL),A
            DEC     HL
            EX      DE,HL
            POP     HL
            CALL    L7985
            LD      A,(L84CE)
            LD      DE,0846CH
            CP      005H
            JR      Z,L83A0             ; (+003H)
            LD      DE,L845D
L83A0:      CALL    L7BB3
            PUSH    HL
            LD      DE,(08473H)
            LD      HL,(L8471)
            PUSH    HL
            PUSH    DE
            CALL    L5E93
            POP     HL
            CALL    L7EA4
            EX      (SP),HL
            CALL    L797C
            POP     DE
            CALL    L7C9E
            PUSH    DE
            CALL    L5E93
            POP     HL
            PUSH    DE
            LD      E,L
            LD      D,H
            CALL    L7BB3
            POP     DE
            PUSH    HL
            PUSH    DE
            CALL    L5E93
            POP     HL
            LD      DE,L8403
            CALL    L7985
            EX      DE,HL
            POP     HL
            CALL    L7BB3
            LD      DE,L610D
            CALL    L7D99
            CALL    L7985
            CALL    L7D9E
            EX      DE,HL
            LD      HL,(L8471)
            CALL    L7BB3
            LD      DE,L83FE
            CALL    L7BB3
            POP     DE
            CALL    L7985
            POP     BC
            RET     

L83F8:      NOP     
L83F9:      LD      A,A
            LD      E,(HL)
            LD      E,E
            RET     C
            XOR     C
L83FE:      LD      A,A
            LD      C,H
            CALL    Z,0CDCCH
L8403:      ADD     A,C
            LD      D,L
            LD      D,L
            LD      D,L
            LD      D,(HL)
L8408:      ADD     A,C
            DEC     (HL)
            INC     B
            DI      
            INC     (HL)
            ADD     A,C
            JR      L8447               ; (+037H)

            RET     P
            LD      D,D
            ADD     A,C
            DEC     BC
L8414:      SUB     L
            POP     BC
            CALL    PO,00581H
            XOR     D
            JP      08168H

            LD      (BC),A
            CALL    09986H
            ADD     A,C
            LD      BC,0D164H
            CALL    P,L0081
            OR      C
            IN      D,(C)
            ADD     A,C
            NOP     
            LD      E,B
            RST     010H
            OUT     (080H),A
            DEC     (HL)
            INC     B
            DI      
            INC     (HL)
            ADD     A,B
            LD      D,A
            LD      B,H
            CALL    M,L80CB
            LD      L,D
            RET     NZ
            ADD     A,0E8H
            ADD     A,B
            LD      (HL),L
            DEC     H
            LD      A,L
            LD      D,080H
            LD      A,D
            ADD     A,E
L8447:      OR      D
            CALL    C,L7D80
            LD      A,00CH
            DEC     C
            ADD     A,B
            LD      A,(HL)
            SBC     A,(HL)
            LD      DE,0805DH
            LD      A,A
            LD      C,(HL)
            BIT     3,D
L8458:      LD      A,D
            EX      AF,AF'
            ADC     A,B
            ADC     A,B
            ADC     A,C
L845D:      ADD     A,B
            LD      SP,L1772
            RET     M
L8462:      ADD     A,B
            LD      SP,L1772
            RET     M
L8467:      ADD     A,C
            JR      C,L8414             ; (-056H)
            DEC     SP
            LD      HL,(03180H)
            LD      (HL),D
            RLA     
            RET     M
L8471:      DS      2, 000H
L8473:      DS      2, 000H
L8475:      DS      4, 000H
L8479:      DS      4, 000H
L847D:      DS      2, 000H
L847F:      DS      2, 000H
L8481:      DS      2, 000H
L8483:      DS      2, 000H
L8485:      DB      000H
L8486:      DB      000H
L8487:      DS      2, 000H
L8489:      DS      2, 000H
L848B:      DS      4, 000H
L848F:      DB      000H
L8490:      DS      2, 000H
            DB      015H
            DB      000H
            DB      0EBH
            DB      0FFH
            DS      2, 000H
L8498:      DS      2, 000H
L849A:      DS      2, 000H
L849C:      DS      7, 000H
L84A3:      DB      000H
L84A4:      DS      3, 000H
L84A7:      DB      000H
L84A8:      DS      4, 000H
L84AC:      DB      000H
L84AD:      DS      8, 000H
L84B5:      DS      25, 000H
L84CE:      DB      008H                ; Splash screen / banner text
L84CF:      DS      8, 000H
L84D7:      DS      8, 000H
L84DF:      DS      8, 000H
L84E7:      DB      016H
            DB      00DH
            DB      020H
            DS      38, 0D7H
            DS      4, 020H
            DB      "BASIC ",005H,"INTERPRETER"
            DS      2, 020H
            DB      006H,"MZ-5Z"
            DS      2, 030H
            DB      "8 V1.1Z ",00DH
            DS      3, 020H
            DB      043H,005H,"OPYRIGHT ",006H,"(C) 1984 ",005H,"BY ",006H,"SHARP CORP."
            DS      5, 020H
            DS      38, 0CFH
            DB      020H,00DH,020H,005H,"30134 BYTES FR"
            DS      2, 045H
            DS      2, 00DH
            DS      93, 000H

; =============================================================================
; $RFS - ROM Filing System device module (Direct SD Card Access)
;
; V1.0Z: Bypasses the RFS ROM entirely. All SD card access is done via
; direct SPI through the picoZ80 hardware SPI emulation at EFFBh/EFFCh.
;
; Only OUT(E3H) is needed to map MMIO for SPI port access, and OUT(E1H)
; to return to DRAM. No ROM banking, no BNKSELUSER, no lower memory
; switching required.
;
; SPI protocol:
;   SPIOUT: LD (HWSPIDATA),A ; LD (HWSPISTART),A  (send byte)
;   SPIIN:  LD (HWSPISTART),A ; LD A,(HWSPIDATA)   (receive byte)
;
; SD card sector read (CMD17):
;   CS LOW -> CMD17(0x51) + 4-byte sector + 0xFF -> poll R1(0x00) ->
;   poll data token(0xFE) -> read 512 bytes -> 2 CRC -> CS HIGH
;
; SD image layout: each drive = 0x8010 sectors (directory + file blocks).
;   Drive 0 at sector 0, drive 1 at 0x8010, drive 2 at 0x10020, etc.
;   RFS1: = SD drive 3, RFS7: = SD drive 9 (SDDRIVENO = ZCH + 3, offset +3 skips boot drives 0-2).
;
; Directory: 16 entries per sector, 32 bytes each:
;   [0] FLAG1 (bit7=active), [1] ATRB, [2..18] NAME (17, null-term),
;   [19..22] START_SECTOR (4 bytes, big-endian),
;   [23..24] SIZE (2 bytes, LE), [25..26] DTADR (2 bytes, LE),
;   [27..28] EXADR (2 bytes, LE), [29..31] reserved.
;
; Device chain: $QD -> $RFS -> $USR -> NULL
; =============================================================================

; ---------------------------------------------------------------------------
; $RFS Device Descriptor (29 bytes header + 22 bytes state = 51 bytes)
; ---------------------------------------------------------------------------
$RFS:       DW      $USR                ; Next device = $USR
            DB      "RFS",000H          ; Device name
            DB      05FH                ; flags1: bit5=1 ZINP-based DIR, bit6=1 ZRO enum capable
            DB      007H                ; flags2: max channel 7 (bit7 must be 0, SVC #39 errors if set)
            DB      0FFH                ; dirmx: 255 (max directory entries)
            DW      RFSINIT             ; ZINIT handler
            DW      RFSRO               ; ZRO handler (LOAD: find file, extract header)
            DW      RFSWO               ; ZWO handler (SAVE: sets ELMD20 SIZE)
            DW      RFSSTRT             ; ZCL/ZSTRT handler
            DW      00000H              ; ZKL (not used)
            DW      RFSINP_DIR          ; ZINP handler (DIR buffer fill + LOAD data)
            DW      RFSOUT              ; ZOUT handler (triggers RFSWO on first call during SAVE)
            DW      00000H              ; ZDELT (not used)
            DW      RFSDIR              ; ZWDIR handler (stub)
            DW      RFSFREE             ; ZFREE handler
; Device state area (22 bytes) - BASIC reads/writes state at descriptor+29..+50.
; Without this, BASIC reads into code bytes causing "Disk mismatch" errors.
RFSST00:    DB      000H                ; +29: state byte 0
RFSST01:    DB      000H                ; +30: state byte 1
RFSST02:    DB      000H                ; +31: state byte 2
RFSST03:    DS      3, 000H             ; +32..+34: state bytes 3-5
RFSST06:    DB      000H                ; +35: state byte 6
RFSST07:    DS      2, 000H             ; +36..+37: state bytes 7-8
RFSST09:    DB      000H                ; +38: state byte 9
RFSST10:    DB      000H                ; +39: state byte 10
RFSST11:    DB      000H                ; +40: state byte 11
RFSST12:    DB      000H                ; +41: state byte 12
RFSST13:    DS      4, 000H             ; +42..+45: state bytes 13-16
RFSST17:    DS      4, 000H             ; +46..+49: state bytes 17-20
RFSST21:    DB      000H                ; +50: state byte 21

; ---------------------------------------------------------------------------
; RFSFREE - Free space query (not supported, return carry)
; ---------------------------------------------------------------------------
RFSFREE:    XOR     A
            SCF
            RET

; ---------------------------------------------------------------------------
; RFSINIT - Initialize RFS device
; ---------------------------------------------------------------------------
RFSINIT:    XOR     A
            LD      (SAVE_PENDING),A    ; Clear at start of any operation
            LD      (DIR_FILLED),A      ; Clear so next ZINP does DIR fill
            LD      (DIR_ENTRY_IDX),A   ; Reset directory entry enumerator
            LD      A,0FFH
            LD      (DIRSECBUF),A       ; Invalidate sector cache
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3, skipping boot drives 0-2
            LD      (SDDRIVENO),A
            RET

; ---------------------------------------------------------------------------
; RFSSTRT - Start/Open device (A=0 means open, A!=0 means close)
; ---------------------------------------------------------------------------
RFSSTRT:    OR      A
            RET     NZ                  ; Close = just return
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3, skipping boot drives 0-2
            LD      (SDDRIVENO),A
            ;
            ; V1.1Y: Reset RFSRO state so file search starts from entry 0.
            ; Without this, LOAD after DIR/SAVE fails: RFSRO resumes from
            ; the post-DIR DIR_ENTRY_IDX position and misses files at earlier
            ; directory indices ("File not found" for files that exist).
            XOR     A
            LD      (DIR_ENTRY_IDX),A
            LD      A,0FFH
            LD      (DIRSECBUF),A       ; Invalidate sector cache (stale after SAVE)
            RET

; ---------------------------------------------------------------------------
; RFSDIR - ZWDIR handler (stub, not called with bit5=1 in flags1)
; ---------------------------------------------------------------------------
RFSDIR:     XOR     A
            RET

; ---------------------------------------------------------------------------
; RFSWO - Write Open (ZWO handler)
;
; BASIC calls ZWO from BOTH the OPEN handler and L1CE4 SAVE path, THEN
; calls ZOUT.  If we do the actual write here, the file gets created
; multiple times and the premature directory entry triggers "Already exist"
; during BASIC's own duplicate check.
;
; RFSWO calculates SIZE = TEXTED - TEXTST and stores it in ELMD20.
; This is essential because the SAVE path for bit-5 devices never populates
; ELMD20, leaving RFSDOWRITE to read SIZE=0.  The actual SD write is still
; deferred to RFSOUT (ZOUT) via the SAVE_PENDING one-shot flag.
; ---------------------------------------------------------------------------
RFSWO:      LD      HL,(TEXTED)         ; End of BASIC text area
            LD      DE,(TEXTST)         ; Start of BASIC text area
            OR      A
            SBC     HL,DE               ; HL = program size in bytes
            LD      (ELMD20),HL         ; Store SIZE for RFSDOWRITE
            XOR     A                   ; A=0, NC = success
            RET

; ---------------------------------------------------------------------------
; RFSDOWRITE - Actual SD card write (called from RFSOUT only)
;
; Performs the complete save operation:
;   1. Finds a free directory entry
;   2. Calculates the file's start sector from entry number
;   3. Writes file data sector-by-sector (toggling DRAM/MMIO)
;   4. Re-reads directory sector, fills entry, writes it back
;
; Entry: ELMD filled with file header (ATRB, NAME, SIZE, DTADR, EXADR)
;        For type 2 (BASIC): ELMD20=SIZE, source data at TEXTST
;        For type 1 (machine code): ELMD20=DTADR, ELMD18=SIZE
; Exit:  NC = success, C = error
; ---------------------------------------------------------------------------
RFSDOWRITE: PUSH    HL
            PUSH    DE
            PUSH    BC
            ;
            ; Get file info from ELMD — field mapping depends on file type.
            ; BASIC SAVE (type 2) stores SIZE in ELMD20 and source is TEXTST.
            ; Machine code SAVE (type 1) uses ELMD18=SIZE, ELMD20=DTADR.
            LD      A,(ELMD)
            CP      002H
            JR      NZ,WO_NONBASIC
            ;
            ; Type 2 (BASIC): SIZE in ELMD20, source at TEXTST
            LD      HL,(ELMD20)         ; SIZE (program length)
            LD      (FILE_SIZE),HL
            LD      HL,(TEXTST)         ; Source = start of BASIC text
            LD      (FILE_LOADADDR),HL
            JR      WO_GOTINFO
            ;
WO_NONBASIC:
            ; Non-BASIC: SIZE in ELMD18, source at ELMD20
            LD      HL,(ELMD18)         ; SIZE
            LD      (FILE_SIZE),HL
            LD      HL,(ELMD20)         ; DTADR (source address for data)
            LD      (FILE_LOADADDR),HL
WO_GOTINFO:
            ;
            ; Set up drive number
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3
            LD      (SDDRIVENO),A
            ;
            ; Enter MMIO mode
            CALL    MMIO_ON
            ;
            ; Reset directory cache
            LD      A,0FFH
            LD      (DIRSECBUF),A
            ;
            ; --- Find free directory entry ---
            LD      E,000H
            LD      B,000H              ; 256 entries (DJNZ wraps)
WO_SCAN:    PUSH    BC
            PUSH    DE
            CALL    GETSDDIRENT
            POP     DE
            POP     BC
            OR      A
            JP      NZ,WO_IOERR        ; SD read error
            LD      A,(HL)
            BIT     7,A
            JR      Z,WO_FOUND         ; Bit 7 clear = free slot
            INC     E
            DJNZ    WO_SCAN
            JP      WO_IOERR           ; Directory full
            ;
WO_FOUND:   ; E = free entry number
            LD      A,E
            LD      (SAVE_DIRENT),A
            ;
            ; --- Calculate start sector: E * 128 + 16 ---
            ; E * 128 = E << 7, result fits 16 bits (max 255*128=32640)
            LD      H,000H
            LD      L,E                 ; HL = entry number
            ADD     HL,HL               ; *2
            ADD     HL,HL               ; *4
            ADD     HL,HL               ; *8
            ADD     HL,HL               ; *16
            ADD     HL,HL               ; *32
            ADD     HL,HL               ; *64
            ADD     HL,HL               ; *128
            LD      DE,00010H           ; +16 (directory sectors)
            ADD     HL,DE
            LD      D,000H
            LD      E,000H              ; DEHL = relative sector
            CALL    RFSGETSECTR         ; DEHL = absolute sector
            CALL    SETLBAADDR          ; Store in SDSTARTSEC
            ;
            ; --- Write file data sector-by-sector ---
WO_WLOOP:   LD      HL,(FILE_SIZE)
            LD      A,H
            OR      L
            JR      Z,WO_WDONE         ; No data left
            ;
            ; Map DRAM to read source data
            OUT     (PORT_DRAM),A
            ;
            ; Calculate bytes to copy: min(FILE_SIZE, 512)
            LD      HL,(FILE_SIZE)
            LD      DE,00200H           ; 512
            OR      A
            SBC     HL,DE
            JR      C,WO_PARTIAL
            ;
            ; Full sector: remaining = HL
            LD      (FILE_SIZE),HL
            LD      BC,00200H
            JR      WO_COPY
            ;
WO_PARTIAL: ; Less than 512 bytes left
            ADD     HL,DE               ; Restore HL = FILE_SIZE
            LD      B,H
            LD      C,L                 ; BC = bytes to copy
            PUSH    BC                  ; Save actual byte count
            LD      HL,0
            LD      (FILE_SIZE),HL
            ;
            ; Zero-fill SECTORBUF first for partial sector
            LD      HL,SECTORBUF
            LD      DE,SECTORBUF + 1
            PUSH    BC
            LD      BC,001FFH           ; 511 bytes
            LD      (HL),000H
            LDIR
            POP     BC
            POP     BC                  ; Restore actual byte count
            ;
WO_COPY:    LD      HL,(FILE_LOADADDR)
            LD      DE,SECTORBUF
            LDIR                        ; Copy source data to SECTORBUF
            LD      (FILE_LOADADDR),HL  ; Update source address
            ;
            ; Map MMIO for SPI write
            OUT     (PORT_MMIO),A
            ;
            ; Write SECTORBUF to current sector
            CALL    SD_WRITE
            OR      A
            JP      NZ,WO_IOERR
            ;
            ; Increment sector for next write
            CALL    INC_STARTSEC
            JR      WO_WLOOP
            ;
            ; --- Write directory entry ---
WO_WDONE:   ; Ensure MMIO is active
            OUT     (PORT_MMIO),A
            ;
            ; Re-read the directory sector containing our entry
            LD      A,0FFH
            LD      (DIRSECBUF),A       ; Force re-read
            LD      A,(SAVE_DIRENT)
            LD      E,A
            PUSH    DE
            CALL    GETSDDIRENT         ; HL = entry in SECTORBUF
            POP     DE
            OR      A
            JP      NZ,WO_IOERR
            ;
            ; HL = 32-byte entry in SECTORBUF. Fill it.
            ; FLAG1 = 0x80 (active)
            LD      (HL),080H
            INC     HL
            ;
            ; ATRB: remap type 02 -> 05 for SD storage
            LD      A,(ELMD)            ; Disk BASIC type
            CP      002H
            JR      NZ,WO_AT1
            LD      A,005H              ; BTX -> MZ-700 BASIC tape type
WO_AT1:     LD      (HL),A
            INC     HL
            ;
            ; NAME: 17 bytes from ELMD1, CR -> null
            PUSH    DE
            LD      DE,ELMD1
            LD      B,17
WO_NM:      LD      A,(DE)
            CP      00DH                ; CR?
            JR      NZ,WO_NM1
            XOR     A                   ; CR -> null for SD
WO_NM1:     LD      (HL),A
            INC     HL
            INC     DE
            DJNZ    WO_NM
            POP     DE
            ;
            ; START_SECTOR: 4 bytes (calculate from entry number)
            PUSH    HL                  ; Save directory entry pointer
            LD      A,(SAVE_DIRENT)
            LD      H,000H
            LD      L,A
            ADD     HL,HL               ; *2
            ADD     HL,HL               ; *4
            ADD     HL,HL               ; *8
            ADD     HL,HL               ; *16
            ADD     HL,HL               ; *32
            ADD     HL,HL               ; *64
            ADD     HL,HL               ; *128
            LD      DE,00010H           ; +16
            ADD     HL,DE               ; HL = relative start sector (16-bit)
            ; Store as big-endian 32-bit (D=0, E=0, H, L)
            EX      DE,HL               ; DE = sector
            POP     HL                  ; HL = entry pointer
            LD      (HL),000H           ; Byte 0 (MSB) = 0
            INC     HL
            LD      (HL),000H           ; Byte 1 = 0
            INC     HL
            LD      (HL),D              ; Byte 2
            INC     HL
            LD      (HL),E              ; Byte 3 (LSB)
            INC     HL
            ;
            ; SIZE: 2 bytes LE — read original from ELMD (FILE_SIZE is 0 after write loop).
            PUSH    DE
            LD      A,(ELMD)
            CP      002H
            JR      NZ,WO_SZ_NB
            LD      DE,(ELMD20)         ; BASIC: SIZE in ELMD20
            JR      WO_SZ_OK
WO_SZ_NB:  LD      DE,(ELMD18)         ; Non-BASIC: SIZE in ELMD18
WO_SZ_OK:  LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            ;
            ; DTADR: 2 bytes LE — read original from ELMD/TEXTST.
            LD      A,(ELMD)
            CP      002H
            JR      NZ,WO_DA_NB
            LD      DE,(TEXTST)         ; BASIC: load address is TEXTST
            JR      WO_DA_OK
WO_DA_NB:  LD      DE,(ELMD20)         ; Non-BASIC: DTADR in ELMD20
WO_DA_OK:  LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            ;
            ; EXADR: 2 bytes LE from ELMD22
            LD      DE,(ELMD22)
            LD      (HL),E
            INC     HL
            LD      (HL),D
            INC     HL
            ;
            ; RESERVED: 3 bytes zero
            LD      (HL),000H
            INC     HL
            LD      (HL),000H
            INC     HL
            LD      (HL),000H
            POP     DE
            ;
            ; Write the directory sector back to SD
            ; Calculate directory sector address
            LD      A,(SAVE_DIRENT)
            SRL     A
            SRL     A
            SRL     A
            SRL     A                   ; A = dir sector number (0-15)
            LD      DE,0
            LD      H,0
            LD      L,A
            CALL    RFSGETSECTR         ; DEHL = absolute dir sector
            CALL    SETLBAADDR
            CALL    SD_WRITE
            OR      A
            JR      NZ,WO_IOERR
            ;
            ; Success
            CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            XOR     A                   ; NC = success
            RET
            ;
WO_IOERR:   CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            XOR     A
            SCF                         ; C = error
            RET

; ---------------------------------------------------------------------------
; RFSOUT - Write Data (ZOUT handler for SAVE)
;
; BASIC's SAVE path calls ZOUT after ZWO.  Since RFSWO is now a no-op,
; RFSOUT is the single entry point for the actual write.  On the first
; call (SAVE_PENDING=0, cleared by RFSINIT), we call RFSDOWRITE to do
; the full SD card write in one shot (reading directly from DTADR in
; memory). Sets SAVE_PENDING=1 after, so subsequent calls are no-ops.
; ---------------------------------------------------------------------------
RFSOUT:     LD      A,(SAVE_PENDING)
            OR      A
            JR      NZ,RFSOUT_NOP       ; Already written (1 = done), return success
            INC     A                   ; A = 1
            LD      (SAVE_PENDING),A    ; Mark as done (one-shot)
            JP      RFSDOWRITE          ; Do the full SD write and return
RFSOUT_NOP: XOR     A                   ; A=0, NC = success
            RET

; ---------------------------------------------------------------------------
; RFSRO - Read One directory entry (ZRO handler).
;
; V1.1V: Rewritten as directory entry enumerator.
;
; ZRO is called by BASIC for two purposes:
;   1. L1C0F file search loop (HL=KEYBUF) — enumerate entries, compare names
;   2. L1D53 DIR buffer fill loop (HL=L2000+offset) — fill 32-byte entries
;
; Each call returns one valid 32-byte MZF directory entry at the buffer
; pointed to by HL (the caller's HL passed through IOCALL).  The internal
; DIR_ENTRY_IDX counter advances across calls; RFSINIT resets it to 0.
;
; Entry: HL = target buffer (32 bytes, set by caller before IOCALL)
; Exit:  NC = valid entry written to (HL), A = entry type (ATRB byte)
;        CF = no more entries (A=0)
; ---------------------------------------------------------------------------
RFSRO:
            ; Save target buffer address for CONVERT_ENTRY
            LD      (DIR_BUFPTR),HL
            ;
            ; Save registers on BASIC stack (before MMIO_ON switches stacks)
            PUSH    HL
            PUSH    DE
            PUSH    BC
            ;
            ; Set up drive number
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3
            LD      (SDDRIVENO),A
            ;
            ; Enter MMIO mode (switches to temp stack, enables SPI)
            CALL    MMIO_ON
            ;
            ; Clear 32 bytes at target buffer (below D000H, accessible in MMIO)
            LD      HL,(DIR_BUFPTR)
            LD      B,32
RFSRO_CLR:  LD      (HL),000H
            INC     HL
            DJNZ    RFSRO_CLR
            ;
RFSRO_NEXT:
            ; Get current entry index
            LD      A,(DIR_ENTRY_IDX)
            LD      E,A
            ;
            ; Read directory entry from SD card
            CALL    GETSDDIRENT         ; HL=entry in SECTORBUF, A=status
            OR      A
            JR      NZ,RFSRO_END        ; SD read error → done
            ;
            ; Check if entry is active (FLAG1 bit 7 set)
            LD      A,(HL)
            BIT     7,A
            JR      Z,RFSRO_SKIP        ; Inactive, skip
            ;
            ; Check ATRB at offset 1 — reject 0x00 (empty) and 0xFF (uninit)
            INC     HL
            LD      A,(HL)
            DEC     HL                  ; HL back to entry start
            OR      A
            JR      Z,RFSRO_SKIP        ; ATRB=0, skip
            CP      0FFH
            JR      Z,RFSRO_SKIP        ; ATRB=0xFF, skip
            ;
            ; Valid entry: convert SD format to MZF format at DIR_BUFPTR
            ; (DIR_BUFPTR was set at RFSRO entry from caller's HL)
            ; HL = SD entry in SECTORBUF
            CALL    CONVERT_ENTRY
            ;
            ; Advance entry index for next call
            LD      A,(DIR_ENTRY_IDX)
            INC     A
            LD      (DIR_ENTRY_IDX),A
            ;
            ; Debug: show entry delivered
            PUSH    AF
            OUT     (PORT_DRAM),A       ; Switch to DRAM for debug I/O
            LD      A,'R'
            OUT     (050H),A
            LD      A,'O'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,(DIR_ENTRY_IDX)
            DEC     A                   ; Show the index we just returned
            CALL    DBG_HEX
            CALL    DBG_CRLF
            OUT     (PORT_MMIO),A       ; Back to MMIO
            POP     AF
            ;
            ; Success: exit MMIO, restore regs, return NC
            CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            ;
            ; Return ATRB value in A, NC = success
            LD      HL,(DIR_BUFPTR)
            LD      A,(HL)              ; A = ATRB byte (first byte of MZF entry)
            OR      A                   ; Clear carry
            RET
            ;
RFSRO_SKIP:
            ; Advance to next entry
            LD      A,(DIR_ENTRY_IDX)
            INC     A
            LD      (DIR_ENTRY_IDX),A
            JR      Z,RFSRO_END         ; Wrapped 255->0: all 256 scanned
            JR      RFSRO_NEXT
            ;
RFSRO_END:
            ; No more entries (or SD error)
            CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            XOR     A                   ; A=0
            SCF                         ; CF = no more entries
            RET

; ---------------------------------------------------------------------------
; RFSINP_DIR - ZINP handler for $RFS device
;
; For bit-5 devices, BASIC calls ZINP for three purposes:
;   1. Disk identity read (H >= 28H) — returns zeros at 2800-28FF
;   2. DIR buffer fill (DIR_FILLED=0, H < 28H) — fills 2000-27FF
;   3. LOAD data transfer (DIR_FILLED=1, H < 28H) — reads from SD card
;
; IMPORTANT: H >= 28H MUST be checked first, before DIR_FILLED.
; BASIC's data transfer path (L2AD2 → L2FD1 → L2FE8 → L2E36 → L2E67)
; does an identity read (H=28H) AFTER the DIR buffer fill has set
; DIR_FILLED=1. If DIR_FILLED were checked first, this identity read
; would incorrectly dispatch to RFSINP_LD.
;
; Returns: carry clear, A=0 (success)
; ---------------------------------------------------------------------------
RFSINP_DIR:
            ; Debug: trace every ZINP call
            PUSH    AF
            LD      A,'Z'
            OUT     (050H),A
            LD      A,H
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     AF
            ;
            ; LOAD data path: BASIC's LOAD for bit5=0 devices calls ZINP once
            ; via JP 019BBH (XOR A; CALL L18B6; CALL L1929). Entry A=0 signals
            ; "load file data now" — skip DIR fill and identity checks entirely.
            ; The byte-at-a-time refill path (L19B4) enters with A=0xFF (OR 0AFH).
            OR      A
            JP      Z,RFSINP_LD         ; A=0 → LOAD path: read file to DTADR
            ;
            ; Identity reads (H >= 28H) MUST go to RFSINP_DISKID regardless of
            ; DIR_FILLED, because BASIC does identity validation (L2E36 → L2E67 →
            ; L2FD4 → IOCALL) AFTER the DIR buffer fill has set DIR_FILLED=1.
            ; If DIR_FILLED were checked first, the identity read would dispatch
            ; to RFSINP_LD instead of RFSINP_DISKID → identity validation fails.
            LD      A,H
            CP      028H
            JP      NC,RFSINP_DISKID    ; H >= 28H → identity (always)
            ;
            ; H < 28H: DIR buffer fill or data transfer
            LD      A,(DIR_FILLED)
            OR      A
            JP      NZ,RFSINP_LD        ; DIR already filled → data transfer
            ;
            ; --- DIR mode: enumerate files into 0x2000 buffer ---
            ;
            PUSH    HL
            PUSH    DE
            PUSH    BC
            ;
            ; Clear DIR buffer 0x2000-0x27FF (2048 bytes = 64 entries x 32)
            LD      HL,L2000
            LD      DE,L2000 + 1
            LD      BC,007FFH
            LD      (HL),000H
            LDIR
            ;
            ; Set up SD drive from channel number
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3, skipping boot drives 0-2
            LD      (SDDRIVENO),A
            ;
            ; Entry 0 at 0x2000: skip marker (0x80)
            LD      HL,L2000
            LD      (HL),080H
            ;
            ; Enter MMIO mode
            CALL    MMIO_ON
            ;
            ; Reset directory sector cache
            LD      A,0FFH
            LD      (DIRSECBUF),A
            ;
            ; Scan SD directory, fill MZF entries starting at 0x2020
            LD      HL,L2000 + 020H
            LD      (DIR_BUFPTR),HL     ; Current buffer write position
            LD      E,000H              ; E = SD directory entry index (0-255)
            LD      D,03FH              ; D = remaining buffer entries (max 63)
            LD      B,000H              ; B = 256 entries to scan (DJNZ wraps)
            ;
RDIR_LOOP:  PUSH    BC
            PUSH    DE
            CALL    GETSDDIRENT         ; HL = entry in SECTORBUF, A = status
            POP     DE
            POP     BC
            OR      A
            JR      NZ,RDIR_DONE        ; SD read error, stop
            ;
            ; Debug: dump first 4 entries (E=0..3) FLAG1+ATRB
            LD      A,E
            CP      004H
            JR      NC,RDIR_NDBG        ; Only dump entries 0-3
            PUSH    HL
            PUSH    AF
            LD      A,'['
            OUT     (050H),A
            LD      A,E
            CALL    DBG_HEX             ; Entry number
            LD      A,']'
            OUT     (050H),A
            POP     AF
            POP     HL
            PUSH    HL
            PUSH    BC
            ; Dump 8 bytes from entry
            LD      B,008H
RDIR_DBLP:  LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    RDIR_DBLP
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            ;
RDIR_NDBG:
            ; Check if entry is active (FLAG1 bit 7) AND has valid ATRB.
            ; Uninitialized SD entries (0xFF fill) have bit 7 set but ATRB=0xFF,
            ; so we reject ATRB=0x00 (empty) and ATRB=0xFF (uninitialized).
            ; All other values (0x01-0xFE) are accepted, including non-standard
            ; file types like 0x7E found on some partitions.
            LD      A,(HL)
            BIT     7,A
            JR      Z,RDIR_NEXT         ; Inactive (FLAG1 bit7=0), skip
            INC     HL
            LD      A,(HL)              ; A = ATRB (offset 1)
            DEC     HL                  ; HL back to entry start
            OR      A
            JR      Z,RDIR_NEXT         ; ATRB=0 → invalid, skip
            CP      0FFH
            JR      Z,RDIR_NEXT         ; ATRB=0xFF → uninitialized, skip
            ;
            ; Convert SD entry at HL to MZF format in DIR buffer
            PUSH    DE
            PUSH    BC
            CALL    CONVERT_ENTRY       ; HL=SD entry, writes to (DIR_BUFPTR)
            POP     BC
            POP     DE
            DEC     D                   ; One fewer buffer slot
            JR      Z,RDIR_DONE         ; Buffer full
            ;
RDIR_NEXT:  INC     E                   ; Next SD directory entry
            DJNZ    RDIR_LOOP
            ;
RDIR_DONE:  CALL    MMIO_OFF
            ; Debug: show how many entries found (D started at 63, remaining = D)
            PUSH    AF
            LD      A,'F'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            LD      A,03FH              ; 63 max
            SUB     D                   ; A = entries found
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     AF
            ;
            POP     BC
            POP     DE
            POP     HL
            LD      A,001H
            LD      (DIR_FILLED),A      ; Mark DIR buffer as filled
            XOR     A                   ; A=0, carry clear = success
            RET
            ;
            ; --- Disk identity mode: zero L2800-L28FF ---
            ; L2E36 validates: (L2800) exact + L2806-L28FF bitmask against
            ; stored reference. L2E77/L2E84 modifies L2801-L2805 and writes
            ; computed values at L2806+. By presenting all-zeros consistently,
            ; L2E84 computes the same result each time → reference matches.
            ;
RFSINP_DISKID:
            PUSH    HL
            PUSH    DE
            PUSH    BC
            LD      HL,02800H
            LD      DE,02801H
            LD      BC,000FFH           ; 255 bytes: L2800-L28FF
            LD      (HL),000H
            LDIR
            POP     BC
            POP     DE
            POP     HL
            XOR     A                   ; A=0, carry clear = success
            RET

; ---------------------------------------------------------------------------
; RFSINP_LD - LOAD data transfer (ZINP with ZLOG != 0)
;
; Reads file data sector-by-sector from SD card into the load address.
; For bit-5 devices, BASIC's LOAD path does NOT call ZRO and does not
; set ZLOG. RFSINP_DIR dispatches here when DIR_FILLED=1 (DIR buffer
; already populated). We find the file in the SD directory via FINDSD,
; extract SIZE, DTADR, and start sector directly from the SD entry.
; ---------------------------------------------------------------------------
RFSINP_LD:  ; Debug: trace RFSINP_LD entry
            PUSH    AF
            LD      A,'I'
            OUT     (050H),A
            LD      A,'L'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            ; Dump ELMD1 first 8 chars
            PUSH    HL
            PUSH    BC
            LD      HL,ELMD1
            LD      B,008H
ILD_DMP:    LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    ILD_DMP
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            POP     AF
            ;
            ; V1.1Y: Save entry A value (0=SVC #30 LOAD, nonzero=SVC #33 refill).
            ; RLD_DONE needs this to skip status byte writes when L180B still points
            ; at SVC #2C self-modifying code (0x1A85, set by prior DIR's SVC #2E).
            LD      (RFS_LD_A0),A
            ;
            PUSH    HL
            PUSH    DE
            PUSH    BC
            ;
            ; Set up drive number
            LD      A,(ZCH)
            ADD     A,003H              ; RFS1:=drive 3, skipping boot drives 0-2
            LD      (SDDRIVENO),A
            ;
            ; Enter MMIO mode
            CALL    MMIO_ON
            ;
            ; Reset directory sector cache
            LD      A,0FFH
            LD      (DIRSECBUF),A
            ;
            ; Find file by name in SD directory to get start sector
            LD      DE,ELMD1
            CALL    FINDSD
            JP      NZ,RLD_NFERR        ; File not found
            ;
            ; HL = directory entry in SECTORBUF
            ; Skip FLAG1(1) + ATRB(1) + NAME(17) = 19 bytes to START_SECTOR
            LD      DE,19
            ADD     HL,DE
            ;
            ; Copy 4-byte start sector to FILE_STARTSEC (big-endian)
            LD      DE,FILE_STARTSEC
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            ;
            ; HL now at offset 22 (last byte of START_SECTOR).
            ; Read SIZE(2) and DTADR(2) from the SD directory entry
            ; (offset 23..26) instead of ELMD, which may not be populated
            ; for bit-5 device LOAD paths.
            INC     HL                  ; HL = offset 23 = SIZE low
            LD      E,(HL)
            INC     HL                  ; HL = offset 24 = SIZE high
            LD      D,(HL)
            EX      DE,HL               ; HL = SIZE
            LD      (FILE_SIZE),HL
            EX      DE,HL               ; HL = offset 24 again
            INC     HL                  ; HL = offset 25 = DTADR low
            LD      E,(HL)
            INC     HL                  ; HL = offset 26 = DTADR high
            LD      D,(HL)
            EX      DE,HL               ; HL = DTADR
            LD      (FILE_LOADADDR),HL
            ;
            ; For BASIC programs (ELMD type 2/BTX), override FILE_LOADADDR
            ; with TEXTST.  The file's DTADR reflects whatever system it was
            ; saved on and may point inside Disk BASIC code.  BASIC's own
            ; SVC 038H rechain (called in the LOAD command handler at L5C44)
            ; rebuilds all next-line pointers from scratch, so the data just
            ; needs to land at the current TEXTST.
            LD      A,(ELMD)            ; File attribute byte
            CP      002H                ; Type 2 = BTX (BASIC text)?
            JR      NZ,RLD_NORLOC       ; No → use DTADR as-is
            LD      HL,(TEXTST)         ; Current text area start
            LD      (FILE_LOADADDR),HL
RLD_NORLOC:
            ;
            ; Convert FILE_STARTSEC to absolute sector (add drive offset)
            LD      A,(FILE_STARTSEC)
            LD      D,A
            LD      A,(FILE_STARTSEC+1)
            LD      E,A
            LD      A,(FILE_STARTSEC+2)
            LD      H,A
            LD      A,(FILE_STARTSEC+3)
            LD      L,A
            CALL    RFSGETSECTR         ; DEHL += drive_offset
            ;
            ; Store absolute sector in SDSTARTSEC for SD_READ
            LD      A,D
            LD      (SDSTARTSEC),A
            LD      A,E
            LD      (SDSTARTSEC+1),A
            LD      A,H
            LD      (SDSTARTSEC+2),A
            LD      A,L
            LD      (SDSTARTSEC+3),A
            ;
            ; Debug: dump FILE_STARTSEC, absolute sector, SIZE, DTADR
            ; Switch to DRAM for debug output (port 050H may need DRAM)
            PUSH    AF
            OUT     (PORT_DRAM),A
            LD      A,'I'
            OUT     (050H),A
            LD      A,'S'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            ; FILE_STARTSEC (4 bytes)
            LD      A,(FILE_STARTSEC)
            CALL    DBG_HEX
            LD      A,(FILE_STARTSEC+1)
            CALL    DBG_HEX
            LD      A,(FILE_STARTSEC+2)
            CALL    DBG_HEX
            LD      A,(FILE_STARTSEC+3)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            ; Absolute sector (SDSTARTSEC, 4 bytes)
            LD      A,'A'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,(SDSTARTSEC)
            CALL    DBG_HEX
            LD      A,(SDSTARTSEC+1)
            CALL    DBG_HEX
            LD      A,(SDSTARTSEC+2)
            CALL    DBG_HEX
            LD      A,(SDSTARTSEC+3)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            ; SIZE
            LD      A,'Z'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,(FILE_SIZE+1)     ; High byte
            CALL    DBG_HEX
            LD      A,(FILE_SIZE)       ; Low byte
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            ; DTADR
            LD      A,'D'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,(FILE_LOADADDR+1) ; High byte
            CALL    DBG_HEX
            LD      A,(FILE_LOADADDR)   ; Low byte
            CALL    DBG_HEX
            CALL    DBG_CRLF
            ; Switch back to MMIO
            OUT     (PORT_MMIO),A
            POP     AF
            ;
RLD_LOOP:   ; Read one sector into SECTORBUF
            CALL    SD_READ
            OR      A
            JR      NZ,RLD_ERR
            ;
            ; Map DRAM to copy data (target may be >= D000H)
            OUT     (PORT_DRAM),A
            ;
            ; Calculate bytes to copy: min(FILE_SIZE, 512)
            LD      HL,(FILE_SIZE)
            LD      DE,00200H           ; 512
            OR      A
            SBC     HL,DE               ; HL = FILE_SIZE - 512
            JR      C,RLD_PARTIAL       ; FILE_SIZE < 512
            ;
            ; Full sector: remaining = HL, copy 512 bytes
            LD      (FILE_SIZE),HL
            LD      BC,00200H
            JR      RLD_COPY
            ;
RLD_PARTIAL:; Partial sector: remaining = 0, copy FILE_SIZE bytes
            ADD     HL,DE               ; Restore HL = FILE_SIZE
            LD      B,H
            LD      C,L                 ; BC = bytes to copy
            LD      HL,0
            LD      (FILE_SIZE),HL      ; Done after this
            ;
RLD_COPY:   LD      HL,SECTORBUF
            LD      DE,(FILE_LOADADDR)
            LDIR                        ; Copy sector data to target
            LD      (FILE_LOADADDR),DE  ; Update target address
            ;
            ; Check if all data transferred
            LD      HL,(FILE_SIZE)
            LD      A,H
            OR      L
            JR      Z,RLD_DONE          ; All done
            ;
            ; Increment SDSTARTSEC for next sector
            CALL    INC_STARTSEC
            ;
            ; Map MMIO for next sector read
            OUT     (PORT_MMIO),A
            JR      RLD_LOOP
            ;
RLD_DONE:   ; Map MMIO so MMIO_OFF can work (it expects temp stack)
            OUT     (PORT_MMIO),A
            CALL    MMIO_OFF
            ;
            ; V1.1Y: Only write status bytes when entered via SVC #33 refill path
            ; (A≠0 at entry). The SVC #30 LOAD path (A=0) leaves L180B pointing at
            ; 0x1A85 (set by prior DIR's SVC #2E), which is inside SVC #2C's
            ; self-modifying LD HL,nnnn instruction (opcode 0x21 at 0x1A83).
            ; Writing 0xFF there corrupts the opcode to RST 38H, breaking all
            ; subsequent device-open calls ("Dev. name error" after LOAD).
            ; SVC #30 callers don't read the status bytes, so skipping is safe.
            LD      A,(RFS_LD_A0)
            OR      A
            JR      Z,RLD_SKIP_STAT
            ;
            ; SVC #33 refill path: write status bytes at (L180B)-2 to 0xFF,0xFF
            ; = "last block, all done". L1AEF already set L180B to a safe buffer.
            LD      HL,(L180B)
            DEC     HL
            LD      (HL),0FFH           ; (L180B)-1 = 0xFF
            DEC     HL
            LD      (HL),0FFH           ; (L180B)-2 = 0xFF
            ;
RLD_SKIP_STAT:
            POP     BC
            POP     DE
            POP     HL
            XOR     A                   ; NC = success
            RET
            ;
RLD_ERR:    CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            XOR     A
            SCF                         ; Carry = error
            RET
            ;
RLD_NFERR:  CALL    MMIO_OFF
            POP     BC
            POP     DE
            POP     HL
            XOR     A
            SCF                         ; Carry = file not found
            RET

; ===========================================================================
; Low-level SD card access routines
; ===========================================================================

; ---------------------------------------------------------------------------
; MMIO_ON - Enter MMIO mode for SPI access
;
; Saves BASIC stack pointer, switches to temp stack, disables interrupts,
; maps MMIO (D000-FFFF), and trains the upCntr latch.
; After return, caller is on temp stack and can access EFF8-EFFF.
; ---------------------------------------------------------------------------
MMIO_ON:    POP     HL                  ; Get return address (from BASIC stack)
            LD      (SAVE_SP),SP        ; Save BASIC SP
            LD      SP,TEMP_STACK_TOP   ; Switch to temp stack
            PUSH    HL                  ; Push return address on temp stack
            DI
            XOR     A
            OUT     (PORT_MMIO),A       ; Map D000-FFFF to MMIO
            CALL    TRAIN_UPCNTR        ; Train latch (15 reads of EFF8H)
            RET                         ; Return to caller (via temp stack)

; ---------------------------------------------------------------------------
; MMIO_OFF - Leave MMIO mode, restore DRAM and BASIC stack
;
; Maps D000-FFFF back to DRAM, enables interrupts, restores BASIC stack.
; ---------------------------------------------------------------------------
MMIO_OFF:   POP     HL                  ; Get return address (from temp stack)
            XOR     A
            OUT     (PORT_DRAM),A       ; Map D000-FFFF to DRAM
            EI
            LD      SP,(SAVE_SP)        ; Restore BASIC stack
            JP      (HL)                ; Return to caller (on BASIC stack)

; ---------------------------------------------------------------------------
; TRAIN_UPCNTR - Train the RFS register latch (upCntr) to 15
;
; Reads BNKCTRLRST 15 times to enable control register writes.
; Entry: MMIO must be mapped (OUT E3H done)
; Exit:  A = 0, HL and B modified
; ---------------------------------------------------------------------------
TRAIN_UPCNTR:
            LD      HL,BNKCTRLRST       ; EFF8H
            LD      B,15
TRAIN_LP:   LD      A,(HL)              ; Each read increments upCntr
            DJNZ    TRAIN_LP
            XOR     A
            RET

; ---------------------------------------------------------------------------
; SPIOUT - Send one byte via hardware SPI
; Entry: A = byte to send
; Exit:  A unchanged
; ---------------------------------------------------------------------------
SPIOUT:     LD      (HWSPIDATA),A       ; Write data to SPI register
            LD      (HWSPISTART),A      ; Start SPI transfer (clocks 8 bits)
            RET

; ---------------------------------------------------------------------------
; SPIIN - Receive one byte via hardware SPI
; Entry: (none)
; Exit:  A = received byte
; ---------------------------------------------------------------------------
SPIIN:      LD      (HWSPISTART),A      ; Clock 8 bits (sends whatever was in A)
            LD      A,(HWSPIDATA)       ; Read received byte
            RET

; ---------------------------------------------------------------------------
; SPI_CS_LO - Activate SD card chip select (CS LOW)
; ---------------------------------------------------------------------------
SPI_CS_LO:  LD      A,SDCS_ACT          ; CS LOW, clock enable
            LD      (BNKCTRL),A
            RET

; ---------------------------------------------------------------------------
; SPI_CS_HI - Deactivate SD card chip select (CS HIGH)
; ---------------------------------------------------------------------------
SPI_CS_HI:  LD      A,SDCS_DEACT        ; CS HIGH, clock enable
            LD      (BNKCTRL),A
            RET

; ---------------------------------------------------------------------------
; SD_READ - Read one 512-byte sector from SD card into SECTORBUF
;
; Entry: SDSTARTSEC = 32-bit sector address (big-endian, 4 bytes)
;        MMIO must be mapped, upCntr trained
; Exit:  A = 0 success, A = 1 error
;        SECTORBUF contains 512 bytes of sector data
; Modified: A, B, D, HL
; ---------------------------------------------------------------------------
SD_READ:    CALL    SPI_CS_LO           ; CS LOW (activate card)
            ;
            ; Send CMD17 (Read Single Block): 0x51 + 4-byte sector + 0xFF
            LD      A,051H              ; CMD17
            CALL    SPIOUT
            LD      A,(SDSTARTSEC)      ; Sector byte 0 (MSB)
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+1)    ; Sector byte 1
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+2)    ; Sector byte 2
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+3)    ; Sector byte 3 (LSB)
            CALL    SPIOUT
            LD      A,0FFH              ; CRC (dummy, ignored by emulation)
            CALL    SPIOUT
            ;
            ; Poll for R1 response (should be 0x00, not 0xFF)
            LD      B,0FFH              ; Max 255 retries
SDR_R1:     CALL    SPIIN
            CP      0FFH
            JR      NZ,SDR_GOTR1        ; Got non-0xFF response
            DJNZ    SDR_R1
            JR      SDR_ERR             ; Timeout
            ;
SDR_GOTR1:  OR      A                   ; R1 should be 0x00 (no error)
            JR      NZ,SDR_ERR
            ;
            ; Poll for data token (0xFE)
            LD      B,0FFH
SDR_TOK:    CALL    SPIIN
            CP      0FEH               ; Data token?
            JR      Z,SDR_DATA          ; Yes, start reading data
            CP      0FFH               ; Still busy?
            JR      NZ,SDR_ERR          ; Error token
            DJNZ    SDR_TOK
            JR      SDR_ERR             ; Timeout
            ;
            ; Read 512 bytes into SECTORBUF (2 pages of 256 bytes)
SDR_DATA:   LD      HL,SECTORBUF
            LD      D,002H              ; 2 pages
SDR_PG:     LD      B,000H              ; 256 bytes per page (B=0 wraps DJNZ)
SDR_BYTE:   CALL    SPIIN
            LD      (HL),A
            INC     HL
            DJNZ    SDR_BYTE
            DEC     D
            JR      NZ,SDR_PG
            ;
            ; Skip 2 CRC bytes
            CALL    SPIIN
            CALL    SPIIN
            ;
            CALL    SPI_CS_HI           ; CS HIGH (deactivate)
            XOR     A                   ; A = 0, success
            RET
            ;
SDR_ERR:    CALL    SPI_CS_HI           ; CS HIGH on error too
            LD      A,001H              ; A = 1, error
            RET

; ---------------------------------------------------------------------------
; SD_WRITE - Write one 512-byte sector from SECTORBUF to SD card
;
; Entry: SDSTARTSEC = 32-bit sector address (big-endian, 4 bytes)
;        SECTORBUF contains 512 bytes to write
;        MMIO must be mapped, upCntr trained
; Exit:  A = 0 success, A = 1 error
; Modified: A, B, D, HL
; ---------------------------------------------------------------------------
SD_WRITE:   CALL    SPI_CS_LO           ; CS LOW (activate card)
            ;
            ; Send CMD24 (Write Single Block): 0x58 + 4-byte sector + 0xFF
            LD      A,058H              ; CMD24
            CALL    SPIOUT
            LD      A,(SDSTARTSEC)
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+1)
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+2)
            CALL    SPIOUT
            LD      A,(SDSTARTSEC+3)
            CALL    SPIOUT
            LD      A,0FFH              ; Dummy CRC
            CALL    SPIOUT
            ;
            ; Poll for R1 response (should be 0x00)
            LD      B,0FFH
SDW_R1:     CALL    SPIIN
            CP      0FFH
            JR      NZ,SDW_GOTR1
            DJNZ    SDW_R1
            JR      SDW_ERR             ; Timeout
            ;
SDW_GOTR1:  OR      A
            JR      NZ,SDW_ERR          ; R1 error
            ;
            ; Send data token 0xFE
            LD      A,0FEH
            CALL    SPIOUT
            ;
            ; Send 512 bytes from SECTORBUF (2 pages of 256)
            LD      HL,SECTORBUF
            LD      D,002H
SDW_PG:     LD      B,000H              ; 256 bytes per page
SDW_BYTE:   LD      A,(HL)
            CALL    SPIOUT
            INC     HL
            DJNZ    SDW_BYTE
            DEC     D
            JR      NZ,SDW_PG
            ;
            ; Send 2 dummy CRC bytes
            LD      A,0FFH
            CALL    SPIOUT
            CALL    SPIOUT
            ;
            ; Read data response token: xxx00101 = accepted (0x05)
            CALL    SPIIN
            AND     01FH
            CP      005H
            JR      NZ,SDW_ERR
            ;
            ; Wait for busy (card holds MISO low while writing)
            LD      D,040H              ; Outer loop: 64
SDW_BSY1:   LD      B,000H              ; Inner loop: 256 -> 64*256=16384 polls
SDW_BSY2:   CALL    SPIIN
            CP      0FFH                ; 0xFF = not busy
            JR      Z,SDW_DONE
            DJNZ    SDW_BSY2
            DEC     D
            JR      NZ,SDW_BSY1
            JR      SDW_ERR             ; Timeout
            ;
SDW_DONE:   CALL    SPI_CS_HI
            XOR     A                   ; A = 0, success
            RET
            ;
SDW_ERR:    CALL    SPI_CS_HI
            LD      A,001H              ; A = 1, error
            RET

; ===========================================================================
; Directory and file access routines
; ===========================================================================

; ---------------------------------------------------------------------------
; ADD3216 - Add 16-bit BC to 32-bit DEHL
;
; Entry: DEHL = 32-bit number, BC = 16-bit addend
; Exit:  DEHL = DEHL + BC
; Modified: BC (set to 0)
; ---------------------------------------------------------------------------
ADD3216:    ADD     HL,BC
            EX      DE,HL
            LD      BC,0
            ADC     HL,BC               ; Add carry to upper 16 bits
            EX      DE,HL
            RET

; ---------------------------------------------------------------------------
; RFSGETSECTR - Calculate absolute SD sector from drive-relative sector
;
; Adds (SDDRIVENO * 0x8010) to DEHL.
; Each drive image occupies 0x8010 sectors in the SD card image.
;
; Entry: DEHL = relative sector (32-bit)
;        SDDRIVENO = drive number (0 = first drive, 1 = second, etc.)
; Exit:  DEHL = absolute sector
; Modified: A, BC
; ---------------------------------------------------------------------------
RFSGETSECTR:
            LD      A,(SDDRIVENO)
GETSEC_LP:  OR      A
            RET     Z                   ; Drive 0 = no offset
            DEC     A
            LD      BC,08010H           ; Sectors per drive image
            CALL    ADD3216
            JR      GETSEC_LP

; ---------------------------------------------------------------------------
; SETLBAADDR - Store DEHL into SDSTARTSEC (big-endian)
;
; Entry: DEHL = 32-bit sector (D=MSB, L=LSB)
; Exit:  SDSTARTSEC updated
; Modified: A
; ---------------------------------------------------------------------------
SETLBAADDR: LD      A,D
            LD      (SDSTARTSEC),A
            LD      A,E
            LD      (SDSTARTSEC+1),A
            LD      A,H
            LD      (SDSTARTSEC+2),A
            LD      A,L
            LD      (SDSTARTSEC+3),A
            RET

; ---------------------------------------------------------------------------
; INC_STARTSEC - Increment SDSTARTSEC by 1 (32-bit big-endian)
; Modified: HL
; ---------------------------------------------------------------------------
INC_STARTSEC:
            LD      HL,SDSTARTSEC+3     ; LSB
            INC     (HL)
            RET     NZ
            DEC     HL                  ; Byte 2
            INC     (HL)
            RET     NZ
            DEC     HL                  ; Byte 1
            INC     (HL)
            RET     NZ
            DEC     HL                  ; MSB
            INC     (HL)
            RET

; ---------------------------------------------------------------------------
; GETSDDIRENT - Get SD directory entry with sector caching
;
; Reads the directory sector containing entry E if not cached, then
; returns a pointer to the entry within SECTORBUF.
;
; Entry: E = directory entry number (0-255)
;        MMIO mapped, upCntr trained
; Exit:  HL = pointer to 32-byte entry in SECTORBUF
;        A = 0 success, A = 1 error
; Modified: A, BC, DE, HL
; ---------------------------------------------------------------------------
GETSDDIRENT:
            PUSH    DE
            ;
            ; Calculate which sector this entry is in (E / 16)
            LD      A,E
            SRL     A
            SRL     A
            SRL     A
            SRL     A                   ; A = sector number within directory
            ;
            ; Check if this sector is already cached
            LD      C,A
            LD      A,(DIRSECBUF)
            CP      C
            JR      Z,GDIR_CACHED       ; Same sector, use cached data
            ;
            ; Need to read a new sector
            LD      A,C
            LD      (DIRSECBUF),A       ; Update cache tag
            ;
            ; Calculate absolute sector: drive_offset + dir_sector
            LD      DE,0
            LD      H,0
            LD      L,A                 ; DEHL = directory sector (relative)
            CALL    RFSGETSECTR         ; DEHL = absolute sector
            CALL    SETLBAADDR          ; Store in SDSTARTSEC
            CALL    SD_READ             ; Read sector into SECTORBUF
            OR      A
            JR      NZ,GDIR_ERR
            ;
GDIR_CACHED:; Calculate offset within sector: (E & 0x0F) * 32
            POP     DE
            PUSH    DE
            LD      A,E
            AND     00FH                ; Entry within sector (0-15)
            LD      HL,SECTORBUF
            OR      A
            JR      Z,GDIR_DONE         ; First entry, no offset needed
            ;
            ; Add A * 32 to HL
            LD      B,A
            LD      DE,00020H           ; 32 bytes per entry
GDIR_ADD:   ADD     HL,DE
            DJNZ    GDIR_ADD
            ;
GDIR_DONE:  POP     DE
            XOR     A                   ; A = 0, success
            RET
            ;
GDIR_ERR:   POP     DE
            LD      A,001H              ; A = 1, error
            RET

; ---------------------------------------------------------------------------
; FINDSD - Find file by name in SD card directory
;
; Searches all 256 directory entries for an active entry whose filename
; matches the string at DE (prefix match: input terminated by CR or null).
;
; Entry: DE = filename string (CR or null terminated)
;        MMIO mapped, upCntr trained, DIRSECBUF set to 0xFF
; Exit:  Z flag set = found, HL = directory entry in SECTORBUF
;        NZ flag = not found
; Modified: A, BC, DE, HL
; ---------------------------------------------------------------------------
FINDSD:     LD      (FINDSD_NAME),DE    ; Save search name pointer
            LD      E,000H              ; E = directory entry index
            LD      B,000H              ; B = 256 entries (DJNZ wraps from 0)
            ;
FSD_LOOP:   PUSH    BC
            PUSH    DE
            CALL    GETSDDIRENT         ; HL = entry, A = status
            POP     DE
            POP     BC
            OR      A
            JR      NZ,FSD_NF           ; Read error = not found
            ;
            ; Check if entry is active (FLAG1 bit 7) AND has valid ATRB.
            ; Accept all non-zero, non-0xFF ATRB (01-FE).
            LD      A,(HL)
            BIT     7,A
            JR      Z,FSD_NEXT          ; Inactive, skip
            INC     HL
            LD      A,(HL)              ; A = ATRB (offset 1)
            DEC     HL                  ; HL back to entry start
            OR      A
            JR      Z,FSD_NEXT          ; ATRB=0 → invalid, skip
            CP      0FFH
            JR      Z,FSD_NEXT          ; ATRB=0xFF → uninitialized, skip
            ;
            ; Compare filename: dir entry name at HL+2 vs search name
            PUSH    HL                  ; Save entry start
            PUSH    DE
            PUSH    BC
            INC     HL
            INC     HL                  ; HL = directory entry name (offset 2)
            LD      DE,(FINDSD_NAME)    ; DE = search name
            LD      B,17                ; Max filename length
            ;
FSD_CMP:    LD      A,(DE)
            CP      00DH                ; CR terminator?
            JR      Z,FSD_MATCH         ; Input ended = prefix match
            OR      A                   ; Null terminator?
            JR      Z,FSD_MATCH
            CP      (HL)                ; Compare characters
            JR      NZ,FSD_NOMATCH
            INC     DE
            INC     HL
            DJNZ    FSD_CMP
            ; All 17 characters matched
            ;
FSD_MATCH:  POP     BC
            POP     DE
            POP     HL                  ; HL = entry start
            XOR     A                   ; Z flag = found
            RET
            ;
FSD_NOMATCH:POP     BC
            POP     DE
            POP     HL
            ;
FSD_NEXT:   INC     E                   ; Next directory entry
            DJNZ    FSD_LOOP
            ;
FSD_NF:     LD      A,001H
            OR      A                   ; NZ flag = not found
            RET

; ---------------------------------------------------------------------------
; EXTRACT_HDR - Extract file header from SD directory entry
;
; Converts SD directory entry fields to MZF format in ELMD, and saves
; the file's absolute start sector in FILE_STARTSEC for data transfer.
;
; Entry: HL = directory entry in SECTORBUF (MMIO mapped)
;        SDDRIVENO set for current drive
; Exit:  ELMD filled with MZF header (ATRB, NAME, SIZE, DTADR, EXADR)
;        FILE_STARTSEC set to absolute start sector
; Modified: A, BC, DE, HL
; ---------------------------------------------------------------------------
EXTRACT_HDR:
            INC     HL                  ; Skip FLAG1, HL = ATRB (offset 1)
            ;
            ; Copy ATRB to ELMD[0], remapping CMT type 05 -> Disk BASIC type 02
            LD      DE,ELMD
            LD      A,(HL)
            CP      005H                ; CMT type 05 = MZ-700 BASIC text
            JR      NZ,EH_AT1
            LD      A,002H              ; Disk BASIC type 02 = BTX
EH_AT1:     LD      (DE),A
            INC     HL                  ; HL = NAME (offset 2)
            INC     DE                  ; DE = ELMD+1
            ;
            ; Copy 17-byte filename, converting null to CR
            LD      B,17
EH_NAME:    LD      A,(HL)
            OR      A
            JR      NZ,EH_NM1
            LD      A,00DH              ; Null -> CR for BASIC
EH_NM1:     LD      (DE),A
            INC     HL
            INC     DE
            DJNZ    EH_NAME
            ;
            ; HL at offset 19: START_SECTOR (4 bytes, big-endian)
            ; DE at ELMD+18
            PUSH    DE                  ; Save ELMD position
            ;
            ; Copy 4-byte start sector to FILE_STARTSEC
            LD      DE,FILE_STARTSEC
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A
            INC     HL                  ; HL at offset 23: SIZE
            ;
            ; Copy SIZE(2) + DTADR(2) + EXADR(2) = 6 bytes to ELMD
            POP     DE                  ; DE = ELMD+18
            LD      BC,6
            LDIR
            ;
            ; Convert FILE_STARTSEC to absolute sector (add drive offset)
            LD      A,(FILE_STARTSEC)
            LD      D,A
            LD      A,(FILE_STARTSEC+1)
            LD      E,A
            LD      A,(FILE_STARTSEC+2)
            LD      H,A
            LD      A,(FILE_STARTSEC+3)
            LD      L,A
            CALL    RFSGETSECTR         ; DEHL += drive_offset
            ;
            ; Store absolute sector back to FILE_STARTSEC
            LD      A,D
            LD      (FILE_STARTSEC),A
            LD      A,E
            LD      (FILE_STARTSEC+1),A
            LD      A,H
            LD      (FILE_STARTSEC+2),A
            LD      A,L
            LD      (FILE_STARTSEC+3),A
            RET

; ---------------------------------------------------------------------------
; CONVERT_ENTRY - Convert SD directory entry to Disk BASIC DIR buffer format
;
; Writes a 32-byte directory entry to the DIR buffer at (DIR_BUFPTR)
; and advances DIR_BUFPTR by 32. Format matches standard MZ Disk BASIC
; floppy directory layout so LOAD/SAVE work correctly.
;
; SD entry format:    FLAG1(1), ATRB(1), NAME(17), SECTOR(4), SIZE(2), DTADR(2), EXADR(2), RESV(3)
; Disk BASIC format:  ATRB(1), NAME(17), STARTSEC(2), SIZE(2), DTADR(2), EXADR(2), pad(6)
;
; Entry: HL = SD directory entry in SECTORBUF
; Exit:  DIR_BUFPTR advanced by 32
; Modified: A, BC, DE, HL
; ---------------------------------------------------------------------------
CONVERT_ENTRY:
            INC     HL                  ; Skip FLAG1
            ;
            ; Copy ATRB to buffer, remapping CMT type 05 -> Disk BASIC type 02
            LD      DE,(DIR_BUFPTR)
            LD      A,(HL)
            CP      005H                ; CMT type 05 = MZ-700 BASIC text
            JR      NZ,CE_AT1
            LD      A,002H              ; Disk BASIC type 02 = BTX
CE_AT1:     LD      (DE),A
            INC     HL                  ; HL = NAME (offset 2)
            INC     DE
            ;
            ; Copy 17-byte filename, null -> CR
            LD      B,17
CE_NAME:    LD      A,(HL)
            OR      A
            JR      NZ,CE_NM1
            LD      A,00DH
CE_NM1:     LD      (DE),A
            INC     HL
            INC     DE
            DJNZ    CE_NAME
            ;
            ; HL now at SD offset 19 = START_SECTOR (4 bytes, big-endian)
            ; Write 2-byte start sector placeholder at DIR offset 18-19
            ; then SIZE(2)+DTADR(2)+EXADR(2) at DIR offsets 20-25.
            ; BASIC reads SIZE from DIR offset 20 (IY+14H), DTADR from 22,
            ; EXADR from 24. The 2-byte slot at 18 must exist for alignment.
            INC     HL                  ; Skip START_SECTOR[0]
            INC     HL                  ; Skip START_SECTOR[1]
            LD      A,(HL)
            LD      (DE),A              ; DIR[18] = START_SECTOR[2]
            INC     HL
            INC     DE
            LD      A,(HL)
            LD      (DE),A              ; DIR[19] = START_SECTOR[3]
            INC     HL
            INC     DE
            ;
            ; Copy SIZE(2) + DTADR(2) + EXADR(2) = 6 bytes to DIR offset 20-25
            LD      BC,6
            LDIR
            ;
            ; Remaining 8 bytes already zeroed by buffer clear
            ; Advance DIR_BUFPTR by 32
            LD      HL,(DIR_BUFPTR)
            LD      DE,00020H
            ADD     HL,DE
            LD      (DIR_BUFPTR),HL
            RET

; ---------------------------------------------------------------------------
; MEMCLI_PATCH - Trampoline from L4020 (MEMCLI).
; Original code was: RST 018H / DB 020H / EX DE,HL
; SVC 020H programs the boot beep timer and (critically) enables interrupts.
; We must still call it, then load HL with RFS_END for the memory clear.
; ---------------------------------------------------------------------------
MEMCLI_PATCH:
            RST     018H
            DB      020H                ; SVC 020H: boot beep + EI
            LD      HL,RFS_END
            JP      L4023               ; Continue with memory clear loop

; ---------------------------------------------------------------------------
; RFS_LOAD_HOOK - Trampoline for L3153 data transfer
;
; Replaces CALL L32CB + CALL L3174 in L3153.  Checks if the current device
; is $RFS (by comparing the ZINP handler pointer).  For $RFS, performs SD
; card data transfer via RFSINP_LD instead of the FD/QD hardware path.
; For all other devices, calls the original L32CB + L3174 sequence.
;
; ---------------------------------------------------------------------------
; RFS_DIR_FIX — Trampoline for DIR device-name parsing.
; L593A's success path jumps here instead of directly to L596B.
; We advance HL past the ':' so the INC/DEC zero-test at L596B sees
; end-of-text (0x00) rather than ':' (0x3A).
; ---------------------------------------------------------------------------
RFS_DIR_FIX:
            LD      DE,L5982
            INC     HL                  ; Advance HL past ':'.
            JP      L596B
;
; ---------------------------------------------------------------------------
; RFS_DIR_STR — Hook for L5962 string expression path.
; Called instead of L6E6F when L593A's letter parsing fails (e.g., DIR "RFS1:").
; Evaluates the string expression, then copies the device name (up to ':')
; into L5982 and sets B = length for SVC #2CH.
; Entry: HL = text pointer (at '"'), B = 0
; Exit:  HL advanced past expression, B = name length, DE = L5982
; ---------------------------------------------------------------------------
RFS_DIR_STR:
            CALL    L6E6F               ; Evaluate string expr. B=strlen, DE=strptr
            PUSH    HL                  ; Save text pointer (past closing quote)
            EX      DE,HL               ; HL = string data pointer
            LD      DE,L5982            ; DE = name buffer
            LD      A,B                 ; A = string length
            OR      A
            JR      Z,DSTR_DONE         ; Empty string → B stays 0
            LD      C,A                 ; C = remaining chars
            LD      B,000H              ; B = bytes copied
DSTR_LP:    LD      A,(HL)              ; Get char from string
            LD      (DE),A              ; Copy to name buffer
            INC     HL
            INC     DE
            INC     B                   ; Count
            DEC     C
            CP      03AH                ; ':' ?
            JR      Z,DSTR_DONE         ; Stop after ':'
            LD      A,C
            OR      A
            JR      NZ,DSTR_LP          ; Loop if more chars
DSTR_DONE:  LD      DE,L5982            ; DE = start of name buffer (for SVC #2CH)
            POP     HL                  ; Restore text pointer
            RET
;
; Entry: BC = SIZE (file size to load), stack = L10F0 stack
; Exit:  A=0 NC on success; carry on error.  Registers preserved.
; ---------------------------------------------------------------------------
RFS_LOAD_HOOK:
            ; Debug: trace L3153 hook entry
            PUSH    AF
            LD      A,'L'
            OUT     (050H),A
            LD      A,'H'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            ; Show BC (SIZE)
            LD      A,B
            CALL    DBG_HEX
            LD      A,C
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     AF
            ;
            LD      HL,(ZINP)           ; Current device ZINP handler
            PUSH    DE
            LD      DE,RFSINP_DIR       ; $RFS handler address
            OR      A
            SBC     HL,DE
            POP     DE
            JR      Z,RFLH_RFS
            ;
            ; Not $RFS — call original FD/QD transfer
            CALL    L32CB
            JP      L3174
            ;
RFLH_RFS:   ; $RFS device: transfer file data from SD card.
            ; Debug: show ELMD contents before RFSINP_LD
            PUSH    AF
            PUSH    HL
            PUSH    BC
            LD      A,'L'
            OUT     (050H),A
            LD      A,'D'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            ; Dump ELMD[0..7] (ATRB + first 7 chars of name)
            LD      HL,ELMD
            LD      B,008H
RFLH_DMP1:  LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    RFLH_DMP1
            CALL    DBG_CRLF
            ; Dump ELMD18-ELMD23 (SIZE+DTADR+EXADR)
            LD      A,'E'
            OUT     (050H),A
            LD      A,'M'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      HL,ELMD18
            LD      B,006H
RFLH_DMP2:  LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    RFLH_DMP2
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            POP     AF
            ;
            CALL    RFSINP_LD
            ; Debug: show return status
            PUSH    AF
            LD      A,'L'
            OUT     (050H),A
            LD      A,'R'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            JR      C,RFLH_ERR
            LD      A,'K'
            OUT     (050H),A
            JR      RFLH_DONE
RFLH_ERR:   LD      A,'E'
            OUT     (050H),A
RFLH_DONE:  CALL    DBG_CRLF
            POP     AF
            JP      L3101

; ===========================================================================
; DBG_DIRCMD1 — Debug wrapper for L58CA (DIR handler 1: device-name path).
; Outputs "1E\n" on port 050H as version/path marker, then original logic.
; ===========================================================================
DBG_DIRCMD1:
            LD      A,'1'               ; DIR path 1 marker
            OUT     (050H),A
            LD      A,'M'               ; Version letter M
            OUT     (050H),A
            CALL    DBG_CRLF
            CALL    L593A               ; Original: parse device name
            RST     018H
            DB      03AH                ; SVC #3A: list directory
            RET

; ===========================================================================
; DBG_DIRCMD2 — Debug wrapper for L58D0 (DIR handler 2: string-expr path).
; Outputs "2E\n" on port 050H as version/path marker, then original logic.
; ===========================================================================
DBG_DIRCMD2:
            LD      A,'2'               ; DIR path 2 marker
            OUT     (050H),A
            LD      A,'M'               ; Version letter M
            OUT     (050H),A
            CALL    DBG_CRLF
            CALL    L413E               ; Original: check current char
            LD      B,000H
            CALL    NZ,L6E6F            ; Evaluate string expression
            JP      L58D8               ; Continue at PUSH HL / SVC #2C / SVC #3C

; ===========================================================================
; DBG_DIR — Debug wrapper replacing L596B for DIR SVC #2C.
; Outputs trace info on port 050H, then performs the original SVC call and
; error check.  Format:
;   D:BB NAMESTR...       (before SVC: B=count, name from DE)
;   K:xx xx xx xx xx xx   (after SVC: KEYBUF[0..5])
;   =HHHH:VV              (after SVC: HL pointer and (HL) value)
; ===========================================================================
DBG_DIR:
            PUSH    AF
            ; --- Before SVC: output "D:" + B (hex) + space + name string ---
            LD      A,'D'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,B
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            ; Output the name (B chars from DE)
            PUSH    BC
            PUSH    DE
            LD      A,B
            OR      A
            JR      Z,DBG_NN
DBG_NLP:    LD      A,(DE)
            OUT     (050H),A
            INC     DE
            DJNZ    DBG_NLP
DBG_NN:     POP     DE
            POP     BC
            CALL    DBG_CRLF
            POP     AF
            ; --- Call SVC #2C (original L596B logic) ---
            PUSH    HL
            RST     018H
            DB      02CH
            ; --- After SVC: dump KEYBUF[0..7] ---
            PUSH    AF
            PUSH    BC
            PUSH    DE
            PUSH    HL                  ; save SVC result HL
            LD      A,'K'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      HL,KEYBUF
            LD      B,008H
DBG_KLP:    LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    DBG_KLP
            CALL    DBG_CRLF
            POP     HL                  ; restore SVC result HL
            ; --- Output "=" + HL (4 hex) + ":" + (HL) (2 hex) ---
            LD      A,'='
            OUT     (050H),A
            LD      A,H
            CALL    DBG_HEX
            LD      A,L
            CALL    DBG_HEX
            LD      A,':'
            OUT     (050H),A
            LD      A,(HL)
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     DE
            POP     BC
            POP     AF
            ; --- Original error check ---
            INC     (HL)
            DEC     (HL)
            JP      NZ,04B25H
            POP     HL
            RET
;
; ---------------------------------------------------------------------------
; DBG_HEX — Output A as 2 hex ASCII digits to port 050H.
;            Preserves BC, DE, HL.
; ---------------------------------------------------------------------------
DBG_HEX:    PUSH    AF
            RRCA
            RRCA
            RRCA
            RRCA
            AND     00FH
            CALL    DBG_NIB
            POP     AF
            AND     00FH
DBG_NIB:    CP      00AH
            JR      C,DBG_DIG
            ADD     A,037H              ; 'A'..'F'
            OUT     (050H),A
            RET
DBG_DIG:    ADD     A,030H              ; '0'..'9'
            OUT     (050H),A
            RET
;
; ---------------------------------------------------------------------------
; DBG_CRLF — Output CR+LF to port 050H.  Preserves all registers.
; ---------------------------------------------------------------------------
DBG_CRLF:   PUSH    AF
            LD      A,00DH              ; CR
            OUT     (050H),A
            LD      A,00AH              ; LF
            OUT     (050H),A
            POP     AF
            RET

; ===========================================================================
; DBG_DIR3 — Debug wrapper for L5CE3 (the real DIR command handler).
; Replicates the full DIR flow with debug markers around each SVC call.
; Output format on port 050H:
;   D3:E  (entry marker + version)
;   82:AA  (L5882 result in A, hex)
;   2E:AA C=x B1=x  (SVC #2E result: A, carry flag, bit1 of A)
;   C2:    (L58C2 done)
;   3A:AA  (L593A result: channel in A)
;   39a:   (SVC #39 with A=0 done)
;   3Ac:AA (SVC #3A with A=channel done)
;   39b:AA (SVC #39 with A=orig done)
;   D3:X   (exit marker)
; ===========================================================================
DBG_DIR3:
            ; --- Entry marker ---
            LD      A,'D'
            OUT     (050H),A
            LD      A,'3'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,'M'               ; Version M
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- Call L5882 (original first instruction) ---
            CALL    L5882
            ; --- Debug: show L5882 result ---
            PUSH    AF
            PUSH    AF
            LD      A,'8'
            OUT     (050H),A
            LD      A,'2'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show A from L5882
            CALL    DBG_CRLF
            POP     AF
            ; --- PUSH AF (as original) ---
            ; AF is already on stack from our PUSH above
            ; --- SVC #2E ---
            RST     018H
            DB      02EH
            ; --- Debug: show SVC #2E result ---
            PUSH    AF                  ; save flags from SVC #2E
            PUSH    AF
            LD      A,'2'
            OUT     (050H),A
            LD      A,'E'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show A from SVC #2E
            LD      A,' '
            OUT     (050H),A
            LD      A,'C'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            JR      C,DBG_D3_CY1
            LD      A,'0'
            JR      DBG_D3_CY1D
DBG_D3_CY1:
            LD      A,'1'
DBG_D3_CY1D:
            OUT     (050H),A
            CALL    DBG_CRLF
            POP     AF                  ; restore flags from SVC #2E
            ; --- JP C,L57B4 (error 0x2C on carry) ---
            JP      C,L57B4
            ; --- BIT 1,A / JP Z,057B7H ---
            PUSH    AF
            LD      A,'B'
            OUT     (050H),A
            LD      A,'1'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            BIT     1,A
            PUSH    AF
            JR      Z,DBG_D3_B0
            LD      A,'1'
            JR      DBG_D3_B1D
DBG_D3_B0:
            LD      A,'0'
DBG_D3_B1D:
            OUT     (050H),A
            CALL    DBG_CRLF
            POP     AF
            JP      Z,057B7H            ; error 0xBB if bit1=0
            ; --- CALL L58C2 (check ZLOG) ---
            CALL    L58C2
            LD      A,'C'
            OUT     (050H),A
            LD      A,'2'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,'K'               ; OK
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- CALL L593A (parse device name) ---
            CALL    L593A
            ; --- Debug: show L593A result (channel in A) ---
            PUSH    AF
            LD      A,'3'
            OUT     (050H),A
            LD      A,'A'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show channel
            CALL    DBG_CRLF
            POP     AF
            ; --- LD B,A / XOR A / SVC #39 (A=0) ---
            LD      B,A
            ;
            ; Debug V1.1J: dump ZINP pointer and ZFLAG1 before SVC #39
            PUSH    BC
            LD      A,'I'               ; "IP=" marker for ZINP
            OUT     (050H),A
            LD      A,'P'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            LD      A,(ZINP+1)          ; High byte of ZINP pointer
            CALL    DBG_HEX
            LD      A,(ZINP)            ; Low byte of ZINP pointer
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            LD      A,'F'               ; "F1=" marker for ZFLAG1
            OUT     (050H),A
            LD      A,'1'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            LD      A,(ZFLAG1)
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     BC
            ;
            ; Debug V1.1K: dump first 4 bytes at RFSINP_DIR (runtime) and 0x2000
            PUSH    HL
            PUSH    BC
            LD      A,'R'
            OUT     (050H),A
            LD      A,'@'
            OUT     (050H),A
            LD      HL,RFSINP_DIR       ; Point to RFSINP_DIR code in memory
            LD      B,004H
DBG_RDMP:   LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    DBG_RDMP
            CALL    DBG_CRLF
            ; Also dump first 4 bytes of DIR buffer at 0x2000
            LD      A,'B'
            OUT     (050H),A
            LD      A,'@'
            OUT     (050H),A
            LD      HL,L2000
            LD      B,004H
DBG_BDMP:   LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    DBG_BDMP
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            ;
            ; (V1.1L debug hook removed in V1.1Y — was calling RFSINP_DIR for
            ;  ALL devices, causing 20s delay + empty DIR for non-RFS devices)
            ;
            XOR     A
            LD      A,'S'
            OUT     (050H),A
            LD      A,'9'
            OUT     (050H),A
            LD      A,'a'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            CALL    DBG_CRLF
            XOR     A                   ; A=0 again (debug output clobbered it)
            RST     018H
            DB      039H
            ; --- Debug: after SVC #39 --- dump 0x2000 again
            PUSH    AF
            PUSH    HL
            PUSH    BC
            LD      A,'B'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            LD      HL,L2000
            LD      B,004H
DBG_BDMP2:  LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    DBG_BDMP2
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            POP     AF
            ; --- original after-SVC debug ---
            PUSH    AF
            LD      A,'S'
            OUT     (050H),A
            LD      A,'9'
            OUT     (050H),A
            LD      A,'a'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show A result
            LD      A,' '
            OUT     (050H),A
            LD      A,'C'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            JR      C,DBG_D3_CY2
            LD      A,'0'
            JR      DBG_D3_CY2D
DBG_D3_CY2:
            LD      A,'1'
DBG_D3_CY2D:
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- LD A,B / SVC #3A (A=channel) ---
            LD      A,B
            PUSH    AF
            LD      A,'S'
            OUT     (050H),A
            LD      A,'A'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show channel being sent
            CALL    DBG_CRLF
            POP     AF
            RST     018H
            DB      03AH
            ; --- Debug: after SVC #3A ---
            PUSH    AF
            LD      A,'S'
            OUT     (050H),A
            LD      A,'A'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            LD      A,'C'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            JR      C,DBG_D3_CY3
            LD      A,'0'
            JR      DBG_D3_CY3D
DBG_D3_CY3:
            LD      A,'1'
DBG_D3_CY3D:
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- POP AF / SVC #39 (A=original from L5882) ---
            POP     AF
            PUSH    AF
            PUSH    AF
            LD      A,'S'
            OUT     (050H),A
            LD      A,'9'
            OUT     (050H),A
            LD      A,'b'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX             ; show original A being sent
            CALL    DBG_CRLF
            POP     AF
            RST     018H
            DB      039H
            ; --- Debug: after final SVC #39 ---
            PUSH    AF
            LD      A,'S'
            OUT     (050H),A
            LD      A,'9'
            OUT     (050H),A
            LD      A,'b'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            PUSH    AF
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            LD      A,'C'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            POP     AF
            JR      C,DBG_D3_CY4
            LD      A,'0'
            JR      DBG_D3_CY4D
DBG_D3_CY4:
            LD      A,'1'
DBG_D3_CY4D:
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- Exit marker ---
            LD      A,'D'
            OUT     (050H),A
            LD      A,'3'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            LD      A,'X'
            OUT     (050H),A
            CALL    DBG_CRLF
            ; --- Clean up and return ---
            POP     AF                  ; balance the PUSH AF from after L5882
            RET

; ===========================================================================
; Data area
; ===========================================================================
SDSTARTSEC: DS      4, 000H             ; Current sector address for SD_READ (big-endian)
DIRSECBUF:  DB      0FFH                ; Cached directory sector number (0xFF = none)
FILE_STARTSEC:
            DS      4, 000H             ; Saved file start sector (absolute, big-endian)
FILE_LOADADDR:
            DW      00000H              ; Current load address during LOAD transfer
; ===========================================================================
; DBG_L1C5B_WRAP — Wrapper around CALL L1C5B to dump ZFLAG1 before and after.
; This reveals whether L1C5B modifies ZFLAG1 (specifically bit 5).
; Output format: "F1:xx>xx\r\n" where xx=before, xx=after
; ===========================================================================
DBG_L1C5B_WRAP:
            PUSH    AF
            PUSH    HL
            ; --- Print "F1:" ---
            LD      A,'F'
            OUT     (050H),A
            LD      A,'1'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            ; --- Dump ZFLAG1 before ---
            LD      A,(ZFLAG1)
            CALL    DBG_HEX
            POP     HL
            POP     AF
            ; --- Call the real L1C5B ---
            CALL    L1C5B
            ; --- Save return flags ---
            PUSH    AF
            PUSH    HL
            ; --- Print ">" ---
            LD      A,'>'
            OUT     (050H),A
            ; --- Dump ZFLAG1 after ---
            LD      A,(ZFLAG1)
            CALL    DBG_HEX
            CALL    DBG_CRLF
            POP     HL
            POP     AF
            RET

; ===========================================================================
; DBG_FLAGS_TEST — Replaces the LD HL,ZFLAG1 / BIT 7 / BIT 5 sequence.
; V1.1Q: Workaround for suspected Z80 emulator bug where AND immediate
; does not correctly set the Z flag (0x5F AND 0x20 = 0x20 yet Z is set).
; Instead of testing ZFLAG1 bit 5, check device name for "$RFS" and force
; the ZINP path directly. This is safe because this code point is only
; reached when A=0 (DIR enumeration) in SVC #39.
; ===========================================================================
DBG_FLAGS_TEST:
            LD      HL,ZFLAG1
            ; --- Check bit 7 first (error condition, applies to all devices) ---
            LD      A,(HL)
            AND     080H
            JR      Z,DBG_FT_NO7
            POP     DE              ; discard CALL return address
            JP      01F97H
DBG_FT_NO7:
            ; --- Check if this is the $RFS device by name ---
            LD      A,(ZDEVNM)      ; first char of device name
            CP      'R'
            JR      NZ,DBG_FT_NORFS
            LD      A,(ZDEVNM+1)    ; second char
            CP      'F'
            JR      NZ,DBG_FT_NORFS
            ; --- This IS $RFS — force ZINP/DIR path ---
            PUSH    AF
            LD      A,'R'
            OUT     (050H),A
            LD      A,'!'
            OUT     (050H),A
            CALL    DBG_CRLF
            POP     AF
            POP     DE              ; discard CALL return address
            JP      DBG_BIT5PATH
DBG_FT_NORFS:
            ; --- Not $RFS: fall through to ZRO path ---
            ; HL = ZFLAG1 (correct for INC HL that follows the NOPs)
            RET

; ===========================================================================
; DBG_BIT5PATH — Replacement for the SVC #39 bit-5 path at 0x2F38.
; Instead of calling through L2FD1 → IOCALL, this calls RFSINP_DIR directly
; to bypass the broken IOCALL dispatch. Also traces the path.
; Original bit-5 path: XOR A / LD HL,L2000 / LD DE,800 / LD BC,10 / CALL L2FD1
;   L2FD1 does TWO ZINP calls:
;     1st: L2FE8 → disk identity (H=0x28)
;     2nd: L2FD4 → DIR fill (H=0x20)
; We replicate both calls directly to RFSINP_DIR.
; ===========================================================================
DBG_BIT5PATH:
            ; --- Marker ---
            LD      A,'B'
            OUT     (050H),A
            LD      A,'5'
            OUT     (050H),A
            LD      A,':'
            OUT     (050H),A
            CALL    DBG_CRLF
            ;
            ; --- 1st ZINP call: disk identity (H=0x28) ---
            LD      HL,02800H           ; H=0x28 → DISKID path
            LD      A,(ZCH)             ; Channel number
            CALL    RFSINP_DIR
            ;
            ; --- 2nd ZINP call: DIR fill (H=0x20) ---
            LD      HL,L2000            ; H=0x20 → DIR fill
            LD      A,(ZCH)
            CALL    RFSINP_DIR
            ;
            ; --- After both calls, check DIR buffer ---
            PUSH    HL
            PUSH    BC
            LD      A,'D'
            OUT     (050H),A
            LD      A,'R'
            OUT     (050H),A
            LD      A,'='
            OUT     (050H),A
            LD      HL,L2000
            LD      B,008H
DBG_B5DMP:  LD      A,(HL)
            CALL    DBG_HEX
            LD      A,' '
            OUT     (050H),A
            INC     HL
            DJNZ    DBG_B5DMP
            CALL    DBG_CRLF
            POP     BC
            POP     HL
            ;
            ; --- Replicate original return logic ---
            ; Original: LD A,(HL) / OR A / JP P,01F70H / RET
            ; After L2FD1 returns, HL=0x2000 (restored by wrappers).
            ; We set HL=0x2000 explicitly since we bypassed the wrappers.
            LD      HL,L2000
            LD      A,(HL)
            OR      A
            JP      P,01F70H
            ;
            ; Clear DIR_FILLED for next DIR call
            XOR     A
            LD      (DIR_FILLED),A
            ;
            ; Return — will go through SKPDE cleanup from SVC #39 handler
            RET

FILE_SIZE:  DW      00000H              ; Remaining bytes during LOAD transfer
RFS_LD_A0:  DB      000H                ; V1.1Y: Entry A for RFSINP_LD (0=SVC #30, else SVC #33)
DIR_BUFPTR: DW      00000H              ; Current write position in DIR buffer
FINDSD_NAME:DW      00000H              ; Saved filename pointer for FINDSD
SAVE_DIRENT:DB      000H                ; Saved directory entry number for SAVE
SAVE_PENDING:DB     000H                ; 1 = SAVE in progress, trigger RFSWO on first RFSOUT call
DIR_FILLED: DB      000H                ; 1 = DIR buffer filled, next ZINP call is data transfer
SAVE_SP:    DW      00000H              ; Saved BASIC stack pointer
DIR_ENTRY_IDX: DB   000H                ; Current SD directory entry index for RFSRO (0-255)
SECTORBUF:  DS      512, 000H           ; SD card sector buffer (was EQU 0CE00H, moved here
                                        ; to avoid overlap with BASIC stack at ~0xCFFF)
            DS      128, 000H           ; Temp stack (64 levels)
TEMP_STACK_TOP:                         ; SP points here (grows downward)
RFS_END     EQU     TEMP_STACK_TOP      ; End of RFS code = new TEXTST
