Spectrum 128 ROM 0 - Incomplete disassembly

; *** SPECTRUM 128 ROM 0 DISASSEMBLY ***

; The Spectrum ROMs are copyright Amstrad, who have kindly given permission \
; to publish ROM disassemblies.

; General notes:
;
; (5B66) flags;
; bit 4 for LOAD, bit 5 for SAVE, bit 6 for MERGE, bit 7 for VERIFY
; bit 3 indicates RAMdisk or not
;
; (5b8d-5d91): last value printed by calculator
;
; page 7:
; EC00-02, 03-05 are flags of some sort
; EC06-07 is two-word store
; (EC08;EC09) seems to be the last line number entered
;
; EC0C: current menu index
; 
; EC0D flags:
; bit 0 : ?
;         Forcibly setting it seems to ignore all keys apart from DELETE
;        (which clears it) and ENTER (which doesn't)
; bit 1 : set when a menu is displayed
; bit 2 : ?
; bit 3 : set when the current line has been altered
; bits 4 and 5 set during calculator; bit 4 reset when returning to main menu
; bit 6 : set when editing area is the lower screen
; bit 7 : set during editing (when waiting for editing keypress?)
; 
; EC0E starts at 00, goes to 04 when entering calc mode,
;    goes to FF when aborting Tape Loader,
;    set to 07 in some other Tape Loader scenario
;
; EC13 is the temporary store for P-FLAG
;
; EC15 holds the number of editing lines: 20 for the main screen, 1 for the
;      lower screen.
; EC16... holds the currently editing stuff;
;  32 * chars + 3 * flags
;  characters on the screen but not editable (eg indent chars after line wrap)
;  are zero
;  first flag byte:
;   on first line,       seems to be 09 until full, then goes to 03
;                                    1001                        0011
;   on subsequent lines, seems to be 08 until full, then goes to 02
;                                    1000                        0010
;    bit 0 = first line flag?
; F6EA holds the jump table for the current menu
; F6EC holds the text information for the current menu
; F6EE,F6EF,F6F0 holds the cursor position - row, column, alternate column.
;
; Line being edited has a temporary copy (???) made at f6f8?
; 
; FC9A,B holds the line at the top of the screen, or 0 for the first line
; 
; AY-3-8912 registers
; Register       Function                        Range
; 0              Channel A fine pitch            8-bit (0-255)
; 1              Channel A course pitch          4-bit (0-15)
; 2              Channel B fine pitch            8-bit (0-255)
; 3              Channel B course pitch          4-bit (0-15)
; 4              Channel C fine pitch            8-bit (0-255)
; 5              Channel C course pitch          4-bit (0-15)
; 6              Noise pitch                     5-bit (0-31)
; 7              Mixer                           8-bit
; 8              Channel A volume                4-bit (0-15)
; 9              Channel B volume                4-bit (0-15)
; 10             Channel C volume                4-bit (0-15)
; 11             Envelope fine duration          8-bit (0-255)
; 12             Envelope course duration        8-bit (0-255)
; 13             Envelope shape                  4-bit (0-15)
; 14             I/O port A                      8-bit (0-255)

; RAMdisk format:
;    Files start at 1C000 and build upwards
;    Catalogue entries start at 7EBFF and build downwards
;    Directory stack doesn't go below 7C000
;    Start address at +0A,+0B,+0C
;    Length at +0D,+0E,+0F
;    End addresss at +10,+11,+12

; ROM 1 subroutines
EXPT_EXP    .equ $1c8c
STK_FETCH   .equ $2bf1
CLS         .equ $0d6b 
CLS_LOWER   .equ $0d6e
TEST_SCREEN .equ $3c04

; ** The restart and initialisation routines **
L0000 di                ; Reset the machine.
      ld   bc,$692b     ; 
L0004 dec  bc           ; 
      ld   a,b          ; 
      or   c            ; 
      jr   nz,L0004     ; (There is no RST 8).
      jp   L00c7        ; to the main reset routine
      .byte 00,00,00,00 ; spare bytes

; RST 10,18 and 20 call the equivalent subroutines in ROM 1, via RST 28.

L0010  rst  28h          ; 
L0011  .word $0010       ;
L0013  ret               ;
L0014  .byte 00,00,00,00  ; spare bytes

L0018  rst  28h          ;
       .word $0018       ;
       ret               ;
       .byte 00,00,00,00  ; spare bytes

L0020  rst  28h          ;
L0021  .word $0020       ;
L0023  ret               ;
       .byte 00,00,00,00  ; spare bytes

; RST 28 calls a routine in ROM 1 (or alternatively a routine in RAM while \
; ROM 1 is paged in). Call as follows: RST 28 / .word address

L0028  ex   (sp),hl      ; Get the address after the RST 28 into HL,
                         ; saving HL on the stack.
       push af           ; 
       ld   a,(hl)       ;
L002b  inc  hl           ; Point HL to the byte after
       inc  hl           ; the required address.
       ld   ($5b5a),hl   ; Store this in RETADDR.
L0030  dec  hl           ; 
       ld   h,(hl)       ;
L0032  ld   l,a          ; HL=subroutine to call.
       pop  af           ; Restore AF.
       jp   L005c        ; Jump ahead to continue.
L0037  .byte 0           ; Spare byte.

; Interrupt routine
L0038  push hl           ; e5
       ld   hl,$0048     ; 21 48 00
L003c  push hl           ; e5
       ld   hl,$5b00     ; 21 00 5b
L0040  push hl           ; e5
       ld   hl,$0038     ; 21 38 00
       push hl           ; e5
       jp   $5b00        ; c3 00 5b
L0048  pop  hl           ; e1
       ret               ; c9

L004a  ld   bc,$7ffd     ; 01 fd 7f
       xor  a            ; af
       di                ; f3
       out  (c),a        ; ed 79
       ld   ($5b5c),a    ; 32 5c 5b
       ei                ; fb
       dec  a            ; 3d
       ld   (iy+$00),a   ; fd 77 00
       jp   L0321        ; c3 21 03

L005c  ld   ($5b58),hl   ; 22 58 5b
       ld   hl,$5b14     ; 21 14 5b
       ex   (sp),hl      ; e3
       push hl           ; e5
L0064  ld   hl,($5b58)   ; NB There is no NMI code [AOwen]
       ex   (sp),hl      ; e3
       jp   $5b00        ; c3 00 5b

; The following code will end up at 5b00
L006b  push af           ; (5b00)
       push bc           ; (5b01)
       ld   bc,$7ffd     ; (5b02)
       ld   a,($5b5c)    ; (5b05)
       xor  $10          ; (5b08)
       di                ; (5b0a)
       ld   ($5b5c),a    ; (5b0b)
       out  (c),a        ; (5b0e)
       ei                ; (5b10)
       pop  bc           ;
       pop  af           ;
       ret               ;
       call $5b00        ;
L0082  push hl           ;
       ld   hl,($5b5a)   ;
       ex   (sp),hl      ;
       ret               ;
       di                ;
       ld   a,($5b5c)    ;
       and  $ef          ; (5b21)
       ld   ($5b5c),a    ;
       ld   bc,$7ffd     ;
       out  (c),a        ;
L0096  ei                ;
       jp   L00c3        ;
       ld   hl,$06d8     ; (5b2f)
       jr   L00a2        ;
       ld   hl,$07ca     ;
L00a2  ex   af,af'       ;
       ld   bc,$7ffd     ;
       ld   a,($5b5c)    ;
       push af           ;
       and  $ef          ;
       di                ;
       ld   ($5b5c),a    ;
       out  (c),a        ;
       jp   L05e6        ;
       ex   af,af'       ;
       pop  af           ;
       ld   bc,$7ffd     ;
       di                ;
       ld   ($5b5c),a    ;
       out  (c),a        ;
       ei                ;
       ex   af,af'       ;
       ret               ;

L00c3  ld   hl,($5b8b)   ; 2a 8b 5b
       jp   (hl)         ; e9

; Continuation of the RESET routine
L00c7  ld   b,$08        ; 06 08
L00c9  ld   a,b          ; 78
       exx               ; d9
       dec  a            ; 3d
       ld   bc,$7ffd     ; Switch RAM page
L00cf  out  (c),a        ; 
       ld   hl,$c000     ; 
       ld   de,$c001     ;
       ld   bc,$3fff     ;
       ld   a,$ff        ;
       ld   (hl),a       ; Store FF into RAM
       cp   (hl)         ; Check RAM integrity
       jr   nz,L0131     ; Fatal error
       xor  a            ;
       ld   (hl),a       ;
       cp   (hl)         ;
       jr   nz,L0131     ;
       ldir              ; Clear the whole page
       exx               ;
       djnz L00c9        ; Keep looping through pages
       ld   ($5b88),a    ; 32 88 5b
       ld   c,$fd        ; 0e fd
       ld   d,$ff        ; 16 ff
       ld   e,$bf        ; 1e bf
       ld   b,d          ; 42
       ld   a,$0e        ; 3e 0e
       out  (c),a        ; ed 79
       ld   b,e          ; 43
       ld   a,$ff        ; 3e ff
       out  (c),a        ; ed 79
       jr   L0137        ; 18 38

       .byte $00          ; spare byte

; Jump table. For descriptions of 118..12a, see
; http://groups.google.com/groups?selm=bbbede39.0310140525.5b1e5a78%40posting.google.com
L0100  jp   L17af        ; c3 af 17
L0103  jp   L1838        ; c3 38 18
L0106  jp   L1ecf        ; c3 cf 1e
L0109  jp   L1f04        ; c3 04 1f
L010c  jp   L004a        ; c3 4a 00
L010f  jp   L03a2        ; c3 a2 03
L0112  jp   L182a        ; c3 2a 18
L0115  jp   L18a8        ; c3 a8 18
L0118  jp   L012d        ; Keypad scan
L011b  jp   L0a05        ; Play music strings
L011e  jp   L11a3        ; Send to MIDI
L0121  jp   L06d8        ; RS232 input
L0124  jp   L07ca        ; RS232 output (1)
L0127  jp   L08a3        ; RS232 output (2)
L012a  jp   L08f0        ; COPY (screen dump) routine

L012d  rst  28h          ; 
       .word $3b01
       ret

; Fatal RAM error
L0131  exx               ;
       ld   a,b          ; Indicate which RAM page failed by
       out  ($fe),a      ; setting the border colour
L0135  jr   L0135        ; Infinite loop

; Next bit by Geoff Wearmouth.

; Reset routine continued
; complete setting up sound chip registers.
L0137  ld   b,d          ; 42
       ld   a,$07        ; 3e 07
       out  (c),a        ; ed 79
       ld   b,e          ; 43
       ld   a,$ff        ; 3e ff
       out  (c),a        ; ed 79

; Now copy the paging mechanism. 

       ld   de,$5b00     ; The destination is the start of the old ZX buffer.
       ld   hl,$006b     ; The source is in this ROM.
       ld   bc,$0058     ; There are eighty eight bytes to copy.
       ldir              ; Copy the block of bytes.

       ld   a,$cf        ; Load A with the code for the Z80 instruction 'RST 08'.
       ld   ($5b5d),a    ; Insert into New System Variable RAMRST.
       ld   sp,$5bff     ; Set the stack pointer to last location of old buffer.

       ld   a,$04        ; page in logical page 4
       call L1c64        ;   (physical page 7)

       ld   ix,$ebec     ; "end of catalogue" marker in RAMdisk
       ld   ($5b83),ix   ; SF_NEXT
       ld   (ix+$0a),$00 ; 
       ld   (ix+$0b),$c0 ;
       ld   (ix+$0c),$00 ;
       ld   hl,$2bec     ;
       ld   a,$01        ; AHL = free space in RAMdisk
       ld   ($5b85),hl   ; 
       ld   ($5b87),a    ;

       ld   a,$05        ; page in logical page 5
       call L1c64        ;  (physical page 0)

       ld   hl,$ffff     ; Load HL with known last working byte - 65535.
       ld   ($5cb4),hl   ; Set P-RAMT standard System Variable.

       ld   de,$3eaf     ; Set DE to address of the last bitmap of 'U' in the
                         ; Main ROM.
       ld   bc,$00a8     ; There are 21 User Defined Graphics to copy.
       ex   de,hl        ; Swap so destination is $FFFF.
       rst  28h          ; 
       .word $1661       ; calling this address (LDDR/RET) in the Main ROM
                         ; cleverly copies the 21 characters to the end of RAM.

       ex   de,hl        ; Transfer DE to HL.
       inc  hl           ; Increment to address first byte of UDG 'A'.
       ld   ($5c7b),hl   ; Update standard System Variable UDG.

       dec  hl           ; decrement HL.
       ld   bc,$0040     ; set values zero and 64.
       ld   ($5c38),bc   ; Update standard System Variables RASP and PIP.
       ld   ($5cb2),hl   ; Update standard System Variable RAMTOP - the last
                         ; byte of the BASIC system area. Any machine code and
                         ; graphics above this address are protected from NEW.

; entry point for NEW with Interrupts disabled and physical page zero occupying
; the upper RAM region $C000 - $FFFF - the normal BASIC memory configuration.

L019d  ld   hl,$3c00     ; Set HL to where, in theory character zero would be.
       ld   ($5c36),hl   ; Update standard System Variable CHARS.

       ld   hl,($5cb2)   ; Load HL with value of System Variable RAMTOP.
       inc  hl           ; Address next location.
       ld   sp,hl        ; Set the Stack Pointer.
       im   1            ; Select Interrupt Mode 1.
       ld   iy,$5c3a     ; Set the IY register to address the Standard System
                         ; Variables and many of the new System Variables and
                         ; even those of Interface 1 in some cases.
       set  4,(iy+$01)   ; Update FLAGS - signal 128K mode
                         ; Note. This bit was unused and therefore never set
                         ; by 48K BASIC.

       ei                ; With a stack and the IY register set, Interrupts can
                         ; be enabled.

       ld   hl,$000b     ; Set HL to eleven.
       ld   ($5b5f),hl   ; Update new System Variable BAUD - the RS232 bit 
                         ; period. A value of eleven is equivalent to 9600 baud.

       xor  a            ; Clear accumulator.
       ld   ($5b61),a    ; Set new System Variable SERFL to zero.
       ld   ($5b63),a    ; Set new System Variable COL to zero.
       ld   ($5b65),a    ; Set new System Variable TVPARS to zero.

       ld   hl,$ec00     ; Should write to  page 7 [AOwen]
       ld   ($ff24),hl   ;

; Note. this is dangerously close to the machine stack. \
; The 1985 Sinclair Research ESPAGNOL source code says that this instruction \
; will write to the [previously cleared] Main BASIC RAM during initialization \
; but that a different page of RAM will be present during NEW. \
; Stuff and Nonsense! Assemblers and other languages will be corrupted by the \
; BASIC NEW command.

       ld   a,$50        ; Set printer width to a default of eighty.
       ld   ($5b64),a    ; Update new System Variable WIDTH for 80 columns.
       ld   hl,$000a     ; set HL to ten.
       ld   ($5b94),hl   ; Store in spare locations between STRIP2 
L01d4  ld   ($5b96),hl   ; and the temporary stack TSTACK.

       ld   hl,$5cb6     ; Load HL with address following System Variables.
       ld   ($5c4f),hl   ; Set standard System Variable CHANS.

       ld   de,$0589     ; Point to Initial Channel Information in this ROM.
                         ; Note. this is similar to that in Main ROM but 
                         ; channel 'P' has input and output addresses in the
                         ; new $5Bxx region.
       ld   bc,$0015     ; There are 21 bytes to copy.
       ex   de,hl        ; Switch pointer so destination is CHANS.
       ldir              ; Copy the block of bytes.

       ex   de,hl        ; Transfer DE to HL.
       dec  hl           ; Decrement to point to $80 end-marker.
       ld   ($5c57),hl   ; Update standard System Variable DATADD to this 
                         ; resting address.
       inc  hl           ; Bump address.
       ld   ($5c53),hl   ; Set standard System Variable PROG.
       ld   ($5c4b),hl   ; Set standard System Variable VARS.
       ld   (hl),$80     ; Insert the Variables end-marker.
       inc  hl           ; Increment the address.
       ld   ($5c59),hl   ; Set standard System Variable ELINE.
       ld   (hl),$0d     ; Insert a carriage return.
       inc  hl           ; Increment address. 
       ld   (hl),$80     ; Insert the $80 end-marker.
       inc  hl           ; increment address.
       ld   ($5c61),hl   ; Set the standard System Variable WORKSP.
       ld   ($5c63),hl   ; Set the standard System Variable STKBOT.
       ld   ($5c65),hl   ; Set the standard System Variable STKEND.

       ld   a,$38        ; Set colour attribute to black ink on white paper.
       ld   ($5c8d),a    ; Set the standard System Variable ATTR_P.
       ld   ($5c8f),a    ; Set the standard System Variable MASK_P.
       ld   ($5c48),a    ; Set the standard System Variable BORDCR.

       xor  a            ; Clear the accumulator.
       ld   ($ec13),a    ; and zero a byte in the upper page of RAM.

; Note $EC13 is the temporary store for P-FLAG in Page 7 of RAM.
; Again Main RAM has been corrupted.

       ld   a,$07        ; Load the accumulator with colour for white.
       out  ($fe),a      ; Output to Port $FE changing border to white.

       ld   hl,$0523     ; The values five and thirty five.
       ld   ($5c09),hl   ; Set the standard System Variables REPDEL and REPPER

       dec  (iy-$3a)     ; Set KSTATE-0 to $FF
       dec  (iy-$36)     ; Set KSTATE-4 to $FF

       ld   hl,$059e     ; Set source to Initial Stream Data in this ROM.
                         ; Note. this is identical to that is Main ROM.
       ld   de,$5c10     ; Set destination to standard System Variable STRMS-FD
       ld   bc,$000e     ; There are fourteen bytes to copy.
       ldir              ; Block copy the bytes.
       res  1,(iy+$01)   ; Update FLAGS - signal printer not is use.
       ld   (iy+$00),$ff ; Set standard System Variable ERR_NR to $FF (OK-1).
       ld   (iy+$31),$02 ; Set standard Sytem Variable DF_SZ to two lines.
       rst  28h
       .word CLS
       rst  28h
       .word TEST_SCREEN
       ld   de,$0561     ; Sinclair copyright message
       call L057d        ; print a message terminated by having bit 7 set.

       ld   (iy+$31),$02 ; Set standard System Variable DF_SZ to two lines.
       set  5,(iy+$02)   ; Update TV-FLAG - signal lower screen will require 
                         ; clearing.
       ld   hl,$5bff     ; Set HL to location of temporary stack
       ld   ($5b81),hl   ; Store in STRIP1.
       call L1f45        ; switch RAM to Page 7
       ld   a,$38        ; set colours to black ink on white paper.
       ld   ($ec11),a    ; 32 11 ec
       ld   ($ec0f),a    ; 32 0f ec

; Note this is where  $EC13 and $FF24 should be set!!!!

       call L2584        ; 
       call L1f20        ; 
       jp   L259f        ;  to menu ........



L026b  ld   hl,$5b66     ; 21 66 5b
       set  0,(hl)       ; cb c6
       ld   (iy+$00),$ff ; '0 OK' status
       ld   (iy+$31),$02 ; fd 36 31 02
       ld   hl,$5b1d     ; 21 1d 5b
       push hl           ; e5
       ld   ($5c3d),sp   ; ed 73 3d 5c
       ld   hl,$02ba     ; 21 ba 02
       ld   ($5b8b),hl   ; 22 8b 5b
       call L228e        ; Point to start of BASIC command
       call L22cb        ; cd cb 22
       jp   z,L21f8      ; ca f8 21
       cp   $28          ; '('
       jp   z,L21f8      ; ca f8 21
       cp   $2d          ; '-'
       jp   z,L21f8      ; ca f8 21
       cp   $2b          ; '+'
       jp   z,L21f8      ; ca f8 21
       call L22e0        ; cd e0 22
       jp   z,L21f8      ; ca f8 21
       call L1f45        ; cd 45 1f
       ld   a,($ec0e)    ; 3a 0e ec
       call L1f20        ; cd 20 1f
       cp   $04          ; Calculator mode?
       jp   nz,L17af     ; c2 af 17
       call L2297        ; cd 97 22
       jp   z,L17af      ; ca af 17
       pop  hl           ; e1
       ret               ; c9

L02ba  bit  7,(iy+$00)   ; fd cb 00 7e
       jr   nz,L02c1     ; 20 01
L02c0  ret               ; c9
L02c1  ld   hl,($5c59)   ; 2a 59 5c
       ld   ($5c5d),hl   ; 22 5d 5c
       rst  28h          ; ef
       .word $19fb ; E-LINE-NO
       ld   a,b          ; 78
       or   c            ; b1
       jp   nz,L03f7     ; c2 f7 03
       rst  18h          ; df
       cp   $0d          ; fe 0d
       ret  z            ; c8
       call L21ef        ; cd ef 21
       bit  6,(iy+$02)   ; fd cb 02 76
       jr   nz,L02df     ; 20 03
       rst  28h          ; ef
       .word CLS_LOWER
L02df  res  6,(iy+$02)   ; fd cb 02 b6
       call L1f45        ; cd 45 1f
       ld   hl,$ec0d     ; 
       bit  6,(hl)       ; Test for editing in lower-screen area
       jr   nz,L02f4     ; 20 07
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       cp   $00          ; fe 00
       call z,L3881      ; cc 81 38
L02f4  call L1f20        ; cd 20 1f
       ld   hl,$5c3c     ; 21 3c 5c
       res  3,(hl)       ; cb 9e
       ld   a,$19        ; 3e 19
       sub  (iy+$4f)   ; fd 96 4f
       ld   ($5c8c),a    ; 32 8c 5c
       set  7,(iy+$01)   ; fd cb 01 fe
       ld   (iy+$0a),$01 ; fd 36 0a 01
       ld   hl,$3e00     ; 21 00 3e
       push hl           ; e5
       ld   hl,$5b1d     ; 21 1d 5b
       push hl           ; e5
       ld   ($5c3d),sp   ; ed 73 3d 5c
       ld   hl,$0321     ; 21 21 03
       ld   ($5b8b),hl   ; 22 8b 5b
       jp   L1838        ; c3 38 18
L0321  ld   sp,($5cb2)   ; ed 7b b2 5c
       inc  sp           ; 33
       ld   hl,$5bff     ; 21 ff 5b
       ld   ($5b81),hl   ; 22 81 5b
       halt              ; 76
       res  5,(iy+$01)   ; fd cb 01 ae
       ld   hl,$5b66     ; 21 66 5b
       bit  2,(hl)       ; cb 56
       jr   z,L034a      ; 28 12
       call L1f45        ; cd 45 1f
       ld   ix,($5b83)   ; SF_NEXT
       ld   bc,$0014     ; 01 14 00
       add  ix,bc        ; dd 09
       call L1d56        ; cd 56 1d
       call L1f20        ; cd 20 1f
L034a  ld   a,($5c3a)    ; 3a 3a 5c
       inc  a            ; 3c
L034e  push af           ; f5
       ld   hl,$0000     ; 21 00 00
       ld   (iy+$37),h   ; fd 74 37
       ld   (iy+$26),h   ; fd 74 26
       ld   ($5c0b),hl   ; 22 0b 5c
       ld   hl,$0001     ; 21 01 00
       ld   ($5c16),hl   ; 22 16 5c
       rst  28h          ; ef
       .word $16b0 ; SET-MIN  "Clears editing area and areas after it"
       res  5,(iy+$37)
       rst  28h          ; ef
       .word CLS_LOWER
       set  5,(iy+$02)   ; fd cb 02 ee
       pop  af           ; f1
       ld   b,a          ; 47
       cp   $0a          ; Is it a digit?
       jr   c,L037f      ; If so jump ahead to print it
       cp   $1d          ; Is it one of the old errors (A-R)?
       jr   c,L037d      ; If so jump ahead to add 7 and print
       add  a,$14        ; Otherwise add 20
       jr   L037f        ; and jump ahead to print
                         ; [Could have saved two bytes by having add $0c instead
                         ; of these two instructions]
L037d  add  a,$07        ;
L037f  rst  28h          ;
       .word $15ef       ; OUT-CODE
       ld   a,$20        ; Print a space
       rst  10h          ; 
       ld   a,b          ; Restore the error code
       cp   $1d          ; 
       jr   c,L039c      ; Old error message
       sub  $1d          ; 
       ld   b,$00        ;
       ld   c,a          ;
       ld   hl,L046c     ; start of new error messages
       add  hl,bc        ; 
       add  hl,bc        ; 
       ld   e,(hl)       ;
       inc  hl           ; get start of requested error
       ld   d,(hl)       ; message in DE
       call L057d        ; print error message
       jr   L03a2        ; jump ahead
L039c  ld   de,$1391     ; Position of the error messages in ROM 1
       rst  28h          ;
      .word $0c0a        ; PO-MSG, message printing
L03a2  xor  a            ; af
       ld   de,$1536     ; 11 36 15
       rst  28h          ; ef
      .word $0c0a        ; PO-MSG; print a comma followed by a space
       ld   bc,($5c45)   ; PPC, current line number
       rst  28h          ; ef
      .word $1a1b        ; OUT-NUM-1
       ld   a,$3a        ; ":"
       rst  10h          ;
       ld   c,(iy+$0d)   ; SUBPPC
       ld   b,$00        ;
       rst  28h          ;
      .word $1a1b        ; OUT-NUM-1
       rst  28h          ;
      .word $1097 ; CLEAR-SP  ; tidy up
       ld   a,($5c3a)    ; ERR NR
       inc  a
       jr   z,L03df      ; Jump ahead for 0 OK
       cp   $09          ; 
       jr   z,L03cc      ; Jump for A Invalid argument
       cp   $15          ; 
       jr   nz,L03cf     ; Jump unless M Ramtop no good
L03cc  inc  (iy+$0d)     ; SUBPPC
L03cf  ld   bc,$0003     ; 01 03 00
       ld   de,$5c70     ; 11 70 5c
       ld   hl,$5c44     ; 21 44 5c
       bit  7,(hl)       ; cb 7e
       jr   z,L03dd      ; 28 01
       add  hl,bc        ; 09
L03dd  lddr              ; ed b8
L03df  ld   (iy+$0a),$ff ; fd 36 0a ff
       res  3,(iy+$01)   ; fd cb 01 9e
       ld   hl,$5b66     ; 21 66 5b
       res  0,(hl)       ; cb 86
       jp   L25cb        ; c3 cb 25
L03ef  ld   a,$10        ; 3e 10
       ld   bc,$0000     ; 01 00 00
       jp   L034e        ; c3 4e 03
L03f7  ld   ($5c49),bc   ; ed 43 49 5c
       call L1f45        ; cd 45 1f
       ld   a,b          ; 78
       or   c            ; b1
       jr   z,L040a      ; 28 08
       ld   ($5c49),bc   ; ed 43 49 5c
       ld   ($ec08),bc   ; ed 43 08 ec
L040a  call L1f20        ; cd 20 1f
       ld   hl,($5c5d)   ; 2a 5d 5c
       ex   de,hl        ; eb
       ld   hl,$03ef     ; 21 ef 03
       push hl           ; e5
       ld   hl,($5c61)   ; 2a 61 5c
       scf               ; 37
       sbc  hl,de        ; ed 52
       push hl           ; e5
       ld   h,b          ; 60
       ld   l,c          ; 69
       rst  28h          ; ef
       .word $196e ; LINE-ADDR
       jr   nz,L0429     ; 20 06
       rst  28h          ; ef
       .word $19b8 ; NEXT-ONE
       rst  28h          ; ef
      .word $19e8 ; RECLAIM-2
L0429  pop  bc           ; c1
       ld   a,c          ; 79
       dec  a            ; 3d
       or   b            ; b0
       jr   nz,L0442     ; 20 13
       call L1f45        ; cd 45 1f
       push hl           ; e5
       ld   hl,($5c49)   ; 2a 49 5c
       call L334a        ; cd 4a 33
       ld   ($5c49),hl   ; 22 49 5c
       pop  hl           ; e1
       call L1f20        ; cd 20 1f
       jr   L046a        ; 18 28
L0442  push bc           ; c5
       inc  bc           ; 03
       inc  bc           ; 03
       inc  bc           ; 03
       inc  bc           ; 03
       dec  hl           ; 2b
       ld   de,($5c53)   ; ed 5b 53 5c
       push de           ; d5
       rst  28h          ; ef
       .word $1655 ; MAKE-ROOM
       pop hl
       ld   ($5c53),hl   ; 22 53 5c
       pop  bc           ; c1
       push bc           ; c5
       inc  de           ; 13
       ld   hl,($5c61)   ; 2a 61 5c
       dec  hl           ; 2b
       dec  hl           ; 2b
       lddr              ; ed b8
       ld   hl,($5c49)   ; 2a 49 5c
       ex   de,hl        ; eb
       pop  bc           ; c1
       ld   (hl),b       ; 70
       dec  hl           ; 2b
       ld   (hl),c       ; 71
       dec  hl           ; 2b
       ld   (hl),e       ; 73
       dec  hl           ; 2b
       ld   (hl),d       ; 72
L046a  pop  af           ; f1
       ret               ; c9


; Pointers into the new error message table
L046c .word $048c, $0497, $04a6, $04b0, $04c1
      .word $04d4, $04e0, $04e0, $04f3, $0501
      .word $0512, $0523, $0531, $0542, $054e, $0561

L048c .text "MERGE erro"
      .byte 'r'+$80
      .text "Wrong file typ"
      .byte 'e'+$80
      .text "CODE erro"
      .byte 'r'+$80
      .text "Too many bracket"
      .byte 's'+$80
      .text "File already exist"
      .byte 's'+$80
      .text "Invalid nam"
      .byte 'e'+$80
      .text "File does not exis"
      .byte 't'+$80
      .text "Invalid devic"
      .byte 'e'+$80
      .text "Invalid baud rat"
      .byte 'e'+$80
      .text "Invalid note nam"
      .byte 'e'+$80
      .text "Number too bi"
      .byte 'g'+$80
      .text "Note out of rang"
      .byte 'e'+$80
      .text "Out of rang"
      .byte 'e'+$80
      .text "Too many tied note"
      .byte 's'+$80
L0561 .byte $7f ; copyright sign
      .text " 1986 Sinclair Research Lt"
      .byte 'd'+$80

; Print a message which is terminated by having bit 7 set
L057d  ld   a,(de)       ;
       and  $7f          ;
       push de           ;
       rst  10h          ;
       pop  de           ;
       ld   a,(de)       ;
       inc  de           ;
       add  a,a          ;
       jr   nc,L057d     ;
       ret               ;

; 21-byte table used at 01dd (channel information)
L0589 .byte $f4,$09,$a8,$10,$4b,$f4,$09,$c4
      .byte $15,$53,$81,$0f,$c4,$15,$52,$34
      .byte $5b,$2f,$5b,$50,$80

; 14-byte table used at L0226 (stream information)
L059e .byte $01,$00,$06,$00,$0b,$00,$01,$00
      .byte $01,$00,$06,$00,$10,$00

; Cause error report
L05ac  pop  hl           ; e1
       ld   bc,$7ffd     ; 01 fd 7f
       xor  a            ; af
       di                ; f3
       ld   ($5b5c),a    ; 32 5c 5b
       out  (c),a        ; ed 79
       ei                ; fb
       ld   sp,($5c3d)   ; ed 7b 3d 5c
       ld   a,(hl)       ; 7e
       ld   ($5b5e),a    ; 32 5e 5b
       inc  a            ; 3c
       cp   $1e          ; fe 1e
       jr   nc,L05c8     ; 30 03
       rst  28h          ; ef
       .word $5b5d
L05c8  dec  a            ; 3d
       ld   (iy+$00),a   ; fd 77 00
       ld   hl,($5c5d)   ; 2a 5d 5c
       ld   ($5c5f),hl   ; 22 5f 5c
       rst  28h          ; ef
       .word $16c5 ; SET-STK
       ret

; Check for BREAK
L05d6  ld   a,$7f        ; 3e 7f
       in   a,($fe)      ; db fe
       rra               ; 1f
       ret  c            ; d8
       ld   a,$fe        ; 3e fe
       in   a,($fe)      ; db fe
       rra               ; 1f
       ret  c            ; d8
       call L05ac        ; cd ac 05
       .byte $14         ; "L Break into program"
L05e6  ei                ; fb
       ex   af,af'       ; 08
       ld   de,$5b4a     ; 11 4a 5b
       push de           ; d5
       res  3,(iy+$02)   ; fd cb 02 9e
       push hl           ; e5
       ld   hl,($5c3d)   ; ERR SP
       ld   e,(hl)       ;
       inc  hl           ;
       ld   d,(hl)       ; DE = current error address
       and  a            ;
       ld   hl,$107f     ; HL= ED-ERROR
       sbc  hl,de        ; 
       jr   nz,L0637     ; 
       pop  hl           ; 
       ld   sp,($5c3d)   ; 
       pop  de           ;
       pop  de           ;
       ld   ($5c3d),de   ;
L060a  push hl           ;
       ld   de,$0610     ; Address to return to
       push de           ; Stack address
       jp   (hl)         ; Jump to ... ?
L0610  jr   c,L061b      ; 38 09
       jr   z,L0618      ; 28 04
L0614  call L05ac        ; 
      .byte $07          ; "8 End of file"
L0618  pop  hl           ; e1
       jr   L060a        ; 18 ef
L061b  cp   $0d          ; fe 0d
       jr   z,L062d      ; 28 0e
       ld   hl,($5b5a)   ; 2a 5a 5b
       push hl           ; e5
       rst  28h          ; ef
       add  a,l          ; 85
       rrca              ; 0f
       pop  hl           ; e1
       ld   ($5b5a),hl   ; 22 5a 5b
       pop  hl           ; e1
       jr   L060a        ; 18 dd
L062d  pop  hl           ; e1
       ld   a,($5b5c)    ; 3a 5c 5b
       or   $10          ; f6 10
       push af           ; f5
       jp   $5b4a        ; c3 4a 5b
L0637  pop  hl           ; e1
       ld   de,$063d     ; Continuation address
       push de           ; Stack address
       jp   (hl)         ; 
L063d  ret  c            ;
       ret  z            ;
       jr   L0614        ; Report "8 End of file"

; FORMAT routine
L0641  rst  28h          ; [Could just RST 18]
       .word $0018
       rst  28h          ; Get an expression
       .word EXPT_EXP
       bit  7,(iy+$01)   ;
       jr   z,L0661      ; Jump ahead if syntax checking
       rst  28h          ;
       .word STK_FETCH
       ld   a,c          ;
       dec  a            ;
       or   b            ;
       jr   z,L0659      ; Jump ahead if string is 1 character long
       call L05ac        ;
       .byte $24         ; "i Invalid device"
L0659  ld   a,(de)       ; Get character
       and  $df          ; Convert to upper case
       cp   $50          ; Is it channel "P"?
       jp   nz,L1912     ; Error "C Nonsense in BASIC" if not
L0661  ld   hl,($5c5d)   ; (CH ADD), next character
       ld   a,(hl)       ;
       cp   $3b          ; Next character must be ";"
       jp   nz,L1912     ; Otherwise error C
       rst  28h          ; Skip past the ; character
       .word $0020       ; [Could just do RST 20] 
       rst  28h          ; Get a numeric expression from the line
       .word $1c82       ; EXPT-1NUM
       bit  7,(iy+$01)   ; Jump ahead if syntax checking
       jr   z,L067d      ;
       rst  28h          ; Get the result as an integer
       .word $1e99       ; FIND-INT2
       ld   ($5b71),bc   ; Store the result in the system variables
L067d  rst  28h          ; [Could just do RST 18]
       .word $0018       ; Get the next character in the BASIC line;
       cp   $0d          ; should be ENTER...
       jr   z,L0689      ; 
       cp   $3a          ; or ":",
       jp   nz,L1912     ; otherwise error C
L0689  call L18a1        ; Check for end of line
       ld   bc,($5b71)   ; Get the baud rate
       ld   a,b          ; Is it zero?
       or   c            ;
       jr   nz,L0698     ;
       call L05ac        ; If so, then report error
       .byte $25         ; "j invalid baud rate"
L0698  ld   hl,L06b8     ; Table of known baud rates
L069b  ld   e,(hl)       ;
       inc  hl           ;
       ld   d,(hl)       ;
       inc  hl           ; 
       ex   de,hl        ; HL = known baud rate
       ld   a,h          ; If we have reached the last item in the table without
       cp   $25          ; using a baud rate, then use this one (9600)
       jr   nc,L06af     ; 
       and  a            ; 
       sbc  hl,bc        ; Use this baud rate?
       jr   nc,L06af     ;
       ex   de,hl        ;
       inc  hl           ; Skip past the internal value used
       inc  hl           ; for the baud rate
       jr   L069b        ;
L06af  ex   de,hl        ; HL points to internal representation 
       ld   e,(hl)       ; 
       inc  hl           ;
       ld   d,(hl)       ; DE holds internal representation (T states/26 according to +3 manual)
       ld   ($5b5f),de   ; Store new value in BAUD SYS_VAR
       ret               ; Finish

; BAUD_RATE_TABLE
L06b8 .word $0032,$0aa5
      .word $006e,$04d4
      .word $012c,$01c3
      .word $0258,$00e0
      .word $04b0,$006e
      .word $0960,$0036
      .word $12c0,$0019
      .word $2580,$000b

; RS232 input
L06d8  ld   hl,$5b61     ; hl= SERFL SYS_VAR  keeps second char that can be received
       ld   a,(hl)       ; Is the second-character received flag set,
       and  a            ; i.e. have we already received data?
       jr   z,L06e5      ; Jump ahead if not.
       ld   (hl),$00     ; Otherwise clear the flag
       inc  hl           ; 
       ld   a,(hl)       ; and return the data which we received earlier.
       scf               ; Set carry flag to indicate success
       ret               ; 


; BAUD system variable: 23391 ,$5b5f
; Value = (3500000/(25.7*BaudRate))-3
;REC-BYTE  routine that handles the receiving of a byte              
L06e5  call L05d6         ; Check for BREAK    
       di                
       exx               
       ld   de,($5b5f)   ;Get BAUD
       ld   hl,($5b5f)   
       srl  h            ;  0 -> 76543210 -> C  Halve the value 
       rr   l            ;  C -> 76543210 -> C 
                         ; HL=1/2 BAUD
       or   a            
       ld   b,$fa        ;waiting time for start bit
       exx               
       ld   c,$fd        
       ld   d,$ff        
       ld   e,$bf        
       ld   b,d          
       ld   a,$0e        
       out  (c),a        ;selects register 14, port I/O of AY
       in   a,(c)        
       or   $f0          ;%11110000
       and  $fb          ;%11111011
                         ;result=%1111x0xx  
       ld   b,e          
       out  (c),a        ; Make CTS (Clear To Send) 
       ld   h,a          
L070e  ld   b,d          
       in   a,(c)        
       and  $80           ;%10000000 ;bit 7- RS232 pin 5 (TXD in)
       jr   z,L071e       ;First test for START BIT - test if it's zero 
L0715  exx               
       dec  b            
       exx               
       jr   nz,L070e     
       xor  a            
       push af            ;(*) Save the zero failure flag 
       jr   L0757         ;  time out waiting for START BIT

L071e  in   a,(c)         ;2. Second test - test again to see if it's zero -  START BIT
       and  $80          
       jr   nz,L0715     
       in   a,(c)        
       and  $80          ;3. third test - test again to see if it's zero -  START BIT
       jr   nz,L0715     

  ; ---
  ;   The branch is executed when TXdata was high for 3 tests.

       exx               
       ld   bc,$fffd     
       ld   a,$80        ;A stays with START BIT  
       ex   af,af'       
L0731  add  hl,de        ; HL=BAUD +1/2 BAUD  
       nop               ; (4) timing value  
       nop               
       nop               
       nop   
;BD-DELAY             
L0736  dec  hl            ; ( 6) Delay for 26 * BAUD 
       ld   a,h           ; ( 4) 
       or   l             ; ( 4)  
       jr   nz,L0736      ; (12) back to BD-DELAY

       in   a,(c)          ; Read a BIT  
       and  $80          
       jp   z,L074b      
       ex   af,af'         ;received one 1
       scf                 ;set  carry  
       rra                 ; C->76543210->C  
       jr   c,L0754        ;When we reach START BIT it ends 
  
       ex   af,af'       
       jp   L0731        ;Go read next bit

L074b  ex   af,af'        ;received one 0
       or   a             ;clear carry
       rra                ; C->76543210->C  
       jr   c,L0754       ;When we reach START BIT it ends
       ex   af,af'       
       jp   L0731         ;Go read next bit

;   After looping eight times, the start bit will pass through and A will
;   contain a received byte.

L0754  scf               ; signal success.
       push af            ; (*) push success flag  
       exx               

 ;   The success and failure (TIME OUT START BIT) paths converge here 
L0757  ld   a,h          
       or   $04      

   ;result=%1111x1xx
    
       ld   b,e           ;e=$bf  
       out  (c),a         ;reset CTS line 
       exx               
       ld   h,d
       ld   l,e          ; HL = (BAUD)
       ld   bc,$0007
       or   a
       sbc  hl,bc        ; HL = (BAUD) - 7

L0766  dec  hl         ;Delay for stop bit
       ld   a,h
       or   l
       jr   nz,L0766
                        ;hl=0

       ld   bc,$fffd     
       add  hl,de        
       add  hl,de        
       add  hl,de        ;HL = 3 * BAUD  

;   The device at the other end of the cable may send a second byte even though
;   CTS is low. So now repeat the procedure to read another byte

L0771  in   a,(c)         ;test START BIT
       and  $80          
       jr   z,L077f      
       dec  hl           
       ld   a,h          
       or   l            
       jr   nz,L0771     
       pop  af            ; restore byte and flags
       ei                 ; (either 0 and NC or received byte and carry).
       ret               

L077f  in   a,(c)         ;performs again  3 tests to START BIT
       and  $80          
       jr   nz,L0771     
       in   a,(c)        
       and  $80          
       jr   nz,L0771     

;   A second byte is on its way and is received exactly as before.
       ld   h,d          
       ld   l,e          
       ld   bc,$0002     
       srl  h            
       rr   l            
       or   a            
       sbc  hl,bc        
       ld   bc,$fffd     
       ld   a,$80        
       ex   af,af'       
L079d  nop               
       nop               
       nop               
       nop               
       add  hl,de        
L07a2  dec  hl           
       ld   a,h          
       or   l            
       jr   nz,L07a2     
       in   a,(c)        
       and  $80          
       jp   z,L07b7      
       ex   af,af'       
       scf               
       rra               
       jr   c,L07c0      
       ex   af,af'       
       jp   L079d        
L07b7  ex   af,af'       
       or   a            
       rra               
       jr   c,L07c0      
       ex   af,af'       
       jp   L079d        
L07c0  ld   hl,$5b61      ; SERFL:   second char ;flag e data
       ld   (hl),$01     
       inc  hl           
       ld   (hl),a       
       pop  af           
       ei                
       ret               


; The following has something to do with the LPRINT CHR$ 16; CHR$ 1; "ABC" bug
; (getting here from 063C)
L07ca  push af           ; A = character to print
       ld   a,($5b65)    ; TVPARS, #parameters expected
       or   a            ;
       jr   z,L07e0      ;
       dec  a            ;
       ld   ($5b65),a    ;
       jr   nz,L07db     ; Jump ahead if we have not processed all parameters
       pop  af           ; 
       jp   L0872        ; Otherwise...
L07db  pop  af           ;
       ld   ($5c0f),a    ; TVDATA + 1
       ret               ;
L07e0  pop  af           ;
       cp   $a3          ; "SPECTRUM"
       jr   c,L07f2      ;
       ld   hl,($5b5a)   ; Save RETADDR temporarily
       push hl           ;
       rst  28h          ;
      .word $0b52 ; PO-T&UDG
       pop  hl           ;
       ld   ($5b5a),hl   ;
       scf               ;
       ret               ;
L07f2  ld   hl,$5c3b     ; FLAGS
       res  0,(hl)       ;
       cp   $20          ;
       jr   nz,L07fd     ;
       set  0,(hl)       ;
L07fd  cp   $7f          ;
       jr   c,L0803      ; 38 02
       ld   a,$3f        ; 3e 3f
L0803  cp   $20          ; fe 20
       jr   c,L081e      ; 38 17
L0807  push af           ; f5
       ld   hl,$5b63     ; COL, column number
       inc  (hl)         ; 34
       ld   a,($5b64)    ; WIDTH
       cp   (hl)         ; be
       jr   nc,L081a     ; 30 08
       call L0822        ; cd 22 08
       ld   a,$01        ; 3e 01
       ld   ($5b63),a    ; 32 63 5b
L081a  pop  af           ; f1
       jp   L08a3        ; c3 a3 08
L081e  cp   $0d          ; fe 0d
       jr   nz,L0830     ; 20 0e
L0822  xor  a            ; af
       ld   ($5b63),a    ; 32 63 5b
       ld   a,$0d        ; 3e 0d
       call L08a3        ; cd a3 08
       ld   a,$0a        ; 3e 0a
       jp   L08a3        ; c3 a3 08
L0830  cp   $06          ; fe 06
       jr   nz,L0853     ; 20 1f
       ld   bc,($5b63)   ; ed 4b 63 5b
       ld   e,$00        ; 1e 00
L083a  inc  e            ; 1c
       inc  c            ; 0c
       ld   a,c          ; 79
       cp   b            ; b8
       jr   z,L0848      ; 28 08
L0840  sub  $08        ; d6 08
       jr   z,L0848      ; 28 04
       jr   nc,L0840     ; 30 fa
       jr   L083a        ; 18 f2
L0848  push de           ; d5
       ld   a,$20        ; 3e 20
       call L07ca        ; cd ca 07
       pop  de           ; d1
       dec  e            ; 1d
       ret  z            ; c8
       jr   L0848        ; 18 f5
L0853  cp   $16          ; AT
       jr   z,L0860      ; 
       cp   $17          ; TAB
       jr   z,L0860      ;
       cp   $10          ; fe 10
       ret  c            ; d8
       jr   L0869        ; 18 09
L0860  ld   ($5c0e),a    ; 32 0e 5c
       ld   a,$02        ; 3e 02
       ld   ($5b65),a    ; TV_PARS
       ret               ; c9
L0869  ld   ($5c0e),a    ;
       ld   a,$02        ; Should be 1
       ld   ($5b65),a    ; 
       ret               ; 

L0872  ld   d,a          ; D = character to print
       ld   a,($5c0e)    ; TV DATA
       cp   $16          ;
       jr   z,L0882      ; 
       cp   $17          ;
       ccf               ;
       ret  nz           ;
       ld   a,($5c0f)    ; TV DATA + 1
       ld   d,a          ;
L0882  ld   a,($5b64)    ; WIDTH
       cp   d            ; ba
       jr   z,L088a      ; 28 02
       jr   nc,L0890     ; 30 06
L088a  ld   b,a          ; 47
       ld   a,d          ; 7a
       sub  b            ; 90
       ld   d,a          ; 57
       jr   L0882        ; 18 f2
L0890  ld   a,d          ; 7a
       or   a            ; b7
       jp   z,L0822      ; ca 22 08
L0895  ld   a,($5b63)    ; 3a 63 5b
       cp   d            ; ba
       ret  z            ; c8
       push de           ; d5
       ld   a,$20        ; 3e 20
       call L07ca        ; cd ca 07
       pop  de           ; d1
       jr   L0895        ; 18 f2

; Talk to printer ;OUTPUT SERVICE   RS232
L08a3  push af           ;A=value to send

       ld   c,$fd        
       ld   d,$ff       

       ld   e,$bf        
       ld   b,d          ;FFFD = Address of the
       ld   a,$0e        ;command register  do AY
       out  (c),a        ;chose reg 14 do AY <=> porta I/O
                        

L08af  call L05d6         ; Check for BREAK

       in   a,(c)         ;Read  status of data register 
       and  $40           ; test DTR - %01000000   
       jr   nz,L08af     

       ld   hl,($5b5f)   ;HL = SYS_VAR BAUD
       ld   de,$0002     
       or   a            
       sbc  hl,de        
       ex   de,hl        ;DE=BAUD-2
       pop  af           
       cpl                ; Invert the bits in the character. (RS232 logic is inverted)
       scf                ;Carry is used to send START BIT
       ld   b,$0b         ;B= bits= 1 start + 8 data + 2 stop
       di    

;SEND BITS RS232            
L08c8  push bc           
       push af           
       ld   a,$fe        
       ld   h,d          
       ld   l,e          
L08ce  ld   bc,$bffd     ;AY data register
       jp   nc,L08da     ;jp nc,Send_one

;Send_zero   
       and  $f7          
       out  (c),a        
       jr   L08e0     

;Send_one   
L08da  or   $08          
       out  (c),a        
       jr   L08e0    ;??????

;Delay Loop BD-DEL     Wait 26 * BAUD cycles
L08e0  dec  hl         ; ( 6)  
       ld   a,h        ; ( 4)  
       or   l          ; ( 4)  
       jr   nz,L08e0   ; (12)
       nop               
       nop               
       nop               
       pop  af           
       pop  bc           
       or   a           ;clear carry      
       rra              ;C->7xxxxxx0->C 
       djnz L08c8       ;loops B bits 
       ei                
       ret               

; COPY routine (continuation)
L08f0  ld   hl,$5b72     ; 21 72 5b
       ld   (hl),$2b     ; 36 2b
L08f5  ld   hl,L0979     ; Point to data table
       call L095f        ; Send data to printer (?)
       call L0915        ; cd 15 09
       ld   hl,$0980     ; Point to data table
       call L095f        ; Send data to printer (?)
       ld   hl,$5b72     ; 21 72 5b
       xor  a            ; af
       cp   (hl)         ; be
       jr   z,L090e      ; 28 03
       dec  (hl)         ; 35
       jr   L08f5        ; 18 e7
L090e  ld   hl,L0982     ; 21 82 09
       call L095f        ; cd 5f 09
       ret               ; c9

L0915  ld   hl,$5b71     ; 21 71 5b
       ld   (hl),$ff     ; 36 ff
L091a  call L0926        ; cd 26 09
       ld   hl,$5b71     ; 21 71 5b
       xor  a            ; af
       cp   (hl)         ; be
       ret  z            ; c8
       dec  (hl)         ; 35
       jr   L091a        ; 18 f4

L0926  ld   de,$c000     ; 11 00 c0
       ld   bc,($5b71)   ; ed 4b 71 5b
       scf               ; 37
       rl   b            ; cb 10
       scf               ; 37
       rl   b            ; cb 10
       ld   a,c          ; 79
       cpl               ; 2f
       ld   c,a          ; 4f
       xor  a            ; af
       push af           ; f5
       push de           ; d5
       push bc           ; c5
L093a  call L096d        ; Test whether pixel (B,C) is set
       pop  bc           ; c1
       pop  de           ; d1
       ld   e,$00        ; 1e 00
       jr   z,L0944      ; 28 01
       ld   e,d          ; 5a
L0944  pop  af           ; f1
       or   e            ; b3
       push af           ; f5
       dec  b            ; 05
       srl  d            ; cb 3a
       srl  d            ; cb 3a
       push de           ; d5
       push bc           ; c5
       jr   nc,L093a     ; 30 ea
       pop  bc           ; c1
       pop  de           ; d1
       pop  af           ; f1
       ld   b,$03        ; 06 03
L0955  push bc           ; c5
       push af           ; f5
       call L08a3        ; cd a3 08
       pop  af           ; f1
       pop  bc           ; c1
       djnz L0955        ; 10 f7
       ret               ; c9

L095f  ld   b,(hl)       ; Get number of bytes
       inc  hl           ; 
L0961  ld   a,(hl)       ; Retrieve value
       push hl           ; 
       push bc           ; 
       call L08a3        ; Send to printer?
       pop  bc           ; c1
       pop  hl           ; e1
       inc  hl           ; 23
       djnz L0961        ; 10 f5
       ret               ; c9

; Test whether the pixel (B,C) is set or not
; If the pixel is reset, returns with A=0
; If the pixel is set, returns with A>0
;   (actually the value of the bit corresponding to the pixel within the byte)
L096d  rst  28h          ; Get address of (B,C) pixel into HL/A
       .word $22aa ; PIXEL-ADDR
       ld   b,a          ; B=pixel position within byte (0-7)
       inc  b            ;
       xor  a            ; 
       scf               ; Set bit to be rotated into A
L0974  rra               ; Rotate...
       djnz L0974        ; ... and repeat
       and  (hl)         ; Isolate this pixel from A
       ret               ;

; Some data used in COPY
L0979 .byte $06,$1b,$31,$1b,$4c,$00,$03
; Some more data
L0980 .byte $01,$0a
; A bit more data
L0982  .byte $02,$1b,$32

; ** THE PLAY SUBROUTINES **

; Reserve and intialise space for PLAY data. B = the number of strings
; in the PLAY command. There is a block of $003c bytes allocated (IY
; points to this) plus $0037 bytes for each string used
; (IX is used to point to the current channel).
;

; IY data block:
; (IY+00, IY+01) points to data for channel 0
; (IY+02, IY+03) points to data for channel 1
; ...
; (IY+$10) gets initialised to 0ffh and RLed for each string;
; i.e. it ends up with the least-significant 'n' bits reset
; if there are n strings provided to the PLAY command
; ...
; (IY+$27;$28) holds the tempo
; (IY+$29) effect value
; (IY+$2B...$37) (for 13d bytes) gets initialised to values from L0a31

; When referring to a
; channel, the following values are used:
; (ix+01) = MIDI channel
; (ix+02) = index of string within PLAY command
; (ix+03) = 12 * current octave number
; (ix+04) = current volume
; (ix+06,07) = current position in string
; (ix+08,09) = 1 after end position
; (ix+0b) = current bracket nesting level
; (ix+0e,0f...) = stored bracket return positions
; (ix+$10,$11...) = stored bracket return positions
; (ix+$12,$13...) = stored bracket return positions
; (ix+$14,$15...) = stored bracket return positions
; (ix+$16) = ??? initialised to FF
; (ix+$17) = ??? initialised to 00
; (ix+$18) = ??? initialised to 00
; ... ?
; (ix+$21) = counter of tied notes

L0985  di
       push bc
       ld   de,$0037     ; Calculate HL = $003C + $0037 * B
       ld   hl,$003c     ;
L098d  add  hl,de        ;
       djnz L098d        ;
       ld   c,l          ;
       ld   b,h          ; BC = space required
       rst  28h          ; 
       .word $0030       ; Make BC bytes of space in the workspace.
       di                ;
       push de           ; 
       pop  iy           ; IY = first new byte
       push hl           ; 
       pop  ix           ; IX = last new byte
       ld   (iy+$10),$ff ; Initial value meaning 'zero strings'

; Loop over each string to be played
L09a0  ld   bc,$ffc9     ; $-37
       add  ix,bc        ; IX points to start of space for last channel
       ld   (ix+$03),$3c ; Default octave is 4
       ld   (ix+$01),$ff ;
       ld   (ix+$04),$0f ; Default volume is 15
       ld   (ix+$05),$05 ;
       ld   (ix+$21),$00 ;
       ld   (ix+$0a),$00 ;
       ld   (ix+$0b),$00 ; Initial bracket nesting level
       ld   (ix+$16),$ff ;
       ld   (ix+$17),$00 ;
       ld   (ix+$18),$00 ;
       rst  28h          ; Get the details of the string from the stack
       .word STK_FETCH
       di                ;
       ld   (ix+$06),e   ; Store the start of the string as
       ld   (ix+$07),d   ; the current position
       ld   (ix+$0c),e   ;
       ld   (ix+$0d),d   ;
       ex   de,hl        ;
       add  hl,bc        ;
       ld   (ix+$08),l   ; Store the address of the character just
       ld   (ix+$09),h   ; after the string
       pop  bc           ; Restore B=current string index
       push bc           ; and save it again
       dec  b            ;
       ld   c,b          ;
       ld   b,$00        ; 
       sla  c            ; BC=string index*2
       push iy           ;
       pop  hl           ; 
       add  hl,bc        ; HL 
       push ix           ;
       pop  bc           ; BC=pointer to data for this channel
       ld   (hl),c       ; Store point
       inc  hl           ;
       ld   (hl),b       ;
       or   a            ;
       rl   (iy+$10)     ; Rotate one zero-bit into the least significant bit of
                         ; (iy+$10). Once this loop is over, this byte is one
                         ; zero bit for each string parameter to the PLAY command.
       pop  bc           ; Restore B=current string index
       dec  b            ; Decrement string index
       push bc           ; and save it for future use
       ld   (ix+$02),b   ; Set index of string
       jr   nz,L09a0     ; Jump back while more channels
       pop  bc           ; 
L0a05  ld   (iy+$27),$1a ; Initialise tempo
       ld   (iy+$28),$0b ;
       push iy           ;
       pop  hl           ; 
       ld   bc,$002b     ; 
       add  hl,bc        ; 
       ex   de,hl        ; DE=main data area + $2b bytes
       ld   hl,L0a31     ; data area in ROM
       ld   bc,$000d     ;
       ldir              ; Copy 13 bytes
       ld   d,$07        ; Set sound chip register 07 to $f8
       ld   e,$f8        ; ("Mixer")
       call L0e7c        ; 
       ld   d,$0b        ; Set register $0b to $ff
       ld   e,$ff        ; ("Envelope fine duration")
       call L0e7c        ;
       inc  d            ; Set register $0c to $ff
       call L0e7c        ; ("Envelope coarse duration")
       jr   L0a7d        ; Jump forwards

; 13 bytes of data (initial PLAY data)
L0a31 .byte $ef,$a4,$01,$05,$34,$df,$75,$f4
      .byte $38,$75,$05,$38,$c9

; Test for BREAK (returns with carry clear if BREAK is pressed)
L0a3e  ld   a,$7f        ;
       in   a,($fe)      ;
       rra               ;
       ret  c            ; Return with C set if SPACE not pressed
       ld   a,$fe        ;
       in   a,($fe)      ; 
       rra               ;
       ret               ; Return with C set if CAPS not pressed

L0a4a  ld   bc,$0011     ; 01 11 00
       jr   L0a52        ; 18 03

; Set up some PLAY data ??
L0a4f  ld   bc,$0000     ; 01 00 00
L0a52  push iy           ; fd e5
       pop  hl           ; e1
       add  hl,bc        ; 09
       ld   (iy+$23),l   ; fd 75 23
       ld   (iy+$24),h   ; fd 74 24
       ld   a,(iy+$10)   ; fd 7e 10
       ld   (iy+$22),a   ; fd 77 22
       ld   (iy+$21),$01 ; fd 36 21 01
       ret               ; c9

; Get IX (channel data pointer) from the bytes at (HL, HL+1)
L0a67  ld   e,(hl)       ;
       inc  hl           ;
       ld   d,(hl)       ;
       push de           ;
       pop  ix           ;
       ret               ;

; Increase the pointer at (iy+$23;iy+$24) by two
; (??? What is this pointer?)
L0a6e  ld   l,(iy+$23)   ; fd 6e 23
       ld   h,(iy+$24)   ; fd 66 24
       inc  hl           ; 23
       inc  hl           ; 23
       ld   (iy+$23),l   ; fd 75 23
       ld   (iy+$24),h   ; fd 74 24
       ret               ; c9

; Continuation of the PLAY code
L0a7d  call L0a4f        ; Set up some data (leaves with HL=IY)
L0a80  rr   (iy+$22)     ; fd cb 22 1e
       jr   c,L0a8c      ; 38 06
       call L0a67        ; Get IX from (HL;HL+1)
       call L0b5c        ; Process the PLAY string for this channel
L0a8c  sla  (iy+$21)     ; fd cb 21 26
       jr   c,L0a97      ; 38 05
       call L0a6e        ; cd 6e 0a
       jr   L0a80        ; 18 e9
L0a97  call L0f91        ; cd 91 0f
       push de           ; d5
       call L0f42        ; cd 42 0f
       pop  de           ; d1
L0a9f  ld   a,(iy+$10)   ; 
       cp   $ff          ; $ff means nothing to PLAY
       jr   nz,L0aab     ; 20 05
       call L0e93        ; cd 93 0e
       ei                ; fb
       ret               ; c9
L0aab  dec  de           ; 1b
       call L0f76        ; cd 76 0f
       call L0fc1        ; cd c1 0f
       call L0f91        ; cd 91 0f
       jr   L0a9f        ; 18 e8

; Recognised characters in PLAY commands
L0ab7  .text "HZYXWUVMT)(NO!"

; Get the current character from the PLAY string and increment the pointer
L0ac5  call L0ee3        ; Get the current character
       ret  c            ; Return if error condition
       inc  (ix+$06)     ; Increment the low byte of the pointer
       ret  nz           ;
       inc  (ix+$07)     ; ... and the high byte if necessary
       ret               ;

; Return A=number of semitones above C for the next note in the string,
; or A=$80 for a rest
L0ad1  push hl           ; Save HL
       ld   c,$00        ; Default is for a 'natural' note; ie no adjustment
L0ad4  call L0ac5        ; Get the current character from the PLAY string
       jr   c,L0ae1      ; Jump if error condition
       cp   $26          ; "&" (rest)
       jr   nz,L0aec     ; Jump ahead if not
       ld   a,$80        ;
L0adf  pop  hl           ; Restore HL
       ret               ; and return
L0ae1  ld   a,(iy+$21)   ; fd 7e 21
       or   (iy+$10)     ; fd b6 10
       ld   (iy+$10),a   ; fd 77 10
       jr   L0adf        ; 18 f3
L0aec  cp   $23          ; "#" (sharpen)
       jr   nz,L0af3     ;
       inc  c            ; +1 semitone
       jr   L0ad4        ;
L0af3  cp   $24          ; "$" (flatten)
       jr   nz,L0afa     ;
       dec  c            ; -1 semitone
       jr   L0ad4        ;
L0afa  bit  5,a          ;
       jr   nz,L0b04     ; Jump ahead if lower case
       push af           ; 
       ld   a,$0c        ; Increase A by an octave
       add  a,c          ; by adding 12 semitones to C
       ld   c,a          ;
       pop  af           ;
L0b04  and  $df          ; Make sure upper-case
       sub  $41          ; Convert "A" -> 0, "G"->6
       jp   c,L0f22      ; jump if error
       cp   $07          ; 
       jp   nc,L0f22     ; jump if error
       push bc           ; 
       ld   b,$00        ; 
       ld   c,a          ; BC represents the note, 0="a", 6="g"
       ld   hl,L0df9     ; Lookup table
       add  hl,bc        ; HL=pointer to look up table
       ld   a,(hl)       ; Get A=number of semitones above the note C
       pop  bc           ; Restore BC
       add  a,c          ; Adjust for sharp/flat
       pop  hl           ; Restore HL
       ret               ; 

; Get a numeric value from a PLAY string into BC
L0b1d  push hl           ;
       push de           ;
       ld   l,(ix+$06)   ; Get the pointer into
       ld   h,(ix+$07)   ; the PLAY string
       ld   de,$0000     ; Initialise result to 0
L0b28  ld   a,(hl)       ;
       cp   $30          ; Is character numeric?
       jr   c,L0b45      ; 
       cp   $3a          ;
       jr   nc,L0b45     ;
       inc  hl           ; Got a numeric digit
       push hl           ;
       call L0b50        ; Multiply result so far by 10
       sub  $30          ; Convert ASCII digit to numeric value
       ld   h,$00        ; 
       ld   l,a          ; 
       add  hl,de        ; Increase result so far by the value
       jr   c,L0b42      ; Error on overflow
       ex   de,hl        ; Result into DE
       pop  hl           ; Retrieve pointer into string
       jr   L0b28        ; Loop back

L0b42  jp   L0f1a        ; Jump to generate error "l number too big"

L0b45  ld   (ix+$06),l   ; Update pointer into
       ld   (ix+$07),h   ; PLAY string
       push de           ; Result into BC
       pop  bc           ; 
       pop  de           ; Restore DE, HL
       pop  hl           ;
       ret               ;

; Multiply DE by 10
L0b50  ld   hl,$0000     ; Do HL=10*DE
       ld   b,$0a        ;
L0b55  add  hl,de        ;
       jr   c,L0b42      ; Error if overflow
       djnz L0b55        ;
       ex   de,hl        ; Result into DE
       ret               ; 

L0b5c  call L0a3e        ; Test for BREAK
       jr   c,L0b69      ; Jump ahead if not pressed
       call L0e93        ; Reset sound registers
       ei                ; 
       call L05ac        ; Generate error
       .byte $14         ; "L Break into program"
L0b69  call L0ac5        ; get next character from PLAY string
       jp   c,L0da2      ; jump if error
       call L0df0        ; Test for a recognised PLAY control character
       ld   b,$00        ;
       sla  c            ; Double the return value
       ld   hl,L0dca     ; Start of lookup table
       add  hl,bc        ; HL points to address for this commmand code
       ld   e,(hl)       ; 
       inc  hl           ;
       ld   d,(hl)       ;
       ex   de,hl        ; HL holds address for this command code
       call L0b84        ; Call this code
       jr   L0b5c        ; Loop back
       ret               ; Finish
L0b84  jp   (hl)         ;

; "!" (comment) command for PLAY
L0b85  call L0ac5        ; get next character from PLAY string
       jp   c,L0da1      ; error condition
       cp   $21          ; "!", end-of-comment character
       ret  z            ;
       jr   L0b85        ;

; "O" (octave) command for PLAY
L0b90  call L0b1d        ; Get a numeric value from the string
       ld   a,c          ; There is no check on the high value
       cp   $09          ; of the value, so O256 works
       jp   nc,L0f12     ; Error for 9 and above
       sla  a            ; Multiply A by 12
       sla  a            ;
       ld   b,a          ;
       sla  a            ;
       add  a,b          ;
       ld   (ix+$03),a   ; ... and store it
       ret               ;

; "N" (separator) command for PLAY
L0ba5  ret               ; Nothing to do

; "(" command for PLAY
L0ba6  ld   a,(ix+$0b)   ; Current level of bracket nesting
       inc  a            ;
       cp   $05          ;
       jp   z,L0f2a      ; "Too many brackets" error
       ld   (ix+$0b),a   ; Store new nesting level
       ld   de,$000c     ;
       call L0c27        ; Fetch HL = IX + L000C + 2*A
       ld   a,(ix+$06)   ; Store current position
       ld   (hl),a       ;
       inc  hl           ;
       ld   a,(ix+$07)   ;
       ld   (hl),a       ;
       ret               ;

; ")" command for PLAY
L0bc2  ld   a,(ix+$16)   ; dd 7e 16
       ld   de,$0017     ; 11 17 00
       or   a            ; b7
       jp   m,L0bf0      ; fa f0 0b
       call L0c27        ; cd 27 0c
       ld   a,(ix+$06)   ; dd 7e 06
       cp   (hl)         ; be
       jr   nz,L0bf0     ; 20 1b
       inc  hl           ; 23
       ld   a,(ix+$07)   ; dd 7e 07
       cp   (hl)         ; be
       jr   nz,L0bf0     ; 20 14
       dec  (ix+$16)     ; dd 35 16
       ld   a,(ix+$16)   ; dd 7e 16
       or   a            ; b7
       ret  p            ; f0
       bit  0,(ix+$0a)   ; dd cb 0a 46
       ret  z            ; c8
       ld   (ix+$16),$00 ; dd 36 16 00
       xor  a            ; af
       jr   L0c0b        ; 18 1b
L0bf0  ld   a,(ix+$16)   ; dd 7e 16
       inc  a            ; 3c
       cp   $05          ; fe 05
       jp   z,L0f2a      ; ca 2a 0f
       ld   (ix+$16),a   ; dd 77 16
       call L0c27        ; cd 27 0c
       ld   a,(ix+$06)   ; dd 7e 06
       ld   (hl),a       ; 77
       inc  hl           ; 23
       ld   a,(ix+$07)   ; dd 7e 07
       ld   (hl),a       ; 77
       ld   a,(ix+$0b)   ; dd 7e 0b
L0c0b  ld   de,$000c     ; 11 0c 00
       call L0c27        ; cd 27 0c
       ld   a,(hl)       ; 7e
       ld   (ix+$06),a   ; dd 77 06
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       ld   (ix+$07),a   ; dd 77 07
       dec  (ix+$0b)     ; dd 35 0b
       ret  p            ; f0
       ld   (ix+$0b),$00 ; dd 36 0b 00
       set  0,(ix+$0a)   ; dd cb 0a c6
       ret               ; c9

; Get HL = IX + DE + 2*A
L0c27  push ix           ; dd e5
       pop  hl           ; e1
       add  hl,de        ; 19
       ld   b,$00        ; 06 00
       ld   c,a          ; 4f
       sla  c            ; cb 21
       add  hl,bc        ; 09
       ret               ; c9

; "T" (tempo) command for PLAY
L0c32  call L0b1d        ; Get numeric value into BC
       ld   a,b          ;
       or   a            ;
       jp   nz,L0f12     ; Error if 256 or above
       ld   a,c          ; 
       cp   $3c          ;
       jp   c,L0f12      ; Error if 59 or below
       cp   $f1          ; 
       jp   nc,L0f12     ; Jump if 241 or above
       ld   a,(ix+$02)   ; Index of current string
       or   a            ; 
       ret  nz           ; T commands have to be specified in string 0
       ld   b,$00        ; [B is already zero]
       push bc           ;
       pop  hl           ;
       add  hl,hl        ;
       add  hl,hl        ; HL = tempo * 4
       push hl           ;
       pop  bc           ; BC = tempo * 4
       push iy           ;
       rst  28h          ;
       .word $2d2b       ; STACK-BC
       di                ;
       pop  iy           ; 
       push iy           ;
       push iy           ;
       pop  hl           ; HL = IY
       ld   bc,$002b     ;
       add  hl,bc        ; HL = IY + $002B
       ld   iy,$5c3a     ; Restore IY
       push hl           ; 
       ld   hl,L0c76     ;
       ld   ($5b5a),hl   ; RETADDR
       ld   hl,$5b14     ;
       ex   (sp),hl      ;
       push hl           ;
       jp   $5b00        ; Eh?
L0c76  di                ;
       rst  28h          ;
       .word $2da2       ; FP-TO-BC
       di                ;
       pop  iy           ;
       ld   (iy+$27),c   ; Store tempo
L0c80  ld   (iy+$28),b   ;
       ret               ;

; "M" (channel) command for play
L0c84  call L0b1d        ; Get numeric value into BC
       ld   a,c          ; A=(low byte of) value
       cp   $40          ;
       jp   nc,L0f12     ; Error n out of range
       cpl               ;
       ld   e,a          ;
       ld   d,$07        ; Register 7
       call L0e7c        ; Value E
       ret               ;

; V command for PLAY
L0c95  call L0b1d        ; Get numeric value
       ld   a,c          ;
       cp   $10          ; Test against 16
       jp   nc,L0f12     ; Error for 16 and above
       ld   (ix+$04),a   ; Store volume
       ld   e,(ix+$02)   ; Get index of string
       ld   a,$08        ;
       add  a,e          ; A = number of string + 8
       ld   d,a          ; D = register to write to
       ld   e,c          ; E = value to write = volume
       call L0e7c        ; set register
       ret               ;

; "U" (use volume effect) command for PLAY
L0cad  ld   e,(ix+$02)   ; Get index of string
       ld   a,$08        ;
       add  a,e          ;
       ld   d,a          ; Register (8+index)
       ld   e,$1f        ;
       ld   (ix+$04),e   ; Store volume of 15
       ret               ;

; "W" (volume effect specifier) command for PLAY
L0cba  call L0b1d        ; Get numeric value into BC
       ld   a,c          ; (Discard high byte)
       cp   $08          ;
       jp   nc,L0f12     ; Error n out of range
       ld   b,$00        ; 
       ld   hl,$0de8     ; Lookup table
       add  hl,bc        ; HL points to the value in the table
       ld   a,(hl)       ;
       ld   (iy+$29),a   ; Store new effect value
       ret               ;

; "X" (duration of volume effect) command for PLAY
L0cce  call L0b1d        ; Get numeric value into BC
       ld   d,$0b        ;
       ld   e,c          ;
       call L0e7c        ; Set register 0B to the low byte of the value
       inc  d            ;
       ld   e,b          ;
       call L0e7c        ; Set register 0C to the high byte of the value
       ret               ;

; "Y" (MIDI channel) command for PLAY
L0cdd  call L0b1d        ; Get numeric parameter
       ld   a,c          ;
       dec  a            ; (No check on high byte)
       jp   m,L0f12      ; Error
       cp   $10          ; 
       jp   nc,L0f12     ; Error
       ld   (ix+$01),a   ; Store MIDI channel
       ret               ;

; "Z" (MIDI programming code) command for PLAY
L0cee  call L0b1d        ; Get numeric value
       ld   a,c          ; A=(low byte of) value
       call L11a3        ;
       ret               ;

; "H" (stop) command for PLAY
L0cf6  ld   (iy+$10),$ff ; Indicate 'nothing left to play'
       ret               ;

; any other command for PLAY (a..g, A..G, 1..12, &, _)
L0cfb  call L0e19        ; Test for digit
       jp   c,L0d81      ; Jump if not digit
       call L0dac        ; Get HL = IX + L0022
       call L0db4        ; Store HL
       xor  a            ;
       ld   (ix+$21),a   ;
       call L0ec8        ; Get the previous character
       call L0b1d        ; Get numeric value
       ld   a,c          ;
       or   a            ;
       jp   z,L0f12      ; Error if zero
       cp   $0d          ; 
       jp   nc,L0f12     ; Error if 13 or above
       cp   $0a          ; 
       jr   c,L0d32      ; Jump if 1..9
       call L0e00        ; Otherwise it's a triplet semi-quaver (10),
                         ; triplet quaver (11) or triplet crotchet (12).
                         ; Look up length (into DE) from table
       call L0d74        ; Increment the counter of tied notes
       ld   (hl),e       ; Store the duration value
       inc  hl           ; into (IX+$22, IX+$23)
       ld   (hl),d       ; 
L0d28  call L0d74        ; Increment the counter of tied notes
       inc  hl           ; 
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       inc  hl           ; 23
       jr   L0d38        ; Jump ahead

L0d32  ld   (ix+$05),c   ; C=1..9 for the duration of note
       call L0e00        ; DE=duration of this note (from lookup table)

L0d38  call L0d74        ; Increment counter of tied notes
L0d3b  call L0ee3        ; Get character from this PLAY string
       cp   $5f          ; Is it "_" (indicating a tied note)?
       jr   nz,L0d6e     ; Jump ahead if not
       call L0ac5        ; Get next character from the PLAY string
       call L0b1d        ; then get numeric value
       ld   a,c          ; Fetch the value into A
       cp   $0a          ; Compare with 10d
       jr   c,L0d5f      ; Jump ahead for 1..9 (semiquaver .. semibreve)
L0d4d  push hl           ; e5
       push de           ; d5
       call L0e00        ; cd 00 0e
       pop  hl           ; e1
       add  hl,de        ; 19
       ld   c,e          ; 4b
       ld   b,d          ; 42
       ex   de,hl        ; eb
       pop  hl           ; e1
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       ld   e,c          ; 59
       ld   d,b          ; 50
       jr   L0d28        ; 18 c9
L0d5f  ld   (ix+$05),c   ; dd 71 05
       push hl           ; e5
       push de           ; d5
       call L0e00        ; cd 00 0e
       pop  hl           ; e1
       add  hl,de        ; 19
       ex   de,hl        ; eb
       pop  hl           ; e1
L0d6b  jp   L0d3b        ; c3 3b 0d
L0d6e  ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       jp   L0d9c        ; c3 9c 0d

L0d74  ld   a,(ix+$21)   ; Increment counter of tied notes
       inc  a            ;
       cp   $0b          ;
       jp   z,L0f3a      ; Error "o too many tied notes" 
       ld   (ix+$21),a   ; Store incremented counter
       ret               ;

L0d81  call L0ec8        ; Get the previous character from the string
       ld   (ix+$21),$01 ; 
       call L0dac        ; Get HL=IX+L0022
       call L0db4        ; Store HL
       ld   c,(ix+$05)   ; C = ?
       push hl           ; [Not necessary]
       call L0e00        ; Look up the C'th element of a table
       pop  hl           ; [Not necessary]
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       jp   L0d9c        ; c3 9c 0d

L0d9c  pop  hl           ;
       inc  hl           ; 
       inc  hl           ;
       push hl           ;
       ret               ;

; error
L0da1  pop  hl           ; e1
L0da2  ld   a,(iy+$21)   ; fd 7e 21
       or   (iy+$10)     ; fd b6 10
       ld   (iy+$10),a   ; fd 77 10
       ret               ; c9

; GET HL = IX+$0022 (point to information for next PLAY parameter)
L0dac  push ix           ; dd e5
       pop  hl           ; e1
       ld   bc,$0022     ; 01 22 00
       add  hl,bc        ; 09
       ret               ; c9

; Store HL in (IY + $0011 + 2 * (IX+$02))
L0db4  push hl           ; e5
       push iy           ; fd e5
       pop  hl           ; e1
       ld   bc,$0011     ; 01 11 00
       add  hl,bc        ; 09
       ld   b,$00        ; 06 00
       ld   c,(ix+$02)   ; = index of string
       sla  c            ; cb 21
       add  hl,bc        ; HL = IY + $0011 + 2 * (IX+$02)
       pop  de           ; d1
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       ex   de,hl        ; eb
       ret               ; c9

; Addresses for PLAY commands
L0dca .word L0cfb, L0b85, L0b90, L0ba5   ;   ! O N
      .word L0ba6, L0bc2, L0c32, L0c84   ; ( ) T M
      .word L0c95, L0cad, L0cba, L0cce   ; V U W X
      .word L0cdd, L0cee, L0cf6          ; Y Z H

; Lookup table when using the "W" command in PLAY
L0de8  .byte $00,$04,$0b,$0d
       .byte $08,$0c,$0e,$0a

; Test for command characters in PLAY
L0df0  ld   bc,$000f     ; Number of characters in lookup table
       ld   hl,L0ab7     ; Start of lookup table
       cpir              ; Test for character
       ret               ; 

; Data table for PLAY
; Each entry is equivalent to a note of the scale, A...G
; and is the number of semitones above (the note) C
L0df9  .byte $09, $0b, $00, $02, $04, $05, $07

; Get the C'th entry in the table from 0E0C into E
L0e00  push hl           ;
       ld   b,$00        ;
       ld   hl,$0e0c     ;
       add  hl,bc        ;
       ld   d,$00        ;
       ld   e,(hl)       ;
       pop  hl           ;
       ret               ;

L0e0c .byte $80,$06,$09,$0c,$12,$18,$24,$30
      .byte $48,$60,$04,$08,$10

; Test for a digit; returns with C flag false for a digit
L0e19  cp   $30          ; '0' or less?
       ret  c
       cp   $3a          ; More than '9'?
       ccf               ; 
       ret               ;

; Play the note A at the current octave
; (A = 0 corresponds to C; A=2 corresponds to D etc.)
L0e20  ld   c,a          ;
       ld   a,(ix+$03)   ; Octave number * 12
       add  a,c          ;
       cp   $80          ; fe 80
       jp   nc,L0f32     ; d2 32 0f
       ld   c,a          ; C = note value
       ld   a,(ix+$02)   ; dd 7e 02
       or   a            ; b7
       jr   nz,L0e3f     ; 20 0e
       ld   a,c          ; 79
       cpl               ; 2f
       and  $7f          ; e6 7f
       srl  a            ; cb 3f
       srl  a            ; cb 3f
       ld   d,$06        ; 16 06
       ld   e,a          ; 5f
       call L0e7c        ; 
L0e3f  ld   (ix+$00),c   ; dd 71 00
       ld   a,(ix+$02)   ; dd 7e 02
       cp   $03          ; fe 03
       ret  nc           ; d0
       ld   hl,$1096     ; Start of lookup table
       ld   b,$00        ; 
       ld   a,c          ; 
       sub  $15        ; 
       jr   nc,L0e57     ; 
       ld   de,$0fbf     ; If 0..$14, use values 0f,bf [the lowest note]
       jr   L0e5e        ;
L0e57  ld   c,a          ; If $15+...
       sla  c            ; 
       add  hl,bc        ; Lookup DE from 1096 + 2 * C
       ld   e,(hl)       ;
       inc  hl           ;
       ld   d,(hl)       ;
L0e5e  ex   de,hl        ; HL = value looked up
       ld   d,(ix+$02)   ; index of string in PLAY command
       sla  d            ; D = 2 * index
       ld   e,l          ; E = 1st byte looked up
       call L0e7c        ; Set register D to value E
       inc  d            ; Next register
       ld   e,h          ; 2nd byte looked up
       call L0e7c        ; Set register D to value E
       bit  4,(ix+$04)   ; dd cb 04 66
       ret  z            ;
       ld   d,$0d        ; Register 13
       ld   a,(iy+$29)   ; Effect value
       ld   e,a          ; 
       call L0e7c        ; Set register
       ret               ;

; Set sound chip register D to value E
L0e7c  push bc           ; c5
       ld   bc,$fffd     ; 01 fd ff
       out  (c),d        ; ed 51
       ld   bc,$bffd     ; 01 fd bf
       out  (c),e        ; ed 59
       pop  bc           ; c1
       ret               ; c9

L0e89  push bc           ; c5
       ld   bc,$fffd     ; 01 fd ff
       out  (c),a        ; ed 79
       in   a,(c)        ; ed 78
       pop  bc           ; c1
       ret               ; c9

; Set sound chip register 7 to FF and 8,9,10 to 0
; (resets the registers to their default state)
L0e93  ld   d,$07        ; 16 07
       ld   e,$ff        ; 1e ff
       call L0e7c        ; cd 7c 0e
       ld   d,$08        ; 16 08
       ld   e,$00        ; 1e 00
       call L0e7c        ; cd 7c 0e
       inc  d            ; 14
       call L0e7c        ; cd 7c 0e
       inc  d            ; 14
       call L0e7c        ; cd 7c 0e
       call L0a4f        ; cd 4f 0a
L0eac  rr   (iy+$22)     ; fd cb 22 1e
       jr   c,L0eb8      ; 38 06
       call L0a67        ; cd 67 0a
       call L118d        ; cd 8d 11
L0eb8  sla  (iy+$21)     ; fd cb 21 26
       jr   c,L0ec3      ; 38 05
       call L0a6e        ; cd 6e 0a
       jr   L0eac        ; 18 e9
L0ec3  ld   iy,$5c3a     ; fd 21 3a 5c
       ret               ; c9

; Get the previous character from the PLAY string (excluding SPACE/ENTER)
L0ec8  push hl           ;
       push de           ;
       ld   l,(ix+$06)   ; HL points to PLAY string
       ld   h,(ix+$07)   ; 
L0ed0  dec  hl           ; Point to previous character
       ld   a,(hl)       ; 
       cp   $20          ;
       jr   z,L0ed0      ; Back again if SPACE...
       cp   $0d          ; 
       jr   z,L0ed0      ; ... or ENTER
       ld   (ix+$06),l   ; Store decremented pointer
       ld   (ix+$07),h   ;
       pop  de           ;
       pop  hl           ;
       ret               ;

; Get the current character from the PLAY string (skipping SPACE and ENTER)
L0ee3  push hl           ;
       push de           ;
       push bc           ;
       ld   l,(ix+$06)   ; Get pointer to PLAY string
       ld   h,(ix+$07)   ;
L0eec  ld   a,h          ;
       cp   (ix+$09)     ; Reached end-of-string high byte?
       jr   nz,L0efb     ; 
       ld   a,l          ;
       cp   (ix+$08)     ; Reached end-of-string low byte?
       jr   nz,L0efb     ; 
       scf               ; Indicate error
       jr   L0f05        ; 
L0efb  ld   a,(hl)       ; Get character
       cp   $20          ;
       jr   z,L0f09      ; Jump ahead to get next character for SPACE...
       cp   $0d          ;
       jr   z,L0f09      ; ... or for an ENTER character
       or   a            ; Otherwise with Z reset
                         ;  (apart from CHR$ 0)
L0f05  pop  bc           ;
       pop  de           ;
       pop  hl           ;
       ret               ;

L0f09  inc  hl           ; 23
       ld   (ix+$06),l   ; dd 75 06
       ld   (ix+$07),h   ; dd 74 07
       jr   L0eec        ; 18 da

L0f12  call L0e93        ; Reset sound registers
       ei                ; 
       call L05ac        ; Generate error
       .byte $29         ; "n Out of range"

L0f1a  call L0e93        ; Reset sound chip registers
       ei                ;
       call L05ac        ; Generate error
       .byte $27         ; "l Number too big"

L0f22  call L0e93        ; Reset sound registers
       ei                ;
       call L05ac        ; Generate error
       .byte $26         ; "k Invalid note name"

L0f2a  call L0e93        ; Reset sound registers
       ei                ;
       call L05ac        ; Generate error
       .byte $1f         ; "d Too many brackets" 

L0f32  call L0e93        ; Reset sound registers
       ei                ;
       call L05ac        ; Generate error
       .byte $28         ; "m Note out of range"

L0f3a  call L0e93        ; Reset sound registers
       ei                ;
       call L05ac        ; Generate error
       .byte $2a         ; "o Too many tied notes"

L0f42  call L0a4f        ; ??
L0f45  rr   (iy+$22)     ; fd cb 22 1e
       jr   c,L0f6c      ; 38 21
       call L0a67        ; cd 67 0a
       call L0ad1        ; Get the next note in the string
       cp   $80          ; Is it a rest?
       jr   z,L0f6c      ; Jump ahead if so
       call L0e20        ; Otherwise play the note
       ld   a,(ix+$02)   ; Get string index (channel)
       cp   $03          ; Only set volume for channels 0-2
       jr   nc,L0f69     ;
       ld   d,$08        ; Find register number
       add  a,d          ;
       ld   d,a          ; D = register = 8 + string index
       ld   e,(ix+$04)   ; Current volume
       call L0e7c        ; Output volume control
L0f69  call L116e        ; cd 6e 11
L0f6c  sla  (iy+$21)     ; fd cb 21 26
       ret  c            ; d8
       call L0a6e        ; cd 6e 0a
       jr   L0f45        ; 18 cf

; Wait (during the PLAY command). The duration of the \
; pause is based on the current tempo and the \
; value in DE.
L0f76  push hl           ; Save HL
       ld   l,(iy+$27)   ; Get tempo
       ld   h,(iy+$28)   ; 
       ld   bc,$0064     ; BC=100d
       or   a            ;
       sbc  hl,bc        ; HL=tempo-100d
       push hl           ;
       pop  bc           ; BC=tempo-100d
       pop  hl           ; Restore HL
L0f86  dec  bc           ; Wait for 'tempo-100' loops
       ld   a,b          ;
       or   c            ;
       jr   nz,L0f86     ;
       dec  de           ; Repeat 'DE' times
       ld   a,d          ;
       or   e            ;
       jr   nz,L0f76     ;
       ret               ;

L0f91  ld   de,$ffff     ; 11 ff ff
       call L0a4a        ; cd 4a 0a
L0f97  rr   (iy+$22)     ; fd cb 22 1e
       jr   c,L0faf      ; 38 12
       push de           ; d5
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       ex   de,hl        ; eb
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       push de           ; d5
       pop  hl           ; e1
       pop  bc           ; c1
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       jr   c,L0faf      ; 38 02
       push bc           ; c5
       pop  de           ; d1
L0faf  sla  (iy+$21)     ; fd cb 21 26
       jr   c,L0fba      ; 38 05
       call L0a6e        ; cd 6e 0a
       jr   L0f97        ; 18 dd
L0fba  ld   (iy+$25),e   ; fd 73 25
       ld   (iy+$26),d   ; fd 72 26
       ret               ; c9

L0fc1  xor  a            ; af
       ld   (iy+$2a),a   ; fd 77 2a
       call L0a4f        ; cd 4f 0a
L0fc8  rr   (iy+$22)     ; fd cb 22 1e
       jp   c,L105a      ; da 5a 10
       call L0a67        ; cd 67 0a
       push iy           ; fd e5
       pop  hl           ; e1
       ld   bc,$0011     ; 01 11 00
       add  hl,bc        ; 09
       ld   b,$00        ; 06 00
       ld   c,(ix+$02)   ; dd 4e 02
       sla  c            ; cb 21
       add  hl,bc        ; 09
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       ex   de,hl        ; eb
       push hl           ; e5
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       ex   de,hl        ; eb
       ld   e,(iy+$25)   ; fd 5e 25
       ld   d,(iy+$26)   ; fd 56 26
       or   a            ; b7
       sbc  hl,de        ; ed 52
       ex   de,hl        ; eb
       pop  hl           ; e1
       jr   z,L0ffc      ; 28 05
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       jr   L105a        ; 18 5e
L0ffc  ld   a,(ix+$02)   ; dd 7e 02
       cp   $03          ; fe 03
       jr   nc,L100c     ; 30 09
       ld   d,$08        ; 16 08
       add  a,d          ; 82
       ld   d,a          ; 57
       ld   e,$00        ; 1e 00
       call L0e7c        ; cd 7c 0e
L100c  call L118d        ; cd 8d 11
       push ix           ; dd e5
       pop  hl           ; e1
       ld   bc,$0021     ; 01 21 00
       add  hl,bc        ; 09
       dec  (hl)         ; 35
       jr   nz,L1026     ; 20 0d
       call L0b5c        ; cd 5c 0b
       ld   a,(iy+$21)   ; fd 7e 21
       and  (iy+$10)     ; fd a6 10
       jr   nz,L105a     ; 20 36
       jr   L103d        ; 18 17
L1026  push iy           ; fd e5
       pop  hl           ; e1
       ld   bc,$0011     ; 01 11 00
       add  hl,bc        ; 09
       ld   b,$00        ; 06 00
       ld   c,(ix+$02)   ; dd 4e 02
       sla  c            ; cb 21
       add  hl,bc        ; 09
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       inc  de           ; 13
       inc  de           ; 13
       ld   (hl),d       ; 72
       dec  hl           ; 2b
       ld   (hl),e       ; 73
L103d  call L0ad1        ; cd d1 0a
       ld   c,a          ; 4f
       ld   a,(iy+$21)   ; fd 7e 21
       and  (iy+$10)     ; fd a6 10
       jr   nz,L105a     ; 20 11
       ld   a,c          ; 79
       cp   $80          ; fe 80
       jr   z,L105a      ; 28 0c
       call L0e20        ; cd 20 0e
       ld   a,(iy+$21)   ; fd 7e 21
       or   (iy+$2a)     ; fd b6 2a
       ld   (iy+$2a),a   ; fd 77 2a
L105a  sla  (iy+$21)     ; fd cb 21 26
       jr   c,L1066      ; 38 06
       call L0a6e        ; cd 6e 0a
       jp   L0fc8        ; c3 c8 0f
L1066  ld   de,$0001     ; 11 01 00
       call L0f76        ; cd 76 0f
       call L0a4f        ; cd 4f 0a
L106f  rr   (iy+$2a)     ; fd cb 2a 1e
       jr   nc,L108c     ; 30 17
       call L0a67        ; cd 67 0a
       ld   a,(ix+$02)   ; String index (channel)
       cp   $03          ; Only set volume
       jr   nc,L1089     ;  for channels 0-2
L107f  ld   d,$08        ; Get register number
       add  a,d          ;  = 8 + channel
       ld   d,a          ; 
       ld   e,(ix+$04)   ; Current volume
       call L0e7c        ; Set register
L1089  call L116e        ; cd 6e 11
L108c  sla  (iy+$21)     ; fd cb 21 26
       ret  c            ; d8
       call L0a6e        ; cd 6e 0a
       jr   L106f        ; 18 d9

; Lookup table for PLAY; each pair of bytes gives the values of the
; registers for a given note
L1096 .byte $bf,$0f,$dc,$0e,$07,$0e,$3d,$0d,$7f,$0c,$cc,$0b,$22,$0b,$82,$0a
      .byte $eb,$09,$5d,$09,$d6,$08,$57,$08,$df,$07,$6e,$07,$03,$07,$9f,$06
      .byte $40,$06,$e6,$05,$91,$05,$41,$05,$f6,$04,$ae,$04,$6b,$04,$2c,$04
      .byte $f0,$03,$b7,$03,$82,$03,$4f,$03,$20,$03,$f3,$02,$c8,$02,$a1,$02
      .byte $7b,$02,$57,$02,$36,$02,$16,$02,$f8,$01,$dc,$01,$c1,$01,$a8,$01
      .byte $90,$01,$79,$01,$64,$01,$50,$01,$3d,$01,$2c,$01,$1b,$01,$0b,$01
      .byte $fc,$00,$ee,$00,$e0,$00,$d4,$00,$c8,$00,$bd,$00,$b2,$00,$a8,$00
      .byte $9f,$00,$96,$00,$8d,$00,$85,$00,$7e,$00,$77,$00,$70,$00,$6a,$00
      .byte $64,$00,$5e,$00,$59,$00,$54,$00,$4f,$00,$4b,$00,$47,$00,$43,$00
      .byte $3f,$00,$3b,$00,$38,$00,$35,$00,$32,$00,$2f,$00,$2d,$00,$2a,$00
      .byte $28,$00,$25,$00,$23,$00,$21,$00,$1f,$00,$1e,$00,$1c,$00,$1a,$00
      .byte $19,$00,$18,$00,$16,$00,$15,$00,$14,$00,$13,$00,$12,$00,$11,$00
      .byte $10,$00,$0f,$00,$0e,$00,$0d,$00,$0c,$00,$0c,$00,$0b,$00,$0b,$00
      .byte $0a,$00,$09,$00,$09,$00,$08,$00

L116e  ld   a,(ix+$01)   ; dd 7e 01
       or   a            ; b7
       ret  m            ; f8
       or   $90          ; f6 90
       call L11a3        ; cd a3 11
       ld   a,(ix+$00)   ; dd 7e 00
       call L11a3        ; cd a3 11
       ld   a,(ix+$04)   ; dd 7e 04
       res  4,a          ; cb a7
       sla  a            ; cb 27
       sla  a            ; cb 27
       sla  a            ; cb 27
       call L11a3        ; cd a3 11
       ret               ; c9

L118d  ld   a,(ix+$01)   ; dd 7e 01
       or   a            ; b7
       ret  m            ; f8
       or   $80          ; f6 80
       call L11a3        ; cd a3 11
       ld   a,(ix+$00)   ; dd 7e 00
       call L11a3        ; cd a3 11
       ld   a,$40        ; 3e 40
       call L11a3        ; cd a3 11
       ret               ; c9

L11a3  ld   l,a          ; 6f
       ld   bc,$fffd     ; 01 fd ff
       ld   a,$0e        ; 3e 0e
       out  (c),a        ; ed 79
       ld   bc,$bffd     ; 01 fd bf
       ld   a,$fa        ; 3e fa
       out  (c),a        ; ed 79
       ld   e,$03        ; 1e 03
L11b4  dec  e            ; 1d
       jr   nz,L11b4     ; 20 fd
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       ld   a,l          ; 7d
       ld   d,$08        ; 16 08
L11be  rra               ; 1f
       ld   l,a          ; 6f
       jp   nc,L11c9     ; d2 c9 11
       ld   a,$fe        ; 3e fe
       out  (c),a        ; ed 79
       jr   L11cf        ; 18 06
L11c9  ld   a,$fa        ; 3e fa
       out  (c),a        ; ed 79
       jr   L11cf        ; 18 00
L11cf  ld   e,$02        ; 1e 02
L11d1  dec  e            ; 1d
       jr   nz,L11d1     ; 20 fd
       nop               ; 00
       add  a,$00        ; c6 00
       ld   a,l          ; 7d
       dec  d            ; 15
       jr   nz,L11be     ; 20 e3
       nop               ; 00
       nop               ; 00
       add  a,$00        ; c6 00
       nop               ; 00
       nop               ; 00
       ld   a,$fe        ; 3e fe
       out  (c),a        ; ed 79
       ld   e,$06        ; 1e 06
L11e7  dec  e            ; 1d
       jr   nz,L11e7     ; 20 fd
       ret               ; c9

; ** THE TAPE AND RAMDISK OPERATIONS **

; SAVE
L11eb  ld   hl,$5b66
       set  5,(hl)
       jr   L1205

; LOAD
L11f2  ld   hl,$5b66
       set  4,(hl)
       jr   L1205

; VERIFY
L11f9  ld   hl,$5b66
       set  7,(hl)
       jr   L1205

; MERGE
L1200  ld   hl,$5b66
       set  6,(hl)

L1205  ld   hl,$5b66     ; 
       res  3,(hl)       ; Reset RAMdisk flag
       rst  18h          ; Get current character
       cp   $21          ; "!"
       jp   nz,L13be     ;
       ld   hl,$5b66     ; 
       set  3,(hl)       ; Set RAMdisk flag
       rst  20h          ; Move on to next character
       jp   L13be        ;

L1219  call L05ac        ;
       .byte $0b         ; "C Nonsense in BASIC"

; RAMDisk operation.
; The information relating to the file is copied into memory in 5B66... \
; to ensure that it is available once other RAM pages are switched in
L121d  ld   ($5b74),hl   ;
       ld   a,(ix+$00)   ; File type
       ld   ($5b71),a    ;
       ld   l,(ix+$0b)   ; HL=length
       ld   h,(ix+$0c)   ; 
       ld   ($5b72),hl   ;
       ld   l,(ix+$0d)   ; HL=start address
       ld   h,(ix+$0e)   ; 
L1235  ld   ($5b78),hl   ;
       ld   l,(ix+$0f)   ;
       ld   h,(ix+$10)   ;
       ld   ($5b76),hl   ;
       or   a            ; Test file type
       jr   z,L124e      ; Jump ahead for a BASIC file
       cp   $03          ; 
       jr   z,L124e      ; Jump ahead for a CODE file
       ld   a,(ix+$0e)   ;
       ld   ($5b76),a    ;
L124e  push ix           ; IX points to file header
       pop  hl           ; Retrieve into HL
       inc  hl           ; HL points to filename
       ld   de,$5b67     ; Copy to 5B67
       ld   bc,$000a     ;
       ldir              ;
       ld   hl,$5b66     ;
       bit  5,(hl)       ; Set for SAVE
       jp   nz,L1bad     ; Jump ahead if SAVE
       ld   hl,$5b71     ; Copy the expected details to 5b7a
       ld   de,$5b7a     ;
       ld   bc,$0007     ;
       ldir              ;
       call L1c2e        ; Load header to 5b71
       ld   a,($5b7a)    ; Check that file types match
       ld   b,a          ;
       ld   a,($5b71)    ;
       cp   b            ;
       jr   nz,L1280     ; Error b if not
       cp   $03          ; Check for type 3 (CODE)
       jr   z,L1290      ;
       jr   c,L1284      ; Only file types 00-02 are OK
L1280  call L05ac        ;
       .byte $1d         ; "b Wrong file type"
L1284  ld   a,($5b66)    ; 
       bit  6,a          ; Set for MERGE
       jr   nz,L12c5     ; Jump for MERGE
       bit  7,a          ; Set for VERIFY
       jp   z,L12db      ; 
L1290  ld   a,($5b66)    ; Flags
       bit  6,a          ; Set for MERGE
       jr   z,L129b      ; Jump if not MERGE
       call L05ac        ;
       .byte $1c         ; "a MERGE error"
L129b  ld   hl,($5b7b)   ; Length requested
       ld   de,($5b72)   ; Length of actual file (don't know when this was set up?)
       ld   a,h          ;
       or   l            ;
       jr   z,L12ae      ; Jump ahead if requested length is 0 (= not specified?)
       sbc  hl,de        ;
       jr   nc,L12ae     ; Jump ahead if requested length is OK
       call L05ac        ;
       .byte $1e         ; "c CODE error"
L12ae  ld   hl,($5b7d)
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L12b8     ; 20 03
       ld   hl,($5b74)   ; 2a 74 5b
L12b8  ld   a,($5b71)    ; 3a 71 5b
       and  a            ; a7
       jr   nz,L12c1     ; 20 03
       ld   hl,($5c53)   ; 2a 53 5c
L12c1  call L137e        ; cd 7e 13
       ret               ; c9
L12c5  ld   bc,($5b72)   ; ed 4b 72 5b
       push bc           ; c5
       inc  bc           ; 03
       rst  28h          ; ef
       .word $0030
       ld   (hl),$80     ; 36 80
       ex   de,hl        ; eb
       pop  de           ; d1
       push hl           ; e5
       call L137e        ; cd 7e 13
       pop  hl           ; e1
       rst  28h          ; ef
       .word $08ce ; ME-CONTRL + ?
       ret               ; c9

L12db  ld   de,($5b72)   ; DE = length
       ld   hl,($5b7d)   ; 2a 7d 5b
       push hl           ; e5
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L12ed     ; 20 06
       inc  de           ; 13
       inc  de           ; 13
       inc  de           ; 13
       ex   de,hl        ; eb
       jr   L12f6        ; 18 09
L12ed  ld   hl,($5b7b)   ; 2a 7b 5b
       ex   de,hl        ; eb
       scf               ; 37
       sbc  hl,de        ; ed 52
       jr   c,L12ff      ; 38 09
L12f6  ld   de,$0005     ; 11 05 00
       add  hl,de        ; 19
       ld   b,h          ; 44
       ld   c,l          ; 4d
       rst  28h          ; ef
       .word $1f05 ; TEST-ROOM
L12ff  pop  hl           ; e1
       ld   a,($5b71)    ; 3a 71 5b
L1303  and  a            ; a7
       jr   z,L1335      ; 28 2f
       ld   a,h          ; 7c
       or   l            ; b5
       jr   z,L1315      ; 28 0b
       dec  hl           ; 2b
       ld   b,(hl)       ; 46
       dec  hl           ; 2b
       ld   c,(hl)       ; 4e
       dec  hl           ; 2b
       inc  bc           ; 03
       inc  bc           ; 03
       inc  bc           ; 03
       rst  28h          ; ef
       .word $19e8 ; RECLAIM-2
L1315  ld   hl,($5c59)   ; 2a 59 5c
       dec  hl           ; 2b
       ld   bc,($5b72)   ; ed 4b 72 5b
       push bc           ; c5
       inc  bc           ; 03
       inc  bc           ; 03
       inc  bc           ; 03
       ld   a,($5b7f)    ; 3a 7f 5b
       push af           ; f5
       rst  28h          ; ef
       .word $1655 ; MAKE-ROOM
       inc hl
       pop  af           ; f1
       ld   (hl),a       ; 77
       pop  de           ; d1
       inc  hl           ; 23
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       inc  hl           ; 23
L1331  call L137e        ; cd 7e 13
       ret               ; c9
L1335  ld   hl,$5b66     ; 21 66 5b
       res  1,(hl)       ; cb 8e
       ld   de,($5c53)   ; ed 5b 53 5c
       ld   hl,($5c59)   ; 2a 59 5c
       dec  hl           ; 2b
       rst  28h          ; ef
       .word $19e5 ; RECLAIM
       ld   bc,($5b72)   ; ed 4b 72 5b
       ld   hl,($5c53)   ; 2a 53 5c
       rst  28h          ; ef
       .word $1655 ; MAKE-ROOM
       inc hl
       ld   bc,($5b76)   ; ed 4b 76 5b
       add  hl,bc        ; 09
       ld   ($5c4b),hl   ; 22 4b 5c
       ld   a,($5b79)    ; 3a 79 5b
       ld   h,a          ; 67
       and  $c0          ; e6 c0
       jr   nz,L1370     ; 20 10
       ld   a,($5b78)    ; 3a 78 5b
       ld   l,a          ; 6f
       ld   ($5c42),hl   ; 22 42 5c
       ld   (iy+$0a),$00 ; fd 36 0a 00
       ld   hl,$5b66     ; 21 66 5b
       set  1,(hl)       ; cb ce
L1370  ld   hl,($5c53)   ; 2a 53 5c
       ld   de,($5b72)   ; ed 5b 72 5b
       dec  hl           ; 2b
       ld   ($5c57),hl   ; 22 57 5c
       inc  hl           ; 23
       jr   L1331        ; 18 b3
L137e  ld   a,d          ; 7a
       or   e            ; b3
       ret  z            ; c8
       call L1c4b        ; Load bytes
       ret               ; c9

L1385  rst  28h          ; Expecting an expression on the BASIC line
       .word EXPT_EXP
       bit  7,(iy+$01)   ; Return early if syntax-checking
       ret  z            ;
       push af           ; Get the item off the calculator stack
       rst  28h          ; 
       .word STK_FETCH
       pop  af           ; 
       ret               ;

; Called to check a filename for validity and to copy it into a buffer
; starting at 5B67
L1393  rst  20h          ; Advance the pointer into the BASIC line
       call L1385        ; Get expression from BASIC line
       ret  z            ; Return if syntax checking
       push af           ; 
       ld   a,c          ; Check for zero length
       or   b            ;
       jr   z,L13ba      ; Error f if so
       ld   hl,$000a     ; Check for length &gt; 10
       sbc  hl,bc        ; 
       jr   c,L13ba      ; Error f if so
       push de           ; Stack the filename start...
       push bc           ; ... and length
       ld   hl,$5b67     ; HL points to where we will store the filename
       ld   b,$0a        ; Fill it with 10 spaces
       ld   a,$20        ;
L13ad  ld   (hl),a       ;
       inc  hl           ;
       djnz L13ad        ;
       pop  bc           ; Restore filename length...
       pop  hl           ; ... and start
       ld   de,$5b67     ; DE points to where we will store the filename
       ldir              ; Perform the copy
       pop  af           ; Restore flags
       ret               ; 

L13ba  call L05ac        ; cd ac 05
      .byte $21          ; "f Invalid name"

; SAVE, LOAD, MERGE, VERIFY
L13be  rst 28h
       .word EXPT_EXP
       bit  7,(iy+$01)   ;
       jr   z,L1407      ; Jump ahead if checking syntax
       ld   bc,$0011     ; Size of header, 17d bytes
       ld   a,($5c74)    ; Effectively the BASIC command
       and  a            ; Is it SAVE?
       jr   z,L13d2      ; Jump ahead if so
       ld   c,$22        ; Otherwise need 34d bytes
L13d2  rst  28h          ;
       .word $0030       ; Create memory in workspace
       push de           ; Get start of the created memory into IX
       pop  ix           ; 
       ld   b,$0b        ; Clear the filename
       ld   a,$20        ; ' '
L13dc  ld   (de),a       ; 
       inc  de           ;
       djnz L13dc        ;
       ld   (ix+$01),$ff ;
       rst  28h          ; Get value from calculator stack
       .word STK_FETCH
       ld   hl,$fff6     ; = -10
       dec  bc           ; 0b
       add  hl,bc        ; 09
       inc  bc           ; 03
       jr   nc,L1400     ; 30 11
       ld   a,($5c74)    ; Indicates which BASIC command
       and  a            ; Is it SAVE?
       jr   nz,L13f9     ; Jump ahead if not
       call L05ac        ; Error report
       .byte $0e         ; "F Invalid filename"
L13f9  ld   a,b
       or   c            ; b1
       jr   z,L1407      ; 28 0a
       ld   bc,$000a     ; 01 0a 00
L1400  push ix           ; dd e5
       pop  hl           ; e1
       inc  hl           ; 23
       ex   de,hl        ; eb
       ldir              ; ed b0
L1407  rst  18h          ; Get character from BASIC line
       cp   $e4          ; "DATA"
       jr   nz,L145f     ; Jump if not DATA
       ld   a,($5c74)    ; Check the BASIC command
       cp   $03          ; Is it MERGE?
       jp   z,L1219      ; "C Nonsense in BASIC" if so
       rst  20h          ; Get next character from BASIC line
       rst  28h          ;
       .word $28b2       ; LOOK_VARS
       jr nc,L142f
       ld   hl,$0000     ; 21 00 00
       bit  6,(iy+$01)   ; fd cb 01 76
       jr   z,L1425      ; 28 02
       set  7,c          ; cb f9
L1425  ld   a,($5c74)    ; 3a 74 5c
       dec  a            ; 3d
       jr   z,L1444      ; 28 19
       call L05ac        ; Error report
       .byte $01          ; "2 Variable not found"

L142f  jp   nz,L1219     ; "C Nonsense in BASIC"
       bit  7,(iy+$01)   ; fd cb 01 7e
       jr   z,L1451      ; 28 19
       ld   c,(hl)       ; 4e
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       ld   (ix+$0b),a   ; dd 77 0b
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       ld   (ix+$0c),a   ; dd 77 0c
       inc  hl           ; 23
L1444  ld   (ix+$0e),c   ; dd 71 0e
       ld   a,$01        ; 3e 01
       bit  6,c          ; cb 71
       jr   z,L144e      ; 28 01
       inc  a            ; 3c
L144e  ld   (ix+$00),a   ; dd 77 00
L1451  ex   de,hl        ; eb
       rst  20h          ; e7
       cp   $29          ; fe 29
       jr   nz,L142f     ; 20 d8
       rst  20h          ; e7
       call L18a1        ; cd a1 18
       ex   de,hl        ; eb
       jp   L1519        ; c3 19 15
L145f  cp   $aa          ; 'SCREEN$' ?
       jr   nz,L1482     ; Jump ahead if not
       ld   a,($5c74)    ; Check the BASIC command
       cp   $03          ; Is it MERGE?
       jp   z,L1219      ; "C Nonsense in BASIC" if so
       rst  20h          ; Advance pointer into BASIC line
       call L18a1        ; Ensure end of statement 
       ld   (ix+$0b),$00 ; Set up length...
       ld   (ix+$0c),$1b ; 
       ld   hl,$4000     ; ... and start of CODE block
       ld   (ix+$0d),l   ;
       ld   (ix+$0e),h   ;
       jr   L14cf        ;
L1482  cp   $af          ; 'CODE'?
       jr   nz,L14d5     ; Jump ahead if not
       ld   a,($5c74)    ; Check the BASIC command
       cp   $03          ; Is it MERGE?
       jp   z,L1219      ; "C Nonsense in BASIC" if so
       rst  20h          ; e7
       rst  28h          ; ef
       .word $2048 ; PR-ST-END
       jr   nz,L14a0
       ld   a,($5c74)    ; 3a 74 5c
       and  a            ; a7
       jp   z,L1219      ; "C Nonsense in BASIC"
       rst  28h          ; ef
       .word $1ce6 ; USE-ZERO
       jr   L14af        ; 18 0f
L14a0  rst  28h          ; ef
       .word $1c82 ; EXPT-1NUM
       rst  18h          ; df
       cp   $2c          ; fe 2c
       jr   z,L14b4      ; 28 0c
       ld   a,($5c74)    ; 3a 74 5c
       and  a            ; a7
       jp   z,L1219      ; "C Nonsense in BASIC"
L14af  rst  28h          ; ef
       .word $1ce6 ; USE-ZERO
       jr   L14b8        ; 18 04
L14b4  rst  20h          ; e7
       rst  28h          ; ef
       .word $1c82 ; EXPT-1NUM
L14b8  call L18a1        ; cd a1 18
       rst  28h          ;
       .word $1e99 ; FIND-INT2
       ld   (ix+$0b),c   ; Store the length of the CODE block
       ld   (ix+$0c),b   ;
       rst  28h          ;
       .word $1e99 ; FIND-INT2
       ld   (ix+$0d),c   ; Store the start address of the CODE block
       ld   (ix+$0e),b   ; 
       ld   h,b          ; 60
       ld   l,c          ; 69
L14cf  ld   (ix+$00),$03 ; Store file type = 03 (CODE)
       jr   L1519        ;
L14d5  cp   $ca          ; 'LINE'?
       jr   z,L14e2      ; Jump ahead if so
       call L18a1        ; Check for end of statement
       ld   (ix+$0e),$80 ; Indicate no LINE number
       jr   L14f9        ; 
L14e2  ld   a,($5c74)    ; Check the BASIC command
       and  a            ; Is it SAVE?
       jp   nz,L1219     ; "C Nonsense in BASIC" if not
       rst  20h          ; Advance pointer into BASIC line
       rst  28h          ; Get LINE number onto calculator stack
       .word $1c82 ; EXPT-1NUM
       call L18a1        ; Ensure end-of-statement
       rst  28h          ; Retrieve LINE number from calculator stack
       .word $1e99 ; FIND-INT2
       ld   (ix+$0d),c   ; Store LINE number
       ld   (ix+$0e),b   ;
L14f9  ld   (ix+$00),$00 ; Store file type = 00
       ld   hl,($5c59)   ; 2a 59 5c
L1500  ld   de,($5c53)   ; ed 5b 53 5c
       scf               ; 37
       sbc  hl,de        ; ed 52
       ld   (ix+$0b),l   ; dd 75 0b
       ld   (ix+$0c),h   ; dd 74 0c
       ld   hl,($5c4b)   ; 2a 4b 5c
       sbc  hl,de        ; ed 52
       ld   (ix+$0f),l   ; dd 75 0f
       ld   (ix+$10),h   ; dd 74 10
       ex   de,hl        ; eb
L1519  ld   a,($5b66)    ; Flags byte
       bit  3,a          ; RAMdisk bit
       jp   nz,L121d     ; Jump if the operation is on the RAMdisk
       ld   a,($5c74)    ; Otherwise get the BASIC command
       and  a            ; Test for SAVE
       jr   nz,L152b     ; Jump ahead if not
       rst  28h          ; 
       .word $0970 ; SA-CONTROL
       ret               ;
L152b  rst  28h          ;
       .word $0761 ; SA-ALL + ?
       ret               ;

; called when a line is entered?
L152f  ld   hl,$eef5     ; 
       res  0,(hl)       ; flag ?
       set  1,(hl)       ; flag ?
L1536  ld   hl,($5c49)   ; E PPC - current line number
       ld   a,h          ; 
       or   l            ;
       jr   nz,L1540     ; Jump ahead if not zero
       ld   ($ec06),hl   ; Store in ???
L1540  ld   a,($f9db)    ;
       push af           ; 
       ld   hl,($fc9a)   ; First line on screen (or 0)
       call L334a        ; Get address of line number in BASIC,
                         ; or 0 if no such line
       ld   ($f9d7),hl   ;
       call L3222        ; copy some bytes
       call L30d6        ; around
       pop  af           ;
L1554  or   a            ; b7
       jr   z,L1563      ; 28 0c
       push af           ; f5
       call L30df        ; cd df 30
       ex   de,hl        ; eb
       call L326a        ; cd 6a 32
       pop  af           ; f1
       dec  a            ; 3d
       jr   L1554        ; 18 f1
L1563  ld   c,$00        ; first line on screen
       call L30b4        ; DE = start of editing line C
       ld   b,c          ; currently indexed editing line
       ld   a,($ec15)    ;
       ld   c,a          ; C=#editing lines on screen
       push bc           ; 
       push de           ;
L156f  call L30df        ; cd df 30
       ld   a,($eef5)    ; 3a f5 ee
       bit  1,a          ; cb 4f
       jr   z,L1596      ; 28 1d
       push de           ; d5
       push hl           ; e5
       ld   de,$0020     ; 11 20 00
       add  hl,de        ; 19
       bit  0,(hl)       ; cb 46
       jr   z,L1594      ; 28 11
       inc  hl           ;
       ld   d,(hl)       ; Get line number into DE
       inc  hl           ; 
       ld   e,(hl)       ;
       or   a            ;
       ld   hl,($5c49)   ; E PPC, number of current line
       sbc  hl,de        ;
       jr   nz,L1594     ; Jump ahead unless this is the current line
       ld   hl,$eef5     ; 
       set  0,(hl)       ;
L1594  pop  hl           ; e1
       pop  de           ; d1
L1596  push bc           ; c5
       push hl           ; e5
       ld   bc,$0023     ; 01 23 00
       ldir              ; [ this is where the IF n1>n2 corruption initally
                         ;   occurs ]
       pop  hl           ; e1
       pop  bc           ; c1
       push de           ; d5
       push bc           ; c5
       ex   de,hl        ; eb
       ld   hl,$eef5     ; 21 f5 ee
       bit  0,(hl)       ; cb 46
       jr   z,L15d3      ; 28 2a
       ld   b,$00        ; 06 00
L15ab  ld   hl,($ec06)   ; 2a 06 ec
       ld   a,h          ; 7c
       or   l            ; b5
       jr   z,L15c0      ; 28 0e
       push hl           ; e5
       call L2e41        ; cd 41 2e
       pop  hl           ; e1
       jr   nc,L15cb     ; 30 12
       dec  hl           ; 2b
       inc  b            ; 04
       ld   ($ec06),hl   ; 22 06 ec
       jr   L15ab        ; 18 eb
L15c0  call L2e41        ; cd 41 2e
       call nc,L2e63     ; d4 63 2e
       ld   hl,$eef5     ; 21 f5 ee
       ld   (hl),$00     ; 36 00
L15cb  ld   a,b          ; 78
       pop  bc           ; c1
       push bc           ; c5
       ld   c,b          ; 48
       ld   b,a          ; 47
       call L2a11        ; cd 11 2a
L15d3  pop  bc           ; c1
       pop  de           ; d1
       ld   a,c          ; 79
       inc  b            ; 04
       cp   b            ; b8
       jr   nc,L156f     ; 30 95
       ld   a,($eef5)    ; 3a f5 ee
       bit  1,a          ; cb 4f
       jr   z,L1602      ; 28 21
       bit  0,a          ; cb 47
       jr   nz,L1602     ; 20 1d
       ld   hl,($5c49)   ; 2a 49 5c       current line number
       ld   a,h          ; 7c
       or   l            ; b5
       jr   z,L15f4      ; 28 08
       ld   ($fc9a),hl   ; 22 9a fc
L15ef  call L3222        ; cd 22 32
       jr   L15fd        ; 18 09
L15f4  ld   ($fc9a),hl   ; 22 9a fc
       call L3352        ; cd 52 33
       ld   ($5c49),hl   ; 22 49 5c
L15fd  pop  de           ; d1
       pop  bc           ; c1
       jp   L1536        ; c3 36 15
L1602  pop  de           ; d1
       pop  bc           ; c1
       cp   a            ; Set Z flag
L1605  push af           ; f5
       ld   a,c          ; 79
       ld   c,b          ; 48
       call L30b4        ; Get address of start of line in editing area
       ex   de,hl        ; ... into HL
L160c  push af           ; 
       call L3604        ;
       pop  af           ;
       ld   de,$0023     ; next line
       add  hl,de        ;
L1615  inc  c            ;
       cp   c            ;
       jr   nc,L160c     ;
       pop  af           ;
       ret  z            ;
       call L2a07        ; cd 07 2a
L161e  call L2b78        ; cd 78 2b
       ld   hl,($ec06)   ; 2a 06 ec
       dec  hl           ; 2b
       ld   a,h          ; 7c
       or   l            ; b5
       ld   ($ec06),hl   ; 22 06 ec
       jr   nz,L161e     ; 20 f2
       jp   L2a11        ; c3 11 2a
       ret               ; c9

L1630  ld   b,$00        ; 
       ld   a,($ec15)    ; 
       ld   d,a          ; D=#editing lines available
       jp   L3b5e        ;

L1639  ld   b,$00        ; 06 00
       push hl           ; e5
       ld   c,b          ; 48
       call L30b4        ; DE =  start of editing line C
       call L326a        ; cd 6a 32
       pop  hl           ; e1
       ret  nc           ; d0
       call L30df        ; cd df 30
L1648  push bc           ; c5
       push hl           ; e5
       ld   hl,$0023     ; 21 23 00
       add  hl,de        ; 19
       ld   a,($ec15)    ; 3a 15 ec
       ld   c,a          ; 4f
       cp   b            ; b8
       jr   z,L1663      ; 28 0e
L1655  push bc           ; c5
L1656  push bc           ; c5
       ld   bc,$0023     ; 01 23 00
       ldir              ; ed b0
       pop  bc           ; c1
       ld   a,c          ; 79
       inc  b            ; 04
       cp   b            ; b8
       jr   nz,L1656     ; 20 f4
       pop  bc           ; c1
L1663  pop  hl           ; e1
L1664  call L3618        ; cd 18 36
       ld   bc,$0023     ; 01 23 00
       ldir              ; ed b0
       scf               ; 37
       pop  bc           ; c1
       ret               ; c9

L166f  ld   b,$00        ; 06 00
       call L322b        ; cd 2b 32
       ret  nc           ; d0
L1675  push bc           ; c5
       push hl           ; e5
       ld   a,($ec15)    ; 3a 15 ec
       ld   c,a          ; 4f
       call L30b4        ; DE = start of editing line C
       call L311e        ; cd 1e 31
       jr   nc,L16a9     ; 30 26
       dec  de           ; 1b
       ld   hl,$0023     ; 21 23 00
       add  hl,de        ; 19
       ex   de,hl        ; eb
       push bc           ; c5
       ld   a,b          ; 78
       cp   c            ; b9
       jr   z,L169a      ; 28 0c
L168e  push bc           ; c5
       ld   bc,$0023     ; 01 23 00
       lddr              ; ed b8
       pop  bc           ; c1
L1695  ld   a,b          ; 78
       dec  c            ; 0d
       cp   c            ; b9
       jr   c,L168e      ; 38 f4
L169a  ex   de,hl        ; eb
       inc  de           ; 13
       pop  bc           ; c1
       pop  hl           ; e1
       call L362c        ; cd 2c 36
       ld   bc,$0023     ; 01 23 00
       ldir              ; ed b0
       scf               ; 37
       pop  bc           ; c1
       ret               ; c9
L16a9  pop  hl           ; e1
       pop  bc           ; c1
       ret               ; c9

; Insert the byte A into the line beginning at DE at position B,
; shifting characters right
; On return, if A>00 then it is the right-most character
; which has been shifted off the end,
; HL points to the last character inserted [?]
L16ac  push de           ;
       ld   h,$00        ;
       ld   l,b          ; 
L16b0  add  hl,de        ; HL = DE + B
       ld   d,a          ; Value to store
       ld   a,b          ; A = counter
L16b3  ld   e,(hl)       ; Take a copy of the value we are about to overwrite
       ld   (hl),d       ; Store the previous value
       ld   d,e          ; Keep the copy for next time
       inc  hl           ; 
       inc  a            ;
       cp   $20          ; Finished?
       jr   c,L16b3      ; 
       ld   a,e          ; A = value shifted out the right
       cp   $00          ; Return with Z set if no more to do
L16bf  pop  de           ; Restore DE
       ret               ; 

L16c1  push de           ; d5
       ld   hl,$0020     ; 21 20 00
L16c5  add  hl,de        ; 19
       push hl           ; e5
       ld   d,a          ; 57
       ld   a,$1f        ; 3e 1f
       jr   L16d3        ; 18 07
L16cc  ld   e,(hl)       ; 5e
       ld   (hl),d       ; 72
       ld   d,e          ; 53
       cp   b            ; b8
       jr   z,L16d6      ; 28 04
       dec  a            ; 3d
L16d3  dec  hl           ; 2b
       jr   L16cc        ; 18 f6
L16d6  ld   a,e          ; 7b
       cp   $00          ; fe 00
       pop  hl           ; e1
       pop  de           ; d1
       ret               ; c9

; The offset table; similar in construction to the table in the 48K ROM \
; at 1A48
L16dc  .byte $b1   ; DEF FN    -> $178d
       .byte $c9   ; CAT       -> $17a6
       .byte $bc   ; FORMAT    -> $179a
       .byte $be   ; MOVE      -> $179d
       .byte $c3   ; ERASE     -> $17a3
       .byte $af   ; OPEN #    -> $1790
       .byte $b4   ; CLOSE #   -> $1796
       .byte $93   ; MERGE     -> $1776
       .byte $91   ; VERIFY    -> $1775
L16e5  .byte $92   ; BEEP      -> $1777
       .byte $95   ; CIRCLE    -> $177b
       .byte $98   ; INK       -> $177f
       .byte $98   ; PAPER     -> $1780
       .byte $98   ; FLASH     -> $1781
       .byte $98   ; BRIGHT    -> $1782
       .byte $98   ; INVERSE   -> $1783
       .byte $98   ; OVER      -> $1784
       .byte $98   ; OUT       -> $1785
       .byte $7f   ; LPRINT    -> $176d
       .byte $81   ; LLIST     -> $1770
       .byte $2e   ; STOP      -> $171e
       .byte $6c   ; READ      -> $175d
       .byte $6e   ; DATA      -> $1760
       .byte $70   ; RESTORE   -> $1763
       .byte $48   ; NEW       -> $173c
       .byte $94   ; BORDER    -> $1789
       .byte $56   ; CONTINUE  -> $174c
       .byte $3f   ; DIM       -> $1736
       .byte $41   ; REM       -> $1739
       .byte $2b   ; FOR       -> $1724
       .byte $17   ; GO TO     -> $1711
       .byte $1f   ; GO SUB    -> $171a
       .byte $37   ; INPUT     -> $1733
       .byte $77   ; LOAD      -> $1774
       .byte $44   ; LIST      -> $1742
       .byte $0f   ; LET       -> $170e
       .byte $59   ; PAUSE     -> $1759
       .byte $2b   ; NEXT      -> $172c
       .byte $43   ; POKE      -> $1745
       .byte $2d   ; PRINT     -> $1730
       .byte $51   ; PLOT      -> $1755
       .byte $3a   ; RUN       -> $173f
       .byte $6d   ; SAVE      -> $1773
       .byte $42   ; RANDOMIZE -> $1749
       .byte $0d   ; IF        -> $1715
       .byte $49   ; CLS       -> $1752
       .byte $5c   ; DRAW      -> $1766
       .byte $44   ; CLEAR     -> $174f
       .byte $15   ; RETURN    -> $1721
       .byte $5d   ; COPY      -> $176a

; The parameter table; similar to the parameter table in the 48K ROM at 1a7a
L170e  .byte $01          ; CLASS-01    LET
       .byte $3d          ; "="
       .byte $02          ; CLASS-02

L1711  .byte $06          ; CLASS-06    GO TO
       .byte $00          ; CLASS-00
       .word $1e67 ; GO-TO ; routine in ROM 1

L1715 .byte $06          ; CLASS-06    IF
      .byte $cb          ; "THEN"
      .byte $0e          ; CLASS-0E
      .word $1967        ; new IF routine in ROM 0

L171a .byte $06           ; CLASS-06    GO SUB
      .byte $0c          ; CLASS-0C
      .word $1a53        ; new GOSUB routine in ROM 0

L171e  .byte $00          ; CLASS-00    STOP
       .word $1cee        ; STOP routine in ROM 1

L1721  .byte $0c          ; RETURN
       .word $1a6f        ; new RETURN routine in ROM 0

L1724  .byte $04          ; CLASS-04    FOR
       .byte $3d          ; "="
       .byte $06          ; CLASS-06
       .byte $cc          ; "TO"
       .byte $06          ; CLASS-06
       .byte $0e          ; CLASS-0E
       .word $1981        ; new FOR routine in ROM 0 (?)

L172c  .byte $04          ; CLASS-04    NEXT
       .byte $00          ; CLASS-00
       .word $1dab        ; NEXT routine in ROM 1

L1730  .byte $0e          ; CLASS-0E    PRINT
       .word $2178        ; new PRINT routine in ROM 0

L1733  .byte $0e          ; CLASS-0E    INPUT
       .word $218c        ; new INPUT routine in ROM 0

L1736  .byte $0e          ; CLASS-0E    DIM
       .word $21d5        ; new DIM routine in ROM 0

L1739  .byte $0e          ; CLASS-0E    REM
       .word $1862        ; new REM routine in ROM 0

L173c  .byte $0c          ; CLASS-0C    NEW
       .word $21aa        ; new NEW routine in ROM 0

L173f  .byte $0d          ; CLASS-0D   RUN
       .word $1a02        ; new RUN routine in ROM 0

L1742  .byte $0e          ; CLASS-0E   LIST
       .word $1b75        ; new LIST routine in ROM 0

L1745  .byte $08          ; CLASS-08   POKE
       .byte $00          ; CLASS-00
       .word $1e80        ; POKE routine in ROM 1

L1749  .byte $03          ; CLASS-03   RANDOMIZE
       .word $1e4f        ; RANDOMIZE routine in ROM 1

L174c  .byte $00          ; CLASS-00   CONTINUE
       .word $1e5f        ; CONTINUE routine in ROM 1

L174f  .byte $0d          ; CLASS-0D   CLEAR
       .word $1a0d        ; new CLEAR routine in ROM 0

L1752  .byte $00          ; CLASS-00   CLS
       .word CLS          ; CLS routine in ROM 1

L1755  .byte $09          ; CLASS-09   PLOT
       .byte $00          ; CLASS-00
       .word $22dc        ; PLOT routine in ROM 1

L1759  .byte $06          ; CLASS-06   PAUSE
       .byte $00          ; CLASS-00
       .word $1f3a        ; PAUSE routine in ROM 1

L175d  .byte $0e          ; CLASS-0E   READ
       .word $19ab        ; new READ routine in ROM 0

L1760  .byte $0e          ; CLASS-0E   DATA
       .word $19eb        ; new DATA routine in ROM 0

L1763  .byte $03          ; CLASS-03  RESTORE
       .word $1e42        ; RESTORE routine in ROM 1

L1766  .byte $09          ; CLASS-09  DRAW
       .byte $0e          ; CLASS-0e
       .word $21be        ; new DRAW routine in ROM 0

L176a  .byte $0c          ; CLASS-0C  COPY
       .word $21a7        ; new COPY routine in ROM 0

L176d  .byte $0e          ; CLASS-0E  LPRINT
       .word $2174        ; new LPRINT routine in ROM 0

L1770  .byte $0e          ; CLASS-0E  LLIST
       .word $1b71        ; new LLIST routine in ROM 0

L1773  .byte $0b          ; CLASS-0B   SAVE
L1774  .byte $0b          ; CLASS-0B   LOAD
L1775  .byte $0b          ; CLASS-0B   VERIFY
L1776  .byte $0b          ; CLASS-0B   MERGE

L1777  .byte $08          ; CLASS-08   BEEP
       .byte $00          ; CLASS-00
       .word $03f8        ; BEEP routine in ROM 1

L177b  .byte $09          ; CLASS-09   CIRCLE
       .byte $0e          ; CLASS-0E
       .word $21ae        ; new CIRCLE routine in ROM 0

L177f  .byte $07          ; CLASS-07   INK
L1780  .byte $07          ; CLASS-07   PAPER
L1781  .byte $07          ; CLASS-07   FLASH
L1782  .byte $07          ; CLASS-07   BRIGHT
L1783  .byte $07          ; CLASS-07   INVERSE
L1784  .byte $07          ; CLASS-07   OVER

L1785  .byte $08          ; CLASS-08     OUT
       .byte $00          ; CLASS-00
       .word $1e7a        ; OUT routine in ROM 1

L1789  .byte $06          ; CLASS-06     BORDER
       .byte $00          ; CLASS-00
       .word $2294        ; BORDER routine in ROM 1

L178d  .byte $0e,$8c,$1a  ; DEF-FN

L1790  .byte $06          ; CLASS-06    OPEN#
      .byte $2c          ; ","
       .byte $0a          ; CLASS-0A
       .byte $00          ; CLASS-00
      .word $1736        ; OPEN# routine in ROM 1

L1796 .byte $06          ; CLASS-06    CLOSE
      .byte $00          ; CLASS-00
      .word $16e5        ; CLOSE# routine in ROM 1

L179a  .byte $0e          ; CLASS-0E   FORMAT
       .word $0641        ; FORMAT routine in ROM 0

L179d .byte $0a          ; CLASS-0A   MOVE
      .byte $2c          ; ","
      .byte $0a          ; CLASS-0A
      .byte $0c          ; CLASS-0C
      .word $1af0        ; (just a RET)

L17a3  .byte $0e          ; CLASS-0E  ERASE
       .word $1c0c        ; new ERASE routine in ROM 0

L17a6  .byte $0e          ; CLASS-0E  CAT
       .word $1be5        ; new CAT routine in ROM 0

L17a9  .byte $0c           ; CLASS-0C  SPECTRUM
       .word $1b2b         ; SPECTRUM routine in ROM 0

L17ac  .byte $0e           ; CLASS-0E  PLAY
       .word $2317         ; PLAY routine in ROM 0

; [From Logan & O'Hara's 48K ROM disassembly]:
; The requirements for the different command classes are as follows:
; CLASS-00 - No further operands
; CLASS-01 - Used in LET. A variable is required.
; CLASS-02 - Used in LET. An expression, numeric or string, must follow.
; CLASS-03 - A numeric expression may follow. Zero to be used in case of default
; CLASS-04 - A single character variable must follow.
; CLASS-05 - A set of items may be given.
; CLASS-06 - A numeric expression must follow
; CLASS-07 - Handles colour items.
; CLASS-08 - Two numeric expressions, separated by a comma, must follow.
; CLASS-09 - As for CLASS-08 but colour items may precede the expressions.
; CLASS-0A - A string expression must follow.
; CLASS-0B - Handles cassette routines.

; In addition the 128 adds the following classes:
; CLASS-0C - (used by SPECTRUM) Like class 0 but calling ROM 0?
; CLASS-0D - Like class 06 but calling ROM 0?
; CLASS-0E - (used by PLAY)

L17af  res  7,(iy+$01)   ; fd cb 01 be
       rst  28h          ; ef
      .word $19fb ; E-LINE-NO
       xor  a            ; af
       ld   ($5c47),a    ; 32 47 5c
       dec  a            ; 3d
       ld   ($5c3a),a    ; 32 3a 5c
       jr   L17c1        ; 18 01
L17c0  rst  20h          ; NEXT-CHAR
L17c1  rst  28h          ;
      .word $16bf ; SET-WORK
      inc  (iy+$0d)
       jp   m,L1912      ; 
       rst  18h          ; GET-CHAR
       ld   b,$00        ;
       cp   $0d          ; ENTER
       jp   z,L1863      ;
       cp   $3a          ; ":"
       jr   z,L17c0      ;
       ld   hl,$1821     ;
       push hl           ;
       ld   c,a          ;
       rst  20h          ; NEXT-CHAR
       ld   a,c          ;
       sub  $ce        ; 
       jr   nc,L17f4     ; jump for DEF FN and above
       add  a,$ce        ; otherwise...
       ld   hl,$17a9     ; 
       cp   $a3          ; "SPECTRUM"
       jr   z,L1800      ;
       ld   hl,$17ac     ; 
       cp   $a4          ; "PLAY"
       jr   z,L1800      ;
       jp   L1912        ; C Nonsense in BASIC

L17f4  ld   c,a          ;
       ld   hl,$16dc     ;
       add  hl,bc        ;
       ld   c,(hl)       ;
       add  hl,bc        ;
       jr   L1800        ;

L17fd  ld   hl,($5c74)   ; T ADDR
L1800  ld   a,(hl)       ; 
       inc  hl           ; 
       ld   ($5c74),hl   ; T ADDR
       ld   bc,$17fd     ; address to return to
       push bc           ; stack address
       ld   c,a          ; 4f
       cp   $20          ; fe 20
       jr   nc,L181a     ; jump with code of 20h or over
       ld   hl,$18b5     ; Lookup offset in table
       ld   b,$00        ; of (18b5 + code)
       add  hl,bc        ;
       ld   c,(hl)       ;
       add  hl,bc        ; HL = 18B5 + code + (18B5 + code)
       push hl           ;
       rst  18h          ; Get the current character
       dec  b            ; 05
       ret               ; Return to the stacked address
L181a  rst  18h          ; 
       cp   c            ; b9
       jp   nz,L1912     ; c2 12 19
       rst  20h          ; e7
       ret               ; c9

L1821  call L05d6        ; Check for BREAK
       jr   c,L182a      ;
       call L05ac        ; 
       .byte $14         ; "L Break into program"
L182a  bit  7,(iy+$0a)   ; NSPPC - statement number in line to be jumped to
       jp   nz,L18a8     ;
       ld   hl,($5c42)   ; NEWPPC, line number to be jumped to
       bit  7,h          ;
       jr   z,L184c      ;
L1838  ld   hl,$fffe     ;
       ld   ($5c45),hl   ; PPC
       ld   hl,($5c61)   ; WORKSP
       dec  hl           ; 
       ld   de,($5c59)   ; E LINE
       dec  de           ;
       ld   a,($5c44)    ; NSPPC
       jr   L1882        ;
L184c  rst  28h          ;
      .word $196e ;  LINE-ADDR
       ld   a,($5c44)    ; NSPPC
       jr   z,L1870      ; 28 1c
       and  a            ; a7
       jr   nz,L189d     ; 20 46
       ld   b,a          ; 47
       ld   a,(hl)       ; 7e
       and  $c0          ; e6 c0
       ld   a,b          ; 78
       jr   z,L1870      ; 28 12
       call L05ac        ; cd ac 05
       .byte $ff          ; "0 OK"

; REM routine
L1862  pop  bc           ;
L1863  bit  7,(iy+$01)   ; fd cb 01 7e
       ret  z            ; c8
       ld   hl,($5c55)   ; NXTLIN
       ld   a,$c0        ;
       and  (hl)         ;
       ret  nz           ;
       xor  a            ; af
L1870  cp   $01          ; fe 01
       adc  a,$00        ; ce 00
       ld   d,(hl)       ; 56
       inc  hl           ; 23
       ld   e,(hl)       ; 5e
       ld   ($5c45),de   ; PPC
       inc  hl           ; 
       ld   e,(hl)       ; 
       inc  hl           ;
       ld   d,(hl)       ;
       ex   de,hl        ;
       add  hl,de        ;
       inc  hl           ;
L1882  ld   ($5c55),hl   ; NXTLIN
       ex   de,hl        ;
       ld   ($5c5d),hl   ; CH ADD
       ld   d,a          ;
       ld   e,$00        ;
       ld   (iy+$0a),$ff ; NSPPC
       dec  d            ;
       ld   (iy+$0d),d   ; SUB PPC
       jp   z,L17c0      ; ca c0 17
       inc  d            ; 14
       rst  28h          ; ef
      .word $198b ; EACH-STMT
       jr   z,L18a8      ; 28 0b
L189d  call L05ac        ;
      .byte $16          ; "N Statement lost"

L18a1  bit  7,(iy+$01)   ; Very like CHECK-END at 1BEE in ROM 1
       ret  nz           ; Return unless checking syntax.
       pop  bc           ;
       pop  bc           ;
L18a8  rst  18h          ;
       cp   $0d          ;
       jr   z,L1863      ;
       cp   $3a          ;
       jp   z,L17c0      ;
       jp   L1912        ; C Nonsense in BASIC

; keyword data
L18b5 .byte $24   ; CLASS-00 -> L18d9
      .byte $43   ; CLASS-01 -> L18f9
      .byte $46   ; CLASS-02 -> L18fd
      .byte $1e   ; CLASS-03 -> L18d6
      .byte $4c   ; CLASS-04 -> L1905
      .byte $20   ; CLASS-05 -> L18da
      .byte $53   ; CLASS-06 -> L190e
      .byte $5e   ; CLASS-07 -> L191a
      .byte $4d   ; CLASS-08 -> L190A
      .byte $86   ; CLASS-09 -> L1944
      .byte $57   ; CLASS-0a -> L1916
      .byte $88   ; CLASS-0b -> L1948
      .byte $06   ; CLASS-0c -> L18c7
      .byte $02   ; CLASS-0d -> L18c4
      .byte $05   ; CLASS-0e -> L18c8

L18c4  rst  28h          ; Code 0d enters here
       .word $1cde       ; FETCH-NUM
L18c7  cp   a            ; Code 0c enters here
                         ; Set zero flag
L18c8  pop  bc           ; Code 0e enters here
                         ; Retrieve return address
       call z,L18a1      ; Check for end of line
       ex   de,hl        ; eb
       ld   hl,($5c74)   ; 2a 74 5c
       ld   c,(hl)       ; 4e
       inc  hl           ; 23
       ld   b,(hl)       ; 46
       ex   de,hl        ; eb
       push bc           ; c5
       ret               ; c9

L18d6  rst  28h          ; Code 03 enters here
      .word $1cde ; FETCH-NUM
L18d9  cp   a            ; Code 00 enters here
L18da  pop  bc           ; Code 05 enters here
       call z,L18a1      ; cc a1 18
       ex   de,hl        ; eb
       ld   hl,($5c74)   ; 2a 74 5c
       ld   c,(hl)       ; 4e
       inc  hl           ; 23
       ld   b,(hl)       ; 46
       ex   de,hl        ; eb
       push hl           ; e5
       ld   hl,$18f8     ; 21 f8 18
       ld   ($5b5a),hl   ; 22 5a 5b
       ld   hl,$5b14     ; 21 14 5b
       ex   (sp),hl      ; e3
       push hl           ; e5
       ld   h,b          ; 60
       ld   l,c          ; 69
       ex   (sp),hl      ; e3
       jp   $5b00        ; c3 00 5b
L18f8  ret               ; c9

L18f9  rst  28h          ; ef
      .word $1c1f ; CLASS-01  ; "...identification of the variable in a
                              ;   LET, READ or INPUT statement" (Logan/O'Hara)
       ret               ;

L18fd  pop  bc           ; Code 02 enters here
       rst  28h          ;
      .word $1c56 ; VAL-FET-1 ; "... used by LET, READ and INPUT statements to
                              ;   first evaluate and then assign values to the
                              ;   previously designated variable" (Logan/O'Hara)
       call L18a1        ; cd a1 18
       ret               ; c9

L1905  rst  28h          ; Code 04 enters here
       .word $1c6c ; CLASS-04 ; "... used by FOR & NEXT statements" (Logan/O'Hara)
       ret               ;

       rst  20h          ; Get the next character
       rst  28h          ;
      .word $1c7a ; EXPT-2NUM
       ret               ; c9

L190e  rst  28h          ; Code 06 enters here
       .word $1c82 ; EXPT-1NUM
       ret               ; c9

L1912  call L05ac        ; Error report
       .byte $0b          ; "C Nonsense in BASIC"

L1916  rst  28h          ;
       .word EXPT_EXP
       ret               ;

L191a  bit  7,(iy+$01)   ; fd cb 01 7e
       res  0,(iy+$02)   ; fd cb 02 86
       jr   z,L1927      ; 28 03
       rst  28h          ; ef
       .word $0d4d ; TEMPS
L1927  pop  af           ; f1
       ld   a,($5c74)    ; 3a 74 5c
       sub  $a7        ; d6 a7
       rst  28h          ; ef
       .word $21fc ; CO-TEMP-4
       call L18a1
       ld   hl,($5c8f)
       ld   ($5c8d),hl   ; 22 8d 5c
       ld   hl,$5c91     ; 21 91 5c
       ld   a,(hl)       ; 7e
       rlca              ; 07
       xor  (hl)         ; ae
       and  $aa          ; e6 aa
       xor  (hl)         ; ae
       ld   (hl),a       ; 77
       ret               ; c9

L1944  rst  28h          ; ef
       .word $1cbe ; CLASS-09
       ret               ; c9

L1948  pop  af           ; f1
       ld   a,($5b66)    ; 3a 66 5b
       and  $0f          ; e6 0f
       ld   ($5b66),a    ; 32 66 5b
       ld   a,($5c74)    ; (T ADDR-lo)
       sub  $74          ; Correct by $74 so that SAVE = 00, LOAD = 01,
       ld   ($5c74),a    ;   VERIFY = 02, MERGE = 03
       jp   z,L11eb      ; ca eb 11
       dec  a            ; 3d
       jp   z,L11f2      ; ca f2 11
       dec  a            ; 3d
       jp   z,L11f9      ; ca f9 11
       jp   L1200        ; c3 00 12

; IF routine
L1967  pop  bc           ; c1
       bit  7,(iy+$01)   ; fd cb 01 7e
       jr   z,L197e      ; 28 10
L196e  ld   hl,($5c65)   ; 2a 65 5c
       ld   de,$fffb     ; 11 fb ff
       add  hl,de        ; 19
       ld   ($5c65),hl   ; 22 65 5c
       rst  28h          ; ef
      .word $34e9 ; TEST-ZERO
       jp   c,L1863      ; da 63 18
L197e  jp   L17c1        ; c3 c1 17

; FOR routine
L1981  cp   $cd          ; 'STEP'
       jr   nz,L198e     ;
       rst  20h          ; Advance pointer
       call L190e        ; EXPT-1NUM; get the value of the STEP
       call L18a1        ; cd a1 18
       jr   L19a6        ; 18 18
L198e  call L18a1        ; cd a1 18
       ld   hl,($5c65)   ; 2a 65 5c
       ld   (hl),$00     ; 36 00
       inc  hl           ; 23
       ld   (hl),$00     ; 36 00
       inc  hl           ; 23
       ld   (hl),$01     ; 36 01
       inc  hl           ; 23
       ld   (hl),$00     ; 36 00
       inc  hl           ; 23
       ld   (hl),$00     ; 36 00
       inc  hl           ; 23
       ld   ($5c65),hl   ; 22 65 5c
L19a6  rst  28h          ; ef
       .word $1d16 ; F-REORDER
       ret               ; c9

L19aa  rst  20h          ; READ
L19ab  call L18f9        ; cd f9 18
       bit  7,(iy+$01)   ; fd cb 01 7e
       jr   z,L19e2      ; 28 2e
       rst  18h          ; df
       ld   ($5c5f),hl   ; 22 5f 5c
L19b8  ld   hl,($5c57)   ; 2a 57 5c
       ld   a,(hl)       ; 7e
       cp   $2c          ; fe 2c
       jr   z,L19cb      ; 28 0b
       ld   e,$e4        ; 1e e4
       rst  28h          ; ef
       .word $1d86 ; LOOK-PROG
       jr   nc,L19cb     ; 30 04
       call L05ac        ; Error report
       .byte $0d ;       ; "E Out of Data"
L19cb  inc  hl           ; 23
       ld   ($5c5d),hl   ; 22 5d 5c
       ld   a,(hl)       ; 7e
       rst  28h          ; ef
       .word $1c56 ; VAL-FET-1
       rst  18h          ; df
       ld   ($5c57),hl   ; 22 57 5c
       ld   hl,($5c5f)   ; 2a 5f 5c
       ld   (iy+$26),$00 ; fd 36 26 00
       ld   ($5c5d),hl   ; 22 5d 5c
       ld   a,(hl)       ; 7e
L19e2  rst  18h          ; df
       cp   $2c          ; fe 2c
L19e5  jr   z,L19aa      ; 28 c3
       call L18a1        ; cd a1 18
       ret               ; c9

; DATA
L19eb  bit  7,(iy+$01)   ; fd cb 01 7e
       jr   nz,L19fc     ; 20 0b
L19f1  rst  28h          ; ef
       .word $24fb ; SCANNING
       cp   $2c          ; fe 2c
       call nz,L18a1     ; c4 a1 18
       rst  20h          ; e7
       jr   L19f1        ; 18 f5
L19fc  ld   a,$e4        ; 3e e4
L19fe  rst  28h          ; ef
      .word $1e39 ; PASS-BY
       ret

; RUN routine
L1a02  rst  28h
       .word $1e67 ; GO-TO
       ld bc,$0000
       rst  28h
       .word $1e45 ; REST-RUN     ; RESTORE
       jr   L1a10

; CLEAR
L1a0d  rst  28h          ;
      .word $1e99 ; FIND-INT2
L1a10  ld   a,b          ; 
       or   c            ; 
       jr   nz,L1a18     ; 
       ld   bc,($5cb2)   ; Use RAMTOP if the parameter is 0
L1a18  push bc           ; BC = address to clear to
       ld   de,($5c4b)   ; VARS
       ld   hl,($5c59)   ; E LINE
       dec  hl           ; 
       rst  28h          ; Delete the VARS area
       .word $19e5 ; RECLAIM
       rst  28h          ; Clear the screen
       .word CLS
       ld   hl,($5c65)   ; STKEND
       ld   de,$0032     ;
       add  hl,de        ;
       pop  de           ; ADE = address to clear to
       sbc  hl,de        ;
       jr   nc,L1a3b     ; Ramtop no good
       ld   hl,($5cb4)   ; P RAMT
       and  a            ;
       sbc  hl,de        ;
       jr   nc,L1a3f     ;
L1a3b  call L05ac        ; Error report:
       .byte $15          ; "M Ramtop no good"
L1a3f  ld   ($5cb2),de   ; RAMTOP
       pop  de           ; Retrieve interpreter return address from stack
       pop  hl           ; Retrieve ? from stack
       pop  bc           ; Retrieve top entry from GOSUB stack from stack
                         ;   This will be the end marker, if no GOSUBs are
                         ;   stacked; or the last 'called-from' line number
                         ;   otherwise
       ld   sp,($5cb2)   ; RAMTOP
       inc  sp           ; 33
       push bc           ; c5
       push hl           ; e5
       ld   ($5c3d),sp   ; ERR SP
       push de           ; d5
       ret               ; c9

L1a53  pop  de           ; d1
       ld   h,(iy+$0d)   ; fd 66 0d
       inc  h            ; 24
       ex   (sp),hl      ; e3
       inc  sp           ; 33
       ld   bc,($5c45)   ; ed 4b 45 5c
       push bc           ; c5
       push hl           ; e5
       ld   ($5c3d),sp   ; ed 73 3d 5c
       push de           ; d5
       rst  28h          ; ef
       .word $1e67 ; GO-TO
       ld   bc,$0014
       rst  28h          ; ef
       .word $1f05 ; TEST-ROOM
       ret               ; c9

; RETURN routine
L1a6f  pop  bc           ; c1
       pop  hl           ; e1
       pop  de           ; d1
       ld   a,d          ; 7a
       cp   $3e          ; fe 3e
       jr   z,L1a86      ; 28 0f
       dec  sp           ; 3b
       ex   (sp),hl      ; e3      8000 gets corrupted in test here
       ex   de,hl        ; eb
       ld   ($5c3d),sp   ; ed 73 3d 5c
       push bc           ; c5
       ld   ($5c42),hl   ; 22 42 5c
       ld   (iy+$0a),d   ; fd 72 0a
       ret               ; c9
L1a86  push de           ; d5
       push hl           ; e5
       call L05ac        ; cd ac 05
       .byte $06    ; "7 RETURN without GO SUB"
       bit  7,(iy+$01)
       jr   z,L1a97      ; 28 05
       ld   a,$ce        ; 3e ce
       jp   L19fe        ; c3 fe 19
L1a97  set  6,(iy+$01)   ; fd cb 01 f6
       rst  28h          ; ef
       .word $2c8d ; ALPHA
       jr   nc,L1ab6     ; 30 16
       rst  20h          ; e7
       cp   $24          ; fe 24
       jr   nz,L1aaa     ; 20 05
       res  6,(iy+$01)   ; fd cb 01 b6
       rst  20h          ; e7
L1aaa  cp   $28          ; fe 28
       jr   nz,L1aea     ; 20 3c
       rst  20h          ; e7
       cp   $29          ; fe 29
       jr   z,L1ad3      ; 28 20
L1ab3  rst  28h          ; ef
       .word $2c8d ; ALPHA
L1ab6  jp   nc,L1912     ; d2 12 19
       ex   de,hl        ; eb
       rst  20h          ; e7
       cp   $24          ; fe 24
       jr   nz,L1ac1     ; 20 02
       ex   de,hl        ; eb
       rst  20h          ; e7
L1ac1  ex   de,hl        ; eb
       ld   bc,$0006     ; 01 06 00
       rst  28h          ; ef
       .word $1655 ; MAKE-ROOM
       inc hl
       inc  hl           ; 23
       ld   (hl),$0e     ; 36 0e
       cp   $2c          ; fe 2c
       jr   nz,L1ad3     ; 20 03
       rst  20h          ; e7
       jr   L1ab3        ; 18 e0
L1ad3  cp   $29          ; fe 29
       jr   nz,L1aea     ; 20 13
       rst  20h          ; e7
       cp   $3d          ; fe 3d
       jr   nz,L1aea     ; 20 0e
       rst  20h          ; e7
       ld   a,($5c3b)    ; 3a 3b 5c
       push af           ; f5
       rst  28h          ; ef
       .word $24fb ; SCANNING
       pop  af           ; f1
       xor  (iy+$01)     ; fd ae 01
       and  $40          ; e6 40
L1aea  jp   nz,L1912     ; c2 12 19
       call L18a1        ; cd a1 18
L1af0  ret               ; c9

; Tape Loader
L1af1  ld   hl,$ec0e     ; 21 0e ec
       ld   (hl),$ff     ; 36 ff
       call L1f20        ; cd 20 1f
       rst  28h          ;
       .word $16b0       ; SET-MIN. Clear out editing area.
       ld   hl,($5c59)
       ld   bc,$0003     ; Create 3 bytes of space for the LOAD "" command
       rst  28h          ; 
       .word $1655 ; MAKE-ROOM
       ld   hl,$1b6e     ;
       ld   de,($5c59)   ; (E LINE)
       ld   bc,$0003     ;
       ldir              ; Copy LOAD "" into the line editing area
       call L026b        ; cd 6b 02
L1b14  call L1f20        ; cd 20 1f
       rst  28h          ; ef
       .word $16b0       ; SET-MIN
       ld   hl,($5c59)
       ld   bc,$0001     ; 01 01 00
       rst  28h          ; ef
       .word $1655       ; MAKE-ROOM
       ld   hl,($5c59)
       ld   (hl),$e1     ; 36 e1
       call L026b        ; cd 6b 02
L1b2b  call L1b53        ; SPECTRUM command comes in here
       ld   sp,($5c3d)   ; ed 7b 3d 5c
       pop  hl           ; e1
       ld   hl,$1303     ; 21 03 13
       push hl           ; e5
       ld   hl,$0013     ; 21 13 00
       push hl           ; e5
       ld   hl,$0008     ; 21 08 00
       push hl           ; e5
       ld   a,$20        ; 3e 20
       ld   ($5b5c),a    ; 32 5c 5b
       jp   $5b00        ; c3 00 5b

; 48 BASIC option
L1b47  ld   hl,$0000     ; Stack a 0000 address to return to
       push hl           ; 
       ld   a,$20        ;
       ld   ($5b5c),a    ; Force 48 mode
       jp   $5b00        ; Call SWAP

L1b53  ld   hl,($5c4f)   ; CHANS
       ld   de,$0005     ; 11 05 00
       add  hl,de        ; 19
       ld   de,$000a     ; 11 0a 00
       ex   de,hl        ; eb
       add  hl,de        ; 19
       ex   de,hl        ; eb
       ld   bc,$0004     ; 01 04 00
       ldir              ; ed b0
       res  3,(iy+$30)   ; fd cb 30 9e
       res  4,(iy+$01)   ; fd cb 01 a6
       ret               ; c9

; LOAD ""
L1b6e  .byte $ef,$22,$22

; LLIST routine
L1b71  ld   a,$03        ; Printer channel
       jr   L1b77        ; Jump ahead to join LIST

; LIST routine
L1b75  ld   a,$02        ; Main screen
L1b77  ld   (iy+$02),$00 ;
       rst  28h          ;
       .word $2530       ; SYNTAX-Z
       jr   z,L1b83      ; 28 03
       rst  28h          ; 
       .word $1601       ; Select channel
       rst  28h          ;
       .word $0018       ; [Could just do RST 18]
       rst  28h          ;
       .word $2070       ; STR-ALTER
       jr   c,L1ba3      ; Jump if no attempt to change channel
       rst  28h
       .word $0018       ; Get current character
       cp   $3b          ; ";"
       jr   z,L1b96      ;
       cp   $2c          ; ","
       jr   nz,L1b9e     ;
L1b96  rst  28h          ; Get the next character
       .word $0020       ; 
       call L190e        ;
       jr   L1ba6        ;
L1b9e  rst  28h          ;
       .word $1ce6       ; USER-ZERO
       jr   L1ba6        ;
L1ba3  rst  28h          ;
       .word $1cde       ; FETCH-NUM
L1ba6  call L18a1        ; Test end-of-statement
       rst  28h          ;
       .word $1825       ; a bit after LIST-5
       ret

; RAMdisc SAVE
L1bad  ld   ($5b81),sp   ; Save SP...
       ld   sp,$5bff     ; .. and set it to TSTACK
       call L1c97        ; Create new catalogue entry
       ld   bc,($5b72)   ; Length of file
       ld   hl,$fff7     ; -9
       or   $ff          ; 
       sbc  hl,bc        ; HL=-(length of file + 9)
       call L1cf3        ; check for space in RAMdisk
       ld   bc,$0009     ;
       ld   hl,$5b71     ;
       call L1dac        ; Store 9 header bytes to RAMdisc
       ld   hl,($5b74)   ; Start of file
       ld   bc,($5b72)   ; Length of file
       call L1dac        ; Store actual bytes
       call L1d56        ; Tidy up last catalogue entry
       ld   a,$05        ; Page in logical page 5
       call L1c64        ;   (physical page 0)
       ld   sp,($5b81)   ; Restore SP
       ret               ; Finish

; CAT routine
L1be5  rst  28h          ; Get the current character
       .word $0018       ;    [Could just do RST 18 here]
       cp   $21          ; Test against "!"
       jp   nz,L1912     ; "C Nonsense in BASIC" otherwise
       rst  28h          ; Get the next character
       .word $0020       ;    [Could just do RST 20 here]
       call L18a1        ; Check for end of statement
       ld   a,$02        ; Select main screen
       rst  28h          ; 
       .word $1601       ;
       ld   ($5b81),sp   ; Store SP
       ld   sp,$5bff     ;
       call L20d2        ; Print out the catalogue
       ld   a,$05        ; Page in logical page 5
       call L1c64        ;   (physical page 0)
       ld   sp,($5b81)   ; Restore SP
       ret               ;

; ERASE routine
L1c0c  rst  28h          ; Get character from BASIC line
       .word $0018       ;
       cp   $21          ; Test for "!"
       jp   nz,L1912     ; Nonsense in BASIC
       call L1393        ; Get the filename into (5B67...)
       call L18a1        ; Make sure we've reached the end of the BASIC statement.
       ld   ($5b81),sp   ; Store SP.
       ld   sp,$5bff     ; 
       call L1f5f        ; Do the actual erasing.
       ld   a,$05        ; Restore RAM configuration.
       call L1c64        ;
       ld   sp,($5b81)   ; Restore SP.
       ret               ;

; Load header from RAMdisk
L1c2e  ld   ($5b81),sp   ; Store SP
       ld   sp,$5bff     ; SP=TSTACK
       call L1d35        ;
       ld   hl,$5b71     ; Load 9 header bytes to 5b71
       ld   bc,$0009     ; 
       call L1e37        ; Load bytes from RAMdisk
       ld   a,$05        ; Restore RAM configuration
       call L1c64        ;
       ld   sp,($5b81)   ; Restore SP
       ret               ;

; Load bytes from RAMdisk
L1c4b  ld   ($5b81),sp   ; Store SP
       ld   sp,$5bff     ; and set SP to TSTACK
       ld   b,d          ;
       ld   c,e          ;
       call L1e37        ; Load bytes from RAMdisk
       call L1d56        ; Tidy up last catalogue entry
       ld   a,$05        ; Restore RAM configuration
       call L1c64        ;
       ld   sp,($5b81)   ; Restore SP
       ret               ;

L1c64  push hl           ; page in logical page A
       push bc           ;
       ld   hl,$1c81     ;
       ld   b,$00        ;
       ld   c,a          ;
L1c6c  add  hl,bc        ;
       ld   c,(hl)       ; look up physical page
       di                ;
       ld   a,($5b5c)    ;
       and  $f8          ; mask off current RAM page
       or   c            ; include new page
       ld   ($5b5c),a    ;
       ld   bc,$7ffd     ;
       out  (c),a        ;
       ei                ;
       pop  bc           ;
       pop  hl           ;
       ret               ;

L1c81  .byte 01,03,04,06,07,00  ; physical pages used for RAMdisk

; Compare filenames at 5b67 and IX
L1c87  ld   de,$5b67     ;
; Compare filenames at DE and IX
L1c8a  push ix           ;
L1c8c  pop  hl           ;
       ld   b,$0a        ; max 10 characters
L1c8f  ld   a,(de)       ;
       inc  de           ;
       cp   (hl)         ; compare characters
       inc  hl           ;
       ret  nz           ; return if characters are different
       djnz L1c8f        ; otherwise continue
       ret               ;

; Create new entry in catalogue
L1c97  call L1d12        ; Find entry in RAMdisk area
       jr   z,L1ca0      ; If found?
       call L05ac        ; 
       .byte $20         ; "File already exists"
L1ca0  push ix           ; 
       ld   bc,$3fec     ; 16384-20
       add  ix,bc        ; 
       pop  ix           ; 
       jr   nc,L1d0e     ; 
       ld   hl,$ffec     ; -20
       ld   a,$ff        ; 
       call L1cf3        ; Ensure space in RAMdisk area
       ld   hl,$5b66     ; 
       set  2,(hl)       ; 
       push ix           ; 
       pop  de           ; 
       ld   hl,$5b67     ; 
L1cbe  ld   bc,$000a     ; 
       ldir              ; 
       set  0,(ix+$13)   ; 
       ld   a,(ix+$0a)   ; 
       ld   (ix+$10),a   ; 
       ld   a,(ix+$0b)   ; 
       ld   (ix+$11),a   ; 
       ld   a,(ix+$0c)   ; 
       ld   (ix+$12),a   ; 
       xor  a            ; 
       ld   (ix+$0d),a   ; 
       ld   (ix+$0e),a   ; 
       ld   (ix+$0f),a   ; 
       ld   a,$05        ; 
       call L1c64        ; Logical page 5 (Physical page 0)
       push ix           ;
       pop  hl           ;
       ld   bc,$ffec     ;
L1cee  add  hl,bc        ;
       ld   ($5b83),hl   ; Adjust SF_NEXT
       ret               ;

; Adjust the count of bytes free on RAMdisc
; reserving or freeing up AHL bytes of space in RAMdisk
L1cf3  ld   de,($5b85)   ;
       ex   af,af'       ; A'HL=requested space
       ld   a,($5b87)    ; ADE=free space on RAMdisk
       ld   c,a          ; CDE=free space
       ex   af,af'       ; AHL=requested space
       bit  7,a          ; Is A < 0?
       jr   nz,L1d0a     ; Jump ahead if so
       add  hl,de        ; 
       adc  a,c          ; AHL=free space left
L1d03  ld   ($5b85),hl   ; Store free space
       ld   ($5b87),a    ;
       ret               ; and return
L1d0a  add  hl,de        ;
       adc  a,c          ;
       jr   c,L1d03      ; jump back to store free space if space left
L1d0e  call L05ac        ;
       .byte 03          ;   "4 Out of memory"

; Find catalogue entry for filename at (5B67...)
L1d12  ld   a,$04        ; page in logical page 4
       call L1c64        ;     (physical page 7)
       ld   ix,$ebec     ; 
L1d1b  ld   de,($5b83)   ; SF_NEXT, pointer to last dir.entry
       or   a            ; 
       push ix           ; 
       pop  hl           ; 
       sbc  hl,de        ; 
       ret  z            ; ??
       call L1c87        ; test filename match with (5B67...)
       jr   nz,L1d2e     ; 
       or   $ff          ; 
       ret               ; 
L1d2e  ld   bc,$ffec     ; next directory entry
       add  ix,bc        ;
       jr   L1d1b        ;

L1d35  call L1d12        ; Find RAMdisc file
       jr   nz,L1d3e     ;
       call L05ac        ;
       .byte $23         ; "h File does not exist"
L1d3e  ld   a,(ix+$0a)   ; Take the current start address
       ld   (ix+$10),a   ; and store it as the end address (?)
       ld   a,(ix+$0b)   ;
       ld   (ix+$11),a   ;
       ld   a,(ix+$0c)   ;
       ld   (ix+$12),a   ;
       ld   a,$05        ; Page in logical page 5
       call L1c64        ;   (physical page 0)
       ret               ;

; Tidy up last catalogue entry
L1d56  ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       bit  0,(ix+$13)   ; dd cb 13 46
       ret  z            ; c8
       res  0,(ix+$13)   ; dd cb 13 86
       ld   hl,$5b66     ; 21 66 5b
       res  2,(hl)       ; cb 96
       ld   l,(ix+$10)   ; dd 6e 10
       ld   h,(ix+$11)   ; dd 66 11
       ld   a,(ix+$12)   ; dd 7e 12
       ld   e,(ix+$0a)   ; dd 5e 0a
       ld   d,(ix+$0b)   ; dd 56 0b
       ld   b,(ix+$0c)   ; dd 46 0c
       or   a            ; b7
       sbc  hl,de        ; ed 52
       sbc  a,b          ; 98
       rl   h            ; cb 14
       rl   h            ; cb 14
       sra  a            ; cb 2f
       rr   h            ; cb 1c
       sra  a            ; cb 2f
       rr   h            ; cb 1c
       ld   (ix+$0d),l   ; dd 75 0d
       ld   (ix+$0e),h   ; dd 74 0e
       ld   (ix+$0f),a   ; dd 77 0f
       ld   l,(ix+$10)   ; dd 6e 10
       ld   h,(ix+$11)   ; dd 66 11
       ld   a,(ix+$12)   ; dd 7e 12
       ld   bc,$ffec     ; 01 ec ff
       add  ix,bc        ; dd 09
       ld   (ix+$0a),l   ; dd 75 0a
       ld   (ix+$0b),h   ; dd 74 0b
       ld   (ix+$0c),a   ; dd 77 0c
L1dab  ret               ; c9

; Store BC bytes from HL to RAMdisc
L1dac  ld   a,b          ; Return immediately if BC=0
       or   c            ;
       ret  z            ;
       push hl           ; Save HL
       ld   de,$c000     ;
       ex   de,hl        ; HL=$C000; DE=address from which to save bytes
       sbc  hl,de        ;
       jr   z,L1dd5      ; Jump ahead if saving bytes from C000
       jr   c,L1dd5      ; Jump ahead if saving bytes from an address above C000
       push hl           ; e5
       sbc  hl,bc        ; ed 42
       jr   nc,L1dcc     ; 30 0d
       ld   h,b          ; 60
       ld   l,c          ; 69
       pop  bc           ; c1
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       ex   (sp),hl      ; e3
       ld   de,$c000     ; 11 00 c0
       push de           ; d5
       jr   L1df4        ; 18 28
L1dcc  pop  hl           ; e1
       pop  hl           ; e1
       ld   de,$0000     ; 11 00 00
       push de           ; d5
       push de           ; d5
       jr   L1df4        ; 18 1f
L1dd5  ld   h,b          ; 60
       ld   l,c          ; 69
       ld   de,$0020     ; 11 20 00
       or   a            ; b7
       sbc  hl,de        ; ed 52
       jr   c,L1de4      ; 38 05
       ex   (sp),hl      ; e3
       ld   b,d          ; 42
       ld   c,e          ; 4b
       jr   L1de9        ; 18 05
L1de4  pop  hl           ; e1
       ld   de,$0000     ; 11 00 00
       push de           ; d5
L1de9  push bc           ; c5
       ld   de,$5b98     ; 11 98 5b
       ldir              ; ed b0
       pop  bc           ; c1
       push hl           ; e5
       ld   hl,$5b98     ; 21 98 5b
L1df4  ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       ld   e,(ix+$10)   ; dd 5e 10
       ld   d,(ix+$11)   ; dd 56 11
       ld   a,(ix+$12)   ; dd 7e 12
       call L1c64        ; cd 64 1c
L1e05  ldi               ; ed a0
       ld   a,d          ; 7a
       or   e            ; b3
       jr   z,L1e24      ; 28 19
L1e0b  ld   a,b          ; 78
       or   c            ; b1
       jp   nz,L1e05     ; c2 05 1e
       ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       ld   (ix+$10),e   ; dd 73 10
       ld   (ix+$11),d   ; dd 72 11
       ld   a,$05        ; 3e 05
       call L1c64        ; cd 64 1c
       pop  hl           ; e1
       pop  bc           ; c1
       jr   L1dac        ; 18 88
L1e24  ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       inc  (ix+$12)     ; dd 34 12
       ld   a,(ix+$12)   ; dd 7e 12
       ld   de,$c000     ; 11 00 c0
       call L1c64        ; cd 64 1c
       jr   L1e0b        ; 18 d4

; Load bytes from RAMdisc (guess?)
L1e37  ld   a,b          ; 78
       or   c            ; b1
L1e39  ret  z            ; c8
       push hl           ; e5
       ld   de,$c000     ; 11 00 c0
       ex   de,hl        ; eb
       sbc  hl,de        ; ed 52
       jr   z,L1e67      ; 28 24
       jr   c,L1e67      ; 38 22
L1e45  push hl           ; e5
       sbc  hl,bc        ; ed 42
       jr   nc,L1e5c     ; 30 12
       ld   h,b          ; 60
       ld   l,c          ; 69
       pop  bc           ; c1
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       ex   (sp),hl      ; e3
       ld   de,$0000     ; 11 00 00
       push de           ; d5
       ld   de,$c000     ; 11 00 c0
       push de           ; d5
       ex   de,hl        ; eb
       jr   L1e80        ; 18 24
L1e5c  pop  hl           ; e1
       pop  hl           ; e1
       ld   de,$0000     ; 11 00 00
       push de           ; d5
       push de           ; d5
       push de           ; d5
       ex   de,hl        ; eb
       jr   L1e80        ; 18 19
L1e67  ld   h,b          ; 60
       ld   l,c          ; 69
       ld   de,$0020     ; 11 20 00
       or   a            ; b7
       sbc  hl,de        ; ed 52
       jr   c,L1e76      ; 38 05
       ex   (sp),hl      ; e3
       ld   b,d          ; 42
       ld   c,e          ; 4b
       jr   L1e7b        ; 18 05
L1e76  pop  hl           ; e1
       ld   de,$0000     ; 11 00 00
L1e7a  push de           ; d5
L1e7b  push bc           ; c5
       push hl           ; e5
       ld   de,$5b98     ; 11 98 5b
L1e80  ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       ld   l,(ix+$10)   ; dd 6e 10
       ld   h,(ix+$11)   ; dd 66 11
       ld   a,(ix+$12)   ; dd 7e 12
       call L1c64        ; cd 64 1c
L1e91  ldi               ; ed a0
       ld   a,h          ; 7c
       or   l            ; b5
       jr   z,L1ebc      ; 28 25
L1e97  ld   a,b          ; 78
       or   c            ; b1
L1e99  jp   nz,L1e91     ; c2 91 1e
       ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       ld   (ix+$10),l   ; dd 75 10
       ld   (ix+$11),h   ; dd 74 11
       ld   a,$05        ; 3e 05
       call L1c64        ; cd 64 1c
       pop  de           ; d1
       pop  bc           ; c1
       ld   hl,$5b98     ; 21 98 5b
       ld   a,b          ; 78
       or   c            ; b1
       jr   z,L1eb7      ; 28 02
       ldir              ; ed b0
L1eb7  ex   de,hl        ; eb
       pop  bc           ; c1
       jp   L1e37        ; c3 37 1e
L1ebc  ld   a,$04        ; 3e 04
       call L1c64        ; cd 64 1c
       inc  (ix+$12)     ; dd 34 12
       ld   a,(ix+$12)   ; dd 7e 12
       ld   hl,$c000     ; 21 00 c0
       call L1c64        ; cd 64 1c
       jr   L1e97        ; 18 c8

L1ecf  push af           ; f5
       ld   a,($5b5c)    ; 3a 5c 5b
       push af           ; f5
       push hl           ; e5
       push de           ; d5
       push bc           ; c5
       ld   ix,$5b6a     ; dd 21 6a 5b
       ld   (ix+$10),e   ; dd 73 10
       ld   (ix+$11),d   ; dd 72 11
       ld   (ix+$12),$04 ; dd 36 12 04
       call L1dac        ; cd ac 1d
L1ee8  ld   a,$05        ; 3e 05
       call L1c64        ; cd 64 1c
       pop  bc           ; c1
       pop  de           ; d1
       pop  hl           ; e1
       add  hl,bc        ; 09
       ex   de,hl        ; eb
       add  hl,bc        ; 09
       ex   de,hl        ; eb
       pop  af           ; f1
       ld   bc,$7ffd     ; 01 fd 7f
       di                ; f3
       out  (c),a        ; ed 79
       ld   ($5b5c),a    ; 32 5c 5b
       ei                ; fb
       ld   bc,$0000     ; 01 00 00
       pop  af           ; f1
       ret               ; c9

L1f04  push af           ; f5
L1f05  ld   a,($5b5c)    ; 3a 5c 5b
       push af           ; f5
       push hl           ; e5
       push de           ; d5
       push bc           ; c5
       ld   ix,$5b6a     ; dd 21 6a 5b
       ld   (ix+$10),l   ; dd 75 10
       ld   (ix+$11),h   ; dd 74 11
       ld   (ix+$12),$04 ; dd 36 12 04
       ex   de,hl        ; eb
       call L1e37        ; cd 37 1e
       jr   L1ee8        ; 18 c8

L1f20  ex   af,af'       ;
       ld   a,$00        ; 
       di                ;
       call L1f3a        ; RAM 0
       pop  af           ; AF=address on stack when CALLed
       ld   ($5b58),hl   ; Store HL
       ld   hl,($5b81)   ; Swap SP and (5b81)
       ld   ($5b81),sp   ;
       ld   sp,hl        ;
       ei                ;
       ld   hl,($5b58)   ; Restore HL
       push af           ; Restack the address
       ex   af,af'       ; Get A back
       ret               ;

; Set RAM etc. according to the value in A
L1f3a  push bc           ;
       ld   bc,$7ffd     ;
       out  (c),a        ;
       ld   ($5b5c),a    ;
       pop  bc           ;
       ret               ;

L1f45  ex   af,af'       ; 08
       di                ; f3
       pop  af           ; f1
       ld   ($5b58),hl   ; 22 58 5b
       ld   hl,($5b81)   ; 2a 81 5b
       ld   ($5b81),sp   ; ed 73 81 5b
       ld   sp,hl        ; f9
       ld   hl,($5b58)   ; 2a 58 5b
       push af           ; f5
       ld   a,$07        ; 3e 07
       call L1f3a        ; cd 3a 1f
       ei                ; fb
       ex   af,af'       ; 08
       ret               ; c9

; Erase a RAMdisc file
L1f5f  call L1d12        ; Get the catalogue entry matching the filename at (5B67...)
       jr   nz,L1f68     ; Jump ahead if it was found
       call L05ac        ; Otherwise, error:
       .byte $23         ; "h File does not exist"
L1f68  ld   l,(ix+$0d)   ; AHL=length of file
       ld   h,(ix+$0e)   ;
       ld   a,(ix+$0f)   ;
       call L1cf3        ; Free up this amount of space
       push iy           ; Preserve current value of IY
       ld   iy,($5b83)   ; IY points to end-of-catalogue marker
       ld   bc,$ffec     ; BC=-20d
       add  ix,bc        ; IX points to the next catalogue entry
       ld   l,(iy+$0a)   ; AHL=first spare byte in RAMdisk area
       ld   h,(iy+$0b)   ;
       ld   a,(iy+$0c)   ;
       pop  iy           ; Restore IY to normal value
       ld   e,(ix+$0a)   ; BDE=start of address of next catalogue entry
       ld   d,(ix+$0b)   ;
       ld   b,(ix+$0c)   ;
       or   a            ; Reduce AHL by BDE
       sbc  hl,de        ; 
       sbc  a,b          ;
       rl   h            ; cb 14
       rl   h            ; cb 14
       sra  a            ; cb 2f
       rr   h            ; cb 1c
       sra  a            ; cb 2f
       rr   h            ; cb 1c
       ld   bc,$0014     ; Catalogue entry to delete
       add  ix,bc        ;
       ld   (ix+$10),l   ; Store in the deleted catalogue entry
       ld   (ix+$11),h   ;
       ld   (ix+$12),a   ;
       ld   bc,$ffec     ; 
       add  ix,bc        ; Next catalogue entry
       ld   l,(ix+$0a)   ; DHL=start address of next catalogue entry
       ld   h,(ix+$0b)   ;
       ld   d,(ix+$0c)   ;
       ld   bc,$0014     ;
       add  ix,bc        ; IX points to catalogue entry to delete
       ld   a,d          ; Page in RAM for start address of entry to delete
       call L1c64        ; 
       ld   a,($5b5c)    ; Get physical page in to A
       ld   e,a          ; and then E
       ld   bc,$7ffd     ; Page in physical page 7
       ld   a,$07        ;
       di                ;
       out  (c),a        ;
       exx               ; DHL'=start address of next catalogue entry
       ld   l,(ix+$0a)   ; DHL=start of address of catalogue entry to delete
       ld   h,(ix+$0b)   ;
       ld   d,(ix+$0c)   ;
       ld   a,d          ;
L1fdf  call L1c64        ; Page in RAM for start address
       ld   a,($5b5c)    ;
       ld   e,a          ;
       ld   bc,$7ffd     ;
       exx               ; DHL=start address of next catalogue entry

; At this point we have the registers and alternate registers pointing
; to the actual bytes in the RAMdisc files for the file to be deleted
; and the next file in the catalogue. DHL holds the page-coded address of the
; byte to be moved, and E contains the value which should be OUTed to (5B5C)
; to page in the relevant RAM page.

L1fea  ld   a,$07        ; Physical page 7
       di                ; 
       out  (c),a        ;
       ld   a,(ix+$10)   ; Decrement end address
       sub  $01          ;
       ld   (ix+$10),a   ;
       jr   nc,L200d     ; If no carry then the decrement is finished
       ld   a,(ix+$11)   ; Otherwise decrement the middle byte
       sub  $01          ;
       ld   (ix+$11),a   ; 
       jr   nc,L200d     ; If no carry then the decrement is finished
       ld   a,(ix+$12)   ; Otherwise decrement the highest byte
       sub  $01          ; 
       ld   (ix+$12),a   ;
       jr   c,L203e      ; Jump forward if finished
L200d  out  (c),e        ; Page in RAM page containing the actual file
       ld   a,(hl)       ; Get the byte from the file
       inc  l            ; Increment DHL, setting E if necessary
       jr   nz,L2024     ; 20 11
       inc  h            ; 24
       jr   nz,L2024     ; 20 0e
       ex   af,af'       ; 08
       inc  d            ; 14
       ld   a,d          ; 7a
       call L1c64        ; cd 64 1c
       ld   a,($5b5c)    ; 3a 5c 5b
       ld   e,a          ; 5f
       ld   hl,$c000     ; 21 00 c0
       ex   af,af'       ; 08
L2024  exx               ; DHL=address of file being deleted
       di                ; 
       out  (c),e        ; Page in relevant RAM page
       ld   (hl),a       ; Store the byte taken from the next file
       inc  l            ; Increment DHL, setting E if necessary
       jr   nz,L203b     ; 20 0f
       inc  h            ; 24
       jr   nz,L203b     ; 20 0c
       inc  d            ; 14
       ld   a,d          ; 7a
       call L1c64        ; cd 64 1c
       ld   a,($5b5c)    ; 3a 5c 5b
       ld   e,a          ; 5f
       ld   hl,$c000     ; 21 00 c0
L203b  exx               ; DHL=address of byte in next file;
                         ; DHL'=address of byte in file being deleted
       jr   L1fea        ;
L203e  ld   a,$04        ; Page in logical page 4 (physical page 7)
       call L1c64        ; 
       ld   a,$00        ; 3e 00
       ld   hl,$0014     ; 21 14 00
L2048  call L1cf3        ; cd f3 1c
       ld   e,(ix+$0d)   ; dd 5e 0d
       ld   d,(ix+$0e)   ; dd 56 0e
       ld   c,(ix+$0f)   ; dd 4e 0f
       ld   a,d          ; 7a
       rlca              ; 07
       rl   c            ; cb 11
       rlca              ; 07
       rl   c            ; cb 11
       ld   a,d          ; 7a
       and  $3f          ; e6 3f
       ld   d,a          ; 57
       push ix           ; dd e5
L2061  push de           ; d5
       ld   de,$ffec     ; 11 ec ff
       add  ix,de        ; dd 19
       pop  de           ; d1
       ld   l,(ix+$0a)   ; dd 6e 0a
       ld   h,(ix+$0b)   ; dd 66 0b
       ld   a,(ix+$0c)   ; dd 7e 0c
       or   a            ; b7
       sbc  hl,de        ; ed 52
       sub  c          ; 91
       bit  6,h          ; cb 74
       jr   nz,L207c     ; 20 03
       set  6,h          ; cb f4
       dec  a            ; 3d
L207c  ld   (ix+$0a),l   ; dd 75 0a
       ld   (ix+$0b),h   ; dd 74 0b
       ld   (ix+$0c),a   ; dd 77 0c
       ld   l,(ix+$10)   ; dd 6e 10
       ld   h,(ix+$11)   ; dd 66 11
       ld   a,(ix+$12)   ; dd 7e 12
       or   a            ; b7
       sbc  hl,de        ; ed 52
       sub  c          ; 91
       bit  6,h          ; cb 74
       jr   nz,L2099     ; 20 03
       set  6,h          ; cb f4
       dec  a            ; 3d
L2099  ld   (ix+$10),l   ; dd 75 10
       ld   (ix+$11),h   ; dd 74 11
       ld   (ix+$12),a   ; dd 77 12
       push ix           ; dd e5
       pop  hl           ; e1
       push de           ; d5
       ld   de,($5b83)   ; ed 5b 83 5b      SF_NEXT
       or   a            ; b7
       sbc  hl,de        ; ed 52
       pop  de           ; d1
       jr   nz,L2061     ; 20 b1
       ld   de,($5b83)   ; ed 5b 83 5b      SF_NEXT
       pop  hl           ; e1
       push hl           ; e5
       or   a            ; b7
       sbc  hl,de        ; ed 52
       ld   b,h          ; 44
       ld   c,l          ; 4d
       pop  hl           ; e1
       push hl           ; e5
       ld   de,$0014     ; 11 14 00
       add  hl,de        ; 19
L20c1  ex   de,hl        ; eb
       pop  hl           ; e1
       dec  de           ; 1b
       dec  hl           ; 2b
       lddr              ; ed b8
       ld   hl,($5b83)   ; 2a 83 5b      SF_NEXT
       ld   de,$0014     ; 11 14 00
       add  hl,de        ; 19
       ld   ($5b83),hl   ; 22 83 5b      SF_NEXT
       ret               ; c9

; Print RAMdisc catalogue
L20d2  ld   a,$04        ; Page in logical page 4
       call L1c64        ;  (physical page 7)
       ld   hl,$2121     ; HL points to ten zero bytes
L20da  ld   bc,$212b     ; BC point to ten 0xFF bytes
       ld   ix,$ebec     ; IX points to first catalogue entry
L20e1  call L05d6        ; Check for BREAK
       push ix           ; 
       ex   (sp),hl      ; HL points to current catalogue entry
       ld   de,($5b83)   ; DE = (SF_NEXT) (end-of-catalogue marker)
       or   a            ; 
       sbc  hl,de        ; Have we reached end-of-catalogue?
       pop  hl           ; 
       jr   z,L2111      ; Jump ahead if so
       ld   d,h          ; 
       ld   e,l          ; DE=current catalogue entry
       push hl           ;
       push bc           ;
       call L1c8a        ; Compare current filename with ten zero bytes
       pop  bc           ;
       pop  hl           ;
       jr   nc,L210a     ; 30 0e
       ld   d,b          ; 50
       ld   e,c          ; 59
       push hl           ; e5
       push bc           ; c5
       call L1c8a        ; cd 8a 1c
       pop  bc           ; c1
       pop  hl           ; e1
       jr   c,L210a      ; 38 03
       push ix           ; dd e5
       pop  bc           ; c1
L210a  ld   de,$ffec     ; 11 ec ff
       add  ix,de        ; dd 19
       jr   L20e1        ; 18 d0
L2111  push hl           ; e5
       ld   hl,$212b     ; 21 2b 21
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       pop  hl           ; e1
       ret  z            ; c8
       ld   h,b          ; 60
       ld   l,c          ; 69
       call L2135        ; print the catalogue entry
       jr   L20da        ;

L2121  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
L212b  .byte $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff

; Print a single catalogue entry at BC
L2135  push hl           ;
       push bc           ;
       pop  hl           ;
       ld   de,$5b67     ; Copy the filename to 5B67
       ld   bc,$000a     ;  (so that it is visible when this RAM page is
       ldir              ;   paged out)
       ld   a,$05        ; Page in logical page 5
       call L1c64        ;   (physical page 0)
       ld   hl,($5b81)   ; Use SP as stored in (5b81)
       ld   ($5b81),sp   ;
       ld   sp,hl        ;
       ld   hl,$5b67     ; HL points to filename
       ld   b,$0a        ; 10 characters to print
L2152  ld   a,(hl)       ; 
       push hl           ; 
       push bc           ;
       rst  28h          ; print a character
      .word $0010
       pop  bc           ;
       pop  hl           ;
       inc  hl           ;
       djnz L2152        ;
       ld   a,$0d        ; Print new-line character
       rst  28h          ;
       .word $0010
       rst  28h          ;
       .word $0d4d       ; "TEMPS" - copy permanent colours to temporary colours
       ld   hl,($5b81)   ; Restore SP
       ld   ($5b81),sp   ;
       ld   sp,hl        ;
       ld   a,$04        ; Page in logical page 4
       call L1c64        ;   (physical page 7)
       pop  hl           ;
       ret               ;

; LPRINT routine
L2174  ld   a,$03        ; Printer channel
       jr   L217a        ; Jump ahead

; PRINT routine
L2178  ld   a,$02        ; Main screen
L217a  rst  28h          ;
       .word $2530       ; SYNTAX-Z
       jr   z,L2182      ; 
       rst  28h          ;
       .word $1601       ; CHAN-OPEN
       rst  28h
       .word $0d4d       ; TEMPS
       rst  28h          ;
       .word $1fdf       ; PRINT-2
       call L18a1        ; "C Nonsense in BASIC" during syntax checking if not
                         ; at end of line or statement.
       ret               ;

; INPUT
L218c  rst  28h          ; ef
       .word $2530 ; SYNTAX-Z
       jr   z,L2199      ; 28 08
       ld   a,$01        ; 3e 01
       rst  28h          ; ef
       .word $1601 ; CHAN-OPEN
       rst 28h
       .word CLS_LOWER
L2199  ld   (iy+$02),$01 ; should be DF-SZ not TV-FLAG [AOwen]
       rst  28h          ; ef
       .word $20c1 ; IN-ITEM-1
       call L18a1
       rst 28h
       .word $20a0 ; INPUT-1 + ?
       ret

; COPY routine
L21a7  jp   L08f0        ;

; NEW routine
L21aa  di                ;
       jp   L019d        ;

; CIRCLE routine
L21ae  rst  18h          ; Get character from BASIC line
       cp   ','          ; Check for 2nd parameter
       jr   nz,L21eb     ; Jump ahead (for error C) if not
       rst  20h          ; Advance pointer into BASIC line
       rst  28h          ; Get parameter
       .word $1c82       ; EXPT-1NUM
       call L18a1        ; Ensure end of line or statement
       rst  28h          ;
       .word $232d       ; CIRCLE + $0D
       ret               ;

; DRAW
L21be  rst  18h          ; Get current character
       cp   $2c          ; ","
       jr   z,L21ca      ; Jump if there is a third parameter
       call L18a1        ; Error C during syntax checking if not at end of line/statement
       rst  28h          ;
       .word $2477       ; LINE-DRAW
       ret               ;

L21ca  rst  20h          ; Get the next character
       rst  28h          ;
       .word $1c82       ; EXPT-1NUM
       call L18a1        ; Error C during syntax checking if not at end of line/statement
       rst  28h          ;
       .word $2394       ; after DR-3-PRMS     
       ret               ;

; DIM routine
L21d5  rst  28h          ; Search to see if the array already exists
       .word $28b2       ; LOOK-VARS
       jr   nz,L21eb     ; jump if array variable not found
       rst  28h
       .word $2530       ; SYNTAX-Z
       jr   nz,L21e7     ; jump ahead during syntax checking
       res  6,c          ; Indicate an array (?)
       rst  28h          ; 
       .word $2996       ; STK-VAR
       call L18a1        ; Error when checking syntax unless at end of line/stmt
L21e7  rst  28h          ;
       .word $2c15       ; D-RUN "Reclaim an existing array" (L/O'H)
       ret               ;

L21eb  call L05ac        ; Error report
       .byte $0b         ; C Nonsense in BASIC

; Clear screen if (some condition?) 
L21ef  bit  0,(iy+$30)   ; (FLAGS2?)
       ret  z            ; c8
       rst  28h          ;
       .word $0daf       ; CL-ALL
       ret               ; c9

L21f8  ld   hl,$fffe     ; -2
       ld   ($5c45),hl   ; PPC (current line number)
       res  7,(iy+$01)   ; Indicate 'syntax checking'
       call L228e        ; Point to the start of the line
       rst  28h          ;
       .word $24fb       ; SCANNING ; Evaluate 'next expression'
       bit  6,(iy+$01)   ; Test for type of result
       jr   z,L223a      ; Error if string result
       rst  18h          ; Get current character
       cp   $0d          ; End-of-line?
       jr   nz,L223a     ; Error if not
       set  7,(iy+$01)   ; If so, indicate 'execution'
       call L228e        ; Point to start of line
       ld   hl,$0321     ; 21 21 03
       ld   ($5b8b),hl   ; 22 8b 5b
       rst  28h          ; 
       .word $24fb       ; SCANNING
       bit  6,(iy+$01)   ; Test type of result
       jr   z,L223a      ; Error if string result
       ld   de,$5b8d     ; DE points to last calculator value
       ld   hl,($5c65)   ; (STKEND)
       ld   bc,$0005     ; 
       or   a            ;
       sbc  hl,bc        ; HL points to value on top of calculator stack (?)
       ldir              ; 
       jp   L223e        ;

L223a  call L05ac        ; Error
       .byte $19         ; Q Parameter Error

L223e  ld   a,$0d        ; 3e 0d
       call L226f        ; cd 6f 22
       ld   bc,$0001     ; 01 01 00
       rst  28h          ; ef
       .word $0030
       ld   ($5c5b),hl   ; 22 5b 5c
       push hl           ; e5
       ld   hl,($5c51)   ; 2a 51 5c
       push hl           ; e5
       ld   a,$ff        ; 3e ff
       rst  28h          ; ef
       .word $1601 ; CHAN-OPEN
       rst 28h
       .word $2de3 ; PRINT-FP
       pop  hl           ; e1
       rst  28h          ; ef
       .word $1615 ; CHAN-FLAG
       pop  de
       ld   hl,($5c5b)   ; 2a 5b 5c
       and  a            ; a7
       sbc  hl,de        ; ed 52
L2264  ld   a,(de)       ; 1a
       call L226f        ; cd 6f 22
       inc  de           ; 13
       dec  hl           ; 2b
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L2264     ; 20 f6
       ret               ; c9

L226f  push hl           ; e5
       push de           ; d5
       call L1f45        ; cd 45 1f
       ld   hl,$ec0d     ; 21 0d ec
       res  3,(hl)       ; Reset "line-has-been-altered" flag
       push af           ; f5
       ld   a,$02        ; Main screen
       rst  28h          ; 
       .word $1601       ; CHAN-OPEN
       pop  af
       call L2669        ; cd 69 26
       ld   hl,$ec0d     ;
       res  3,(hl)       ; Reset "line-has-been-altered" flag
       call L1f20        ; cd 20 1f
       pop  de           ; d1
       pop  hl           ; e1
       ret               ; c9

; Point to the start of a typed-in BASIC command
; and get first character
L228e  ld   hl,($5c59)   ; (E LINE), address of command being typed in
       dec  hl           ; 
       ld   ($5c5d),hl   ; (CH ADD), address of next character to be interpreted
       rst  20h          ; get next character
       ret               ;

L2297  call L228e        ; Point to start of command
       cp   $f1          ; 'LET'
       ret  nz           ; Return if not
       ld   hl,($5c5d)   ; (CH ADD); HL points to next character
L22a0  ld   a,(hl)       ; Loop forward to find...
       inc  hl           ; 
       cp   $0d          ; ... end of line or ...
       ret  z            ; 
       cp   $3a          ; ... end of statement
       jr   nz,L22a0     ;
       or   a            ;
L22aa  ret               ;

; Search for A in the list of values at 22bd
L22ab  ld   b,a          ; 47
       ld   hl,$22bd     ; 21 bd 22
L22af  ld   a,(hl)       ; 7e
       inc  hl           ; 23
       or   a            ; b7
       jr   z,L22b9      ; 28 05
       cp   b            ; b8
       jr   nz,L22af     ; 20 f8
       ld   a,b          ; 78
       ret               ; c9
L22b9  or   $ff          ; f6 ff
       ld   a,b          ; 78
       ret               ; c9

L22bd  .byte $2b,$2d,$2a,$2f      ; '+','-','*','/'
       .byte $5e,$3d,$3e,$3c      ; '^','=','>','<'
       .byte $c7,$c8,$c9,$c5,$c6  ; '<=','>=','<>','OR','AND'
       .byte $00                  ; end marker

L22cb  cp   $a5          ; 'RND' (first 48K keyword)
       jr   c,L22dd      ;
       cp   $c4          ; 'BIN'
       jr   nc,L22dd     ; 
       cp   $ac          ; 'AT'
       jr   z,L22dd      ;
       cp   $ad          ; 'TAB'
       jr   z,L22dd      ;
       cp   a            ;
L22dc  ret               ;
L22dd  cp   $a5          ; 'RND' (first 48K keyword)
       ret               ;

L22e0  ld   b,a          ; 47
       or   $20          ; f6 20
       cp   $61          ; fe 61
       jr   c,L22ed      ; 38 06
       cp   $7b          ; fe 7b
L22e9  jr   nc,L22ed     ; 30 02
       cp   a            ; bf
       ret               ; c9
L22ed  ld   a,b          ; 78
       cp   $2e          ; fe 2e
       ret  z            ; c8
       call L230a        ; cd 0a 23
       jr   nz,L2307     ; 20 11
L22f6  rst  20h          ; e7
       call L230a        ; cd 0a 23
       jr   z,L22f6      ; 28 fa
       cp   $2e          ; '.'
       ret  z            ;
       cp   $45          ; 'E'
       ret  z            ;
       cp   $65          ; 'e'
       ret  z            ;
       jr   L22ab        ; 18 a4
L2307  or   $ff          ; f6 ff
       ret               ; c9
L230a  cp   $30          ; fe 30
       jr   c,L2314      ; 38 06
       cp   $3a          ; fe 3a
       jr   nc,L2314     ; 30 02
       cp   a            ; bf
       ret               ; c9
L2314  cp   $30          ; fe 30
       ret               ; c9

; PLAY enters here
L2317  ld   b,$00        ; String index
       rst  18h          ;
L231a  push bc           ;
       rst  28h          ; Get string expression
       .word EXPT_EXP
       pop  bc           ; 
       inc  b            ;
       cp   $2c          ; "," indicates another string
       jr   nz,L2327     ; Jump ahead if no more
       rst  20h          ; Advance to the next character
       jr   L231a        ; Loop back
L2327  ld   a,b          ; Check the index
       cp   $09          ; Maximum of 8 strings
       jr   c,L2330      ; 
       call L05ac        ; Otherwise report error:
       .byte $2b         ; "p (c) 1986 Sinclair Research..." !
L2330  call L18a1        ; Ensure end-of-statement or end-of-line
       jp   L0985        ; Continue with PLAY code

L2336  ld   hl,$5bff     ; 21 ff 5b
       ld   ($5b81),hl   ; 22 81 5b
       call L1f45        ; cd 45 1f
       jp   L25cb        ; c3 cb 25

       and  a            ; a7
       sbc  hl,de        ; ed 52
       ld   b,h          ; 44
       ld   c,l          ; 4d
       add  hl,de        ; 19
       ex   de,hl        ; eb
       ret               ; c9

       ld   bc,$0001     ; 01 01 00
       push hl           ; e5
       push de           ; d5
       call L2358        ; cd 58 23
       pop  de           ; d1
       pop  hl           ; e1
       rst  28h          ; ef
       .word $1655 ;MAKE-ROOM
       ret
L2358  ld   hl,($5c65)   ; 2a 65 5c
       add  hl,bc        ; 09
       jr   c,L2368      ; 38 0a
       ex   de,hl        ; eb
       ld   hl,$0082     ; 21 82 00
       add  hl,de        ; 19
       jr   c,L2368      ; 38 03
       sbc  hl,sp        ; ed 72
       ret  c            ; d8
L2368  ld   (iy+$00),$03 ; fd 36 00 03
       jp   L0321        ; c3 21 03
L236f  add  a,a          ; 87
       add  a,a          ; 87
L2371  ld   l,a          ; 6f
       ld   h,$00        ; 26 00
       add  hl,hl        ; 29
       add  hl,hl        ; 29
       add  hl,hl        ; 29
       ret               ; c9
       ld   hl,$0000     ; 21 00 00
       add  hl,sp        ; 39
       ld   de,($5c65)   ; ed 5b 65 5c
       or   a            ; b7
       sbc  hl,de        ; ed 52
       ret               ; c9
       res  0,(iy-$39)   ; fd cb c7 86
       call L236f        ; cd 6f 23
       push hl           ; e5
       ld   de,($ff24)   ; ed 5b 24 ff
       add  hl,de        ; 19
       ld   d,h          ; 54
       ld   e,l          ; 5d
       ex   (sp),hl      ; e3
L2394  push hl           ; e5
       push de           ; d5
       ld   de,$5800     ; 11 00 58
       add  hl,de        ; 19
       ex   de,hl        ; eb
       pop  hl           ; e1
       ld   bc,$0020     ; 01 20 00
       ld   a,($5c8f)    ; 3a 8f 5c
       call L249b        ; cd 9b 24
       pop  hl           ; e1
       ld   a,h          ; 7c
       ld   h,$00        ; 26 00
       add  a,a          ; 87
       add  a,a          ; 87
       add  a,a          ; 87
       add  a,$40        ; c6 40
       ld   d,a          ; 57
       ld   e,h          ; 5c
       add  hl,de        ; 19
       ex   de,hl        ; eb
       pop  hl           ; e1
       ld   b,$20        ; 06 20
       jp   L23e1        ; c3 e1 23
       ld   d,$ff        ; 16 ff
       call L236f        ; cd 6f 23
       ld   a,d          ; 7a
       ld   de,($ff24)   ; ed 5b 24 ff
       add  hl,de        ; 19
       ld   e,l          ; 5d
       ld   d,h          ; 54
       inc  de           ; 13
       ld   (hl),a       ; 77
       dec  bc           ; 0b
       ldir              ; ed b0
       ret               ; c9
       call L2488        ; cd 88 24
       ld   de,$4000     ; 11 00 40
       ld   hl,($ff24)   ; 2a 24 ff
       ld   b,e          ; 43
       call L23e1        ; cd e1 23
       ld   d,$48        ; 16 48
       call L23e1        ; cd e1 23
       ld   d,$50        ; 16 50
       ld   b,$c0        ; 06 c0
L23e1  ld   a,(hl)       ; 7e
       push hl           ; e5
       push de           ; d5
       cp   $fe          ; fe fe
       jr   c,L23ec      ; 38 04
       sub  $fe        ; d6 fe
       jr   L2422        ; 18 36
L23ec  cp   $20          ; fe 20
       jr   nc,L23f7     ; 30 07
       ld   hl,$2527     ; 21 27 25
       and  a            ; a7
       ex   af,af'       ; 08
       jr   L242b        ; 18 34
L23f7  cp   $80          ; fe 80
       jr   nc,L2409     ; 30 0e
       call L2371        ; cd 71 23
       ld   de,($5c36)   ; ed 5b 36 5c
       add  hl,de        ; 19
       pop  de           ; d1
       call $ff28        ; cd 28 ff
       jr   L2450        ; 18 47
L2409  cp   $90          ; fe 90
       jr   nc,L2411     ; 30 04
       sub  $7f        ; d6 7f
       jr   L2422        ; 18 11
L2411  sub  $90        ; d6 90
       call L2371        ; cd 71 23
       pop  de           ; d1
       call L1f20        ; cd 20 1f
       push de           ; d5
       ld   de,($5c7b)   ; ed 5b 7b 5c
       scf               ; 37
       jr   L2429        ; 18 07
L2422  ld   de,$252f     ; 11 2f 25
       call L2371        ; cd 71 23
       and  a            ; a7
L2429  ex   af,af'       ; 08
       add  hl,de        ; 19
L242b  pop  de           ; d1
L242c  ld   c,d          ; 4a
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  d            ; 14
       ld   a,(hl)       ; 7e
       ld   (de),a       ; 12
       ld   d,c          ; 51
       ex   af,af'       ; 08
       call c,L1f45      ; dc 45 1f
L2450  pop  hl           ; e1
       inc  hl           ; 23
       inc  de           ; 13
       djnz L23e1        ; 10 8c
       ret               ; c9

; copied to ff28 (see L246f)
L2456  push bc           ; c5
       di                ; f3
       ld   bc,$7ffd     ; 01 fd 7f
       ld   a,($5b5c)    ; 3a 5c 5b
       xor  $10          ; ee 10
       out  (c),a        ; ed 79
       ei                ; fb
       ex   af,af'       ; 08
       ex   af,af'       ; 08
       di                ; f3
       ld   c,$fd        ; 0e fd
       xor  $10          ; ee 10
       out  (c),a        ; ed 79
       ei                ; fb
       pop  bc           ; c1
       ret               ; c9

       ld   hl,$2456     ; 21 56 24
       ld   de,$ff28     ; 11 28 ff
       ld   bc,$000e     ; 01 0e 00
       ldir              ; ed b0
       push hl           ; e5
       ld   hl,$242c     ; 21 2c 24
       ld   c,$20        ; 0e 20
       ldir              ; ed b0
       pop  hl           ; e1
       ld   c,$0b        ; 0e 0b
       ldir              ; ed b0
       ret               ; c9
L2488  res  0,(iy-$39)   ; fd cb c7 86
       ld   de,$5800     ; 11 00 58
       ld   bc,$02c0     ; 01 c0 02
       ld   hl,($ff24)   ; 2a 24 ff
       ld   a,($5c8d)    ; 3a 8d 5c
       ld   ($5c8f),a    ; 32 8f 5c
L249b  ex   af,af'       ; 08
L249c  push bc           ; c5
       ld   a,(hl)       ; 7e
       cp   $ff          ; fe ff
       jr   nz,L24aa     ; 20 08
       ld   a,($5c8d)    ; 3a 8d 5c
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  de           ; 13
       jr   L2507        ; 18 5d
L24aa  ex   af,af'       ; 08
       ld   (de),a       ; 12
       inc  de           ; 13
       ex   af,af'       ; 08
       inc  hl           ; 23
       cp   $15          ; fe 15
       jr   nc,L2507     ; 30 54
       cp   $10          ; fe 10
       jr   c,L2507      ; 38 50
       dec  hl           ; 2b
       jr   nz,L24c2     ; 20 08
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       ld   c,a          ; 4f
       ex   af,af'       ; 08
       and  $f8          ; e6 f8
       jr   L2505        ; 18 43
L24c2  cp   $11          ; fe 11
       jr   nz,L24d1     ; 20 0b
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       add  a,a          ; 87
       add  a,a          ; 87
       add  a,a          ; 87
       ld   c,a          ; 4f
       ex   af,af'       ; 08
       and  $c7          ; e6 c7
       jr   L2505        ; 18 34
L24d1  cp   $12          ; fe 12
       jr   nz,L24de     ; 20 09
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       rrca              ; 0f
       ld   c,a          ; 4f
       ex   af,af'       ; 08
       and  $7f          ; e6 7f
       jr   L2505        ; 18 27
L24de  cp   $13          ; fe 13
       jr   nz,L24ec     ; 20 0a
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       rrca              ; 0f
       rrca              ; 0f
       ld   c,a          ; 4f
       ex   af,af'       ; 08
       and  $bf          ; e6 bf
       jr   L2505        ; 18 19
L24ec  cp   $14          ; fe 14
       inc  hl           ; 23
       jr   nz,L2507     ; 20 16
       ld   c,(hl)       ; 4e
       ld   a,($5c01)    ; 3a 01 5c
       xor  c            ; a9
       rra               ; 1f
       jr   nc,L2507     ; 30 0e
       ld   a,$01        ; 3e 01
L24fb  xor  (iy-$39)     ; fd ae c7
       ld   ($5c01),a    ; 32 01 5c
       ex   af,af'       ; 08
       call L2513        ; cd 13 25
L2505  or   c            ; b1
       ex   af,af'       ; 08
L2507  pop  bc           ; c1
       dec  bc           ; 0b
       ld   a,b          ; 78
       or   c            ; b1
       jp   nz,L249c     ; c2 9c 24
       ex   af,af'       ; 08
       ld   ($5c8f),a    ; 32 8f 5c
       ret               ; c9
L2513  ld   b,a          ; 47
       and  $c0          ; e6 c0
       ld   c,a          ; 4f
       ld   a,b          ; 78
       add  a,a          ; 87
       add  a,a          ; 87
       add  a,a          ; 87
       and  $38          ; e6 38
       or   c            ; b1
       ld   c,a          ; 4f
       ld   a,b          ; 78
       rra               ; 1f
       rra               ; 1f
       rra               ; 1f
       and  $07          ; e6 07
       or   c            ; b1
       ret               ; c9
L2527  nop               ; 00
       inc  a            ; 3c
       ld   h,d          ; 62
       ld   h,b          ; 60
       ld   l,(hl)       ; 6e
       ld   h,d          ; 62
       ld   a,$00        ; 3e 00
L252f  nop               ; 00
L2530  ld   l,h          ; 6c
       djnz L2587        ; 10 54
       cp   d            ; ba
       jr   c,L258a      ; 38 54
       add  a,d          ; 82

; Editing keys lookup table. Each editing key maps to the appropriate \
; subroutine. This includes those keys which mirror the functionality \
; of the add-on keypad; these are found by trapping the keyword produced \
; by the keystrokes in 48K mode. Surprisingly there is no attempt to \
; produce an intelligible layout; instead the first 16 keywords have \
; been used. Additionally the entries for DELETE and ENTER should probably \
; come in the first six entries for efficiency reasons.

L2537 .byte $15                 ; number of entries
      .byte $0b  
      .word $2a94    ; CURSOR-UP
      .byte $0a  
      .word $2ab5    ; CURSOR-DOWN
      .byte $08  
      .word $2ad7    ; CURSOR-LEFT
      .byte $09  
      .word $2ae3    ; CURSOR-RIGHT
      .byte $ad  
      .word $2a4f    ; LINE-UP?   (TAB, EXT-P)
      .byte $ac  
      .word $2a25    ; LINE-DOWN? (AT, SYMB-I)
      .byte $af  
      .word $29d4    ; WORD-LEFT  (CODE, EXT-I)
      .byte $ae  
      .word $29e1    ; WORD-RIGHT (VAL$, EXT-SHIFT-J)
      .byte $a6  
      .word $2983    ; TOP-OF-PROGRAM? (INKEY$, EXT-N or GRAPH-W)
      .byte $a5  
      .word $29ab    ; END-OF-PROGRAM? (RND, EXT-T or GRAPH-V)
      .byte $a8  
      .word $2a87    ; START-OF-LINE (FN, EXT-SYMB-2 or GRAPH-Y)
      .byte $a7  
      .word $2a7a    ; END-OF-LINE   (PI, EXT-M or GRAPH-X)
      .byte $aa  
      .word $291b    ; DELETE-RIGHT (SCREEN$, EXT-SHIFT-K)
      .byte $0c  
      .word $292b    ; DELETE
      .byte $b3  
      .word $3017    ; DELETE-WORD-RIGHT (COS, EXT-W)
      .byte $b4  
      .word $2fbc    ; DELETE-WORD-LEFT  (TAN, EXT-E)
      .byte $b0  
      .word $3072    ; DELETE-TO-END-OF-LINE (VAL, EXT-J)
      .byte $b1  
      .word $303e    ; DELETE-TO-START-OF-LINE (LEN, EXT-K)
      .byte $0d  
      .word $2944    ; ENTER
      .byte $a9  
      .word $269b    ; TOGGLE (POINT, EXT-SYMB-8 or GRAPH-Z)
      .byte $07  
      .word $2704    ; MENU (EDIT, SHIFT-1)

L2577 .byte $04
      .byte $0b ; 
      .word $272e ; MENU-UP
      .byte $0a ; 
      .word $2731 ; MENU-DOWN
      .byte $07 ; 
      .word $2717 ; MENU-SELECT
      .byte $0d ; 
      .word $2717 ; MENU-SELECT

L2584  call L28be        ; cd be 28
L2587  ld   hl,$0000     ; 21 00 00
L258a  ld   ($fc9a),hl   ; 22 9a fc
       ld   a,$82        ; 3e 82
       ld   ($ec0d),a    ; 32 0d ec
       ld   hl,$0000     ; 21 00 00
       ld   ($5c49),hl   ; 22 49 5c
       call L35bc        ; cd bc 35
       call L365e        ; cd 5e 36
       ret               ; c9

L259f  ld   hl,$5bff     ; 21 ff 5b
       ld   ($5b81),hl   ; 22 81 5b
       call L1f45        ; cd 45 1f
       ld   a,$02        ; Main screen
       rst  28h          ;
       .word $1601       ; CHAN-OPEN
L25ad  ld   hl,$2744     ; Jump table for start-up menu
       ld   ($f6ea),hl   ; Store this
       ld   hl,$2754     ; The start-up menu
       ld   ($f6ec),hl   ; Store this as the menu to print when EDIT is pressed?
       push hl           ; Store address of menu on stack
       ld   hl,$ec0d     ; General editing flags
       set  1,(hl)       ; Indicate 'menu displayed'
       res  4,(hl)       ; ???
       dec  hl           ; Current menu index
       ld   (hl),$00     ; Set to zero
       pop  hl           ; Retrieve address of menu
       call L36a8        ; Print menu
       jp   L2653        ; c3 53 26

L25cb  ld   ix,$fd6c     ; dd 21 6c fd
       ld   hl,$5bff     ; 21 ff 5b
       ld   ($5b81),hl   ; 22 81 5b
       call L1f45        ; cd 45 1f
       ld   a,$02        ; 3e 02
       rst  28h          ; ef
       .word $1601 ;CHAN-OPEN
       call L3668
       ld   hl,$5c3b
L25e3  bit  5,(hl)       ; cb 6e
       jr   z,L25e3      ; 28 fc
       ld   hl,$ec0d     ; 21 0d ec
       res  3,(hl)       ; Reset 'line has been altered' flag
       bit  6,(hl)       ; Test whether editing area is lower screen
       jr   nz,L2604     ; 20 14
       ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       jr   z,L2601      ; 28 0a
       cp   $00          ; fe 00
       jp   nz,L28c7     ; c2 c7 28
       call L3848        ; Print "128 BASIC" in the banner line
       jr   L2604        ; 18 03
L2601  call L384d        ; Print "Calculator" in the banner line
L2604  call L30d6        ; cd d6 30
       call L3222        ; cd 22 32
       ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       jr   z,L2653      ; 28 42
       ld   hl,($5c49)   ; 2a 49 5c
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L262d     ; 20 15
       ld   hl,($5c53)   ; 2a 53 5c
       ld   bc,($5c4b)   ; ed 4b 4b 5c
       and  a            ; a7
       sbc  hl,bc        ; ed 42
       jr   nz,L262a     ; 20 06
       ld   hl,$0000     ; 21 00 00
       ld   ($ec08),hl   ; 22 08 ec
L262a  ld   hl,($ec08)   ; 2a 08 ec
L262d  call L1f20        ; cd 20 1f
       rst  28h          ; ef
      .word $196e ; LINE-ADDR
       rst  28h          ; ef
      .word $1695 ; LINE-NO
      call L1f45
       ld   ($5c49),de   ; E PPC
       ld   hl,$ec0d     ; 21 0d ec
       bit  5,(hl)       ; Test ???
       jr   nz,L2653     ; 20 0f
       ld   hl,$0000     ; 21 00 00
       ld   ($ec06),hl   ; 22 06 ec
       call L152f        ; cd 2f 15
       call L29f2        ; cd f2 29
       call L2944        ; cd 44 29
L2653  ld   sp,$5bff     ; 31 ff 5b
       call L3668        ; cd 68 36
       call L367f        ; cd 7f 36
       push af           ; f5
       ld   a,($5c39)    ; 3a 39 5c
       call L26ec        ; cd ec 26
       pop  af           ; f1
       call L2669        ; cd 69 26
       jr   L2653        ; 18 ea

L2669  ld   hl,$ec0d     ; Editing flags
       bit  1,(hl)       ; Test whether a menu is displayed
       push af           ; 
       ld   hl,$2577     ; Lookup table for menu keys
       jr   nz,L2677     ;
       ld   hl,$2537     ; Editing keys lookup table
L2677  call L3fce        ; Lookup for any matches
       jr   nz,L2681     ; If no match found
       call nc,L26e7     ; d4 e7 26
       pop  af           ; f1
       ret               ; c9
L2681  pop  af           ; Restore key code & flags
       jr   z,L2689      ; Jump if menu (not?) displayed
       xor  a            ;
       ld   ($5c41),a    ; MODE
       ret               ;
L2689  ld   hl,$ec0d     ;
       bit  0,(hl)       ; Test ???
       jr   z,L2694      ; 28 04
       call L26e7        ; cd e7 26
       ret               ; c9
L2694  cp   $a3          ; fe a3
       jr   nc,L2653     ; 30 bb
       jp   L28f1        ; c3 f1 28

; TOGGLE. Toggle between editing in the lower and upper screen areas
L269b  ld   a,($ec0e)    ; 
       cp   $04          ; Calculator mode?
       ret  z            ; Return if so (TOGGLE has no effect in Calculator mode)
       call L1630        ; cd 30 16
       ld   hl,$ec0d     ; 21 0d ec
       res  3,(hl)       ; Reset "Current line has been altered flag"
       ld   a,(hl)       ;
       xor  $40          ; Toggle screen editing area
       ld   (hl),a       ;
       and  $40          ;
       jr   z,L26b6      ; Jump forward if the editing area is now the upper area
       call L26bb        ; Set the lower area as the current editing area
       jr   L26b9        ; Jump forward
L26b6  call L26ce        ; Set the upper area as the current editing area
L26b9  scf               ; Indicate success
       ret               ; 

; Set the lower screen as the editing area
L26bb  call L3881        ; cd 81 38
       ld   hl,$ec0d     ; 21 0d ec
       set  6,(hl)       ; Lower screen
       call L2e2d        ; cd 2d 2e
       call L3a88        ; cd 88 3a
       call L28df        ; cd df 28
       jr   L26d9        ; 18 0b

; Set the upper screen as the editing area
L26ce  ld   hl,$ec0d     ; 21 0d ec
       res  6,(hl)       ; Main screen
       call L28be        ; cd be 28
       call L3848        ; Restore the "128 BASIC" banner line
L26d9  ld   hl,($fc9a)   ; 2a 9a fc
       ld   a,h          ; 7c
       or   l            ; b5
       call nz,L334a     ; c4 4a 33
       call L152f        ; cd 2f 15
       jp   L29f2        ; c3 f2 29
L26e7  ld   a,($5c38)    ; (RASP) - should be (PIP)? [AOwen]
       srl  a            ; cb 3f
L26ec  push ix           ; dd e5
       ld   d,$00        ; 16 00
       ld   e,a          ; 5f
       ld   hl,$0c80     ; 21 80 0c
L26f4  rst  28h          ; ef
      .word $03b5 ; BEEPER
       pop  ix           ; dd e1
       ret               ; c9
L26fa  push ix           ; dd e5
       ld   de,$0030     ; 11 30 00
       ld   hl,$0300     ; 21 00 03
       jr   L26f4        ; 18 f0

; MENU
L2704  call L29ec        ; Remove cursor square
       ld   hl,$ec0d     ; HL points to editing flags
       set  1,(hl)       ; Set "displaying menu" flag
       dec  hl           ; HL=EC0C
       ld   (hl),$00     ; Initialise 'current menu item' to 00
L270f  ld   hl,($f6ec)   ; Text for current menu
       call L36a8        ; Print menu
       scf               ;
       ret               ;

; MENU-SELECT
L2717  ld   hl,$ec0d     ; HL points to editing flags
       res  1,(hl)       ; Clear "displaying menu" flag
       dec  hl           ; HL=$ec0c
       ld   a,(hl)       ; A =current menu position
       ld   hl,($f6ea)   ; HL points to jump table for current menu
       push hl           ;
       push af           ;
       call L373e        ;
       pop  af           ;
       pop  hl           ;
       call L3fce        ; Call the item in the jump table corresponding to the
                         ; currently selected menu item
       jp   L29f2        ; c3 f2 29

; MENU-UP
L272e  scf               ; 37
       jr   L2732        ; 18 01

; MENU-DOWN
L2731  and  a            ; a7
L2732  ld   hl,$ec0c     ; 21 0c ec
       ld   a,(hl)       ; 7e
       push hl           ; e5
       ld   hl,($f6ec)   ; 2a ec f6
       call c,L37a7      ; dc a7 37
       call nc,L37b6     ; d4 b6 37
       pop  hl           ; e1
       ld   (hl),a       ; 77
L2742  scf               ; 37
       ret               ; c9

; Lookup table for the main 128K menu

L2744 .byte $05    ; Number of entries in table
      .byte $00
      .word L2831  ; Tape Loader
      .byte $01
      .word L286c  ; 128 BASIC
      .byte $02
      .word L2885  ; Calculator
      .byte $03
      .word L1b47  ; 48 BASIC
      .byte $04
      .word L2816  ; Tape Tester

; Contents of the main 128 menu

L2754 .byte $06
      .text "128     "      ; Menu title
      .byte $ff
L275e .text "Tape Loade"
      .byte 'r'+$80
L2769 .text "128 BASI"
      .byte 'C'+$80
L2772 .text "Calculato"
      .byte 'r'+$80
      .text "48 BASI"
      .byte 'C'+$80
L2784 .text "Tape Teste"
      .byte ('r'+$80)

      .byte $a0

; Lookup table for the Edit menu

L2790 .byte $05
      .byte $00
      .word $2742  ; 128 BASIC
      .byte $01
      .word $2851  ; Renumber
      .byte $02
      .word $2811  ; Screen
      .byte $03
      .word $2862  ; Print
      .byte $04
      .word $281C  ; Exit

; Text for the Edit menu

L27a0 .byte $06
      .text "Options "
      .byte $ff
      .text "128 BASI"
      .byte 'C'+$80
      .text "Renumbe"
      .byte 'r'+$80
      .text "Scree"
      .byte 'n'+$80
      .text "Prin"
      .byte 't'+$80
      .text "Exi"
      .byte 't'+$80

       .byte $a0

; Lookup table for the Calculator menu

L27cb  .byte $02
       .byte $00
       .word $2742   ; [return to] Calculator
       .byte $01
       .word $281c   ; Exit

; Text for the Calculator menu
L27d2 .byte 03
      .text "Options "
      .byte $ff
      .text "Calculato"
      .byte 'r'+$80
      .text "Exi"
      .byte 't'+$80

      .byte $a0

L27eb .byte $16,$01,$00
      .byte $10,$00
      .byte $11,$07
      .byte $13,$00
      .text "To cancel - press BREAK twic"
      .byte 'e'+$80

; Screen option from EDIT menu
L2811  call L269b        ; cd 9b 26
       jr   L2874        ; 18 5e

; Tape Tester
L2816  call L3857        ; Clear the menu from the screen and print the "Tape Tester" text in the bottom bar
       call L3be9        ; Runs the tape tester
L281c  ld   hl,$ec0d     ; Tape testing is now finished; restore the system set up and return to the menu
       res  6,(hl)       ; Indicate main screen editing
       call L28be        ; cd be 28
       ld   b,$00        ; 06 00
       ld   d,$17        ; 16 17
       call L3b5e        ; cd 5e 3b
       call L1f20        ; cd 20 1f
       jp   L259f        ; c3 9f 25

; Tape Loader
L2831  call L3852        ; Print "Tape Loader" in the banner line
       ld   hl,$5c3c     ; 21 3c 5c
       set  0,(hl)       ; cb c6
       ld   de,$27eb     ; 11 eb 27
       call L057d        ; cd 7d 05
       res  0,(hl)       ; cb 86
       set  6,(hl)       ; cb f6
       ld   a,$07        ; 3e 07
       ld   ($ec0e),a    ; 32 0e ec
       ld   bc,$0000     ; 01 00 00
       call L372b        ; cd 2b 37
       jp   L1af1        ; c3 f1 1a

; Renumber
L2851  call L3888        ; cd 88 38
       call nc,L26e7     ; d4 e7 26
       ld   hl,$0000     ; 21 00 00
       ld   ($5c49),hl   ; 22 49 5c
       ld   ($ec08),hl   ; 22 08 ec
       jr   L2865        ; 18 03

; Print
L2862  call L1b14        ; cd 14 1b
L2865  ld   hl,$ec0d     ; 21 0d ec
       bit  6,(hl)       ; cb 76
       jr   nz,L2874     ; 20 08
L286c  ld   hl,$5c3c     ; 21 3c 5c 
       res  0,(hl)       ; cb 86
       call L3848        ; Print the "128 BASIC" banner line
L2874  ld   hl,$ec0d     ; 21 0d ec
       res  5,(hl)       ; cb ae
       res  4,(hl)       ; cb a6
       ld   a,$00        ; 3e 00
       ld   hl,$2790     ; 21 90 27
       ld   de,$27a0     ; 11 a0 27
       jr   L28b1        ; 18 2c

; Calculator code
L2885  ld   hl,$ec0d     ;
       set  5,(hl)       ; 
       set  4,(hl)       ;
       res  6,(hl)       ; cb b6
       call L28be        ; cd be 28
       call L384d        ; Print "Calculator" in the banner line
       ld   a,$04        ; Indicate calculator mode
       ld   ($ec0e),a    ;
       ld   hl,$0000     ; 21 00 00
       ld   ($5c49),hl   ; 22 49 5c
       call L152f        ; cd 2f 15
       ld   bc,$0000     ; 01 00 00
       ld   a,b          ; 78
       call L29f8        ; cd f8 29
       ld   a,$04        ; 3e 04
       ld   hl,$27cb     ; Menu jump table
       ld   de,$27d2     ; Menu text table
L28b1  ld   ($ec0e),a    ; 32 0e ec
       ld   ($f6ea),hl   ; Store current menus
       ld   ($f6ec),de   ; 
       jp   L2604        ; c3 04 26
L28be  call L2e1f        ; cd 1f 2e
       call L3a7f        ; cd 7f 3a
       jp   L28e8        ; c3 e8 28
L28c7  ld   b,$00        ; 06 00
       ld   d,$17        ; 16 17
       call L3b5e        ; cd 5e 3b
       jp   L25ad        ; c3 ad 25

L28d1  .byte $06
       .byte $00,$00,$00,$04,$10,$14

L28d8  .byte $06
       .byte $00,$00,$00,$00,$01,$01

; Copy 6 bytes from L28D9 to F6EE
L28df  ld   hl,$28d8
       ld   de,$f6ee
       jp   L3fba        ; c3 ba 3f

; Copy 6 bytes from L28D2 to F6EE
L28e8  ld   hl,$28d1     ; 21 d1 28
       ld   de,$f6ee     ; 11 ee f6
       jp   L3fba        ; c3 ba 3f

L28f1  ld   hl,$ec0d     ; 21 0d ec
       or   a            ; b7
       or   a            ; b7
       bit  0,(hl)       ; cb 46
       jp   nz,L29f2     ; c2 f2 29
       res  7,(hl)       ; cb be
       set  3,(hl)       ; cb de
       push hl           ; e5
       push af           ; f5
       call L29ec        ; Remove cursor from current position
       pop  af           ; f1
       push af           ; f5
       call L2e81        ; cd 81 2e
       pop  af           ; f1
       ld   a,b          ; 78
       call L2b78        ; cd 78 2b
       pop  hl           ; e1
       set  7,(hl)       ; cb fe
       jp   nc,L29f2     ; d2 f2 29
       ld   a,b          ; 78
       jp   c,L29f8      ; da f8 29
       jp   L29f2        ; c3 f2 29

; DELETE-RIGHT
L291b  ld   hl,$ec0d     ; HL points to editing flags
       set  3,(hl)       ; Indicate 'altered this line'
       call L29ec        ; Remove cursor colour from current position
       call L2f12        ; 
       scf               ; 37
       ld   a,b          ; 78
       jp   L29f8        ; c3 f8 29

; DELETE
L292b  ld   hl,$ec0d     ; HL points to editing flags
       res  0,(hl)       ; Reset ??? flag
       set  3,(hl)       ; Indicate 'line altered'
       call L29ec        ; Remove cursor colour from current position
       call L2b5b        ; cd 5b 2b
       ccf               ; 3f
       jp   c,L29f2      ; da f2 29
       call L2f12        ; cd 12 2f
       scf               ; 37
       ld   a,b          ; 78
       jp   L29f8        ; c3 f8 29

; ENTER
L2944  call L29ec        ; Remove cursor colour from current position
       push af           ;
       call L30b4        ; DE = start of editing line C
       push bc           ; stack current editing position
       ld   b,$00        ; go to start of line
       call L2e41        ; get ??? for start of line
       pop  bc           ; retrieve editing position
       jr   c,L295e      ;
       ld   hl,$0020     ; Move on to next line
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       cpl               ; 2f
       and  $09          ; e6 09
       jr   z,L297a      ; 28 1c
L295e  ld   a,($ec0d)    ; Editing flags
       bit  3,a          ; Has the current line been altered?
       jr   z,L296a      ; Jump ahead if not
       call L2c8e        ; [IF n1>n2 corruption here?]
       jr   nc,L297f     ; 30 15
L296a  call L2c4c        ; cd 4c 2c
       call L2b78        ; cd 78 2b
       call L2ece        ; cd ce 2e
       ld   b,$00        ; 06 00
       pop  af           ; f1
       scf               ; 37
       jp   L29f8        ; c3 f8 29
L297a  pop  af           ; f1
       scf               ; 37
       jp   L29f2        ; c3 f2 29
L297f  pop  af           ; f1
       jp   L29f2        ; c3 f2 29
L2983  ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       ret  z            ; c8
       call L29ec        ; Remove cursor colour from the current position
       ld   hl,$0000     ; 21 00 00
       call L1f20        ; cd 20 1f
       rst  28h          ; ef
      .word $196e ; LINE-ADDR
       rst  28h          ; ef
      .word $1695 ; LINE-NO
       call L1f45
       ld   ($5c49),de   ; ed 53 49 5c
       ld   a,$0f        ; 3e 0f
       call L3a96        ; cd 96 3a
       call L152f        ; cd 2f 15
       scf               ; 37
       jp   L29f2        ; c3 f2 29
L29ab  ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       ret  z            ; c8
       call L29ec        ; Remove cursor colour from the current position
       ld   hl,$270f     ; 21 0f 27
       call L1f20        ; cd 20 1f
       rst  28h          ; ef
      .word $196e ;LINE-ADDR
       ex   de,hl        ; eb
       rst  28h          ; ef
      .word $1695 ;LINE-NO
      call L1f45
       ld   ($5c49),de   ; ed 53 49 5c
       ld   a,$0f        ; 3e 0f
       call L3a96        ; cd 96 3a
       call L152f        ; cd 2f 15
       scf               ; 37
       jp   L29f2        ; c3 f2 29

; WORD-LEFT
L29d4  call L29ec        ; Remove cursor colour from the current position
       call L2bea        ; cd ea 2b
       jp   nc,L29f2     ; d2 f2 29
       ld   a,b          ; 78
       jp   L29f8        ; c3 f8 29

; WORD-RIGHT
L29e1  call L29ec        ; Remove cursor colour from the current position
       call L2c09        ; cd 09 2c
       jr   nc,L29f2     ; 30 09
       ld   a,b          ; 78
       jr   L29f8        ; 18 0c

L29ec  call L2a07        ; Get current cursor position
       jp   L364f        ; Restore previous colour to character square

L29f2  call L2a07        ; Get current cursor position
       jp   L3640        ; Set character square to cursor colour

L29f8  call L2a11        ; Store new editing position
       push af           ;
       push bc           ;
       ld   a,$0f        ;
       call L3a96        ; Store new cursor colour
       pop  bc           ;
       pop  af           ;
       jp   L3640        ; Print new cursor then return

; Returns the three bytes of the cursor position from F6EE in ABC.
; C=row, B=column, A=alternate column.
L2a07  ld   hl,$f6ee     ; 21 ee f6
       ld   c,(hl)       ; 4e
       inc  hl           ; 23
       ld   b,(hl)       ; 46
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       inc  hl           ; 23
       ret               ; c9

; Store new editing position
L2a11  ld   hl,$f6ee     ;
       ld   (hl),c       ;
       inc  hl           ;
       ld   (hl),b       ;
       inc  hl           ;
       ld   (hl),a       ;
       ret               ;

; Get current character
L2a1a  push hl           ;
       call L30b4        ; Calculate DE = start of current line
       ld   h,$00        ; 
       ld   l,b          ;
       add  hl,de        ; HL = EC16 + $0023 * C + B = current editing position
       ld   a,(hl)       ; Get character from line
       pop  hl           ; 
       ret               ;

L2a25  call L29ec        ; Remove cursor colour from the current position
       ld   e,a          ; 5f
       ld   d,$0a        ; 16 0a
L2a2b  push de           ; d5
       call L2b30        ; cd 30 2b
       pop  de           ; d1
       jr   nc,L29f2     ; 30 c0
       ld   a,e          ; 7b
       call L2a11        ; cd 11 2a
       ld   b,e          ; 43
       call L2af9        ; cd f9 2a
       jr   nc,L2a42     ; 30 06
       dec  d            ; 15
       jr   nz,L2a2b     ; 20 ec
       ld   a,e          ; 7b
       jr   c,L29f8      ; 38 b6
L2a42  push de           ; d5
       call L2b0b        ; cd 0b 2b
       pop  de           ; d1
       ld   b,e          ; 43
       call L2af9        ; cd f9 2a
       ld   a,e          ; 7b
       or   a            ; b7
       jr   L29f8        ; 18 a9
L2a4f  call L29ec        ; Remove cursor colour from the current position
       ld   e,a          ; 5f
       ld   d,$0a        ; 16 0a
L2a55  push de           ; d5
       call L2b0b        ; cd 0b 2b
       pop  de           ; d1
       jr   nc,L29f2     ; 30 96
       ld   a,e          ; 7b
       call L2a11        ; cd 11 2a
       ld   b,e          ; 43
       call L2b02        ; cd 02 2b
       jr   nc,L2a6d     ; 30 07
       dec  d            ; 15
       jr   nz,L2a55     ; 20 ec
       ld   a,e          ; 7b
       jp   c,L29f8      ; da f8 29
L2a6d  push af           ; f5
       call L2b30        ; cd 30 2b
       ld   b,$00        ; 06 00
       call L2bd4        ; cd d4 2b
       pop  af           ; f1
       jp   L29f8        ; c3 f8 29

; END-OF-LINE
L2a7a  call L29ec        ; Remove cursor colour from current position
       call L2c4c        ; cd 4c 2c
       jp   nc,L29f2     ; d2 f2 29
       ld   a,b          ; 78
       jp   L29f8        ; c3 f8 29

; START-OF-LINE
L2a87  call L29ec        ; Remove cursor colour from current position
       call L2c31        ; cd 31 2c
       jp   nc,L29f2     ; d2 f2 29
       ld   a,b          ; 78
       jp   L29f8        ; c3 f8 29

; CURSOR-UP
L2a94  call L29ec        ; Remove cursor colour from current position
       ld   e,a          ; 5f
       push de           ; d5
       call L2b0b        ; cd 0b 2b
       pop  de           ; d1
       jp   nc,L29f2     ; d2 f2 29
       ld   b,e          ; 43
       call L2b02        ; cd 02 2b
       ld   a,e          ; 7b
       jp   c,L29f8      ; da f8 29
       push af           ; f5
       call L2b30        ; cd 30 2b
       ld   b,$00        ; 06 00
       call L2af9        ; cd f9 2a
       pop  af           ; f1
       jp   L29f8        ; c3 f8 29

; CURSOR-DOWN
L2ab5  call L29ec        ; Clear the cursor from the current position
       ld   e,a          ;
       push de           ;
       call L2b30        ; cd 30 2b
       pop  de           ; d1
       jp   nc,L29f2     ; d2 f2 29
       ld   b,e          ; 43
       call L2b02        ; cd 02 2b
       ld   a,e          ; 7b
       jp   c,L29f8      ; da f8 29
       push de           ; d5
       call L2b0b        ; cd 0b 2b
       pop  de           ; d1
       ld   b,e          ; 43
       call L2af9        ; cd f9 2a
       ld   a,e          ; 7b
       or   a            ; b7
       jp   L29f8        ; c3 f8 29

; CURSOR-LEFT
L2ad7  call L29ec        ; Remove cursor colour from the current position
       call L2b5b        ; cd 5b 2b
       jp   c,L29f8      ; da f8 29
       jp   L29f2        ; c3 f2 29

; CURSOR-RIGHT
L2ae3  call L29ec        ; Clear cursor attribute from current position
       call L2b78        ; Move cursor right
       jp   c,L29f8      ; Jump if completed successfully
       push af           ; f5
       call L2b0b        ; cd 0b 2b
       ld   b,$1f        ; 06 1f
       call L2bdf        ; cd df 2b
       pop  af           ; f1
       jp   L29f8        ; c3 f8 29
L2af9  push de           ; d5
       call L2bd4        ; cd d4 2b
       call nc,L2bdf     ; d4 df 2b
       pop  de           ; d1
       ret               ; c9

L2b02  push de           ; d5
       call L2bdf        ; cd df 2b
       call nc,L2bd4     ; d4 d4 2b
       pop  de           ; d1
       ret               ; c9

L2b0b  call L2c7c        ; cd 7c 2c
       jr   nc,L2b2f     ; 30 1f
       push bc           ; c5
       call L30b4        ; DE = start of editing line C
       ld   b,$00        ; 06 00
       call L2e41        ; cd 41 2e
       call nc,L2f80     ; d4 80 2f
       pop  bc           ; c1
       ld   hl,$f6f1     ; 21 f1 f6
       ld   a,(hl)       ; 7e
       cp   c            ; b9
       jr   c,L2b2d      ; 38 09
       push bc           ; c5
       call L166f        ; cd 6f 16
       pop  bc           ; c1
       ret  c            ; d8
       ld   a,c          ; 79
       or   a            ; b7
       ret  z            ; c8
L2b2d  dec  c            ; 0d
       scf               ; 37
L2b2f  ret               ; c9

L2b30  push bc           ;
       call L30b4        ; get position in editing area
       ld   b,$00        ; 06 00
       call L2e41        ; cd 41 2e
       pop  bc           ; c1
       jr   c,L2b3f      ; 38 03
       jp   L2f80        ; c3 80 2f
L2b3f  call L2c68        ; cd 68 2c
       jr   nc,L2b5a     ; 30 16
       ld   hl,$f6f1     ; 21 f1 f6
       inc  hl           ; 23
       ld   a,c          ; 79
       cp   (hl)         ; be
       jr   c,L2b58      ; 38 0c
       push bc           ; c5
       push hl           ; e5
       call L1639        ; cd 39 16
       pop  hl           ; e1
       pop  bc           ; c1
       ret  c            ; d8
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       cp   c            ; b9
       ret  z            ; c8
L2b58  inc  c            ; 0c
       scf               ; 37
L2b5a  ret               ; c9

L2b5b  ld   d,a          ; 57
       dec  b            ; 05
       jp   m,L2b66      ; fa 66 2b
       ld   e,b          ; 58
       call L2bdf        ; cd df 2b
       ld   a,e          ; 7b
       ret  c            ; d8
L2b66  push de           ; d5
       call L2b0b        ; cd 0b 2b
       pop  de           ; d1
       ld   a,e          ; 7b
       ret  nc           ; d0
       ld   b,$1f        ; 06 1f
       call L2bdf        ; cd df 2b
       ld   a,b          ; 78
       ret  c            ; d8
       ld   a,d          ; 7a
       ld   b,$00        ; 06 00
       ret               ; c9

; Move editing cursor right
L2b78  ld   d,a          ;
       inc  b            ; Move cursor to the right
       ld   a,$1f        ; 
       cp   b            ;
       jr   c,L2b85      ; If change of line...
       ld   e,b          ; 
       call L2bd4        ; update position
       ld   a,e          ; new horizontal position
       ret  c            ; return if everything OK
L2b85  dec  b            ; B = old cursor position
       push bc           ;
       push hl           ;
       ld   hl,$ec0d     ; ?
       bit  7,(hl)       ;
       jr   nz,L2bc0     ;
       call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       bit  1,a          ; cb 4f
       jr   nz,L2bc0     ; 20 25
       set  1,(hl)       ; cb ce
       res  3,(hl)       ; cb 9e
       ld   hl,$0023     ; 21 23 00
       add  hl,de        ; 19
       ex   de,hl        ; eb
       pop  hl           ; e1
       pop  bc           ; c1
       push af           ; f5
       call L2b30        ; cd 30 2b
       pop  af           ; f1
       call L30b4        ; DE = start of editing line C
       ld   hl,$0023     ; 21 23 00
       add  hl,de        ; 19
       ex   de,hl        ; eb
       res  0,a          ; cb 87
       set  3,a          ; cb df
       call L2ed3        ; cd d3 2e
       call L35f4        ; cd f4 35
       ld   a,b          ; 78
       scf               ; 37
       ret               ; c9

L2bc0  pop  hl           ;
       pop  bc           ;
       push de           ;
       call L2b30        ; cd 30 2b
       pop  de           ; d1
       ld   a,b          ; 78
       ret  nc           ; d0
       ld   b,$00        ; 06 00
       call L2bd4        ; cd d4 2b
       ld   a,b          ; 78
       ret  c            ; d8
       ld   a,e          ; 7b
       ld   b,$00        ; 06 00
       ret               ; c9

L2bd4  push de           ;
       push hl           ;
       call L30b4        ; Get position in editing area in page 7
       call L2e41        ; 
       jp   L2c65        ; retrieve registers and return

L2bdf  push de           ; d5
       push hl           ; e5
       call L30b4        ; DE = start of editing line C
       call L2e63        ; cd 63 2e
       jp   L2c65        ; c3 65 2c
L2bea  push de           ; d5
       push hl           ; e5
L2bec  call L2b5b        ; cd 5b 2b
       jr   nc,L2c07     ; 30 16
L2bf1  call L2a1a        ; Get character from current cursor position
       cp   $20          ; fe 20
       jr   z,L2bec      ; 28 f4
L2bf8  call L2b5b        ; cd 5b 2b
       jr   nc,L2c07     ; 30 0a
       call L2a1a        ; Get character from current cursor position
       cp   $20          ; fe 20
       jr   nz,L2bf8     ; 20 f4
       call L2b78        ; cd 78 2b
L2c07  jr   L2c65        ; 18 5c

; Called from WORD-RIGHT
L2c09  push de           ; Save HL and DE
       push hl           ;
L2c0b  call L2b78        ; Move cursor right
       jr   nc,L2c2b     ; At end-of-line (?)
       call L2a1a        ; Get character at cursor position
       cp   $20          ; ' '
L2c15  jr   nz,L2c0b     ; Loop back till we find a SPACE
L2c17  call L2b78        ; Move cursor right again
       jr   nc,L2c2b     ; At end-of-line (?)
       call L2e41        ; cd 41 2e
       jr   nc,L2c2b     ;
       call L2a1a        ; Get character from current cursor position
       cp   $20          ; ' '
       jr   z,L2c17      ; Loop back while we have SPACE
       scf               ; Indicate success
       jr   L2c65        ; and jump ahead to finish
L2c2b  call nc,L2b5b     ; d4 5b 2b
       or   a            ; b7
       jr   L2c65        ; 18 34

; called from START-OF-LINE
L2c31  push de           ; d5
       push hl           ; e5
L2c33  call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  0,(hl)       ; cb 46
       jr   nz,L2c45     ; 20 07
       call L2b0b        ; cd 0b 2b
       jr   c,L2c33      ; 38 f0
       jr   L2c65        ; 18 20
L2c45  ld   b,$00        ; 06 00
       call L2bd4        ; cd d4 2b
       jr   L2c65        ; 18 19

; called from END-OF-LINE
L2c4c  push de           ; d5
       push hl           ; e5
L2c4e  call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  3,(hl)       ; cb 5e
       jr   nz,L2c60     ; 20 07
       call L2b30        ; cd 30 2b
       jr   c,L2c4e      ; 38 f0
       jr   L2c65        ; 18 05
L2c60  ld   b,$1f        ; 06 1f
       call L2bdf        ; cd df 2b
L2c65  pop  hl           ; Restore HL, DE
       pop  de           ; 
       ret               ; and RETURN

L2c68  ld   a,($ec0d)    ;
       bit  3,a          ; Has the current line been altered?
       scf               ;
       ret  z            ; Return if it hasn't
       call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ;
       add  hl,de        ; HL points to flags for editing line C
       bit  3,(hl)       ; cb 5e
       scf               ; 37
       ret  z            ; c8
       jr   L2c8e        ; 18 12

L2c7c  ld   a,($ec0d)    ; 3a 0d ec
       bit  3,a          ; cb 5f
       scf               ; 37
       ret  z            ; c8
       call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  0,(hl)       ; cb 46
       scf               ; 37
L2c8d  ret  z            ; c8

L2c8e  ld   a,$02        ; 3e 02
L2c90  call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ;
       add  hl,de        ; HL points to flags for line C
       bit  0,(hl)       ; cb 46
       jr   nz,L2ca3     ; 20 08
       dec  c            ; 0d
       jp   p,L2c90      ; f2 90 2c
       ld   c,$00        ; 0e 00
       ld   a,$01        ; 3e 01
L2ca3  ld   hl,$ec00     ; 21 00 ec
       ld   de,$ec03     ; 11 03 ec
       or   $80          ; f6 80
       ld   (hl),a       ; 77
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  de           ; 13
       ld   a,$00        ; 3e 00
       ld   (hl),a       ; 77
       ld   (de),a       ; 12
       inc  hl           ; 23
       inc  de           ; 13
       ld   a,c          ; 79
       ld   (hl),a       ; 77
       ld   (de),a       ; 12
       ld   hl,$0000     ; 21 00 00
       ld   ($ec06),hl   ; 22 06 ec
       call L335f        ; cd 5f 33
       call L3c67        ; cd 67 3c
       push ix           ; dd e5
       call L1f20        ; cd 20 1f
       call L026b        ; cd 6b 02
       call L1f45        ; cd 45 1f
       pop  ix           ; dd e1
       ld   a,($5c3a)    ; 3a 3a 5c
       inc  a            ; 3c
       jr   nz,L2cef     ; 20 18
       ld   hl,$ec0d     ; 21 0d ec
       res  3,(hl)       ; cb 9e
       call L365e        ; cd 5e 36
       ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       call nz,L152f     ; c4 2f 15
       call L26fa        ; cd fa 26
       call L2a07        ; cd 07 2a
       scf               ; 37
       ret               ; c9

; After a syntax error...
L2cef  ld   hl,$ec00     ; 21 00 ec
       ld   de,$ec03     ; 11 03 ec
       ld   a,(de)       ; 1a
       res  7,a          ; cb bf
       ld   (hl),a       ; 77
       inc  hl           ; 23
       inc  de           ; 13
       ld   a,(de)       ; 1a
       ld   (hl),a       ; 77
       inc  hl           ; 23
       inc  de           ; 13
       ld   a,(de)       ; 1a
       ld   (hl),a       ; 77
       call L3c63        ; cd 63 3c
       jr   c,L2d0a      ; 38 04
       ld   bc,($ec06)   ; ed 4b 06 ec
L2d0a  ld   hl,($ec06)   ; 2a 06 ec
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       push af           ; f5
       push hl           ; e5
       call L2a07        ; Get editing position
       pop  hl           ; e1
       pop  af           ; f1
       jr   c,L2d2a      ; 38 11
       jr   z,L2d45      ; 28 2a
L2d1b  push hl           ; e5
       ld   a,b          ; 78
       call L2b5b        ; cd 5b 2b
       pop  hl           ; e1
       jr   nc,L2d45     ; 30 22
       dec  hl           ; 2b
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L2d1b     ; 20 f3
       jr   L2d45        ; 18 1b
L2d2a  push hl           ; e5
L2d2b  ld   hl,$ec0d     ; 21 0d ec
       res  7,(hl)       ; cb be
       pop  hl           ; e1
       ex   de,hl        ; eb
       ld   hl,$0000     ; 21 00 00
       or   a            ; b7
       sbc  hl,de        ; ed 52
L2d38  push hl           ; e5
       ld   a,b          ; 78
       call L2b78        ; cd 78 2b
       pop  hl           ; e1
       jr   nc,L2d45     ; 30 05
       dec  hl           ; 2b
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L2d38     ; 20 f3
L2d45  ld   hl,$ec0d     ;
       set  7,(hl)       ; Set "waiting for keypress" flag
       call L2a11        ; Store the editing position
       ld   a,$17        ; Store cursor attribute value?
       call L3a96        ;
       or   a            ;
       ret               ;

L2d54  ld   hl,$ec00     ; 21 00 ec
       bit  7,(hl)       ; cb 7e
       jr   z,L2d62      ; 28 07
       ld   hl,($ec06)   ; 2a 06 ec
       inc  hl           ; 23
       ld   ($ec06),hl   ; 22 06 ec
L2d62  ld   hl,$ec00     ; 21 00 ec
       ld   a,(hl)       ; 7e
       inc  hl           ; 23
       ld   b,(hl)       ; 46
       inc  hl           ; 23
       ld   c,(hl)       ; 4e
       push hl           ; e5
       and  $0f          ; e6 0f
       ld   hl,$2d85     ; 21 85 2d
       call L3fce        ; cd ce 3f
       ld   e,l          ; 5d
       pop  hl           ; e1
       jr   z,L2d79      ; 28 02
       ld   a,$0d        ; 3e 0d
L2d79  ld   (hl),c       ; 71
       dec  hl           ; 2b
       ld   (hl),b       ; 70
       dec  hl           ; 2b
       push af           ; f5
       ld   a,(hl)       ; 7e
       and  $f0          ; e6 f0
       or   e            ; b3
       ld   (hl),a       ; 77
       pop  af           ; f1
       ret               ; c9

; Some data table?
L2d85 .byte $03 ; number of entries
      .byte $02 ;
      .word $2dac
      .byte $04 ;
      .word $2de9
      .byte $01 ;
      .word $2d8f

L2d8f  call L32b7        ; cd b7 32
L2d92  call L2e0e        ; cd 0e 2e
       jr   nc,L2d9e     ; 30 07
       cp   $00          ; fe 00
       jr   z,L2d92      ; 28 f7
       ld   l,$01        ; 2e 01
       ret               ; c9
L2d9e  inc  c            ; 0c
       ld   b,$00        ; 06 00
       ld   hl,($f9db)   ; 2a db f9
       ld   a,c          ; 79
       cp   (hl)         ; be
       jr   c,L2d8f      ; 38 e7
       ld   b,$00        ; 06 00
       ld   c,$00        ; 0e 00
L2dac  push hl           ; e5
       ld   hl,$f6ee     ; 21 ee f6
       ld   a,(hl)       ; 7e
       cp   c            ; b9
       jr   nz,L2dbe     ; 20 0a
       inc  hl           ; 23
       ld   a,(hl)       ; 7e
       cp   b            ; b8
       jr   nz,L2dbe     ; 20 05
       ld   hl,$ec00     ; 21 00 ec
       res  7,(hl)       ; cb be
L2dbe  pop  hl           ; e1
L2dbf  call L30b4        ; DE = start of editing line C
       call L2e0e        ; cd 0e 2e
       jr   nc,L2dce     ; 30 07
       cp   $00          ; fe 00
       jr   z,L2dac      ; 28 e1
       ld   l,$02        ; 2e 02
       ret               ; c9
L2dce  ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  3,(hl)       ; cb 5e
       jr   z,L2ddb      ; 28 05
       ld   l,$08        ; 2e 08
       ld   a,$0d        ; 3e 0d
       ret               ; c9
L2ddb  ld   hl,$f6f3     ; 21 f3 f6
       inc  c            ; 0c
       ld   a,(hl)       ; 7e
       cp   c            ; b9
       ld   b,$00        ; 06 00
L2de3  jr   nc,L2dbf     ; 30 da
       ld   b,$00        ; 06 00
       ld   c,$01        ; 0e 01
L2de9  call L31c3        ; cd c3 31
L2dec  call L2e0e        ; cd 0e 2e
       jr   nc,L2df8     ; 30 07
       cp   $00          ; fe 00
       jr   z,L2dec      ; 28 f7
       ld   l,$04        ; 2e 04
       ret               ; c9
L2df8  ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  3,(hl)       ; cb 5e
       jr   nz,L2e09     ; 20 09
       inc  c            ; 0c
       ld   b,$00        ; 06 00
       ld   a,($f6f5)    ; 3a f5 f6
       cp   c            ; b9
       jr   nc,L2de9     ; 30 e0
L2e09  ld   l,$08        ; 2e 08
       ld   a,$0d        ; 3e 0d
       ret               ; c9
L2e0e  ld   a,$1f        ; 3e 1f
       cp   b            ; b8
       ccf               ; 3f
       ret  nc           ; d0
       ld   l,b          ; 68
       ld   h,$00        ; 26 00
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       inc  b            ; 04
       scf               ; 37
       ret               ; c9

L2e1b  .byte $01
L2e1c  .byte $14

L2e1d  .byte $01
L2e1e  .byte $01

L2e1f  ld   hl,$5c3c     ; TV FLAG
       res  0,(hl)       ;
       ld   hl,$2e1b     ;
       ld   de,$ec15     ;
       jp   L3fba        ; Copy one byte from L2e1c to $EC15

L2e2d  ld   hl,$5c3c     ; TV FLAG
       set  0,(hl)       ;
       ld   bc,$0000     ;
       call L372b        ; PRINT AT 0,0;
       ld   hl,$2e1d     ;
       ld   de,$ec15     ;
       jp   L3fba        ; Copy one byte from L2e1e to EC15

; Get the editing address of character B in the line stored in memory at DE
; or something
L2e41  ld   h,$00        ;
       ld   l,b          ; horizontal position
       add  hl,de        ; HL = character in editing area in EC16...
       ld   a,(hl)       ; 
       cp   $00          ; end-of-line or past the end-of-line?
       scf               ; 
       ret  nz           ; Return straightaway if this character is part of
                         ; the editing line
       ld   a,b          ; Otherwise check the horizontal position
       or   a            ;
       jr   z,L2e5b      ; if zero, then jump ahead
       push hl           ; Otherwise check the
       dec  hl           ; preceding byte
       ld   a,(hl)       ; and if it is non-zero
       cp   $00          ; then return with
       scf               ; HL pointing to the
       pop  hl           ; first zero byte
       ret  nz           ; 
L2e56  ld   a,(hl)       ; get the current byte
       cp   $00          ; end-of-line or past the end-of-line?
       scf               ; 
       ret  nz           ; return with carry set if so
L2e5b  inc  hl           ; increment pointer to editing space
       inc  b            ; increment horizontal position
       ld   a,b          ; 
       cp   $1f          ; check against 31 (max.horiz.cursor position)
       jr   c,L2e56      ; jump back if we haven't reached the end of the line
       ret               ;

L2e63  ld   h,$00        ; 26 00
       ld   l,b          ; 68
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       cp   $00          ; fe 00
       scf               ; 37
       ret  nz           ; c0
L2e6c  ld   a,(hl)       ; 7e
       cp   $00          ; fe 00
       jr   nz,L2e78     ; 20 07
       ld   a,b          ; 78
       or   a            ; b7
       ret  z            ; c8
       dec  hl           ; 2b
       dec  b            ; 05
       jr   L2e6c        ; 18 f4
L2e78  inc  b            ; 04
       scf               ; 37
       ret               ; c9
       ld   h,$00        ; 26 00
       ld   l,b          ; 68
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       ret               ; c9

L2e81  ld   hl,$ec0d     ; 21 0d ec
       or   a            ; b7
       bit  0,(hl)       ; cb 46
       ret  nz           ; c0
       push bc           ; c5
       push af           ; f5
       call L30b4        ; DE = start of editing line C
       pop  af           ; f1
L2e8e  call L16ac        ; cd ac 16
       push af           ; f5
       ex   de,hl        ; eb
       call L3604        ; cd 04 36
       ex   de,hl        ; eb
       pop  af           ; f1
       ccf               ; 3f
       jr   z,L2ecc      ; 28 31
       push af           ; f5
       ld   b,$00        ; 06 00
       inc  c            ; 0c
       ld   a,($ec15)    ; 3a 15 ec
       cp   c            ; b9
       jr   c,L2ec8      ; 38 23
       ld   a,(hl)       ; 7e
       ld   e,a          ; 5f
       and  $d7          ; e6 d7
       cp   (hl)         ; be
       ld   (hl),a       ; 77
       ld   a,e          ; 7b
       set  1,(hl)       ; cb ce
       push af           ; f5
       call L30b4        ; DE = start of editing line C
       pop  af           ; f1
       jr   z,L2ec2      ; 28 0d
       res  0,a          ; cb 87
       call L2ed3        ; cd d3 2e
       jr   nc,L2ecc     ; 30 10
       call L35f4        ; cd f4 35
       pop  af           ; f1
       jr   L2e8e        ; 18 cc
L2ec2  call L2e41        ; cd 41 2e
       pop  af           ; f1
       jr   L2e8e        ; 18 c6
L2ec8  pop  af           ; f1
       call L316e        ; cd 6e 31
L2ecc  pop  bc           ; c1
       ret               ; c9

L2ece  call L30b4        ; DE = start of editing line C
       ld   a,$09        ; 3e 09
L2ed3  push bc           ; c5
       push de           ; d5
       ld   b,c          ; 41
       ld   hl,$2eef     ; 21 ef 2e
       ld   c,a          ; 4f
       push bc           ; c5
       call L1675        ; cd 75 16
       pop  bc           ; c1
       ld   a,c          ; 79
       jr   nc,L2eec     ; 30 0a
       ld   c,b          ; 48
       call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   (hl),a       ; 77
       scf               ; 37
L2eec  pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L2eef  nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       nop               ; 00
       add  hl,bc        ; 09
       nop               ; 00
       nop               ; 00

L2f12  push bc           ; BC=cursor position
       call L30b4        ; DE=start of current line
       push bc           ; Stack cursor position again
L2f17  ld   hl,$0020     ; 
       add  hl,de        ; HL points to 3 bytes of flags for this line
       bit  1,(hl)       ; Is this line complete? [???]
       ld   a,$00        ;
       jr   z,L2f31      ; Jump ahead if not
       inc  c            ; C=next line down
       ld   hl,$0023     ; 
       add  hl,de        ;
       ex   de,hl        ; DE points to flags for next line;
                         ; HL points to flags for current line
       ld   a,($ec15)    ; Number of editing lines available
       cp   c            ; 
       jr   nc,L2f17     ; Jump back if within screen range
       dec  c            ;
       call L31c9        ; cd c9 31
L2f31  pop  hl           ; Retrieve initial cursor position into HL
L2f32  push hl           ; Stack it again
       call L30b4        ; Get start of editing line C into DE
       pop  hl           ; HL=initial cursor position
       ld   b,a          ; 
       ld   a,c          ;
       cp   l            ;
       ld   a,b          ;
       push af           ;
       jr   nz,L2f41     ;
       ld   b,h          ;
       jr   L2f4a        ;
L2f41  push af           ;
       push hl           ;
       ld   b,$00        ;
       call L2e41        ;
       pop  hl           ;
       pop  af           ;
L2f4a  push hl           ;
       ld   hl,$f6f4     ;
       set  0,(hl)       ;
       jr   z,L2f54      ;
       res  0,(hl)       ;
L2f54  call L16c1        ;
       push af           ;
       push bc           ;
       push de           ;
       ld   hl,$f6f4     ;
       bit  0,(hl)       ;
       jr   nz,L2f6f     ;
       ld   b,$00        ;
       call L2bd4        ;
       jr   c,L2f6f      ;
       call L2f80        ;
       pop  de           ;
       pop  bc           ;
       jr   L2f74        ;
L2f6f  pop  hl           ;
       pop  bc           ;
       call L3604        ;
L2f74  pop  af           ;
       dec  c            ;
       ld   b,a          ;
       pop  hl           ;
       pop  af           ;
       ld   a,b          ;
       jp   nz,L2f32     ;
       scf               ;
       pop  bc           ;
       ret               ;

L2f80  ld   hl,$0020     ; (Assuming entered with HL pointing to start of line)
       add  hl,de        ; Point HL to flags for this line
       ld   a,(hl)       ;
       bit  0,(hl)       ; Is this the first line (?)
       jr   nz,L2fb2     ; Jump ahead if it is (?)
       push af           ;
       push bc           ;
       ld   a,c          ;
       or   a            ;
       jr   nz,L2fa4     ;
       push bc           ;
       ld   hl,($fc9a)   ;
       call L334a        ;
       ld   ($fc9a),hl   ;
       ld   a,($f9db)    ;
       ld   c,a          ;
       dec  c            ;
       call L32b7        ;
       pop  bc           ;
       jr   L2fa8        ;
L2fa4  dec  c            ;
       call L30b4        ;
L2fa8  pop  bc           ;
       pop  af           ;
       ld   hl,$0020     ;
       add  hl,de        ;
       res  1,(hl)       ;
       or   (hl)         ;
       ld   (hl),a       ;
L2fb2  ld   b,c          ;
       call L30b4        ;
       call L30df        ;
       jp   L1648        ;

; DELETE-WORD-LEFT
L2fbc  call L3084        ; cd 84 30
L2fbf  push hl           ; e5
       call L3095        ; cd 95 30
       jr   z,L2ff7      ; 28 32
       call L2b5b        ; cd 5b 2b
       pop  hl           ; e1
       jr   nc,L2ff8     ; 30 2d
       call L2a1a        ; Get character from current cursor position
       push af           ; f5
       push hl           ; e5
       call L2f12        ; cd 12 2f
       pop  hl           ; e1
       pop  af           ; f1
       cp   $20          ; fe 20
       jr   z,L2fbf      ; 28 e6
L2fd9  push hl           ; e5
       call L3095        ; cd 95 30
       jr   z,L2ff7      ; 28 18
       call L2b5b        ; cd 5b 2b
       pop  hl           ; e1
       jr   nc,L2ff8     ; 30 13
       call L2a1a        ; Get character from current cursor position
       cp   $20          ; fe 20
       jr   z,L2ff3      ; 28 07
       push hl           ; e5
       call L2f12        ; cd 12 2f
       pop  hl           ; e1
       jr   L2fd9        ; 18 e6
L2ff3  push hl           ; e5
       call L2b78        ; cd 78 2b
L2ff7  pop  hl           ; e1
L2ff8  ld   a,b          ; 78
       push af           ; f5
       push hl           ; e5
       ld   hl,$eef5     ; 21 f5 ee
       res  2,(hl)       ; cb 96
       ld   a,($ec15)    ; 3a 15 ec  This will end up being used as the
                         ;            alternate cursor column (bad)
       push bc           ;
       ld   b,$00        ;
       ld   c,a          ; BC = size of editing area
       cp   a            ; Set Z
       call L1605        ; 
       pop  bc           ;
       ld   hl,$ec0d     ; Editing flags
       set  3,(hl)       ; Indicate current line has been altered
       pop  hl           ; 
       call L29f8        ; Store new cursor position and print new cursor
       pop  af           ;
       ret               ;

; DELETE-WORD-RIGHT
L3017  call L3084        ; cd 84 30
L301a  push hl           ;
       call L2a1a        ; Get current character
       pop  hl           ;
       cp   $00          ; 00 means end
       scf               ;
       jr   z,L2ff8      ; so finish
       push af           ; f5
       push hl           ; e5
       call L2f12        ; cd 12 2f
       pop  hl           ; e1
       pop  af           ; f1
       cp   $20          ; Is it a space?
       jr   nz,L301a     ; Jump back if not
L302f  call L2a1a        ; Get current character
       cp   $20          ; Is it a space?
       scf               ; 
       jr   nz,L2ff8     ; 20 c1
       push hl           ; e5
       call L2f12        ; cd 12 2f
       pop  hl           ; e1
       jr   L302f        ; 18 f1

; DELETE-TO-START-OF-LINE
L303e  call L3084        ; cd 84 30
L3041  push hl           ; e5
       call L30b4        ; cd b4 30
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       bit  0,(hl)       ; cb 46
       jr   nz,L3059     ; 20 0c
       call L2b5b        ; cd 5b 2b
       jr   nc,L306d     ; 30 1b
       call L2f12        ; cd 12 2f
       pop  hl           ; e1
       jr   L3041        ; 18 e9
       push hl           ; e5
L3059  ld   a,b          ; 78
       cp   $00          ; fe 00
       jr   z,L306d      ; 28 0f
       dec  b            ; 05
       call L2a1a        ; Get character from current cursor position
       inc  b            ; 04
       cp   $00          ; fe 00
       jr   z,L306d      ; 28 06
       dec  b            ; 05
       call L2f12        ; cd 12 2f
       jr   L3059        ; 18 ec
L306d  pop  hl           ; e1
L306e  scf               ; 37
       jp   L2ff8        ; c3 f8 2f

; DELETE-TO-END-OF-LINE
L3072  call L3084        ; Set up ???
L3075  call L2a1a        ; Get character from current cursor position
       cp   $00          ; Is it 00?
       scf               ; 
       jr   z,L306e      ; Finished if so, so jump to tidy up
       push hl           ; Otherwise...
       call L2f12        ; ... delete the character...
       pop  hl           ; 
       jr   L3075        ; ...and jump back to continue deleting

L3084  ld   hl,$ec0d     ; Editing flags
       res  0,(hl)       ; Reset ??? flag
       call L29ec        ; Remove cursor square from current position
       ld   hl,$eef5     ; 21 f5 ee
       set  2,(hl)       ; cb d6
       ld   hl,$f6f1     ; 21 f1 f6
       ret               ; c9

L3095  call L30b4        ; DE = start of editing line C
       ld   hl,$0020     ; 
       add  hl,de        ; HL points to 3 bytes of flags for this line
       bit  0,(hl)       ; ???
       jr   z,L30ae      ; 
       ld   a,b          ; 78
       cp   $00          ; fe 00
       jr   z,L30b2      ; 28 0d
       dec  b            ; 05
       call L2a1a        ; Get character from current cursor position
L30a9  inc  b            ; 04
       cp   $00          ; fe 00
       jr   z,L30b2      ; 28 04
L30ae  ld   a,$01        ; 3e 01
       or   a            ; b7
       ret               ; c9
L30b2  xor  a            ; af
       ret               ; c9

; Calculate DE = EC16 + $0023 * C (start of editing line C)
L30b4  ld   hl,$ec16     ;
L30b7  push af           ;
       ld   a,c          ;
       ld   de,$0023     ;
L30bc  or   a            ;
       jr   z,L30c3      ;
       add  hl,de        ;
       dec  a            ;
       jr   L30bc        ;
L30c3  ex   de,hl        ; DE = $EC16 + $0023 * C
       pop  af           ;
       ret               ;

; Calculate DE = EC16 + $0023 * C + B (character B in editing line C)
       push de           ;
       call L30b4        ; Calculate DE = EC16 + L0023 * C
       ld   h,$00        ; 
       ld   l,b          ; 
       add  hl,de        ; DE = EC16 + $0023 * C + B
       pop  de           ; 
       ret               ; 

L30d0  .byte $05
       .byte $00,$00,$00,$f8,$f6

; Copy 5 bytes from 30d1 to f6f5
L30d6  ld   hl,$30d0
       ld   de,$f6f5
       jp   L3fba

L30df  push bc           ; c5
       push de           ; d5
       ld   hl,$f6f5     ; 21 f5 f6
       push hl           ; e5
       ld   a,(hl)       ; 7e
       or   a            ; b7
       jr   nz,L3101     ; 20 18
       push hl           ; e5
       call L335f        ; cd 5f 33
       ld   hl,($f9d7)   ; 2a d7 f9
       call L3352        ; cd 52 33
       jr   nc,L30f8     ; 30 03
       ld   ($f9d7),hl   ; 22 d7 f9
L30f8  ld   b,h          ; 44
       ld   c,l          ; 4d
       pop  hl           ; e1
       call L32d6        ; cd d6 32
       dec  a            ; 3d
       jr   L3116        ; 18 15
L3101  ld   hl,$ec0d     ; 21 0d ec
       res  0,(hl)       ; cb 86
       ld   hl,$f6f8     ; temporary copy of line being edited
       ld   d,h          ; 54
       ld   e,l          ; 5d
       ld   bc,$0023     ; 01 23 00
       add  hl,bc        ; 09
       ld   bc,$02bc     ; 700d
       ldir              ; ed b0
       dec  a            ; 3d
       scf               ; 37
L3116  pop  de           ; d1
       ld   (de),a       ; 12
       ld   hl,$f6f8     ; 21 f8 f6
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L311e  push bc           ; c5
       push de           ; d5
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       cpl               ; 2f
       and  $11          ; e6 11
       jr   nz,L313f     ; 20 15
       push hl           ; e5
       push de           ; d5
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       inc  hl           ; 23
       ld   e,(hl)       ; 5e
       push de           ; d5
       call L335f        ; cd 5f 33
       pop  hl           ; e1
       call L334a        ; cd 4a 33
       jr   nc,L313d     ; 30 03
       ld   ($f9d7),hl   ; 22 d7 f9
L313d  pop  de           ; d1
       pop  hl           ; e1
L313f  bit  0,(hl)       ; cb 46
       ld   hl,$f6f5     ; 21 f5 f6
       push hl           ; e5
       jr   z,L314c      ; 28 05
       ld   a,$00        ; 3e 00
       scf               ; 37
       jr   L3116        ; 18 ca
L314c  ld   a,(hl)       ; 7e
       cp   $14          ; fe 14
       jr   z,L3116      ; 28 c5
       ld   bc,$0023     ; 01 23 00
       ld   hl,$f6f8     ; 21 f8 f6
       ex   de,hl        ; eb
       ldir              ; ed b0
       ld   hl,$f9d6     ; 21 d6 f9
       ld   d,h          ; 54
       ld   e,l          ; 5d
       ld   bc,$0023     ; 01 23 00
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       ld   bc,$02bc     ; 01 bc 02
       lddr              ; ed b8
       inc  a            ; 3c
       scf               ; 37
       jr   L3116        ; 18 a8

L316e  push bc           ; c5
       push de           ; d5
       push af           ; f5
       ld   b,$00        ; 06 00
       ld   c,$01        ; 0e 01
       push hl           ; e5
       call L31c3        ; cd c3 31
       pop  hl           ; e1
       bit  3,(hl)       ; cb 5e
       res  3,(hl)       ; cb 9e
       jr   nz,L31a0     ; 20 20
L3180  call L2e41        ; cd 41 2e
       pop  af           ; f1
L3184  call L16ac        ; Insert character A into position B of line at DE
       jr   z,L31ba      ; Jump ahead if job completed
       push af           ; Otherwise stack the character which needs to be put into the next line
       ld   b,$00        ; B=new column
       inc  c            ; C=new row
       ld   a,c          ; 
       cp   $15          ; 
       jr   c,L31a0      ; Jump if row number > 21d
       dec  hl           ; Point to last character
       ld   a,(hl)       ; Get character
       inc  hl           ; 
       cp   $00          ; Was it 00?
       jr   z,L31a0      ; 28 07
       push hl           ; e5
       ld   hl,$ec0d     ; 21 0d ec
       set  0,(hl)       ; Set the mystery flag
       pop  hl           ; e1
L31a0  bit  1,(hl)       ; cb 4e
       set  1,(hl)       ; cb ce
       res  3,(hl)       ; cb 9e
       call L31c3        ; cd c3 31
       jr   nz,L3180     ; 20 d5
       push bc           ; c5
       push de           ; d5
       call L35e6        ; cd e6 35
       ld   (hl),$08     ; 36 08
       pop  de           ; d1
       pop  bc           ; c1
       call L35f4        ; cd f4 35
       pop  af           ; f1
       jr   L3184        ; 18 ca
L31ba  ld   a,c          ; 79
       ld   ($f6f5),a    ; 32 f5 f6
       set  3,(hl)       ; cb de
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

;  Calculate DE = F6F8 + $0023 * C
L31c3  ld   hl,$f6f8
       jp   L30b7

L31c9  push bc           ; c5
       push de           ; d5
       ld   hl,$ec0d     ; 21 0d ec
       res  0,(hl)       ; cb 86
       ld   a,($f6f5)    ; 3a f5 f6
       ld   c,a          ; 4f
       or   a            ; b7
       ld   a,$00        ; 3e 00
       jr   z,L321b      ; 28 42
L31d9  call L31c3        ; cd c3 31
       push af           ; f5
       ld   b,$00        ; 06 00
       call L2e41        ; cd 41 2e
       jr   nc,L31f2     ; 30 0e
       pop  af           ; f1
       call L16c1        ; cd c1 16
       push af           ; f5
       push bc           ; c5
       ld   b,$00        ; 06 00
       call L2e41        ; cd 41 2e
       pop  bc           ; c1
       jr   c,L3216      ; 38 24
L31f2  inc  hl           ; 23
       ld   a,(hl)       ; 7e
       push af           ; f5
       push bc           ; c5
       ld   a,c          ; 79
       cp   $01          ; fe 01
       jr   nz,L3204     ; 20 09
       ld   a,($ec15)    ; 3a 15 ec
       ld   c,a          ; 4f
       call L30b4        ; DE = start of editing line C
       jr   L3208        ; 18 04
L3204  dec  c            ; 0d
       call L31c3        ; cd c3 31
L3208  pop  bc           ; c1
       pop  af           ; f1
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       res  1,(hl)       ; cb 8e
       or   (hl)         ; b6
       ld   (hl),a       ; 77
       ld   hl,$f6f5     ; 21 f5 f6
       dec  (hl)         ; 35
L3216  pop  af           ; f1
       dec  c            ; 0d
       jr   nz,L31d9     ; 20 bf
       scf               ; 37
L321b  pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L321e .byte $03           ; number of bytes
L321f .byte $00, $de, $f9 ; data

; Copy 3 bytes from L321f to F9DB
L3222  ld   hl,$321e     ;
       ld   de,$f9db     ;
       jp   L3fba        ;

L322b  push bc           ; c5
       push de           ; d5
       ld   hl,$f9db     ; 21 db f9
       push hl           ; e5
       ld   a,(hl)       ; 7e
       or   a            ; b7
       jr   nz,L3253     ; 20 1e
       push hl           ; e5
       call L335f        ; cd 5f 33
       ld   hl,($fc9a)   ; 2a 9a fc
       call L334a        ; cd 4a 33
       jr   nc,L3244     ; 30 03
       ld   ($fc9a),hl   ; 22 9a fc
L3244  ld   b,h          ; 44
       ld   c,l          ; 4d
       pop  hl           ; e1
       inc  hl           ; 23
       inc  hl           ; 23
       inc  hl           ; 23
       jr   nc,L325d     ; 30 11
       call L32d6        ; cd d6 32
       dec  a            ; 3d
       ex   de,hl        ; eb
       jr   L325d        ; 18 0a
L3253  ld   hl,($f9dc)   ; 2a dc f9
       ld   bc,$0023     ; 01 23 00
       sbc  hl,bc        ; ed 42
       scf               ; 37
       dec  a            ; 3d
L325d  ex   de,hl        ; eb
       pop  hl           ; e1
       jr   nc,L3262     ; 30 01
       ld   (hl),a       ; 77
L3262  inc  hl           ; 23
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       ex   de,hl        ; eb
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L326a  push bc           ; c5
       push de           ; d5
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   a,(hl)       ; 7e
       cpl               ; 2f
       and  $11          ; e6 11
       jr   nz,L3282     ; 20 0c
       push de           ; d5
       push hl           ; e5
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       inc  hl           ; 23
       ld   e,(hl)       ; 5e
       ld   ($fc9a),de   ; ed 53 9a fc
       pop  hl           ; e1
       pop  de           ; d1
L3282  bit  3,(hl)       ; cb 5e
       ld   hl,$f9db     ; 21 db f9
       push hl           ; e5
       jr   z,L32a0      ; 28 16
       push hl           ; e5
       call L335f        ; cd 5f 33
       ld   hl,($fc9a)   ; 2a 9a fc
       call L3352        ; cd 52 33
       ld   ($fc9a),hl   ; 22 9a fc
       pop  hl           ; e1
       inc  hl           ; 23
       inc  hl           ; 23
       inc  hl           ; 23
       ld   a,$00        ; 3e 00
       scf               ; 37
       jr   L325d        ; 18 bd
L32a0  ld   a,(hl)       ; 7e
       cp   $14          ; fe 14
       jr   z,L32b3      ; 28 0e
       inc  a            ; 3c
       ld   hl,($f9dc)   ; 2a dc f9
       ld   bc,$0023     ; 01 23 00
       ex   de,hl        ; eb
       ldir              ; ed b0
       ex   de,hl        ; eb
       scf               ; 37
       jr   L325d        ; 18 aa
L32b3  pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9
L32b7  ld   hl,$f9de     ; 21 de f9
       jp   L30b7        ; c3 b7 30
L32bd  ex   af,af'       ; 08
       dec  c            ; 0d
       call z,L0135      ; cc 35 01
       jp   c,L1235      ; da 35 12
       ld   e,d          ; 5a
       inc  sp           ; 33
       inc  de           ; 13
       ld   e,d          ; 5a
       inc  sp           ; 33
       inc  d            ; 14
       ld   e,d          ; 5a
       inc  sp           ; 33
       dec  d            ; 15
       ld   e,d          ; 5a
       inc  sp           ; 33
       djnz L332c        ; 10 5a
       inc  sp           ; 33
       ld   de,$335a     ; 11 5a 33
L32d6  ld   d,h          ; 54
       ld   e,l          ; 5d
       inc  de           ; 13
       inc  de           ; 13
       inc  de           ; 13
       push de           ; d5
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   (hl),$01     ; 36 01
       inc  hl           ; 23
       ld   (hl),b       ; 70
       inc  hl           ; 23
       ld   (hl),c       ; 71
       ld   c,$01        ; 0e 01
       ld   b,$00        ; 06 00
L32ea  push bc           ; c5
       push de           ; d5
       ld   a,($ec0e)    ; 3a 0e ec
       cp   $04          ; Calculator mode?
       call nz,L3517     ; c4 17 35
       pop  de           ; d1
       pop  bc           ; c1
       jr   c,L3307      ; 38 0f
       ld   a,c          ; 79
       cp   $01          ; fe 01
       ld   a,$0d        ; 3e 0d
       jr   nz,L3307     ; 20 08
       ld   a,b          ; 78
       or   a            ; b7
       ld   a,$01        ; 3e 01
       jr   z,L3307      ; 28 02
       ld   a,$0d        ; 3e 0d
L3307  ld   hl,$32bd     ; 21 bd 32
       call L3fce        ; cd ce 3f
       jr   c,L332c      ; 38 1d
       jr   z,L32ea      ; 28 d9
       push af           ; f5
       ld   a,$1f        ; 3e 1f
       cp   b            ; b8
       jr   nc,L3326     ; 30 0f
       ld   a,$12        ; 3e 12
       call L3331        ; cd 31 33
       jr   c,L3323      ; 38 05
       pop  af           ; f1
       ld   a,$0d        ; 3e 0d
       jr   L3307        ; 18 e4
L3323  call L35f4        ; cd f4 35
L3326  pop  af           ; f1
       call L35c5        ; cd c5 35
       jr   L32ea        ; 18 be
L332c  pop  hl           ; e1
       ld   a,c          ; 79
       ret  z            ; c8
       scf               ; 37
       ret               ; c9
L3331  push af           ; f5
       call L35e6        ; cd e6 35
       pop  af           ; f1
       xor  (hl)         ; ae
       ld   (hl),a       ; 77
       ld   a,c          ; 79
       cp   $14          ; fe 14
       ret  nc           ; d0
       inc  c            ; 0c
       ld   hl,$0023     ; 21 23 00
       add  hl,de        ; 19
       ex   de,hl        ; eb
       ld   hl,$0020     ; 21 20 00
       add  hl,de        ; 19
       ld   (hl),$00     ; 36 00
       scf               ; 37
       ret               ; c9

L334a  call L34b6        ; cd b6 34
       ret  c            ; d8
       ld   hl,$0000     ; 21 00 00
       ret               ; c9

L3352  call L3430        ; cd 30 34
       ret  c            ; d8
       ld   hl,$0000     ; 21 00 00
       ret               ; c9

L335a  call L3517        ; cd 17 35
       ccf               ; 3f
       ret  nc           ; d0
L335f  ld   hl,$0000     ; 21 00 00
       ld   ($fc9f),hl   ; 22 9f fc
       ld   ($fca1),hl   ; 22 a1 fc
       ld   hl,$3374     ; Copy the code to be executed from the ROM to FCAE
       ld   de,$fcae     ;
       ld   bc,$00bc     ; 
       ldir              ;
       ret               ;

; Construct the string representation of a keyword in page 7.
; Does not do SPECTRUM and PLAY, these are dealt with elsewhere.
; This code is executed at FCAE in page 7.
; Depending on the value of A (which should be the ASCII code less $A5,
; eg "RND", the first (48K) keyword, has A=0), a different index into the
; token table is taken. This is to allow speedier lookup, as we never have
; to advance through more than 15 keywords to find the one we want.
L3374  di                ; (fcae)
       ld   bc,$7ffd     ;
       ld   d,$17        ;         ROM 1, RAM page 7
       out  (c),d        ;
       cp   $50          ;
       jr   nc,L33b1     ;
       cp   $40          ;
       jr   nc,L33aa     ;
       cp   $30          ;
       jr   nc,L33a3     ;
       cp   $20          ;
       jr   nc,L339c     ;
       cp   $10          ;
       jr   nc,L3395     ;
                         ;          Otherwise $00 <= A <= $0F
       ld   hl,$0096     ;          Token table entry "RND"
       jr   L33b6        ;

L3395  sub  $10          ;          For $10 <= A <= $1F
       ld   hl,$00cf     ;          Token table entry "ASN"
       jr   L33b6        ; 

L339c  sub  $20          ;          For $20 <= A <= $2F
       ld   hl,$0100     ;          Token table entry "OR"
       jr   L33b6        ;

L33a3  sub  $30          ;          For $30 <= A <= $3F
       ld   hl,$013e     ;          Token table entry "MERGE"
       jr   L33b6        ;

L33aa  sub  $40          ;          For $40 <= A <= $4F
       ld   hl,$018b     ;          Token table entry "RESTORE"
       jr   L33b6        ;

L33b1  sub  $50          ;          For A >= $50
       ld   hl,$01d4     ;          Token table entry "PRINT"

L33b6  ld   b,a          ; Take a copy
       or   a            ; If A=0 then no work to do
L33b8  jr   z,L33c3      ;
L33ba  ld   a,(hl)       ; 7e
       inc  hl           ; 23
       and  $80          ; e6 80
       jr   z,L33ba      ; 28 fa
       dec  b            ; 05
       jr   L33b8        ; 18 f5
L33c3  ld   de,$fca3     ; (fcfd) HL points to keyword. DE is where we will
       ld   ($fca1),de   ;        store the constructed keyword.
       ld   a,($fc9e)    ;
       or   a            ;
       ld   a,$00        ;
       ld   ($fc9e),a    ;
       jr   nz,L33d9     ;
       ld   a,$20        ; Print a leading space.
       ld   (de),a       ;
       inc  de           ;
L33d9  ld   a,(hl)       ;
       ld   b,a          ;
       inc  hl           ; 23
       ld   (de),a       ; 12
       inc  de           ; 13
       and  $80          ; e6 80
       jr   z,L33d9      ; 28 f7
       ld   a,b          ; 78
       and  $7f          ; e6 7f
       dec  de           ; 1b
       ld   (de),a       ; 12
       inc  de           ; 13
       ld   a,$a0        ; " " + end marker?
       ld   (de),a       ; 12
       ld   a,$07        ; 3e 07
       ld   bc,$7ffd     ; 01 fd 7f
       out  (c),a        ; ed 79
       ei                ; fb
       ret               ; c9

       di                ; f3
       ld   bc,$7ffd     ; 01 fd 7f
       ld   d,$17        ; 16 17
       out  (c),d        ; ed 51
       ld   hl,$0096     ; 21 96 00
       ld   b,$a5        ; 06 a5
L3401  ld   de,$fd74     ; text to match against
L3404  ld   a,(de)       ; 1a
       and  $7f          ; e6 7f
       cp   $61          ; fe 61
       ld   a,(de)       ; 1a
       jr   c,L340e      ; 38 02
       and  $df          ; e6 df
L340e  cp   (hl)         ; be
       jr   nz,L341a     ; 20 09
       inc  hl           ; 23
       inc  de           ; 13
       and  $80          ; e6 80
       jr   z,L3404      ; 28 ed
       scf               ; 37
       jr   L3426        ; 18 0c
L341a  inc  b            ; 04
       jr   z,L3425      ; 28 08
L341d  ld   a,(hl)       ; 7e
       and  $80          ; e6 80
       inc  hl           ; 23
       jr   z,L341d      ; 28 fa
       jr   L3401        ; 18 dc
L3425  or   a            ; b7
L3426  ld   a,b          ; 78
       ld   d,$07        ; 16 07
       ld   bc,$7ffd     ; 01 fd 7f
       out  (c),d        ; ed 51
       ei                ; fb
       ret               ; c9


; After inserting a line, or doing something which requires a re-list,
; this seems to be called with HL=0 then HL=each line number in turn,
; being called repeatedly with the last line number
; Called from 3352 (which is called from 30f0) every time 

                         ; called with HL=line number or 0, A=page
L3430  call L34ea        ; clear FCA1, FC9F
       or   a            ; 
       ld   ($fc9e),a    ; 
       call L1f20        ; page swap
       call L34f6        ; find line number
       jr   nc,L3491     ; jump if line number not found
       jr   nz,L344d     ; jump if line number not zero
       ld   a,b          ; BC = line number
       or   c            ; 
       jr   z,L344d      ; 
       call L34cf        ; move to next BASIC line
       call L34d9        ; test for end of program
       jr   nc,L3491     ; 
L344d  ld   d,(hl)       ; HL = start of line in BASIC
       inc  hl           ; 
       ld   e,(hl)       ; DE=line number
       call L1f45        ; page swap
       push de           ; 
       push hl           ; 
       push ix           ; 
       ld   ix,$fca3     ; IX is where the ASCII representation of the line
                         ; number will be stored
       ld   ($fca1),ix   ; 
       ex   de,hl        ; HL=line number
       ld   b,$00        ; 
       ld   de,$fc18     ;  -1000
       call L3495        ; 
       ld   de,$ff9c     ; -100
       call L3495        ; 
       ld   de,$fff6     ; -10
       call L3495        ; 
       ld   de,$ffff     ; -1
       call L3495        ; insert LD B,1 here to fix fault
       dec  ix           ; IX points to last ASCII digit
       ld   a,(ix+$00)   ;
       or   $80          ;
       ld   (ix+$00),a   ; Set bit 7
       pop  ix           ;
       pop  hl           ;
       pop  de           ;
       inc  hl           ; Increment HL ...
       inc  hl           ; ... past the stored line length...
       inc  hl           ; ... to point to the first character in the line proper
       ld   ($fc9f),hl   ; ... and store it.
       ex   de,hl        ;
       scf               ;
       ret               ;

L3491  call L1f45        ; cd 45 1f
       ret               ; c9

L3495  xor  a            ; A=counter
L3496  add  hl,de        ; Keep adding DE...
       inc  a            ; ... and incrementing the counter...
       jr   c,L3496      ; ... until there is no carry
       sbc  hl,de        ; Now subtract one...
       dec  a            ; ... decrement the counter...
       add  a,$30        ; ... convert to an ASCII digit...
       ld   (ix+$00),a   ; ... and store in (IX).
       cp   $30          ; Is it a zero?
       jr   nz,L34b1     ; Jump ahead if not
       ld   a,b          ; Get the "digit printed" flag
       or   a            ;
       jr   nz,L34b3     ; Jump ahead if we have already printed a digit
       ld   a,$00        ; Otherwise this is a leading zero, so
       ld   (ix+$00),a   ; store a zero byte to indicate 'nothing to print'
       jr   L34b3        ; and jump ahead
L34b1  ld   b,$01        ; Indicate "digit printed"
L34b3  inc  ix           ; Increment IX to point to next space
       ret               ; Return

L34b6  call L34ea        ; cd ea 34       clear (fca1) and (fc9f)
       or   a            ; b7
       ld   ($fc9e),a    ; 32 9e fc
       call L1f20        ; cd 20 1f       page swapping
       call L34f6        ; cd f6 34       find line no. in BASIC
       jr   nc,L3491     ; 30 cc          if not found
       ex   de,hl        ; eb
       ld   a,l          ; 7d
       or   h            ; b4
       scf               ; 37
       jp   nz,L344d     ; c2 4d 34
       ccf               ; 3f
       jr   L3491        ; 18 c2

L34cf  push hl           ; If HL points to the start of a BASIC line...
       inc  hl           ; ... then skip past the line number...
       inc  hl           ; 
       ld   e,(hl)       ; ... retrieve the line length into DE ...
       inc  hl           ; 
       ld   d,(hl)       ;
       inc  hl           ;
       add  hl,de        ; and increase HL by DE, pointing HL to the start
       pop  de           ; of the next line, and returning with DE pointing
       ret               ; to the start of the original line

; Test for the end of BASIC
L34d9  ld   a,(hl)       ; 7e
       and  $c0          ; e6 c0
       scf               ; 37
       ret  z            ; c8
       ccf               ; 3f
       ret               ; c9

; Test to see if the BASIC line starting at (HL) has line number BC
L34e0  ld   a,b          ;
       cp   (hl)         ;
       ret  nz           ;
       ld   a,c          ;
       inc  hl           ;
       cp   (hl)         ;
       dec  hl           ;
       ret  nz           ;
       scf               ;
L34e9  ret               ;

; Clear out the stores in (fc9f,a0) and (fca1,a2)
L34ea  push hl           ;
       ld   hl,$0000     ;
       ld   ($fca1),hl   ;
       ld   ($fc9f),hl   ;
       pop  hl           ;
       ret               ;

L34f6  push hl           ; 
       pop  bc           ; 
       ld   de,$0000     ; 
       ld   hl,($5c53)   ; start of BASIC
       call L34d9        ; test for end of BASIC
       ret  nc           ; return if at end of program
       call L34e0        ; compare (HL) and BC
       ret  c            ; return with carry set if line no. found
       ld   a,b          ; 
       or   c            ; 
       scf               ; 
       ret  z            ; return with carry set if BC=0
L350a  call L34cf        ; move to next BASIC line
       call L34d9        ; test for end of BASIC
       ret  nc           ; return if at end of program
       call L34e0        ; compare (HL) and BC
       jr   nc,L350a     ; return with carry set if line no. found
       ret               ; 

L3517  ld   hl,($fca1)   ; 2a a1 fc
       ld   a,l          ; 7d
       or   h            ; b4
       jr   z,L353c      ; 28 1e
       ld   a,(hl)       ; 7e
       inc  hl           ; 23
       cp   $a0          ; fe a0
       ld   b,a          ; 47
       ld   a,$00        ; 3e 00
       jr   nz,L3529     ; 20 02
       ld   a,$ff        ; 3e ff
L3529  ld   ($fc9e),a    ; 32 9e fc
       ld   a,b          ; 78
       bit  7,a          ; cb 7f
       jr   z,L3534      ; 28 03
       ld   hl,$0000     ; 21 00 00
L3534  ld   ($fca1),hl   ; 22 a1 fc
       and  $7f          ; e6 7f
       jp   L358f        ; c3 8f 35
L353c  ld   hl,($fc9f)   ; 2a 9f fc
       ld   a,l          ; 7d
       or   h            ; b4
       jp   z,L3591      ; ca 91 35
       call L1f20        ; cd 20 1f
L3547  ld   a,(hl)       ; 7e
       cp   $0e          ; Indicates floating-point representation
       jr   nz,L3554     ; 
       inc  hl           ; Skip over it
       inc  hl           ;
       inc  hl           ;
       inc  hl           ;
       inc  hl           ;
       inc  hl           ;
       jr   L3547        ; 18 f3
L3554  call L1f45        ; cd 45 1f
       inc  hl           ; 23
       ld   ($fc9f),hl   ; 22 9f fc
       cp   $a5          ; 'RND' (first 48K keyword)
       jr   c,L3567      ; 38 08
       sub  $a5          ; 'RND' (first 48K keyword)
       call $fcae        ; Construct representation
       jp   L3517        ; c3 17 35
L3567  cp   $a3          ; 'SPECTRUM' (first 128K keyword)
       jr   c,L357b      ; 38 10
       jr   nz,L3572     ; 20 05
       ld   hl,$3594     ; 21 94 35
       jr   L3575        ; 18 03
L3572  ld   hl,$359c     ; 21 9c 35
L3575  call $fcfd        ; cd fd fc
       jp   L3517        ; c3 17 35
L357b  push af           ; f5
       ld   a,$00        ; 3e 00
       ld   ($fc9e),a    ; 32 9e fc
       pop  af           ; f1
       cp   $0d          ; fe 0d
       jr   nz,L358f     ; 20 09
       ld   hl,$0000     ; 21 00 00
       ld   ($fca1),hl   ; 22 a1 fc
       ld   ($fc9f),hl   ; 22 9f fc
L358f  scf               ; 37
       ret               ; c9
L3591  scf               ; 37
       ccf               ; 3f
       ret               ; c9

; The following strings are terminated by having bit 7 set
L3594 .text "SPECTRU"
      .byte 'M'+$80
      .text "PLA"
      .byte 'Y'+$80
      .text "GOT"
      .byte 'O'+$80
      .text "GOSU"
      .byte 'B'+$80
      .text "DEFF"
      .byte 'N'+$80
      .text "OPEN"
      .byte '#'+$80
      .text "CLOSE"
      .byte '#'+$80
      
; L35b9  ld   (bc),a       ; 02
; L35ba  ld   bc,$2105     ; 01 05 21
; L35bd  cp   c            ; b9
; L35be  dec  (hl)         ; 35

L35b9  .byte $02,$01,$05

L35bc  ld hl,$35b9
       ld   de,$fd6a     ; 11 6a fd
       jp   L3fba        ; c3 ba 3f

L35c5  ld   l,b          ; 68
       ld   h,$00        ; 26 00
       add  hl,de        ; 19
       ld   (hl),a       ; 77
       inc  b            ; 04
       ret               ; c9

       call L35e6        ; cd e6 35
       ld   a,(hl)       ; 7e
       or   $18          ; f6 18
       ld   (hl),a       ; 77
       ld   hl,$fd6a     ; 21 6a fd
       set  0,(hl)       ; cb c6
       scf               ; 37
       ret               ; c9

       call L35e6        ; cd e6 35
       set  3,(hl)       ; cb de
       ld   hl,$fd6a     ; 21 6a fd
       set  0,(hl)       ; cb c6
       scf               ; 37
       ret               ; c9
L35e6  ld   l,b          ; 68
       ld   h,$00        ; 26 00
       add  hl,de        ; 19
       ld   a,$20        ; 3e 20
L35ec  cp   b            ; b8
       ret  z            ; c8
       ld   (hl),$00     ; 36 00
       inc  hl           ; 23
       inc  b            ; 04
       jr   L35ec        ; 18 f8
L35f4  ld   a,($fd6b)    ; 3a 6b fd
       ld   b,$00        ; 06 00
L35f9  ld   h,$00        ; 26 00
       ld   l,b          ; 68
       add  hl,de        ; 19
       ld   (hl),$00     ; 36 00
       inc  b            ; 04
       dec  a            ; 3d
       jr   nz,L35f9     ; 20 f6
       ret               ; c9

L3604  push bc           ; c5
       push de           ; d5
       push hl           ; e5
       push hl           ; e5
       ld   hl,$eef5     ; 21 f5 ee
       bit  2,(hl)       ; cb 56
       pop  hl           ; e1
       jr   nz,L3614     ; 20 04
       ld   b,c          ; 41
       call L3b1e        ; cd 1e 3b
L3614  pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L3618  push bc           ; c5
       push de           ; d5
       push hl           ; e5
       push hl           ; e5
       ld   hl,$eef5     ; 21 f5 ee
       bit  2,(hl)       ; cb 56
       pop  hl           ; e1
       jr   nz,L3628     ; 20 04
       ld   e,c          ; 59
       call L3abf        ; cd bf 3a
L3628  pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

L362c  push bc           ; c5
       push de           ; d5
       push hl           ; e5
       push hl           ; e5
       ld   hl,$eef5     ; 21 f5 ee
       bit  2,(hl)       ; cb 56
       pop  hl           ; e1
       jr   nz,L363c     ; 20 04
       ld   e,c          ; 59
       call L3ac6        ; cd c6 3a
L363c  pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       ret               ; c9

; Set cursor colour
L3640  push af           ; f5
       push bc           ; c5
       push de           ; d5
       push hl           ; e5
       ld   a,b          ; 78
       ld   b,c          ; 41
       ld   c,a          ; 4f
       call L3a9d        ; cd 9d 3a
       pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       pop  af           ; f1
       ret               ; c9

; Restore previous colours
L364f  push af           ; f5
       push bc           ; c5
       push de           ; d5
       push hl           ; e5
       ld   a,b          ; 78
       ld   b,c          ; 41
       ld   c,a          ; 4f
       call L3ab2        ; cd b2 3a
       pop  hl           ; e1
       pop  de           ; d1
       pop  bc           ; c1
       pop  af           ; f1
       ret               ; c9

L365e  ld   a,$00        ;
       ld   ($5c41),a    ; MODE
       ld   a,$02        ;
       ld   ($5c0a),a    ; REPPER
L3668  ld   hl,$5c3b     ; FLAGS
       ld   a,(hl)       ; 7e
       or   $0c          ; f6 0c
       ld   (hl),a       ; 77
       ld   hl,$ec0d     ; 21 0d ec
       bit  4,(hl)       ; cb 66
       ld   hl,$5b66     ; 21 66 5b
       jr   nz,L367c     ; 20 03
       res  0,(hl)       ; cb 86
       ret               ; c9
L367c  set  0,(hl)       ; cb c6
       ret               ; c9

L367f  push hl           ; 
L3680  ld   hl,$5c3b     ; FLAGS
L3683  bit  5,(hl)       ; 
       jr   z,L3683      ;
       res  5,(hl)       ;
       ld   a,($5c08)    ; LAST K
       ld   hl,$5c41     ; MODE
       res  0,(hl)       ;
       cp   $20          ;
       jr   nc,L36a2     ;
       cp   $10          ;
       jr   nc,L3680     ;
       cp   $06          ;
       jr   c,L3680      ;
       call L36a4        ;
       jr   nc,L3680     ;
L36a2  pop  hl           ;
       ret               ;
L36a4  rst  28h          ;
       .word $10db       ; KEY-M&CL
       ret               ;

; Print menu
L36a8  push hl           ; e5
       call L373b        ; cd 3b 37
       ld   hl,$5c3c     ; 21 3c 5c
       res  0,(hl)       ; cb 86
       pop  hl           ; e1
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       push hl           ; e5
       ld   hl,$37ec     ; 21 ec 37
       call L3733        ; cd 33 37
       pop  hl           ; e1
       call L3733        ; cd 33 37
       push hl           ; e5
       call L3822        ; cd 22 38
       ld   hl,$37fa     ; 21 fa 37
       call L3733        ; cd 33 37
       pop  hl           ; e1
       push de           ; d5
       ld   bc,$0807     ; 01 07 08
       call L372b        ; cd 2b 37
L36d1  push bc           ; c5
       ld   b,$0c        ; 06 0c
       ld   a,$20        ; 3e 20
       rst  10h          ; d7
L36d7  ld   a,(hl)       ; 7e
       inc  hl           ; 23
       cp   $80          ; fe 80
       jr   nc,L36e0     ; 30 03
       rst  10h          ; d7
       djnz L36d7        ; 10 f7
L36e0  and  $7f          ; e6 7f
       rst  10h          ; d7
L36e3  ld   a,$20        ; 3e 20
       rst  10h          ; d7
       djnz L36e3        ; 10 fb
       pop  bc           ; c1
       inc  b            ; 04
       call L372b        ; cd 2b 37
       dec  e            ; 1d
       jr   nz,L36d1     ; 20 e1
       ld   hl,$6f38     ; 21 38 6f
       pop  de           ; d1
       sla  e            ; cb 23
       sla  e            ; cb 23
       sla  e            ; cb 23
       ld   d,e          ; 53
       dec  d            ; 15
       ld   e,$6f        ; 1e 6f
       ld   bc,$ff00     ; 01 00 ff
       ld   a,d          ; 7a
       call L3719        ; cd 19 37
       ld   bc,$0001     ; 01 01 00
       ld   a,e          ; 7b
       call L3719        ; cd 19 37
       ld   bc,$0100     ; 01 00 01
       ld   a,d          ; 7a
       inc  a            ; 3c
       call L3719        ; cd 19 37
       xor  a            ; af
       call L37ca        ; cd ca 37
       ret               ; c9
L3719  push af           ; f5
       push hl           ; e5
       push de           ; d5
       push bc           ; c5
       ld   b,h          ; 44
       ld   c,l          ; 4d
       rst  28h          ; ef
      .word $22e9 ;PLOT-SUB +4
      pop  bc
      pop  de
       pop  hl           ; e1
       pop  af           ; f1
       add  hl,bc        ; 09
       dec  a            ; 3d
       jr   nz,L3719     ; 20 ef
       ret               ; c9

; Print "AT B,C" characters
L372b  ld   a,$16        ; 3e 16
       rst  10h          ; d7
       ld   a,b          ; 78
       rst  10h          ; d7
       ld   a,c          ; 79
       rst  10h          ; d7
       ret               ; c9

; Print characters until $ff
L3733  ld   a,(hl)       ; 7e
       inc  hl           ; 23
       cp   $ff          ; fe ff
       ret  z            ; c8
       rst  10h          ; d7
       jr   L3733        ; 18 f8

L373b  scf               ; Start here to transfer data from 5c3c to eef6
       jr   L373f        ;
L373e  and  a            ; or from here to transfer from eef6 to 5c3c
L373f  ld   de,$eef6     ; 
       ld   hl,$5c3c     ;
       jr   c,L3748      ;
       ex   de,hl        ; Exchange DE, HL if carry clear
L3748  ldi               ; Transfer a single byte
       jr   c,L374d      ; Flip registers
       ex   de,hl        ;   if carry clear
L374d  ld   hl,$5c7d     ; (DE=eef7 now)
       jr   c,L3753      ; Flip registers
       ex   de,hl        ;   if carry clear
L3753  ld   bc,$0014     ; Copy 20d bytes
       ldir              ;
       jr   c,L375b      ; Flip register
       ex   de,hl        ;   if carry clear
L375b  ex   af,af'       ; 
       ld   bc,$0707     ; b=07, c=07
                         ; [ don't understand the next bit, seems to just set
                         ;   b=$11, c=$1a (unless (ix+$01) changes in the middle) ]
       call L3b94        ; b=$11-(IX+$01), c=$1a
       ld   a,(ix+$01)   ; 
       add  a,b          ;
       ld   b,a          ; b=$11
       ld   a,$0c        ;
L3769  push bc           ;
       push af           ;
       push de           ;
       rst  28h          ;
       .word $0e9b       ; CL-ADDR; HL= display address of line B
       ld   bc,$0007     ;
       add  hl,bc        ; HL=display address of byte 7 of line B
       pop  de           ; 
       call L377e        ; cd 7e 37
       pop  af           ; f1
       pop  bc           ; c1
       dec  b            ; 05
       dec  a            ; 3d
       jr   nz,L3769     ; 20 ec
       ret               ; c9

L377e  ld   bc,$080e     ; 01 0e 08
L3781  push bc           ; c5
       ld   b,$00        ; 06 00
       push hl           ; e5
       ex   af,af'       ; 08
       jr   c,L3789      ; 38 01
       ex   de,hl        ; eb
L3789  ldir              ; ed b0
       jr   c,L378e      ; 38 01
       ex   de,hl        ; eb
L378e  ex   af,af'       ; 08
       pop  hl           ; e1
       inc  h            ; 24
       pop  bc           ; c1
       djnz L3781        ; 10 ed
       push bc           ; c5
       push de           ; d5
       rst  28h          ; ef
       .word $0e88 ;CL-ATTR
       ex   de,hl
       pop  de           ; d1
       pop  bc           ; c1
       ex   af,af'       ; 08
       jr   c,L37a0      ; 38 01
       ex   de,hl        ; eb
L37a0  ldir              ; ed b0
       jr   c,L37a5      ; 38 01
       ex   de,hl        ; eb
L37a5  ex   af,af'       ; 08
       ret               ; c9
L37a7  call L37ca        ; cd ca 37
       dec  a            ; 3d
       jp   p,L37b1      ; f2 b1 37
       ld   a,(hl)       ; 7e
       dec  a            ; 3d
       dec  a            ; 3d
L37b1  call L37ca        ; cd ca 37
       scf               ; 37
       ret               ; c9
L37b6  push de           ; d5
       call L37ca        ; cd ca 37
       inc  a            ; 3c
       ld   d,a          ; 57
       ld   a,(hl)       ; 7e
       dec  a            ; 3d
       dec  a            ; 3d
       cp   d            ; ba
       ld   a,d          ; 7a
       jp   p,L37c5      ; f2 c5 37
       xor  a            ; af
L37c5  call L37ca        ; cd ca 37
       pop  de           ; d1
       ret               ; c9

; Set the attributes on the menu. A=menu option
L37ca  push af           ;
       push hl           ;
       push de           ;
       ld   hl,$5907     ; First attribute byte
       ld   de,$0020     ; The increment for each row
       and  a            ; 
       jr   z,L37da      ; Jump ahead if nothing to do
L37d6  add  hl,de        ; Otherwise increase HL
       dec  a            ; for each row
       jr   nz,L37d6     ; 
L37da  ld   a,$78        ; Change the attribute between
       cp   (hl)         ; $68 and $78
       jr   nz,L37e1     ; 
       ld   a,$68        ; 
L37e1  ld   d,$0e        ; No.of bytes to change
L37e3  ld   (hl),a       ;
       inc  hl           ;
       dec  d            ;
       jr   nz,L37e3     ;
       pop  de           ; Restore registers
       pop  hl           ;
       pop  af           ;
       ret               ;

L37ec .byte $16,$07,$07  ; AT 7,7;
      .byte $15,$00      ; OVER 0;
      .byte $14,$00      ; INVERSE 0;
      .byte $10,$07      ; INK 7;
      .byte $11,00       ; PAPER 0;
      .byte $13,$01      ; BRIGHT 1;
      .byte $ff
      
L37fa .byte $11,$00
      .text " "
      .byte $11,$07
      .byte $10,$00
      .byte $ff

; Bit-patterns for the Sinclair stripes on the menus
L3802  .byte $01,$03,$07,$0f,$1f,$3f,$7f,$ff
       .byte $fe,$fc,$f8,$f0,$e0,$c0,$80,$00

L3812  .byte $10,$02,$20
       .byte $11,$06,"!"
       .byte $10,$04,$20
       .byte $11,$05,"!"
       .byte $10,$00,$20,$ff

; Print the Sinclair stripes on the menu
L3822  push bc           ;
       push de           ;
       push hl           ;
       ld   hl,$3802     ; Graphics bit-patterns
       ld   de,$5b98     ;
       ld   bc,$0010     ;
       ldir              ;
       ld   hl,($5c36)   ; CHARS
       push hl           ;
       ld   hl,$5a98     ; Set CHARS to point to
       ld   ($5c36),hl   ; new graphics temporarily
       ld   hl,L3812     ; String to print
       call L3733        ; 
       pop  hl           ; Restore CHARS
       ld   ($5c36),hl   ;
       pop  hl           ;
       pop  de           ;
       pop  bc           ;
       ret               ;

L3848  ld   hl,$2769     ; "128 BASIC" option from main menu
       jr   L385a        ;

L384d  ld   hl,$2772     ; "Calculator" option from main menu
       jr   L385a        ;

L3852  ld   hl,$275e     ; "Tape Loader" option
       jr   L385a        ;

L3857  ld   hl,L2784     ; "Tape Tester" option

L385a  push hl           ; Address in memory of the text of the selected menu item
       call L3881        ; cd 81 38
       ld   hl,$5aa0     ; 21 a0 5a
       ld   b,$20        ; 06 20
       ld   a,$40        ; 3e 40
L3865  ld   (hl),a       ; 77
       inc  hl           ; 23
       djnz L3865        ; 10 fc
       ld   hl,$37ec     ; 21 ec 37
       call L3733        ; cd 33 37
       ld   bc,$1500     ; 01 00 15
       call L372b        ; cd 2b 37
       pop  de           ; d1
       call L057d        ; cd 7d 05
       ld   c,$1a        ; 0e 1a
       call L372b        ; cd 2b 37
       jp   L3822        ; c3 22 38

L3881  ld   b,$15        ; 06 15
       ld   d,$17        ; 16 17
       jp   L3b5e        ; c3 5e 3b

; Renumber subroutine
L3888  call L1f20        ; get RAM page 0
       call L3a05        ; count # BASIC lines
       ld   a,d          ; 
       or   e            ; 
       jp   z,L39c0      ; nothing to do if no lines
       ld   hl,($5b96)   ; fetch the increment for renumber
       rst  28h          ; 
       .word $30A9       ; HL=HL*DE
       ex   de,hl
       ld   hl,($5b94)   ; starting line for renumber
       add  hl,de        ; hl = last line used
       ld   de,$2710     ; 10000d
       or   a            ; 
       sbc  hl,de        ; 
       jp   nc,L39c0     ; do nothing if the new last line would be > 10000
       ld   hl,($5c53)   ; (PROG)
L38aa  rst  28h          ; get next BASIC line
       .word $19B8       ; NEXT-ONE
       inc  hl           ; Advance past line number
       inc  hl           ;
       ld   ($5b92),hl   ; Store as pointer to BASIC line length
       inc  hl           ; Advance past line length
       inc  hl           ;
       ld   ($5b6b),de   ; Store as start of BASIC line
L38b8  ld   a,(hl)       ; Get character from line
       rst  28h          ; Advance past f-p number, if present
       .word $18b6       ; NUMBER
       cp   $0d          ; 'ENTER'
       jr   z,L38c5      ;
       call L390e        ; Test character
       jr   L38b8        ; Repeat until end of line
L38c5  ld   de,($5b6b)   ; DE points to start of BASIC line
       ld   hl,($5c4b)   ; VARS
       and  a            ;
       sbc  hl,de        ;
       ex   de,hl        ;
       jr   nz,L38aa     ; Jump back if more lines
       call L3a05        ; Count the number of BASIC lines
       ld   b,d          ;
       ld   c,e          ; Initialise BC to #BASIC lines
       ld   de,$0000     ; Initialise DE to 0000
       ld   hl,($5c53)   ; Initialise HL to (PROG), start of BASIC program
L38dd  push bc           ; BC=#of lines left to do
       push de           ; DE=index of current line
       push hl           ; HL=start of next BASIC line
       ld   hl,($5b96)   ; Renumber increment
       rst  28h          ; Calculate HL= (increment * line index)
       .word $30a9       ; HL=HL*DE
       ld   de,($5b94)   ; The renumber start line
       add  hl,de        ; HL=line number for BC'th line
       ex   de,hl        ; DE=line number for BC'th line
       pop  hl           ; HL=start of BASIC line
       ld   (hl),d       ; Store line number (DE) into (HL,HL+1)
       inc  hl           ;
       ld   (hl),e       ;
       inc  hl           ;
       ld   c,(hl)       ; Get BC=line length
       inc  hl           ;
       ld   b,(hl)       ;
       inc  hl           ;
       add  hl,bc        ; Move to next line
       pop  de           ;
       inc  de           ; Increment DE=line index
       pop  bc           ; Decrement BC=#lines left
       dec  bc           ;
       ld   a,b          ;
       or   c            ;
       jr   nz,L38dd     ; Jump back while more lines to do
       call L1f45        ;
       ld   ($5b92),bc   ;
       scf               ;
       ret               ;

; Tokens which are followed by line numbers
L3907 .byte $ca          ; LINE
      .byte $f0          ; LIST
      .byte $e1          ; LLIST
      .byte $ec          ; GOTO
      .byte $ed          ; GOSUB
      .byte $e5          ; RESTORE
      .byte $f7          ; RUN

L390e  inc  hl           ;
       ld   ($5b79),hl   ;
       ex   de,hl        ;
       ld   bc,$0007     ;
       ld   hl,$3907     ;
       cpir              ; Search for any of the tokens above
       ex   de,hl        ;
       ret  nz           ; Return if no match
       ld   c,$00        ;
L391f  ld   a,(hl)       ;
       cp   $20          ; ' '
       jr   z,L393f      ;
       rst  28h          ;
       .word $2d1b       ; test if A is numeric
       jr   nc,L393f     ;
       cp   $2e          ; '.'
       jr   z,L393f      ;
       cp   $0e          ; indicates floating-point representation
       jr   z,L3943      ;
       or   $20          ; f6 20
       cp   $65          ; fe 65
       jr   nz,L393b     ; 20 04
       ld   a,b          ; 78
       or   c            ; b1
       jr   nz,L393f     ; 20 04
L393b  ld   hl,($5b79)   ; 2a 79 5b
       ret               ; c9

L393f  inc  bc           ; 03
       inc  hl           ; 23
       jr   L391f        ; 18 dc

L3943  ld   ($5b71),bc   ; ed 43 71 5b
       push hl           ; e5
       rst  28h          ; ef
       .word $18B6 ; NUMBER advance past internal f-p representation, if present
       call L3a36
       ld   a,(hl)       ; 7e
       pop  hl           ; e1
       cp   $3a          ; fe 3a    ':'
       jr   z,L3957      ; 28 03
       cp   $0d          ; fe 0d    ENTER
       ret  nz           ; c0
L3957  inc  hl           ; 23
       rst  28h          ; ef
       .word $33b4 ; STACK-NUM
       rst  28h          ; ef
       .word $2da2 ; FP-TO-BC
       ld   h,b          ; 60
       ld   l,c          ; 69 
       rst  28h          ; ef
       .word $196E ; LINE-ADDR          ; HL points to line (or line after)
       jr   z,L396f      ; 28 0a       ; if exact line number match
       ld   a,(hl)       ; 7e
       cp   $80          ; fe 80       ; (bad) test for end of BASIC
       jr   nz,L396f     ; 20 05
       ld   hl,$270f     ; 21 0f 27    ; make line to 9999
       jr   L3980        ; 18 11
L396f  ld   ($5b77),hl   ; 22 77 5b
       call L3a0b        ; cd 0b 3a
       ld   hl,($5b96)   ; 2a 96 5b    ; line number increment
       rst  28h          ; ef
       .word $30A9 ; HL=HL*DE
       ld   de,($5B94)
       add  hl,de        ; 19
L3980  ld   de,$5b73     ; 11 73 5b
       push hl           ; e5
       call L3a3c        ; cd 3c 3a
       ld   e,b          ; 58
       inc  e            ; 1c
       ld   d,$00        ; 16 00
       push de           ; d5
       push hl           ; e5
       ld   l,e          ; 6b
       ld   h,$00        ; 26 00
       ld   bc,($5b71)   ; ed 4b 71 5b
       or   a            ; b7
       sbc  hl,bc        ; ed 42
       ld   ($5b71),hl   ; 22 71 5b
       jr   z,L39cf      ; 28 33
       jr   c,L39c5      ; 38 27
       ld   b,h          ; 44
       ld   c,l          ; 4d
       ld   hl,($5b79)   ; 2a 79 5b
       push hl           ; e5
       push de           ; d5
       ld   hl,($5c65)   ; 2a 65 5c
       add  hl,bc        ; 09
       jr   c,L39be      ; 38 13
       ex   de,hl        ; eb
       ld   hl,$0082     ; 21 82 00
       add  hl,de        ; 19
       jr   c,L39be      ; 38 0c
       sbc  hl,sp        ; ed 72
       ccf               ; 3f
       jr   c,L39be      ; 38 07
       pop  de           ; d1
       pop  hl           ; e1
       rst  28h          ; ef
       .word $1655 ; MAKE-ROOM
       jr L39cf
       pop de
       pop hl
L39c0  call L1f45        ; cd 45 1f
       and  a            ; a7
       ret               ; c9
L39c5  dec  bc           ; 0b
       dec  e            ; 1d
       jr   nz,L39c5     ; 20 fc
       ld   hl,($5b79)   ; 2a 79 5b
       rst  28h          ; ef
       .word $19e8 ; RECLAIM-2
L39cf  ld   de,($5b79)   ; ed 5b 79 5b
       pop  hl           ; e1
       pop  bc           ; c1
       ldir              ; ed b0
       ex   de,hl        ; eb
       ld   (hl),$0e     ; 36 0e
       pop  bc           ; c1
       inc  hl           ; 23
       push hl           ; e5
       rst  28h          ; ef
       .word $2d2b ;STACK-BC
       pop  de           ; d1
       ld   bc,$0005     ; 01 05 00
       ldir              ; ed b0
       ex   de,hl        ; eb
       push hl           ; e5
       ld   hl,($5b92)   ; 2a 92 5b
       push hl           ; e5
       ld   e,(hl)       ; 5e
       inc  hl           ; 23
       ld   d,(hl)       ; 56
       ld   hl,($5b71)   ; 2a 71 5b
       add  hl,de        ; 19
       ex   de,hl        ; eb
       pop  hl           ; e1
       ld   (hl),e       ; 73
       inc  hl           ; 23
       ld   (hl),d       ; 72
       ld   hl,($5b6b)   ; 2a 6b 5b
       ld   de,($5b71)   ; ed 5b 71 5b
       add  hl,de        ; 19
       ld   ($5b6b),hl   ; 22 6b 5b
       pop  hl           ; e1
       ret               ; c9

; Count the number of BASIC lines, returning the result in DE
L3a05  ld   hl,($5c4b)   ; (VARS)
       ld   ($5b77),hl   ; 
L3a0b  ld   hl,($5c53)   ; (PROG)
       ld   de,($5b77)   ; 
       or   a            ; 
       sbc  hl,de        ; 
       jr   z,L3a31      ; jump if no BASIC
       ld   hl,($5c53)   ; (PROG)
       ld   bc,$0000     ; 
L3a1d  push bc           ; 
       rst  28h          ; 
       .word $19B8       ; NEXT-ONE, get next BASIC line
       ld   hl,($5b77)   ; 
       and  a            ; 
       sbc  hl,de        ; 
       jr   z,L3a2e      ; jump if reached end
       ex   de,hl        ; 
       pop  bc           ; 
       inc  bc           ; jump back, keeping count of lines
       jr   L3a1d        ; 
L3a2e  pop  de           ; retrieve number of BASIC lines
       inc  de           ; increment
       ret               ; 
L3a31  ld   de,$0000     ; no BASIC lines
       ret               ;

L3a35  inc  hl           ; 23
L3a36  ld   a,(hl)       ; 7e
       cp   $20          ; fe 20
       jr   z,L3a35      ; 28 fa
       ret               ; c9

; Store the ASCII representation of a four-digit number in HL into
; memory starting from DE, replacing leading zeroes with spaces.
L3a3c  push de           ;
       ld   bc,$fc18     ; -1000
       call L3a60        ;
       ld   bc,$ff9c     ; -100
       call L3a60        ;
       ld   c,$f6        ; BC = -10
       call L3a60        ; 
       ld   a,l          ;
       add  a,$30        ; convert number in the range 0-9 to ASCII character
       ld   (de),a       ;
       inc  de           ;
       ld   b,$03        ;
       pop  hl           ; Retrieve the starting location
L3a56  ld   a,(hl)       ;
       cp   $30          ; Return as soon as a non-zero character is found
       ret  nz           ;
       ld   (hl),$20     ; Replace "0" with " "
       inc  hl           ; (suppressing leading zeroes)
       djnz L3a56        ;
       ret               ;

; Count how many times BC can be added to HL
L3a60  xor  a            ;
L3a61  add  hl,bc        ;
       inc  a            ;
       jr   c,L3a61      ; Keep going while carry is set
       sbc  hl,bc        ; Undo the last step
       dec  a            ;
       add  a,$30        ; Convert to ASCII character
       ld   (de),a       ; Store in DE
       inc  de           ; 
       ret               ;

L3a6d  .byte $08
L3a6e  .byte $00,$00,$14,$00,$00,$00,$0f,$00

L3a76  .byte $08
L3a77  .byte $00,$16,$01,$00,$00,$00,$0f,$00

; Copy 8 bytes from L3a6e to fd6c
L3a7f  ld   ix,$fd6c     ; dd 21 6c fd
       ld   hl,$3a6d     ; 21 6d 3a
       jr   L3a8b        ; 18 03

; Copy 8 bytes from L3a77 to fd6c
L3a88  ld   hl,$3a76
L3a8b  ld   de,$fd6c
       jp   L3fba

       rst  10h          ; d7
       ld   a,d          ; 7a
       rst  10h          ; d7
       scf               ; 37
       ret               ; c9

L3a96  and  $3f          ; e6 3f
       ld   (ix+$06),a   ; dd 77 06
       scf               ; 37
       ret               ; c9

L3a9d  ld   a,(ix+$01)   ; dd 7e 01
       add  a,b          ; 80
       ld   b,a          ; 47
       call L3ba0        ; Get address of attribute byte into HL
       ld   a,(hl)       ; 
       ld   (ix+$07),a   ; Store the current attribute byte
       cpl               ; 
       and  $c0          ; 
       or   (ix+$06)     ; 
       ld   (hl),a       ; Store new attribute value to screen
       scf               ;
       ret               ; Return

L3ab2  ld   a,(ix+$01)   ; dd 7e 01
       add  a,b          ; 80
       ld   b,a          ; 47
       call L3ba0        ; Get address of attribute byte into HL
       ld   a,(ix+$07)   ; Get previous attribute value
       ld   (hl),a       ; Set colour
       ret               ; Return

L3abf  push hl           ; e5
       ld   h,$00        ; 26 00
       ld   a,e          ; 7b
       sub  b          ; 90
       jr   L3acd        ; 18 07
L3ac6  push hl           ; e5
       ld   a,e
       ld   e,b          ; 58
       ld   b,a          ; 47
       sub  e          ; 93
       ld   h,$ff        ; 26 ff
L3acd  ld   c,a          ; 4f
       ld   a,b          ; 78
       cp   e            ; bb
       jr   z,L3b1d      ; 28 4b
       push de           ; d5
       call L3b98        ; Calculate B=L18-B-(IX+L01)
L3ad6  push bc           ; c5
       ld   c,h          ; 4c
       rst  28h          ; Get address of B'th line in HL
       .word $0e9b ; CL-ADDR
       ex   de,hl
       xor  a            ; af
       or   c            ; b1
       jr   z,L3ae3      ; 28 03
       inc  b            ; 04
       jr   L3ae4        ; 18 01
L3ae3  dec  b            ; 05
L3ae4  push de           ; d5
       rst  28h          ; ef
      .word $0e9b ;  CL-ADDR
      pop de
       ld   a,c          ; 79
       ld   c,$20        ; 0e 20
       ld   b,$08        ; 06 08
L3aee  push bc           ; c5
       push hl           ; e5
       push de           ; d5
       ld   b,$00        ; 06 00
       ldir              ; ed b0
       pop  de           ; d1
       pop  hl           ; e1
       pop  bc           ; c1
       inc  h            ; 24
       inc  d            ; 14
       djnz L3aee        ; 10 f2
       push af           ; f5
       push de           ; d5
       rst  28h          ; ef
      .word $0e88 ;  CL-ATTR
      ex   de,hl
       ex   (sp),hl      ; e3
       rst  28h          ; ef
      .word $0e88 ;  CL-ATTR
      ex   de,hl
       ex   (sp),hl      ; e3
       pop  de           ; d1
       ld   bc,$0020     ; 01 20 00
       ldir              ; ed b0
       pop  af           ; f1
       pop  bc           ; c1
       and  a            ; a7
       jr   z,L3b16      ; 28 03
       inc  b            ; 04
       jr   L3b17        ; 18 01
L3b16  dec  b            ; 05
L3b17  dec  c            ; 0d
       ld   h,a          ; 67
       jr   nz,L3ad6     ; 20 bb
       pop  de           ; d1
       ld   b,e          ; 43
L3b1d  pop  hl           ; e1
L3b1e  call L3bb8        ; cd b8 3b
       ex   de,hl        ; eb
       ld   a,($5c3c)    ; TV FLAG
       push af           ; f5
       ld   hl,$ec0d     ; 21 0d ec
       bit  6,(hl)       ; cb 76
       res  0,a          ; cb 87
       jr   z,L3b31      ; 28 02
       set  0,a          ; cb c7
L3b31  ld   ($5c3c),a    ; 32 3c 5c
       ld   c,$00        ; 0e 00
       call L372b        ; cd 2b 37
       ex   de,hl        ; eb
       ld   b,$20        ; 06 20
L3b3c  ld   a,(hl)       ; 7e
       and  a            ; a7
       jr   nz,L3b42     ; 20 02
       ld   a,$20        ; 3e 20
L3b42  cp   $90          ; fe 90
       jr   nc,L3b55     ; 30 0f
       rst  28h          ; ef
      .word $0010
L3b49  inc  hl           ; 23
       djnz L3b3c        ; 10 f0
       pop  af           ; f1
       ld   ($5c3c),a    ; 32 3c 5c
       call L3bb8        ; cd b8 3b
       scf               ; 37
       ret               ; c9

L3b55  call L1f20        ; cd 20 1f
       rst  10h          ; d7
       call L1f45        ; cd 45 1f
       jr   L3b49        ; 18 eb

L3b5e  call L3bb8        ; Exchange 48+128 editing colour items
       ld   a,d          ; # lines
       sub  b            ; B=?
       inc  a            ; 3c
       ld   c,a          ; 4f
       call L3b98        ; cd 98 3b
L3b68  push bc           ; c5
       rst  28h          ; ef
       .word $0e9b ;  CL-ADDR
       ld   c,$08
L3b6e  push hl           ; e5
       ld   b,$20        ; 06 20
       xor  a            ; af
L3b72  ld   (hl),a       ; 77
       inc  hl           ; 23
       djnz L3b72        ; 10 fc
       pop  hl           ; e1
       inc  h            ; 24
       dec  c            ; 0d
       jr   nz,L3b6e     ; 20 f3
       ld   b,$20        ; 06 20
       push bc           ; c5
       rst  28h          ; ef
       .word $0e88 ; CL-ATTR
       ex   de,hl
       pop  bc           ; c1
       ld   a,($5c8d)    ; 3a 8d 5c
L3b86  ld   (hl),a       ; 77
       inc  hl           ; 23
       djnz L3b86        ; 10 fc
       pop  bc           ; c1
       dec  b            ; 05
       dec  c            ; 0d
       jr   nz,L3b68     ; 20 d9
       call L3bb8        ; cd b8 3b
       scf               ; 37
       ret               ; c9

; Something to do with screen positions presumably
; Returns C=33d-C; B=24d-B-(IX+01)

L3b94  ld   a,$21        ; 3e 21
       sub  c            ; 91
       ld   c,a          ; 4f
L3b98  ld   a,$18        ; 3e 18
       sub  b            ; 90
       sub  (ix+$01)     ; dd 96 01
       ld   b,a          ; 47
       ret               ; c9

; Get the address of the attribute byte for the character position (B,C)
; into HL
L3ba0  push bc           ;
       xor  a            ; 
       ld   d,b          ; DE = B * 256
       ld   e,a          ;
       rr   d            ;
       rr   e            ;
       rr   d            ;
       rr   e            ;
       rr   d            ;
       rr   e            ; DE = B * 32
       ld   hl,$5800     ; Start of attributes
       ld   b,a          ; B = 0
       add  hl,bc        ; 
       add  hl,de        ; 
       pop  bc           ; 
       ret               ; 

; Exchange 128 editor colour items and 48K colour items
L3bb8  push af           ; f5
       push hl           ; e5
       push de           ; d5
       ld   hl,($5c8d)   ; ATTR P, MASK P
       ld   de,($5c8f)   ; ATTR T, MASK T
       exx               ; d9
       ld   hl,($ec0f)   ; 2a 0f ec
       ld   de,($ec11)   ; ed 5b 11 ec
       ld   ($5c8d),hl   ; 22 8d 5c
       ld   ($5c8f),de   ; ed 53 8f 5c
       exx               ; d9
       ld   ($ec0f),hl   ; 22 0f ec
       ld   ($ec11),de   ; ed 53 11 ec
       ld   hl,$ec13     ; Temporary store for P-FLAG
       ld   a,($5c91)    ; P-FLAG
       ld   d,(hl)       ; 56
       ld   (hl),a       ; 77
       ld   a,d          ; 7a
       ld   ($5c91),a    ; 32 91 5c
       pop  de           ; d1
       pop  hl           ; e1
       pop  af           ; f1
       ret               ; c9

; Tape Tester code
L3be9  call L3c56        ; cd 56 3c
       di                ; f3
       in   a,($fe)      ; db fe
       and  $40          ; e6 40
       ex   af,af'       ; 08
       ld   hl,$58e1     ; 21 e1 58
       ld   de,$0006     ; 11 06 00
       ld   b,e          ; 43
       ld   a,d          ; 7a
L3bfa  ld   (hl),a       ; 77
       add  hl,de        ; 19
       djnz L3bfa        ; 10 fc
L3bfe  ld   hl,$0000     ; 21 00 00
       ld   de,$0800     ; 11 00 08
L3c04  ld   bc,$bffe     ; 01 fe bf
       in   a,(c)        ; ed 78
       bit  0,a          ; cb 47
       jr   z,L3c56      ; 28 49
       ld   b,$7f        ; 06 7f
       in   a,(c)        ; ed 78
       bit  0,a          ; cb 47
       jr   z,L3c56      ; 28 41
       ld   b,$f7        ; 06 f7
       in   a,(c)        ; ed 78
       bit  0,a          ; cb 47
       jr   z,L3c56      ; 28 39
L3c1d  dec  de           ; 1b
       ld   a,d          ; 7a
       or   e            ; b3
       jr   z,L3c2b      ; 28 09
       in   a,($fe)      ; db fe
       and  $40          ; e6 40
       jr   z,L3c1d      ; 28 f5
       inc  hl           ; 23
       jr   L3c1d        ; 18 f2
L3c2b  rl   l            ; cb 15
       rl   h            ; cb 14
       rl   l            ; cb 15
       rl   h            ; cb 14
       ex   af,af'       ; 08
       jr   z,L3c3d      ; 28 07
       ex   af,af'       ; 08
       ld   a,$20        ; 3e 20
       sub  h          ; 94
       ld   l,a          ; 6f
       jr   L3c3f        ; 18 02
L3c3d  ex   af,af'       ; 08
       ld   l,h          ; 6c
L3c3f  xor  a            ; af
       ld   h,a          ; 67
       ld   de,$591f     ; 11 1f 59
       ld   b,$20        ; 06 20
       ld   a,$48        ; 3e 48
       ei                ; fb
       halt              ; 76
       di                ; f3
L3c4b  ld   (de),a       ; 12
       dec  de           ; 1b
       djnz L3c4b        ; 10 fc
       inc  de           ; 13
       add  hl,de        ; 19
       ld   a,$68        ; 3e 68
       ld   (hl),a       ; 77
       jr   L3bfe        ; 18 a8

L3c56  ei                ; fb
       ld   b,$19        ; 06 19
L3c59  halt              ; 76
       djnz L3c59        ; 10 fd
       ld   hl,$5c3b     ; 21 3b 5c
       res  5,(hl)       ; cb ae
       scf               ; 37
       ret               ; c9

L3c63  ld   a,$01        ; 3e 01
       jr   L3c69        ; 18 02
L3c67  ld   a,$00        ; 3e 00
L3c69  ld   ($fd8a),a    ; 32 8a fd
       ld   hl,$0000     ; 21 00 00
       ld   ($fd85),hl   ; 22 85 fd
       ld   ($fd87),hl   ; 22 87 fd
       add  hl,sp        ; 39
       ld   ($fd8b),hl   ; 22 8b fd
       call L34ea        ; cd ea 34
       ld   a,$00        ; 3e 00
       ld   ($fd84),a    ; 32 84 fd
       ld   hl,$fd74     ; 21 74 fd
       ld   ($fd7d),hl   ; 22 7d fd
       call L1f20        ; cd 20 1f
       rst  28h          ; ef
       .word $16b0 ;  SET-MIN
       call L1f45
       ld   a,$00        ; 3e 00
       ld   ($fd81),a    ; 32 81 fd
       ld   hl,($5c59)   ; 2a 59 5c
       ld   ($fd82),hl   ; 22 82 fd
       ld   hl,$0000     ; 21 00 00
       ld   ($fd7f),hl   ; 22 7f fd
L3ca1  ld   hl,($fd85)   ; 2a 85 fd
       inc  hl           ; 23
       ld   ($fd85),hl   ; 22 85 fd
       call L3d9d        ; cd 9d 3d
       ld   c,a          ; 4f
       ld   a,($fd81)    ; 3a 81 fd
       cp   $00          ; fe 00
       jr   nz,L3cf4     ; 20 41
L3cb3  ld   a,c          ; 79
       and  $04          ; e6 04
       jr   z,L3ced      ; 28 35
L3cb8  call L3de9        ; cd e9 3d
       jr   nc,L3cc4     ; 30 07
       ld   a,$01        ; 3e 01
       ld   ($fd81),a    ; 32 81 fd
       jr   L3ca1        ; 18 dd
L3cc4  ld   hl,($fd7f)   ; 2a 7f fd
       ld   a,l          ; 7d
       or   h            ; b4
       jp   nz,L3d1e     ; c2 1e 3d
L3ccc  push bc           ; c5
       call L3dcd        ; cd cd 3d
       pop  bc           ; c1
       ld   a,$00        ; 3e 00
       ld   ($fd81),a    ; 32 81 fd
L3cd6  ld   a,c          ; 79
       and  $01          ; e6 01
       jr   nz,L3cb3     ; 20 d8
       ld   a,b          ; 78
       call L3e16        ; cd 16 3e
       ret  nc           ; d0
       ld   hl,($fd85)   ; 2a 85 fd
       inc  hl           ; 23
       ld   ($fd85),hl   ; 22 85 fd
       call L3d9d        ; cd 9d 3d
       ld   c,a          ; 4f
       jr   L3cd6        ; 18 e9
L3ced  ld   a,b          ; 78
       call L3e16        ; cd 16 3e
       ret  nc           ; d0
       jr   L3ca1        ; 18 ad
L3cf4  cp   $01          ; fe 01
       jr   nz,L3ced     ; 20 f5
       ld   a,c          ; 79
       and  $01          ; e6 01
       jr   z,L3cb8      ; 28 bb
       push bc           ; c5
L3cfe  call L3f7e        ; cd 7e 3f
       pop  bc           ; c1
       jr   c,L3d7d      ; 38 79
       ld   hl,($fd7f)   ; 2a 7f fd
       ld   a,h          ; 7c
       or   l            ; b5
       jr   nz,L3d1e     ; 20 13
       ld   a,c          ; 79
       and  $02          ; e6 02
       jr   z,L3ccc      ; 28 bc
       call L3de9        ; cd e9 3d
       jr   nc,L3cc4     ; 30 af
       ld   hl,($fd7d)   ; 2a 7d fd
       dec  hl           ; 2b
       ld   ($fd7f),hl   ; 22 7f fd
       jr   L3ca1        ; 18 83
L3d1e  push bc           ; c5
       ld   hl,$fd74     ; 21 74 fd
       ld   de,($fd7f)   ; ed 5b 7f fd
       ld   a,d          ; 7a
       cp   h            ; bc
       jr   nz,L3d2f     ; 20 05
       ld   a,e          ; 7b
       cp   l            ; bd
       jr   nz,L3d2f     ; 20 01
       inc  de           ; 13
L3d2f  dec  de           ; 1b
       jr   L3d33        ; 18 01
L3d32  inc  hl           ; 23
L3d33  ld   a,(hl)       ; 7e
       and  $7f          ; e6 7f
       push hl           ; e5
       push de           ; d5
       call L3e16        ; cd 16 3e
       pop  de           ; d1
       pop  hl           ; e1
       ld   a,h          ; 7c
       cp   d            ; ba
       jr   nz,L3d32     ; 20 f1
       ld   a,l          ; 7d
       cp   e            ; bb
       jr   nz,L3d32     ; 20 ed
       ld   de,($fd7f)   ; ed 5b 7f fd
       ld   hl,$fd74     ; 21 74 fd
       ld   ($fd7f),hl   ; 22 7f fd
       ld   bc,($fd7d)   ; ed 4b 7d fd
       dec  bc           ; 0b
       ld   a,d          ; 7a
       cp   h            ; bc
       jr   nz,L3d70     ; 20 18
       ld   a,e          ; 7b
       cp   l            ; bd
       jr   nz,L3d70     ; 20 14
       inc  de           ; 13
       push hl           ; e5
       ld   hl,$0000     ; 21 00 00
       ld   ($fd7f),hl   ; 22 7f fd
       pop  hl           ; e1
       ld   a,b          ; 78
       cp   h            ; bc
       jr   nz,L3d70     ; 20 07
       ld   a,c          ; 79
       cp   l            ; bd
       jr   nz,L3d70     ; 20 03
       pop  bc           ; c1
       jr   L3d8f        ; 18 1f
L3d70  ld   a,(de)       ; 1a
       ld   (hl),a       ; 77
       inc  hl           ; 23
       inc  de           ; 13
       and  $80          ; e6 80
       jr   z,L3d70      ; 28 f8
       ld   ($fd7d),hl   ; 22 7d fd
       jr   L3cfe        ; 18 81
L3d7d  push bc           ; c5
       call L3e16        ; cd 16 3e
       pop  bc           ; c1
       ld   hl,$0000     ; 21 00 00
       ld   ($fd7f),hl   ; 22 7f fd
       ld   a,($fd81)    ; 3a 81 fd
       cp   $04          ; fe 04
       jr   z,L3d94      ; 28 05
L3d8f  ld   a,$00        ; 3e 00
       ld   ($fd81),a    ; 32 81 fd
L3d94  ld   hl,$fd74     ; 21 74 fd
       ld   ($fd7d),hl   ; 22 7d fd
       jp   L3cb3        ; c3 b3 3c

L3d9d  call L2d54        ; cd 54 2d
       ld   b,a          ; 47
       cp   $3f          ; fe 3f
       jr   c,L3daf      ; 38 0a
       or   $20          ; f6 20
       call L3dc6        ; cd c6 3d
       jr   c,L3dc3      ; 38 17
L3dac  ld   a,$01        ; 3e 01
       ret               ; c9
L3daf  cp   $20          ; fe 20
       jr   z,L3dc0      ; 28 0d
       cp   $23          ; fe 23
       jr   z,L3dbd      ; 28 06
       jr   c,L3dac      ; 38 f3
       cp   $24          ; fe 24
       jr   nz,L3dac     ; 20 ef
L3dbd  ld   a,$02        ; 3e 02
       ret               ; c9
L3dc0  ld   a,$03        ; 3e 03
       ret               ; c9
L3dc3  ld   a,$06        ; 3e 06
       ret               ; c9
L3dc6  cp   $7b          ; fe 7b
       ret  nc           ; d0
       cp   $61          ; fe 61
       ccf               ; 3f
       ret               ; c9

L3dcd  ld   hl,$fd74     ; 21 74 fd
       ld   ($fd7d),hl   ; 
       sub  a            ; 97
       ld   ($fd7f),a    ; 32 7f fd
       ld   ($fd80),a    ; 32 80 fd
L3dda  ld   a,(hl)       ; 7e
       and  $7f          ; e6 7f
       push hl           ; e5
       call L3e9c        ; "IF n1<n2" bug [AOwen]
       pop  hl           ; e1
       ld   a,(hl)       ; 7e
       and  $80          ; e6 80
       ret  nz           ; c0
       inc  hl           ; 23
       jr   L3dda        ; 18 f1
L3de9  ld   hl,($fd7d)   ; 2a 7d fd
       ld   de,$fd7d     ; 11 7d fd
       ld   a,d          ; 7a
       cp   h            ; bc
       jr   nz,L3df8     ; 20 05
       ld   a,e          ; 7b
       cp   l            ; bd
       jp   z,L3e13      ; ca 13 3e
L3df8  ld   de,$fd74     ; 11 74 fd
       ld   a,d          ; 7a
       cp   h            ; bc
       jr   nz,L3e03     ; 20 04
       ld   a,e          ; 7b
L3e00  cp   l            ; bd
       jr   z,L3e09      ; 28 06
L3e03  dec  hl           ; 2b
       ld   a,(hl)       ; 7e
       and  $7f          ; e6 7f
       ld   (hl),a       ; 77
       inc  hl           ; 23
L3e09  ld   a,b          ; 78
       or   $80          ; f6 80
       ld   (hl),a       ; 77
       inc  hl           ; 23
       ld   ($fd7d),hl   ; 22 7d fd
       scf               ; 37
       ret               ; c9
L3e13  scf               ; 37
       ccf               ; 3f
       ret               ; c9
L3e16  push af           ; f5
       ld   a,($fd89)    ; 3a 89 fd
       or   a            ; b7
       jr   nz,L3e2f     ; 20 12
       pop  af           ; f1
       cp   $3e          ; fe 3e
       jr   z,L3e2a      ; 28 08
       cp   $3c          ; fe 3c
       jr   z,L3e2a      ; 28 04
L3e26  call L3e64        ; cd 64 3e
       ret               ; c9
L3e2a  ld   ($fd89),a    ; 32 89 fd
       scf               ; 37
       ret               ; c9

L3e2f  cp   $3c          ; fe 3c
       ld   a,$00        ; 3e 00
       ld   ($fd89),a    ; 32 89 fd
       jr   nz,L3e52     ; 20 1a
       pop  af           ; f1
       cp   $3e          ; fe 3e
       jr   nz,L3e41     ; 20 04
       ld   a,$c9        ; 3e c9
       jr   L3e26        ; 18 e5
L3e41  cp   $3d          ; fe 3d
       jr   nz,L3e49     ; 20 04
       ld   a,$c7        ; 3e c7
       jr   L3e26        ; 18 dd
L3e49  push af           ; f5
       ld   a,$3c        ; 3e 3c
       call L3e64        ; cd 64 3e
       pop  af           ; f1
       jr   L3e26        ; 18 d4
L3e52  pop  af           ; f1
       cp   $3d          ; fe 3d
       jr   nz,L3e5b     ; 20 04
       ld   a,$c8        ; 3e c8
       jr   L3e26        ; 18 cb
L3e5b  push af           ; f5
       ld   a,$3e        ; 3e 3e
       call L3e64        ; cd 64 3e
       pop  af           ; f1
       jr   L3e26        ; 18 c2
L3e64  cp   $0d          ; fe 0d
       jr   z,L3e88      ; 28 20
       cp   $ea          ; 'REM'
       ld   b,a          ; 47
       jr   nz,L3e74     ; 20 07
       ld   a,$04        ; 3e 04
       ld   ($fd81),a    ; 32 81 fd
       jr   L3e82        ; 18 0e
L3e74  cp   $22          ; fe 22
       jr   nz,L3e82     ; 20 0a
       ld   a,($fd81)    ; 3a 81 fd
       and  $fe          ; e6 fe
       xor  $02          ; ee 02
       ld   ($fd81),a    ; 32 81 fd
L3e82  ld   a,b          ; 78
       call L3e9c        ; cd 9c 3e
       scf               ; 37
       ret               ; c9
L3e88  ld   a,($fd8a)    ; 3a 8a fd
       cp   $00          ; fe 00
       jr   z,L3e99      ; 28 0a
       ld   bc,($fd85)   ; ed 4b 85 fd
       ld   hl,($fd8b)   ; 2a 8b fd
       ld   sp,hl        ; f9
       scf               ; 37
       ret               ; c9
L3e99  scf               ; 37
       ccf               ; 3f
       ret               ; c9

L3e9c  ld   e,a          ; 5f
       ld   a,($fd84)    ; 3a 84 fd
       ld   d,a          ; 57
       ld   a,e          ; 7b
       cp   $20          ; fe 20
       jr   nz,L3ec6     ; 20 20
       ld   a,d          ; 7a
       and  $01          ; e6 01
       jr   nz,L3ebf     ; 20 14
       ld   a,d          ; 7a
       and  $02          ; e6 02
       jr   nz,L3eb7     ; 20 07
       ld   a,d          ; 7a
       or   $02          ; f6 02
       ld   ($fd84),a    ; 32 84 fd
       ret               ; c9
L3eb7  ld   a,e          ; 7b
       call L3efb        ; cd fb 3e
       ld   a,($fd84)    ; 3a 84 fd
       ret               ; c9
L3ebf  ld   a,d          ; 7a
       and  $fe          ; e6 fe
       ld   ($fd84),a    ; 32 84 fd
       ret               ; c9
L3ec6  cp   $a3          ; fe a3
       jr   nc,L3eee     ; 30 24
       ld   a,d          ; 7a
       and  $02          ; e6 02
       jr   nz,L3eda     ; 20 0b
       ld   a,d          ; 7a
       and  $fe          ; e6 fe
       ld   ($fd84),a    ; 32 84 fd
       ld   a,e          ; 7b
       call L3efb        ; cd fb 3e
       ret               ; c9

L3eda  push de           ; d5
       ld   a,$20        ; 3e 20
       call L3efb        ; cd fb 3e
       pop  de           ; d1
       ld   a,d          ; 7a
       and  $fe          ; e6 fe
       and  $fd          ; e6 fd
       ld   ($fd84),a    ; 32 84 fd
       ld   a,e          ; 7b
       call L3efb        ; cd fb 3e
       ret               ; c9

L3eee  ld   a,d          ; 7a
       and  $fd          ; e6 fd
       or   $01          ; f6 01
       ld   ($fd84),a    ; 32 84 fd
       ld   a,e          ; 7b
       call L3efb        ; cd fb 3e
       ret               ; c9

L3efb  ld   hl,($fd87)   ; 2a 87 fd
       inc  hl           ; 23
       ld   ($fd87),hl   ; 22 87 fd
       ld   hl,($fd82)   ; 2a 82 fd
       ld   b,a          ; 47
       ld   a,($fd8a)    ; 3a 8a fd
       cp   $00          ; fe 00
       ld   a,b          ; 78
       jr   z,L3f33      ; 28 25
       ld   de,($5c5f)   ; (X PTR), address of character after error marker
       ld   a,h          ; 7c
       cp   d            ; ba
       jr   nz,L3f30     ; 20 1a
       ld   a,l          ; 7d
       cp   e            ; bb
       jr   nz,L3f30     ; 20 16
       ld   bc,($fd85)   ; ed 4b 85 fd
       ld   hl,($fd87)   ; 2a 87 fd
       and  a            ; a7
       sbc  hl,bc        ; "ERROR MARKER bug" [AOwen]
       jr   nc,L3f2a     ; 30 04
       ld   bc,($fd87)   ; ed 4b 87 fd
L3f2a  ld   hl,($fd8b)   ; 2a 8b fd
       ld   sp,hl        ; f9
       scf               ; 37
       ret               ; c9

L3f30  scf               ; 37
       jr   L3f35        ; 18 02

L3f33  scf               ; 37
       ccf               ; 3f
L3f35  call L1f20        ; cd 20 1f
       jr   nc,L3f47     ; 30 0d
       ld   a,(hl)       ; 7e
       ex   de,hl        ; eb
       cp   $0e          ; fe 0e
       jr   nz,L3f5d     ; 20 1d
       inc  de           ; bug [AOwen]
       inc  de           ; 13
       inc  de           ; 13
       inc  de           ; 13
       inc  de           ; 13
       jr   L3f5d        ; 18 16
L3f47  push af           ; f5
       ld   bc,$0001     ; 01 01 00
       push hl           ; e5
       push de           ; d5
       call L3f66        ; cd 66 3f
       pop  de           ; d1
       pop  hl           ; e1
       rst  28h          ; ef
       .word $1664 ;  POINTERS
       ld   hl,($5c65)
       ex   de,hl        ; eb
       lddr              ; ed b8
       pop  af           ; f1
       ld   (de),a       ; 12
L3f5d  inc  de           ; 13
       call L1f45        ; cd 45 1f
       ld   ($fd82),de   ; ed 53 82 fd
       ret               ; c9
L3f66  ld   hl,($5c65)   ; 2a 65 5c
       add  hl,bc        ; 09
       jr   c,L3f76      ; 38 0a
       ex   de,hl        ; eb
       ld   hl,$0082     ; 21 82 00
       add  hl,de        ; 19
       jr   c,L3f76      ; 38 03
       sbc  hl,sp        ; ed 72
       ret  c            ; d8
L3f76  ld   a,$03        ; 3e 03
       ld   ($5c3a),a    ; 32 3a 5c
       jp   L0321        ; c3 21 03

L3f7e  call $fd2e        ; cd 2e fd
       ret  c            ; d8
       ld   b,$f9        ;
       ld   de,$fd74     ; Convert mis-spelled keywords
       ld   hl,$3594     ;
       call $fd3b        ;
       ret  nc           ;
       cp   $ff          ; CLOSE#
       jr   nz,L3f96     ;
       ld   a,$d4        ; CLOSE #
       jr   L3fb8        ; 
L3f96  cp   $fe          ; OPEN#
       jr   nz,L3f9e     ; 
       ld   a,$d3        ; OPEN #
       jr   L3fb8        ; 
L3f9e  cp   $fd          ; DEFFN
       jr   nz,L3fa6     ; 
       ld   a,$ce        ; DEF FN
       jr   L3fb8        ; 
L3fa6  cp   $fc          ; GOSUB
       jr   nz,L3fae     ; 
       ld   a,$ed        ; GO SUB
       jr   L3fb8        ; 
L3fae  cp   $fb          ; GOTO
       jr   nz,L3fb6     ; 
       ld   a,$ec        ; GO TO
       jr   L3fb8        ; 
L3fb6  sub  $56          ; 
L3fb8  scf               ;
       ret               ;

; Copy (hl) bytes from (hl+1 ... ) to (de...)
L3fba  ld   b,(hl)       ; Get number of bytes to copy
       inc  hl           ;
L3fbc  ld   a,(hl)       ; Copy from (HL)...
       ld   (de),a       ; ... to (DE)
       inc  de           ; Increment
       inc  hl           ; 
       djnz L3fbc        ; and repeat
       ret               ;

; Get the numeric value for an ASCII character (if it is numeric)
       cp   $30          ; Test against "0"
       ccf               ; 
       ret  nc           ; Return with carry reset if not numeric character
       cp   $3a          ; Test against ":"
       ret  nc           ; Return with carry reset if not numeric character
       sub  $30          ; Get numeric value
       scf               ; Return with carry set
       ret               ;

; Does the action in A match anything in the table at HL?
; Make the call if so
L3fce  push bc           ;
       push de           ;
       ld   b,(hl)       ; # entries in table
       inc  hl           ;
L3fd2  cp   (hl)         ; Possible match for A?
       inc  hl           ;
       ld   e,(hl)       ;
       inc  hl           ;
       ld   d,(hl)       ; DE = address to call for match
       jr   z,L3fe1      ;
       inc  hl           ;
       djnz L3fd2        ;
       scf               ; Return with carry and
       ccf               ; zero reset if
       pop  de           ; no match found
       pop  bc           ;
       ret               ;
L3fe1  ex   de,hl        ; Found a match
       pop  de           ; 
       pop  bc           ;
       call L3fee        ;
       jr   c,L3feb      ; If match found, return with Z set,
       cp   a            ; and C set according to the result
       ret               ; of the called subroutine
L3feb  cp   a            ;
L3fec  scf               ;
       ret               ;
L3fee  jp   (hl)         ; 

; Initials of the programmers [Andrew Owen]
       .byte 00
       .text "MB"    ; Martin Brennan
       .byte 00
       .text "SB"    ; Steve Berry
       .byte 00
       .text "AC"    ; Andrew Cummins
       .byte 00
       .text "RG"    ; Rupert Goodwins
       .byte 00
       .text "KM"    ; Kevin Males
       .byte 00
       .byte 01

.end