home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / CGENIE / TOOLS / BASICROM.ZIP / BASICROM.TXT
Text File  |  1999-02-11  |  772KB  |  15,712 lines

  1. ; **************************************************************************
  2. ; *                 ROM listing Colour BASIC interpreter                   *
  3. ; **************************************************************************
  4. ;
  5. ; This is a ROM listing of the BASIC interpreter for the EACA EG2000
  6. ; Colour Genie computer. There are several versions of the Colour BASIC
  7. ; interpreter around. This is the most commonly used. 
  8. ;
  9. ; -------------------------------------------------------------------------
  10. ; Conventions:
  11. ;
  12. ; A,B,C,D,E,
  13. ; H,L,BC,DE,
  14. ; HL,IX,IY,
  15. ; SP,PC                 Identifiers of the Z80 registers
  16. ;
  17. ; BCDE                  Concatination of BC and DE registers to hold
  18. ;                       a single precision value
  19. ;
  20. ; X                     Accumulator X (from 411DH onwards)
  21. ;
  22. ; Y                     Accumulator Y (from 4127H onwards)
  23. ;
  24. ; Exp ( X )             Exponent of value in X
  25. ;
  26. ; Exp ( Y )             Exponent of value in Y
  27. ;
  28. ; Exp                   Exponent of any floating point value
  29. ;
  30. ; Sign                  Sign of a value
  31. ;
  32. ; VT                    Variable Type
  33. ;                       (02 = INT, 03 = STR, 04 = SNG, 08 = DBL)
  34. ;
  35. ; LP                    Line Pointer (= link)
  36. ;
  37. ; LN                    Line Number
  38. ;
  39. ; PTP                   Program Text Pointer
  40. ;
  41. ; A = B                 A equals B
  42. ;
  43. ; A -> B                A points to B (contains address of B)
  44. ;
  45. ; A,n                   Bit n of register A
  46. ;
  47. ; I:                    List of entry parameters
  48. ;
  49. ; O:                    List of exit parameters
  50. ;
  51. ; SUB                   Subroutine
  52. ;
  53. ; --                    Unused instruction
  54. ;
  55. ; '*'                   The following byte is multiple used
  56. ;
  57. ; ---------------------------------------------------------------------------
  58. ; Start of the BASIC interpreter ROMs
  59.  
  60. ; Entry at 0000H after switch on of the computer or after <RST>+<R>
  61. ; Cold start
  62.  
  63.   00000 F3              DI                      ;Disable interrupts
  64.   00001 AF              XOR     A               ;A = 0
  65.   00002 C37406          JP      0674H           ;Continue at 0674H
  66.  
  67.   00005 C30040          JP      4000H           ;--
  68.  
  69.   00008 C30040          JP      4000H           ;RST 08H: jump to 4000H and
  70.                                                 ;from there to 1C96H
  71.  
  72.   0000B E1              POP     HL              ;--
  73.   0000C E9              JP      (HL)
  74.  
  75.   0000D C30000          JP      0000H           ;--
  76.  
  77.   00010 C30340          JP      4003H           ;RST 10H: jump to 4003H and
  78.                                                 ;from there to 1D78H
  79.  
  80. ; DCB Call for Input (AF,DE)
  81. ;
  82. ; I: DE = DCB Address
  83. ; O: A = Input byte
  84.  
  85.   00013 C5              PUSH    BC              ;Save BC
  86.   00014 0601            LD      B,01H           ;DCB type: input
  87.   00016 182E            JR      0046H           ;Call DCB
  88.  
  89.  
  90.   00018 C30640          JP      4006H           ;RST 18H: jump to 4006H and
  91.                                                 ;from there to 1C90H
  92.  
  93. ; DCB Call for Output (AF,DE)
  94. ;
  95. ; I: DE = DCB Address
  96. ; O: A = Output byte
  97.  
  98.   0001B C5              PUSH    BC              ;Save BC
  99.   0001C 0602            LD      B,02H           ;DCB type: output
  100.   0001E 1826            JR      0046H           ;Call DCB
  101.  
  102.  
  103.   00020 C30940          JP      4009H           ;RST 20H: jump to 4009H and
  104.                                                 ;from there to 25D9H
  105.  
  106. ; DCB Call for Input/Output (AF,DE) (not used)
  107. ;
  108. ; I: DE = DCB Address
  109. ; O: A = Output byte
  110.  
  111.   00023 C5              PUSH    BC              ;Save BC
  112.   00024 0604            LD      B,04H           ;DCB type: input/output
  113.   00026 181E            JR      0046H           ;Call DCB
  114.  
  115.  
  116.   00028 C30C40          JP      400CH           ;RST 28H: jump to 400CH
  117.                                                 ;(BREAK-vector, reserved for
  118.                                                 ;DOS)
  119.  
  120. ; Get byte from keyboard (AF,DE)
  121. ;
  122. ; I: -
  123. ; O: A = ASCII code of the key pressed or 00H when no key pressed
  124.  
  125.   0002B 111540          LD      DE,4015H        ;DE -> Keyboard DCB
  126.   0002E 18E3            JR      0013H           ;Continue at 0013H
  127.  
  128.  
  129.   00030 C30F40          JP      400FH           ;RST 38H: jump to 400FH
  130.                                                 ;(reserved for DOS)
  131.  
  132. ; Output a byte on the screen (AF,DE)
  133. ;
  134. ; I: A = ASCII code of the character to print on the screen
  135. ; O: -
  136.  
  137.   00033 111D40          LD      DE,401DH        ;DE -> Screen DCB
  138.   00036 18E3            JR      001BH           ;Continue at 001BH
  139.  
  140.  
  141.   00038 C31240          JP      4012H           ;RST 38H: jump to 4012H
  142.                                                 ;(Interrupt vector, reserved
  143.                                                 ;for DOS)
  144.  
  145. ; Output a byte on the printer (AF,DE)
  146. ;
  147. ; I: A = ASCII code of the character to print on the printer
  148. ; O: -
  149.  
  150.   0003B 112540          LD      DE,4025H        ;DE -> Printer DCB
  151.   0003E 18DB            JR      001BH           ;Continue at 001BH
  152.  
  153.  
  154.   00040 C3D905          JP      05D9H           ;Input of a line
  155.                                                 ;(see 05D9H)
  156.  
  157.   00043 C9              RET                     ;--
  158.   00044 00              NOP
  159.   00045 00              NOP
  160.  
  161.   00046 C3C203          JP      03C2H           ;Jump to DCB call
  162.  
  163.  
  164. ; Wait for keypress (AF,DE)
  165. ;
  166. ; I: -
  167. ; O: A = ASCII code of key pressed
  168.  
  169.   00049 CD2B00          CALL    002BH           ;Get byte from keyboard
  170.   0004C B7              OR      A               ;Key pressed ? (A <> 0)
  171.   0004D C0              RET     NZ              ;Yes: return
  172.   0004E 18F9            JR      0049H           ;No: again
  173.  
  174.  
  175. ; Decoding table for keyboard routine
  176. ; ASCII codes of corresponding keys
  177.  
  178.   00050 0D              DEFB    0DH             ;RETURN
  179.   00051 0D              DEFB    0DH             ;RETURN SHIFT
  180.   00052 1F              DEFB    1FH             ;CLEAR
  181.   00053 1F              DEFB    1FH             ;CLEAR SHIFT
  182.   00054 01              DEFB    01H             ;BREAK
  183.   00055 01              DEFB    01H             ;BREAK SHIFT
  184.   00056 5B              DEFB    5BH             ;UP ARROW
  185.   00057 1B              DEFB    1BH             ;UP ARROW SHIFT
  186.   00058 0A              DEFB    0AH             ;DOWN ARROW
  187.   00059 1A              DEFB    1AH             ;DOWN ARROW SHIFT
  188.   0005A 08              DEFB    08H             ;LEFT ARROW
  189.   0005B 18              DEFB    18H             ;LEFT ARROW SHIFT
  190.   0005C 09              DEFB    09H             ;RIGHT ARROW
  191.   0005D 19              DEFB    19H             ;RIGHT ARROW SHIFT
  192.   0005E 20              DEFB    20H             ;SPACE BAR
  193.   0005F 20              DEFB    20H             ;SPACE BAR SHIFT
  194.  
  195.  
  196. ; Delay loop (AF,BC)
  197. ;
  198. ; I: BC = counter (delay time = BC * 11.3 microseconds)
  199. ; O: -
  200.  
  201.   00060 0B              DEC     BC              ;Decrement counter
  202.   00061 78              LD      A,B
  203.   00062 B1              OR      C               ;Counter zero ?
  204.   00063 20FB            JR      NZ,0060H        ;No: loop
  205.   00065 C9              RET
  206.  
  207.  
  208. ; RESET entry after pressing both <RST> keys
  209.  
  210.   00066 01181A          LD      BC,1A18H        ;BC -> entry back into BASIC
  211.   00069 C3CA05          JP      05CAH           ;Continue at 05CAH
  212.  
  213.  
  214. ; Start 4 (continuation of 05C7H)
  215. ; Prepare RAM for BASIC
  216.  
  217.   0006C 31F841          LD      SP,41F8H        ;Set stackpointer
  218.   0006F 118040          LD      DE,4080H        ;Copy ROM area of 18F7H
  219.   00072 21F718          LD      HL,18F7H        ;to 191CH into RAM area
  220.   00075 012700          LD      BC,0027H        ;from 4080H to 40A6H
  221.   00078 C34001          JP      0140H           ;Continue at 0140H
  222.  
  223.  
  224. ; Start 6 when no ROM cartridge with a BASIC program is present
  225. ; (continuation of 0149H)
  226.  
  227.   0007B 210158          LD      HL,5801H        ;HL -> start of BASIC program
  228.   0007E 3A80F8          LD      A,(0F880H)
  229.   00081 CB4F            BIT     1,A             ;<MOD SEL> pressed ?
  230.   00083 2009            JR      NZ,008EH        ;Yes: continue at 008EH
  231.  
  232.   00085 22A440          LD      (40A4H),HL      ;Save pointer in system RAM
  233.   00088 CD4638          CALL    3846H           ;FCLS
  234.  
  235.  
  236. ; Start 6 when a ROM cartridge with a BASIC program is present
  237. ; (continuation of 014CH)
  238.  
  239.   0008B 22A440          LD      (40A4H),HL      ;Save pointer to start of
  240.                                                 ;program in system RAM
  241.   0008E 21E541          LD      HL,41E5H        ;HL -> (start of line buffer-3)
  242.                                                 ;Mark start:
  243.   00091 363A            LD      (HL),3AH        ;':'
  244.   00093 23              INC     HL
  245.   00094 70              LD      (HL),B          ;00H
  246.   00095 23              INC     HL
  247.   00096 362C            LD      (HL),2CH        ;'.'
  248.   00098 23              INC     HL
  249.   00099 22A740          LD      (40A7H),HL      ;Save line buffer pointer in
  250.                                                 ;system RAM
  251.  
  252.   0009C 113B01          LD      DE,013BH        ;DE -> error routine
  253.  
  254.                                                 ;Block Disk BASIC vectors
  255.   0009F 061C            LD      B,1CH           ;28 vectors
  256.   000A1 215241          LD      HL,4152H        ;HL -> vector table in RAM
  257.                                                 ;Construct vectors:
  258.                                                 ;28 times a JP 013BH in RAM
  259.   000A4 36C3            LD      (HL),0C3H       ;JP opcode
  260.   000A6 23              INC     HL
  261.   000A7 73              LD      (HL),E          ;LSB of error routine address
  262.   000A8 23              INC     HL
  263.   000A9 72              LD      (HL),D          ;MSB of error routine address
  264.   000AA 23              INC     HL
  265.   000AB 10F7            DJNZ    00A4H           ;Next vector
  266.  
  267.                                                 ;Remaining DOS vectors are
  268.                                                 ;blocked with a RET
  269.   000AD 0615            LD      B,15H           ;21 vectors
  270.   000AF 36C9            LD      (HL),0C9H       ;Insert RET opcode
  271.   000B1 23              INC     HL              ;Reserve 2 bytes RAM space
  272.   000B2 23              INC     HL              ;so that the RET can be changed
  273.   000B3 23              INC     HL              ;to a JP instruction
  274.   000B4 10F9            DJNZ    00AFH           ;Next vector
  275.  
  276.   000B6 2AA440          LD      HL,(40A4H)      ;HL -> start of BASIC program
  277.   000B9 2B              DEC     HL              ;Adjust pointer
  278.   000BA 70              LD      (HL),B          ;Mark start - 1 with 00H
  279.   000BB CD7038          CALL    3870H           ;Initialize CRTC
  280.   000BE CD8F1B          CALL    1B8FH           ;Set stack pointer
  281.   000C1 CDAF06          CALL    06AFH           ;Test ROM cartridge present
  282.   000C4 CDC901          CALL    01C9H           ;CLS
  283.   000C7 211801          LD      HL,0118H        ;HL -> 'MEM SIZE'
  284.   000CA CDA728          CALL    28A7H           ;Print text
  285.   000CD CDB31B          CALL    1BB3H           ;Print '?' and wait for
  286.                                                 ;input
  287.                                                 ;<BREAK> pressed ?
  288.   000D0 38F5            JR      C,00C7H         ;Yes: repeat input
  289.   000D2 D7              RST     10H             ;Get next non-space character
  290.   000D3 B7              OR      A               ;Only <RETURN> entered ?
  291.   000D4 2013            JR      NZ,00E9H        ;No: an number was entered,
  292.                                                 ;continue a 00E9H
  293.   000D6 210040          LD      HL,4000H        ;Yes: HL -> start of RAM
  294.   000D9 23              INC     HL              ;HL + 1
  295.   000DA 7C              LD      A,H             ;HL = 0000 ?
  296.   000DB B5              OR      L               ;(Complete memory tested)
  297.   000DC 281C            JR      Z,00FAH         ;Yes: continue at 00FAH
  298.   000DE 7E              LD      A,(HL)          ;Take byte from memory
  299.   000DF 47              LD      B,A             ;Save it in B
  300.   000E0 2F              CPL                     ;Complement A
  301.   000E1 77              LD      (HL),A          ;Put it back in memory
  302.   000E2 BE              CP      (HL)            ;Is it really in RAM ?
  303.   000E3 70              LD      (HL),B          ;Restore original value
  304.   000E4 28F3            JR      Z,00D9H         ;Yes: next byte
  305.                                                 ;No: HL -> end of RAM + 1
  306.   000E6 25              DEC     H               ;Subtract 256 bytes for
  307.                                                 ;SHAPE table
  308.   000E7 1811            JR      00FAH           ;Continue at 00FAH
  309.  
  310. ; Enter number at MEM SIZE?
  311.  
  312.   000E9 CD5A1E          CALL    1E5AH           ;Evaluate entered number
  313.   000EC B7              OR      A               ;Only digits entered ?
  314.   000ED C29719          JP      NZ,1997H        ;No: ?SN Error
  315.  
  316.   000F0 EB              EX      DE,HL
  317.   000F1 2B              DEC     HL              ;HL = MEM SIZE - 1
  318.   000F2 3E8F            LD      A,8FH           ;A = test byte
  319.   000F4 46              LD      B,(HL)          ;Save original byte
  320.   000F5 77              LD      (HL),A          ;Testbyte to (HL)
  321.   000F6 BE              CP      (HL)            ;Is it really saved ?
  322.   000F7 70              LD      (HL),B          ;Put original value back
  323.   000F8 20CD            JR      NZ,00C7H        ;No: again!
  324.  
  325. ; Store memory boundaries
  326. ; HL -> highest available memory location - 1
  327.  
  328.   000FA 2B              DEC     HL              ;HL - 1
  329.   000FB 111444          LD      DE,4414H        ;DE = 4414H
  330.   000FE DF              RST     18H             ;Compare HL with DE
  331.                                                 ;MEM SIZE < 4414H ?
  332.   000FF DA7A19          JP      C,197AH         ;Yes: ?OM Error
  333.   00102 11CEFF          LD      DE,0FFCEH       ;DE = -50
  334.   00105 22B140          LD      (40B1H),HL      ;Store TOPMEM
  335.   00108 19              ADD     HL,DE           ;HL = HL - 50: CLEAR 50
  336.   00109 22A040          LD      (40A0H),HL      ;Save start address of
  337.                                                 ;string space
  338.   0010C CD4D1B          CALL    1B4DH           ;NEW and CLEAR
  339.   0010F 212101          LD      HL,0121H        ;HL -> 'COLOUR BASIC'
  340.   00112 CDA728          CALL    28A7H           ;Print text
  341.   00115 C3191A          JP      1A19H           ;Jump to BASIC active command
  342.                                                 ;mode
  343.  
  344. ; Text 'MEM SIZE'
  345.  
  346.   00118 4D454D          DEFB    'MEM'
  347.   0011B 20              DEFB    ' '
  348.   0011C 53495A45        DEFB    'SIZE'
  349.   00120 00              DEFB    00H             ;End of string
  350.  
  351. ; Text 'COLOUR BASIC'
  352.  
  353.   00121 434F4C4F5552    DEFB    'COLOUR'
  354.   00127 20              DEFB    ' '
  355.   00127 4241534943      DEFB    'BASIC'
  356.   0012D 0D              DEFB    0DH             ;Newline
  357.   0012E 00              DEFB    00H             ;End of string
  358.  
  359. ; Unused ROM space
  360.  
  361.   0012F FF              RST     38H             ;--
  362.   00130 FF              RST     38H
  363.   00131 FF              RST     38H
  364.  
  365.  
  366. ; X = CHECK ( Bit, Address)
  367. ; -------------------------
  368.  
  369.   00132 C36B01          JP      016BH           ;Continue at 016BH
  370.  
  371. ; SET Bit, Address
  372. ; ----------------
  373.  
  374.   00135 C34F01          JP      014FH           ;Continue at 014FH
  375.  
  376. ; RESET Bit, Address
  377. : ------------------
  378.  
  379.   00138 C35D01          JP      015DH           ;Continue at 015DH
  380.  
  381. ; Entry with Disk BASIC vectors disabled
  382.  
  383.   0013B 1E2C            LD      E,2CH           ;E = Errorcode for ?SN Error
  384.                                                 ;without EDIT call
  385.   0013D C3A219          JP      19A2H           ;Continue at error routine
  386.  
  387.  
  388. ; Start 5 (Continuation of 0078H)
  389.  
  390.   00140 EDB0            LDIR                    ;Copy
  391.   00142 2101C0          LD      HL,0C001H       ;HL -> ROM catridge
  392.   00145 3A00C0          LD      A,(0C000H)      ;ROM cartridge with
  393.   00148 B7              OR      A               ;a BASIC program present ?
  394.   00149 C27B00          JP      NZ,007BH        ;No: continue at 007BH
  395.   0014C C38B00          JP      008BH           ;Yes: continue at 0088H
  396.  
  397.  
  398. ; Continuation of SET routine of 0135H
  399.  
  400.   0014F CD8301          CALL    0183H           ;Get bitnr. and address
  401.   00152 3E01            LD      A,01H           ;A = bit mask
  402.   00154 07              RLCA                    ;Shift A until specified bit
  403.   00155 10FD            DJNZ    0154H           ;is set in A
  404.   00157 0F              RRCA                    ;Ajust because B was bitnr + 1
  405.   00158 47              LD      B,A             ;B = bit mask
  406.   00159 1A              LD      A,(DE)          ;Take byte from memory
  407.   0015A B0              OR      B               ;Set bit
  408.   0015B 12              LD      (DE),A          ;Put byte back in memory
  409.   0015C C9              RET
  410.  
  411.  
  412. ; Continuation of RESET routine of 0138H
  413.  
  414.   0015D CD8301          CALL    0183H          ;Get bitnr. and address
  415.   00160 3EFE            LD      A,0FEH         ;A = bit mask
  416.   00162 07              RLCA                   ;Shift A until specified bit
  417.   00163 10FD            DJNZ    0162H          ;is reset in A
  418.   00165 0F              RRCA                   ;Ajust because B was bitnr + 1
  419.   00166 47              LD      B,A            ;B = bit mask
  420.   00167 1A              LD      A,(DE)         ;Take byte from memory
  421.   00168 A0              AND     B              ;Reset bit
  422.   00169 12              LD      (DE),A         ;Put byte back in memory
  423.   0016A C9              RET
  424.  
  425.  
  426. ; Continuation of CHECK routine of 0132H
  427.  
  428.   0016B D7              RST     10H             ;Get next non-space caharacter
  429.   0016C CF              RST     08H             ;Next byte must be
  430.   0016D 28              DEFB    '('             ;a '('
  431.   0016E CD8301          CALL    0183H           ;Get bitnr and address
  432.   00171 E5              PUSH    HL              ;Save PTP
  433.   00172 1A              LD      A,(DE)          ;Get byte from memory
  434.   00173 1F              RRA                     ;and specified bit in C-flag
  435.   00174 10FD            DJNZ    0173H           ;Loop
  436.   00176 21FFFF          LD      HL,0FFFFH       ;HL = -1 (TRUE)
  437.                                                 ;Bit set ?
  438.   00179 3801            JR      C,017CH         ;Yes: continue at 017CH
  439.   0017B 23              INC     HL              ;HL = 0 (FALSE)
  440.   0017C CD9A0A          CALL    0A9AH           ;Write HL to X as INT
  441.   0017F E1              POP     HL              ;Restore PTP
  442.   00180 CF              RST     08H             ;Next byte must be
  443.   00181 29              DEFB    ')'             ;')'
  444.   00182 C9              RET
  445.  
  446.  
  447. ; SUB for SET, RESET and CHECK
  448. ; Get bit number and address form program text
  449. ;
  450. ; I: HL = PTP
  451. ; O: B = Bit number + 1
  452. ;    DE = Address
  453.  
  454.   00183 CD1C2B          CALL    2B1CH           ;A = Bit number
  455.   00186 FE08            CP      08H             ;Out of range ?
  456.   00188 D24A1E          JP      NC,1E4AH        ;Yes: ?FC Error
  457.   0018B F5              PUSH    AF              ;Save bit number
  458.   0018C CF              RST     08H             ;Both parameters separated by
  459.   0018D 2C              DEFB    ','             ;a comma ?
  460.   0018E CD022B          CALL    2B02H           ;DE = address
  461.   00191 F1              POP     AF              ;Restore bit number
  462.   00192 47              LD      B,A             ;Put it in B
  463.   00193 04              INC     B               ;B = bit number + 1
  464.   00194 C9              RET
  465.  
  466.  
  467. ; Unused ROM space
  468.  
  469.   00195 FF              RST     38H             ;--
  470.   00196 FF              RST     38H
  471.   00197 FF              RST     38H
  472.   00198 FF              RST     38H
  473.   00199 FF              RST     38H
  474.   0019A FF              RST     38H
  475.   0019B FF              RST     38H
  476.   0019C FF              RST     38H
  477.  
  478.  
  479. ; X = INKEY$
  480. ; ----------
  481.  
  482.   0019D D7              RST     10H             ;Get next non-space character
  483.   0019E E5              PUSH    HL              ;Save PTP
  484.   0019F 3A9940          LD      A,(4099H)       ;Has a key already been pressed
  485.   001A2 B7              OR      A               ;before ?
  486.   001A3 2006            JR      NZ,01ABH        ;Yes, pass value and return
  487.  
  488.   001A5 CD5803          CALL    0358H           ;No: key pressed now ?
  489.   001A8 B7              OR      A
  490.   001A9 2811            JR      Z,01BCH         ;No: null string as result
  491.  
  492.   001AB F5              PUSH    AF              ;Save key code
  493.   001AC AF              XOR     A               ;Delete last key code
  494.   001AD 329940          LD      (4099H),A
  495.   001B0 3C              INC     A               ;Reserve a byte in
  496.   001B1 CD5728          CALL    2857H           ;string space
  497.   001B4 F1              POP     AF              ;A = key code
  498.   001B5 2AD440          LD      HL,(40D4H)      ;HL -> memory for new string
  499.   001B8 77              LD      (HL),A          ;Save string
  500.   001B9 C38428          JP      2884H           ;Set VT to STR
  501.  
  502. ; No key pressed
  503. ; Return a null string as result
  504.  
  505.   001BC 212819          LD      HL,1928H        ;HL -> null string
  506.   001BF 222141          LD      (4121H),HL      ;Set as result
  507.   001C2 3E03            LD      A,03H           ;Set VT to STR
  508.   001C4 32AF40          LD      (40AFH),A
  509.   001C7 E1              POP     HL              ;Restore PTP
  510.   001C8 C9              RET
  511.  
  512.  
  513. ; CLS statement
  514. ; -------------
  515.  
  516.   001C9 3E1C            LD      A,1CH           ;A = cursor home
  517.   001CB CD3A03          CALL    033AH           ;Output A
  518.   001CE 3E1F            LD      A,1FH           ;A = clear screen from
  519.                                                 ;cursor pos. to end
  520.   001D0 C33A03          JP      033AH           :Output A
  521.  
  522.  
  523. ; RANDOM statement
  524. ; ----------------
  525.  
  526.   001D3 ED5F            LD      A,R             ;A = refresh register
  527.   001D5 32AB40          LD      (40ABH),A       ;Put it in system RAM
  528.   001D8 C9              RET
  529.  
  530.  
  531. ; Issue a level change to cassette port
  532.  
  533.   001D9 3A1C43          LD      A,(431CH)       ;A = last written value
  534.   001DC EE01            XOR     01H             ;Toggle bit 0
  535.   001DE D3FF            OUT     (0FFH),A        ;Output value to cassette port
  536.   001E0 321C43          LD      (431CH),A       ;and save it is system RAM
  537.   001E3 C9              RET
  538.  
  539. ; blink '*'
  540.  
  541.   001E4 3A2744          LD      A,(4427H)       ;Get '*' or ' ' from screen
  542.   001E7 EE0A            XOR     0AH             ;Swap '*' and ' '
  543.   001E9 322744          LD      (4427H),A       ;Put it back on screen
  544.   001EC C9              RET
  545.  
  546. ; Read one byte from cassette (AF)
  547. ;
  548. ; I: -
  549. ; O: A = byte read from cassette port
  550.  
  551.   001ED D9              EXX                     ;Exchange registers
  552.  
  553.   001EE 0608            LD      B,08H           ;Read 8 bits
  554.   001F0 1600            LD      D,00H           ;Put byte to 0
  555.   001F2 CDFA01          CALL    01FAH           ;Read bit and shift into D
  556.   001F5 10FB            DJNZ    01F2H           ;Next bit
  557.  
  558.   001F7 7A              LD      A,D             ;A = complete byte read
  559.   001F8 D9              EXX                     ;Exchange registers
  560.   001F9 C9              RET
  561.  
  562.  
  563. ; Read one bit from cassette and shift into D
  564.  
  565.   001FA C5              PUSH    BC              ;Save counter
  566.                                                 ;Wait for clock pulse:
  567.   001FB DBFF            IN      A,(0FFH)        ;Test port FFH
  568.   001FD E601            AND     01H             ;Mask level
  569.   001FF 5F              LD      E,A             ;and save in E
  570.  
  571.   00200 DBFF            IN      A,(0FFH)        ;Test port FFH
  572.   00202 E601            AND     01H             ;Mask level
  573.   00204 AB              XOR     E               ;Compare with previous level
  574.   00205 1F              RRA                     ;Put result in C-flag
  575.   00206 30F8            JR      NC,0200H        ;Search clock pulse
  576.  
  577.   00208 3C              INC     A               ;A = 1
  578.   00209 AB              XOR     E               ;A = new level value
  579.   0020A 5F              LD      E,A             ;Store it in E for compare
  580.   0020B 3A1243          LD      A,(4312H)       ;A = delay time
  581.   0020E 47              LD      B,A             ;Put it in B
  582.   0020F 10FE            DJNZ    020FH           ;Delay loop
  583.  
  584.   00211 DBFF            IN      A,(0FFH)        ;Now take new input level
  585.   00213 E601            AND     01H
  586.   00215 AB              XOR     E               ;and compare it with previous
  587.                                                 ;level. A = 1 if a level change
  588.                                                 ;is recognized.
  589.   00216 CB22            SLA     D               ;Shift D
  590.   00218 B2              OR      D               ;and add new value
  591.   00219 57              LD      D,A             ;Result in D
  592.   0021A C1              POP     BC              ;Restore counter
  593.   0021B C9              RET
  594.  
  595.  
  596. ; Write the same byte twice to cassette
  597.  
  598.   0021C CD1F02          CALL    021FH           ;Write first byte
  599.  
  600.  
  601. ; Write one byte to cassette (AF)
  602. ;
  603. ; I: A = byte to be written
  604. ; O: A = byte written
  605.  
  606.   0021F D9              EXX                     ;Save registers
  607.   00220 F5              PUSH    AF              ;Save byte
  608.   00221 0E08            LD      C,08H           ;8 bits to write
  609.   00223 57              LD      D,A             ;D = byte
  610.   00224 CDD901          CALL    01D9H           ;Issue clock pulse
  611.   00227 3A1043          LD      A,(4310H)       ;A = delay value
  612.   0022A 47              LD      B,A             ;B = A
  613.   0022B 10FE            DJNZ    022BH           ;Delay loop
  614.  
  615.   0022D 7A              LD      A,D             ;A = byte
  616.   0022E 07              RLCA                    ;Shift next bit into C-Flag
  617.   0022F 57              LD      D,A             ;And save shifted value in D
  618.   00230 DCD901          CALL    C,01D9H         ;Bit = 1: Issue level change
  619.   00233 3A1143          LD      A,(4311H)       ;A = delay value
  620.   00236 47              LD      B,A             ;B = A
  621.   00237 10FE            DJNZ    0237H           ;Delay loop
  622.  
  623.   00239 0D              DEC     C               ;Decrement bit counter
  624.                                                 ;All bits done ?
  625.   0023A 20E8            JR      NZ,0224H        ;No: loop, issue next bit
  626.   0023C F1              POP     AF              ;Restore byte value
  627.   0023D D9              EXX                     ;Restore registers
  628.   0023E C9              RET
  629.  
  630.  
  631. ; Write leader and sync on cassette
  632.  
  633.   0023F 06FF            LD      B,0FFH          ;write 255 times
  634.   00241 3EAA            LD      A,0AAH          ;the byte AAH
  635.   00243 CD1F02          CALL    021FH           ;on cassette tape
  636.   00246 10FB            DJNZ    0243H
  637.  
  638.   00248 3E66            LD      A,66H           ;Sync = 66H
  639.   0024A 18D3            JR      021FH           ;Write on cassette tape
  640.  
  641.  
  642. ; Search for leader and sync
  643.  
  644.   0024C E5              PUSH    HL              ;Save registers
  645.   0024D D5              PUSH    DE
  646.   0024E C5              PUSH    BC
  647.  
  648.   0024F 216935          LD      HL,3569H        ;HL -> colour code table
  649.   00252 110000          LD      DE,0000H        ;DE = 0000H
  650.   00255 3A2340          LD      A,(4023H)       ;A = present colour value
  651.   00258 5F              LD      E,A             ;DE = offset in table
  652.   00259 19              ADD     HL,DE           ;HL -> colour code
  653.   0025A 7E              LD      A,(HL)          ;A = colour code from table
  654.   0025B 3226F0          LD      (0F026H),A      ;Store in colour RAM at the
  655.   0025E 3227F0          LD      (0F027H),A      ;location for both asterisks
  656.  
  657.   00261 01AA80          LD      BC,80AAH        ;B = counter = 128
  658.                                                 ;C = byte to search for
  659.   00264 CDFA01          CALL    01FAH           ;Read bit from cassette,
  660.                                                 ;shift into D and load in A
  661.   00267 B9              CP      C               ;Byte found ?
  662.   00268 20F7            JR      NZ,0261H        ;No: search again
  663.  
  664.   0026A 3EFF            LD      A,0FFH          ;Yes: invert all bits in C
  665.   0026C A9              XOR     C               ;so search for 55H now
  666.   0026D 4F              LD      C,A             ;Result in C
  667.   0026E 10F4            DJNZ    0264H           ;Continue search until 128
  668.                                                 ;times AAH found
  669.   00270 CDFA01          CALL    01FAH           ;Search sync (66H)
  670.   00273 FE66            CP      66H             ;Found ?
  671.   00275 20F9            JR      NZ,0270H        ;No: continue seach
  672.  
  673.   00277 3E2A            LD      A,2AH           ;A = '*'
  674.   00279 322644          LD      (4426H),A       ;Put both asterisks on screen
  675.   0027C 322744          LD      (4427H),A
  676.  
  677.   0027F C1              POP     BC              ;Restore registers
  678.   00280 D1              POP     DE
  679.   00281 E1              POP     HL
  680.   00282 C9              RET
  681.  
  682.  
  683. ; Unused ROM area
  684.  
  685.   00283 FF              RST     38H             ;--
  686.   00284 FF              RST     38H
  687.   00285 FF              RST     38H
  688.   00286 FF              RST     38H
  689.   00287 FF              RST     38H
  690.   00288 FF              RST     38H
  691.   00289 FF              RST     38H
  692.   0028A FF              RST     38H
  693.   0028B FF              RST     38H
  694.   0028C FF              RST     38H
  695.   0028D FF              RST     38H
  696.   0028E FF              RST     38H
  697.   0028F FF              RST     38H
  698.   00290 FF              RST     38H
  699.   00291 FF              RST     38H
  700.   00292 FF              RST     38H
  701.   00293 FF              RST     38H
  702.   00294 FF              RST     38H
  703.   00295 FF              RST     38H
  704.   00296 FF              RST     38H
  705.   00297 FF              RST     38H
  706.   00298 FF              RST     38H
  707.   00299 FF              RST     38H
  708.   0029A FF              RST     38H
  709.   0029B FF              RST     38H
  710.   0029C FF              RST     38H
  711.   0029D FF              RST     38H
  712.   0029E FF              RST     38H
  713.   0029F FF              RST     38H
  714.   002A0 FF              RST     38H
  715.   002A1 FF              RST     38H
  716.   002A2 FF              RST     38H
  717.   002A3 FF              RST     38H
  718.   002A4 FF              RST     38H
  719.   002A5 FF              RST     38H
  720.   002A6 FF              RST     38H
  721.   002A7 FF              RST     38H
  722.   002A8 FF              RST     38H
  723.   002A9 FF              RST     38H
  724.   002AA FF              RST     38H
  725.   002AB FF              RST     38H
  726.  
  727.  
  728. ; Read start address of loaded program from tape (for SYSTEM)
  729.  
  730.   002AC CD1403          CALL    0314H           ;Read 2 bytes (start address)
  731.   002AF 22DF40          LD      (40DFH),HL      ;and store it in system RAM
  732.  
  733.  
  734. ; SYSTEM statement
  735. ; ----------------
  736.  
  737.   002B2 CDE241          CALL    41E2H           ;DOS
  738.   002B5 318842          LD      SP,4288H        ;Put stack in keyboard buffer
  739.                                                 ;to get it out of the way
  740.   002B8 CDFE20          CALL    20FEH           ;Start new line
  741.   002BB 3E2A            LD      A,2AH           ;A = '*'
  742.   002BD CD2A03          CALL    032AH           ;Print it
  743.   002C0 CDB31B          CALL    1BB3H           ;Ask for filename
  744.   002C3 DA6600          JP      C,0066H         ;Back to BASIC
  745.                                                 ;if <BREAK> key pressed
  746.  
  747.   002C6 D7              RST     10H             ;Get next non-space character
  748.                                                 ;Anything entered ?
  749.   002C7 CA9719          JP      Z,1997H         ;No: ?SN Error
  750.   002CA FE2F            CP      '/'             ;Is it a '/' ?
  751.   002CC 284F            JR      Z,031DH         ;Yes: continue at 031DH
  752.  
  753.   002CE CD4C02          CALL    024CH           ;No: search for leader and sync
  754.   002D1 CDED01          CALL    01EDH           ;Get first byte from tape
  755.   002D4 FE55            CP      55H             ;Is it a SYSTEM program ?
  756.   002D6 20F9            JR      NZ,02D1H        ;No: continue search
  757.  
  758.   002D8 0606            LD      B,06H           ;6 characters for filename
  759.  
  760.   002DA 7E              LD      A,(HL)          ;Get character from buffer
  761.   002DB B7              OR      A               ;End of line ?
  762.   002DC 2809            JR      Z,02E7H         ;Yes: load program
  763.  
  764.   002DE CDED01          CALL    01EDH           ;No: load filename character
  765.   002E1 BE              CP      (HL)            ;Same as in specified name ?
  766.   002E2 20ED            JR      NZ,02D1H        ;No: search next file
  767.  
  768.   002E4 23              INC     HL              ;Yes: pointer + 1
  769.   002E5 10F3            DJNZ    02DAH           ;Check next filename char.
  770.  
  771.   002E7 CDE401          CALL    01E4H           ;Blink right asterisk
  772.   002EA CDED01          CALL    01EDH           ;Read block marker from tape
  773.   002ED FE78            CP      78H             ;End found ?
  774.   002EF 28BB            JR      Z,02ACH         ;Yes: get start address and
  775.                                                 ;issue '*?' again
  776.   002F1 FE3C            CP      3CH             ;Start of block ?
  777.   002F3 20F5            JR      NZ,02EAH        ;No: search new byte
  778.  
  779.   002F5 CDED01          CALL    01EDH           ;Read block length
  780.   002F8 47              LD      B,A             ;B = block length
  781.   002F9 CD1403          CALL    0314H           ;HL = block address
  782.   002FC 85              ADD     A,L             ;Calculate checksum
  783.   002FD 4F              LD      C,A             ;Store in C
  784.   002FE CDED01          CALL    01EDH           ;Read byte
  785.   00301 77              LD      (HL),A          ;Store it in memory
  786.   00302 23              INC     HL              ;Memory pointer + 1
  787.   00303 81              ADD     A,C             ;Add to checksum
  788.   00304 4F              LD      C,A             ;Store result in C
  789.   00305 10F7            DJNZ    02FEH           ;Load next byte from block
  790.  
  791.   00307 CDED01          CALL    01EDH           ;Block is done: get checksum
  792.   0030A B9              CP      C               ;Same as calculated checksum ?
  793.   0030B 28DA            JR      Z,02E7H         ;Yes: load next block
  794.  
  795.   0030D 3E43            LD      A,43H           ;No: A = 'C'
  796.   0030F 322644          LD      (4426H),A       ;Indicate checksum error
  797.   00312 18D6            JR      02EAH           ;Continue loading anyway
  798.  
  799. ; SUB for SYSTEM
  800. ; Load address (block address or start address) from tape
  801. ;
  802. ; I: -
  803. ; O: HL = address read from tape
  804.  
  805.   00314 CDED01          CALL    01EDH           ;Load LSB from tape
  806.   00317 6F              LD      L,A             ;L = LSB of address
  807.   00318 CDED01          CALL    01EDH           ;Load MSB from tape
  808.   0031B 67              LD      H,A             ;H = MSB of address
  809.   0031C C9              RET
  810.  
  811. ; Issue '/' with SYSTEM
  812.  
  813.   0031D EB              EX      DE,HL           ;DE -> text
  814.   0031E 2ADF40          LD      HL,(40DFH)      ;HL = start address
  815.   00321 EB              EX      DE,HL           ;Load DE
  816.   00322 D7              RST     10H             ;Get next non-space character
  817.                                                 ;Start address entered ?
  818.   00323 C45A1E          CALL    NZ,1E5AH        ;Yes: new value into DE
  819.   00326 208A            JR      NZ,02B2H        ;Restart SYSTEM if rubbish
  820.                                                 ;was entered
  821.  
  822.   00328 EB              EX      DE,HL           ;Load address into HL
  823.   00329 E9              JP      (HL)            ;Execute program
  824.  
  825.  
  826. ; Output of A on screen, printer or cassette (AF,HL)
  827. ;
  828. ; I: A = ASCII value of the byte to be output
  829. ;    (409CH) = output flag: 00H = screen, 01H = printer, 80H = cassette
  830.  
  831.   0032A C5              PUSH    BC              ;Save BC
  832.   0032B 4F              LD      C,A             ;C = output byte
  833.  
  834.   0032C CDC141          CALL    41C1H           ;DOS
  835.   0032F 3A9C40          LD      A,(409CH)       ;Test flag
  836.   00332 B7              OR      A
  837.   00333 79              LD      A,C             ;Byte back in A
  838.   00334 C1              POP     BC              ;Restore  BC
  839.   00335 C36405          JP      0564H           ;Continue at 0564H
  840.  
  841.  
  842. ; Unused ROM space
  843.  
  844.   00338 FF              RST     38H             ;--
  845.   00339 FF              RST     38H
  846.  
  847.  
  848. ; Output A on screen and increment POS (40A6H)
  849.  
  850.   0033A D9              EXX                     ;Save registers
  851.   0033B F5              PUSH    AF              ;Save byte
  852.   0033C CD3300          CALL    0033H           ;Output byte
  853.   0033F CD4803          CALL    0348H           ;Calculate new POS value
  854.   00342 32A640          LD      (40A6H),A       ;and save it in system RAM
  855.   00345 F1              POP     AF              ;Restore byte
  856.   00346 D9              EXX                     ;Restore registers
  857.   00347 C9              RET
  858.  
  859. ; Calculate new POS (AF,DE)
  860. ; I: -
  861. ; O: A = new POS value
  862.  
  863.   00348 E5              PUSH    HL              ;Save PTP
  864.   00349 2A2040          LD      HL,(4020H)      ;HL = cursor address
  865.   0034C 110044          LD      DE,4400H        ;DE -> start of screen memory
  866.   0034F B7              OR      A               ;C-flag = 0
  867.   00350 C3D904          JP      04D9H           ;Continue at 04D9H
  868.  
  869.  
  870. ; Unused ROM space
  871.  
  872.   00353 FF              RST     38H             ;--
  873.   00354 FF              RST     38H
  874.  
  875.  
  876. ; Calculate new POS (as 0348H)
  877.  
  878.   00355 C39D30          JP      309DH           ;continue at 309DH
  879.  
  880.  
  881. ; Keyboard scan (AF)
  882. ; (as 002B but with DOS and saving DE)
  883.  
  884.   00358 CDC441          CALL    41C4H           ;DOS
  885.   0035B D5              PUSH    DE              ;Save DE
  886.   0035C CD2B00          CALL    002BH           ;Scan keyboard
  887.   0035F D1              POP     DE              ;Restore DE
  888.   00360 C9              RET
  889.  
  890.  
  891. ; Input of a line with max. 240 characters in line buffer (AF,DE,HL)
  892. ;
  893. ; I: -
  894. ; O: C-flag = 1: <BREAK> key pressed.
  895. ;    HL -> start of line buffer - 1
  896.  
  897.   00361 AF              XOR     A               ;A = 00H
  898.   00362 329940          LD      (4099H),A       ;Delete last key code
  899.   00365 32A640          LD      (40A6H),A       ;POS = 0
  900.   00368 CDAF41          CALL    41AFH           ;DOS
  901.   0036B C5              PUSH    BC              ;Save BC
  902.   0036C 2AA740          LD      HL,(40A7H)      ;HL -> start of line buffer
  903.   0036F 06F0            LD      B,0F0H          ;B = max. number of characters
  904.   00371 CDD905          CALL    05D9H           ;Enter line
  905.   00374 F5              PUSH    AF              ;Save flags
  906.   00375 48              LD      C,B             ;C = number of entered chars.
  907.   00376 0600            LD      B,00H           ;BC = length of line
  908.   00378 09              ADD     HL,BC           ;HL -> last character + 1
  909.   00379 3600            LD      (HL),00H        ;Terminate line with 00H
  910.   0037B 2AA740          LD      HL,(40A7H)      ;HL -> start of line buffer
  911.   0037E F1              POP     AF              ;Restore flags
  912.   0037F C1              POP     BC              ;Restore BC
  913.   00380 2B              DEC     HL              ;Adjust HL for RST 10H
  914.                                                 ;<BREAK> pressed ?
  915.   00381 D8              RET     C               ;Yes: return
  916.  
  917.   00382 AF              XOR     A               ;C-flag = 0
  918.   00383 C9              RET
  919.  
  920.  
  921. ; Wait for key pressed (AF)
  922. ; (as 0049H but with DOS and save DE)
  923.  
  924.   00384 CD5803          CALL    0358H           ;Get key
  925.   00387 B7              OR      A               ;New key pressed ?
  926.   00388 C0              RET     NZ              ;Yes: return
  927.   00389 18F9            JR      0384H           ;No: wait for key pressed
  928.  
  929.  
  930. ; End output to printer (AF)
  931. ;
  932. ; I: -
  933. ; O: PPOS = 0 (Printer POS)
  934.  
  935.   0038B AF              XOR     A               ;Set output flag
  936.   0038C 329C40          LD      (409CH),A       ;to screen output
  937.   0038F 3A9B40          LD      A,(409BH)       ;A = PPOS
  938.   00392 B7              OR      A               ;Zero ?
  939.   00393 C8              RET     Z               ;Yes: return
  940.  
  941.   00394 3E0D            LD      A,0DH           ;No: CR to printer
  942.   00396 D5              PUSH    DE              ;Save DE
  943.   00397 CD9C03          CALL    039CH           ;Output CR
  944.   0039A D1              POP     DE              ;Restore DE
  945.   0039B C9              RET
  946.  
  947.  
  948. ; Print one character on printer and  intercept control characters
  949.  
  950.   0039C F5              PUSH    AF              ;Save registers
  951.   0039D D5              PUSH    DE
  952.   0039E C5              PUSH    BC
  953.   0039F 4F              LD      C,A             ;Save character in C
  954.   003A0 1E00            LD      E,00H           ;E = 00H for new PPOS
  955.   003A2 FE0C            CP      0CH             ;Is it a Form Feed ?
  956.   003A4 2810            JR      Z,03B6H         ;Yes: PPOS to 0 and print
  957.                                                 ;character
  958.   003A6 FE0A            CP      0AH             ;Is it a Line Feed ?
  959.   003A8 2003            JR      NZ,03ADH        ;No: continue test
  960.  
  961.   003AA 3E0D            LD      A,0DH           ;Convert LF into CR
  962.   003AC 4F              LD      C,A             ;Store it in C
  963.  
  964.   003AD FE0D            CP      0DH             ;Is it a Carriage Return ?
  965.   003AF 2805            JR      Z,03B6H         ;Yes: PPOS to 0 and print
  966.                                                 ;character
  967.   003B1 3A9B40          LD      A,(409BH)       ;No: get PPOS
  968.   003B4 3C              INC     A               ;and increment
  969.   003B5 5F              LD      E,A             ;Store in E
  970.   003B6 7B              LD      A,E             ;Save new PPOS
  971.   003B7 329B40          LD      (409BH),A       ;in system RAM
  972.   003BA 79              LD      A,C             ;A = character to print
  973.   003BB CD3B00          CALL    003BH           ;Print it
  974.   003BE C1              POP     BC              ;Restore registers
  975.   003BF D1              POP     DE
  976.   003C0 F1              POP     AF
  977.   003C1 C9              RET
  978.  
  979.  
  980. ; DCB call (AF) (see 0046H)
  981. ;
  982. ; I: A = character (when output)
  983. ;    B = DCB type
  984. ;    DE -> DCB
  985. ; O: A = character (when input)
  986.  
  987.   003C2 E5              PUSH    HL              ;Save registers
  988.   003C3 DDE5            PUSH    IX
  989.   003C5 D5              PUSH    DE
  990.   003C6 DDE1            POP     IX              ;IX -> DCB
  991.   003C8 D5              PUSH    DE              ;Adjust stack
  992.   003C9 21DD03          LD      HL,03DDH        ;HL = new return address
  993.   003CC E5              PUSH    HL              ;Put it in stack
  994.   003CD 4F              LD      C,A             ;Save character in C
  995.   003CE 1A              LD      A,(DE)          ;Load DCB type
  996.   003CF A0              AND     B               ;Mask unused bits
  997.   003D0 B8              CP      B               ;Same as indicated type ?
  998.   003D1 C23340          JP      NZ,4033H        ;No: false DCB type, set A
  999.                                                 ;to 00H and back to program
  1000.                                                 ;(4033H is used by DOS)
  1001.   003D4 FE02            CP      02H             ;DCB type = output ?
  1002.   003D6 DD6E01          LD      L,(IX01H)       ;HL = address of DCB routine
  1003.   003D9 DD6602          LD      H,(IX02H)
  1004.   003DC E9              JP      (HL)            ;Execute DCB routine
  1005.  
  1006. ; End of DCB routine
  1007.  
  1008.   003DD D1              POP     DE              ;Restore registers
  1009.   003DE DDE1            POP     IX
  1010.   003E0 E1              POP     HL
  1011.   003E1 C1              POP     BC
  1012.   003E2 C9              RET
  1013.  
  1014.  
  1015. ; Keyboard routine (called by DCB)
  1016.  
  1017.   003E3 CDAF06          CALL    06AFH           ;ROM catrigde present ?
  1018.                                                 ;Yes: start ROM program
  1019.   003E6 3A80F8          LD      A,(0F880H)      ;Scan keyboard
  1020.   003E9 FE12            CP      12H             ;<CTRL> and <MOD SEL> pressed ?
  1021.   003EB 200D            JR      NZ,03FAH        ;No: continue at 03FAH
  1022.  
  1023.   003ED CDA938          CALL    38A9H           ;FGR
  1024.  
  1025.   003F0 3A40F8          LD      A,(0F840H)      ;Scan keyboard
  1026.   003F3 CB57            BIT     2,A             ;<BREAK> pressed ?
  1027.   003F5 28F9            JR      Z,03F0H         ;No: wait for <BREAK>
  1028.  
  1029.   003F7 CDB038          CALL    38B0H           ;LGR
  1030.   003FA 213640          LD      HL,4036H        ;HL -> temporary memory
  1031.   003FD 0101F8          LD      BC,0F801H       ;BC = first keyboard address
  1032.   00400 1600            LD      D,00H           ;Line counter = 0
  1033.   00402 0A              LD      A,(BC)          ;Scan keyboard
  1034.   00403 5F              LD      E,A             ;Result in E
  1035.   00404 AE              XOR     (HL)            ;Set only the bits for those
  1036.                                                 ;keys, that have changed since
  1037.                                                 ;last scan
  1038.   00405 73              LD      (HL),E          ;Store new code for next scan
  1039.   00406 A3              AND     E               ;Set only the bit for the key
  1040.                                                 ;key that was newly pressed
  1041.                                                 ;since last scan
  1042.   00407 2010            JR      NZ,0419H        ;Was a key pressed ?
  1043.                                                 ;Yes: find ASCII code of key
  1044.   00409 14              INC     D               ;No: line counter + 1
  1045.   0040A 2C              INC     L               ;Buffer pointer  + 1
  1046.   0040B CB01            RLC     C               ;Update keyboard address
  1047.   0040D 30F3            JR      NC,0402H        ;Scan next keyboard address
  1048.  
  1049.   0040F 3A80F8          LD      A,(0F880H)      ;Scan keyboard
  1050.   00412 CB5F            BIT     3,A             ;<RPT> pressed ?
  1051.   00414 C2D404          JP      NZ,04D4H        ;Yes: continue at 04D4H
  1052.  
  1053.   00417 AF              XOR     A               ;A = 00H
  1054.   00418 C9              RET
  1055.  
  1056. ; Find ASCII code of key
  1057.  
  1058.   00419 5F              LD      E,A             ;Bit value to E
  1059.   0041A 211840          LD      HL,4018H        ;HL = flag byte
  1060.   0041D 3A80F8          LD      A,(0F880H)      ;Scan keyboard
  1061.   00420 CB4F            BIT     1,A             ;<MOD SEL> pressed ?
  1062.   00422 C2C904          JP      NZ,04C9H        ;Yes: continue at 04C9H
  1063.  
  1064.   00425 CB67            BIT     4,A             ;<CTRL> pressed ?
  1065.   00427 C2D004          JP      NZ,04D0H        ;Yes: continue at 04D0H
  1066.  
  1067.   0042A 3E07            LD      A,07H           ;Was the key in the last
  1068.                                                 ;keyboard address and was not
  1069.                                                 ;intercepted ?
  1070.   0042C BA              CP      D               ;Line counter = 7 ?
  1071.   0042D 28E8            JR      Z,0417H         ;Yes: the key pressed was
  1072.                                                 ;the <SHIFT> key (code = 00H)
  1073.   0042F 7A              LD      A,D             ;Multiply D by 8
  1074.   00430 07              RLCA                    ;* 2
  1075.   00431 07              RLCA                    ;* 2
  1076.   00432 07              RLCA                    ;* 2
  1077.   00433 57              LD      D,A             ;Because there are 8 keys per
  1078.                                                 ;keyboard address
  1079.   00434 0E01            LD      C,01H           ;C = mask for calculation
  1080.   00436 79              LD      A,C             ;A = bit mask
  1081.   00437 A3              AND     E               ;Mask key code
  1082.                                                 ;Key newly pressed ?
  1083.   00438 2005            JR      NZ,043FH        ;Yes: continue at 043FH
  1084.  
  1085.   0043A 14              INC     D               ;Increment D to establish value
  1086.                                                 ;of key in keyboard matrix
  1087.   0043B CB01            RLC     C               ;Shift mask for next test
  1088.   0043D 18F7            JR      0436H
  1089.  
  1090. ; D now indicates the value of the key in the keyboard matrix
  1091. ; @ has value 00H, A has value 01H etc., upto the space bar which has
  1092. ; a value of 37H
  1093.  
  1094.   0043F 3A80F8          LD      A,(0F880H)      ;Scan keyboard
  1095.   00442 47              LD      B,A             ;Save value in B
  1096.   00443 7A              LD      A,D             ;A = matrix value
  1097.   00444 C640            ADD     A,40H           ;Matrix value + 40H gives
  1098.                                                 ;correct ASCII code for the
  1099.                                                 ;kes '@' to 'F4' (ASCII codes
  1100.                                                 ;40H to 5FH)
  1101.   00446 FE60            CP      60H             ;Value in range ?
  1102.   00448 3013            JR      NC,045DH        ;No: continue at 045DH
  1103.  
  1104.   0044A CB08            RRC     B               ;<SHIFT> pressed ?
  1105.   0044C 3031            JR      NC,047FH        ;No: key code is ok
  1106.  
  1107.   0044E C620            ADD     A,20H           ;Add 20H to get lower case
  1108.   00450 57              LD      D,A             ;Save value
  1109.   00451 3A40F8          LD      A,(0F840H)      ;Scan keyboard
  1110.   00454 E610            AND     10H             ;<SHIFT> + down arrow pressed ?
  1111.   00456 7A              LD      A,D             ;Restore value
  1112.   00457 2826            JR      Z,047FH         ;No: key code is ok
  1113.  
  1114.   00459 D660            SUB     60H             ;Convert value to control code
  1115.   0045B 1822            JR      047FH           ;Continue at 047FH
  1116.  
  1117. ; Numbers, special characters or control characters were pressed:
  1118. ; A has a value between 60H (for '0') and 77H (for space bar)
  1119.  
  1120.   0045D D670            SUB     70H             ;Control character pressed ?
  1121.   0045F 3010            JR      NC,0471H        ;Yes: continue at 0471H
  1122.  
  1123.   00461 C640            ADD     A,40H           ;Adjust code
  1124.   00463 FE3C            CP      3CH             ;',' '-' '.' or '/' pressed ?
  1125.   00465 3802            JR      C,0469H         ;No: correct ASCII code is
  1126.                                                 ;in A, continue at 0469H
  1127.   00467 EE10            XOR     10H             ;Yes: With these keys, the
  1128.                                                 ;symbols are swapped. This has
  1129.                                                 ;to be corrected with XOR 10H
  1130.                                                 ;(see ASCII table)
  1131.   00469 CB08            RRC     B               ;<SHIFT> pressed ?
  1132.   0046B 3012            JR      NC,047FH        ;No: pass on ASCII code
  1133.  
  1134.   0046D EE10            XOR     10H             ;Yes: exchange codes from
  1135.                                                 :20H to 2FH with 30H to 3FH
  1136.   0046F 180E            JR      047FH
  1137.  
  1138. ; Control key pressed
  1139. ; Take ASCII code from table at 0050H
  1140.  
  1141.   00471 07              RLCA                    ;A = A * 2 as table offset
  1142.   00472 CB08            RRC     B               ;<SHIFT> pressed ?
  1143.   00474 3001            JR      NC,0477H        ;No: offset is ok
  1144.  
  1145.   00476 3C              INC     A               ;Adjust offset
  1146.   00477 215000          LD      HL,0050H        ;HL -> table with ASCII codes
  1147.   0047A 5F              LD      E,A             ;DE = offset
  1148.   0047B 1600            LD      D,00H
  1149.   0047D 19              ADD     HL,DE           ;Set pointer on key code
  1150.   0047E 7E              LD      A,(HL)          ;A = key code
  1151.  
  1152. ; The correct ASCII code in now in A. Only the <MOD SEL> key has to be
  1153. ; checked.
  1154.  
  1155.   0047F 211840          LD      HL,4018H        ;HL = flag byte
  1156.   00482 CB76            BIT     6,(HL)          ;<MOD SEL> active ?
  1157.                                                 ;(graphic characters)
  1158.   00484 2824            JR      Z,04AAH         ;No: finish routine
  1159.  
  1160.   00486 FE2B            CP      2BH             ;ASCII code < 2BH
  1161.   00488 3820            JR      C,04AAH         ;Yes: these keys have no
  1162.                                                 ;graphic character
  1163.   0048A FE30            CP      30H             ;ASCII code between 2BH, 2FH
  1164.   0048C 3004            JR      NC,0492H        ;No: check further
  1165.  
  1166.   0048E D62B            SUB     2BH             ;Yes: subtract 2BH
  1167.   00490 1816            JR      04A8H           ;and add C0H so that '+' gets
  1168.                                                 ;code C0H and '/' gets code
  1169.                                                 ;C4H
  1170.  
  1171.   00492 FE3B            CP      3BH             ;Code smaller then ':' ?
  1172.   00494 3814            JR      C,04AAH         ;Yes: no graphic character
  1173.  
  1174.   00496 FE5B            CP      5BH             ;Code bigger then 'Z' ?
  1175.   00498 3004            JR      NC,049EH        ;Yes: clear <F1> - <F4>
  1176.  
  1177.   0049A D636            SUB     36H             ;':' to 'Z' becomes 05H to 24H
  1178.   0049C 180A            JR      04A8H           ;Add C0H for graphic char.
  1179.  
  1180.   0049E FE60            CP      60H             ;<F1> - <F4> ?
  1181.   004A0 3808            JR      C,04AAH         ;Yes: finish routine
  1182.  
  1183.   004A2 FE7B            CP      7BH             ;<SHIFT>+<F1> - <SHIFT>+<F4> ?
  1184.   004A4 3004            JR      NC,04AAH        ;Yes: finish routine
  1185.  
  1186.   004A6 D63B            SUB     3BH             ;''' to 'z' becomes 25H to 3FH
  1187.   004A8 C6C0            ADD     A,0C0H          ;Add C0H for graphic char.
  1188.  
  1189. ; Now A contains the right ASCII code from 01H to FFH
  1190.  
  1191.   004AA 322440          LD      (4024H),A       ;Save ASCII code for repeat
  1192.   004AD 57              LD      D,A             ;Save for delay loop
  1193.   004AE 010020          LD      BC,2000H        ;Call delay loop for
  1194.   004B1 CD6000          CALL    0060H           ;debouncing purposes
  1195.   004B4 7A              LD      A,D             ;Restore ASCII code
  1196.  
  1197.   004B5 FE0D            CP      0DH             ;<RETURN> pressed ?
  1198.   004B7 2807            JR      Z,04C0H         ;Yes: deactivate <MOD SEL>
  1199.  
  1200.   004B9 FE01            CP      01H             ;<BREAK pressed ?
  1201.   004BB 2803            JR      Z,04C0H         ;Yes: deactivate <MOD SEL>
  1202.  
  1203.   004BD C0              RET     NZ              ;Done when <BREAK> not pressed
  1204.  
  1205.   004BE EF              RST     28H             ;RST 28H when <BREAK> pressed
  1206.                                                 ;(used by DOS)
  1207.   004BF C9              RET
  1208.  
  1209. ; Deactivate <MOD SEL>
  1210.  
  1211.   004C0 211840          LD      HL,4018H        ;HL -> flag byte
  1212.   004C3 CBB6            RES     6,(HL)          ;Clear MOD SEL flag
  1213.   004C5 FE01            CP      01H             ;<BREAK> pressed ?
  1214.   004C7 18F4            JR      04BDH           ;Continue at 04DBH
  1215.  
  1216. ; <MOD SEL> pressed
  1217.  
  1218.   004C9 3E40            LD      A,40H           ;Set bit 6 (MOD SEL flag)
  1219.   004CB AE              XOR     (HL)            ;Toggle MOD SEL flag
  1220.   004CC 77              LD      (HL),A          ;Save it in system RAM
  1221.   004CD AF              XOR     A               ;Return no value
  1222.   004CE 18DD            JR      04ADH           ;Finish routine
  1223.  
  1224. ; <CTRL> pressed
  1225.  
  1226.   004D0 CBFE            SET     7,(HL)          ;Set CTRL flag
  1227.   004D2 18F9            JR      04CDH           ;Pass no value
  1228.  
  1229. ; <RPT> pressed
  1230.  
  1231.   004D4 3A2440          LD      A,(4024H)       ;A = last character code
  1232.   004D7 18A6            JR      047FH           ;Use it for further processing
  1233.  
  1234. ; Compute POS (continuation of 0350H)
  1235.  
  1236.   004D9 ED52            SBC     HL,DE           ;HL = screen position of cursor
  1237.   004DB 112800          LD      DE,0028H        ;DE = number of chars/line
  1238.   004DE B7              OR      A               ;Clear C-flag
  1239.  
  1240.   004DF ED52            SBC     HL,DE           ;Subtract 40 from HL
  1241.   004E1 30FB            JR      NC,04DEH        ;until HL < 0
  1242.  
  1243.   004E3 19              ADD     HL,DE           ;Adjust HL: HL = cursor
  1244.                                                 ;position within line
  1245.   004E4 7D              LD      A,L             ;A = POS
  1246.   004E5 E1              POP     HL              ;Restore PTP
  1247.   004E6 C9              RET
  1248.  
  1249.  
  1250. ; Printer routine (activated by DCB call)
  1251.  
  1252.   004E7 79              LD      A,C             ;A = ASCII code of character
  1253.                                                 ;to be printed.
  1254.   004E8 B7              OR      A               ;Null character ?
  1255.   004E9 283E            JR      Z,0529H         ;Yes: Just wait for printer
  1256.  
  1257.   004EB FE0B            CP      0BH             ;Vertical Tab character ?
  1258.   004ED 280A            JR      Z,04F9H         ;Yes: incorporate line counter
  1259.  
  1260.   004EF FE0C            CP      0CH             ;Form Feed character ?
  1261.   004F1 201B            JR      NZ,050EH        ;No: Print character
  1262.  
  1263.   004F3 AF              XOR     A               ;A = maximum number of lines
  1264.   004F4 DDB603          OR      (IX+03H)        ;per page. A = 0 ?
  1265.   004F7 2815            JR      Z,050EH         ;Yes: send 00H to printer
  1266.  
  1267.   004F9 DD7E03          LD      A,(IX+03H)      ;A = max. number of lines/page
  1268.   004FC DD9604          SUB     (IX+04H)        ;minus number of already
  1269.                                                 ;printed lines.
  1270.   004FF 47              LD      B,A             ;B = number of lines remaining
  1271.                                                 ;until next page
  1272.   00500 CD2905          CALL    0529H           ;Printer ready ?
  1273.   00503 20FB            JR      NZ,0500H        ;No: wait for printer
  1274.  
  1275.   00505 0E0A            LD      C,0AH           ;C = Line Feed (0AH)
  1276.   00507 CD3C05          CALL    053CH           ;Output C to printer
  1277.   0050A 10F4            DJNZ    0500H           ;Until next line
  1278.  
  1279.   0050C 1816            JR      0524H           ;Line counter = 0
  1280.  
  1281. ; Print character
  1282.  
  1283.   0050E CD2905          CALL    0529H           ;Printer ready ?
  1284.   00511 20FB            JR      NZ,050EH        ;No: wait for printer
  1285.  
  1286.   00513 CD3C05          CALL    053CH           ;Print character in C
  1287.   00516 FE0D            CP      0DH             ;was it a Carriage Return
  1288.   00518 C0              RET     NZ              ;No: done
  1289.  
  1290.   00519 DD3404          INC     (IX+04H)        ;Increment line counter
  1291.   0051C DD7E04          LD      A,(IX+04H)      ;A = number of printed lines
  1292.   0051F DDBE03          CP      (IX+03H)        ;Page full ?
  1293.   00522 79              LD      A,C             ;Printed character back in A
  1294.   00523 C0              RET     NZ              ;No: done
  1295.  
  1296.   00524 DD360400        LD      (IX+04H),00H    ;Line counter = 0
  1297.   00528 C9              RET
  1298.  
  1299.  
  1300. ; Printer ready ? (AF)
  1301. ;
  1302. ; I: -
  1303. ; O: Z-flag = 1 when printer is ready
  1304.  
  1305.   00529 3E07            LD      A,07H           ;Select register 7
  1306.   0052B D3F8            OUT     (0F8H),A        ;of the PSG
  1307.   0052D 3E7F            LD      A,7FH           ;Set port A to output
  1308.   0052F D3F9            OUT     (0F9H),A        ;and port B to input
  1309.   00531 3E0F            LD      A,0FH           ;Select port B
  1310.   00533 D3F8            OUT     (0F8H),A
  1311.   00535 DBF9            IN      A,(0F9H)        ;Get printer status
  1312.   00537 E6EF            AND     0EFH            ;Mask status bits
  1313.   00539 FE2F            CP      2FH             ;and set Z-flag
  1314.   0053B C9              RET
  1315.  
  1316. ; C to printer without checking
  1317.  
  1318.   0053C 3E07            LD      A,07H           ;Select register 7
  1319.   0053E D3F8            OUT     (0F8H),A        ;of the PSG
  1320.   00540 3E7F            LD      A,7FH           ;I/O port A on output and
  1321.   00542 D3F9            OUT     (0F9H),A        ;I/O port B on input
  1322.   00544 3E0E            LD      A,0EH           ;Select register 14 (port A)
  1323.   00546 D3F8            OUT     (0F8H),A
  1324.   00548 79              LD      A,C             ;Output byte
  1325.   00549 D3F9            OUT     (0F9H),A
  1326.   0054B 3E07            LD      A,07H           ;Put both ports on output
  1327.   0054D D3F8            OUT     (0F8H),A
  1328.   0054F 3EFF            LD      A,0FFH
  1329.   00551 D3F9            OUT     (0F9H),A
  1330.   00553 3E0F            LD      A,0FH           ;To port B
  1331.   00555 D3F8            OUT     (0F8H),A
  1332.   00557 AF              XOR     A               ;Output 00H
  1333.   00558 D3F9            OUT     (0F9H),A
  1334.   0055A 3E0F            LD      A,0FH           ;Then to port B
  1335.   0055C D3F8            OUT     (0F8H),A
  1336.   0055E 3E01            LD      A,01H           ;Ouput 01H and indicate to
  1337.   00560 D3F9            OUT     (0F9H),A        ;printer that a byte has been
  1338.                                                 ;send
  1339.   00562 79              LD      A,C             ;Character back in A
  1340.   00563 C9              RET
  1341.  
  1342.  
  1343. ; Output routine (continuation of 0335H)
  1344.                                                 ;Output to cassette ?
  1345.   00564 FA1F02          JP      M,021FH         ;Yes: continue at 021FH
  1346.                                                 ;Output to printer ?
  1347.   00567 C29C03          JP      NZ,039CH        ;Yes: continue at 039CH
  1348.  
  1349.   0056A C33A03          JP      033AH           ;Output to screen
  1350.  
  1351.  
  1352. ; Start 2 (continuation of 06ACH)
  1353.  
  1354.   0056D 31F841          LD      SP,41F8H        ;Set stack pointer
  1355.   00570 AF              XOR     A               ;Clear port 255 (NBGRD, CHAR 1,
  1356.   00571 D3FF            OUT     (0FFH),A        ;and LGR)
  1357.  
  1358.   00573 2100F4          LD      HL,0F400H       ;Clear colour memory
  1359.   00576 1101F4          LD      DE,0F401H
  1360.   00579 01FF03          LD      BC,03FFH
  1361.   0057C 3600            LD      (HL),00H
  1362.   0057E EDB0            LDIR
  1363.  
  1364.   00580 0EFF            LD      C,0FFH          ;C = port address
  1365.   00582 ED78            IN      A,(C)           ;Parameters for NTSC standard
  1366.   00584 E608            AND     08H             ;(have no effect in PAL)
  1367.   00586 47              LD      B,A
  1368.   00587 D3FC            OUT     (0FCH),A
  1369.   00589 ED78            IN      A,(C)
  1370.   0058B E608            AND     08H
  1371.  
  1372.   0058D 211137          LD      HL,3711H        ;--
  1373.   00590 110038          LD      DE,3800H        ;DE -> CRTC table for PAL
  1374.                                                 ;standard
  1375.   00593 A8              XOR     B
  1376.   00594 00              NOP
  1377.   00595 00              NOP
  1378.   00596 D3FD            OUT     (0FDH),A
  1379.   00598 ED78            IN      A,(C)
  1380.   0059A E608            AND     08H
  1381.   0059C 216935          LD      HL,3569H        ;HL -> colour code table
  1382.   0059F A8              XOR     B
  1383.   005A0 1812            JR      05B4H           ;Continue at 05B4H
  1384.  
  1385.  
  1386. ; Unused routine (only for computers with NTSC standard)
  1387.  
  1388.   005A2 D3FE            OUT     (0FEH),A
  1389.   005A4 ED78            IN      A,(C)
  1390.   005A6 E608            AND     08H
  1391.   005A8 210A37          LD      HL,370AH
  1392.   005AB A8              XOR     B
  1393.   005AC 2006            JR      NZ,05B4H
  1394.   005AE 213137          LD      HL,3731H
  1395.   005B1 112338          LD      DE,3823H
  1396.  
  1397.  
  1398. ; Start 3 (continuation of 05A0H)
  1399.  
  1400.   005B4 E5              PUSH    HL              ;Save HL
  1401.   005B5 EB              EX      DE,HL           ;HL -> CRTC table
  1402.   005B6 11F042          LD      DE,42F0H        ;DE -> system RAM
  1403.   005B9 012300          LD      BC,0023H        ;BC = table size (35 bytes)
  1404.   005BC EDB0            LDIR                    ;Copy table into sytem RAM
  1405.  
  1406.   005BE E1              POP     HL              ;Restore HL
  1407.   005BF 119043          LD      DE,4390H        ;DE -> colour code table
  1408.   005C2 011000          LD      BC,0010H        ;BC = table size (16 colours)
  1409.   005C5 EDB0            LDIR                    ;Copy into system RAM
  1410.  
  1411.   005C7 C36C00          JP      006CH           ;Continue at 006CH
  1412.  
  1413.  
  1414. ; Reset 2 (continuation of 0069H)
  1415.  
  1416.   005CA AF              XOR     A               ;A = 0;
  1417.   005CB D3ED            OUT     (0EDH),A        ;
  1418.   005CD 3A04F8          LD      A,(0F804H)      ;Read keyboard
  1419.   005D0 CB57            BIT     2,A             ;<R> pressed ?
  1420.   005D2 C20000          JP      NZ,0000H        ;Yes: cold start
  1421.  
  1422.   005D5 C3C006          JP      06C0H           ;No: warm start
  1423.  
  1424.   005D8 FF              RST     38H             ;--
  1425.  
  1426.  
  1427. ; Input of one line incl. cursor control, FKEY evaluation und presentation
  1428. ; of the entered character on screen (AF, BC, DE, HL).
  1429. ; The input is terminated by <RETURN> or <BREAK>.
  1430. ; The End Of Line is indicated in the buffer by a 0DH character
  1431. ;
  1432. ; I: HL -> input buffer to store the entered characters.
  1433. ;    B = Maximum number of characters allowed to be entered.
  1434. ;        (more characters are not accepted. The input must be terminated
  1435. ;        by <RETURN> or <BREAK>)
  1436. ; O: HL -> start of input buffer
  1437. ;    DE = 401DH (from 0033H call)
  1438. ;    B  = number of entered characters
  1439. ;    C  = B from start
  1440. ;    A  = last character entered (0DH or 01H)
  1441. ;    C-flag = 1 when input was terminated with <BREAK>
  1442.  
  1443.   005D9 E5              PUSH    HL              ;Save buffer address
  1444.   005DA 3E0E            LD      A,0EH           ;A = Cursor on
  1445.   005DC CD3300          CALL    0033H           ;Output character
  1446.   005DF 48              LD      C,B             ;C = max. number of characters
  1447.   005E0 C30030          JP      3000H           ;Continue at 3000H for FKEY
  1448.                                                 ;evaluation
  1449.  
  1450.   005E3 FE20            CP      20H             ;Control key entered ?
  1451.   005E5 3025            JR      NC,060CH        ;No: store character
  1452.  
  1453.   005E7 FE0D            CP      0DH             ;<RETURN> ?
  1454.   005E9 CA6206          JP      Z,0662H         ;Yes: continue at 0662H
  1455.  
  1456.   005EC FE1F            CP      1FH             ;<CLEAR> ?
  1457.   005EE 2829            JR      Z,0619H         ;Yes: continue at 0619H
  1458.  
  1459.   005F0 FE01            CP      01H             ;<BREAK> ?
  1460.   005F2 286D            JR      Z,0661H         ;Yes: continue at 0661H
  1461.  
  1462.   005F4 11E005          LD      DE,05E0H        ;Set new return address
  1463.   005F7 D5              PUSH    DE              ;to 05E0H
  1464.  
  1465.   005F8 FE08            CP      08H             ;Backspace (arrow left) ?
  1466.   005FA 2834            JR      Z,0630H         ;Yes: continue at 0630H
  1467.  
  1468.   005FC FE18            CP      18H             ;Delete line
  1469.                                                 ;(shift-arrow left) ?
  1470.   005FE 282B            JR      Z,062BH         ;Yes: continue at 062BH
  1471.  
  1472.   00600 FE09            CP      09H             ;TAB (arrow right) ?
  1473.   00602 2842            JR      Z,0646H         ;Yes: continue at 0646H
  1474.  
  1475.   00604 FE19            CP      19H             ;Shift-arrow right ? (32
  1476.                                                 ;characters/line in GENIE I)
  1477.   00606 2839            JR      Z,0641H         ;Yes: continue at 0641H
  1478.  
  1479.   00608 FE0A            CP      0AH             ;New Line (arrow down) ?
  1480.   0060A C0              RET     NZ              ;No: control character has no
  1481.                                                 ;function, get new character
  1482.  
  1483.   0060B D1              POP     DE              ;Remove return address from
  1484.                                                 ;stack
  1485.   0060C 77              LD      (HL),A          ;Store character in buffer
  1486.   0060D 78              LD      A,B             ;Entering of characters
  1487.   0060E B7              OR      A               ;still allowed ?
  1488.   0060F 28CF            JR      Z,05E0H         ;No: get new character
  1489.  
  1490.   00611 7E              LD      A,(HL)          ;Get character from buffer
  1491.   00612 23              INC     HL              ;Buffer pointer + 1
  1492.   00613 CD3300          CALL    0033H           ;Output character
  1493.   00616 05              DEC     B               ;Character counter - 1
  1494.   00617 18C7            JR      05E0H           ;Get next character
  1495.  
  1496. ; <CLEAR> pressed
  1497.  
  1498.   00619 CDC901          CALL    01C9H           ;CLS
  1499.   0061C 41              LD      B,C             ;Restart character counter
  1500.   0061D E1              POP     HL              ;HL -> start of buffer
  1501.   0061E E5              PUSH    HL              ;Save pointer again
  1502.   0061F C3E005          JP      05E0H           ;Get next character
  1503.  
  1504. ; Delete line
  1505.  
  1506.   00622 CD3006          CALL    0630H           ;Delete character
  1507.   00625 2B              DEC     HL              ;Buffer pointer - 1
  1508.   00626 7E              LD      A,(HL)          ;A = last character
  1509.   00627 23              INC     HL              ;Increment buffer pointer
  1510.   00628 FE0A            CP      0AH             ;Has a new line already been
  1511.                                                 ;started ?
  1512.   0062A C8              RET     Z               ;Yes: then only delete until
  1513.                                                 ;here
  1514. ; <Shift>+<arrow left> pressed
  1515.  
  1516.   0062B 78              LD      A,B             ;Have characters been
  1517.   0062C B9              CP      C               ;entered already ?
  1518.   0062D 20F3            JR      NZ,0622H        ;Yes: delete line
  1519.  
  1520.   0062F C9              RET
  1521.  
  1522. ; <arrow left> pressed
  1523.  
  1524.   00630 78              LD      A,B             ;Character counter still on
  1525.   00631 B9              CP      C               ;initial value ?
  1526.   00632 C8              RET     Z               ;Yes: done
  1527.  
  1528.   00633 2B              DEC     HL              ;Buffer pointer - 1
  1529.   00634 7E              LD      A,(HL)          ;Get previous character
  1530.   00635 FE0A            CP      0AH             ;New line started ?
  1531.   00637 23              INC     HL              ;Buffer pointer + 1
  1532.   00638 C8              RET     Z               ;Yes: return
  1533.   00639 2B              DEC     HL              ;No: buffer pointer + 1
  1534.   0063A 3E08            LD      A,08H           ;A = backspace character
  1535.   0063C CD3300          CALL    0033H           ;Output byte
  1536.   0063F 04              INC     B               ;Character counter + 1
  1537.   00640 C9              RET
  1538.  
  1539. ; <SHIFT>+<arrow right> pressed
  1540.  
  1541.   00641 3E17            LD      A,17H           ;17H was code for switching to
  1542.                                                 ;32 characters/lines
  1543.   00643 C33300          JP      0033H           ;Output byte
  1544.                                                 ;(has no effect on the
  1545.                                                 ;Colour Genie)
  1546. ; <arrow right> pressed
  1547.  
  1548.   00646 CD4803          CALL    0348H           ;A = POS
  1549.   00649 E607            AND     07H             ;Calculate last TAB position
  1550.   0064B 2F              CPL                     ;Negate value
  1551.   0064C 3C              INC     A               ;adjust to 2 complement
  1552.   0064D C608            ADD     A,08H           ;+8 gives number of characters
  1553.                                                 ;until next TAB position
  1554.   0064F 5F              LD      E,A             ;E = counter
  1555.  
  1556.   00650 78              LD      A,B             ;Is it allowed to enter more
  1557.   00651 B7              OR      A               ;characters ?
  1558.   00652 C8              RET     Z               ;No: return
  1559.  
  1560.   00653 3E20            LD      A,20H           ;A = space character
  1561.   00655 77              LD      (HL),A          ;Put it in buffer
  1562.   00656 23              INC     HL              ;Buffer pointer + 1
  1563.   00657 D5              PUSH    DE              ;Save DE
  1564.   00658 CD3300          CALL    0033H           ;Output byte
  1565.   0065B D1              POP     DE              ;Restore DE
  1566.   0065C 05              DEC     B               ;Character counter - 1
  1567.   0065D 1D              DEC     E               ;TAB counter - 1
  1568.   0065E C8              RET     Z               ;Return when done
  1569.   0065F 18EF            JR      0650H           ;Next character in buffer
  1570.  
  1571. ; <BREAK> pressed
  1572.  
  1573.   00661 37              SCF                     ;Set C-flag = 1 and treat it
  1574.                                                 ;further like <RETURN>
  1575. ; <RETURN> pressed
  1576.  
  1577.   00662 F5              PUSH    AF              ;Save flags
  1578.   00663 3E0D            LD      A,0DH           ;A = Carriage Return
  1579.   00665 77              LD      (HL),A          ;Put it in buffer
  1580.   00666 CD3300          CALL    0033H           ;Output byte
  1581.   00669 3E0F            LD      A,0FH           ;A = cursor off
  1582.   0066B CD3300          CALL    0033H           ;Output character
  1583.   0066E 79              LD      A,C             ;Calculate the number of
  1584.   0066F 90              SUB     B               ;entered characters
  1585.   00670 47              LD      B,A             ;Result in B
  1586.   00671 F1              POP     AF              ;Restore flags
  1587.   00672 E1              POP     HL              ;HL -> start of buffer
  1588.   00673 C9              RET
  1589.  
  1590.  
  1591. ; Start 1
  1592.  
  1593.   00674 3E08            LD      A,08H           ;CHAR 2, NBGRD and LGR
  1594.   00676 D3FF            OUT     (0FFH),A        ;Output to port
  1595.   00678 321C43          LD      (431CH),A       ;Save port status
  1596.  
  1597.   0067B 21D206          LD      HL,06D2H        ;HL -> start of block
  1598.   0067E 110040          LD      DE,4000H        ;DE -> destination system RAM
  1599.   00681 013600          LD      BC,0036H        ;BC = 54 bytes
  1600.   00684 EDB0            LDIR                    ;Initialize system RAM
  1601.  
  1602.   00686 3D              DEC     A               ;Repeat until A = 00H
  1603.   00687 3D              DEC     A
  1604.   00688 20F1            JR      NZ,067BH
  1605.  
  1606.   0068A 0627            LD      B,27H           ;Clear the next 39 bytes
  1607.   0068C 12              LD      (DE),A
  1608.   0068D 13              INC     DE
  1609.   0068E 10FC            DJNZ    068CH
  1610.  
  1611.   00690 21AB34          LD      HL,34ABH        ;HL -> function key defaults
  1612.   00693 115043          LD      DE,4350H        ;DE -> function key RAM space
  1613.   00696 013800          LD      BC,0038H        ;BC = size to copy
  1614.   00699 EDB0            LDIR                    ;Copy function key definitions
  1615.  
  1616.   0069B 3E01            LD      A,01H           ;Set SCALE = 1
  1617.   0069D 321443          LD      (4314H),A
  1618.   006A0 213039          LD      HL,3930H        ;Set pointer on table with
  1619.   006A3 228C43          LD      (438CH),HL      ;Colour BASIC statements in
  1620.                                                 ;system RAM
  1621.   006A6 21DB39          LD      HL,39DBH        ;Set pointer on vector table
  1622.   006A9 228E43          LD      (438EH),HL      ;in system RAM
  1623.   006AC C36D05          JP      056DH           ;Continue at 05D6H
  1624.  
  1625.   006AF 3A00C0          LD      A,(0C000H)      ;Take first byte of ROM
  1626.                                                 ;cartridge space
  1627.   006B2 FE43            CP      'C'             ;Program there ?
  1628.   006B4 CA01C0          JP      Z,0C001H        ;Yes: execute program
  1629.  
  1630.   006B7 3A00C0          LD      A,(0C000H)      ;Take first byte of ROM
  1631.                                                 ;cartridge space
  1632.   006BA FE44            CP      'D'             ;Program there ?
  1633.   006BC CA01C0          JP      Z,0C001H        ;Yes: execute program
  1634.  
  1635.   006BF C9              RET
  1636.  
  1637.  
  1638.   006C0 3A00C0          LD      A,(0C000H)      ;Take first byte of ROM
  1639.                                                 ;cartridge space
  1640.   006C3 FE43            CP      'C'             ;Program there ?
  1641.   006C5 CA01C0          JP      Z,0C001H        ;Yes: execute program
  1642.  
  1643.   006C8 C3AE19          JP      19AEH
  1644.  
  1645.  
  1646. ; Unused ROM space
  1647.  
  1648.   006CB FF              RST     38H             ;--
  1649.   006CC FF              RST     38H
  1650.   006CD FF              RST     38H
  1651.   006CE FF              RST     38H
  1652.   006CF FF              RST     38H
  1653.   006D0 FF              RST     38H
  1654.   006D1 FF              RST     38H
  1655.  
  1656. ; The following block of code is copied into system RAM at 4000H to initialize
  1657. ; the vectors and DCBs located there
  1658.  
  1659.   006D2 C3961C          JP      1C96H           ;RST 08H vector
  1660.   006D5 C3781D          JP      1D78H           ;RST 10H vector
  1661.   006D8 C3901C          JP      1C90H           ;RST 18H vector
  1662.   006DB C3D925          JP      25D9H           ;RST 20H vector
  1663.   006DE C9              RET                     ;RST 28H vector (BREAK vector)
  1664.   006DF 00              NOP
  1665.   006E0 00              NOP
  1666.   006E1 C9              RET                     ;RST 30H vector
  1667.   006E2 00              NOP
  1668.   006E3 00              NOP
  1669.   006E4 FB              EI                      ;RST 38H vector
  1670.   006E5 C9              RET
  1671.   006E6 00              NOP
  1672.  
  1673.   006E7 01              DEFB    01H             ;Initialization data for
  1674.   006E8 E303            DEFW    03E3H           ;contructing keyboard DCB
  1675.   006EA 00              DEFB    00H
  1676.   006EB 07              DEFB    07H
  1677.   006EC 40              DEFB    40H
  1678.   006ED 20              DEFB    20H
  1679.   006EE 49              DEFB    47H
  1680.  
  1681.   006EF 07              DEFB    07H             ;Initialization data for
  1682.   006F0 E430            DEFW    30E4H           ;contructing screen DCB
  1683.   006F2 0044            DEFW    4400H
  1684.   006F4 01              DEFB    01H
  1685.   006F4 01              DEFB    01H
  1686.   006F4 03              DEFB    01H
  1687.  
  1688.   006F7 06              DEFB    06H             ;Initialization data for
  1689.   006F8 E704            DEFW    04E7H           ;contructing printer DCB
  1690.   006FA 43              DEFB    43H
  1691.   006FB 00              DEFB    00H
  1692.   006FC 00              DEFB    00H
  1693.   006FD 50              DEFB    50H
  1694.   006FE 52              DEFB    52H
  1695.  
  1696.   006FF C30050          JP      5000H           ;
  1697.   00702 C7              RST     00H             ;
  1698.   00703 00              NOP
  1699.   00704 00              NOP
  1700.  
  1701.   00705 3E00            LD      A,00H           ;False DCB vector
  1702.   00707 C9              RET
  1703.  
  1704. ; X = X + 0.5 (SNG)
  1705.  
  1706.   00708 218013          LD      HL,1380H        ;HL -> Constant 0.5
  1707.  
  1708. ; X = X + (HL)
  1709.  
  1710.   0070B CDC209          CALL    09C2H           ;BCDE = (HL)
  1711.   0070E 1806            JR      0716H           ;X = X + BCDE
  1712.  
  1713. ; X = (HL) - X (SNG)
  1714.  
  1715.   00710 CDC209          CALL    09C2H           ;BCDE = (HL)
  1716.  
  1717. ; X = BCDE - X (SNG)
  1718.  
  1719.   00713 CD8209          CALL    0982H           ;X = -X
  1720.  
  1721. ; X = BCDE + X (SNG)
  1722.  
  1723.   00716 78              LD      A,B             ;A = Exp (BCDE)
  1724.   00717 B7              OR      A               ;BCDE = 0 ?
  1725.   00718 C8              RET     Z               ;Yes: result already in X
  1726.  
  1727.   00719 3A2441          LD      A,(4124H)       ;A = Exp (X)
  1728.   0071C B7              OR      A               ;X = 0 ?
  1729.   0071D CAB409          JP      Z,09B4H         ;Yes: X = BCDE
  1730.  
  1731.   00720 90              SUB     B               ;A = Exp (X) - Exp (BCDE)
  1732.                                                 ;Largest value in X ?
  1733.   00721 300C            JR      NC,072FH        ;Yes: continue at 072FH
  1734.  
  1735.   00723 2F              CPL                     ;A = -A
  1736.   00724 3C              INC     A               ;A is the difference of the
  1737.                                                 ;exponents
  1738.   00725 EB              EX      DE,HL           ;Save DE
  1739.   00726 CDA409          CALL    09A4H           ;(SP) = X
  1740.                                                 ;(2nd operand on stack)
  1741.   00729 EB              EX      DE,HL           ;Restore DE
  1742.   0072A CDB409          CALL    09B4H           ;X = BCDE (1st operand)
  1743.   0072D C1              POP     BC              ;BCDE = 2nd operand
  1744.   0072E D1              POP     DE
  1745.                                                 ;X is now the operand with
  1746.                                                 ;the biggest exponent
  1747.                                                 ;A contains the differnce
  1748.                                                 ;between the exponents
  1749.   0072F FE19            CP      19H             ;Difference bigger then 24 ?
  1750.                                                 ;(difference between the
  1751.                                                 ;operands bigger then 2^24)
  1752.   00731 D0              RET     NC              ;Yes: BCDE is too small, the
  1753.                                                 ;sum would not change X
  1754.  
  1755.   00732 F5              PUSH    AF              ;Save exponent difference
  1756.   00733 CDDF09          CALL    09DFH           ;Adjust mantissas
  1757.   00736 67              LD      H,A             ;H, 7 = 0, if signs are not
  1758.                                                 ;equal
  1759.   00737 F1              POP     AF              ;Restore exponent difference
  1760.   00738 CDD707          CALL    07D7H           ;Shifting CDE to the right by
  1761.                                                 ;A bits: bring mantissa of BCDE
  1762.                                                 ;on the same exponent as X
  1763.   0073B B4              OR      H               ;A,7 = 0 when unequal signs
  1764.   0073C 212141          LD      HL,4121H        ;HL -> X
  1765.                                                 ;Signs equal ?
  1766.   0073F F25407          JP      P,0754H         ;No: continue at 0754H
  1767.  
  1768.   00742 CDB707          CALL    07B7H           ;Add mantissas
  1769.                                                 ;Overflow ?
  1770.   00745 D29607          JP      NC,0796H        ;No: continue at 0796H
  1771.  
  1772.   00748 23              INC     HL              ;HL -> Exp (X)
  1773.   00749 34              INC     (HL)            ;Exp (X) + 1 (adjust Exp
  1774.                                                 ;for overflow)
  1775.                                                 ;Exponent overflow ?
  1776.   0074A CAB207          JP      Z,07B2H         ;Yes: ?OV Error
  1777.  
  1778.   0074D 2E01            LD      L,01H           ;Divide Mantissa by 2
  1779.   0074F CDEB07          CALL    07EBH           ;because Exponent incremented
  1780.   00752 1842            JR      0796H           ;Round CDE and store in X
  1781.  
  1782. ; Unequal signs
  1783.  
  1784.   00754 AF              XOR     A               ;B = -B
  1785.   00755 90              SUB     B
  1786.   00756 47              LD      B,A
  1787.   00757 7E              LD      A,(HL)          ;Subtract Mantissa
  1788.   00758 9B              SBC     A,E
  1789.   00759 5F              LD      E,A
  1790.   0075A 23              INC     HL
  1791.   0075B 7E              LD      A,(HL)
  1792.   0075C 9A              SBC     A,D
  1793.   0075D 57              LD      D,A
  1794.   0075E 23              INC     HL
  1795.   0075F 7E              LD      A,(HL)          ;Subtract MSBs
  1796.   00760 99              SBC     A,C
  1797.   00761 4F              LD      C,A             ;Underflow ?
  1798.  
  1799. ; SFLOAT: Convert CDEB in 2-exp format and store in X
  1800. ;         The mantissa is shifted to the left by one bit until
  1801. ;         the highest bit = 1 and the exponent is as small as possible
  1802.  
  1803.   00762 DCC307          CALL    C,07C3H         ;Invert Mantissa (CDEB) when
  1804.                                                 ;over or underflow
  1805.   00765 68              LD      L,B             ;HL = LSBs
  1806.   00766 63              LD      H,E             ;Manstissa now in CDHL
  1807.   00767 AF              XOR     A               ;A = 00H
  1808.   00768 47              LD      B,A             ;B = counter
  1809.   00769 79              LD      A,C             ;A = MSB
  1810.   0076A B7              OR      A               ;MSB = 0 ?
  1811.   0076B 2018            JR      NZ,0785H        ;No: continue shifting
  1812.  
  1813.   0076D 4A              LD      C,D             ;C << D << H << L << 00H
  1814.   0076E 54              LD      D,H             ;(Shift Mantissa left bytewise)
  1815.   0076F 65              LD      H,L             ;until MSB <> 0
  1816.   00770 6F              LD      L,A
  1817.   00771 78              LD      A,B
  1818.   00772 D608            SUB     08H             ;Counter - 8 (8 bits shifted)
  1819.   00774 FEE0            CP      0E0H            ;Counter = -32 ?
  1820.   00776 20F0            JR      NZ,0768H        ;No: continue shifting
  1821.  
  1822.                                                 ;Shifted 4 times:
  1823.                                                 ;Mantissa is now 0, result = 0
  1824.  
  1825. ; X = 0 (SNG)
  1826.  
  1827.   00778 AF              XOR     A               ;A = 00H
  1828.   00779 322441          LD      (4124H),A       ;Exp (X) = 00H,  X = 0
  1829.   0077C C9              RET
  1830.  
  1831. ; Shift Mantissa CDHL bitwise to the left until highest bit = 1
  1832.  
  1833.   0077D 05              DEC     B               ;Counter - 1 (shift 1 bit)
  1834.   0077E 29              ADD     HL,HL           ;HL = HL * 2 (= shift left)
  1835.   0077F 7A              LD      A,D
  1836.   00780 17              RLA                     ;D = D * 2, C-flag = overflow
  1837.   00781 57              LD      D,A
  1838.   00782 79              LD      A,C             ;C = C + C + C-flag
  1839.   00783 8F              ADC     A,A
  1840.   00784 4F              LD      C,A
  1841.                                                 ;Highest bit of C = 1 ?
  1842.   00785 F27D07          JP      P,077DH         ;No: continue shifting
  1843.  
  1844.   00788 78              LD      A,B             ;A = negative number of times
  1845.                                                 ;that has been shifted
  1846.   00789 5C              LD      E,H             ;Mantissa = CDEB
  1847.   0078A 45              LD      B,L
  1848.   0078B B7              OR      A               ;Anything shifted ?
  1849.   0078C 2808            JR      Z,0796H         ;Yes: continue at 0796H
  1850.  
  1851.   0078E 212441          LD      HL,4124H        ;HL -> Exp (X)
  1852.   00791 86              ADD     A,(HL)          ;A = new Exponent
  1853.   00792 77              LD      (HL),A          ;Save Exponent
  1854.                                                 ;Old Exponent to small ?
  1855.   00793 30E3            JR      NC,0778H        ;Yes: X = 0
  1856.                                                 ;Exponent = 0 ?
  1857.   00795 C8              RET     Z               ;Yes: return
  1858.  
  1859. ; Round CDEB and store in X
  1860. ; Exp (X) and sign are kept
  1861.  
  1862.   00796 78              LD      A,B             ;A = LSB
  1863.  
  1864.   00797 212441          LD      HL,4124H        ;HL -> Exp (X)
  1865.   0079A B7              OR      A               ;Highest bit of LSB = 1 ?
  1866.   0079B FCA807          CALL    M,07A8H         ;Yes: round up CDE
  1867.  
  1868.   0079E 46              LD      B,(HL)          ;B = Exp (X)
  1869.   0079F 23              INC     HL
  1870.   007A0 7E              LD      A,(HL)          ;A = Sign flag
  1871.   007A1 E680            AND     80H             ;Clear sign bit
  1872.   007A3 A9              XOR     C
  1873.   007A4 4F              LD      C,A             ;BCDE is negative in case
  1874.                                                 ;sign flag was 00H
  1875.   007A5 C3B409          JP      09B4H           ;X = BCDE
  1876.  
  1877. ; Round CDE and write overflow to Exp (X)
  1878.  
  1879.   007A8 1C              INC     E               ;LSB + 1
  1880.                                                 ;Overflow ?
  1881.   007A9 C0              RET     NZ              ;No: return
  1882.  
  1883.   007AA 14              INC     D               ;Next byte + 1
  1884.                                                 ;Overflow ?
  1885.   007AB C0              RET     NZ              ;No: return
  1886.  
  1887.   007AC 0C              INC     C               ;MSB + 1
  1888.                                                 ;Overflow ?
  1889.   007AD C0              RET     NZ              ;No: return
  1890.  
  1891.   007AE 0E80            LD      C,80H           ;Set highest bit of Mantissa
  1892.   007B0 34              INC     (HL)            ;and increment Exp (X)
  1893.                                                 ;Overflow ?
  1894.   007B1 C0              RET     NZ              ;No: return
  1895.  
  1896. ; ?OV Error
  1897.  
  1898.   007B2 1E0A            LD      E,0AH           ;E = error code for ?OV error
  1899.   007B4 C3A219          JP      19A2H           ;Continue at error routine
  1900.  
  1901. ; CDE = CDE + (HL) (SNG)
  1902. ; Fixed point (Mantissa) addition
  1903.  
  1904.   007B7 7E              LD      A,(HL)          ;A = (HL)
  1905.   007B8 83              ADD     A,E             ;A = E + (HL)
  1906.   007B9 5F              LD      E,A             ;E = E + (HL)
  1907.   007BA 23              INC     HL              ;Next byte
  1908.   007BB 7E              LD      A,(HL)
  1909.   007BC 8A              ADC     A,D             ;Now add with carry
  1910.   007BD 57              LD      D,A
  1911.   007BE 23              INC     HL              ;Last byte
  1912.   007BF 7E              LD      A,(HL)
  1913.   007C0 89              ADC     A,C
  1914.   007C1 4F              LD      C,A
  1915.   007C2 C9              RET
  1916.  
  1917. ; Invert Mantissa of CDEB and Sign flag (4125H)
  1918.  
  1919.   007C3 212541          LD      HL,4125H        ;HL -> Sign flag
  1920.   007C6 7E              LD      A,(HL)          ;A = Sign Flag
  1921.   007C7 2F              CPL                     ;Invert
  1922.   007C8 77              LD      (HL),A          ;and store
  1923.  
  1924.   007C9 AF              XOR     A               ;A = 00H
  1925.   007CA 6F              LD      L,A             ;L = 00H
  1926.   007CB 90              SUB     B
  1927.   007CC 47              LD      B,A             ;B = 00H - B
  1928.   007CD 7D              LD      A,L             ;A = 00H
  1929.   007CE 9B              SBC     A,E             ;E = 00H - E - C-flag
  1930.   007CF 5F              LD      E,A
  1931.   007D0 7D              LD      A,L             ;Same with D
  1932.   007D1 9A              SBC     A,D
  1933.   007D2 57              LD      D,A
  1934.   007D3 7D              LD      A,L             ;and with C
  1935.   007D4 99              SBC     A,C
  1936.   007D5 4F              LD      C,A
  1937.   007D6 C9              RET
  1938.  
  1939. ; Shift CDE by A bits to the right. B becomes LSB
  1940.  
  1941.   007D7 0600            LD      B,00H           ;Overflow on 00H
  1942.  
  1943.   007D9 D608            SUB     08H             ;More as 8 shifts ?
  1944.   007DB 3807            JR      C,07E4H         ;No: shift bitwise
  1945.                                                 ;Yes: shift bytewise
  1946.   007DD 43              LD      B,E
  1947.   007DE 5A              LD      E,D
  1948.   007DF 51              LD      D,C
  1949.   007E0 0E00            LD      C,00H           ;00H >> C >> D >> E >> B
  1950.   007E2 18F5            JR      07D9H           ;Loop
  1951.  
  1952. ; Shift bitwise
  1953.  
  1954.   007E4 C609            ADD     A,09H           ;Reverse SUB and adjust by 1
  1955.   007E6 6F              LD      L,A             ;L = counter
  1956.  
  1957.   007E7 AF              XOR     A               ;A = 00H
  1958.   007E8 2D              DEC     L               ;Counter - 1
  1959.                                                 ;Counter = 0 ?
  1960.   007E9 C8              RET     Z               ;Yes: return
  1961.  
  1962.   007EA 79              LD      A,C             ;Shift right CDE by one bit
  1963.   007EB 1F              RRA
  1964.   007EC 4F              LD      C,A
  1965.   007ED 7A              LD      A,D             ;Shift D
  1966.   007EE 1F              RRA
  1967.   007EF 57              LD      D,A
  1968.   007F0 7B              LD      A,E             ;Shift E
  1969.   007F1 1F              RRA
  1970.   007F2 5F              LD      E,A
  1971.   007F3 78              LD      A,B             ;Shift B
  1972.   007F4 1F              RRA
  1973.   007F5 47              LD      B,A             ;Overflow in B
  1974.   007F6 18EF            JR      07E7H           ;Loop
  1975.  
  1976.  
  1977. ; Constant 1 (SNG)
  1978.  
  1979.   007F8 00              DEFB    00H
  1980.   007F9 00              DEFB    00H
  1981.   007FA 00              DEFB    00H
  1982.   007FB 81              DEFB    81H
  1983.  
  1984.  
  1985. ; Table of SNG coefficients for LOG function
  1986.  
  1987.   007FC 03              DEFB    03H             ;3 Coefficients
  1988.  
  1989.   007FD AA              DEFB    AAH             ;0.598979 approx.
  1990.   007FE 56              DEFB    56H             ;2 / ( 5*LOG(2) )
  1991.   007FF 19              DEFB    19H
  1992.   00800 80              DEFB    80H
  1993.  
  1994.   00801 F1              DEFB    F1H             ;0.961471 approx.
  1995.   00802 22              DEFB    22H             ;2 / ( 3*LOG(2) )
  1996.   00803 76              DEFB    76H
  1997.   00804 80              DEFB    80H
  1998.  
  1999.   00805 45              DEFB    45H             ;2.88539  approx.
  2000.   00806 AA              DEFB    AAH             ;2 / ( 1*LOG(2) )
  2001.   00807 38              DEFB    38H
  2002.   00808 82              DEFB    82H
  2003.  
  2004.  
  2005. ; X = LOG ( X )
  2006. ; -------------
  2007. ; Calculates the natural logarithm of X
  2008. ;
  2009. ; I: X = numerical value (<> 0)
  2010. ; O: X = LOG (numerical value)
  2011.  
  2012.   00809 CD5509          CALL    0955H           ;TEST2
  2013.   0080C B7              OR      A               ;Argument = 0 ?
  2014.   0080D EA4A1E          JP      PE,1E4AH        ;Yes: ?FC Error
  2015.  
  2016.   00810 212441          LD      HL,4124H        ;HL -> Exp (argument)
  2017.   00813 7E              LD      A,(HL)          ;A = Exp (argument)
  2018.   00814 013580          LD      BC,8035H        ;BCDE = 0.707107 = SQR(2)/2
  2019.   00817 11F304          LD      DE,04F3H
  2020.                                                 ;Conversion Arg = x * 2 ^ n
  2021.   0081A 90              SUB     B               ;Exp (Arg) - 128 = n (B is 128)
  2022.   0081B F5              PUSH    AF              ;save n
  2023.   0081C 70              LD      (HL),B          ;X = x (set Exp to 80H)
  2024.   0081D D5              PUSH    DE              ;Save BCDE
  2025.   0081E C5              PUSH    BC
  2026.   0081F CD1607          CALL    0716H           ;X = X + BCDE = X+1/2*SQR(2)
  2027.   00822 C1              POP     BC              ;Restore BCDE
  2028.   00823 D1              POP     DE
  2029.   00824 04              INC     B               ;Exp(BCDE) + 1
  2030.                                                 ;BCDE = BCDE * 2 + SQR(2)
  2031.   00825 CDA208          CALL    08A2H           ;X = BCDE / X = SQR(2) / X
  2032.   00828 21F807          LD      HL,07F8H        ;HL -> 1.0
  2033.   0082B CD1007          CALL    0710H           ;X = (HL) - X = 1.0 - X
  2034.   0082E 21FC07          LD      HL,07FCH        ;HL -> numeric table
  2035.   00831 CD9A14          CALL    149AH           ;Compute row1
  2036.   00834 018080          LD      BC,8080H        ;BCDE = -0.5
  2037.   00837 110000          LD      DE,0000H
  2038.   0083A CD1607          CALL    0716H           ;X = BCDE + X = X - 0.5
  2039.   0083D F1              POP     AF              ;Restore n
  2040.   0083E CD890F          CALL    0F89H           ;X = X + n
  2041.                                                 ;and multiply with LOG(2)
  2042.  
  2043. ; X = X * LOG(2)
  2044.  
  2045.   00841 013180          LD      BC,8031H        ;BCDE = 0.693147 = LOG(2)
  2046.   00844 111872          LD      DE,7218H
  2047.  
  2048. ; SMUL: X = BCDE * X
  2049. ; Multiply two single precision numbers
  2050. ;
  2051. ; I: BCDE = 1st factor
  2052. ;    X    = 2nd factor
  2053. ; O: X    = product
  2054.  
  2055.   00847 CD5509          CALL    0955H           ;TEST2
  2056.   0084A C8              RET     Z               ;X = 0: result = 0
  2057.  
  2058.   0084B 2E00            LD      L,00H           ;Flag = 0 (MUL indication)
  2059.   0084D CD1409          CALL    0914H           ;Process exponent
  2060.   00850 79              LD      A,C             ;Save CDE (1st factor) in
  2061.   00851 324F41          LD      (414FH),A       ;system RAM from 414FH onwards
  2062.   00854 EB              EX      DE,HL
  2063.   00855 225041          LD      (4150H),HL
  2064.   00858 010000          LD      BC,0000H        ;BCDE = 00000000H
  2065.   0085B 50              LD      D,B
  2066.   0085C 58              LD      E,B
  2067.   0085D 216507          LD      HL,0765H        ;Put new return address
  2068.   00860 E5              PUSH    HL              ;to 0765H
  2069.   00861 216908          LD      HL,0869H        ;Put new return address twice
  2070.   00864 E5              PUSH    HL              ;to 0869H
  2071.   00865 E5              PUSH    HL
  2072.   00866 212141          LD      HL,4121H        ;HL -> 2nd factor
  2073.   00869 7E              LD      A,(HL)          ;A = next byte of mantissa of
  2074.                                                 ;the 2nd factor
  2075.   0086A 23              INC     HL              ;Pointer + 1
  2076.   0086B B7              OR      A               ;Byte = 00H ?
  2077.   0086C 2824            JR      Z,0892H         ;Yes: continue at 0892H
  2078.  
  2079.   0086E E5              PUSH    HL              ;Save pointer
  2080.   0086F 2E08            LD      L,08H           ;L = counter for 8 bits
  2081.   00871 1F              RRA                     ;Shift next bit into C-flag
  2082.   00872 67              LD      H,A             ;Save byte in H
  2083.   00873 79              LD      A,C             ;A = MSB
  2084.                                                 ;Bit set by last shift ?
  2085.   00874 300B            JR      NC,0881H        ;No: continue at 0881H
  2086.  
  2087.   00876 E5              PUSH    HL              ;Save HL
  2088.   00877 2A5041          LD      HL,(4150H)      ;CDE = CDE + 1st factor
  2089.   0087A 19              ADD     HL,DE
  2090.   0087B EB              EX      DE,HL
  2091.   0087C E1              POP     HL              ;Restore HL
  2092.   0087D 3A4F41          LD      A,(414FH)
  2093.   00880 89              ADC     A,C             ;A = MSB
  2094.   00881 1F              RRA                     ;CDEB one bit to the right
  2095.   00882 4F              LD      C,A
  2096.   00883 7A              LD      A,D             ;Shift D
  2097.   00884 1F              RRA
  2098.   00885 57              LD      D,A
  2099.   00886 7B              LD      A,E             ;Shift E
  2100.   00887 1F              RRA
  2101.   00888 5F              LD      E,A
  2102.   00889 78              LD      A,B             ;Shift B
  2103.   0088A 1F              RRA
  2104.   0088B 47              LD      B,A
  2105.   0088C 2D              DEC     L               ;Counter - 1
  2106.   0088D 7C              LD      A,H             ;Byte back into A
  2107.   0088E 20E1            JR      NZ,0871H        ;Check next bit
  2108.  
  2109.   00890 E1              POP     HL              ;HL -> X
  2110.   00891 C9              RET                     ;RET twice to 0896H and
  2111.                                                 ;once to 0765H
  2112.  
  2113. ; SUB for SMUL
  2114. ; Shift CDEB 1 byte to the right and fill with 00H
  2115.  
  2116.   00892 43              LD      B,E             ;00H >> C >> D >> E >> B
  2117.   00893 5A              LD      E,D
  2118.   00894 51              LD      D,C
  2119.   00895 4F              LD      C,A
  2120.   00896 C9              RET
  2121.  
  2122.  
  2123. ; SDIV10: X = X / 10
  2124. ; Divides number in X BY 10
  2125. ;
  2126. ; I: X = single precision number
  2127. ; O: X = number / 10
  2128.  
  2129.   00897 CDA409          CALL    09A4H           ;(SP) = X
  2130.   0089A 21D80D          LD      HL,0DD8H        ;HL -> 10
  2131.  
  2132. ; X = (SP) / (HL)
  2133.  
  2134.   0089D CDB109          CALL    09B1H           ;X = BCDE = (HL)
  2135.  
  2136. ; X = (SP) / X
  2137.  
  2138.   008A0 C1              POP     BC              ;BCDE = (SP)
  2139.   008A1 D1              POP     DE
  2140.  
  2141.  
  2142. ; SDIV: X = BCDE / X
  2143. ; Divides 2 single precision numbers
  2144. ;
  2145. ; I: BCDE = dividend
  2146. ;    X = divisor
  2147. ; O: X = quotient
  2148.  
  2149.   008A2 CD5509          CALL    0955H           ;TEST2
  2150.                                                 ;X = 0 ?
  2151.   008A5 CA9A19          JP      Z,199AH         ;Yes: ?/0 Error
  2152.  
  2153.   008A8 2EFF            LD      L,0FFH          ;Flag = FFH (DIV marker)
  2154.   008AA CD1409          CALL    0914H           ;Process exponent and sign
  2155.   008AD 34              INC     (HL)            ;Adjust exponents
  2156.   008AE 34              INC     (HL)
  2157.   008AF 2B              DEC     HL              ;HL -> MSB (X)
  2158.   008B0 7E              LD      A,(HL)          ;Store divisor in RAM
  2159.   008B1 328940          LD      (4089H),A       ;MSB at 4089H
  2160.   008B4 2B              DEC     HL
  2161.   008B5 7E              LD      A,(HL)
  2162.   008B6 328540          LD      (4085H),A       ;1st LSB at 4085H
  2163.   008B9 2B              DEC     HL
  2164.   008BA 7E              LD      A,(HL)
  2165.   008BB 328140          LD      (4081H),A       ;2nd LSB at 4081H
  2166.   008BE 41              LD      B,C             ;BHL = CDE
  2167.   008BF EB              EX      DE,HL           ;BHL = mantissa of dividend
  2168.   008C0 AF              XOR     A               ;A = 00H
  2169.   008C1 4F              LD      C,A             ;CDE = 000000H
  2170.   008C2 57              LD      D,A             ;(Result is computed in CDE)
  2171.   008C3 5F              LD      E,A
  2172.   008C4 328C40          LD      (408CH),A       ;(408C) = 00H
  2173.   008C7 E5              PUSH    HL              ;Save dividend
  2174.   008C8 C5              PUSH    BC
  2175.   008C9 7D              LD      A,L             ;A = 2nd LSB of dividend
  2176.   008CA CD8040          CALL    4080H           ;BHL = BHL - X (subtract
  2177.                                                 ;mantissa of dividend and
  2178.                                                 ;divisor)
  2179.                                                 ;A = (408CH)
  2180.   008CD DE00            SBC     A,00H           ;A = A - C-flag (subtract
  2181.                                                 ;borrow from last subtract
  2182.                                                 ;from MSB
  2183.   008CF 3F              CCF                     ;Invert C-flag
  2184.   008D0 3007            JR      NC,08D9H        ;Yes: continue at 08D9H
  2185.  
  2186.   008D2 328C40          LD      (408CH),A       ;No: write MSB back
  2187.   008D5 F1              POP     AF              ;Remove dividend from stack
  2188.   008D6 F1              POP     AF
  2189.   008D7 37              SCF                     ;C-flag = 1: jump is not
  2190.   008D8 D2C1E1          JP      NC,0E1C1H       ;executed
  2191. * 008D9   C1            POP     BC              :Dividend back to BHL
  2192. * 008DA     E1          POP     HL              ;(C-flag = 0 because of 08D0H)
  2193.   008DB 79              LD      A,C             ;A = MSB of result
  2194.   008DC 3C              INC     A               ;Bit 7 of A = 1 ?
  2195.   008DD 3D              DEC     A               ;(S-flag is influenced)
  2196.   008DE 1F              RRA                     ;Shift C-flag to bit 7 of A
  2197.                                                 ;for round routine
  2198.   008DF FA9707          JP      M,0797H         ;Yes: done, round CDE upwards
  2199.                                                 ;(if C-flag was 1) and write
  2200.                                                 ;result in X
  2201.   008E2 17              RLA                     ;Shift back and shift C-flag
  2202.                                                 ;into result
  2203.   008E3 7B              LD      A,E             ;(C << C << E << C-flag)
  2204.   008E4 17              RLA
  2205.   008E5 5F              LD      E,A
  2206.   008E6 7A              LD      A,D
  2207.   008E7 17              RLA
  2208.   008E8 57              LD      D,A
  2209.   008E9 79              LD      A,C
  2210.   008EA 17              RLA
  2211.   008EB 4F              LD      C,A
  2212.   008EC 29              ADD     HL,HL           ;Shift dividend 1 bit left
  2213.   008ED 78              LD      A,B
  2214.   008EE 17              RLA                     ;Overflow from HL to B
  2215.   008EF 47              LD      B,A
  2216.   008F0 3A8C40          LD      A,(408CH)       ;Overflow of dividend-shift
  2217.   008F3 17              RLA                     ;to 408CH (MSB of dividend)
  2218.   008F4 328C40          LD      (408CH),A
  2219.   008F7 79              LD      A,C             ;CDE = 000000H ?
  2220.   008F8 B2              OR      D
  2221.   008F9 B3              OR      E
  2222.   008FA 20CB            JR      NZ,08C7H        ;No: process next bit
  2223.                                                 ;Yes:
  2224.   008FC E5              PUSH    HL              ;Save HL
  2225.   008FD 212441          LD      HL,4124H        ;HL -> Exp (X)
  2226.   00900 35              DEC     (HL)            ;X = X / 2
  2227.                                                 ;Underflow ?
  2228.   00901 E1              POP     HL              ;Restore HL first
  2229.   00902 20C3            JR      NZ,08C7H        ;No: process next bit
  2230.  
  2231.   00904 C3B207          JP      07B2H           ;Yes: ?OV Error
  2232.  
  2233.  
  2234. ; SUB for SMUL, SDIV, DMUL and DDIV
  2235. ; Process exponents and signs
  2236.  
  2237. ; Entry for DDIV
  2238.  
  2239.   00907 3EFF            LD      A,0FFH          ;Flag = FFH
  2240. * 00909 2EAF            LD      L,AFH           ;--
  2241.  
  2242. ; Entry for DMUL
  2243.  
  2244.   0090A   AF            XOR     A               ;Flag = 00H
  2245.   0090B 212D41          LD      HL,412DH        ;HL -> MSB (Y)
  2246.   0090E 4E              LD      C,(HL)          ;C = MSB (Y)
  2247.   0090F 23              INC     HL              ;HL -> Exp (Y)
  2248.   00910 AE              XOR     (HL)            ;DMUL: A = Exp (Y)
  2249.                                                 ;DDIV: A = -Exp (Y) - 1
  2250.   00911 47              LD      B,A             ;B = Exp
  2251.   00912 2E00            LD      L,00H           ;Continue as with SMUL
  2252.  
  2253. ; Entry for SMUL (L=00H) and SDIV (L=FFH)
  2254.  
  2255.   00914 78              LD      A,B             ;A = Exp (BCDE)
  2256.   00915 B7              OR      A               ;BCDE = 0 ?
  2257.   00916 281F            JR      Z,0937H         ;Yes: result = 0
  2258.  
  2259.   00918 7D              LD      A,L             ;A = flag
  2260.   00919 212441          LD      HL,4124H        ;HL -> Exp (X)
  2261.   0091C AE              XOR     (HL)            ;SMUL: A = EXP (X)
  2262.                                                 ;SDIV: A = -Exp (X) - 1
  2263.   0091D 80              ADD     A,B             ;SMUL: A = Exp (Y) + Exp (X)
  2264.                                                 ;SDIV: A = Exp (Y) - Exp (X) -1
  2265.   0091E 47              LD      B,A             ;B = new Exp
  2266.   0091F 1F              RRA                     ;Shift C-flag into A, bit 7
  2267.   00920 A8              XOR     B               ;XOR with B
  2268.                                                 ;A, bit 7 = 0 when an overflow
  2269.                                                 ;or underflow occurs to C-flag
  2270.                                                 ;and A, bit 7 at the addition
  2271.                                                 ;of the exponents. (if both
  2272.                                                 ;exponents were positive (i.e.
  2273.                                                 ;> 80H), only an oveflow to
  2274.                                                 ;C-flag occurs; A, bit 7
  2275.                                                 ;becomes 0! )
  2276.   00921 78              LD      A,B             ;A = new exponent
  2277.   00922 F23609          JP      P,0936H         ;Under/overflow: continue at
  2278.                                                 ;0936H
  2279.   00925 C680            ADD     A,80H           ;Add offset to Exp
  2280.   00927 77              LD      (HL),A          ;Store new exponent in Exp (X)
  2281.                                                 ;Exponent = 0 ?
  2282.   00928 CA9008          JP      Z,0890H         ;Yes: done, continue ar 0890H
  2283.  
  2284.   0092B CDDF09          CALL    09DFH           ;Adjust mantissas
  2285.   0092E 77              LD      (HL),A          ;Save signflag
  2286.   0092F 2B              DEC     HL              ;HL = 4124H
  2287.   00930 C9              RET
  2288.  
  2289. ; Entry from Exp (X) when X > 127 or LSB (X) > 7DH
  2290. ; If X < 0, then set X = 0 else ?OV Error
  2291.  
  2292.   00931 CD5509          CALL    0955H           ;TEST2
  2293.   00934 2F              CPL                     ;If X < 0 then A positive
  2294.                                                 ;         else A negative
  2295.   00935 E1              POP     HL              ;Remove RET address
  2296.   00936 B7              OR      A               ;Set flags
  2297.   00937 E1              POP     HL              ;Remove RET address
  2298.   00938 F27807          JP      P,0778H         ;A positive then X = 0
  2299.  
  2300.   0093B C3B207          JP      07B2H           ;Else ?OV Error
  2301.  
  2302.  
  2303. ; SMUL10: X = X * 10
  2304. ; Multiplies number in X with 10
  2305. ;
  2306. ; I: X = single precision number
  2307. ; O: X = number * 10
  2308.  
  2309.   0093E CDBF09          CALL    09BFH           ;BCDE = X
  2310.   00941 78              LD      A,B             ;BCDE = 0 ?
  2311.   00942 B7              OR      A
  2312.   00943 C8              RET     Z               ;Yes: result = 0
  2313.  
  2314.   00944 C602            ADD     A,02H           ;Exp + 2: BCDE = BCDE * 4
  2315.                                                 ;Exp overflow ?
  2316.   00946 DAB207          JP      C,07B2H         ;Yes: ?OV Error
  2317.  
  2318.   00949 47              LD      B,A             ;Set Exp in BCDE
  2319.   0094A CD1607          CALL    0716H           ;X = X + BCDE = X + 4*X = 5 * X
  2320.   0094D 212441          LD      HL,4124H        ;HL -> Exp (X)
  2321.   00950 34              INC     (HL)            ;Exp (X) + 1: X = X * 2
  2322.                                                 ;Overflow ?
  2323.   00951 C0              RET     NZ              ;No: done, return
  2324.  
  2325.   00952 C3B207          JP      07B2H           ;?OV Error
  2326.  
  2327.  
  2328. ; TEST2: SNG function for X
  2329. ; Test single or double precision number in X if smaller,
  2330. ; equal or greater then zero
  2331. ; ?TM Error when X contains a STR
  2332. ;
  2333. ; I: X = single or double precision number to be tested
  2334. ; O: if X < 0: A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2335. ; O: if X = 0: A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0, P/V-flag = 1
  2336. ; O: if X > 0: A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2337.  
  2338.  
  2339.   00955 3A2441          LD      A,(4124H)       ;A = Exp (X)
  2340.   00958 B7              OR      A               ;X = 0 ?
  2341.   00959 C8              RET     Z               :Yes: return
  2342.  
  2343.   0095A 3A2341          LD      A,(4123H)       ;A,7 = sign
  2344.   0095D FE2F            CP      2FH             ;--
  2345.  
  2346. ; Entry from compare routines
  2347.  
  2348. * 0095D   2F            CPL
  2349.   0095F 17              RLA                     ;C-flag = sign
  2350.   00960 9F              SBC     A,A             ;A = FFH if X<0 else A = 00H
  2351.                                                 ;X < 0 ?
  2352.   00961 C0              RET     NZ              ;Yes: return
  2353.  
  2354.   00962 3C              INC     A               ;X > 0: A = 01H
  2355.   00963 C9              RET
  2356.  
  2357.  
  2358. ; FLOATA: convert A single precision number
  2359. ;
  2360. ; I: A = number
  2361. ; O: X = numerical value of A in single precision
  2362.  
  2363.   00964 0688            LD      B,88H           ;B = Exp for 2 ^ 8
  2364.   00966 110000          LD      DE,0000H        ;LSBs = 0
  2365.  
  2366.  
  2367. ; FLOAT: convert a binary number into single precision
  2368. ; 1. Binary number of 8 bits:
  2369. ;    I: B = 88H (Exp for 2 ^ 8)
  2370. ;       A = binary value
  2371. ;       DE = 0000H
  2372. ; 2. Binary number of 16 bits: (see also CINT)
  2373. ;    I: B = 90H (Exp for 2 ^ 16)
  2374. ;       A = MSB of 16 bit value
  2375. ;       D = LSB of 16 bit value
  2376. ;       E = 0000H
  2377. ; 3. Binary number of 24 bits:
  2378. ;    I: B = 98H (Exp for 2 ^ 24)
  2379. ;       A = MSB of 24 bit value
  2380. ;       DE = LSBs of 24 bit value
  2381. ; O: X = single precision number
  2382.  
  2383.   00969 212441          LD      HL,4124H        ;HL -> Exp (X)
  2384.   0096C 4F              LD      C,A             ;C = 8 bit value
  2385.   0096D 70              LD      (HL),B          ;Set exponent
  2386.   0096E 0600            LD      B,00H           ;B = 00H (becomes LSB at 0762H)
  2387.   00970 23              INC     HL              ;HL -> sign-flag
  2388.   00971 3680            LD      (HL),80H        ;Set sign-flag to 80H
  2389.   00973 17              RLA                     ;Sign-bit to C-flag (for 0762H)
  2390.   00974 C36207          JP      0762H           ;SFLOAT conversion CDEB to X
  2391.  
  2392.  
  2393. ; X = ABS (X)
  2394. ; -----------
  2395.  
  2396.   00977 CD9409          CALL    0994H           ;TEST1
  2397.                                                 :X is positive ?
  2398.   0097A F0              RET     P               ;Yes: return
  2399.  
  2400.   0097B E7              RST     20H             ;TSTTYP
  2401.                                                 ;INT type ?
  2402.   0097C FA5B0C          JP      M,0C5BH         ;Yes: continue at 0C5BH
  2403.                                                 ;STR type ?
  2404.   0097F CAF60A          JP      Z,0AF6H         ;Yes: ?TM error
  2405.  
  2406.  
  2407. ; SDNEG: X = -X
  2408. ; Negate number in X
  2409. ;
  2410. ; I: X = single of double precision number
  2411. ; O: X = negated number
  2412.  
  2413.   00982 212341          LD      HL,4123H        ;HL -> MSB of X
  2414.   00985 7E              LD      A,(HL)          ;A = MSB of X
  2415.   00986 EE80            XOR     80H             ;Invert sign bit
  2416.   00988 77              LD      (HL),A          ;Store MSB of X
  2417.   00989 C9              RET
  2418.  
  2419.  
  2420. ; X = SNG (X)
  2421. ; -----------
  2422.  
  2423.   0098A CD9409          CALL    0994H           ;TEST1
  2424.   0098D 6F              LD      L,A             ;L = SNG(value) (FFH,00H,01H)
  2425.   0098E 17              RLA                     ;Highest bit to C-flag
  2426.   0098F 9F              SBC     A,A             ;A = 00 when positive result
  2427.                                                 ;else A = FFH
  2428.   00990 67              LD      H,A             ;Now HL = A
  2429.   00991 C39A0A          JP      0A9AH           ;Write HL to X as INT
  2430.  
  2431.  
  2432. ; TEST1: As SNG function, but result in A
  2433. ; Test number in X if smaller, equal or greater then zero
  2434. ; ?TM Error when X contains a STR
  2435. ;
  2436. ; I: X = number to be tested
  2437. ; O: if X < 0: A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2438. ; O: if X = 0: A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0, P/V-flag = 1
  2439. ; O: if X > 0: A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2440.  
  2441.   00994 E7              RST     20H             ;TSTTYP
  2442.                                                 ;STR type ?
  2443.   00995 CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  2444.                                                 ;INT type ?
  2445.   00998 F25509          JP      P,0955H         ;No: SNG or DBL so TEST2
  2446.  
  2447.   0099B 2A2141          LD      HL,(4121H)      ;HL = X (INT)
  2448.   0099E 7C              LD      A,H             ;HL = 0000 ?
  2449.   0099F B5              OR      L
  2450.   009A0 C8              RET     Z               ;Yes: return
  2451.  
  2452.   009A1 7C              LD      A,H             ;A = MSB of X
  2453.                                                 ;if H < 0 then A, bit 7 = 1
  2454.                                                 ;         else A, bit 7 = 0
  2455.   009A2 18BB            JR      095FH           :Continue inside TEST2
  2456.  
  2457. ; (SP) = X (SNG)
  2458. ; (Save value in X)
  2459.  
  2460.   009A4 EB              EX      DE,HL           ;Save HL
  2461.   009A5 2A2141          LD      HL,(4121H)      ;HL = LSB (X)
  2462.   009A8 E3              EX      (SP),HL         ;Put LSBs on stack
  2463.   009A9 E5              PUSH    HL              ;Save RET address
  2464.   009AA 2A2341          LD      HL,(4123H)      ;Same with MSB and Exp
  2465.   009AD E3              EX      (SP),HL
  2466.   009AE E5              PUSH    HL
  2467.   009AF EB              EX      DE,HL           ;Restore HL
  2468.   009B0 C9              RET
  2469.  
  2470.  
  2471. ; X = BCDE = (HL) (SNG)
  2472.  
  2473.   009B1 CDC209          CALL    09C2H           ;BCDE = (HL)
  2474.  
  2475. ; X = BCDE (SNG)
  2476.  
  2477.   009B4 EB              EX      DE,HL           ;HL = LSBs
  2478.   009B5 222141          LD      (4121H),HL      ;Store in X
  2479.   009B8 60              LD      H,B             ;HL = MSB and Exp
  2480.   009B9 69              LD      L,C
  2481.   009BA 222341          LD      (4123H),HL      ;Store in X
  2482.   009BD EB              EX      DE,HL           ;Restore HL
  2483.   009BE C9              RET
  2484.  
  2485.  
  2486. ; BCDE = X (SNG)
  2487.  
  2488.   009BF 212141          LD      HL,4121H        ;HL -> X
  2489.  
  2490. ; BCDE = (HL) (SNG)
  2491.  
  2492.   009C2 5E              LD      E,(HL)          ;E = LSB
  2493.   009C3 23              INC     HL
  2494.   009C4 56              LD      D,(HL)          ;D = LSB + 1
  2495.   009C5 23              INC     HL
  2496.   009C6 4E              LD      C,(HL)          ;C = MSB
  2497.   009C7 23              INC     HL
  2498.   009C8 46              LD      B,(HL)          ;B = Exp
  2499.   009C9 23              INC     HL
  2500.   009CA C9              RET
  2501.  
  2502. ; (HL) = X (SNG)
  2503.  
  2504.   009CB 112141          LD      DE,4121H        ;DE -> X
  2505.   009CE 0604            LD      B,04H           ;B = size of SNG variable
  2506.   009D0 1805            JR      09D7H           ;Continue at 09D7H
  2507.  
  2508. ; (DE) = (HL)
  2509. ; Copy memory from (HL) to (DE), counter = (40AFH)
  2510.  
  2511.   009D2 EB              EX      DE,HL
  2512.  
  2513. ; As above but from (DE) to (HL)
  2514.  
  2515.   009D3 3AAF40          LD      A,(40AFH)       ;A = variable type in X
  2516.  
  2517. ; Copy routine: copies A bytes from (DE) to (HL)
  2518.  
  2519.   009D6 47              LD      B,A             ;Save in B as counter
  2520.  
  2521. ; Copy B bytes from (DE) to (HL)
  2522.  
  2523.   009D7 1A              LD      A,(DE)          ;Copy byte from (DE)
  2524.   009D8 77              LD      (HL),A          ;to (HL)
  2525.   009D9 13              INC     DE              ;Origin pointer + 1
  2526.   009DA 23              INC     HL              ;Destiniation pointer + 1
  2527.   009DB 05              DEC     B               ;All bytes done ?
  2528.   009DC 20F9            JR      NZ,09D7H        ;No: next byte
  2529.  
  2530.   009DE C9              RET
  2531.  
  2532.  
  2533. ; Sign processing for basic arithmatic operations
  2534. ; Set proper mantissas of X and BCDE (highest bit to 1)
  2535. ; and adjust sign
  2536. ;
  2537. ; I: X = single or double precision number
  2538. ;    BCDE = single or double precision number
  2539. ; 0: Correct mantissa of X and BCDE (highest bit = 1)
  2540. ;    If both signs are the same then A, bit 7 = 1 else A, bit 7 = 0
  2541.  
  2542.  
  2543.   009DF 212341          LD      HL,4123H        ;HL -> MSB of X
  2544.   009E2 7E              LD      A,(HL)          ;A = MSB of X
  2545.   009E3 07              RLCA                    ;C-flag and A, bit 0 = Sign X
  2546.   009E4 37              SCF                     ;C-flag = 1
  2547.   009E5 1F              RRA                     ;A, bit 7 = C-flag = 1,
  2548.                                                 ;C-flag = A, bit 0 = sign
  2549.   009E6 77              LD      (HL),A          ;Correct Mantissa
  2550.   009E7 3F              CCF                     ;Invert C-flag
  2551.   009E8 1F              RRA                     ;A, bit 0 = inverted sign
  2552.   009E9 23              INC     HL
  2553.   009EA 23              INC     HL              ;HL = 4125H (sign-flag)
  2554.   009EB 77              LD      (HL),A          ;Save sign flag
  2555.   009EC 79              LD      A,C             ;Same with BCDE but leave sign
  2556.   009ED 07              RLCA
  2557.   009EE 37              SCF
  2558.   009EF 1F              RRA
  2559.   009F0 4F              LD      C,A
  2560.   009F1 1F              RRA
  2561.   009F2 AE              XOR     (HL)            ;XOR both signs
  2562.   009F3 C9              RET
  2563.  
  2564. ; X = Y (SNG,DBL)
  2565.  
  2566.   009F4 212741          LD      HL,4127H        ;HL -> LSB of Y
  2567.  
  2568. ; X = (HL) (SNG,DBL)
  2569.  
  2570.   009F7 11D209          LD      DE,09D2H        ;DE = address of copy routine
  2571.                                                 ;(HL) to (DE)
  2572.   009FA 1806            JR      0A02H           ;Continue at 0A02H
  2573.  
  2574. ; Y = X (SNG,DBL)
  2575.  
  2576.   009FC 212741          LD      HL,4127H        ;HL -> LSB of Y
  2577.  
  2578. ; (HL) = X (SNG,DBL)
  2579.  
  2580.   009FF 11D309          LD      DE,09D3H        ;DE = address of copy routine
  2581.                                                 ;(DE) to (HL)
  2582.   00A02 D5              PUSH    DE              ;Put address on stack as
  2583.                                                 ;new RET address
  2584.   00A03 112141          LD      DE,4121H        ;DE -> X (SNG)
  2585.   00A06 E7              RST     20H             ;TSTTYP
  2586.                                                 ;SNG ?
  2587.   00A07 D8              RET     C               ;Yes: ok
  2588.  
  2589.   00A08 111D41          LD      DE,411DH        ;DE -> X (DBL)
  2590.  
  2591.   00A0B C9              RET
  2592.  
  2593.  
  2594. ; CP X , BCDE (SNG)
  2595. ;
  2596. ; I: -
  2597. ; O: if X < BCDE then A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2598. ;    if X = BCDE then A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0
  2599. ;    if X > BCDE then A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2600.  
  2601.   00A0C 78              LD      A,B             ;BCDE = 0 ?
  2602.   00A0D B7              OR      A
  2603.   00A0E CA5509          JP      Z,0955H         ;Yes: TEST2
  2604.  
  2605.   00A11 215E09          LD      HL,095EH        ;Set new RET address to TEST2
  2606.   00A14 E5              PUSH    HL
  2607.   00A15 CD5509          CALL    0955H           ;TEST2
  2608.   00A18 79              LD      A,C             ;A = MSB (BCDE)
  2609.                                                 ;X = 0 ?
  2610.   00A19 C8              RET     Z               ;Yes: goto TEST2 (only test
  2611.                                                 ;BCDE)
  2612.  
  2613.   00A1A 212341          LD      HL,4123H        ;HL -> MSB (X)
  2614.   00A1D AE              XOR     (HL)            ;Compare signs
  2615.   00A1E 79              LD      A,C             ;A = MSB (BCDE)
  2616.   00A1F F8              RET     M               ;Goto TEST2, when both signs
  2617.                                                 ;are not equal (only test BCDE)
  2618.   00A20 CD260A          CALL    0A26H           ;Compare X and BCDE bytewise
  2619.   00A23 1F              RRA                     ;A, bit 7 = C-flag
  2620.   00A24 A9              XOR     C               ;XOR with sign of BCDE
  2621.   00A25 C9              RET                     ;Continue at TEST2
  2622.  
  2623.  
  2624. ; SUB for CP
  2625. ; Compare X and BCDE bytewise
  2626.  
  2627.   00A26 23              INC     HL              ;Compare Exp
  2628.   00A27 78              LD      A,B
  2629.   00A28 BE              CP      (HL)
  2630.   00A29 C0              RET     NZ              ;If not equal: return
  2631.  
  2632.   00A2A 2B              DEC     HL              :Same with MSBs
  2633.   00A2B 79              LD      A,C
  2634.   00A2C BE              CP      (HL)
  2635.   00A2D C0              RET     NZ
  2636.  
  2637.   00A2E 2B              DEC     HL              ;1st LSB
  2638.   00A2F 7A              LD      A,D
  2639.   00A30 BE              CP      (HL)
  2640.   00A31 C0              RET     NZ
  2641.  
  2642.   00A32 2B              DEC     HL              ;2nd LSB
  2643.   00A33 7B              LD      A,E
  2644.   00A34 96              SUB     (HL)            ;Subtract, so that A = 00H
  2645.                                                 ;in case E = (HL)
  2646.   00A35 C0              RET     NZ
  2647.  
  2648.   00A36 E1              POP     HL              ;Remove RET address (0A23H)
  2649.   00A37 E1              POP     HL              ;Remove RET address (095E)
  2650.   00A38 C9              RET                     ;Return with A = 00H, Z-flag = 1
  2651.  
  2652.  
  2653. ; CP HL , DE (INT)
  2654. ;
  2655. ; I: -
  2656. ; O: if HL < DE then A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2657. ;    if HL = DE then A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0
  2658. ;    if HL > DE then A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2659.  
  2660.   00A39 7A              LD      A,D             ;Compare signs
  2661.   00A3A AC              XOR     H
  2662.   00A3B 7C              LD      A,H             ;A = MSB (HL)
  2663.   00A3C FA5F09          JP      M,095FH         ;If signs are not equal:
  2664.                                                 ;continue at TEST2
  2665.   00A3F BA              CP      D               ;Compare MSBs
  2666.   00A40 C26009          JP      NZ,0960H        ;If not equal: goto TEST2
  2667.  
  2668.   00A43 7D              LD      A,L             ;Compare LSBs
  2669.   00A44 93              SUB     E               ;Subtract, so that A = 00H
  2670.                                                 ;in case E = L
  2671.   00A45 C26009          JP      NZ,0960H        ;If not equal: goto TEST2
  2672.  
  2673.   00A48 C9              RET
  2674.  
  2675.  
  2676. ; CP X , (DE) (DBL)
  2677. ;
  2678. ; I: -
  2679. ; O: if X < (DE) then A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2680. ;    if X = (DE) then A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0
  2681. ;    if X > (DE) then A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2682.  
  2683.   00A49 212741          LD      HL,4127H        ;HL -> Y
  2684.   00A4C CDD309          CALL    09D3H           ;Copy (DE) to (HL)
  2685.   00A4F 112E41          LD      DE,412EH        ;DE -> Exp (Y)
  2686.   00A52 1A              LD      A,(DE)          ;A = Exp (Y)
  2687.   00A53 B7              OR      A               ;Y = 0 ?
  2688.   00A54 CA5509          JP      Z,0955H         ;Yes: execute TEST2
  2689.  
  2690.   00A57 215E09          LD      HL,095EH        ;Set new RET address
  2691.   00A5A E5              PUSH    HL              ;to TEST2
  2692.   00A5B CD5509          CALL    0955H           ;TEST2
  2693.   00A5E 1B              DEC     DE              ;DE = MSB (Y)
  2694.   00A5F 1A              LD      A,(DE)          ;A = MSB (Y)
  2695.   00A60 4F              LD      C,A             ;C = MSB (Y) (for TEST2)
  2696.                                                 ;X = 0 ?
  2697.   00A61 C8              RET     Z               ;Yes: TEST2 using Y
  2698.  
  2699.   00A62 212341          LD      HL,4123H        ;HL -> MSB (X)
  2700.   00A65 AE              XOR     (HL)            ;Compare signs
  2701.   00A66 79              LD      A,C             ;A = MSB (Y)
  2702.                                                 ;Signs not equal ?
  2703.   00A67 F8              RET     M               ;Yes: TEST2
  2704.  
  2705.   00A68 13              INC     DE              ;DE = Exp (Y)
  2706.   00A69 23              INC     HL              ;HL = Exp (X)
  2707.   00A6A 0608            LD      B,08H           ;B = counter for 8 bytes
  2708.   00A6C 1A              LD      A,(DE)          ;Compare X and Y bytewise
  2709.   00A6D 96              SUB     (HL)
  2710.                                                 ;Byte from X = Byte from Y ?
  2711.   00A6E C2230A          JP      NZ,0A23H        ;No: contineu at 0A23H
  2712.  
  2713.   00A71 1B              DEC     DE              ;Pointer on Y - 1
  2714.   00A72 2B              DEC     HL              ;Pointer on X - 1
  2715.   00A73 05              DEC     B               ;Counter - 1
  2716.   00A74 20F6            JR      NZ,0A6CH        ;Compare next byte from X and Y
  2717.  
  2718.   00A76 C1              POP     BC              ;Remove RET address (095EH)
  2719.   00A77 C9              RET
  2720.  
  2721.  
  2722. ; CP X , Y (DBL)
  2723. ;
  2724. ; I: -
  2725. ; O: if X < Y then A = FFH, C-flag = 1, Z-flag = 0, S-flag = 1
  2726. ;    if X = Y then A = 00H, C-flag = 0, Z-flag = 1, S-flag = 0
  2727. ;    if X > Y then A = 01H, C-flag = 0, Z-flag = 0, S-flag = 0
  2728.  
  2729.   00A78 CD4F0A          CALL    0A4FH           ;Compare X and Y
  2730.   00A7B C25E09          JP      NZ,095EH        ;If X <> Y then goto TEST2
  2731.  
  2732.   00A7E C9              RET
  2733.  
  2734.  
  2735. ; X = CINT (X)
  2736. ; ------------
  2737.  
  2738.   00A7F E7              RST     20H             ;TSTTYP
  2739.   00A80 2A2141          LD      HL,(4121H)      ;HL -> X
  2740.                                                 ;INT type ?
  2741.   00A83 F8              RET     M               ;Yes: return
  2742.                                                 ;STR type ?
  2743.   00A84 CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  2744.                                                 ;DBL type ?
  2745.   00A87 D4B90A          CALL    NC,0AB9H        ;Yes: convert to SNG
  2746.   00A8A 21B207          LD      HL,07B2H        ;Set new return address
  2747.   00A8D E5              PUSH    HL              ;to 07B2H (= ?OV Error)
  2748.   00A8E 3A2441          LD      A,(4124H)       ;A = Exp (X)
  2749.   00A91 FE90            CP      90H             ;Exp > (2 ^ 16) ? (16 bit)
  2750.   00A93 300E            JR      NC,0AA3H        ;Yes: continue at 0AA3H
  2751.  
  2752.   00A95 CDFB0A          CALL    0AFBH           ;DE = INT (X)
  2753.   00A98 EB              EX      DE,HL           ;HL = INT value
  2754.   00A99 D1              POP     DE              ;Remove new return address
  2755.  
  2756.  
  2757. ; This routine writes the contents of HL into the X register as INT
  2758. ;
  2759. ; I: HL = integer value
  2760. ; O: X = integer value
  2761. ;    A = VT of X (INT)
  2762.  
  2763.   00A9A 222141          LD      (4121H),HL      ;Write HL in X
  2764.  
  2765. ; Set VT to INT
  2766.  
  2767.   00A9D 3E02            LD      A,02H           ;A = type code INT
  2768.   00A9F 32AF40          LD      (40AFH),A       ;Save VT
  2769.   00AA2 C9              RET
  2770.  
  2771.  
  2772. ; Continuation of CINT (X)
  2773. ; X = -32768 ? (Is it still within the INT format ?)
  2774.  
  2775.   00AA3 018090          LD      BC,9080H        ;BCDE  = -32768
  2776.   00AA6 110000          LD      DE,0000H
  2777.   00AA9 CD0C0A          CALL    0A0CH           ;Compare X and BCDE
  2778.                                                 ;X = BCDE ?
  2779.   00AAC C0              RET     NZ              ;No: ?OV Error
  2780.  
  2781.   00AAD 61              LD      H,C             ;HL = -32768
  2782.   00AAE 6A              LD      L,D
  2783.   00AAF 18E8            JR      0A99H           ;Remove RET address and
  2784.                                                 ;set X = HL
  2785.  
  2786. ; X = CSNG (X)
  2787. ; ------------
  2788.  
  2789.   00AB1 E7              RST     20H             ;TSTTYP
  2790.                                                 ;SNG type ?
  2791.   00AB2 E0              RET     PO              ;Yes: return
  2792.                                                 ;INT type ?
  2793.   00AB3 FACC0A          JP      M,0ACCH         ;Yes: continue at 0ACCH
  2794.                                                 ;STR type ?
  2795.   00AB6 CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  2796.                                                 ;DBL type:
  2797.   00AB9 CDBF09          CALL    09BFH           ;BCDE = X
  2798.   00ABC CDEF0A          CALL    0AEFH           ;VT = SNG
  2799.   00ABF 78              LD      A,B             ;BCDE = 0 ?
  2800.   00AC0 B7              OR      A
  2801.   00AC1 C8              RET     Z               ;Yes: ok
  2802.  
  2803.   00AC2 CDDF09          CALL    09DFH           ;Adjust mantissas
  2804.   00AC5 212041          LD      HL,4120H
  2805.   00AC8 46              LD      B,(HL)          ;B = 3rd LSB (X) with DBL
  2806.   00AC9 C39607          JP      0796H           ;X = BCDE and round
  2807.  
  2808. ; X = CSNG (X) (INT)
  2809.  
  2810.   00ACC 2A2141          LD      HL,(4121H)      ;HL = INT value
  2811.  
  2812. ; X = CSNG (HL) (INT)
  2813.  
  2814.   00ACF CDEF0A          CALL    0AEFH           ;VT = SNG
  2815.   00AD2 7C              LD      A,H             ;A = MSB (HL)
  2816.   00AD3 55              LD      D,L             ;D = LSB (HL)
  2817.   00AD4 1E00            LD      E,00H           ;E = 00H
  2818.   00AD6 0690            LD      B,90H           ;B = 90H (Exp = 2 ^ 16)
  2819.   00AD8 C36909          JP      0969H           ;SFLOAT (X)
  2820.  
  2821.  
  2822. ; X = CDBL (X)
  2823. ; ------------
  2824.  
  2825.   00ADB E7              RST     20H             ;TSTTYP
  2826.                                                 ;DBL type ?
  2827.   00ADC D0              RET     NC              ;Yes: return
  2828.                                                 ;STR type ?
  2829.   00ADD CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  2830.                                                 ;INT type ?
  2831.   00AE0 FCCC0A          CALL    M,0ACCH         ;Yes: CSGN
  2832.   00AE3 210000          LD      HL,0000H
  2833.   00AE6 221D41          LD      (411DH),HL      ;Set 4 LSBs to zero
  2834.   00AE9 221F41          LD      (411FH),HL
  2835.  
  2836. ; Set VT to DBL
  2837.  
  2838.   00AEC 3E08            LD      A,08H           ;A = type code for DBL
  2839.   00AEE 013E04          LD      BC,043EH        ;--
  2840.  
  2841. ; Set VT to SNG
  2842.  
  2843. * 00AEF   3E04          LD      A,04            ;A = type code for SNG
  2844.   00AF1 C39F0A          JP      0A9FH           ;VT = A
  2845.  
  2846.  
  2847. ; Test if X is string type. If not: ?TM Error else return
  2848.  
  2849.   00AF4 E7              RST     20H             ;TSTTYP
  2850.                                                 ;STR type ?
  2851.   00AF5 C8              RET     Z               ;Yes: return
  2852.  
  2853.   00AF6 1E18            LD      E,18H           ;E = error code for ?TM Error
  2854.   00AF8 C3A219          JP      19A2H           ;Continue at error routine
  2855.  
  2856.  
  2857. ; SUB for INT, FIX, CINT
  2858. ; I: X = SNG value with Exp <= 98H (2 ^ 24)
  2859. ;    (so with fractional part)
  2860. ;    A = Exp (X)
  2861. ; O: DE = INT value of X
  2862.  
  2863.   00AFB 47              LD      B,A             ;BCDE = A
  2864.   00AFC 4F              LD      C,A
  2865.   00AFD 57              LD      D,A
  2866.   00AFE 5F              LD      E,A
  2867.   00AFF B7              OR      A               ;Zero ?
  2868.   00B00 C8              RET     Z               ;Yes: return with DE = 0000H
  2869.  
  2870.   00B01 E5              PUSH    HL              ;Save HL
  2871.   00B02 CDBF09          CALL    09BFH           ;BCDE = X
  2872.   00B05 CDDF09          CALL    09DFH           ;Adjust mantissas
  2873.   00B08 AE              XOR     (HL)            ;A, bit 7 = Sign of BCDE or X
  2874.   00B09 67              LD      H,A             ;H, bit 7 = Sign (X)
  2875.                                                 ;X negative ?
  2876.   00B0A FC1F0B          CALL    M,0B1FH         ;Yes: round BCDE
  2877.   00B0D 3E98            LD      A,98H           ;A = Exp (2 ^ 24)
  2878.                                                 ;(24 bit mantissa, no fraction)
  2879.   00B0F 90              SUB     B               ;A = 98H - Exp (X)
  2880.   00B10 CDD707          CALL    07D7H           ;Shift CDE to the right for
  2881.                                                 ;A bits (B becomes LSB)
  2882.                                                 ;Shift out franctional part
  2883.   00B13 7C              LD      A,H             ;A, bit 7 = Sign (X)
  2884.   00B14 17              RLA                     ;C-flag = sign
  2885.                                                 ;X negative ?
  2886.   00B15 DCA807          CALL    C,07A8H         ;Yes: round up X
  2887.   00B18 0600            LD      B,00H           ;Clear LSB
  2888.                                                 ;X negative ?
  2889.   00B1A DCC307          CALL    C,07C3H         ;Yes: Invert CDEB
  2890.   00B1D E1              POP     HL              ;Restore HL
  2891.   00B1E C9              RET
  2892.  
  2893. ; Round down BCDE
  2894.  
  2895.   00B1F 1B              DEC     DE              ;Round LSBs
  2896.   00B20 7A              LD      A,D             ;Test LSBs
  2897.   00B21 A3              AND     E               ;Was DE = 0000H ?
  2898.   00B22 3C              INC     A               ;Yes: then A now is 00H
  2899.   00B23 C0              RET     NZ              ;Done when LSBs <> 0
  2900.  
  2901.   00B24 0B              DEC     BC              ;Round MSBs
  2902.   00B25 C9              RET
  2903.  
  2904.  
  2905. ; X = FIX (X)
  2906. ; -----------
  2907.  
  2908.   00B26 E7              RST     20H             ;TSTTYP
  2909.                                                 ;INT type ?
  2910.   00B27 F8              RET     M               ;Yes: return
  2911.  
  2912.   00B28 CD5509          CALL    0955H           ;TEST2
  2913.                                                 ;X < 0 ?
  2914.   00B2B F2370B          JP      P,0B37H         ;No: X = INT(X)
  2915.  
  2916.   00B2E CD8209          CALL    0982H           ;X = -X
  2917.   00B31 CD370B          CALL    0B37H           ;X = INT(X)
  2918.   00B34 C37B09          JP      097BH           ;X = ABS (X) and RET
  2919.  
  2920.  
  2921. ; X = INT (X)
  2922. ; -----------
  2923.  
  2924.   00B37 E7              RST     20H             ;TSTTYP
  2925.                                                 ;INT type ?
  2926.   00B38 F8              RET     M               ;Yes: return
  2927.                                                 ;DBL type ?
  2928.   00B39 301E            JR      NC,0B59H        ;Yes: continue at 0B59H
  2929.                                                 ;STR type ?
  2930.   00B3B 28B9            JR      Z,0AF6H         ;Yes: ?TM Error
  2931.  
  2932.  
  2933. ; X = INT (X) (SNG)
  2934. ;
  2935. ; I: X = single precision value
  2936. ; O: X = single precision value without fractional part
  2937. ;    A = LSB of integer value of X
  2938.  
  2939.   00B3D CD8E0A          CALL    0A8EH           ;X = CINT (X) (SNG)
  2940.   00B40 212441          LD      HL,4124H        ;HL -> Exp (X)
  2941.   00B43 7E              LD      A,(HL)          ;A = Exp (X)
  2942.   00B44 FE98            CP      98H             ;Exp >= (2 ^ 24) ?
  2943.                                                 ;Yes: X has no fraction
  2944.   00B46 3A2141          LD      A,(4121H)       ;A = LSB of X
  2945.   00B49 D0              RET     NC              ;Done when Exp (X) >= 98H
  2946.  
  2947.   00B4A 7E              LD      A,(HL)          ;A = Exp (X)
  2948.   00B4B CDFB0A          CALL    0AFBH           ;Remove fractional part
  2949.   00B4E 3698            LD      (HL),98H        ;Set Exp to 2 ^ 24
  2950.   00B50 7B              LD      A,E             ;A = LSB
  2951.   00B51 F5              PUSH    AF              ;Save LSB
  2952.   00B52 79              LD      A,C             ;A = MSB
  2953.   00B53 17              RLA                     ;Sign to C-flag
  2954.   00B54 CD6207          CALL    0762H           ;SFLOAT conversion CDEB to X
  2955.   00B57 F1              POP     AF              ;LSB back to A
  2956.   00B58 C9              RET
  2957.  
  2958.  
  2959. ; X = INT (X) (DBL)
  2960.  
  2961.   00B59 212441          LD      HL,4124H        ;HL -> Exp (X)
  2962.   00B5C 7E              LD      A,(HL)          ;A = Exp (X)
  2963.   00B5D FE90            CP      90H             ;Exp (X) < (2 ^ 16)
  2964.   00B5F DA7F0A          JP      C,0A7FH         ;Yes: execute CINT
  2965.                                                 ;Exp (X) = (2 ^ 16)
  2966.   00B62 2014            JR      NZ,0B78H        ;No: continue at 0B78H
  2967.  
  2968.   00B64 4F              LD      C,A             ;C = 90H (Exp = 2 ^ 16!)
  2969.   00B65 2B              DEC     HL              ;HL = MSB (X)
  2970.   00B66 7E              LD      A,(HL)          ;A = MSB (X)
  2971.   00B67 EE80            XOR     80H             ;A = 00H, if X = -32768
  2972.   00B69 0606            LD      B,06H           ;Test remaining 6 bytes
  2973.   00B6B 2B              DEC     HL              ;Pointer - 1
  2974.   00B6C B6              OR      (HL)            ;Test byte
  2975.   00B6D 05              DEC     B               ;Counter - 1
  2976.   00B6E 20FB            JR      NZ,0B6BH        ;Next byte
  2977.  
  2978.   00B70 B7              OR      A               ;All bytes = 00H ?
  2979.   00B71 210080          LD      HL,8000H        ;HL = -32768 (INT value)
  2980.   00B74 CA9A0A          JP      Z,0A9AH         ;Yes: Write HL to X as INT
  2981.  
  2982.   00B77 79              LD      A,C             ;A = Exp (X) = 90H (because
  2983.                                                 ;of 0B64H)
  2984.   00B78 FEB8            CP      0B8H            ;Exp >= (2 ^ 56) ?
  2985.                                                 ;(56 bit mantissa)
  2986.   00B7A D0              RET     NC              ;Yes: no fractional part
  2987.  
  2988.   00B7B F5              PUSH    AF              ;Save Exp
  2989.   00B7C CDBF09          CALL    09BFH           ;BCDE = X (SNG)
  2990.   00B7F CDDF09          CALL    09DFH           ;Adjust mantissas
  2991.   00B82 AE              XOR     (HL)            ;A, bit 7 = Sign of X or BCDE
  2992.   00B83 2B              DEC     HL              ;HL -> Exp (X)
  2993.   00B84 36B8            LD      (HL),0B8H       ;Set Exp to 2 ^ 56
  2994.   00B86 F5              PUSH    AF              ;Save sign
  2995.                                                 ;X negative ?
  2996.   00B87 FCA00B          CALL    M,0BA0H         ;Yes: round X
  2997.   00B8A 212341          LD      HL,4123H        ;HL -> MSB (X)
  2998.   00B8D 3EB8            LD      A,0B8H          ;A = Exp for 2 ^ 56
  2999.   00B8F 90              SUB     B               ;Compute Exp-difference
  3000.   00B90 CD690D          CALL    0D69H           ;(HL) - (HL-7) (corresponds
  3001.                                                 ;with X) and shift A bits to
  3002.                                                 ;the right: shift out the
  3003.                                                 ;fractional part
  3004.   00B93 F1              POP     AF              ;Restore sign
  3005.                                                 ;X negative ?
  3006.   00B94 FC200D          CALL    M,0D20H         ;Yes: round up mantissa of X
  3007.   00B97 AF              XOR     A               ;A = 00H
  3008.   00B98 321C41          LD      (411CH),A       ;Clear LSB-underflow of X
  3009.   00B9B F1              POP     AF              ;Restore Exp in A
  3010.   00B9C D0              RET     NC              ;Done when CALLed from 12B8H
  3011.                                                 ;(Otherwise C-flag = 1 because
  3012.                                                 ;of 0B7AH)
  3013.   00B9D C3D80C          JP      0CD8H           ;Continue at DFLOAT
  3014.  
  3015.  
  3016. ; Round down mantissa of X (DBL)
  3017.  
  3018.   00BA0 211D41          LD      HL,411DH        ;HL -> X
  3019.   00BA3 7E              LD      A,(HL)          ;A = byte of X
  3020.   00BA4 35              DEC     (HL)            ;Round down
  3021.   00BA5 B7              OR      A               ;Was the byte 00H ?
  3022.   00BA6 23              INC     HL              ;Pointer + 1
  3023.   00BA7 28FA            JR      Z,0BA3H         ;Yes: next byte
  3024.  
  3025.   00BA9 C9              RET
  3026.  
  3027.  
  3028. ; SUB for array size calculation with DIM
  3029. ; DE = DE * BC
  3030.  
  3031.   00BAA E5              PUSH    HL              ;Save PTP
  3032.   00BAB 210000          LD      HL,0000H        ;Result = 0000H
  3033.   00BAE 78              LD      A,B             ;BC = 0 ?
  3034.   00BAF B1              OR      C
  3035.   00BB0 2812            JR      Z,0BC4H         ;Yes: return with result = 0
  3036.  
  3037.   00BB2 3E10            LD      A,10H           ;A = counter for 16 bit
  3038.   00BB4 29              ADD     HL,HL           ;Shift result to the left
  3039.                                                 ;Overflow ?
  3040.   00BB5 DA3D27          JP      C,273DH         ;Yes: ?BS Error
  3041.  
  3042.   00BB8 EB              EX      DE,HL           ;Save HL
  3043.   00BB9 29              ADD     HL,HL           ;Highest bit of DE to C-flag
  3044.   00BBA EB              EX      DE,HL           ;Restore HL
  3045.   00BBB 3004            JR      NC,0BC1H        ;Jump when bit = 0
  3046.  
  3047.   00BBD 09              ADD     HL,BC           ;Add BC to result
  3048.                                                 ;Overflow ?
  3049.   00BBE DA3D27          JP      C,273DH         ;Yes: ?BS Error
  3050.  
  3051.   00BC1 3D              DEC     A               ;Counter - 1
  3052.   00BC2 20F0            JR      NZ,0BB4H        ;Next bit
  3053.  
  3054.   00BC4 EB              EX      DE,HL           ;Result in DE
  3055.   00BC5 E1              POP     HL              ;Restore PTP
  3056.   00BC6 C9              RET
  3057.  
  3058.  
  3059. ; ISUB: X = HL = DE - HL (INT)
  3060. ;
  3061. ; I: DE = Original value
  3062. ;    HL = Subtractor
  3063. ; O: HL = Difference when in INT range
  3064. ;    X  = difference (automatic conversion to SNG format when result not
  3065. ;    in INT range)
  3066.  
  3067.   00BC7 7C              LD      A,H             ;A = MSB of HL
  3068.   00BC8 17              RLA                     ;C-flag - sign of HL
  3069.   00BC9 9F              SBC     A,A             ;If HL >= 0 then A = 00H
  3070.                                                 ;           else A = FFH
  3071.   00BCA 47              LD      B,A             ;B = A
  3072.   00BCB CD510C          CALL    0C51H           ;HL = = HL, C-flag = 1
  3073.   00BCE 79              LD      A,C             ;A = 00H (C = 00H by 0C51H)
  3074.   00BCF 98              SBC     A,B             ;if HL was >= 0 then A = FFH
  3075.                                                                 else A = 00H
  3076.   00BD0 1803            JR      0BD5H           ;Continue at IADD
  3077.                                                 ;(do a DE = DE + (-HL)
  3078.  
  3079. ; IADD: X = HL = DE + HL (INT)
  3080. ;
  3081. ; I: DE = 1st sum argument
  3082. ;    HL = 2nd sum argument
  3083. ; O: HL = sum when in INT range
  3084. ;    X  = sum (automatic conversion to SNG format when result not
  3085. ;    in INT range)
  3086.  
  3087.   00BD2 7C              LD      A,H             ;Calculate sign-flag
  3088.   00BD3 17              RLA                     ;(See ISUB)
  3089.   00BD4 9F              SBC     A,A
  3090.   00BD5 47              LD      B,A             ;If HL >= 0 then B = 00H
  3091.                                                 ;           else B = FFH
  3092.   00BD6 E5              PUSH    HL              ;Save 2nd sum argument
  3093.   00BD7 7A              LD      A,D             ;Calculate sign of 2nd arg.
  3094.   00BD8 17              RLA
  3095.   00BD9 9F              SBC     A,A
  3096.   00BDA 19              ADD     HL,DE           ;Perform addition
  3097.   00BDB 88              ADC     A,B             ;Process sign-flags and
  3098.   00BDC 0F              RRCA                    ;overflow
  3099.   00BDD AC              XOR     H               ;A, bit 7 is set, when an
  3100.                                                 ;overflow occurs in case of
  3101.                                                 ;both are equal or when no
  3102.                                                 ;overflow occurs in case of
  3103.                                                 ;both signs are not equal
  3104.                                                 ;A, bit 7 = 0 ?
  3105.   00BDE F2990A          JP      P,0A99H         ;Yes: result is ok
  3106.                                                 ;No:
  3107.   00BE1 C5              PUSH    BC              ;Save sign flag of 2nd arg.
  3108.   00BE2 EB              EX      DE,HL           ;HL = 1st sum argument
  3109.   00BE3 CDCF0A          CALL    0ACFH           ;X = CSNG (HL)
  3110.   00BE6 F1              POP     AF              ;Restore sign flag
  3111.   00BE7 E1              POP     HL              ;Restore 2nd sum argument
  3112.   00BE8 CDA409          CALL    09A4H           ;(SP) = X = 1st argument
  3113.   00BEB EB              EX      DE,HL           ;DE = 2nd argument
  3114.   00BEC CD6B0C          CALL    0C6BH           ;X = SFLOAT (DE)
  3115.   00BEF C38F0F          JP      0F8FH           ;X = X + (SP) (addition in
  3116.                                                 ;SNG format)
  3117.  
  3118. ; IMUL: X = HL = DE * HL (INT)
  3119. ;
  3120. ; I: DE = multiplicant
  3121. ;    HL = multiplicator (both values in INT format)
  3122. ; O: HL = product when in INT range
  3123. ;    X  = product (automatic conversion to SNG format when result not
  3124. ;    in INT range)
  3125.  
  3126.   00BF2 7C              LD      A,H             ;HL = 0000H ?
  3127.   00BF3 B5              OR      L
  3128.   00BF4 CA9A0A          JP      Z,0A9AH         ;Yes: Result = 0
  3129.  
  3130.   00BF7 E5              PUSH    HL              ;Save multiplicator
  3131.   00BF8 D5              PUSH    DE              ;Save multiplicant
  3132.   00BF9 CD450C          CALL    0C45H           ;Clear sign, make both numbers
  3133.                                                 ;positive
  3134.   00BFC C5              PUSH    BC              ;Save sign flag (B, bit 7 = 0
  3135.                                                 ;in case both signs are equal)
  3136.   00BFD 44              LD      B,H             ;BC = HL
  3137.   00BFE 4D              LD      C,L
  3138.   00BFF 210000          LD      HL,0000H        ;Result = 0
  3139.   00C02 3E10            LD      A,10H           ;A = counter for 16 bits
  3140.   00C04 29              ADD     HL,HL           ;Shift result to the left
  3141.                                                 ;Overflow ?
  3142.   00C05 381F            JR      C,0C26H         ;Yes: continue at 0C26H
  3143.  
  3144.   00C07 EB              EX      DE,HL           ;Next bit form De to C-flag
  3145.   00C08 29              ADD     HL,HL
  3146.   00C09 EB              EX      DE,HL
  3147.   00C0A 3004            JR      NC,0C10H        ;Jump if bit not set
  3148.  
  3149.   00C0C 09              ADD     HL,BC           ;Add BC to result if bit set
  3150.                                                 ;Overflow ?
  3151.   00C0D DA260C          JP      C,0C26H         ;Yes: continue at 0C26H
  3152.  
  3153.   00C10 3D              DEC     A               ;Counter - 1
  3154.   00C11 20F1            JR      NZ,0C04H        ;Next bit
  3155.  
  3156.   00C13 C1              POP     BC              ;Restore sign flag
  3157.   00C14 D1              POP     DE              ;Restore multiplicant
  3158.   00C15 7C              LD      A,H             ;Result negative ?
  3159.   00C16 B7              OR      A
  3160.   00C17 FA1F0C          JP      M,0C1FH         ;Yes: continue at 0C1FH
  3161.  
  3162.   00C1A D1              POP     DE              ;Restore multiplicator
  3163.   00C1B 78              LD      A,B             ;A = sign-flag
  3164.   00C1C C34D0C          JP      0C4DH           ;Set sign of result
  3165.  
  3166.  
  3167. ; Overflow into bit 15 (sign bit)
  3168. ; Because both factors were made positive, the result must also be positive.
  3169. ; This means that the 16th bit must be 0.
  3170.  
  3171.   00C1F EE80            XOR     80H             ;A = 00H when H = 80H
  3172.   00C21 B5              OR      L               ;HL = 8000 = 32768 ?
  3173.                                                 ;(no sign)
  3174.   00C22 2813            JR      Z,0C37H         ;Yes: continue at 0C37H
  3175.                                                 ;No:
  3176.   00C24 EB              EX      DE,HL           ;HL = multiplicant
  3177.   00C25 01C1E1          LD      BC,0E1C1H       ;--
  3178.  
  3179. ; Overflow at IMUL
  3180. ; Convert both factors to SNG format and then perform a SMUL
  3181.  
  3182. * 00C26   C1            POP     BC              ;B = sign-flag
  3183. * 00C27     E1          POP     HL              ;HL = Multiplicant
  3184.   00C28 CDCF0A          CALL    0ACFH           ;X = CSNG (HL)
  3185.   00C2B E1              POP     HL              ;HL = Multiplicator
  3186.   00C2C CDA409          CALL    09A4H           ;(SP) = X
  3187.   00C2F CDCF0A          CALL    0ACFH           ;X = CSNG (HL)
  3188.   00C32 C1              POP     BC              ;BCDE = (SP)
  3189.   00C33 D1              POP     DE
  3190.   00C34 C34708          JP      0847H           ;X = BCDE * X (SNG)
  3191.  
  3192. ; The result is 32768 (unsigned)
  3193.  
  3194.   00C37 78              LD      A,B             ;A = sign-flag
  3195.   00C38 B7              OR      A               ;Signs not equal ?
  3196.   00C39 C1              POP     BC              ;Correct stack
  3197.   00C3A FA9A0A          JP      M,0A9AH         ;Yes: result = 8000H = -32768
  3198.                                                 ;No: result = +32768
  3199.   00C3D D5              PUSH    DE              ;Save DE
  3200.   00C3E CDCF0A          CALL    0ACFH           ;X = CSNG (HL) = -32768
  3201.   00C41 D1              POP     DE              ;Restore DE
  3202.   00C42 C38209          JP      0982H           ;X = -X (result = +32768)
  3203.  
  3204.  
  3205. ; Sign test at IMUL:
  3206. ; Clear sign at DE and HL (make both positive)
  3207. ;
  3208. ; I: DE = multiplicant
  3209. ;    HL = multiplicator
  3210. ; O: DE = ABS (multiplicator)
  3211. ;    HL = ABS (multiplicant)
  3212. ;    If both signs are equal then B, bit 7 = 0
  3213. ;                            else B, bit 7 = 1
  3214.  
  3215.   00C45 7C              LD      A,H             ;Sign from HL
  3216.   00C46 AA              XOR     D               ;XORed with sign from D
  3217.   00C47 47              LD      B,A             ;Result in B, bit 7
  3218.   00C48 CD4C0C          CALL    0C4CH           ;HL = ABS (HL)
  3219.   00C4B EB              EX      DE,HL           ;Same with DE
  3220.   00C4C 7C              LD      A,H             ;HL >= 0 ?
  3221.   00C4D B7              OR      A
  3222.   00C4E F29A0A          JP      P,0A9AH         ;Yes: ok
  3223.  
  3224. ; X = HL = -HL (INT)
  3225. ;
  3226. ; I: HL = INT value
  3227. ; O: HL = negative INT value
  3228. ;    X = HL and VT = INT
  3229.  
  3230.   00C51 AF              XOR     A               ;A = 00H
  3231.   00C52 4F              LD      C,A             ;C = 00H
  3232.   00C53 95              SUB     L
  3233.   00C54 6F              LD      L,A             ;L = 00H - L
  3234.   00C55 79              LD      A,C             ;A = 00H
  3235.   00C56 9C              SBC     A,H
  3236.   00C57 67              LD      H,A             ;H = 00H - H - C-flag
  3237.   00C58 C39A0A          JP      0A9AH           ;Write HL to X as INT
  3238.  
  3239.  
  3240. ; X = -X (INT)
  3241. ; Conversion to SNG, when X = -32768. This because +32768 is no longer
  3242. ; in the INT range.
  3243.  
  3244.   00C5B 2A2141          LD      HL,(4121H)      ;HL = X
  3245.   00C5E CD510C          CALL    0C51H           ;X = HL = -HL
  3246.   00C61 7C              LD      A,H             ;HL used to be -32768 ?
  3247.   00C62 EE80            XOR     80H
  3248.   00C64 B5              OR      L
  3249.   00C65 C0              RET     NZ              ;No : value ok
  3250.  
  3251.   00C66 EB              EX      DE,HL           ;DE = value
  3252.   00C67 CDEF0A          CALL    0AEFH           ;VT = SNG
  3253.   00C6A AF              XOR     A               ;A = 0
  3254.   00C6B 0698            LD      B,98H           ;B = Exp (2 ^ 24)
  3255.   00C6D C36909          JP      0969H           ;SFLOAT (X)
  3256.  
  3257.  
  3258. ; X = X - Y = X + (-Y) (DBL)
  3259.  
  3260.   00C70 212D41          LD      HL,412DH        ;HL -> MSB (Y)
  3261.   00C73 7E              LD      A,(HL)          ;A = MSB (Y)
  3262.   00C74 EE80            XOR     80H             ;Y = -Y
  3263.   00C76 77              LD      (HL),A          ;Store MSB (Y)
  3264.  
  3265. ; X = X + Y (DBL)
  3266.  
  3267.   00C77 212E41          LD      HL,412EH        ;HL -> Exp (Y)
  3268.   00C7A 7E              LD      A,(HL)          ;A = Exp (Y)
  3269.   00C7B B7              OR      A               ;Exp (Y) = 0 ?
  3270.   00C7C C8              RET     Z               ;Yes: X is the result
  3271.  
  3272.   00C7D 47              LD      B,A             ;B = Exp (Y)
  3273.   00C7E 2B              DEC     HL              HL -> MSB (Y)
  3274.   00C7F 4E              LD      C,(HL)          ;C = MSB (Y)
  3275.   00C80 112441          LD      DE,4124H        ;DE -> Exp (X)
  3276.   00C83 1A              LD      A,(DE)          ;A = Exp (X)
  3277.   00C84 B7              OR      A               ;X = 0 ? (Exp (X) = 0)
  3278.   00C85 CAF409          JP      Z,09F4H         ;Yes: X = Y
  3279.  
  3280.   00C88 90              SUB     B               ;A = Exp (X) - Exp (Y)
  3281.                                                 ;Exp (X) >= Exp (Y)
  3282.   00C89 3016            JR      NC,0CA1H        ;Yes: continue at 0CA1H
  3283.                                                 ;No: swap X and Y
  3284.   00C8B 2F              CPL                     ;A = -A
  3285.   00C8C 3C              INC     A               ;A + 1 for 2 complement
  3286.   00C8D F5              PUSH    AF              ;Save Exp-difference
  3287.   00C8E 0E08            LD      C,08H           ;Swap 8 bytes
  3288.   00C90 23              INC     HL              ;HL -> Exp (Y)
  3289.   00C91 E5              PUSH    HL              ;Save pointer
  3290.   00C92 1A              LD      A,(DE)          ;Swap bytes from (DE)
  3291.   00C93 46              LD      B,(HL)          ;and (HL)
  3292.   00C94 77              LD      (HL),A
  3293.   00C95 78              LD      A,B
  3294.   00C96 12              LD      (DE),A
  3295.   00C97 1B              DEC     DE              ;Pointer - 1
  3296.   00C98 2B              DEC     HL              ;Pointer - 1
  3297.   00C99 0D              DEC     C               ;Counter - 1
  3298.   00C9A 20F6            JR      NZ,0C92H        ;Next byte
  3299.  
  3300.   00C9C E1              POP     HL              ;Restore pointer on Exp (X)
  3301.   00C9D 46              LD      B,(HL)          ;B = Exp (X)
  3302.   00C9E 2B              DEC     HL
  3303.   00C9F 4E              LD      C,(HL)          ;C = MSB (X)
  3304.   00CA0 F1              POP     AF              ;Restore Exp-difference
  3305.                                                 ;In X now the larger argument
  3306.   00CA1 FE39            CP      39H             ;Exp-diff. > (2 ^ 56)
  3307.   00CA3 D0              RET     NC              ;Yes: Y is too small, the sum
  3308.                                                 ;would not alter X
  3309.   00CA4 F5              PUSH    AF              ;Save Exp-difference
  3310.   00CA5 CDDF09          CALL    09DFH           ;Adjust mantissas
  3311.   00CA8 23              INC     HL              ;HL -> Exp (X)
  3312.   00CA9 3600            LD      (HL),00H        ;Set Exp to 0
  3313.   00CAB 47              LD      B,A             ;B = sign-flag
  3314.   00CAC F1              POP     AF              ;Restore Exp-difference
  3315.   00CAD 212D41          LD      HL,412DH        ;HL -> Y
  3316.   00CB0 CD690D          CALL    0D69H           ;Shift (HL) upto (HL-7) A bits
  3317.                                                 ;to the right. This results
  3318.                                                 ;in the same exponent for
  3319.                                                 ;X and Y
  3320.   00CB3 3A2641          LD      A,(4126H)       ;Copy underflow
  3321.   00CB6 321C41          LD      (411CH),A       ;to X
  3322.   00CB9 78              LD      A,B             ;A = sign-flag
  3323.   00CBA B7              OR      A               ;Equal signs ?
  3324.   00CBB F2CF0C          JP      P,0CCFH         ;No: continue at 0CCFH
  3325.  
  3326.   00CBE CD330D          CALL    0D33H           ;Add mantissas
  3327.                                                 ;Overflow ?
  3328.   00CC1 D20E0D          JP      NC,0D0EH        ;No: continue at 0D0EH
  3329.  
  3330.   00CC4 EB              EX      DE,HL           ;Overflow: HL -> Exp (X)
  3331.   00CC5 34              INC     (HL)            ;Exp (X) + 1
  3332.                                                 ;Overflow ?
  3333.   00CC6 CAB207          JP      Z,07B2H         ;Yes: ?OV Error
  3334.  
  3335.   00CC9 CD900D          CALL    0D90H           ;X = X / 2, because the Exp + 1
  3336.                                                 ;means an X * 2
  3337.   00CCC C30E0D          JP      0D0EH           ;Continue at 0D0EH
  3338.  
  3339. ; Unequal signs: subtract mantissas
  3340.  
  3341.   00CCF CD450D          CALL    0D45H           ;Subtract mantissas
  3342.   00CD2 212541          LD      HL,4125H        ;HL -> sign-flag
  3343.                                                 ;Onderflow ?
  3344.   00CD5 DC570D          CALL    C,0D57H         ;Yes: negate mantissa of X
  3345.  
  3346.  
  3347. ; DFLOAT (DBL)
  3348. ; Shift DBL-mantissa to  the left until the highest bit of the mantissa
  3349. ; is 1 and the exponent is as small as possible
  3350.  
  3351.   00CD8 AF              XOR     A               ;A = 00H
  3352.   00CD9 47              LD      B,A             ;B = shift-counter
  3353.   00CDA 3A2341          LD      A,(4123H)       ;A = MSB (X)
  3354.   00CDD B7              OR      A               ;MSB = 0 ?
  3355.   00CDE 201E            JR      NZ,0CFEH        ;No: test bits of MSB
  3356.                                                 ;Yes: Shift X by 1 byte to the
  3357.                                                 ;left
  3358.   00CE0 211C41          LD      HL,411CH        ;HL -> underflow (X)
  3359.   00CE3 0E08            LD      C,08H           ;C = counter for 8 bytes
  3360.   00CE5 56              LD      D,(HL)          ;Get new byte
  3361.   00CE6 77              LD      (HL),A          ;Use old byte
  3362.   00CE7 7A              LD      A,D             ;old byte = new byte
  3363.   00CE8 23              INC     HL              ;Pointer + 1
  3364.   00CE9 0D              DEC     C               ;Counter - 1
  3365.   00CEA 20F9            JR      NZ,0CE5H        ;Next byte
  3366.  
  3367.   00CEC 78              LD      A,B             ;A = shift-counter
  3368.   00CED D608            SUB     08H             ;Subtract 8 (for 8 bits)
  3369.   00CEF FEC0            CP      0C0H            ;-64 reached ? (already 8
  3370.                                                 ;bytes shifted ?)
  3371.   00CF1 20E6            JR      NZ,0CD9H        ;No: test MSB again
  3372.  
  3373.   00CF3 C37807          JP      0778H           ;Yes: all bytes of X were 00H
  3374.                                                 ;Set X = 0
  3375. ; Continue shifting bitwise
  3376.  
  3377.   00CF6 05              DEC     B               ;Shift-counter - 1
  3378.   00CF7 211C41          LD      HL,411CH        ;HL -> Underflow (X)
  3379.   00CFA CD970D          CALL    0D97H           ;Shift X by 1 bit to the left
  3380.   00CFD B7              OR      A               ;A = new MSB, bit 7 = 1 ?
  3381.   00CFE F2F60C          JP      P,0CF6H         ;No: shift again
  3382.  
  3383.   00D01 78              LD      A,B             ;A = shift-counter
  3384.   00D02 B7              OR      A               ;Anything shifted ?
  3385.   00D03 2809            JR      Z,0D0EH         ;No: X ready
  3386.  
  3387.   00D05 212441          LD      HL,4124H        ;HL -> Exp (X)
  3388.   00D08 86              ADD     A,(HL)          ;Subtract number of shifted
  3389.   00D09 77              LD      (HL),A          ;bits from exponent
  3390.                                                 ;Overflow ?
  3391.   00D0A D27807          JP      NC,0778H        ;No: X = 0
  3392.  
  3393.   00D0D C8              RET     Z               ;Done when exponent = 0
  3394.  
  3395.   00D0E 3A1C41          LD      A,(411CH)       ;A = underflow byte
  3396.   00D11 B7              OR      A               ;A, bit 7 = 1
  3397.   00D12 FC200D          CALL    M,0D20H         ;Yes: round up X
  3398.  
  3399.   00D15 212541          LD      HL,4125H        ;HL -> sign-flag
  3400.   00D18 7E              LD      A,(HL)          ;A = sign-flag
  3401.   00D19 E680            AND     80H             ;Mask sign. A, bit 7 = 1 when
  3402.                                                 ;equal sign
  3403.   00D1B 2B              DEC     HL
  3404.   00D1C 2B              DEC     HL              ;HL -> MSB (X)
  3405.   00D1D AE              XOR     (HL)            ;XOR with MSB (X)
  3406.   00D1E 77              LD      (HL),A          ;Save new MSB incl. sign
  3407.   00D1F C9              RET
  3408.  
  3409.  
  3410. ; Round up X (DBL)
  3411.  
  3412.   00D20 211D41          LD      HL,411DH        ;HL -> LSB of X (DBL)
  3413.   00D23 0607            LD      B,07H           ;B = mantissa size of DBL
  3414.   00D25 34              INC     (HL)            ;Increment LSB
  3415.                                                 ;Overflow ?
  3416.   00D26 C0              RET     NZ              ;No: return
  3417.  
  3418.   00D27 23              INC     HL              ;Next byte
  3419.   00D28 05              DEC     B               ;Counter - 1
  3420.                                                 ;All mantissa bytes done ?
  3421.   00D29 20FA            JR      NZ,0D25H        ;No: increment next mantissa
  3422.                                                 ;byte
  3423.   00D2B 34              INC     (HL)            ;Increment Exp (X)
  3424.                                                 ;Overflow ?
  3425.   00D2C CAB207          JP      Z,07B2H         ;Yes: ?OV Error
  3426.   00D2F 2B              DEC     HL              ;HL -> MSB mantissa
  3427.   00D30 3680            LD      (HL),80H        ;Set mantissa to negative
  3428.   00D32 C9              RET
  3429.  
  3430.  
  3431. ; Add mantissas of X and Y. Result in X
  3432.  
  3433.   00D33 212741          LD      HL,4127H        ;HL -> LSB of Y (DBL)
  3434.   00D36 111D41          LD      DE,411DH        ;DE -> LSB of X (DBL)
  3435.   00D39 0E07            LD      C,07H           ;C = mantissa size of DBL
  3436.   00D3B AF              XOR     A               ;A = 0
  3437.   00D3C 1A              LD      A,(DE)          ;A = mantissa byte X
  3438.   00D3D 8E              ADC     A,(HL)          ;A = A + mantissa byte Y
  3439.                                                 ;      + C-flag
  3440.   00D3E 12              LD      (DE),A          ;Store result in X
  3441.   00D3F 13              INC     DE              ;Pointer to X + 1
  3442.   00D40 23              INC     HL              ;Pointer to Y + 1
  3443.   00D41 0D              DEC     C               ;Counter - 1
  3444.                                                 ;All mantissa bytes done ?
  3445.   00D42 20F8            JR      NZ,0D3CH        ;No, add next mantissa bytes
  3446.  
  3447.   00D44 C9              RET
  3448.  
  3449.  
  3450. ; Subtract mantissas of X and Y. Result in X
  3451.  
  3452.   00D45 212741          LD      HL,4127H        ;HL -> LSB of Y (DBL)
  3453.   00D48 111D41          LD      DE,411DH        ;DE -> LSB of X (DBL)
  3454.   00D4B 0E07            LD      C,07H           ;C = mantissa size of DBL
  3455.   00D4D AF              XOR     A               ;A = 0
  3456.   00D4E 1A              LD      A,(DE)          ;A = mantissa byte X
  3457.   00D4F 9E              SBC     A,(HL)          ;A = A - mantissa byte Y
  3458.                                                 ;      - C-flag
  3459.   00D50 12              LD      (DE),A          ;Store result in X
  3460.   00D51 13              INC     DE              ;Pointer to X + 1
  3461.   00D52 23              INC     HL              ;Pointer to Y + 1
  3462.   00D53 0D              DEC     C               ;Counter - 1
  3463.                                                 ;All mantissa bytes done ?
  3464.   00D54 20F8            JR      NZ,0D4EH        ;No, add next mantissa bytes
  3465.  
  3466.   00D56 C9              RET
  3467.  
  3468.  
  3469. ; Negate mantissa of X (incl. underflow) and sign
  3470.  
  3471.   00D57 7E              LD      A,(HL)          ;A = sign-flag
  3472.   00D58 2F              CPL                     ;Negation
  3473.   00D59 77              LD      (HL),A          ;Save sign-flag
  3474.   00D5A 211C41          LD      HL,411CH        ;HL -> underflow (X)
  3475.   00D5D 0608            LD      B,08H           ;Negate 9 bytes
  3476.   00D5F AF              XOR     A               ;A = 00H
  3477.   00D60 4F              LD      C,A             ;C = 00H
  3478.  
  3479.   00D61 79              LD      A,C             ;A = C
  3480.   00D62 9E              SBC     A,(HL)          ;A = A - (HL) - C-flag
  3481.   00D63 77              LD      (HL),A          ;Save difference
  3482.   00D64 23              INC     HL              ;Pointer + 1
  3483.   00D65 05              DEC     B               ;Counter - 1
  3484.   00D66 20F9            JR      NZ,0D61H        ;Next byte
  3485.  
  3486.   00D68 C9              RET
  3487.  
  3488.  
  3489. ; Shift (HL) upto (HL-7) by A bits to the right
  3490.  
  3491.   00D69 71              LD      (HL),C          ;Save MSB
  3492.   00D6A E5              PUSH    HL              ;Save pointer
  3493.   00D6B D608            SUB     08H             ;More then 8 bits to shift ?
  3494.   00D6D 380E            JR      C,0D7DH         ;No: shift bits at 0D7DH
  3495.  
  3496.   00D6F E1              POP     HL              ;Restore pointer
  3497.   00D70 E5              PUSH    HL              ;And save it again
  3498.   00D71 110008          LD      DE,0800H        ;Fill 8 bytes mantissa with 00H
  3499.   00D74 4E              LD      C,(HL)          ;C = new byte
  3500.   00D75 73              LD      (HL),E          ;(HL) = old byte
  3501.   00D76 59              LD      E,C             ;Old byte = new byte
  3502.   00D77 2B              DEC     HL              ;Pointer - 1
  3503.   00D78 15              DEC     D               ;Counter - 1
  3504.   00D79 20F9            JR      NZ,0D74H        ;Shift next byte
  3505.  
  3506.   00D7B 18EE            JR      0D6BH           ;Shift anything more ?
  3507.  
  3508.  
  3509. ; Shift bitwise
  3510.  
  3511.   00D7D C609            ADD     A,09H           ;Reverse the SUB 08H
  3512.   00D7F 57              LD      D,A             ;D = counter
  3513.   00D80 AF              XOR     A               ;A = 00H
  3514.   00D81 E1              POP     HL              ;Restore pointer
  3515.   00D82 15              DEC     D               ;Counter - 1
  3516.   00D83 C8              RET     Z               ;RET when done
  3517.  
  3518.   00D84 E5              PUSH    HL              ;Save pointer
  3519.   00D85 1E08            LD      E,08H           ;8 bytes mantissa
  3520.                                                 ;(with underflow)
  3521.   00D87 7E              LD      A,(HL)          ;A = byte
  3522.   00D88 1F              RRA                     ;shift right (incl. C-flag)
  3523.   00D89 77              LD      (HL),A          ;Save byte
  3524.   00D8A 2B              DEC     HL              ;Pointer - 1
  3525.   00D8B 1D              DEC     E               ;Counter - 1
  3526.   00D8C 20F9            JR      NZ,0D87H        ;Next byte
  3527.  
  3528.   00D8E 18F0            JR      0D80H           ;Any more to shift ?
  3529.  
  3530. ; Shift mantissa of X one bit to the right
  3531.  
  3532.   00D90 212341          LD      HL,4123H        ;HL -> mantissa
  3533.   00D93 1601            LD      D,01H           ;D = counter for 1 bit
  3534.   00D95 18ED            JR      0D84H           ;Continue at 0D84H
  3535.  
  3536. ; Shift (HL) to (HL+7) one bit to the right
  3537.  
  3538.   00D97 0E08            LD      C,08H           ;8 bits Mantissa (with
  3539.                                                 ;underflow)
  3540.   00D99 7E              LD      A,(HL)          ;A = byte
  3541.   00D9A 17              RLA                     ;shift incl. carry
  3542.   00D9B 77              LD      (HL),A          ;Store byte
  3543.   00D9C 23              INC     HL              ;Pointer + 1
  3544.   00D9D 0D              DEC     C               ;Counter - 1
  3545.   00D9E 20F9            JR      NZ,0D99H        ;Shift next byte
  3546.  
  3547.   00DA0 C9              RET
  3548.  
  3549.  
  3550. ; DMUL: X = X * Y (DBL)
  3551. ;
  3552. ; I: X = 1st factor (DBL)
  3553. ;    Y = 2nd factor (DBL)
  3554. ; O: X = product (DBL)
  3555.  
  3556.   00DA1 CD5509          CALL    0955H           ;TEST2
  3557.                                                 ;X = 0 ?
  3558.   00DA4 C8              RET     Z               ;Yes: return
  3559.  
  3560.   00DA5 CD0A09          CALL    090AH           ;process exponent and sign
  3561.   00DA8 CD390E          CALL    0E39H           ;Save mantissa of 1st factor in
  3562.                                                 ;414AH and clear mantissa of X
  3563.   00DAB 71              LD      (HL),C          ;Underflow of X = 0
  3564.   00DAC 13              INC     DE              ;DE -> LSB of 1st factor
  3565.   00DAD 0607            LD      B,07H           ;Process 7 bytes mantissa
  3566.   00DAF 1A              LD      A,(DE)          ;A = byte from mantissa
  3567.   00DB0 13              INC     DE              ;Pointer + 1
  3568.   00DB1 B7              OR      A               ;No bit set ?
  3569.   00DB2 D5              PUSH    DE              ;Save pointer
  3570.   00DB3 2817            JR      Z,0DCCH         ;Yes: shift X 1 byte to the
  3571.                                                 ;right and get next byte from
  3572.                                                 ;1st factor
  3573.   00DB5 0E08            LD      C,08H           ;8 bits per byte
  3574.   00DB7 C5              PUSH    BC              ;Save counter
  3575.   00DB8 1F              RRA                     ;Test next bit
  3576.                                                 ;Bit = 1 ?
  3577.   00DB9 47              LD      B,A             ;Save byte first
  3578.   00DBA DC330D          CALL    C,0D33H         ;Yes: add mantissa of X and Y
  3579.   00DBD CD900D          CALL    0D90H           ;Shift mantissa of X one bit
  3580.                                                 ;to the right (next position)
  3581.   00DC0 78              LD      A,B             ;Restore byte
  3582.   00DC1 C1              POP     BC              ;Restore counter
  3583.   00DC2 0D              DEC     C               ;Bit counter - 1
  3584.   00DC3 20F2            JR      NZ,0DB7H        ;Test next bit
  3585.  
  3586.   00DC5 D1              POP     DE              ;Restore pointer
  3587.   00DC6 05              DEC     B               ;Byte counter - 1
  3588.   00DC7 20E6            JR      NZ,0DAFH        ;Test next byte
  3589.  
  3590.   00DC9 C3D80C          JP      0CD8H           ;Continue at DFLOAT
  3591.  
  3592. ; Shift mantissa of X one byte to the right
  3593.  
  3594.   00DCC 212341          LD      HL,4123H        ;HL -> mantissa of X
  3595.   00DCF CD700D          CALL    0D70H           ;Shift one byte to the right
  3596.   00DD2 18F1            JR      0DC5H           ;Back to DMUL
  3597.  
  3598.  
  3599. ; Constant 10 (DBL, SNG)
  3600.  
  3601.   00DD4 00              DEFB    00H             ;Constant 10 (DBL)
  3602.   00DD5 00              DEFB    00H
  3603.   00DD6 00              DEFB    00H
  3604.   00DD7 00              DEFB    00H
  3605.   00DD8 00              DEFB    00H             ;Constant 10 (SNG)
  3606.   00DD9 00              DEFB    00H
  3607.   00DDA 20              DEFB    20H
  3608.   00DDB 84              DEFB    84H
  3609.  
  3610.  
  3611. ; X = X / 10 (DBL)
  3612.  
  3613.   00DDC 11D40D          LD      DE,0DD4H        ;DE -> Constant 10 (DBL)
  3614.   00DDF 212741          LD      HL,4127H        ;HL -> Y
  3615.   00DE2 CDD309          CALL    09D3H           ;Copy (DE) to (HL): Y = 10
  3616.  
  3617.  
  3618. ; DDIV: X = X / Y (DBL)
  3619. ;
  3620. ; I: X = dividend (DBL)
  3621. ;    Y = divisor (DBL)
  3622. ; O: X = quotient
  3623.  
  3624.   00DE5 3A2E41          LD      A,(412EH)       ;A = Exp (X)
  3625.   00DE8 B7              OR      A               ;zero ?
  3626.   00DE9 CA9A19          JP      Z,199AH         ;Yes: ?/0 Error
  3627.  
  3628.   00DEC CD0709          CALL    0907H           ;Process exponent and sign
  3629.   00DEF 34              INC     (HL)            ;Adjust exponent
  3630.   00DF0 34              INC     (HL)            ;(see also SDIV)
  3631.   00DF1 CD390E          CALL    0E39H           ;Save dividend and set mantissa
  3632.                                                 ;of X to 0
  3633.   00DF4 215141          LD      HL,4151H        ;HL -> underflow of dividend
  3634.   00DF7 71              LD      (HL),C          ;Clear underflow byte
  3635.   00DF8 41              LD      B,C             ;Unterflow flag = 0
  3636.   00DF9 114A41          LD      DE,414AH        ;DE -> LSB of dividend
  3637.   00DFC 212741          LD      HL,4127H        ;HL -> LSB of divisor
  3638.   00DFF CD4B0D          CALL    0D4BH           ;Subtract mantissas
  3639.   00E02 1A              LD      A,(DE)          ;A = undeflow byte
  3640.   00E03 99              SBC     A,C             ;A = A - C-flag (C-flag = 0)
  3641.   00E04 3F              CCF                     ;Invert C-flag
  3642.                                                 ;Underflow ?
  3643.   00E05 380B            JR      C,0E12H         ;No: continue at 0E12H
  3644.                                                 ;Yes: reverse last subtract
  3645.   00E07 114A41          LD      DE,414AH        ;DE -> LSB of dividend
  3646.   00E0A 212741          LD      HL,4127H        ;HL -> LSB of divisor
  3647.   00E0D CD390D          CALL    0D39H           ;Add mantissas
  3648.   00E10 AF              XOR     A               ;C-flag = 0
  3649.   00E11 DA1204          JP      C,0412H         ;--
  3650. * 00E12   12            LD      (DE),A          ;No underflow: write back
  3651.                                                 ;undeflow byte
  3652. * 00E13     04          INC     B               ;Underflow flag = 1
  3653.  
  3654.   00E14 3A2341          LD      A,(4123H)       ;A = MSB of quotient
  3655.   00E17 3C              INC     A
  3656.   00E18 3D              DEC     A
  3657.   00E19 1F              RRA                     ;Shift bit 7 of A to C-flag
  3658.                                                 ;for rounding purposes
  3659.   00E1A FA110D          JP      M,0D11H         ;Done when highest bit = 1
  3660.  
  3661.   00E1D 17              RLA                     ;Shift back bit 7 of A
  3662.   00E1E 211D41          LD      HL,411DH        ;HL -> LSB of quotient
  3663.   00E21 0E07            LD      C,07H           ;7 bytes mantissa
  3664.   00E23 CD990D          CALL    0D99H           ;Shift quotient one bit to the
  3665.                                                 ;left
  3666.   00E26 214A41          LD      HL,414AH        ;HL -> LSB of dividend
  3667.   00E29 CD970D          CALL    0D97H           ;Shift dividend one bit to the
  3668.                                                 ;left
  3669.   00E2C 78              LD      A,B             ;A = underflow byte
  3670.   00E2D B7              OR      A               ;Underflow occured ?
  3671.   00E2E 20C9            JR      NZ,0DF9H        ;No: process next bit
  3672.                                                 ;Yes:
  3673.   00E30 212441          LD      HL,4124H        ;HL -> Exp (quotient)
  3674.   00E33 35              DEC     (HL)            ;Quotient = Quotient / 2
  3675.                                                 ;Exp = 0 ?
  3676.   00E34 20C3            JR      NZ,0DF9H        ;No: process next bit
  3677.                                                 ;Yes:
  3678.   00E36 C3B207          JP      07B2H           ;?OV Error
  3679.  
  3680.  
  3681. ; SUB for DDIV
  3682. ; Save mantissa of X to 414AH to 4150H and set as result X to zero
  3683.  
  3684.   00E39 79              LD      A,C             ;A = MSB (Y)
  3685.   00E3A 322D41          LD      (412DH),A       ;Write back MSB (Y)
  3686.   00E3D 2B              DEC     HL              ;HL -> MSB (X)
  3687.   00E3E 115041          LD      DE,4150H        ;DE -> temp. memory space
  3688.   00E41 010007          LD      BC,0700H        ;Copy 7 bytes, set to 00H
  3689.   00E44 7E              LD      A,(HL)          ;A = byte from X
  3690.   00E45 12              LD      (DE),A          ;Save it in (DE)
  3691.   00E46 71              LD      (HL),C          ;Clear mantissa of X
  3692.   00E47 1B              DEC     DE              ;Pointer to temp. - 1
  3693.   00E48 2B              DEC     HL              ;Pointer to X - 1
  3694.   00E49 05              DEC     B               ;Counter - 1
  3695.   00E4A 20F8            JR      NZ,0E44H        ;Next byte
  3696.  
  3697.   00E4C C9              RET
  3698.  
  3699.  
  3700. ; X = X * 10 (DBL)
  3701.  
  3702.   00E4D CDFC09          CALL    09FCH           ;Y = X
  3703.   00E50 EB              EX      DE,HL           ;HL + 1 -> Exp (X)
  3704.   00E51 2B              DEC     HL              ;HL -> Exp (X)
  3705.   00E52 7E              LD      A,(HL)          ;A = Exp (X)
  3706.   00E53 B7              OR      A               ;X = 0 ?
  3707.   00E54 C8              RET     Z               ;Yes: done, return
  3708.  
  3709.   00E55 C602            ADD     A,02H           ;A = Exp(X) + 2
  3710.                                                 ;Overflow ?
  3711.   00E57 DAB207          JP      C,07B2H         ;Yes: ?OV Error
  3712.  
  3713.   00E5A 77              LD      (HL),A          ;Store Exp: X = X * 4
  3714.   00E5B E5              PUSH    HL              ;Save pointer
  3715.   00E5C CD770C          CALL    0C77H           ;X = X + Y (now: X = X * 5)
  3716.   00E5F E1              POP     HL              ;Restore pointer
  3717.   00E60 34              INC     (HL)            ;Exp + 1: X = X * 2
  3718.                                                 ;(results in X = X * 10)
  3719.   00E61 C0              RET     NZ              ;Return if no overflow
  3720.  
  3721.   00E62 C3B207          JP      07B2H           ;?OV Error
  3722.  
  3723.  
  3724. ; Convert string to number (DBL)
  3725. ; (like VAL function)
  3726. ;
  3727. ; I: HL -> string
  3728. ; O: X = number (DBL)
  3729.  
  3730.   00E65 CD7807          CALL    0778H           ;X = 0
  3731.   00E68 CDEC0A          CALL    0AECH           ;set VT to DBL
  3732.   00E6B F6AF            OR      0AFH            ;Set flag <> 0
  3733.  
  3734.  
  3735. ; Convert string in a number according to type (INT, SNG, DBL)
  3736. ; (like VAL function)
  3737. ;
  3738. ; I: HL -> string
  3739. ; O: X = number
  3740.  
  3741. * 00E6C   AF            XOR     A               ;Set flag = 0
  3742.                                                 ;First, the routine will
  3743.                                                 ;attempt to convert to an INT.
  3744.                                                 ;In case of overflow, it will
  3745.                                                 ;attempt to convert to SNG or
  3746.                                                 ;to DBL
  3747.   00E6D EB              EX      DE,HL           ;DE -> string
  3748.   00E6E 01FF00          LD      BC,00FFH        ;B = 00 (number of positions
  3749.                                                 ;after the decimal point)
  3750.                                                 ;C = FFH (floating point flag:
  3751.                                                 ;see 0EE4H and 0F29H)
  3752.   00E71 60              LD      H,B             ;HL = 0000H
  3753.   00E72 68              LD      L,B             ;(HL is initial value)
  3754.   00E73 CC9A0A          CALL    Z,0A9AH         ;Write HL to X as INT
  3755.   00E76 EB              EX      DE,HL           ;HL = pointer, DE = 0000H
  3756.   00E77 7E              LD      A,(HL)          ;A = string character
  3757.   00E78 FE2D            CP      '-'             ;Test for negative sign
  3758.                                                 ;(Negative: Z-flag = 1)
  3759.   00E7A F5              PUSH    AF              ;Save sign
  3760.                                                 ;Negative sign ?
  3761.   00E7B CA830E          JP      Z,0E83H         ;Yes: continue at 0E83H
  3762.  
  3763.   00E7E FE2B            CP      '+'             ;Positive sign ?
  3764.   00E80 2801            JR      Z,0E83H         ;Yes: continue at 0E83H
  3765.  
  3766.   00E82 2B              DEC     HL              ;Pointer - 1 for RST 10H
  3767.   00E83 D7              RST     10H             ;A = next non-space character
  3768.                                                 ;Digit ?
  3769.   00E84 DA290F          JP      C,0F29H         ;Yes: continue at 0F29H
  3770.  
  3771.   00E87 FE2E            CP      '.'             ;'.' ?
  3772.   00E89 CAE40E          JP      Z,0EE4H          Yes: continue at 0EE4H
  3773.  
  3774.   00E8C FE45            CP      'E'             ;'E' ?
  3775.   00E8E 2814            JR      Z,0EA4H         ;Yes: continue at 0EA4H
  3776.  
  3777.   00E90 FE25            CP      '%'             ;'%' ? (INT indicator)
  3778.   00E92 CAEE0E          JP      Z,0EEEH         ;Yes: continue at 0EEEH
  3779.  
  3780.   00E95 FE23            CP      '#'             ;'#' ? (DBL indicator)
  3781.   00E97 CAF50E          JP      Z,0EF5H         ;Yes: continue at 0EF5H
  3782.  
  3783.   00E9A FE21            CP      '!'             ;'!' ? (SNG indicator)
  3784.   00E9C CAF60E          JP      Z,0EF6H         ;Yes: continue at 0EF6H
  3785.  
  3786.   00E9F FE44            CP      'D'             ;'D' ?
  3787.   00EA1 2024            JR      NZ,0EC7H        ;No: No digit or special
  3788.                                                 ;character recognized:
  3789.                                                 ;end of number string reached
  3790.   00EA3 B7              OR      A               ;Yes: Set Z-flag = 0
  3791.  
  3792. ; 'E' (Z-flag = 1) or 'D' (Z-flag = 0)
  3793.  
  3794.   00EA4 CDFB0E          CALL    0EFBH           ;Convert X to SNG (Z-flag = 1)
  3795.                                                 ;or DBL (Z-flag = 0)
  3796.   00EA7 E5              PUSH    HL              ;Save pointer
  3797.   00EA8 21BD0E          LD      HL,0EBDH        ;Set new RET address to 0EBDH
  3798.   00EAB E3              EX      (SP),HL         ;and restore pointer
  3799.   00EAC D7              RST     10H             ;A = next character following
  3800.                                                 ;'D' or 'E'
  3801.   00EAD 15              DEC     D               ;D = FFH (-1)
  3802.   00EAE FECE            CP      0CEH            ;'-' (BASIC token) ?
  3803.   00EB0 C8              RET     Z               ;Yes: continue at 0EBDH
  3804.  
  3805.   00EB1 FE2D            CP      '-'             ;'-' ?
  3806.   00EB3 C8              RET     Z               ;Yes: continue at 0EBDH
  3807.  
  3808.   00EB4 14              INC     D               ;D = 00H (0)
  3809.   00EB5 FECD            CP      0CDH            ;'+' (BASIC token) ?
  3810.   00EB7 C8              RET     Z               ;Yes: continue at 0EBDH
  3811.  
  3812.   00EB8 FE2B            CP      '+'             ;'+' ?
  3813.   00EBA C8              RET     Z               ;Yes: continue at 0EBDH
  3814.  
  3815.   00EBB 2B              DEC     HL              ;Pointer - 1 (for RST 10H)
  3816.   00EBC F1              POP     AF              ;Remove RET address (0EBDH)
  3817.   00EBD D7              RST     10H             ;A = Exponent character
  3818.                                                 ;Digit found ?
  3819.   00EBE DA940F          JP      C,0F94H         ;yes: continue at 0F94H
  3820.                                                 ;No: exponent finished
  3821.   00EC1 14              INC     D               ;Exponent negative ? (D was FFH)
  3822.   00EC2 2003            JR      NZ,0EC7H        ;No: continue at 0EC7H
  3823.                                                 ;Yes:
  3824.   00EC4 AF              XOR     A               ;Negate exponent
  3825.   00EC5 93              SUB     E               ;A = 00H - E
  3826.   00EC6 5F              LD      E,A             ;E = correct exponent
  3827.  
  3828. ; Number in X is ready: process exponent / floating point / sign
  3829.  
  3830.   00EC7 E5              PUSH    HL              ;Save pointer
  3831.   00EC8 7B              LD      A,E             ;A = exponent
  3832.   00EC9 90              SUB     B               ;A = difference between
  3833.                                                 ;exponent and number of
  3834.                                                 ;positions after the decimal
  3835.                                                 ;point.
  3836.                                                 ;Larger than exponent ?
  3837.   00ECA F40A0F          CALL    P,0F0AH         ;Yes: multiply X with 10
  3838.   00ECD FC180F          CALL    M,0F18H         ;No: divide X by 10 and
  3839.                                                 ;difference + 1
  3840.                                                 ;Difference = 0 ?
  3841.   00ED0 20F8            JR      NZ,0ECAH        ;No: continue processing
  3842.  
  3843.   00ED2 E1              POP     HL              ;Restore pointer
  3844.   00ED3 F1              POP     AF              ;Restore sign
  3845.   00ED4 E5              PUSH    HL              ;Save pointer
  3846.                                                 ;Negative sign ?
  3847.   00ED5 CC7B09          CALL    Z,097BH         ;Yes: X = -X
  3848.   00ED8 E1              POP     HL              ;Restore pointer
  3849.  
  3850.   00ED9 E7              RST     20H             ;TSTTYP
  3851.                                                 ;DBL type ?
  3852.   00EDA E8              RET     PE              ;Yes : return
  3853.  
  3854.   00EDB E5              PUSH    HL              ;Save pointer
  3855.   00EDC 219008          LD      HL,0890H        ;Set RET address to POP HL
  3856.   00EDF E5              PUSH    HL              ;(to restore pointer)
  3857.   00EE0 CDA30A          CALL    0AA3H           ;X = -32768 ?
  3858.                                                 ;Yes: convert X to INT
  3859.   00EE3 C9              RET                     ;RET and restore pointer
  3860.  
  3861. ; '.' found
  3862.  
  3863.   00EE4 E7              RST     20H             ;TSTTYP
  3864.   00EE5 0C              INC     C               ;If point found for the first
  3865.                                                 ;time then C = 00H else C > 00H
  3866.                                                 ;Number done (C > 0) ?
  3867.   00EE6 20DF            JR      NZ,0EC7H        ;Yes: continue at 0EC7H
  3868.                                                 ;X in INT format ?
  3869.   00EE8 DCFB0E          CALL    C,0EFBH         ;Yes: convert to SNG
  3870.   00EEB C3830E          JP      0E83H           ;Get next character
  3871.  
  3872. ; '%' (INT indicator) found
  3873.  
  3874.   00EEE E7              RST     20H             ;TSTTYP
  3875.                                                 ;INT type ?
  3876.   00EEF F29719          JP      P,1997H         ;No: ?SN Error
  3877.  
  3878.   00EF2 23              INC     HL              ;Pointer + 1
  3879.   00EF3 18D2            JR      0EC7H           ;Number is ready
  3880.  
  3881. ; '#' (DBL indicator) found
  3882.  
  3883.   00EF5 B7              OR      A               ;Z-flag = 0
  3884.  
  3885. ; '!' (SNG indicator) found
  3886.  
  3887.   00EF6 CDFB0E          CALL    0EFBH           ;Convert number to SNG
  3888.                                                 ;(Z-flag=1) or DBL (Z-flag=0)
  3889.   00EF9 18F7            JR      0EF2H           ;Pointer + 1, number is ready
  3890.  
  3891.  
  3892. ; Conversion into SNG or DBL
  3893. ;
  3894. ; I: Z-flag = 1: X = CSNG ( X )
  3895. ;    Z-flag = 0: X = CDBL ( X )
  3896. ; O: -
  3897.  
  3898.   00EFB E5              PUSH    HL              ;Save registers
  3899.   00EFC D5              PUSH    DE
  3900.   00EFD C5              PUSH    BC
  3901.   00EFE F5              PUSH    AF
  3902.                                                 ;Convert to SNG ?
  3903.   00EFF CCB10A          CALL    Z,0AB1H         ;Yes: CSNG
  3904.  
  3905.   00F02 F1              POP     AF              ;Restore flags
  3906.                                                 ;Convert to DBL ?
  3907.   00F03 C4DB0A          CALL    NZ,0ADBH        ;Yes: CDBL
  3908.  
  3909.   00F06 C1              POP     BC              ;Restore registers
  3910.   00F07 D1              POP     DE
  3911.   00F08 E1              POP     HL
  3912.   00F09 C9              RET
  3913.  
  3914.  
  3915. ; X = X * 10 (SNG,DBL)
  3916. ; Type conform multiplication with 10 of X
  3917. ; Used at processing of the exponent and fractional part
  3918. ;
  3919. ; I: X = number (SNG or DBL format)
  3920. ; O: X = X * 10
  3921. ;    A = A - 1 (for exponent and fractional part processing)
  3922.  
  3923.                                                 ;Difference between exponent
  3924.                                                 ;and fractional part = 0 ?
  3925.                                                 ;(Z-flag = 1)
  3926.   00F0A C8              RET     Z               ;Yes: done, return
  3927.  
  3928.   00F0B F5              PUSH    AF              ;Save difference
  3929.   00F0C E7              RST     20H             ;TSTTYP
  3930.   00F0D F5              PUSH    AF              ;Save flags
  3931.                                                 ;SNG type ?
  3932.   00F0E E43E09          CALL    PO,093EH        ;Yes: X = X * 10 (SNG)
  3933.   00F11 F1              POP     AF              ;Restore flags
  3934.                                                 ;DBL type ?
  3935.   00F12 EC4D0E          CALL    PE,0E4DH        ;Yes: X = X * 10 (DBL)
  3936.   00F15 F1              POP     AF              ;Restore difference
  3937.   00F16 3D              DEC     A               ;Difference - 1
  3938.   00F17 C9              RET
  3939.  
  3940.  
  3941. ; X = X / 10 (SNG,DBL)
  3942. ; Type conform division by 10 of X
  3943. ;
  3944. ; I: X = number (SNG or DBL format)
  3945. ; O: X = X / 10
  3946. ;    A = A + 1 (for exponent and fractional part processing)
  3947.  
  3948.   00F18 D5              PUSH    DE              ;Save registers
  3949.   00F19 E5              PUSH    HL
  3950.   00F1A F5              PUSH    AF
  3951.   00F1B E7              RST     20H             ;TSTTYP
  3952.   00F1C F5              PUSH    AF              ;Save flags
  3953.                                                 ;SNG type ?
  3954.   00F1D E49708          CALL    PO,0897H        ;Yes: X = X / 10 (SNG)
  3955.   00F20 F1              POP     AF              ;Restore flags
  3956.                                                 ;DBL type ?
  3957.   00F21 ECDC0D          CALL    PE,0DDCH        ;Yes: X = X / 10 (DBL)
  3958.   00F24 F1              POP     AF              ;Restore registers
  3959.   00F25 E1              POP     HL
  3960.   00F26 D1              POP     DE
  3961.   00F27 3C              INC     A               ;Difference + 1
  3962.   00F28 C9              RET
  3963.  
  3964.  
  3965. ; Process digit (C-flag = 1 because of previous RST 10H)
  3966.  
  3967.   00F29 D5              PUSH    DE              ;Save exponent flags
  3968.   00F2A 78              LD      A,B             ;A = number of fractional
  3969.                                                 ;positions
  3970.   00F2B 89              ADC     A,C             ;C = FFH
  3971.                                                 ;If no fractional part
  3972.                                                 ;recognized then
  3973.                                                 ;C = FFH + C-flag (1), so A
  3974.                                                 ;remains the same else
  3975.                                                 ;C = 00H + C-flag (1) = 1, so
  3976.                                                 ;the number of positions behind
  3977.                                                 ;the decimal pointer is
  3978.                                                 ;incremented by one
  3979.   00F2C 47              LD      B,A             ;Result in B
  3980.   00F2D C5              PUSH    BC              ;Save BC
  3981.   00F2E E5              PUSH    HL              ;Save pointer
  3982.   00F2F 7E              LD      A,(HL)          ;A = digit (ASCII: '0' to '9')
  3983.   00F30 D630            SUB     30H             ;A = value (numerical: 0 to 9)
  3984.   00F32 F5              PUSH    AF              ;Save digit
  3985.   00F33 E7              RST     20H             ;TSTTYP
  3986.                                                 ;X still INT type ?
  3987.   00F34 F25D0F          JP      P,0F5DH         ;No: continue at 0F5DH
  3988.  
  3989. ; Insert new digit into INT number
  3990.  
  3991.   00F37 2A2141          LD      HL,(4121H)      ;HL -> X (INT number)
  3992.   00F3A 11CD0C          LD      DE,0CCDH        ;DE = 3277 (approx. 32767/10)
  3993.   00F3D DF              RST     18H             ;Current number already larger
  3994.                                                 ;than 3277 ?
  3995.   00F3E 3019            JR      NC,0F59H        ;Yes, inserting the new digit
  3996.                                                 ;will cause the number to
  3997.                                                 ;go outside of the INT range:
  3998.                                                 ;the number has to be
  3999.                                                 ;converted to SNG
  4000.   00F40 54              LD      D,H             ;DE = number
  4001.   00F41 5D              LD      E,L
  4002.   00F42 29              ADD     HL,HL           ;HL = HL * 2  = number *  2
  4003.   00F43 29              ADD     HL,HL           ;HL = HL * 2  = number *  4
  4004.   00F44 19              ADD     HL,DE           ;HL = HL + DE = number *  5
  4005.   00F45 29              ADD     HL,HL           ;HL = HL * 2  = number * 10
  4006.   00F46 F1              POP     AF              ;Restore digit value
  4007.   00F47 4F              LD      C,A             ;BC = digit value
  4008.                                                 ;(B = fractional part = 0,
  4009.                                                 ;because the number is INT)
  4010.   00F48 09              ADD     HL,BC           ;HL = number + new digit value
  4011.   00F49 7C              LD      A,H             ;New number > 32767
  4012.   00F4A B7              OR      A
  4013.   00F4B FA570F          JP      M,0F57H         ;Yes: convert number to SNG
  4014.  
  4015.   00F4E 222141          LD      (4121H),HL      ;X = HL = new number
  4016.   00F51 E1              POP     HL              ;Restore pointer
  4017.   00F52 C1              POP     BC              ;Restore decimal point
  4018.   00F53 D1              POP     DE              ;Restore exponent flag
  4019.   00F54 C3830E          JP      0E83H           ;Get next digit
  4020.  
  4021. ; Overflow of INT
  4022.  
  4023.   00F57 79              LD      A,C             ;A = digit value
  4024.   00F58 F5              PUSH    AF              ;Save it
  4025.   00F59 CDCC0A          CALL    0ACCH           ;X = CSNG ( X )
  4026.   00F5C 37              SCF                     ;Set C-flag to 1 for indicate
  4027.                                                 ;SNG processing
  4028.  
  4029. ; Insert new digit into SNG (C-flag = 1) or DBL (c-flag = 0) number
  4030.  
  4031.                                                 ;DBL ?
  4032.   00F5D 3018            JR      NC,0F77H        ;Yes: continue at 0F77H
  4033.  
  4034.   00F5F 017494          LD      BC,9474H        ;BCDE = 1E+6
  4035.   00F62 110024          LD      DE,2400H
  4036.   00F65 CD0C0A          CALL    0A0CH           ;Compare X and BCDE
  4037.                                                 ;X already >= 1E+6 ?
  4038.   00F68 F2740F          JP      P,0F74H         ;Yes: convert X to DBL, because
  4039.                                                 ;the new number X gets more
  4040.                                                 ;than 6 positions and will go
  4041.                                                 ;outide the SNG range
  4042.   00F6B CD3E09          CALL    093EH           ;X = X * 10
  4043.   00F6E F1              POP     AF              ;A = digit value
  4044.   00F6F CD890F          CALL    0F89H           ;X = X + A (SNG)
  4045.   00F72 18DD            JR      0F51H           ;Get next digit
  4046.  
  4047. ; Overflow of SNG
  4048.  
  4049.   00F74 CDE30A          CALL    0AE3H           ;X = CDBL ( X )
  4050.  
  4051. ; Insert new digit into DBL number
  4052.  
  4053.   00F77 CD4D0E          CALL    0E4DH           ;X = X * 10 (DBL)
  4054.   00F7A CDFC09          CALL    09FCH           ;Y = X
  4055.   00F7D F1              POP     AF              ;A = digit value
  4056.   00F7E CD6409          CALL    0964H           ;X = A
  4057.   00F81 CDE30A          CALL    0AE3H           ;X = CDBL ( X )
  4058.   00F84 CD770C          CALL    0C77H           ;X = X + Y
  4059.   00F87 18C8            JR      0F51H           ;Get next digit
  4060.  
  4061. ; X = X + A (SNG)
  4062.  
  4063.   00F89 CDA409          CALL    09A4H           ;(SP) = X
  4064.   00F8C CD6409          CALL    0964H           ;X = A
  4065.  
  4066. ; X = X + (SP) (SNG)
  4067.  
  4068.   00F8F C1              POP     BC              ;BCDE = (SP)
  4069.   00F90 D1              POP     DE
  4070.   00F91 C31607          JP      0716H           ;X = X + BCDE
  4071.  
  4072. ; Digit found after 'E' or 'D'
  4073.  
  4074.   00F94 7B              LD      A,E             ;A = current exponent
  4075.   00F95 FE0A            CP      0AH             ;Exponent >= 10 ?
  4076.                                                 ;Already two exponent postions
  4077.                                                 ;recognized ?
  4078.                                                 ;(one position can only give
  4079.                                                 ;a maximum of 9)
  4080.   00F97 3009            JR      NC,0FA2H        ;Yes: set exponent to 48 to
  4081.                                                 ;force an overflow
  4082.   00F99 07              RLCA                    ;A = A * 2 = exponent *  2
  4083.   00F9A 07              RLCA                    ;A = A * 2 = exponent *  4
  4084.   00F9B 83              ADD     A,E             ;A = A * E = exponent *  5
  4085.   00F9C 07              RLCA                    ;A = A * 2 = exponent * 10
  4086.   00F9D 86              ADD     A,(HL)          ;Insert new exponent digit
  4087.   00F9E D630            SUB     30H             ;Subtract 30H, because an
  4088.                                                 ;ASCII value was inserted
  4089.                                                 ;(Result is always positive,
  4090.                                                 ;because (HL) is in the range
  4091.                                                 ;from 30H to 39H ('0' to '9'))
  4092.   00FA0 5F              LD      E,A             ;E = new exponent
  4093.   00FA1 FA1E32          JP      M,321EH         ;-- (positive result!)
  4094. * 00FA1   1E32          LD      E,32H           ;Exponent = 48 at overflow
  4095.   00FA4 C3BD0E          JP      0EBDH           ;Get next exponent digit
  4096.  
  4097.  
  4098. ; Print 'in' followed by number in HL (routine for Error and Break)
  4099.  
  4100.   00FA7 E5              PUSH    HL              ;Save number
  4101.   00FA8 212419          LD      HL,1924H        ;HL -> ' in '
  4102.   00FAB CDA728          CALL    28A7H           ;Print text
  4103.   00FAE E1              POP     HL              ;Restore number
  4104.  
  4105. ; Print HL as decimal number (routine for printing line numbers with LIST)
  4106.  
  4107.   00FAF CD9A0A          CALL    0A9AH           ;Write HL to X as INT
  4108.   00FB2 AF              XOR     A               ;A = 00H: no formatting
  4109.   00FB3 CD3410          CALL    1034H           ;Store formatting byte and
  4110.                                                 ;clear sign
  4111.   00FB6 B6              OR      (HL)            ;A = 20H (bit 7 of A = 0)
  4112.   00FB7 CDD90F          CALL    0FD9H           ;Generate unformatted string
  4113.   00FBA C3A628          JP      28A6H           ;Print string
  4114.  
  4115.  
  4116. ; Conversion of X into an unformatted string (for PRINT)
  4117. ; (like STR$ function)
  4118. ;
  4119. ; I: X = number
  4120. ; O: HL -> string (= 4130H)
  4121.  
  4122.   00FBD AF              XOR     A               ;Clear format byte
  4123.  
  4124. ; Conversion of X into a formatted string (for PRINT USING)
  4125. ; (like STR$ function)
  4126. ;
  4127. ; I: X = number
  4128. ;    A = formatting code: Bit 7 = 1: do a format
  4129. ;                         Bit 6 = 1: print ',' for separation of thousands
  4130. ;                         Bit 5 = 1: fill leading spaces with '*'
  4131. ;                         Bit 4 = 1: print '$' in front of number
  4132. ;                         Bit 3 = 1: print sign (also '+')
  4133. ;                         Bit 2 = 1: print sign behind number
  4134. ;                         Bit 1 = -: not used
  4135. ;                         Bit 0 = 1: print exponent
  4136. ;
  4137. ; O: HL -> start of string (= 4130H)
  4138. ;    DE -> end of string
  4139.  
  4140.   00FBE CD3410          CALL    1034H           ;Store format byte
  4141.                                                 ;Clear sign position in buffer
  4142.                                                 ;and set HL to start of buffer
  4143.                                                 ;( = 4130H)
  4144.   00FC1 E608            AND     08H             ;Sign requested ?
  4145.   00FC3 2802            JR      Z,0FC7H         ;No: continue at 0FC7H
  4146.  
  4147.   00FC5 362B            LD      (HL),2BH        ;First set sign to '+'
  4148.   00FC7 EB              EX      DE,HL           ;Save HL in DE
  4149.   00FC8 CD9409          CALL    0994H           ;TEST1
  4150.   00FCB EB              EX      DE,HL           ;Restore HL
  4151.                                                 ;X positive ?
  4152.   00FCC F2D90F          JP      P,0FD9H         ;Yes: leave sign as it is
  4153.  
  4154.   00FCF 362D            LD      (HL),2DH        ;Set sign to '-'
  4155.   00FD1 C5              PUSH    BC              ;Save BC
  4156.   00FD2 E5              PUSH    HL              ;Save HL
  4157.   00FD3 CD7B09          CALL    097BH           ;X = -X
  4158.                                                 ;(X is processed as a positive
  4159.                                                 ;value because the sign is
  4160.                                                 ;already in the buffer)
  4161.   00FD6 E1              POP     HL              ;Restore HL
  4162.   00FD7 C1              POP     BC              ;Restore BC
  4163.   00FD8 B4              OR      H               ;Z-flag = 0
  4164.                                                 ;X is now positive
  4165.                                                 ;Z-flag = 1 in case X = 0
  4166.   00FD9 23              INC     HL              ;Pointer + 1
  4167.   00FDA 3630            LD      (HL),30H        ;Put '0' in buffer
  4168.   00FDC 3AD840          LD      A,(40D8H)       ;A = format byte
  4169.   00FDF 57              LD      D,A             ;Save it in D
  4170.   00FE0 17              RLA                     ;C-flag = bit 7 of A
  4171.   00FE1 3AAF40          LD      A,(40AFH)       ;A = VT
  4172.                                                 ;Formatting required ?
  4173.   00FE4 DA9A10          JP      C,109AH         ;Yes: continue at 109AH
  4174.                                                 ;Number is zero ?
  4175.   00FE7 CA9210          JP      Z,1092H         ;Yes: done, continue at 1092H
  4176.  
  4177.   00FEA FE04            CP      04H             ;Is X an INT (VT < 4) ?
  4178.   00FEC D23D10          JP      NC,103DH        ;No: continue at 103DH
  4179.  
  4180. ; Convert INT number to unformatted string
  4181.  
  4182.   00FEF 010000          LD      BC,0000H        ;B = 0: do not generate a
  4183.                                                 ;decimal point
  4184.                                                 ;C = 0: no separator of
  4185.                                                 ;thousands
  4186.   00FF2 CD2F13          CALL    132FH           ;Convert number into
  4187.                                                 ;unformatted string using 5
  4188.                                                 ;digits (incl. leading zeroes)
  4189.  
  4190. ; Delete leading zeroes or replace them by '*'
  4191.  
  4192.   00FF5 213041          LD      HL,4130H        ;HL -> buffer
  4193.   00FF8 46              LD      B,(HL)          ;B = sign (' ' or '-')
  4194.   00FF9 0E20            LD      C,' '           ;C = ' '
  4195.   00FFB 3AD840          LD      A,(40D8H)       ;A = format byte
  4196.   00FFE 5F              LD      E,A             ;E = format byte
  4197.   00FFF E620            AND     20H             ;Fill space with '*' ?
  4198.   01001 2807            JR      Z,100AH         ;No: continue at 100AH
  4199.  
  4200.   01003 78              LD      A,B             ;A = sign
  4201.   01004 B9              CP      C               ;Sign = ' ' ?
  4202.   01005 0E2A            LD      C,'*'           ;C = '*'
  4203.   01007 2001            JR      NZ,100AH        ;No: sign = '-', goto 100AH
  4204.  
  4205.   01009 41              LD      B,C             ;Replace sign (' ') by '*'
  4206.  
  4207.   0100A 71              LD      (HL),C          ;Write ' ' or '*' into buffer
  4208.   0100B D7              RST     10H             ;A = next character
  4209.                                                 ;End of string ?
  4210.   0100C 2814            JR      Z,1022H         ;Yes: continue at 1022H
  4211.  
  4212.   0100E FE45            CP      'E'             ;'E' found ?
  4213.   01010 2810            JR      Z,1022H         ;Yes: string end ('E' does
  4214.                                                 ;not belong to INT format)
  4215.   01012 FE44            CP      'D'             ;'D' found ?
  4216.   01014 280C            JR      Z,1022H         ;Yes: string end ('D' does
  4217.                                                 ;not belong to INT format)
  4218.   01016 FE30            CP      '0'             ;Leading zero ?
  4219.   01018 28F0            JR      Z,100AH         ;Yes: replace by ' 'or '*'
  4220.  
  4221.   0101A FE2C            CP      ','             ;',' found ?
  4222.   0101C 28EC            JR      Z,100AH         ;Yes: replace by ' ' or '*'
  4223.  
  4224.   0101E FE2E            CP      '.'             ;'.' found
  4225.   01020 2003            JR      NZ,1025H        ;No: continue at 1025H
  4226.  
  4227.   01022 2B              DEC     HL              ;Yes: decimal point found
  4228.   01023 3630            LD      (HL),30H        ;Replace by '0'
  4229.   01025 7B              LD      A,E             ;A = format byte
  4230.   01026 E610            AND     10H             ;'$' in front of number ?
  4231.   01028 2803            JR      Z,102DH         ;No: continue at 102DH
  4232.  
  4233.   0102A 2B              DEC     HL              ;Yes: insert '$'
  4234.   0102B 3624            LD      (HL),'$'
  4235.   0102D 7B              LD      A,E             ;A = format byte
  4236.   0102E E604            AND     04H             ;Print sign behind number ?
  4237.   01030 C0              RET     NZ              ;Yes: string is ready, return
  4238.  
  4239.   01031 2B              DEC     HL              ;No: put back sign in front of
  4240.   01032 70              LD      (HL),B          ;number
  4241.   01033 C9              RET
  4242.  
  4243. ; Save format byte, set HL to start of buffer and clear sign
  4244.  
  4245.   01034 32D840          LD      (40D8H),A       ;Save format byte in system RAM
  4246.   01037 213041          LD      HL,4130H        ;HL -> start of buffer
  4247.   0103A 3620            LD      (HL),' '        ;Clear sign in buffer
  4248.   0103C C9              RET
  4249.  
  4250. ; X is in floating point format
  4251. ; Generate unformatted string
  4252.  
  4253.   0103D FE05            CP      05H             ;if SNG then C-flag = 1
  4254.                                                 ;       else C-flag = 1
  4255.   0103F E5              PUSH    HL              ;Save pointer
  4256.   01040 DE00            SBC     A,00H           ;If SNG then A = 3
  4257.                                                 ;if DBL then A = 8
  4258.   01042 17              RLA                     ;* 2 gives the number of
  4259.                                                 ;decimal positions to be
  4260.                                                 ;generated  - 1
  4261.   01043 57              LD      D,A             ;D = A
  4262.   01044 14              INC     D               ;D = maximum number of
  4263.                                                 ;positions (7 for SNG,
  4264.                                                 ;17 for DBL)
  4265.   01045 CD0112          CALL    1201H           ;Scale X to 6 / 16 positions
  4266.                                                 ;A = exponent offset
  4267.                                                 ;( = number of decimal point
  4268.                                                 ;shifts to the left during
  4269.                                                 ;scaling)
  4270.   01048 010003          LD      BC,0300H        ;B = 3 (decimal point pos. + 1)
  4271.                                                 ;C = 0 (no thousands separator)
  4272.   0104B 82              ADD     A,D             ;A = exponent-offset + maximum
  4273.                                                 ;number of positions =
  4274.                                                 ;10-exponent + 2
  4275.                                                 ;10-exponent < -2 ?
  4276.   0104C FA5710          JP      M,1057H         ;Yes: continue at 1057H
  4277.  
  4278.   0104F 14              INC     D               ;Position mnumber + 1 smaller
  4279.   01050 BA              CP      D               ;than 10-exponent + 2
  4280.   01051 3004            JR      NC,1057H        ;Yes: continue at 1057H
  4281.  
  4282.   01053 3C              INC     A               ;10-exponent + 3 =
  4283.                                                 ;decimal point position + 1
  4284.   01054 47              LD      B,A             ;B = decimal point pos. + 1
  4285.   01055 3E02            LD      A,02H           ;A = 2 because of SUB 02H:
  4286.                                                 ;no 10-exponent is printed
  4287.   01057 D602            SUB     02H             ;A = 10-exponent
  4288.   01059 E1              POP     HL              ;Restore buffer pointer
  4289.   0105A F5              PUSH    AF              ;Save 10-exponent
  4290.   0105B CD9112          CALL    1291H           ;Set ',' and '.', B - 1
  4291.   0105E 3630            LD      (HL),'0'        ;Use '0'
  4292.   01060 CCC909          CALL    Z,09C9H         ;Pointer + 1 when decimal point
  4293.                                                 ;set
  4294.   01063 CDA412          CALL    12A4H           ;Convert X into unformatted
  4295.                                                 ;string with 7 / 17 positions
  4296.                                                 ;Use decimal point after B
  4297.                                                 ;positions
  4298.   01066 2B              DEC     HL              ;Pointer - 1
  4299.   01067 7E              LD      A,(HL)          ;A = character
  4300.   01068 FE30            CP      '0'             ;Trailing zero ?
  4301.   0106A 28FA            JR      Z,1066H         ;Yes: pointer = last character
  4302.                                                 ;unequal to '0'. (trailing
  4303.                                                 ;zeroes are represented by
  4304.                                                 ;10-exponent)
  4305.   0106C FE2E            CP      '.'             ;Last character is the decimal
  4306.                                                 ;point ?
  4307.                                                 ;Yes: leave pointer alone,
  4308.                                                 ;decimal point is deleted
  4309.   0106E C4C909          CALL    NZ,09C9H        ;No: pointer + 1
  4310.   01071 F1              POP     AF              ;Restore 10-exponent
  4311.                                                 ;10-exponent = 0 ?
  4312.   01072 281F            JR      Z,1093H         ;Yes: done, continue at 1093H
  4313.  
  4314. ; Use 10-exponent
  4315. ; A = 10-exponent
  4316.  
  4317.   01074 F5              PUSH    AF              ;Save 10-exponent
  4318.   01075 E7              RST     20H             ;TSTTYP
  4319.   01076 3E22            LD      A,22H           ;A = ASCII value of 'D' / 2
  4320.   01078 8F              ADC     A,A             ;A = A *2 + C-flag
  4321.                                                 ;A = 'E' (X in SNG format)
  4322.                                                 ;A = 'D' (X in DBL format)
  4323.   01079 77              LD      (HL),A          ;Put correct exponent character
  4324.                                                 ;in buffer
  4325.   0107A 23              INC     HL              ;Pointer + 1
  4326.   0107B F1              POP     AF              ;Restore 10-exponent
  4327.   0107C 362B            LD      (HL),'+'        ;Assume positve sign
  4328.                                                 ;10-exponent > 0 ?
  4329.   0107E F28510          JP      P,1085H         ;Yes: continue at 1085H
  4330.  
  4331.   01081 362D            LD      (HL),'-'        ;Use '-' instead
  4332.   01083 2F              CPL                     ;Negate exponent (so treat it
  4333.   01084 3C              INC     A               ;as a positive number)
  4334.   01085 062F            LD      B,2FH           ;B = ASCII value of '0' - 1
  4335.   01087 04              INC     B               ;B + 1 (next digit in the
  4336.                                                 ;tenth position
  4337.   01088 D60A            SUB     0AH             ;Subtract 10 for 10-exponent
  4338.                                                 ;10-exponent < 10 ?
  4339.   0108A 30FB            JR      NC,1087H        ;No: increment digit again
  4340.  
  4341.   0108C C63A            ADD     A,3AH           ;+ 3AH gives correct ASCII
  4342.                                                 ;value for remaining position
  4343.   0108E 23              INC     HL              ;Pointer + 1
  4344.   0108F 70              LD      (HL),B          ;Set first position of exp.
  4345.   01090 23              INC     HL              ;Pointer + 1
  4346.   01091 77              LD      (HL),A          ;Set second position of exp.
  4347.  
  4348. ; Terminate string with 00H
  4349.  
  4350.   01092 23              INC     HL              ;Pointer + 1
  4351.   01093 3600            LD      (HL),00H        ;Terminate with 00H
  4352.   01095 EB              EX      DE,HL           ;DE -> end of string
  4353.   01096 213041          LD      HL,4130H        ;HL -> start of string
  4354.   01099 C9              RET
  4355.  
  4356.  
  4357. ; Formatting required
  4358. ;
  4359. ; I: A= VT
  4360. ;    BC = position counter before and after decimal point
  4361. ;    D = format byte
  4362. ;    HL -> buffer
  4363.  
  4364.   0109A 23              INC     HL              ;Pointer + 1
  4365.   0109B C5              PUSH    BC              ;Save counters
  4366.   0109C FE04            CP      04H             ;X in INT format ?
  4367.   0109E 7A              LD      A,D             ;A = format byte
  4368.   0109F D20911          JP      NC,1109H        ;No: continue at 1109H
  4369.  
  4370. ; Convert INT number into formatted string
  4371.  
  4372.   010A2 1F              RRA                     ;C-flag = bit 0 of A
  4373.                                                 ;Use 10-exponent ?
  4374.   010A3 DAA311          JP      C,11A3H         ;Yes: continue at 11A3H
  4375.                                                 ;(convert number to SNG)
  4376.   010A6 010306          LD      BC,0603H        ;B = maximum number of
  4377.                                                 ;positions before decimal point
  4378.                                                 ;C = counter for thousands
  4379.                                                 ;separation
  4380.   010A9 CD8912          CALL    1289H           ;Set C = 0 if no thousands
  4381.                                                 ;separation required
  4382.   010AC D1              POP     DE              ;Restore counters
  4383.                                                 ;D = number of positions
  4384.                                                 ;before decimal point
  4385.                                                 ;E = number of positions
  4386.                                                 ;after decimal point
  4387.   010AD 7A              LD      A,D             ;A = number of positions
  4388.                                                 ;before decimal point
  4389.   010AE D605            SUB     05H             ;More then 4 positions ?
  4390.   010B0 F46912          CALL    P,1269H         ;Yes: use corresponding number
  4391.                                                 ;of leading zeroes
  4392.   010B3 CD2F13          CALL    132FH           ;Convert X into 5 digit string
  4393.   010B6 7B              LD      A,E             ;E = number of positions
  4394.                                                 ;after decimal point
  4395.   010B7 B7              OR      A               ;Any positions at all ?
  4396.   010B8 CC2F09          CALL    Z,092FH         ;Yes: buffer pointer -1
  4397.   010BB 3D              DEC     A               ;Any positions required ?
  4398.   010BC F46912          CALL    P,1269H         ;Yes: use corresponding number
  4399.                                                 ;of zeroes
  4400.   010BF E5              PUSH    HL              ;Save pointer on end of buffer
  4401.   010C0 CDF50F          CALL    0FF5H           ;Delete leading zeroes or
  4402.                                                 ;replace them by '*'
  4403.   010C3 E1              POP     HL              ;Restore buffer pointer
  4404.                                                 ;Sign in front ?
  4405.   010C4 2802            JR      Z,10C8H         ;yes: continue at 10C8H
  4406.  
  4407.   010C6 70              LD      (HL),B          ;Set sign behind the number
  4408.   010C7 23              INC     HL              ;Pointer + 1
  4409.   010C8 3600            LD      (HL),00H        ;Terminate string
  4410.   010CA 212F41          LD      HL,412FH        ;HL -> Start of buffer - 1
  4411.   010CD 23              INC     HL              ;Pointer + 1
  4412.   010CE 3AF340          LD      A,(40F3H)       ;A = LSB of buffer address of
  4413.                                                 ;decimal point
  4414.   010D1 95              SUB     L               ;-LSB of current buffer pointer
  4415.   010D2 92              SUB     D               ;Equals the requested number of
  4416.                                                 ;positions before the decimal
  4417.                                                 ;point ?
  4418.   010D3 C8              RET     Z               ;Yes: done, return
  4419.  
  4420. ; Shift string in buffer
  4421. ; Delete a position before the decimal point (means deleting a space)
  4422.  
  4423.   010D4 7E              LD      A,(HL)          ;Get next character from buffer
  4424.   010D5 FE20            CP      ' '             ;Is it a leading space ?
  4425.   010D7 28F4            JR      Z,10CDH         ;Yes: skip it, next character
  4426.  
  4427.   010D9 FE2A            CP      '*'             ;'*' in front of number ?
  4428.   010DB 28F0            JR      Z,10CDH         ;Yes: skip it, next character
  4429.  
  4430.   010DD 2B              DEC     HL              ;Pointer - 1
  4431.   010DE E5              PUSH    HL              ;Save pointer
  4432.                                                 ;(now points on sign character
  4433.                                                 ;or the first digit or '$')
  4434.   010DF F5              PUSH    AF              ;Save character on stack
  4435.   010E0 01DF10          LD      BC,10DFH        ;Set new RET address to 10DFH
  4436.   010E3 C5              PUSH    BC
  4437.   010E4 D7              RST     10H             ;Search start of number string
  4438.                                                 ;and get next character
  4439.   010E5 FE2D            CP      '-'             ;'-' found ?
  4440.   010E7 C8              RET     Z               ;Yes: save character, next
  4441.                                                 ;character
  4442.   010E8 FE2B            CP      '+'             ;'+' found ?
  4443.   010EA C8              RET     Z               ;Yes: save character, next
  4444.                                                 ;character
  4445.   010EB FE24            CP      '$'             ;'$' found ?
  4446.   010ED C8              RET     Z               ;Yes: save character, next
  4447.                                                 ;character
  4448.   010EE C1              POP     BC              ;Remove RET adress
  4449.   010EF FE30            CP      '0'             ;Leading zero found ?
  4450.   010F1 200F            JR      NZ,1102H        ;No: field overflow!
  4451.                                                 ;Yes:
  4452.   010F3 23              INC     HL              ;Pointer + 1, skip leading zero
  4453.   010F4 D7              RST     10H             ;Get next character
  4454.                                                 ;Is it a digit ?
  4455.   010F5 300B            JR      NC,1102H        ;No: field overflow!
  4456.                                                 ;Yes:
  4457.   010F7 2B              DEC     HL              ;Pointer - 1 (string starts
  4458.                                                 ;one position earlier in
  4459.                                                 ;the buffer)
  4460.   010F8 012B77          LD      BC,772BH        ;--
  4461. * 010F8   2B            DEC     HL              ;Buffer pointer - 1
  4462. * 010F8     77          LD      (HL),A          ;Store character back into
  4463.                                                 ;the buffer
  4464.   010FB F1              POP     AF              ;Get character from stack
  4465.                                                 ;Start of string reached ?
  4466.   010FC 28FB            JR      Z,10F9H         ;No: store next character
  4467.                                                 ;Yes:
  4468.   010FE C1              POP     BC              ;Remove buffer pointer from
  4469.                                                 ;stack
  4470.   010FF C3CE10          JP      10CEH           ;Number of positions now ok ?
  4471.  
  4472.  
  4473. ; Field overflow
  4474. ; More positions before decimal point are generated as there are reqeusted
  4475.  
  4476.   01102 F1              POP     AF              ;Get character from stack
  4477.                                                 ;Last character ?
  4478.   01103 28FD            JR      Z,1102H         ;No: get next character
  4479.                                                 ;Yes:
  4480.   01105 E1              POP     HL              ;Restore buffer pointer to
  4481.                                                 ;start of string
  4482.   01106 3625            LD      (HL),'%'        ;Use '%' to indicate overflow
  4483.   01108 C9              RET
  4484.  
  4485.  
  4486. ; Formatting requested
  4487. ; X is in floating point format
  4488.  
  4489.   01109 E5              PUSH    HL              ;Save pointer
  4490.   0110A 1F              RRA                     ;C-flag = bit 0 of A
  4491.                                                 ;Exponent output requested ?
  4492.   0110B DAAA11          JP      C,11AAH         ;Yes: continue at 11AAH
  4493.                                                 ;X in SNG format ?
  4494.   0110E 2814            JR      Z,1124H         ;Yes: continue at 1124H
  4495.  
  4496.  
  4497. ; X in DBL format
  4498. ; Generate number string without exponent
  4499. ; (X must not have more that 16 positions before the decimal point. This
  4500. ; means that X must be smaller than 1D+16)
  4501.  
  4502.   01110 118413          LD      DE,1384H        ;DE -> 1D+16
  4503.   01113 CD490A          CALL    0A49H           ;CP X,(DE) = CP X,1D+16
  4504.   01116 1610            LD      D,10H           ;D = maximum number of
  4505.                                                 ;positions (16)
  4506.                                                 ;X < 1D+16 ?
  4507.   01118 FA3211          JP      M,1132H         ;Yes: continue at 1132H
  4508.  
  4509. ; Field overflow with floating point number
  4510.  
  4511.   0111B E1              POP     HL              ;Restore buffer pointer
  4512.   0111C C1              POP     BC              ;Restore counters
  4513.   0111D CDBD0F          CALL    0FBDH           ;Generate unformatted string
  4514.   01120 2B              DEC     HL              ;Buffer pointer - 1
  4515.   01121 3625            LD      (HL),'%'        ;Use '%' in front of string
  4516.                                                 ;to indicate overflow
  4517.   01123 C9              RET
  4518.  
  4519. ; X has SNG format
  4520.  
  4521.   01124 010EB6          LD      BC,0B60EH       ;BCDE = 1E+16
  4522.   01127 11CA1B          LD      DE,1BCAH
  4523.   0112A CD0C0A          CALL    0A0CH           ;CP X,BCDE = CP X,1E+16
  4524.                                                 ;X > 1E+16 ?
  4525.   0112D F21B11          JP      P,111BH         ;Yes: field overflow
  4526.  
  4527.   01130 1606            LD      D,06H           ;D = maximum number of
  4528.                                                 ;positions (6)
  4529.   01132 CD5509          CALL    0955H           ;TEST2 (number = 0 ?)
  4530.   01135 C40112          CALL    NZ,1201H        ;No: scale number to 6 or 16
  4531.                                                 ;positions
  4532.   01138 E1              POP     HL              ;Restore pointer
  4533.   01139 C1              POP     BC              ;Restore counters on number of
  4534.                                                 ;positions before and after
  4535.                                                 ;the decimal point
  4536.                                                 ;Did scale operation extend ?
  4537.   0113A FA5711          JP      M,1157H         ;Yes: continue at 1157H
  4538.  
  4539. ; The scale operation resulted in truncation (no fractional digits)
  4540. ; A = exponent offset ( > 0 )
  4541.  
  4542.   0113D C5              PUSH    BC              ;Save counters
  4543.   0113E 5F              LD      E,A             ;E = exponent offset
  4544.   0113F 78              LD      A,B             ;A = number of positions before
  4545.                                                 ;the decimal point
  4546.   01140 92              SUB     D               ;- maximum number of positions
  4547.   01141 93              SUB     E               ;- exponent offset
  4548.   01142 F46912          CALL    P,1269H         ;Use corresponding number of
  4549.                                                 ;leading zeroes
  4550.   01145 CD7D12          CALL    127DH           ;Compute decimal point position
  4551.                                                 ;and thousand separation
  4552.   01148 CDA412          CALL    12A4H           ;Convert floating point number
  4553.                                                 ;into unformatted string
  4554.   0114B B3              OR      E               ;A = exponent offset
  4555.   0114C C47712          CALL    NZ,1277H        ;Set corresponding number
  4556.                                                 ;of trailing zeroes (because
  4557.                                                 ;10-exponent is not used)
  4558.   0114F B3              OR      E               ;A = exponent offset
  4559.   01150 C49112          CALL    NZ,1291H        ;Use decimal point if necessary
  4560.   01153 D1              POP     DE              ;Restore positions counters
  4561.   01154 C3B610          JP      10B6H           ;Number of positions ok ?
  4562.  
  4563. ; The scale operation resulted in extension (fractional digits presents)
  4564. ; A = exponent offset ( < 0 )
  4565.  
  4566.   01157 5F              LD      E,A             ;E = exponent offset
  4567.   01158 79              LD      A,C             ;A = number of requested
  4568.                                                 ;positions after the decimal
  4569.                                                 ;point
  4570.   01159 B7              OR      A               ;Any positions requested ?
  4571.   0115A C4160F          CALL    NZ,0F16H        ;Yes: A - 1 (because of
  4572.                                                 ;decimal point)
  4573.   0115D 83              ADD     A,E             ;A = number of positions
  4574.                                                 ;after the decimal point
  4575.                                                 ;+ exponent offset
  4576.                                                 ;= negative number of the
  4577.                                                 ;superfluous generated
  4578.                                                 ;positions at scaling
  4579.   0115E FA6211          JP      M,1162H         ;If too many positions
  4580.                                                 ;generated then leave A as it is
  4581.   01161 AF              XOR     A               ;else set A = 0
  4582.   01162 C5              PUSH    BC              ;Save position counters
  4583.   01163 F5              PUSH    AF              ;Save number of positions
  4584.                                                 ;generated too many
  4585.   01164 FC180F          CALL    M,0F18H         ;X = X / 10 , A + 1
  4586.   01167 FA6411          JP      M,1164H         ;Reverse scaling until the
  4587.                                                 ;requested number of positions
  4588.                                                 ;behind the decimal point is
  4589.                                                 ;reached
  4590.   0116A C1              POP     BC              ;B = neagative number of
  4591.                                                 ;positions generated too many
  4592.   0116B 7B              LD      A,E             ;A = exponent offset
  4593.   0116C 90              SUB     B               ;+ number of positions
  4594.                                                 ;generated too many
  4595.   0116D C1              POP     BC              ;Restore position counters
  4596.   0116E 5F              LD      E,A             ;E = exponent offset ( < 0!)
  4597.   0116F 82              ADD     A,D             ;Is the exponent offset + the
  4598.                                                 ;maximum number of requested
  4599.                                                 ;positions before the decimal
  4600.                                                 ;point < 0 ? (this means no
  4601.                                                 ;positions before the decimal
  4602.                                                 ;point)
  4603.   01170 78              LD      A,B             ;A = number of positions
  4604.                                                 ;before the decimal point
  4605.   01171 FA7F11          JP      M,117FH         ;Yes: continue at 117FH
  4606.  
  4607.   01174 92              SUB     D               ;A = number of positions before
  4608.                                                 ;decimal point - maximum number
  4609.                                                 ;of positions before decimal
  4610.                                                 ;point
  4611.   01175 93              SUB     E               ;- exponent offset
  4612.   01176 F46912          CALL    P,1269H         ;Set corresponding number
  4613.                                                 ;of leading zeroes in buffer
  4614.   01179 C5              PUSH    BC              ;Saev position counters
  4615.   0117A CD7D12          CALL    127DH           ;Compute decimal point
  4616.                                                 ;position and counter for
  4617.                                                 ;thousands separation
  4618.   0117D 1811            JR      1190H           ;continue at 1190H
  4619.  
  4620. ; No positions before decimal point present (only a fractional part, see 116EH)
  4621.  
  4622.   0117F CD6912          CALL    1269H           ;Simulate requested field
  4623.                                                 ;length before decimal point
  4624.                                                 ;by using leading zeroes
  4625.   01182 79              LD      A,C             ;A = number of requested
  4626.                                                 ;positions behind the decimal
  4627.                                                 ;point + 1
  4628.   01183 CD9412          CALL    1294H           ;Set decinal point, C = B
  4629.   01186 4F              LD      C,A             ;Write field length after
  4630.                                                 ;decimal point back to C
  4631.   01187 AF              XOR     A               ;A = 0
  4632.   01188 92              SUB     D               ;A = A - maximum number of
  4633.                                                 ;positions
  4634.   01189 93              SUB     E               ;+ exponent offset (+ because
  4635.                                                 ;E < 0)
  4636.   0118A CD6912          CALL    1269H           ;Set corresponding number of
  4637.                                                 ;trailing zeroes after the
  4638.                                                 ;decimal point
  4639.   0118D C5              PUSH    BC              ;Save position counters
  4640.   0118E 47              LD      B,A             ;B = 0 (A = 0 because of 1269H)
  4641.   0118F 4F              LD      C,A             ;C = 0: use no decimal point
  4642.                                                 ;and no separation of thousands
  4643.   01190 CDA412          CALL    12A4H           ;Generate unformatted string
  4644.   01193 C1              POP     BC              ;Restore position counters
  4645.   01194 B1              OR      C               ;A = requested number of
  4646.                                                 ;positions behing the decimal
  4647.                                                 ;point.
  4648.                                                 ;Any positions behind the
  4649.                                                 ;decimal point specified ?
  4650.   01195 2003            JR      NZ,119AH        ;Yes: leave buffer pointer as
  4651.                                                 ;it is (buffer pointer points
  4652.                                                 ;at the last string char! )
  4653.   01197 2AF340          LD      HL,(40F3H)      ;No: set buffer pointer to the
  4654.                                                 ;position of the decimal
  4655.                                                 ;point (= end of string!)
  4656.   0119A 83              ADD     A,E             ;A = number of requested
  4657.                                                 ;positions behind the decimal
  4658.                                                 ;point + 1 + exponent offset
  4659.   0119B 3D              DEC     A               ;Adjust because of + 1
  4660.   0119C F46912          CALL    P,1269H         ;Set corresponding number of
  4661.                                                 ;trailing zeroes after the
  4662.                                                 ;decimal point
  4663.   0119F 50              LD      D,B             ;D = number of requested
  4664.                                                 ;positions before decimal point
  4665.   011A0 C3BF10          JP      10BFH           ;Continue at 10BFH
  4666.  
  4667.  
  4668. ; INT number using exponential format
  4669. ; For this, X has to be converted to SNG format
  4670.  
  4671.   011A3 E5              PUSH    HL              ;Save buffer pointer
  4672.   011A4 D5              PUSH    DE              ;Save format byte
  4673.   011A5 CDCC0A          CALL    0ACCH           ;X = CSNG ( X )
  4674.   011A8 D1              POP     DE              ;Restore format byte
  4675.   011A9 AF              XOR     A               ;A = 0 (Z-flag = 1 because
  4676.                                                 ;X in SNG format)
  4677. ; SNG or DBL number using exponential format
  4678.                                                 ;SNG number ?
  4679.   011AA CAB011          JP      Z,11B0H         ;Yes: continue at 11B0H
  4680.  
  4681.   011AD 1E10            LD      E,10H           ;E = maximum number of
  4682.                                                 ;positions (16)
  4683.   011AF 011E06          LD      BC,061EH        ;--
  4684. * 011B0   1E06          LD      E,06H           ;E = maximum number of
  4685.                                                 ;positions (16)
  4686.   011B2 CD5509          CALL    0955H           ;TEST2. X = 0 ?
  4687.   011B5 37              SCF                     ;C-flag = 1
  4688.   011B6 C40112          CALL    NZ,1201H        ;No, scale X to 6 (SNG) or
  4689.                                                 ;16 (DBL) positions
  4690.   011B9 E1              POP     HL              ;Restore buffer pointer
  4691.   011BA C1              POP     BC              ;Restore position counters
  4692.   011BB F5              PUSH    AF              ;Save exponent offset
  4693.   011BC 79              LD      A,C             ;A = number of positions behind
  4694.                                                 ;the decimal point
  4695.   011BD B7              OR      A               ;Any positions behind decimal
  4696.                                                 ;point specified ?
  4697.   011BE F5              PUSH    AF              ;Save number of positions
  4698.   011BF C4160F          CALL    NZ,0F16H        ;Yes: A - 1 (because of decimal
  4699.                                                 ;point)
  4700.   011C2 80              ADD     A,B             ;+ number of positions in front
  4701.                                                 ;of the decimal point
  4702.   011C3 4F              LD      C,A             ;C = total length
  4703.   011C4 7A              LD      A,D             ;A = format byte
  4704.   011C5 E604            AND     04H             ;Print sign behind number ?
  4705.   011C7 FE01            CP      01H             ;No: C-flag = 1
  4706.   011C9 9F              SBC     A,A             ;If not then A = FFH (-1)
  4707.   011CA 57              LD      D,A             ;D = A
  4708.   011CB 81              ADD     A,C             ;A = total length, left alone
  4709.                                                 ;if sign is to printed in front
  4710.                                                 ;else decrement it by 1
  4711.   011CC 4F              LD      C,A             ;C = total length
  4712.   011CD 93              SUB     E               ;A = requested total length
  4713.                                                 ;of string - maximum number
  4714.                                                 ;of positions
  4715.   011CE F5              PUSH    AF              ;Save difference
  4716.   011CF C5              PUSH    BC              ;Save total length
  4717.                                                 ;Is the requested number of
  4718.                                                 ;positions < generated number
  4719.                                                 ;of positions
  4720.   011D0 FC180F          CALL    M,0F18H         ;Yes: X = X / 10, A - 1
  4721.   011D3 FAD011          JP      M,11D0H         ;Scale X according to
  4722.                                                 ;difference
  4723.   011D6 C1              POP     BC              ;Restore total length
  4724.   011D7 F1              POP     AF              ;Restore difference
  4725.   011D8 C5              PUSH    BC              ;Save total length
  4726.   011D9 F5              PUSH    AF              ;Save difference
  4727.                                                 ;Total length > number of
  4728.                                                 ;positions ?
  4729.   011DA FADE11          JP      M,11DEH         ;Yes: continue at 11DEH
  4730.  
  4731.   011DD AF              XOR     A               ;A = 0
  4732.  
  4733.   011DE 2F              CPL                     ;A = positive difference
  4734.   011DF 3C              INC     A
  4735.   011E0 80              ADD     A,B             ;+ number of requested
  4736.                                                 ;positions on front of
  4737.                                                 ;decimal point
  4738.   011E1 3C              INC     A               ;+ 1
  4739.   011E2 82              ADD     A,D             ;-1 if case of sign being
  4740.                                                 ;printed in front of the number
  4741.   011E3 47              LD      B,A             ;= decomal point position
  4742.   011E4 0E00            LD      C,00H           ;Set no thousands separation
  4743.   011E6 CDA412          CALL    12A4H           ;Generate unformatted string
  4744.   011E9 F1              POP     AF              ;Restore position difference
  4745.   011EA F47112          CALL    P,1271H         ;Use corresponding number
  4746.                                                 ;of trailing zeroes
  4747.   011ED C1              POP     BC              ;Restore position counters
  4748.   011EE F1              POP     AF              ;A = requested number of
  4749.                                                 ;positions behind the decimal
  4750.                                                 ;point.
  4751.                                                 ;Any positions requested ?
  4752.   011EF CC2F09          CALL    Z,092FH         ;No: buffer pointer - 1
  4753.                                                 ;(remove decimal point again)
  4754.   011F2 F1              POP     AF              ;Restore exponent offset
  4755.                                                 ;X = 0 ? (see 11B5H)
  4756.   011F3 3803            JR      C,11F8H         ;Yes: 10-exponent is also 0
  4757.                                                 ;No:
  4758.   011F5 83              ADD     A,E             ;Add maximum number of
  4759.                                                 ;positions
  4760.   011F6 90              SUB     B               ;subtract number of already
  4761.                                                 ;generated positions in front
  4762.                                                 ;of decimal point
  4763.   011F7 92              SUB     D               ;Reverse addition of 11E2H
  4764.                                                 ;= 10-exponent
  4765.   011F8 C5              PUSH    BC              ;Save number of positions in
  4766.                                                 ;front of decimal point
  4767.   011F9 CD7410          CALL    1074H           ;Use 10-exponent
  4768.   011FC EB              EX      DE,HL           ;HL = end pointer
  4769.   011FD D1              POP     DE              ;D = number of requested
  4770.                                                 ;positions
  4771.   011FE C3BF10          JP      10BFH           ;Continue at 10BFH
  4772.  
  4773.  
  4774. ; Scaling:
  4775. ; Scale X to 6 (SNG) or 16 (DBL) positions in front of decimal point
  4776. ; O: A = 10-exponent offset
  4777.  
  4778.   01201 D5              PUSH    DE              ;Save DE
  4779.   01202 AF              XOR     A               ;Exponent offset = 00H
  4780.   01203 F5              PUSH    AF              ;Save exponent offset
  4781.   01204 E7              RST     20H             ;TSTTYP
  4782.                                                 ;X in SNG format ?
  4783.   01205 E22212          JP      PO,1222H        ;Yes: continue at 1222H
  4784.  
  4785. ; X in DBL format
  4786.  
  4787.   01208 3A2441          LD      A,(4124H)       ;A = Exp (X)
  4788.   0120B FE91            CP      91H             ;X >= 2 ^ 16 ?
  4789.   0120D D22212          JP      NC,1222H        ;Yes: continue at 1222H
  4790.  
  4791.   01210 116413          LD      DE,1364H        ;DE -> 1D+10
  4792.   01213 212741          LD      HL,4127H        ;HL -> Y
  4793.   01216 CDD309          CALL    09D3H           ;(HL) = (DE): Y = 1D+10
  4794.   01219 CDA10D          CALL    0DA1H           ;X = X * Y - X * 1D+10
  4795.   0121C F1              POP     AF              ;Restore exponent offset
  4796.   0121D D60A            SUB     0AH             ;Exponent offset - 10
  4797.                                                 ;(shifted 10 decimal positions)
  4798.   0121F F5              PUSH    AF              ;Save exponent offset
  4799.   01220 18E6            JR      1208H           ;Repeat until X >= 2 ^ 16
  4800.  
  4801. ; X in SNG format / X >= 65536 when in DBL format
  4802.  
  4803.   01222 CD4F12          CALL    124FH           ;Divide X by 10 until
  4804.                                                 ;X < 1E+6 (SNG) or
  4805.                                                 ;X < 1D+16 (DBL)
  4806. ; X is now < 1E+6 (SNG) or < 1D+16 (DBL)
  4807.  
  4808.   01225 E7              RST     20H             ;TSTTYP
  4809.                                                 ;X in DBL format ?
  4810.   01226 300B            JR      NC,1233H        ;Yes: continue at1233H
  4811.  
  4812. ; X has SNG format
  4813.  
  4814.   01228 014391          LD      BC,9143H        ;BCDE = 1E+5
  4815.   0122B 11F94F          LD      DE,4FF9H
  4816.   0122E CD0C0A          CALL    0A0CH           ;CP X,BCDE = CP X,1E+5
  4817.   01231 1806            JR      1239H           ;Continue at 1239H
  4818.  
  4819. ; X has DBL format
  4820.  
  4821.   01233 116C13          LD      DE,136CH        ;DE -> 1D+15
  4822.   01236 CD490A          CALL    0A49H           ;CP X,(DE) = CP X,1D+15
  4823.                                                 ;X >= 1D+15 (DBL) ?
  4824.                                                 ;X >= 1E+5 (SNG) ?
  4825.   01239 F24B12          JP      P,124BH         ;Yes: done
  4826.  
  4827.   0123C F1              POP     AF              ;Restore exponent offset
  4828.   0123D CD0B0F          CALL    0F0BH           ;X = X + 10 (SNG,DBL), A - 1
  4829.   01240 F5              PUSH    AF              ;Save exponent offset
  4830.   01241 18E2            JR      1225H           ;Repeat until X >= 1E+5 (SNG)
  4831.                                                               X >= 1D+15 (DBL)
  4832. ; X >= 1E+6 (SNG) or X >= 1D+16 (DBL)
  4833. ; (Continuation of 124FH)
  4834.  
  4835.   01243 F1              POP     AF              ;Restore exponent offset
  4836.   01244 CD180F          CALL    0F18H           ;X = X / 10, A + 1
  4837.   01247 F5              PUSH    AF              ;Save exponent offset
  4838.   01248 CD4F12          CALL    124FH           ;Test again
  4839.  
  4840. ; Scaling finished
  4841. ;
  4842. ;  1E+5 <= X < 1E+6 (SNG)
  4843. ; 1D+15 <= X < 1D+16 (DBL)
  4844.  
  4845.   0124B F1              POP     AF              ;Restore exponent offset
  4846.   0124C B7              OR      A               ;C-flag = 0 (for 11B5H)
  4847.   0124D D1              POP     DE              ;Restore DE
  4848.   0124E C9              RET
  4849.  
  4850. ; Divide X by 10 until X < 1E+6 (SNG) or X < 1D+16 (DBL)
  4851.  
  4852.   0124F E7              RST     20H             ;TSTTYP
  4853.                                                 ;X in DBL format ?
  4854.   01250 EA5E12          JP      PE,125EH        ;Yes: continue at 125EH
  4855.  
  4856. ; X has SNG format
  4857.  
  4858.   01253 017494          LD      BC,9474H        ;BCDE = 1E+6
  4859.   01256 11F823          LD      DE,23F8H
  4860.   01259 CD0C0A          CALL    0A0CH           ;CP X,BCDE = CP X,1E+6
  4861.   0125C 1806            JR      1264H           ;Continue at 1264H
  4862.  
  4863. ; X has DBL format
  4864.  
  4865.   0125E 117413          LD      DE,1374H        ;DE -> 1D+16
  4866.   01261 CD490A          CALL    0A49H           ;CP X,(DE) = CP X, 1D+16
  4867.  
  4868.   01264 E1              POP     HL              ;HL = RET address
  4869.                                                 ;X >= 1E+6 (SNG) ?
  4870.                                                 ;X >= 1D+16 (DBL) ?
  4871.   01265 F24312          JP      P,1243H         ;Yes: continue at 1243H
  4872.  
  4873.   01268 E9              JP      (HL)            ;RET
  4874.  
  4875.  
  4876. ; Write A zeroes into buffer from (HL) onwards
  4877. ; (positions after the decimal point)
  4878.  
  4879.   01269 B7              OR      A               ;Counter = 0 ?
  4880.   0126A C8              RET     Z               ;Yes: done, return
  4881.  
  4882.   0126B 3D              DEC     A               ;Counter - 1
  4883.   0126C 3630            LD      (HL),'0'        ;Put a '0' into buffer
  4884.   0126E 23              INC     HL              ;Pointer + 1
  4885.   0126F 18F9            JR      126AH           ;Done ?
  4886.  
  4887.  
  4888. ; Write A zeroes into buffer from (HL) onwards and set ',' and '.'
  4889. ; (positions before the decimal point)
  4890.                                                 ;Counter zero ?
  4891.   01271 2004            JR      NZ,1277H        ;No: continue at 1277H
  4892.  
  4893.   01273 C8              RET     Z               ;Yes: done, return
  4894.  
  4895.   01274 CD9112          CALL    1291H           ;Set ',' and '.'
  4896.   01277 3630            LD      (HL),'0'        ;Put a '0' into buffer
  4897.   01279 23              INC     HL              ;Pointer + 1
  4898.   0127A 3D              DEC     A               ;Counter - 1
  4899.   0127B 18F6            JR      1273H           ;Done ?
  4900.  
  4901.  
  4902. ; Establish decimal point position and counter for thousands separation
  4903.  
  4904.   0127D 7B              LD      A,E             ;A = exponent offset
  4905.   0127E 82              ADD     A,D             ;+ maimum number of generated
  4906.                                                 ;positions
  4907.   0127F 3C              INC     A               ;+ 1
  4908.   01280 47              LD      B,A             ;= decimal point position
  4909.                                                 ;(=number of positions before
  4910.                                                 ;the decimal point)
  4911.                                                 ;Ccompute counter for thousands
  4912.                                                 ;separation
  4913.   01281 3C              INC     A               ;+ 1
  4914.   01282 D603            SUB     03H             ;A = A DIV 3
  4915.   01284 30FC            JR      NC,1282H        ;(Integer division)
  4916.  
  4917.   01286 C605            ADD     A,05H           ;+5
  4918.   01288 4F              LD      C,A             ;Results in counter for
  4919.                                                 ;thousands separation
  4920.   01289 3AD840          LD      A,(40D8H)       ;A = format byte
  4921.   0128C E640            AND     40H             ;Thousands separation required?
  4922.   0128E C0              RET     NZ              ;Yes: B and C are ok, return
  4923.  
  4924.   0128F 4F              LD      C,A             ;Set C to 0
  4925.   01290 C9              RET
  4926.  
  4927.  
  4928. ; Set ',' and '.'
  4929. ;
  4930. ; I: B = number of remaining positions before the decimal point (until
  4931. ;        decimal point)
  4932. ;    C = number of remaining digits until next thousand separation
  4933. ;    C = 0: do not separate thousands
  4934.  
  4935.   01291 05              DEC     B               ;Positions before decimal
  4936.                                                 ;point - 1
  4937.                                                 ;Decimal point reached ?
  4938.   01292 2008            JR      NZ,129CH        ;No: set ','
  4939.  
  4940.   01294 362E            LD      (HL),'.'        ;Put '.' into buffer
  4941.  
  4942. ; Save buffer pointer to decimal point
  4943. ; Do not separate thousands
  4944.  
  4945.   01296 22F340          LD      (40F3H),HL      ;Save buffer pointer
  4946.   01299 23              INC     HL              ;Pointer + 1
  4947.   0129A 48              LD      C,B             ;C = 0 (no thousand separation)
  4948.   0129B C9              RET
  4949.  
  4950.  
  4951. ; Decimal point not reached yet: insert thousands separation
  4952.  
  4953.   0129C 0D              DEC     C               ;Next thousand position
  4954.                                                 ;reached ?
  4955.   0129D C0              RET     NZ              ;No: done, return
  4956.  
  4957.   0129E 362C            LD      (HL),','        ;Yes, put ',' into buffer
  4958.   012A0 23              INC     HL              ;Pointer + 1
  4959.   012A1 0E03            LD      C,03H           ;Counter = 3 for next
  4960.                                                 ;thousands position
  4961.   012A3 C9              RET
  4962.  
  4963.  
  4964. ; Convert floating point number to unformatted string
  4965.  
  4966.   012A4 D5              PUSH    DE              ;Save DE
  4967.   012A5 E7              RST     20H             ;TSTTYP
  4968.                                                 ;SNG ?
  4969.   012A6 E2EA12          JP      PO,12EAH        ;Yes: continue at 12EAH
  4970.  
  4971.  
  4972. ; X in DBL format (and 1D+15 <= X < 1D+16 !)
  4973. ; Generate unformatted string with 17 positions
  4974. ; (10 positions using DBL-mantissas, 2 positions using SNG-mantissas
  4975. ; and 5 positions with INT-format)
  4976.  
  4977.   012A9 C5              PUSH    BC              ;Save position counters
  4978.   012AA E5              PUSH    HL              ;Save buffer pointer
  4979.   012AB CDFC09          CALL    09FCH           ;Y = X
  4980.   012AE 217C13          LD      HL,137CH        ;HL -> Constant 0.5 (DBL)
  4981.   012B1 CDF709          CALL    09F7H           ;X = (HL) = 0.5
  4982.   012B4 CD770C          CALL    0C77H           ;X = X + Y = 0.5 + Y
  4983.                                                 ;(Round up X)
  4984.   012B7 AF              XOR     A               ;C-flag = 0
  4985.   012B8 CD7B0B          CALL    0B7BH           ;Clear positions behind the
  4986.                                                 ;decimal point
  4987.   012BB E1              POP     HL              ;Restore buffer pointer
  4988.   012BC C1              POP     BC              ;Restore position counters
  4989.   012BD 118C13          LD      DE,138CH        ;DE -> DBL-mantissas
  4990.   012C0 3E0A            LD      A,0AH           ;A = counter (10 DBL-mantissas
  4991.                                                 ;from (DE) onwards)
  4992.   012C2 CD9112          CALL    1291H           ;Set ',' and '.'
  4993.   012C5 C5              PUSH    BC              ;Save position counters
  4994.   012C6 F5              PUSH    AF              ;Save mantissa counter
  4995.   012C7 E5              PUSH    HL              ;Save buffer pointer
  4996.   012C8 D5              PUSH    DE              ;Save mantissa pointer
  4997.   012C9 062F            LD      B,2FH           ;B = ASCII '0' - 1
  4998.   012CB 04              INC     B               ;Next digit
  4999.   012CC E1              POP     HL              ;HL = mantissa pointer
  5000.   012CD E5              PUSH    HL              ;Save it again
  5001.   012CE CD480D          CALL    0D48H           ;X = X - (HL): subtract
  5002.                                                 ;mantissa.
  5003.                                                 ;Underflow ?
  5004.   012D1 30F8            JR      NC,12CBH        ;No: next digit
  5005.  
  5006.   012D3 E1              POP     HL              ;Yes: restore mantissa pointer
  5007.   012D4 CD360D          CALL    0D36H           ;X = X + (HL): reverse last
  5008.                                                 ;subtract
  5009.   012D7 EB              EX      DE,HL           ;DE = mantissa pointer
  5010.   012D8 E1              POP     HL              ;HL = buffer pointer
  5011.   012D9 70              LD      (HL),B          ;Insert digit in buffer
  5012.   012DA 23              INC     HL              ;Update buffer pointer to next
  5013.                                                 ;position
  5014.   012DB F1              POP     AF              ;Restore maintissa counter
  5015.   012DC C1              POP     BC              ;Restore position counters
  5016.   012DD 3D              DEC     A               ;Mantissa counter - 1
  5017.   012DE 20E2            JR      NZ,12C2H        ;Next decimal position
  5018.  
  5019.   012E0 C5              PUSH    BC              ;Save position counters
  5020.   012E1 E5              PUSH    HL              ;Save buffer pointer
  5021.   012E2 211D41          LD      HL,411DH        ;HL -> X (DBL)
  5022.   012E5 CDB109          CALL    09B1H           ;X = BCDE = (HL) (SNG)
  5023.                                                 ;Shift the remaining LSBs of
  5024.                                                 ;the DBL number into X as a
  5025.                                                 ;SNG number
  5026.   012E8 180C            JR      12F6H           ;The remaining decimal
  5027.                                                 ;positions are processed in
  5028.                                                 ;SNG format (because now
  5029.                                                 ;X < 1D+6)
  5030.  
  5031. ; X in SNG format (and 1E+5 <= X < 1E+6 !)
  5032. ; Generate unformatted string with 7 positions
  5033. ; (2 positions using SNG-mantissas and the remaining 5 positions with
  5034. ; INT-format)
  5035.  
  5036.   012EA C5              PUSH    BC              ;Save position counters
  5037.   012EB E5              PUSH    HL              ;Save buffer pointers
  5038.   012EC CD0807          CALL    0708H           ;X = X + 0.5 (adjust mantissa)
  5039.   012EF 3C              INC     A               ;A <> 0 (for 0AFBH)
  5040.   012F0 CDFB0A          CALL    0AFBH           ;Clear all positions behind
  5041.                                                 ;the decimal point
  5042.   012F3 CDB409          CALL    09B4H           ;X = BCDE
  5043.                                                 ;(BCDE is the result of 0AFBH)
  5044.   012F6 E1              POP     HL              ;Restore buffer pointer
  5045.   012F7 C1              POP     BC              ;Restore position counters
  5046.   012F8 AF              XOR     A               ;C-flag = 0
  5047.   012F9 11D213          LD      DE,13D2H        ;DE -> mantissas
  5048.   012FC 3F              CCF                     ;C-flag = 1 at first itteration
  5049.                                                 ;after that C-flag = 0
  5050.   012FD CD9112          CALL    1291H           ;Set ',' and '.'
  5051.   01300 C5              PUSH    BC              ;Save position counters
  5052.   01301 F5              PUSH    AF              ;Save repeat flag
  5053.   01302 E5              PUSH    HL              ;Save buffer pointer
  5054.   01303 D5              PUSH    DE              ;Save mantissa pointer
  5055.   01304 CDBF09          CALL    09BFH           ;BCDE = X
  5056.   01307 E1              POP     HL              ;Mantissa pointer back to HL
  5057.   01308 062F            LD      B,2FH           ;B = '0' - 1
  5058.   0130A 04              INC     B               ;next digit
  5059.   0130B 7B              LD      A,E             ;CDE = CDE - (HL):
  5060.   0130C 96              SUB     (HL)            ;Subtract mantissa
  5061.   0130D 5F              LD      E,A
  5062.   0130E 23              INC     HL
  5063.   0130F 7A              LD      A,D
  5064.   01310 9E              SBC     A,(HL)
  5065.   01311 57              LD      D,A
  5066.   01312 23              INC     HL
  5067.   01313 79              LD      A,C
  5068.   01314 9E              SBC     A,(HL)
  5069.   01315 4F              LD      C,A
  5070.   01316 2B              DEC     HL              ;HL -> start of mantissa
  5071.   01317 2B              DEC     HL              ;Underflow ?
  5072.   01318 30F0            JR      NC,130AH        ;No: next digit
  5073.  
  5074.   0131A CDB707          CALL    07B7H           ;CDE= CDE + (HL):
  5075.                                                 ;reverse last subtract
  5076.   0131D 23              INC     HL              ;Mantissa pointer + 1
  5077.   0131E CDB409          CALL    09B4H           ;X = BCDE: new value to X
  5078.   01321 EB              EX      DE,HL           ;DE = mantissa pointer
  5079.   01322 E1              POP     HL              ;Restore buffer pointer
  5080.   01323 70              LD      (HL),B          ;Store digit
  5081.   01324 23              INC     HL              ;Buffer pointer + 1
  5082.   01325 F1              POP     AF              ;Restore repeat flag
  5083.   01326 C1              POP     BC              ;Restore position counters
  5084.                                                 ;Repeat ?
  5085.   01327 38D3            JR      C,12FCH         ;Yes: back to 12FCH (there are
  5086.                                                 ;only 2 mantissas in the SNG
  5087.                                                 ;format)
  5088.   01329 13              INC     DE              ;Mantissa pointer + 2
  5089.   0132A 13              INC     DE              ;(for INT processing)
  5090.   0132B 3E04            LD      A,04H           ;Process 4 mantissas
  5091.                                                 ;(the first mantissa with 10000
  5092.                                                 ;is also in the SNG format)
  5093.   0132D 1806            JR      1335H           ;Process remaining 4 digits
  5094.                                                 ;in INT-format
  5095. ; X in INT-format (and 0 <= X < 32768 !)
  5096. ; Generate unfomratted string with 5 positions
  5097.  
  5098.   0132F D5              PUSH    DE              ;Save DE
  5099.   01330 11D813          LD      DE,13D8H        ;DE -> Mantissas
  5100.   01333 3E05            LD      A,05H           ;5 Mantissas
  5101.   01335 CD9112          CALL    1291H           ;Set '.' and ','
  5102.   01338 C5              PUSH    BC              ;Save position counters
  5103.   01339 F5              PUSH    AF              ;Save mantissa counter
  5104.   0133A E5              PUSH    HL              ;Save buffer pointer
  5105.   0133B EB              EX      DE,HL           ;HL = mantissa pointer
  5106.   0133C 4E              LD      C,(HL)          ;BC = mantissa
  5107.   0133D 23              INC     HL
  5108.   0133E 46              LD      B,(HL)
  5109.   0133F C5              PUSH    BC              ;Save mantissa
  5110.   01340 23              INC     HL              ;HL -> next mantissa
  5111.   01341 E3              EX      (SP),HL         ;Save mantissa pointer,
  5112.                                                 ;HL = mantissa
  5113.   01342 EB              EX      DE,HL           ;DE = mantissa
  5114.   01343 2A2141          LD      HL,(4121H)      ;HL = X
  5115.   01346 062F            LD      B,2FH           ;B = ASCII '0' - 1
  5116.  
  5117.   01348 04              INC     B               ;Next digit
  5118.   01349 7D              LD      A,L             ;HL = HL - DE
  5119.   0134A 93              SUB     E
  5120.   0134B 6F              LD      L,A
  5121.   0134C 7C              LD      A,H
  5122.   0134D 9A              SBC     A,D
  5123.   0134E 67              LD      H,A             ;Underflow ?
  5124.   0134F 30F7            JR      NC,1348H        ;No: next digit
  5125.                                                 ;Yes:
  5126.   01351 19              ADD     HL,DE           ;Reverse subtract
  5127.   01352 222141          LD      (4121H),HL      ;and store new value in X
  5128.   01355 D1              POP     DE              ;Restore mantissa pointer
  5129.   01356 E1              POP     HL              ;Restore buffer pointer
  5130.   01357 70              LD      (HL),B          ;Insert digit
  5131.   01358 23              INC     HL              ;Buffer pointer + 1
  5132.   01359 F1              POP     AF              ;Restore mantissa counter
  5133.   0135A C1              POP     BC              ;Restore position counters
  5134.   0135B 3D              DEC     A               ;Mantissa counter - 1
  5135.   0135C 20D7            JR      NZ,1335H        ;Next mantissa
  5136.  
  5137.   0135E CD9112          CALL    1291H           ;Set ',' and '.'
  5138.   01361 77              LD      (HL),A          ;Terminate string with 00H
  5139.   01362 D1              POP     DE              ;Restore DE
  5140.   01363 C9              RET
  5141.  
  5142.  
  5143. ; Floating point constants
  5144.  
  5145.   01364 00              DEFB    00H             ;1D+10
  5146.   01365 00              DEFB    00H
  5147.   01366 00              DEFB    00H
  5148.   01367 00              DEFB    00H
  5149.   01368 F9              DEFB    F9H             ;1E+10
  5150.   01369 02              DEFB    02H
  5151.   0136A 15              DEFB    15H
  5152.   0136B A2              DEFB    A2H
  5153.  
  5154.   0136C FD              DEFB    FDH             ;1D+15
  5155.   0136D FF              DEFB    FFH
  5156.   0136E 9F              DEFB    9FH
  5157.   0136F 31              DEFB    31H
  5158.   01370 A9              DEFB    A9H             ;1E+15
  5159.   01371 5F              DEFB    5FH
  5160.   01372 63              DEFB    63H
  5161.   01373 B2              DEFB    B2H
  5162.  
  5163.   01374 FE              DEFB    FEH             ;1D+16
  5164.   01375 FF              DEFB    FFH
  5165.   01376 03              DEFB    03H
  5166.   01377 BF              DEFB    BFH
  5167.   01378 C9              DEFB    C9H             ;1E+16
  5168.   01379 1B              DEFB    1BH
  5169.   0137A 0E              DEFB    0EH
  5170.   0137B B6              DEFB    B6H
  5171.  
  5172.   0137C 00              DEFB    00H             ;0.5 (DBL)
  5173.   0137D 00              DEFB    00H
  5174.   0137E 00              DEFB    00H
  5175.   0137F 00              DEFB    00H
  5176.   01380 00              DEFB    00H             ;0.5 (SNG)
  5177.   01381 00              DEFB    00H
  5178.   01382 00              DEFB    00H
  5179.   01383 80              DEFB    80H
  5180.  
  5181.   01384 00              DEFB    00H             ;1D+16
  5182.   01385 00              DEFB    00H
  5183.   01386 04              DEFB    04H
  5184.   01387 BF              DEFB    BFH
  5185.   01388 C9              DEFB    C9H             ;1E+16
  5186.   01389 1B              DEFB    1BH
  5187.   0138A 0E              DEFB    0EH
  5188.   0138B B6              DEFB    B6H
  5189.  
  5190. ; Fixed point constants (mantissas for number conversion)
  5191.  
  5192. ;       LSB        MSB
  5193.  
  5194.                                 ;1000000000000000
  5195.  
  5196.   0138C 0080C6A47E8D03  DEFB    00H,80H,C6H,A4H,7EH,8DH,03H     ;1D+15
  5197.  
  5198.                                 ;100000000000000
  5199.  
  5200.   01393 00407A10F35A00  DEFB    00H,40H,7AH,10H,F3H,5AH,00H     ;1D+14
  5201.  
  5202.                                 ;10000000000000
  5203.  
  5204.   0139A 00A0724E180900  DEFB    00H,A0H,72H,4EH,18H,09H,00H     ;1D+13
  5205.  
  5206.                                 ;1000000000000
  5207.  
  5208.   013A1 0010A5D4E80000  DEFB    00H,10H,A5H,D4H,E8H,00H,00H     ;1D+12
  5209.  
  5210.                                 ;100000000000
  5211.  
  5212.   013A8 00E87648170000  DEFB    00H,E8H,76H,48H,17H,00H,00H     ;1D+11
  5213.  
  5214.                                 ;10000000000
  5215.  
  5216.   013AF 00E40B54020000  DEFB    00H,E4H,0BH,54H,02H,00H,00H     ;1D+10
  5217.  
  5218.                                 ;1000000000
  5219.  
  5220.   013B6 00CA9A3B000000  DEFB    00H,CAH,9AH,3BH,00H,00H,00H     ;1D+9
  5221.  
  5222.                                 ;100000000
  5223.  
  5224.   013BD 00E1F505000000  DEFB    00H,E1H,F5H,05H,00H,00H,00H     ;1D+8
  5225.  
  5226.                                 ;10000000
  5227.  
  5228.   013C4 80969800000000  DEFB    80H,96H,98H,00H,00H,00H,00H     ;1D+7
  5229.  
  5230.                                 ;1000000
  5231.  
  5232.   013CB 40420F00000000  DEFB    40H,42H,0FH,00H,00H,00H,00H     ;1D+6
  5233.  
  5234.                                 ;100000
  5235.  
  5236.   013D2 A08601          DEFB    A0H,86H,01H                     ;1E+5
  5237.  
  5238.                                 ;10000
  5239.  
  5240.   013D5 102700          DEFB    10H,27H,00H                     ;1E+4
  5241.  
  5242.                                 ;1000
  5243.  
  5244.   013D8 E803            DEFB    E8H,03H                         ;INT
  5245.  
  5246.                                 ;100
  5247.  
  5248.   013DC 6400            DEFB    64H,00H
  5249.  
  5250.                                 ;10
  5251.  
  5252.   013DE 0A00            DEFB    0AH,00H
  5253.  
  5254.                                 ;1
  5255.  
  5256.   013E0 0100            DEFB    01H,00H
  5257.  
  5258.  
  5259. ; SUB for SQR and ATN
  5260. ; Negate the result of the routine (X = -X)
  5261.  
  5262.   013E2 218209          LD      HL,0982H        ;HL -> Routine for X = -X
  5263.   013E5 E3              EX      (SP),HL         ;Save HL as RET address on
  5264.                                                 ;stack
  5265.   013E6 E9              JP      (HL)            ;Back to caller (SQR or ATN)
  5266.  
  5267.  
  5268. ; X = SQR ( X ) = X ^ 0.5
  5269. ; -----------------------
  5270. ;
  5271.  
  5272.   013E7 CDA409          CALL    09A4H           ;(SP) = X
  5273.   013EA 218013          LD      HL,1380H        ;HL -> constant 0.5
  5274.   013ED CDB109          CALL    09B1H           ;X = BCDE = (HL)
  5275.   013F0 1803            JR      13F5H           ;Compute (SP) ^ X
  5276.  
  5277. ; X = (SP) ^ X
  5278.  
  5279.   013F2 CDB10A          CALL    0AB1H           ;X = CSNG (X) (Exponent)
  5280.   013F5 C1              POP     BC              ;BCDE = (SP) (Base)
  5281.   013F6 D1              POP     DE
  5282.   013F7 CD5509          CALL    0955H           ;TEST2
  5283.   013FA 78              LD      A,B             ;A = Exp (Base)
  5284.                                                 ;Exponent zero ?
  5285.   013FB 283C            JR      Z,1439H         ;Yes: compute Exp(0) = 1
  5286.                                                 ;Exponent > 0
  5287.   013FD F20414          JP      P,1404H         ;Yes: continue at 1404H
  5288.  
  5289.   01400 B7              OR      A               ;Base = zero ?
  5290.                                                 ;(with negative exponent)
  5291.   01401 CA9A19          JP      Z,199AH         ;Yes: ?/0 Error
  5292.  
  5293.   01404 B7              OR      A               ;Base = zero ?
  5294.                                                 ;(with negative exponent)
  5295.   01405 CA7907          JP      Z,0779H         ;Yes: ?/0 Error
  5296.  
  5297.   01408 D5              PUSH    DE              ;Save Base
  5298.   01409 C5              PUSH    BC
  5299.   0140A 79              LD      A,C             ;A = MSB (Base)
  5300.   0140B F67F            OR      7FH             ;Test sign
  5301.   0140D CDBF09          CALL    09BFH           ;BCDE = X
  5302.                                                 ;Positive Base ?
  5303.   01410 F22114          JP      P,1421H         ;Yes: continue at 1421H
  5304.  
  5305.   01413 D5              PUSH    DE              ;Save Exponent
  5306.   01414 C5              PUSH    BC
  5307.   01415 CD400B          CALL    0B40H           ;X = INT (X) = INT ( Exponent)
  5308.   01418 C1              POP     BC              ;Restore exponent
  5309.   01419 D1              POP     DE
  5310.   0141A F5              PUSH    AF              ;Save LSB (X)
  5311.   0141B CD0C0A          CALL    0A0CH           ;CP X,BCDE
  5312.                                                 ;CP INT(Exponent), Exponent
  5313.                                                 ;Exponent an integer ?
  5314.   0141E E1              POP     HL              ;Restore LSB (X)
  5315.   0141F 7C              LD      A,H             ;A = LSB (X) = INT (Exponent)
  5316.                                                 ;(because exponent < 88)
  5317.   01420 1F              RRA                     ;C-flag = A, bit 0 (lowest bit
  5318.                                                 ;of the exponent)
  5319.                                                 ;Exponent odd ?
  5320.   01421 E1              POP     HL              ;X = (SP) = Base
  5321.   01422 222341          LD      (4123H),HL
  5322.   01425 E1              POP     HL
  5323.   01426 222141          LD      (4121H),HL
  5324.                                                 ;Exponent odd ?
  5325.   01429 DCE213          CALL    C,13E2H         ;Yes: negate result when
  5326.                                                 ;exponent is odd with negative
  5327.                                                 ;base
  5328.                                                 ;Exponent an integer ?
  5329.   0142C CC8209          CALL    Z,0982H         ;Yes: make base positive when
  5330.                                                 ;exponent is integer.
  5331.   0142F D5              PUSH    DE              ;Save Exponent
  5332.   01430 C5              PUSH    BC
  5333.   01431 CD0908          CALL    0809H           ;X = LOG (X) (logaritm of Base)
  5334.   01434 C1              POP     BC              ;Restore Exponent
  5335.   01435 D1              POP     DE
  5336.   01436 CD4708          CALL    0847H           ;X = BCDE * X
  5337.                                                 ;  = Exponent * LOG (Base)
  5338.                                                 ;Now compute the following:
  5339.                                                 ;EXP ( Exponent * LOG (Base) )
  5340. ; X = EXP ( X )
  5341. ; -------------
  5342. ; Only computable for -88.7228 <= X <= 87.3365
  5343.  
  5344.   01439 CDA409          CALL    09A4H           ;(SP) = X: Save argument
  5345.   0143C 013881          LD      BC,8138H        ;BCDE = 1/4427 = 1 / LOG(2)
  5346.   0143F 113BAA          LD      DE,0AA3BH
  5347.   01442 CD4708          CALL    0847H           ;X = BCDE * argument
  5348.                                                 ;  = argument / LOG(2)
  5349.   01445 3A2441          LD      A,(4124H)       ;A = Exp (X)
  5350.   01448 FE88            CP      88H             ;Exp (X) >= 88H  ?
  5351.                                                 ;means: X >= 2 ^ 8  ?
  5352.                                                 ;means: argument/LOG(2) > 127
  5353.                                                 ;(or argument/LOG(2) < -128)  ?
  5354.                                                 ;means: argument > 88.0297
  5355.                                                 ;(or argument < -88.7228) ?
  5356.   0144A D23109          JP      NC,0931H        ;Yes: ?OV Error if
  5357.                                                 ;argument > 88.0297,
  5358.                                                 ;Result = 0 if
  5359.                                                 ;argument < 88.7228)
  5360.   0144D CD400B          CALL    0B40H           ;A = X = INT (X)
  5361.                                                 ;( X = INT( argument/LOG(2) ) )
  5362.                                                 ;(   = 2-exponent of result )
  5363.   01450 C680            ADD     A,80H           ;A > 7DH ? (A + 82H > FFH)
  5364.   01452 C602            ADD     A,02H           ;means: INT(arg./LOG(2)) > 125?
  5365.                                                 ;means X > 87.3365 ?
  5366.   01454 DA3109          JP      C,0931H         ;Yes: ?OV Error
  5367.                                                 ;No:
  5368.   01457 F5              PUSH    AF              ;Save 2-exponent + offset (80H)
  5369.                                                 ;+ 2
  5370.   01458 21F807          LD      HL,07F8H        ;HL -> Constant 1 (SNG)
  5371.   0145B CD0B07          CALL    070BH           ;X = X + HL = X + 1
  5372.   0145E CD4108          CALL    0841H           ;X = X * LOG(2)
  5373.   01461 F1              POP     AF              ;Restore 2-exponent
  5374.   01462 C1              POP     BC              ;BCDE = (SP) = argument
  5375.   01463 D1              POP     DE
  5376.   01464 F5              PUSH    AF              ;Save 2-exponent
  5377.   01465 CD1307          CALL    0713H           ;X = BCDE - X
  5378.   01468 CD8209          CALL    0982H           ;X = -X
  5379.   0146B 217914          LD      HL,1479H        ;HL -> coefficients
  5380.   0146E CDA914          CALL    14A9H           ;Series calculation 2
  5381.   01471 110000          LD      DE,0000H        ;DE = 0000H
  5382.   01474 C1              POP     BC              ;B = 2-exponent + 2
  5383.   01475 4A              LD      C,D             ;C = 00H
  5384.   01476 C34708          JP      0847H           ;X = BCDE * X
  5385.                                                 ;  = (2 ^ 2 + 2-exponent) * X
  5386.  
  5387. ; Coefficients for EXP
  5388. ; '!' means faculty of the number (3! = 1 * 2 * 3)
  5389.  
  5390.   01479 08              DEFB    08H             ;8 coefficients
  5391.  
  5392.   0147A 40              DEFB    40H             ;-1.41361E-4 = -1/7076
  5393.   0147B 2E              DEFB    2EH             ;approx. -1/5040 = -1/7!
  5394.   0147C 94              DEFB    94H
  5395.   0147D 74              DEFB    74H
  5396.  
  5397.   0147E 70              DEFB    70H             ;1.32988E-3 = 1/752
  5398.   0147F 4F              DEFB    4FH             ;approx. 1/720 = 1/6!
  5399.   01480 2E              DEFB    2EH
  5400.   01481 77              DEFB    77H
  5401.  
  5402.   01482 6E              DEFB    6EH             ;=8.30136E-3 = -1/120
  5403.   01483 02              DEFB    02H                          = -1/5!
  5404.   01484 88              DEFB    88H
  5405.   01485 7A              DEFB    7AH
  5406.  
  5407.   01486 E6              DEFB    E6H             ;0.0416574 = 1/24
  5408.   01486 A0              DEFB    A0H                        = 1/4!
  5409.   01488 2A              DEFB    2AH
  5410.   01489 7C              DEFB    7CH
  5411.  
  5412.   0148A 50              DEFB    50H             ;-0.166665 = -1/6
  5413.   0148B AA              DEFB    AAH                        = -1/3!
  5414.   0148C AA              DEFB    AAH
  5415.   0148D 7E              DEFB    7EH
  5416.  
  5417.   0148E FF              DEFB    FFH             ;0.5 = 1/2
  5418.   0148F FF              DEFB    FFH                  = 1/2!
  5419.   01490 7F              DEFB    7FH
  5420.   01491 7F              DEFB    7FH
  5421.  
  5422.   01492 00              DEFB    00H             ;-1 = -1/1!
  5423.   01493 00              DEFB    00H
  5424.   01494 80              DEFB    80H
  5425.   01495 81              DEFB    81H
  5426.  
  5427.   01496 00              DEFB    00H             ;1
  5428.   01497 00              DEFB    00H
  5429.   01498 00              DEFB    00H
  5430.   01499 81              DEFB    81H
  5431.  
  5432. ; Series calculation 1
  5433. ; Calculate Taylor-series of the following form:
  5434. ;
  5435. ; y = k1*x + k2*x*x*x + k3*x*x*x*x*x... (k1, k2, k3 are coefficients)
  5436. ;
  5437. ; I: HL -> coefficients table
  5438. ;          The first byte of the table indicates the number of coefficients
  5439. ;          in the table. Then the coefficients follow, in an inverted
  5440. ;          order (k1 last)
  5441. ;    X = factor in series (x in the example)
  5442. ; O: X = result of series calculation (y in the example)
  5443.  
  5444.   0149A CDA409          CALL    09A4H           ;(SP) = X
  5445.   0149D 11320C          LD      DE,0C32H        ;DE = address of X = X * (SP)
  5446.   014A0 D5              PUSH    DE              ;Save as new RET address
  5447.   014A1 E5              PUSH    HL              ;Save table pointer
  5448.   014A2 CDBF09          CALL    09BFH           ;BCDE = X
  5449.   014A5 CD4708          CALL    0847H           ;X = BCDE * X = X * X
  5450.   014A8 E1              POP     HL              ;Restore table pointer and
  5451.                                                 ;Series calulation 2 using
  5452.                                                 ;X * X and multiply result
  5453.                                                 ;again with X
  5454. ; Series calculation 2
  5455. ; Calculate Taylor-series of the following form:
  5456. ;
  5457. ; y = k1 + k2*x + k3*x*x*x... (k1, k2, k3 are coefficients)
  5458. ;
  5459. ; I: HL -> coefficients table
  5460. ;          The first byte of the table indicates the number of coefficients
  5461. ;          in the table. Then the coefficients follow, in an inverted
  5462. ;          order (k1 last)
  5463. ;    X = factor in series (x in the example)
  5464. ; O: X = result of series calculation (y in the example)
  5465.  
  5466.   014A9 CDA409          CALL    09A4H           ;(SP) = X
  5467.   014AC 7E              LD      A,(HL)          ;A = number of coefficients
  5468.   014AD 23              INC     HL              ;HL -> 1st number (last
  5469.                                                 ;coefficient in the series)
  5470.   014AE CDB109          CALL    09B1H           ;X = BCDE = (HL) = 1st coeff.
  5471.   014B1 06F1            LD      B,0F1H          ;--
  5472. * 014B1   F1            POP     AF              ;Restore counter
  5473.   014B3 C1              POP     BC              ;BCDE = X
  5474.   014B4 D1              POP     DE
  5475.   014B5 3D              DEC     A               ;Any more coefficients ?
  5476.   014B6 C8              RET     Z               ;No: done, return
  5477.  
  5478.   014B7 D5              PUSH    DE              ;Save BCDE
  5479.   014B8 C5              PUSH    BC
  5480.   014B9 F5              PUSH    AF              ;Save counter
  5481.   014BA E5              PUSH    HL              ;Save pointer
  5482.   014BB CD4708          CALL    0847H           ;Result = result + BCDE
  5483.   014BE E1              POP     HL              ;Restore pointer
  5484.   014BF CDC209          CALL    09C2H           ;BCDE = (HL) = coefficient
  5485.   014C2 E5              PUSH    HL              ;Save pointer
  5486.   014C3 CD1607          CALL    0716H           ;Result = result + BCDE
  5487.   014C6 E1              POP     HL              ;Restore pointer
  5488.   014C7 18E9            JR      14B2H           ;Calculate next term
  5489.  
  5490.  
  5491. ; X = RND ( X )
  5492. ; -------------
  5493. ; For X > 1: RND ( X ) = INT( RND(0) * INT(X) + 1 )
  5494.  
  5495.   014C9 CD7F0A          CALL    0A7FH           ;X = HL = CINT( argument )
  5496.   014CC 7C              LD      A,H             ;Argument negative ?
  5497.   014CD B7              OR      A
  5498.   014CE FA4A1E          JP      M,1E4AH         ;Yes: ?FC Error
  5499.  
  5500.   014D1 B5              OR      L               ;Argument = 0 ?
  5501.   014D2 CAF014          JP      Z,14F0H         ;Yes: compute RND (0)
  5502.  
  5503.   014D5 E5              PUSH    HL              ;Save argument
  5504.   014D6 CDF014          CALL    14F0H           ;Compute RND (0)
  5505.   014D9 CDBF09          CALL    09BFH           ;BCDE = X = RND (0)
  5506.   014DC EB              EX      DE,HL           ;(SP) = BCDE, HL = argument
  5507.   014DD E3              EX      (SP),HL
  5508.   014DE C5              PUSH    BC
  5509.   014DF CDCF0A          CALL    0ACFH           ;X = CSNG (HL)
  5510.   014E2 C1              POP     BC              ;BCDE = (SP) = RND (0)
  5511.   014E3 D1              POP     DE
  5512.   014E4 CD4708          CALL    0847H           ;X = X * BCDE
  5513.                                                 ;  = argument * RND (0)
  5514.   014E7 21F807          LD      HL,07F8H        ;HL -> Constant 1 (SNG)
  5515.   014EA CD0B07          CALL    070BH           ;X = X + (HL) = X + 1
  5516.   014ED C3400B          JP      0B40H           ;X = CINT (X)
  5517.  
  5518.  
  5519. ; X = RND ( 0 )
  5520. ;   = last random number * 0.253514 + 0.022228 (ignore carry)
  5521.  
  5522.   014F0 219040          LD      HL,4090H        ;HL -> multiplicator
  5523.   014F3 E5              PUSH    HL              ;Save pointer
  5524.   014F4 110000          LD      DE,0000H        ;CDE = 000000H
  5525.   014F7 4B              LD      C,E             ;(Result in CDE)
  5526.   014F8 2603            LD      H,03H           ;H = byte counter
  5527.                                                 ;(3 bytes mantissa)
  5528.   014FA 2E08            LD      L,08H           ;L = bit counter
  5529.                                                 ;(8 bits per byte)
  5530.   014FC EB              EX      DE,HL           ;Shift CDE 1 bit to the left
  5531.   014FD 29              ADD     HL,HL           ;(for multiplication)
  5532.   014FE EB              EX      DE,HL
  5533.   014FF 79              LD      A,C
  5534.   01500 17              RLA
  5535.   01501 4F              LD      C,A
  5536.   01502 E3              EX      (SP),HL         ;Save counter, restore pointer
  5537.   01503 7E              LD      A,(HL)          ;Shift next bit of
  5538.   01504 07              RLCA                    ;multiplicator into C-flag
  5539.   01505 77              LD      (HL),A
  5540.   01506 E3              EX      (SP),HL         ;Save pointer, restore counter
  5541.                                                 ;Next bit = 1 ?
  5542.   01507 D21615          JP      NC,1516H        ;No: continue at 1516H
  5543.                                                 ;Yes:
  5544.   0150A E5              PUSH    HL              ;Save pointer
  5545.   0150B 2AAA40          LD      HL,(40AAH)      ;HL = LSBs of last random
  5546.                                                 ;number
  5547.   0150E 19              ADD     HL,DE           ;Add to result
  5548.   0150F EB              EX      DE,HL
  5549.   01510 3AAC40          LD      A,(40ACH)       ;A = MSB of last random number
  5550.   01513 89              ADC     A,C             ;Add to result
  5551.   01514 4F              LD      C,A
  5552.   01515 E1              POP     HL              ;Restore counter
  5553.   01516 2D              DEC     L               ;Bit counter - 1
  5554.                                                 ;All bits done ?
  5555.   01517 C2FC14          JP      NZ,14FCH        ;No: process next bit
  5556.                                                 ;Yes:
  5557.   0151A E3              EX      (SP),HL         ;Save counter, restore pointer
  5558.   0151B 23              INC     HL              ;Pointer + 1 (process next byte
  5559.                                                 ;of the multiplicator)
  5560.   0151C E3              EX      (SP),HL         ;Save pointer, restore counter
  5561.   0151D 25              DEC     H               ;Byte counter - 1
  5562.                                                 ;All 3 bytes processed ?
  5563.   0151E C2FA14          JP      NZ,14FAH        ;No: process next byte
  5564.                                                 ;Yes:
  5565.   01521 E1              POP     HL              ;Remove pointer from stack
  5566.   01522 2165B0          LD      HL,0B065H       ;and add 05B065H (=0.022228)
  5567.   01525 19              ADD     HL,DE           ;to mantissa
  5568.   01526 22AA40          LD      (40AAH),HL      ;Store new random number
  5569.   01529 CDEF0A          CALL    0AEFH           ;Set VT = SNG
  5570.   0152C 3E05            LD      A,05H           ;Adjust MSB
  5571.   0152E 89              ADC     A,C
  5572.   0152F 32AC40          LD      (40ACH),A       ;And store it
  5573.   01532 EB              EX      DE,HL           ;CDE = mantissa of new random
  5574.                                                 ;number
  5575.   01533 0680            LD      B,80H           ;Set exponent = 80H
  5576.   01535 212541          LD      HL,4125H        ;HL -> sign flag
  5577.   01538 70              LD      (HL),B          ;Sign-flag = 80H
  5578.                                                 ;(generate a positive number
  5579.                                                 ;at SFLOAT)
  5580.   01539 2B              DEC     HL              ;HL -> Exp (X)
  5581.   0153A 70              LD      (HL),B          ;Exp (X) = 80H (result < 1)
  5582.   0153B 4F              LD      C,A             ;Use MSB in BCDE
  5583.   0153C 0600            LD      B,00H           ;LSB of CDEB = 00H
  5584.   0153E C36507          JP      0765H           ;Convert CDEB into SNG format
  5585.                                                 ;and store in X (SFLOAT)
  5586.  
  5587. ; X = COS ( X )
  5588. ; -------------
  5589. ; X = COS (X) = SIN (X + PI / 2). PI = 3.14159
  5590.  
  5591.   01541 218B15          LD      HL,158BH        ;HL -> PI/2
  5592.   01544 CD0B07          CALL    070BH           ;X = argument + PI/2
  5593.  
  5594.  
  5595. ; X = SIN ( X )
  5596. ; -------------
  5597.  
  5598.   01547 CDA409          CALL    09A4H           ;(SP) = argument
  5599.   0154A 014983          LD      BC,8349H        ;BCDE = 6.28319 = 2*PI
  5600.   0154D 11DB0F          LD      DE,0FDBH
  5601.                                                 ;Sace X into the range of
  5602.                                                 ;-1 to 1
  5603.   01550 CDB409          CALL    09B4H           ;X = BCDE = 2*PI
  5604.   01553 C1              POP     BC              ;BCDE = argument
  5605.   01554 D1              POP     DE
  5606.   01555 CDA208          CALL    08A2H           ;X = X / BCDE = argument/(2*PI)
  5607.   01558 CDA409          CALL    09A4H           ;(SP) = X = argument/(2*PI)
  5608.   0155B CD400B          CALL    0B40H           ;X = INT (X)
  5609.                                                 ;  = INT ( argument / (2*PI) )
  5610.   0155E C1              POP     BC              ;BCDE = argument / (2*PI)
  5611.   0155F D1              POP     DE
  5612.   01560 CD1307          CALL    0713H           ;X = BCDE - X
  5613.                                                 ;= arg./(2*PI)-INT(arg./(2*PI))
  5614.                                                 ;(remove multiples of 2*PI)
  5615.                                                 ;The argument (X) is now
  5616.                                                 ;between -1 (corresponds to
  5617.                                                 ;-2*PI) and 1 (corresponds to
  5618.                                                 ;+2*PI)
  5619.   01563 218F15          LD      HL,158FH        ;HL -> 1/4
  5620.   01566 CD1007          CALL    0710H           ;X = (HL) - X = 1/4 - X
  5621.   01569 CD5509          CALL    0955H           ;TEST2: X >= 0 ? (arg. > PI/2)
  5622.   0156C 37              SCF                     ;C-flag = 1
  5623.   0156D F27715          JP      P,1577H         ;Yes: continue at 1577H
  5624.                                                 ;No:
  5625.   01570 CD0807          CALL    0708H           ;X = X + 1/2
  5626.   01573 CD5509          CALL    0955H           ;TEST2: X >= 0 ? (arg < PI/2)
  5627.   01576 B7              OR      A               ;C-flag = 0 (for 1582H)
  5628.   01577 F5              PUSH    AF              ;Save flags
  5629.   01578 F48209          CALL    P,0982H         ;Yes: X = -X
  5630.   0157B 218F15          LD      HL,158FH        ;HL -> 1/4
  5631.   0157E CD0B07          CALL    070BH           ;X = X + (HL) = X + 1/4
  5632.   01581 F1              POP     AF              ;Restore flags
  5633.   01582 D48209          CALL    NC,0982H        ;if (1/4-X) > 0 then X = -X
  5634.   01585 219315          LD      HL,1593H        ;HL -> coefficients
  5635.   01588 C39A14          JP      149AH           ;Calculate series 1
  5636.  
  5637.  
  5638.   0158B DB              DEFB    DBH             ;Constant 1.5708 = PI / 2
  5639.   0158C 0F              DEFB    0FH
  5640.   0158D 49              DEFB    49H
  5641.   0158E 81              DEFB    81H
  5642.  
  5643.   0158F 00              DEFB    00H             ;Constant 0.25 = 1/4
  5644.   01590 00              DEFB    00H
  5645.   01591 00              DEFB    00H
  5646.   01592 7F              DEFB    7FH
  5647.  
  5648.  
  5649. ; Coefficient table for SIN and COS
  5650. ; '!' means faculty of the number (3! = 1 * 2 * 3)
  5651.  
  5652.   01593 05              DEFB    05H             ;5 coefficients
  5653.  
  5654.   01594 BA              DEFB    BAH             ;39.7107
  5655.   01595 D7              DEFB    D7H             ;= ((2*PI) ^ 9) / 9!
  5656.   01596 1E              DEFB    1EH
  5657.   01597 86              DEFB    86H
  5658.  
  5659.   01598 64              DEFB    64H             ;-76.575
  5660.   01599 26              DEFB    26H             ;= - ((2*PI) ^ 7) / 7!
  5661.   0159A 99              DEFB    99H
  5662.   0159B 87              DEFB    87H
  5663.  
  5664.   0159C 58              DEFB    58H             ;81.6022
  5665.   0159D 34              DEFB    34H             ;= ((2*PI) ^ 5) / 5!
  5666.   0159E 23              DEFB    23H
  5667.   0159F 87              DEFB    87H
  5668.  
  5669.   015A0 E0              DEFB    E0H             ;-41.3417
  5670.   015A1 5D              DEFB    5DH             ;= - ((2*PI) ^ 3) / 3!
  5671.   015A2 A5              DEFB    A5H
  5672.   015A3 86              DEFB    86H
  5673.  
  5674.   015A4 DA              DEFB    DAH             ;6.28319 = 2*PI
  5675.   015A5 0F              DEFB    0FH             ;= ((2*PI) ^ 1) / 1!
  5676.   015A6 49              DEFB    49H
  5677.   015A7 83              DEFB    83H
  5678.  
  5679.  
  5680. ; X = TAN ( X )
  5681. ; -------------
  5682. ; X = TAN (X) = SIN (X) / COS (X)
  5683.  
  5684.   015A8 CDA409          CALL    09A4H           ;(SP) = argument
  5685.   015AB CD4715          CALL    1547H           ;X = SIN (argument)
  5686.   015AE C1              POP     BC              ;BCHL = argument
  5687.   015AF E1              POP     HL
  5688.   015B0 CDA409          CALL    09A4H           ;(SP) = X = SIN (argument)
  5689.   015B3 EB              EX      DE,HL           ;BCDE = argument
  5690.   015B4 CDB409          CALL    09B4H           ;X = BCDE = argument
  5691.   015B7 CD4115          CALL    1541H           ;X = COS (argument)
  5692.   015BA C3A008          JP      08A0H           ;Result = (SP) / X
  5693.                                                 ;= SIN (arg.) / COS (arg.)
  5694.  
  5695. ; X = ATN ( X )
  5696. ; -------------
  5697.  
  5698.   015BD CD5509          CALL    0955H           ;TEST2: argument < 0 ?
  5699.   015C0 FCE213          CALL    M,13E2H         ;Yes: negate restult afterwards
  5700.   015C3 FC8209          CALL    M,0982H         ;and continue processing with
  5701.                                                 ;positive argument
  5702.   015C6 3A2441          LD      A,(4124H)       ;A = Exp (argument)
  5703.   015C9 FE81            CP      81H             ;Argument < 1 ?
  5704.   015CB 380C            JR      C,15D9H         ;Yes: continue at 15D9H
  5705.  
  5706.   015CD 010081          LD      BC,8100H        ;BCDE = 1.0
  5707.   015D0 51              LD      D,C
  5708.   015D1 59              LD      E,C
  5709.   015D2 CDA208          CALL    08A2H           ;X = BCDE / X = 1 / argument
  5710.   015D5 211007          LD      HL,0710H        ;HL = address of X = (HL) - X
  5711.   015D8 E5              PUSH    HL              ;Save as new RET address
  5712.   015D9 21E315          LD      HL,15E3H        ;HL -> coefficient table
  5713.   015DC CD9A14          CALL    149AH           ;Calculate series 1
  5714.   015DF 218B15          LD      HL,158BH        ;HL -> PI/2
  5715.   015E2 C9              RET                     ;Result = PI/2 - X if
  5716.                                                 ;argument was > 1
  5717.                                                 ;(Series calculation is only
  5718.                                                 ;correct for -1 < X < 1)
  5719. ; Coefficients for ATN
  5720.  
  5721.   015E3 09              DEFB    09H             ;9 coefficients
  5722.  
  5723.   015E4 4A              DEFB    4AH             ;2.86623E-3     approx. 1/349
  5724.   015E5 D7              DEFB    D7H
  5725.   015E6 3B              DEFB    3BH
  5726.   015E7 78              DEFB    78H
  5727.  
  5728.   015E8 02              DEFB    02H             ;-0.0161657     approx. -1/62
  5729.   015E9 6E              DEFB    6EH
  5730.   015EA 84              DEFB    84H
  5731.   015EB 7B              DEFB    7BH
  5732.  
  5733.   015EC FE              DEFB    FEH             ;0.0429096      approx. 1/23
  5734.   015ED C1              DEFB    C1H
  5735.   015EE 2F              DEFB    2FH
  5736.   015EF 7C              DEFB    7CH
  5737.  
  5738.   015F0 74              DEFB    74H             ;-0.0752896     approx. -1/11
  5739.   015F1 31              DEFB    31H
  5740.   015F2 9A              DEFB    9AH
  5741.   015F3 7D              DEFB    7DH
  5742.  
  5743.   015F4 84              DEFB    84H             ;0.106563       approx. 1/9
  5744.   015F5 3D              DEFB    3DH
  5745.   015F6 5A              DEFB    5AH
  5746.   015F7 7D              DEFB    7DH
  5747.  
  5748.   015F8 C8              DEFB    C8H             ;-0.142089      approx. -1/7
  5749.   015F9 7F              DEFB    7FH
  5750.   015FA 91              DEFB    91H
  5751.   015FB 7E              DEFB    7EH
  5752.  
  5753.   015FC E4              DEFB    E4H             ;0.199936       = 1/5
  5754.   015FD BB              DEFB    BBH
  5755.   015FE 4C              DEFB    4CH
  5756.   015FF 7E              DEFB    7EH
  5757.  
  5758.   01600 6C              DEFB    6CH             ;-0.333331      = 1/3
  5759.   01601 AA              DEFB    AAH
  5760.   01602 AA              DEFB    AAH
  5761.   01603 7F              DEFB    7FH
  5762.  
  5763.   01604 00              DEFB    00H             ;1.0            = 1/1
  5764.   01605 00              DEFB    00H
  5765.   01606 00              DEFB    00H
  5766.   01607 81              DEFB    81H
  5767.  
  5768.  
  5769. ; Address table for Level II and Disk BASIC functions (tokens D7H to FAH)
  5770.  
  5771.   01608 8A09            DEFW    098AH           ;SNG
  5772.   0160A 370B            DEFW    0B37H           ;INT
  5773.   0160C 7709            DEFW    0977H           ;ABS
  5774.   0160E D427            DEFW    27D4H           ;FRE
  5775.   01610 EF2A            DEFW    2AEFH           ;INP
  5776.   01612 F527            DEFW    27F5H           ;POS
  5777.   01614 E713            DEFW    13E7H           ;SQR
  5778.   01616 C914            DEFW    14C9H           ;RND
  5779.   01618 0908            DEFW    0809H           ;LOG
  5780.   0161A 3914            DEFW    1439H           ;EXP
  5781.   0161C 4115            DEFW    1541H           ;COS
  5782.   0161E 4715            DEFW    1547H           ;SIN
  5783.   01620 A815            DEFW    15A8H           ;TAN
  5784.   01622 BD15            DEFW    15BDH           ;ATN
  5785.   01624 AA2C            DEFW    2CAAH           ;PEEK
  5786.   01626 5241            DEFW    4152H           ;CVI
  5787.   01628 5841            DEFW    4158H           ;CVS
  5788.   0162A 5E41            DEFW    415EH           ;CVD
  5789.   0162C 6141            DEFW    4161H           ;EOF
  5790.   0162E 6441            DEFW    4164H           ;LOC
  5791.   01630 6741            DEFW    4167H           ;LOF
  5792.   01632 6A41            DEFW    416AH           ;MKI$
  5793.   01634 6D41            DEFW    416DH           ;MKS$
  5794.   01636 7041            DEFW    4170H           ;MKD$
  5795.   01638 7F0A            DEFW    0A7FH           ;CINT
  5796.   0163A B10A            DEFW    0AB1H           ;CSNG
  5797.   0163C DB0A            DEFW    0ADBH           ;CDBL
  5798.   0163E 260B            DEFW    0B26H           ;FIX
  5799.   01640 032A            DEFW    2A03H           ;LEN
  5800.   01642 3628            DEFW    2836H           ;STR$
  5801.   01644 C52A            DEFW    2AC5H           ;VAL
  5802.   01646 0F2A            DEFW    2A0FH           ;ASC
  5803.   01648 1F2A            DEFW    2A1FH           ;CHR$
  5804.   01649 612A            DEFW    2A61H           ;LEFT$
  5805.   0164A 912A            DEFW    2A91H           ;RIGHT$
  5806.   0164E 9A2A            DEFW    2A9AH           ;MID$
  5807.  
  5808.  
  5809. ; Table for keywords of Level II and Disk BASIC
  5810.  
  5811.   01650 C54E44          DEFB    80H+'E','ND'            ;END
  5812.   01653 C64F52          DEFB    80H+'F','OR'            ;FOR
  5813.   01656 D245534554      DEFB    80H+'R','ESET'          ;RESET
  5814.   0165B D34554          DEFB    80H+'S','ET'            ;SET
  5815.   0165E C34C53          DEFB    80H+'C','LS'            ;CLS
  5816.   01661 C34D44          DEFB    80H+'C','MD'            ;CMD
  5817.   01664 D2414E444F4D    DEFB    80H+'R','ANDOM          ;RANDOM
  5818.   0166A CE455854        DEFB    80H+'N','EXT'           ;NEXT
  5819.   0166E C4415441        DEFB    80H+'D','ATA'           ;DATA
  5820.   01672 C94E505554      DEFB    80H+'I','NPUT'          ;INPUT
  5821.   01677 C4494D          DEFB    80H+'D','IM'            ;DIM
  5822.   0167A D2454144        DEFB    80H+'R','EAD'           ;READ
  5823.   0167E CC4554          DEFB    80H+'L','ET'            ;LET
  5824.   01681 C74F544F        DEFB    80H+'G','OTO'           ;GOTO
  5825.   01685 D2554E          DEFB    80H+'R','UN'            ;RUN
  5826.   01688 C946            DEFB    80H+'I','F'             ;IF
  5827.   0168A D24553544F5245  DEFB    80H+'R','ESTORE'        ;RESTORE
  5828.   01691 C74F535542      DEFB    80H+'G','OSUB'          ;GOSUB
  5829.   01696 D2455455524E    DEFB    80H+'R','ETURN'         ;RETURN
  5830.   0169C D2454D          DEFB    80H+'R','EM'            ;REM
  5831.   0169F D3544F50        DEFB    80H+'S','TOP'           ;STOP
  5832.   016A3 C54C5345        DEFB    80H+'E','LSE'           ;ELSE
  5833.   016A7 D4524F4E        DEFB    80H+'T','RON'           ;TRON
  5834.   016AB D4524F4646      DEFB    80H+'T','ROFF'          ;TROFF
  5835.   016B0 C44546535452    DEFB    80H+'D','EFSTR'         ;DEFSTR
  5836.   016B6 C44546494E54    DEFB    80H+'D','EFINT'         ;DEFINT
  5837.   016BC C44546534E47    DEFB    80H+'D','EFSNG'         ;DEFSNG
  5838.   016C2 C4454644424C    DEFB    80H+'D','EFDBL'         ;DEFDBL
  5839.   016C8 CC494E45        DEFB    80H+'L','INE'           ;LINE
  5840.   016CC C5444954        DEFB    80H+'E','DIT'           ;EDIT
  5841.   016D0 C552524F52      DEFB    80H+'E','RROR'          ;ERROR
  5842.   016D5 D24553554D45    DEFB    80H+'R','ESUME'         ;RESUME
  5843.   016DB CF5554          DEFB    80H+'O','UT'            ;OUT
  5844.   016DE CF4E            DEFB    80H+'O','N'             ;ON
  5845.   016E0 CF50454E        DEFB    80H+'O','PEN'           ;OPEN
  5846.   016E4 C649454C44      DEFB    80H+'F','IELD'          ;FIELD
  5847.   016E9 C74554          DEFB    80H+'G','ET'            ;GET
  5848.   016EC D05554          DEFB    80H+'P','UT'            ;PUT
  5849.   016EF C34C4F5345      DEFB    80H+'C','LOSE'          ;CLOSE
  5850.   016F4 CC4F4144        DEFB    80H+'L','OAD'           ;LOAD
  5851.   016F8 CD45524745      DEFB    80H+'M','ERGE'          ;MERGE
  5852.   016FD CE414D45        DEFB    80H+'N','AME'           ;NAME
  5853.   01701 CB494C4C        DEFB    80H+'K','ILL'           ;KILL
  5854.   01705 CC534554        DEFB    80H+'L','SET'           ;LSET
  5855.   01709 D2534554        DEFB    80H+'R','SET'           ;RSET
  5856.   0170D D3415645        DEFB    80H+'S','AVE'           ;SAVE
  5857.   01711 D3595354454D    DEFB    80H+'S','YSTEM'         ;SYSTEM
  5858.   01717 CC5052494E54    DEFB    80H+'L','PRINT'         ;LPRINT
  5859.   0171D C44546          DEFB    80H+'D','EF'            ;DEF
  5860.   01720 D04F4B45        DEFB    80H+'P','OKE'           ;POKE
  5861.   01724 D052494E54      DEFB    80H+'P','RINT'          ;PRINT
  5862.   01729 C34F4E54        DEFB    80H+'C','ONT'           ;CONT
  5863.   0172D CC495354        DEFB    80H+'L','IST'           ;LIST
  5864.   01731 CC4C495354      DEFB    80H+'L','LIST'          ;LLIST
  5865.   01736 C4454C455445    DEFB    80H+'D','ELETE'         ;DELETE
  5866.   0173C C155544F        DEFB    80H+'A','UTO'           ;AUTO
  5867.   01740 C34C454152      DEFB    80H+'C','LEAR'          ;CLEAR
  5868.   01745 C34C4F4144      DEFB    80H+'C','LOAD'          ;CLOAD
  5869.   0174A C353415645      DEFB    80H+'C','SAVE'          ;CSAVE
  5870.   0174F CE4557          DEFB    80H+'N','EW'            ;NEW
  5871.   01752 D4414228        DEFB    80H+'T','AB('           ;TAB(
  5872.   01756 D44F            DEFB    80H+'T','0'             ;TO
  5873.   01758 C64E            DEFB    80H+'F','N'             ;FN
  5874.   0175A D553494E47      DEFB    80H+'U','SING'          ;USING
  5875.   0175F D64152505452    DEFB    80H+'V','ARPTR'         ;VARPTR
  5876.   01765 D55352          DEFB    80H+'U','SR'            ;USR
  5877.   01768 C5524C          DEFB    80H+'E','RN'            ;ERN
  5878.   0176B C55252          DEFB    80H+'E','RR'            ;ERR
  5879.   0176E D35452494E4724  DEFB    80H+'S','TRING$'        ;STRING$
  5880.   01775 C94E535452      DEFB    80H+'I','NSTR'          ;INSTR
  5881.   0177A C34845434B      DEFB    80H+'C','HECK'          ;CHECK
  5882.   0177F D4494D4524      DEFB    80H+'T','IME$'          ;TIME$
  5883.   01784 CD454D          DEFB    80H+'M','EM'            ;MEM
  5884.   01787 C94E4B455924    DEFB    80H+'I','NKEY$'         ;INKEY$
  5885.   0178D D448454E        DEFB    80H+'T','HEN'           ;THEN
  5886.   01791 CE4F54          DEFB    80H+'N','OT'            ;NOT
  5887.   01794 D3544550        DEFB    80H+'S','TEP'           ;STEP
  5888.   01798 AB              DEFB    80H+'+'                 ;+
  5889.   01799 AD              DEFB    80H+'-'                 ;-
  5890.   0179A AA              DEFB    80H+'*'                 ;*
  5891.   0179B AF              DEFB    80H+'/'                 ;/
  5892.   0179C DB              DEFB    80H+'['                 ;[
  5893.   0179D C14E44          DEFB    80H+'A','ND'            ;AND
  5894.   017A0 CF52            DEFB    80H+'O','R'             ;OR
  5895.   017A2 BE              DEFB    80H+'>'                 ;>
  5896.   017A3 BD              DEFB    80H+'='                 ;=
  5897.   017A4 BC              DEFB    80H+'<'                 ;<
  5898.   017A5 D3474E          DEFB    80H+'S','GN'            ;SNG
  5899.   017A8 C94E54          DEFB    80H+'I','NT'            ;INT
  5900.   017AB C14253          DEFB    80H+'A','BS'            ;ABS
  5901.   017AE C65245          DEFB    80H+'F','RE'            ;FRE
  5902.   017B1 C94E50          DEFB    80H+'I','NP'            ;INP
  5903.   017B4 D04F53          DEFB    80H+'P','OS'            ;POS
  5904.   017B7 D35152          DEFB    80H+'S','QR'            ;SQR
  5905.   017BA D24E44          DEFB    80H+'R','ND'            ;RND
  5906.   017BD CC4F47          DEFB    80H+'L','OG'            ;LOG
  5907.   017C0 C55850          DEFB    80H+'E','XP'            ;EXP
  5908.   017C3 C34F53          DEFB    80H+'C','OS'            ;COS
  5909.   017C6 D3494E          DEFB    80H+'S','IN'            ;SIN
  5910.   017C9 D4414E          DEFB    80H+'T','AN'            ;TAN
  5911.   017CC C1544E          DEFB    80H+'A','TN'            ;ATN
  5912.   017CF D045454B        DEFB    80H+'P','EEK'           ;PEEK
  5913.   017D3 C35649          DEFB    80H+'C','VI'            ;CVI
  5914.   017D6 C35653          DEFB    80H+'C','VS'            ;CVS
  5915.   017D9 C35644          DEFB    80H+'C','VD'            ;CVD
  5916.   017DC C54F46          DEFB    80H+'E','OF'            ;EOF
  5917.   017DF CC4F43          DEFB    80H+'L','OC'            ;LOC
  5918.   017E2 CC4F46          DEFB    80H+'L','OF'            ;LOF
  5919.   017E5 CD4B4924        DEFB    80H+'M','KI$'           ;MKI$
  5920.   017E9 CD4B5324        DEFB    80H+'M','KS$'           ;MKS$
  5921.   017ED CD4B4424        DEFB    80H+'M','KD$'           ;MKD$
  5922.   017F1 C3494E54        DEFB    80H+'C','INT'           ;CINT
  5923.   017F5 C3534E47        DEFB    80H+'C','SNG'           ;CSNG
  5924.   017F9 C344424C        DEFB    80H+'C','DBL'           ;CDBL
  5925.   017FD C64958          DEFB    80H+'F','IX'            ;FIX
  5926.   01800 CC454E          DEFB    80H+'L','EN'            ;LEN
  5927.   01803 D3545224        DEFB    80H+'S','TR$'           ;STR$
  5928.   01807 D6414C          DEFB    80H+'V','AL'            ;VAL
  5929.   0180A C15343          DEFB    80H+'A','SC'            ;ASC
  5930.   0180D C3485224        DEFB    80H+'C','HR$'           ;CHR$
  5931.   01811 CC45465424      DEFB    80H+'L','EFT$'          ;LEFT$
  5932.   01816 D24947485424    DEFB    80H+'R','IGHT$'         ;RIGHT$
  5933.   0181C CD494424        DEFB    80H+'M','ID$'           ;MID$
  5934.   01820 A7              DEFB    80H+27H                 ;'
  5935.   01821 80              DEFB    80H                     ;End of table
  5936.  
  5937.  
  5938. ; Address table for Level II and Disk BASIC statements (tokens 80H to BBH)
  5939.  
  5940.   01822 AE1D            DEFW    1DAEH           ;END
  5941.   01824 A11C            DEFW    1CA1H           ;FOR
  5942.   01826 3801            DEFW    0138H           ;RESET
  5943.   01828 3501            DEFW    0135H           ;SET
  5944.   0182A C901            DEFW    01C9H           ;CLS
  5945.   0182C 7341            DEFW    4173H           ;CMD
  5946.   0182E D301            DEFW    01D3H           ;RANDOM
  5947.   01830 B622            DEFW    22B6H           ;NEXT
  5948.   01832 051F            DEFW    1F05H           ;DATA
  5949.   01834 9A21            DEFW    219AH           ;INPUT
  5950.   01836 0826            DEFW    2608H           ;DIM
  5951.   01838 EF21            DEFW    21EFH           ;READ
  5952.   0183A 211F            DEFW    1F21H           ;LET
  5953.   0183C C21E            DEFW    1EC2H           ;GOTO
  5954.   0183E A31E            DEFW    1EA3H           ;RUN
  5955.   01840 3920            DEFW    2039H           ;IF
  5956.   01842 911D            DEFW    1D91H           ;RESTORE
  5957.   01844 B11E            DEFW    1EB1H           ;GOSUB
  5958.   01846 DE1E            DEFW    1EDEH           ;RETURN
  5959.   01848 071F            DEFW    1F07H           ;REM
  5960.   0184A A91D            DEFW    1DA9H           ;STOP
  5961.   0184C 071F            DEFW    1F07H           ;ELSE
  5962.   0184E F71D            DEFW    1DF7H           ;TRON
  5963.   01850 F81D            DEFW    1DF8H           ;TROFF
  5964.   01852 001E            DEFW    1E00H           ;DEFSTR
  5965.   01854 031E            DEFW    1E03H           ;DEFINT
  5966.   01856 061E            DEFW    1E06H           ;DEFSNG
  5967.   01858 091E            DEFW    1E09H           ;DEFDBL
  5968.   0185A A341            DEFW    41A3H           ;LINE
  5969.   0185C 602E            DEFW    2E60H           ;EDIT
  5970.   0185E F41F            DEFW    1FF4H           ;ERROR
  5971.   01860 AF1F            DEFW    1FAFH           ;RESUME
  5972.   01862 FB2A            DEFW    2AFBH           ;OUT
  5973.   01864 6C1F            DEFW    1F6CH           ;ON
  5974.   01866 7941            DEFW    4179H           ;OPEN
  5975.   01868 7C41            DEFW    417CH           ;FIELD
  5976.   0186A 7F41            DEFW    417FH           ;GET
  5977.   0186C 8241            DEFW    4182H           ;PUT
  5978.   0186E 8541            DEFW    4185H           ;CLOSE
  5979.   01870 8841            DEFW    4188H           ;LOAD
  5980.   01872 8B41            DEFW    418BH           ;MERGE
  5981.   01874 8E41            DEFW    418EH           ;NAME
  5982.   01876 9141            DEFW    4191H           ;KILL
  5983.   01878 9741            DEFW    4197H           ;LSET
  5984.   0187A 9A41            DEFW    419AH           ;RSET
  5985.   0187C A041            DEFW    41A0H           ;SAVE
  5986.   0187E B202            DEFW    02B2H           ;SYSTEM
  5987.   01880 6720            DEFW    2067H           ;LPRINT
  5988.   01882 5B41            DEFW    415BH           ;DEF
  5989.   01884 B12C            DEFW    2CB1H           ;POKE
  5990.   01886 6F20            DEFW    206FH           ;PRINT
  5991.   01888 E41D            DEFW    1DE4H           ;CONT
  5992.   0188A 2E2B            DEFW    2B2EH           ;LIST
  5993.   0188C 292B            DEFW    2B29H           ;LLIST
  5994.   0188E C62B            DEFW    2BC6H           ;DELETE
  5995.   01890 0820            DEFW    2008H           ;AUTO
  5996.   01892 7A1E            DEFW    1E7AH           ;CLEAR
  5997.   01894 1F2C            DEFW    2C1FH           ;CLOAD
  5998.   01896 F52B            DEFW    2BF5H           ;CSAVE
  5999.   01898 491B            DEFW    1B49H           ;NEW
  6000.  
  6001.  
  6002. ; Prioritytable for operators
  6003. ; highest value corresponds with highest proirity
  6004.  
  6005.   0189A 79              DEFB    79H             ;+
  6006.   0189B 79              DEFB    79H             ;-
  6007.   0189C 7C              DEFB    7CH             ;*
  6008.   0189D 7C              DEFB    7CH             ;/
  6009.   0189E 7F              DEFB    7FH             ;Exponent
  6010.   0189F 50              DEFB    50H             ;AND
  6011.   018A0 46              DEFB    46H             ;OR
  6012.  
  6013.  
  6014. ; Address table for type conversion
  6015.  
  6016.   018A1 DB0A            DEFW    0ADBH           ;0ADBH: CDBL
  6017.   018A3 0000            DEFW    0000H           ;0000H: unused entry in table
  6018.   018A5 7F0A            DEFW    0A7FH           ;0A7FH: CINT
  6019.   018A7 F40A            DEFW    0AF4H           ;0AF4H: ?TM error when no
  6020.                                                 ;       string in X
  6021.   018A9 B10A            DEFW    0AB1H           ;0AB1H: CSNG
  6022.  
  6023.  
  6024. ; Address table for basic arithmatic and comparisons
  6025.  
  6026. ; 1. double precision
  6027.  
  6028.   018AB 770C            DEFW    0C77            ; + (X = X + Y)
  6029.   018AD 700C            DEFW    0C70            ; - (X = X - Y)
  6030.   018AF A10D            DEFW    0DA1            ; * (X = X * Y)
  6031.   018B1 E50D            DEFW    0DE5            ; / (X = X / Y)
  6032.   018B3 780A            DEFW    0A78            ; Compare (CP X , Y)
  6033.  
  6034. ; 2. single precision
  6035.  
  6036.   018B5 1607            DEFW    0716            ; + (X = BCDE + X)
  6037.   018B7 1307            DEFW    0713            ; - (X = BCDE - X)
  6038.   018B9 4708            DEFW    0847            ; * (X = BCDE * X)
  6039.   018BB A208            DEFW    08A2            ; / (X = BCDE / X)
  6040.   018BD 0C0A            DEFW    0A0C            ; Compare (CP X , BCDE)
  6041.  
  6042. ; 3. integer
  6043.  
  6044.   018BF D20B            DEFW    0BD2            ; + (X = DE + HL)
  6045.   018C1 C70B            DEFW    0BC7            ; - (X = DE - HL)
  6046.   018C3 F20B            DEFW    0BF2            ; * (X = DE * HL)
  6047.   018C5 9024            DEFW    2490            ; / (X = DE / HL)
  6048.   018C7 390A            DEFW    0A39            ; Compare (CP HL , DE)
  6049.  
  6050.  
  6051. ; Table with character combinations for error messages
  6052.  
  6053.   018C9 4E46            DEFB    'NF'
  6054.   018CB 534E            DEFB    'SN'
  6055.   018CD 5247            DEFB    'RG'
  6056.   018CF 4F44            DEFB    'OD'
  6057.   018D1 4643            DEFB    'FC'
  6058.   018D3 4F46            DEFB    'OF'
  6059.   018D5 4F4D            DEFB    'OM'
  6060.   018D7 554C            DEFB    'UL'
  6061.   018D9 4253            DEFB    'BS'
  6062.   018DB 4444            DEFB    'DD'
  6063.   018DD 2F30            DEFB    '/0'
  6064.   018DF 4944            DEFB    'ID'
  6065.   018E1 544D            DEFB    'TM'
  6066.   018E3 4F53            DEFB    'OS'
  6067.   018E5 4C53            DEFB    'LS'
  6068.   018E7 5354            DEFB    'ST'
  6069.   018E9 434E            DEFB    'CN'
  6070.   018EB 4E52            DEFB    'NR'
  6071.   018ED 5257            DEFB    'RW'
  6072.   018EF 5545            DEFB    'UE'
  6073.   018F1 4D4F            DEFB    'MO'
  6074.   018F3 4644            DEFB    'FD'
  6075.   018F5 534E            DEFB    'SN'
  6076.  
  6077.  
  6078. ; Following 39 bytes are copied into system RAM from 4080H onwards
  6079.  
  6080.   018F7 D600            SUB     00H
  6081.   018F9 6F              LD      L,A
  6082.   018FA 7C              LD      A,H
  6083.   018FB DE00            SBC     A,00H
  6084.   018FD 67              LD      H,A
  6085.   018FE 78              LD      A,B
  6086.   018FF DE00            SBC     A,00H
  6087.   01901 47              LD      B,A
  6088.   01902 3E00            LD      A,00H
  6089.   01904 C9              RET
  6090.   01905 4A              LD      C,D
  6091.   01906 1E40            LD      E,40H
  6092.   01908 E64D            AND     4DH
  6093.   0190A DB00            IN      A,(00H)
  6094.   0190C C9              RET
  6095.   0190D D300            OUT     (00H),A
  6096.   0190F C9              RET
  6097.  
  6098.   01910 00              DEFB    00H
  6099.   01911 00              DEFB    00H
  6100.   01912 00              DEFB    00H
  6101.   01913 00              DEFB    00H
  6102.   01914 28              DEFB    28H
  6103.   01914 1E              DEFB    1EH
  6104.   01916 00              DEFB    00H
  6105.   01917 4C              DEFB    4CH
  6106.   01918 43              DEFB    43H
  6107.   01919 FE              DEFB    FEH
  6108.   01919 FF              DEFB    FFH
  6109.   0191B 01              DEFB    01H
  6110.   0191C 48              DEFB    48H
  6111.  
  6112.  
  6113. ; Text ' Error'
  6114.  
  6115.   0191D 204572726F72    DEFB    ' Error'
  6116.   01923 00              DEFB    00H             ;End of string
  6117.  
  6118. ; Text ' in '
  6119.  
  6120.   01924 2069            DEFB    ' in '
  6121.   01928 00              DEFB    00H             ;End of string
  6122.  
  6123. ; Text 'READY'
  6124.  
  6125.   01929 5245414459      DEFB    'READY'
  6126.   0192E 0D              DEFB    0DH             ;Carriage Return
  6127.   0192F 00              DEFB    00H             ;End of string
  6128.  
  6129. ; Text 'Break'
  6130.  
  6131.   01930 427265616B      DEFB    'Break'
  6132.   01935 00              DEFB    00H             ;End of string
  6133.  
  6134.  
  6135. ; SUB for FOR, NEXT and RETURN
  6136. ; Retrieves data from stack
  6137. ;
  6138. ; I: DE = VARPTR of new loop variable when a new FOR-TO loop is started
  6139. ;    DE = VARPTR of the variable indicated with NEXT
  6140. ;    DE = 0000H when NEXT with no variable
  6141. ; O: DE = unchanged
  6142. ;    HL = 'stackpointer' on FOR-TO-stack + 1 (when Z-flag = 0)
  6143. ;    HL = 'stackpointer' on FOR-TO-stack + 3 (when Z-flag = 1)
  6144. ;    Z-flag = 0 when no FOR-TO-stack found or when the variable is not used
  6145. ;               in a loop
  6146. ;    Z-flag = 1 when call from NEXT or if the variable had already been used
  6147. ;               in a loop
  6148.  
  6149.   01936 210400          LD      HL,0004H
  6150.   01939 39              ADD     HL,SP           ;HL = SP + 4
  6151.                                                 ;HL is now like stack pointer
  6152.                                                 ;after 2 POPs
  6153.   0193A 7E              LD      A,(HL)          ;Get marker form stack
  6154.   0193B 23              INC     HL              ;"stack pointer" + 1
  6155.   0193C FE81            CP      81H             ;FOR marker found ?
  6156.   0193E C0              RET     NZ              ;No: done, return
  6157.                                                 ;Yes:
  6158.   0193F 4E              LD      C,(HL)          ;BC = VARPTR of loop variable
  6159.   01940 23              INC     HL
  6160.   01941 46              LD      B,(HL)
  6161.   01942 23              INC     HL
  6162.   01943 E5              PUSH    HL              ;save "stack pointer"
  6163.   01944 69              LD      L,C             ;HL -> loop variable
  6164.   01945 60              LD      H,B
  6165.   01946 7A              LD      A,D             ;DE = 0000H (call from NEXT) ?
  6166.   01947 B3              OR      E
  6167.   01948 EB              EX      DE,HL           ;DE = VARPTR of loop variable
  6168.                                                 ;found in stack
  6169.                                                 ;HL = VARPTR of new loop
  6170.                                                 ;variable
  6171.                                                 ;Call from NEXT ?
  6172.   01949 2802            JR      Z,194DH         ;Yes: done
  6173.  
  6174.   0194B EB              EX      DE,HL           ;Swap DE and HL
  6175.   0194C DF              RST     18H             ;Compare both VARPTRs
  6176.   0194D 010E00          LD      BC,000EH        ;BC = offset to next
  6177.                                                 ;FOR-TO-stack
  6178.   01950 E1              POP     HL              ;Restore "stack pointer"
  6179.   01951 C8              RET     Z               ;Done if searched VARPTR found
  6180.  
  6181.   01952 09              ADD     HL,BC           ;Increment "stack pointer" to
  6182.                                                 ;next FOR-TO-stack.
  6183.                                                 ;(Every FOR-TO-loop requires
  6184.                                                 ;17 bytes of stack space. HL
  6185.                                                 ;was already incremented by 3
  6186.                                                 ;(193BH, 1940H, 1942H). Add
  6187.                                                 ;0EH (14) to get 17)
  6188.   01953 18E5            JR      193AH           ;Continue search in stack
  6189.  
  6190.  
  6191. ; Move program text for insertion of a new line
  6192. ; Copies memory from (BC) to (HL) until BC = DE
  6193. ;
  6194. ; I: BC -> old program end (before move)
  6195. ;    DE = LP of new line
  6196. ;    HL -> new program end (after move)
  6197. ; O: DE = LP on new line
  6198. ;    HL = DE
  6199.  
  6200.   01955 CD6C19          CALL    196CH           ;Enough free memory available ?
  6201.   01958 C5              PUSH    BC              ;Swap BC and HL
  6202.   01959 E3              EX      (SP),HL         ;(because of RST 18H)
  6203.   0195A C1              POP     BC
  6204.   0195B DF              RST     18H             ;New line reached ?
  6205.   0195C 7E              LD      A,(HL)          ;Copy byte from old location
  6206.   0195D 02              LD      (BC),A          ;to new location
  6207.   0195E C8              RET     Z               ;Yes: done, return
  6208.  
  6209.   0195F 0B              DEC     BC              ;Pointer - 1
  6210.   01960 2B              DEC     HL
  6211.   01961 18F8            JR      195BH           ;Copy next byte
  6212.  
  6213.  
  6214. ; Test if sufficient memory space is available
  6215. ; ?OM Error if less than 2 * C bytes are available
  6216. ;
  6217. ; I: C = number of required bytes
  6218.  
  6219.   01963 E5              PUSH    HL              ;Save PTP
  6220.   01964 2AFD40          LD      HL,(40FDH)      ;HL -> start of free memory
  6221.   01967 0600            LD      B,00H           ;BC = number of required bytes
  6222.   01969 09              ADD     HL,BC           ;HL - HL + 2 * BC
  6223.   0196A 09              ADD     HL,BC
  6224.   0196B 3EE5            LD      A,0E5H          ;--
  6225.  
  6226. ; Is there sufficient free space from (HL) onwards ?
  6227. ;
  6228. ; I: HL -> free memory space
  6229.  
  6230.   0196B   E5            PUSH    HL              ;Save HL
  6231.                                                 ;HL now points to the new
  6232.                                                 ;start of free memory
  6233.   0196D 3EC6            LD      A,0C6H          ;HL = FFC6H - HL
  6234.   0196F 95              SUB     L
  6235.   01970 6F              LD      L,A
  6236.   01971 3EFF            LD      A,0FFH
  6237.   01973 9C              SBC     A,H
  6238.                                                 ;HL > FFC6H
  6239.   01974 3804            JR      C,197AH         ;Yes: ?OM Error
  6240.  
  6241.   01976 67              LD      H,A             ;MSB back to H
  6242.   01977 39              ADD     HL,SP           ;HL = SP + (FFC6 - HL)
  6243.   01978 E1              POP     HL              ;Restore HL
  6244.   01979 D8              RET     C               ;If room until stack then
  6245.                                                 ;return else ?OM Error
  6246.   0197A 1E0C            LD      E,0CH           ;E = error code for ?OM Error
  6247.   0197C 1824            JR      19A2H           ;Continue at error routine
  6248.  
  6249.  
  6250. ; End program without 'END'
  6251.  
  6252.   0197E 2AA240          LD      HL,(40A2H)      ;HL = current LN
  6253.   01981 7C              LD      A,H             ;Was a program finished ?
  6254.   01982 A5              AND     L               ;(is LN <> 65535)
  6255.   01983 3C              INC     A
  6256.   01984 2808            JR      Z,198EH         ;No: go via 198EH to END
  6257.  
  6258.   01986 3AF240          LD      A,(40F2H)       ;ON ERROR GOTO flag set ?
  6259.   01989 B7              OR      A
  6260.  
  6261.   0198A 1E22            LD      E,22H           ;E = error code for ?NR error
  6262.   0198C 2014            JR      NZ,19A2H        ;Flag set: ?NR Error
  6263.  
  6264.   0198E C3C11D          JP      1DC1H           ;Continue at END
  6265.  
  6266.  
  6267. ; ?SN Error in DATA line
  6268.  
  6269.   01991 2ADA40          LD      HL,(40DAH)      ;HL = DATA LN
  6270.   01994 22A240          LD      (40A2H),HL      ;Store as current LN
  6271.  
  6272. ; ?SN Error
  6273.  
  6274.   01997 1E02            LD      E,02H           ;E = error code for ?SN Error
  6275.   01999 011E14          LD      BC,141EH        ;--
  6276.  
  6277. ; ?/0 Error
  6278.  
  6279. * 0199A   1E14          LD      E,14H           ;E = error code for ?/0 Error
  6280.   0199C 011E00          LD      BC,001EH        ;--
  6281.  
  6282. ; ?NF Error
  6283. * 0199D   1E00          LD      E,00H           ;E = error code for ?NF Error
  6284.   0199F 011E24          LD      BC,241EH        ;--
  6285.  
  6286. ; ?RW Error
  6287.  
  6288. * 019A0   1E24          LD      E,24H           ;E = error code for ?RW Error
  6289.  
  6290.  
  6291. ; Error routine
  6292. ; Displays the error code and line number and aborts program
  6293. ;
  6294. ; I: E = (error code - 1) * 2
  6295. ; O: - (returns to active command mode or to error handling routine if
  6296. ;    ON ERROR GOTO was active
  6297.  
  6298.   019A2 2AA240          LD      HL,(40A2H)      ;HL = current LN
  6299.   019A5 22EA40          LD      (40EAH),HL      ;Save as ERL
  6300.   019A8 22EC40          LD      (40ECH),HL      ;and '.' (current LN)
  6301.   019AB 01B419          LD      BC,19B4H        ;19B4H = new return address
  6302.   019AE 2AE840          LD      HL,(40E8H)      ;HL = last SP value
  6303.   019B1 C39A1B          JP      1B9AH           ;Reinitialize stack
  6304.  
  6305.   019B4 C1              POP     BC              ;Correct stack
  6306.   019B5 7B              LD      A,E             ;A = error code
  6307.   019B6 4B              LD      C,E             ;C = error code
  6308.   019B7 329A40          LD      (409AH),A       ;Save error code as ERR
  6309.   019BA 2AE640          LD      HL,(40E6H)      ;HL = PTP before error
  6310.   019BD 22EE40          LD      (40EEH),HL      ;Save PTP for RESUME
  6311.   019C0 EB              EX      DE,HL           ;DE = PTP
  6312.   019C1 2AEA40          LD      HL,(40EAH)      ;HL = LN
  6313.   019C4 7C              LD      A,H             ;LN = FFFH (65535 ?)
  6314.   019C5 A5              AND     L
  6315.   019C6 3C              INC     A
  6316.   019C7 2807            JR      Z,19D0H         ;Yes: error on active command
  6317.                                                 ;mode level, continue at 19D0H
  6318.   019C9 22F540          LD      (40F5H),HL      ;Save LN for CONT
  6319.   019CC EB              EX      DE,HL           ;HL = PTP
  6320.   019CD 22F740          LD      (40F7H),HL      ;Save PTP for CONT
  6321.   019D0 2AF040          LD      HL,(40F0H)      ;HL = LN of ON ERROR GOTO
  6322.   019D3 7C              LD      A,H             ;ON ERROR GOTO active ?
  6323.   019D4 B5              OR      L               ;(LN <> 0 ?)
  6324.   019D5 EB              EX      DE,HL           ;DE = LN
  6325.   019D6 21F240          LD      HL,40F2H        ;HL = ON ERROR GOT flag
  6326.   019D9 2808            JR      Z,19E3H         ;No: continue at 19E3H
  6327.  
  6328.   019DB A6              AND     (HL)            ;ON ERROR GOTO Flag set ?
  6329.   019DC 2005            JR      NZ,19E3H        ;No: continue at 19E3H
  6330.  
  6331.   019DE 35              DEC     (HL)            ;Flag - 1 (as counter
  6332.                                                 ;for RESUME)
  6333.   019DF EB              EX      DE,HL           ;HL = PTP of ON ERROR GOTO line
  6334.   019E0 C3361D          JP      1D36H           ;Continue program at (HL)
  6335.  
  6336. ; Issue error
  6337.  
  6338.   019E3 AF              XOR     A               ;Clear ON ERROR GOTO flag
  6339.   019E4 77              LD      (HL),A
  6340.   019E5 59              LD      E,C             ;E = Error code
  6341.   019E6 CDF920          CALL    20F9H           ;Start at new line
  6342.   019E9 21C918          LD      HL,18C9H        ;HL -> Error codes table
  6343.   019EC CDA641          CALL    41A6H           ;DOS
  6344.   019EF 57              LD      D,A             ;DE = offset in error table
  6345.   019F0 3E3F            LD      A,3FH           ;A = '?'
  6346.   019F2 CD2A03          CALL    032AH           ;Print it
  6347.   019F5 19              ADD     HL,DE           ;HL -> Error code text
  6348.   019F6 7E              LD      A,(HL)          ;Print both characters
  6349.   019F7 CD2A03          CALL    032AH           ;Print 1st error character
  6350.   019FA D7              RST     10H             ;A = 2nd character
  6351.   019FB CD2A03          CALL    032AH           ;Print 2nd error character
  6352.   019FE 211D19          LD      HL,191DH        ;HL -> ' Error'
  6353.   01A01 E5              PUSH    HL              ;Save HL
  6354.   01A02 2AEA40          LD      HL,(40EAH)      ;HL = ERL
  6355.   01A05 E3              EX      (SP),HL         ;Save ERL and restore text
  6356.                                                 ;pointer
  6357. ; Entry for STOP (see 1DDEH)
  6358.  
  6359.   01A06 CD7935          CALL    3579H           ;Give text and sound
  6360.   01A09 E1              POP     HL              ;Restore ERL
  6361.   01A0A 11FEFF          LD      DE,0FFFEH       ;Error originates from
  6362.                                                 ;MEM SIZE?
  6363.                                                 ;(ERL = 65534)
  6364.   01A0D DF              RST     18H             ;Compare HL and DE
  6365.   01A0E CA7406          JP      Z,0674H         ;Yes: back to start 1
  6366.  
  6367.   01A11 7C              LD      A,H             ;Error originates from program?
  6368.   01A12 A5              AND     L               ;(ERL <> 65535)
  6369.   01A13 3C              INC     A
  6370.   01A14 C4A70F          CALL    NZ,0FA7H        ;Yes: print ' in ' and LN
  6371.   01A17 3EC1            LD      A,0C1H          ;Back into BASIC
  6372. * 01A18   C1            POP     BC              ;--
  6373.  
  6374. ; Entry into active command mode
  6375.  
  6376.   01A19 CD8B03          CALL    038BH           ;End output to printer,
  6377.                                                 ;next output to screen
  6378.   01A1C CDAC41          CALL    41ACH           ;DOS
  6379.   01A1F 00              NOP                     ;--
  6380.   01A20 00              NOP
  6381.   01A21 00              NOP
  6382.   01A22 CDF920          CALL    20F9H           ;Finish screen output
  6383.   01A25 212919          LD      HL,1929H        ;HL -> READY text
  6384.   01A28 CD9238          CALL    3892H           ;Set LGR, NBGRD and print text
  6385.   01A2B 3A9A40          LD      A,(409AH)       ;A = last error code
  6386.   01A2E D602            SUB     02H             ;Was it ?SN Error ?
  6387.   01A30 CC532E          CALL    Z,2E53H         ;Yes: call EDIT
  6388.  
  6389.   01A33 21FFFF          LD      HL,0FFFFH       ;Set current LN to 65535
  6390.   01A36 22A240          LD      (40A2H),HL
  6391.   01A39 3AE140          LD      A,(40E1H)       ;AUTO active ?
  6392.   01A3C B7              OR      A
  6393.   01A3D 2837            JR      Z,1A76H         ;No: goto normal command mode
  6394.  
  6395. ; Process AUTO
  6396.  
  6397.   01A3F 2AE240          LD      HL,(40E2H)      ;HL = AUTO-LN
  6398.   01A42 E5              PUSH    HL              ;Save LN
  6399.   01A43 CDAF0F          CALL    0FAFH           ;Print LN
  6400.   01A46 D1              POP     DE              ;DE = LN
  6401.   01A47 D5              PUSH    DE              ;Save LN
  6402.   01A48 CD2C1B          CALL    1B2CH           ;Search LN and test if already
  6403.                                                 :present
  6404.   01A4B 3E2A            LD      A,'*'           ;A = '*'
  6405.                                                 ;LN already present ?
  6406.   01A4D 3802            JR      C,1A51H         ;Yes: continue at 1A51H
  6407.  
  6408.   01A4F 3E20            LD      A,20H           ;A = ' '
  6409.  
  6410.   01A51 CD2A03          CALL    032AH           ;Print it
  6411.   01A54 CD6103          CALL    0361H           ;Input line
  6412.   01A57 D1              POP     DE              ;Restore LN
  6413.                                                 ;<BREAK> pressed ?
  6414.   01A58 3006            JR      NC,1A60H        ;No: accept line
  6415.  
  6416.   01A5A AF              XOR     A               ;A = 00H
  6417.   01A5B 32E140          LD      (40E1H),A       ;Switch off AUTO
  6418.   01A5E 18B9            JR      1A19H           ;Back to active command mode
  6419.  
  6420.  
  6421. ; Accept AUTO line
  6422.  
  6423.   01A60 2AE440          LD      HL,(40E4H)      ;HL = diffence to next LN
  6424.   01A63 19              ADD     HL,DE           ;Calculate next AUTO-LN
  6425.   01A64 38F4            JR      C,1A5AH         ;Abort AUTO if next LN
  6426.                                                 ;becomes larger than 65535
  6427.   01A66 D5              PUSH    DE              ;Save current LN
  6428.   01A67 11F9FF          LD      DE,0FFF9H       ;DE = 65530 (maximum LN + 1)
  6429.   01A6A DF              RST     18H             ;Compare new LN with 65530
  6430.   01A6B D1              POP     DE              ;Restore current LN
  6431.                                                 ;new LN too large (> 65529) ?
  6432.   01A6C 30EC            JR      NC,1A5AH        ;Yes: abort AUTO
  6433.  
  6434.   01A6E 22E240          LD      (40E2H),HL      ;Store next LN
  6435.   01A71 F6FF            OR      0FFH            ;Set A <> 0
  6436.   01A73 C3EB2F          JP      2FEBH           ;Accept line by EDIT routine
  6437.  
  6438.  
  6439. ; Normal command or line input in active command mode
  6440.  
  6441.   01A76 3E3E            LD      A,3EH           ;A = '>'
  6442.   01A78 CD2A03          CALL    032AH           ;Print it
  6443.   01A7B CD6103          CALL    0361H           ;Input line
  6444.                                                 ;<BREAK> pressed ?
  6445.   01A7E DA331A          JP      C,1A33H         ;Yes: back to active
  6446.                                                 ;command mode
  6447.   01A81 D7              RST     10H             ;A = 1st character of
  6448.                                                 ;entered line
  6449.   01A82 3C              INC     A               ;A = 00H ?
  6450.   01A83 3D              DEC     A               ;(no 'OR A' so that C-flag is
  6451.                                                 ;not infuenced)
  6452.   01A84 CA331A          JP      Z,1A33H         ;Yes: back to active command
  6453.                                                 ;mode
  6454.   01A87 F5              PUSH    AF              ;Save flags
  6455.   01A88 CD5A1E          CALL    1E5AH           ;Decode number
  6456.   01A8B 2B              DEC     HL              ;Set HL back to the
  6457.   01A8C 7E              LD      A,(HL)          ;All following spaces are
  6458.   01A8D FE20            CP      20H             ;last digit of the number
  6459.   01A8F 28FA            JR      Z,1A8BH
  6460.  
  6461.   01A91 23              INC     HL              ;HL -> first character in line
  6462.   01A92 7E              LD      A,(HL)          ;A = character
  6463.   01A93 FE20            CP      20H             ;Space ?
  6464.   01A95 CCC909          CALL    Z,09C9H         ;Yes: HL + 1, the first space
  6465.                                                 ;is not used because LIST
  6466.                                                 ;automaticcaly inserts a space
  6467.                                                 ;after the line number
  6468.   01A98 D5              PUSH    DE              ;Save LN
  6469.   01A99 CDC01B          CALL    1BC0H           ;Tokenize line
  6470.   01A9C D1              POP     DE              ;Restore LN
  6471.   01A9D F1              POP     AF              ;Restore flags
  6472.   01A9E 22E640          LD      (40E6H),HL      ;Save HL as current PTP
  6473.   01AA1 CDB241          CALL    41B2H           ;DOS
  6474.                                                 ;LN indicated ?
  6475.   01AA4 D25A1D          JP      NC,1D5AH        ;No: execute line directly
  6476.  
  6477. ; Use line to program
  6478.  
  6479.   01AA7 D5              PUSH    DE              ;Save LN
  6480.   01AA8 C5              PUSH    BC              ;Save length of line
  6481.   01AA9 AF              XOR     A               ;A = 00H
  6482.   01AAA 32DD40          LD      (40DDH),A       ;Clear STOP-flag
  6483.   01AAD D7              RST     10H             ;A = 1st character of tokenized
  6484.                                                 ;line
  6485.   01AAE B7              OR      A               ;A = 00H ?
  6486.                                                 ;(No program text entered
  6487.                                                 ;behind the line number ? )
  6488.   01AAF F5              PUSH    AF              ;Save flags
  6489.   01AB0 EB              EX      DE,HL           ;Save pointer
  6490.   01AB1 22EC40          LD      (40ECH),HL      ;Save LN as '.'-LN
  6491.   01AB4 EB              EX      DE,HL           ;Restore pointer
  6492.   01AB5 CD2C1B          CALL    1B2CH           ;LN already used in program ?
  6493.   01AB8 C5              PUSH    BC              ;Save LP
  6494.   01AB9 DCE42B          CALL    C,2BE4H         ;Yes: delete line
  6495.   01ABC D1              POP     DE              ;Restore LP
  6496.   01ABD F1              POP     AF              ;Restore flags
  6497.   01ABE D5              PUSH    DE              ;Save LP
  6498.                                                 ;Z-flag = 1 (see 1AAEH) ?
  6499.   01ABF 2827            JR      Z,1AE8H         ;Yes: the line had only to be
  6500.                                                 ;deleted. Now only the LPs in
  6501.                                                 ;the program have to be renewed
  6502.   01AC1 D1              POP     DE              ;Restore LP
  6503.   01AC2 2AF940          LD      HL,(40F9H)      ;HL -> program end
  6504.   01AC5 E3              EX      (SP),HL         ;HL = length of line
  6505.   01AC6 C1              POP     BC              ;BC -> program end
  6506.   01AC7 09              ADD     HL,BC           ;HL -> new program end
  6507.   01AC8 E5              PUSH    HL              ;Save pointer
  6508.   01AC9 CD5519          CALL    1955H           ;Make room for new line
  6509.   01ACC E1              POP     HL              ;Restore pointer to new program
  6510.   01ACD 22F940          LD      (40F9H),HL      ;end and store it in system RAM
  6511.   01AD0 EB              EX      DE,HL           ;HL = new LP
  6512.   01AD1 74              LD      (HL),H          ;Set LP to next line <> 0
  6513.   01AD2 D1              POP     DE              ;Restore LN
  6514.   01AD3 E5              PUSH    HL              ;Save LP
  6515.   01AD4 23              INC     HL              ;LP + 2
  6516.   01AD5 23              INC     HL
  6517.   01AD6 73              LD      (HL),E          ;Insert new LP in program
  6518.   01AD7 23              INC     HL
  6519.   01AD8 72              LD      (HL),D
  6520.   01AD9 23              INC     HL
  6521.   01ADA EB              EX      DE,HL           ;DE -> free space for line text
  6522.   01ADB 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  6523.   01ADE EB              EX      DE,HL           ;HL -> program
  6524.                                                 ;DE -> new line + 2
  6525.   01ADF 1B              DEC     DE              ;DE - 2
  6526.   01AE0 1B              DEC     DE
  6527.  
  6528.   01AE1 1A              LD      A,(DE)          ;copy byte from buffer
  6529.   01AE2 77              LD      (HL),A          ;into program
  6530.   01AE3 23              INC     HL              ;Pointer + 1
  6531.   01AE4 13              INC     DE
  6532.   01AE5 B7              OR      A               ;End of line reached ?
  6533.   01AE6 20F9            JR      NZ,1AE1H        ;No: continue copy
  6534.                                                 ;Yes:
  6535.   01AE8 D1              POP     DE              ;DE = LP of new line
  6536.   01AE9 CDFC1A          CALL    1AFCH           ;Renew all LPs in program
  6537.                                                 ;from DE onwards
  6538.   01AEC CDB541          CALL    41B5H           ;DOS
  6539.   01AEF CD5D1B          CALL    1B5DH           ;CLEAR
  6540.   01AF2 CDB841          CALL    41B8H           ;DOS
  6541.   01AF5 C3331A          JP      1A33H           ;Back to active command mode
  6542.  
  6543. ; Renew all LPs in program
  6544.  
  6545.   01AF8 2AA440          LD      HL,(40A4H)      ;HL = start of program
  6546.   01AFB EB              EX      DE,HL           ;DE = start of program
  6547.  
  6548. ; Renew all LPs in program from DE onwards
  6549.  
  6550.   01AFC 62              LD      H,D             ;HL = LP
  6551.   01AFD 6B              LD      L,E
  6552.   01AFE 7E              LD      A,(HL)          ;LP to next line = 0 ?
  6553.   01AFF 23              INC     HL
  6554.   01B00 B6              OR      (HL)            ;(end of program reached ?)
  6555.   01B01 C8              RET     Z               ;Yes: done, return
  6556.                                                 ;No:
  6557.   01B02 23              INC     HL              ;Increment HL to line text
  6558.   01B03 23              INC     HL
  6559.   01B04 23              INC     HL
  6560.   01B05 AF              XOR     A               ;A = 00H
  6561.  
  6562.   01B06 BE              CP      (HL)            ;Search for 00H (end of line)
  6563.   01B07 23              INC     HL              ;Pointer + 1
  6564.   01B08 20FC            JR      NZ,1B06H        ;Continue search
  6565.  
  6566.   01B0A EB              EX      DE,HL           ;DE = LP on next line
  6567.   01B0B 73              LD      (HL),E          ;Set line
  6568.   01B0C 23              INC     HL
  6569.   01B0D 72              LD      (HL),D
  6570.   01B0E 18EC            JR      1AFCH           ;Process next line
  6571.  
  6572.  
  6573. ; Decode line numbers at LIST and DELETE
  6574. ; (LIST. , LIST FF , LIST FF-LL , LIST FF- , LIST -LL etc. )
  6575. ;
  6576. ; I: HL = PTP on character following command (line number in ASCII format)
  6577. ; O: BC -> first line (FF in example)
  6578. ;    DE = starting LN (FF in example) (default = 0)
  6579. ;    HL -> next line (after FF)
  6580. ;    (SP) = end-LN (LL in example) (default = 65529)
  6581.  
  6582.   01B10 110000          LD      DE,0000H        ;Default first LN = 0
  6583.   01B13 D5              PUSH    DE              ;Save first LN
  6584.                                                 ;LNs indicated ?
  6585.   01B14 2809            JR      Z,1B1FH         ;No: continue at 1B1FH
  6586.                                                 ;Yes:
  6587.   01B16 D1              POP     DE              ;Restore first LN
  6588.   01B17 CD4F1E          CALL    1E4FH           ;Decode first LN
  6589.   01B1A D5              PUSH    DE              ;Save first LN
  6590.                                                 ;( = 0 if '-' indicated)
  6591.                                                 ;Only 1 LN indicated ?
  6592.   01B1B 280B            JR      Z,1B28H         ;Yes: continue at 1B1FH
  6593.  
  6594.   01B1D CF              RST     08H             ;'-' indicated ?
  6595.   01B1E CE              DEFB    0CEH            ;'-' token
  6596.   01B1F 11FAFF          LD      DE,FFFAH        ;Default last LN = 65529
  6597.   01B22 C44F1E          JP      Z,1E4FH         ;Decode last LN
  6598.   01B25 C29719          JR      NZ,1997         ;?SN Error ?
  6599.   01B28 EB              EX      DE,HL           ;HL = last LN
  6600.   01B29 D1              POP     DE              ;Restore first LN
  6601.   01B2A E3              EX      (SP),HL         ;Save last LN
  6602.   01B2B E5              PUSH    HL              ;RET address back on stack
  6603.  
  6604.  
  6605. ; Search line number DE in program
  6606. ;
  6607. ; I: DE = line number of line to be searched
  6608. ; O: BC -> searched line (if line found) or program end (line not found)
  6609. ;    DE = line number
  6610. ;    HL -> next line
  6611. ;    Z-flag = 1, C-flag = 1: search succesfull
  6612. ;    Z-flag = 1, C-flag = 0: BC -> program end
  6613. ;    Z-flag = 0, C-flag = 0: BC -> line with largerst LN < searched LN
  6614.  
  6615.   01B2C 2AA440          LD      HL,(40A4H)      ;HL -> first program lline
  6616.   01B2F 44              LD      B,H             ;BC = LP
  6617.   01B30 4D              LD      C,L
  6618.   01B31 7E              LD      A,(HL)          ;LP to next line = 0 ?
  6619.   01B32 23              INC     HL
  6620.   01B33 B6              OR      (HL)
  6621.   01B34 2B              DEC     HL              ;Reverse INC HL
  6622.   01B35 C8              RET     Z               ;Yes: done, return
  6623.  
  6624.   01B36 23              INC     HL              ;Increment HL to line number
  6625.   01B37 23              INC     HL
  6626.   01B38 7E              LD      A,(HL)          ;HL = line number
  6627.   01B39 23              INC     HL
  6628.   01B3A 66              LD      H,(HL)
  6629.   01B3B 6F              LD      L,A
  6630.   01B3C DF              RST     18H             ;Compare with line number
  6631.                                                 ;to be searched for
  6632.   01B3D 60              LD      H,B             ;LP back to HL
  6633.   01B3E 69              LD      L,C
  6634.   01B3F 7E              LD      A,(HL)          ;HL -> next line
  6635.   01B40 23              INC     HL
  6636.   01B41 66              LD      H,(HL)
  6637.   01B42 6F              LD      L,A
  6638.   01B43 3F              CCF                     ;C-flag = 1 for succesfull
  6639.                                                 ;search
  6640.   01B44 C8              RET     Z               ;Done if line found
  6641.  
  6642.   01B45 3F              CCF                     ;Reverse C-flag again
  6643.   01B46 D0              RET     NC              ;RET if LN found >
  6644.                                                 ;LN to be searched for
  6645.   01B47 18E6            JR      1B2FH           ;Check next line
  6646.  
  6647.  
  6648. ; NEW statement
  6649. ; -------------
  6650.  
  6651.   01B49 C0              RET     NZ              ;?SN Error ?
  6652.  
  6653.   01B4A CDC901          CALL    01C9H           ;CLS
  6654.   01B4D 2AA440          LD      HL,(40A4H)      ;HL -> start of BASIC program
  6655.   01B50 CDF81D          CALL    1DF8H           ;TROFF
  6656.   01B53 32E140          LD      (40E1H),A       ;Switch off AUTO
  6657.   01B56 77              LD      (HL),A          ;Set LP to second line
  6658.   01B57 23              INC     HL              ;to zero
  6659.   01B58 77              LD      (HL),A
  6660.   01B59 23              INC     HL
  6661.   01B5A 22F940          LD      (40F9H),HL      ;Program end = program start+2
  6662.  
  6663.  
  6664. ; Entry for RUN without line number (RET on program loop)
  6665.  
  6666.   01B5D 2AA440          LD      HL,(40A4H)      ;HL -> BASIC program start
  6667.   01B60 2B              DEC     HL              ;HL - 1
  6668.  
  6669.  
  6670. ; CLEAR without argument
  6671.  
  6672.   01B61 22DF40          LD      (40DFH),HL      ;Store PTP to first/next
  6673.                                                 ;command
  6674.   01B64 061A            LD      B,1AH           ;DEFSNG A-Z (B = 26)
  6675.   01B66 210141          LD      HL,4101H        ;HL -> DEF table
  6676.   01B69 3604            LD      (HL),04H        ;Set type code on SNG
  6677.   01B6B 23              INC     HL              ;Pointer + 1
  6678.   01B6C 10FB            DJNZ    1B69H           ;Loop
  6679.  
  6680.   01B6E AF              XOR     A               ;A = 00H
  6681.   01B6F 32F240          LD      (40F2H),A       ;Clear ON ERROR GOTO flag
  6682.   01B72 6F              LD      L,A             ;HL = 0000H
  6683.   01B73 67              LD      H,A
  6684.   01B74 22F040          LD      (40F0H),HL      ;ON ERROR GOTO LN = 0
  6685.   01B77 22F740          LD      (40F7H),HL      ;CONT PTP = 0
  6686.   01B7A 2AB140          LD      HL,(40B1H)      ;HL = TOPMEM
  6687.   01B7D 22D640          LD      (40D6H),HL      ;Address of last string in
  6688.                                                 ;string space = TOPMEM
  6689.                                                 ;(delete all strings)
  6690.   01B80 CD911D          CALL    1D91H           ;RESTORE
  6691.   01B83 2AF940          LD      HL,(40F9H)      ;HL -> BASIC program end
  6692.   01B86 22FB40          LD      (40FBH),HL      ;End of variable tables
  6693.   01B89 22FD40          LD      (40FDH),HL      ;is end of BASIC program:
  6694.                                                 ;Delete all variables
  6695.   01B8C CDBB41          CALL    41BBH           ;DOS
  6696.   01B8F C1              POP     BC              ;BC = return address
  6697.   01B90 2AA040          LD      HL,(40A0H)      ;HL -> start of string memory
  6698.   01B93 2B              DEC     HL              ;HL - 2
  6699.   01B94 2B              DEC     HL
  6700.   01B95 22E840          LD      (40E8H),HL      ;Set new program stack
  6701.   01B98 23              INC     HL
  6702.   01B99 23              INC     HL
  6703.  
  6704.   01B9A F9              LD      SP,HL           ;SP = start of string memory
  6705.   01B9B 21B540          LD      HL,40B5H        ;HL -> start of string table
  6706.   01B9E 22B340          LD      (40B3H),HL      ;Set pointer to first free
  6707.                                                 ;position in string table
  6708.   01BA1 CD8B03          CALL    038BH           ;End output to printer
  6709.   01BA4 CD6921          CALL    2169H           ;Next output to screen
  6710.   01BA7 AF              XOR     A               ;A = 0
  6711.   01BA8 67              LD      H,A             ;HL = 0000H
  6712.   01BA9 6F              LD      L,A
  6713.   01BAA 32DC40          LD      (40DCH),A       ;Release array variables
  6714.   01BAD E5              PUSH    HL              ;Put HL on stack
  6715.   01BAE C5              PUSH    BC              ;Set new return address
  6716.   01BAF 2ADF40          LD      HL,(40DFH)      ;HL = entry address SYSTEM
  6717.   01BB2 C9              RET
  6718.  
  6719.  
  6720. ; SUB for INPUT
  6721. ; Print '? ' and input line
  6722.  
  6723.   01BB3 3E3F            LD      A,3FH           ;A = '?'
  6724.   01BB5 CD2A03          CALL    032AH           ;Print it
  6725.   01BB8 3E20            LD      A,20H           ;A = ' '
  6726.   01BBA CD2A03          CALL    032AH           ;Print it
  6727.   01BBD C36103          JP      0361H           ;Input line
  6728.  
  6729.  
  6730. ; SUB for active command mode
  6731. ; Tokenize program text in line buffer
  6732. ; Text pointer = pointer on entered text
  6733. ; Buffer pointer = pointer on tokenized text
  6734. ;
  6735. ; I: HL = text pointer
  6736. ; O: BC = number of required memory bytes (line length)
  6737. ;    DE -> end of tokenized text
  6738. ;    HL -> start of tokenized text - 1
  6739.  
  6740.   01BC0 AF              XOR     A               ;A = 00H
  6741.   01BC1 32B040          LD      (40B0H),A       ;Allow tokenizing
  6742.   01BC4 4F              LD      C,A             ;Counter = 0
  6743.   01BC5 EB              EX      DE,HL           ;DE = text pointer
  6744.   01BC6 2AA740          LD      HL,(40A7H)      ;HL -> start of line buffer
  6745.   01BC9 2B              DEC     HL
  6746.   01BCA 2B              DEC     HL
  6747.   01BCB EB              EX      DE,HL           ;DE = buffer pointer
  6748.                                                 ;HL = text pointer
  6749.   01BCC 7E              LD      A,(HL)          ;A = text character
  6750.   01BCD FE20            CP      20H             ;Space ?
  6751.   01BCF CA5B1C          JP      Z,1C5BH         ;Yes: store character
  6752.                                                 ;No:
  6753.   01BD2 47              LD      B,A             ;B = character
  6754.   01BD3 FE22            CP      22H             ;Start of a string ?
  6755.   01BD5 CA771C          JP      Z,1C77H         ;Yes: continue at 1C77H
  6756.                                                 ;No:
  6757.   01BD8 B7              OR      A               ;End of line reached ?
  6758.   01BD9 CA7D1C          JP      Z,1C7DH         ;Yes: continue at 1C7DH
  6759.                                                 ;No:
  6760.   01BDC 3AB040          LD      A,(40B0H)       ;Tokenizing allowed ?
  6761.   01BDF B7              OR      A
  6762.   01BE0 7E              LD      A,(HL)          ;A = text character
  6763.   01BE1 C25B1C          JP      NZ,1C5BH        ;No: accept character
  6764.                                                 ;Yes:
  6765.   01BE4 FE3F            CP      '?'             ;Is it '?'
  6766.   01BE6 3EB2            LD      A,0B2H          ;A = PRINT token
  6767.   01BE8 CA5B1C          JP      Z,1C5BH         ;Yes: store PRINT token
  6768.                                                 ;No:
  6769.   01BEB 7E              LD      A,(HL)          ;A = text character
  6770.   01BEC FE30            CP      '0'             ;Is it a digit ?
  6771.   01BEE 3805            JR      C,1BF5H         ;No: tokenize character
  6772.                                                 ;Yes:
  6773.   01BF0 FE3C            CP      3CH             ;Character before '<' ?
  6774.   01BF2 DA5B1C          JP      C,1C5BH         ;Yes: accept character (the
  6775.                                                 ;characters '<' , '=' and '>'
  6776.                                                 ;are tokenized)
  6777. ; Tokenize character / text
  6778.  
  6779.   01BF5 D5              PUSH    DE              ;Save buffer pointer
  6780.   01BF6 114F16          LD      DE,164FH        ;DE -> Keyword table
  6781.   01BF9 C5              PUSH    BC              ;Save counter
  6782.   01BFA 013D1C          LD      BC,1C3DH        ;Set new RET address
  6783.   01BFD C5              PUSH    BC              ;to 1C3DH
  6784.   01BFE 067F            LD      B,7FH           ;B = token counter
  6785.   01C00 7E              LD      A,(HL)          ;A = text character
  6786.   01C01 FE61            CP      'a'             ;Character < 'a' ?
  6787.   01C03 3807            JR      C,1C0CH         ;Yes: continue at 1C0CH
  6788.  
  6789.   01C05 FE7B            CP      'z'+1           ;Character > 'z' ?
  6790.   01C07 3003            JR      NC,1C0CH        ;Yes: continue at 1C0CH
  6791.  
  6792.   01C09 E65F            AND     5FH             ;Convert to upper case
  6793.   01C0B 77              LD      (HL),A          ;Store character
  6794.  
  6795.   01C0C 4E              LD      C,(HL)          ;C = text character
  6796.   01C0D EB              EX      DE,HL           ;HL = table pointer
  6797.  
  6798.   01C0E 23              INC     HL              ;Table pointer + 1
  6799.   01C0F B6              OR      (HL)            ;Next keyword reached ?
  6800.   01C10 F20E1C          JP      P,1C0EH         ;No: increment HL to next
  6801.                                                 ;keyword
  6802.   01C13 04              INC     B               ;Yes: token counter + 1
  6803.   01C14 7E              LD      A,(HL)          ;A = table character
  6804.   01C15 CDE238          CALL    38E2H           ;Intercept Colour BASIC
  6805.                                                 ;keywords
  6806.   01C18 B9              CP      C               ;Compare text character with
  6807.                                                 ;table character; the same ?
  6808.   01C19 20F3            JR      NZ,1C0EH        ;No: increment table pointer
  6809.                                                 ;to next keyword and check
  6810.                                                 ;again
  6811.   01C1B EB              EX      DE,HL           ;DE = table pointer
  6812.                                                 ;HL = buffer pointer on first
  6813.                                                 ;character
  6814.   01C1C E5              PUSH    HL              ;Save buffer pointer
  6815.   01C1D 13              INC     DE              ;Get next character
  6816.   01C1E 1A              LD      A,(DE)          ;from table
  6817.   01C1F B7              OR      A               ;Next keyword reached ?
  6818.   01C20 FA391C          JP      M,1C39H         ;Yes: Put token in B
  6819.                                                 ;No:
  6820.   01C23 4F              LD      C,A             ;C = table character
  6821.   01C24 78              LD      A,B             ;A = token counter
  6822.   01C25 FE8D            CP      8DH             ;At GOTO token ?
  6823.   01C27 2002            JR      NZ,1C2BH        ;No: continue at 1C2BH
  6824.  
  6825.   01C29 D7              RST     10H             ;Increment text pointer to next
  6826.                                                 ;character (GOTO can also be
  6827.                                                 ;written as GO TO !)
  6828.   01C2A 2B              DEC     HL              ;HL - 1 because of HL + 1
  6829.                                                 ;in the RST 10H
  6830.   01C2B 23              INC     HL              ;Text pointer + 1
  6831.   01C2C 7E              LD      A,(HL)          ;A = next character
  6832.   01C2D FE61            CP      'a'             ;Lower case ?
  6833.   01C2F 3802            JR      C,1C33H         ;No: ok, continue at 1C33H
  6834.  
  6835.   01C31 E65F            AND     5FH             ;Convert to upper case
  6836.  
  6837.   01C33 B9              CP      C               ;Text character the same as
  6838.                                                 ;table character
  6839.   01C34 28E7            JR      Z,1C1DH         ;Yes: compare next character
  6840.                                                 ;No:
  6841.   01C36 E1              POP     HL              ;Set text pointer back to first
  6842.                                                 ;character
  6843.   01C37 18D3            JR      1C0CH           ;And compare with next keyword
  6844.  
  6845. ; Compare ended succesfully
  6846.  
  6847.   01C39 48              LD      C,B             ;C = token
  6848.   01C3A F1              POP     AF              ;Remove text pointer from stack
  6849.   01C3B EB              EX      DE,HL           ;DE = text pointer
  6850.   01C3C C9              RET
  6851.  
  6852. ; Store token or character
  6853.  
  6854.   01C3D EB              EX      DE,HL           ;HL = text pointer
  6855.   01C3E 79              LD      A,C             ;A = token
  6856.   01C3F C1              POP     BC              ;Restore character counter
  6857.   01C40 D1              POP     DE              ;Restore buffer pointer
  6858.   01C41 EB              EX      DE,HL           ;DE = text pointer
  6859.                                                 ;HL = buffer pointer
  6860.   01C42 FE95            CP      95H             ;'ELSE' token ?
  6861.   01C44 363A            LD      (HL),3AH        ;Put ':' in buffer
  6862.   01C46 2002            JR      NZ,1C4AH        ;No: do not use ':'
  6863.                                                 ;Yes:
  6864.   01C48 0C              INC     C               ;Counter + 1
  6865.   01C49 23              INC     HL              ;Buffer pointer + 1:
  6866.                                                 ; ':' is used
  6867.   01C4A FEFB            CP      0FBH            ;Apostroph ? (REM)
  6868.   01C4C 200C            JR      NZ,1C5AH        ;No: implement token
  6869.                                                 ;Yes:
  6870.   01C4E 363A            LD      (HL),':'        ;Put ':' in buffer
  6871.   01C50 23              INC     HL              ;Buffer poiner + 1
  6872.   01C51 0693            LD      B,93H           ;B = 'REM' token
  6873.   01C53 70              LD      (HL),B          ;Put token in buffer
  6874.   01C54 23              INC     HL              ;Buffer pointer + 1
  6875.   01C55 EB              EX      DE,HL           ;DE = buffer pointer
  6876.   01C56 0C              INC     C               ;Counter + 1 (for ':')
  6877.   01C57 0C              INC     C               ;Counter + 1 (for 'REM')
  6878.   01C58 181D            JR      1C77H           ;Use all characters until end
  6879.                                                 ;of line (REM line)
  6880. ; Use token or character in A
  6881.  
  6882.   01C5A EB              EX      DE,HL           ;DE = buffer pointer
  6883.   01C5B 23              INC     HL              ;Text pointer + 1
  6884.   01C5C 12              LD      (DE),A          ;Store token in buffer
  6885.   01C5D 13              INC     DE              ;Buffer pointer + 1
  6886.   01C5E 0C              INC     C               ;Counter + 1
  6887.   01C5F D63A            SUB     ':'             ;':' ? (SUBtract !!)
  6888.   01C61 2804            JR      Z,1C67H         ;Yes: release tokenizing and
  6889.                                                 ;tokenize next character
  6890.   01C63 FE4E            CP      4EH             ;'DATA' token ?
  6891.                                                 ;(4EH + 3AH = 88H)
  6892.   01C65 2003            JR      NZ,1C6AH        ;No: leave flag, continue at
  6893.                                                 ;1C6AH
  6894.   01C67 32B040          LD      (40B0H),A       ;Yes: block tokenizing (A=4EH)
  6895.   01C6A D659            SUB     59H             ;'REM' token ? (SUBtract !!)
  6896.   01C6C C2CC1B          JP      NZ,1BCCH        ;No: tokenize next character
  6897.                                                 ;Yes:
  6898.   01C6F 47              LD      B,A             ;Set B = 0 as compare char.
  6899.  
  6900. ; Use all characters until end of line or until character = B
  6901.  
  6902.   01C70 7E              LD      A,(HL)          ;A = text character
  6903.   01C71 B7              OR      A               ;End of line reached ?
  6904.   01C72 2809            JR      Z,1C7DH         ;Yes: done, continue ar 1C7DH
  6905.                                                 ;No:
  6906.   01C74 B8              CP      B               ;Same as compare character ?
  6907.   01C75 28E4            JR      Z,1C5BH         ;Yes: implement character.
  6908.                                                 ;No:
  6909.   01C77 23              INC     HL              ;Text pointer + 1
  6910.   01C78 12              LD      (DE),A          ;Store character in buffer
  6911.   01C79 0C              INC     C               ;Counter + 1
  6912.   01C7A 13              INC     DE              ;Buffer pointer + 1
  6913.   01C7B 18F3            JR      1C70H           ;Next character
  6914.  
  6915. ; Line completely tokenized
  6916.  
  6917.   01C7D 210500          LD      HL,0005H        ;Add 5 bytes to total length
  6918.   01C80 44              LD      B,H             ;(2 bytes line pointer, 2 bytes
  6919.   01C81 09              ADD     HL,BC           ;line number, 1 byte line end)
  6920.   01C82 44              LD      B,H             ;BC = number of bytes required
  6921.   01C83 4D              LD      C,L             ;in memory
  6922.   01C84 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  6923.   01C87 2B              DEC     HL              ;HL -> tokenized line - 1
  6924.   01C88 2B              DEC     HL
  6925.   01C89 2B              DEC     HL
  6926.   01C8A 12              LD      (DE),A          ;Terminate tokenized line
  6927.   01C8B 13              INC     DE              ;with 3 times 00H
  6928.   01C8C 12              LD      (DE),A
  6929.   01C8D 13              INC     DE
  6930.   01C8E 12              LD      (DE),A
  6931.   01C8F C9              RET
  6932.  
  6933.  
  6934. ; SUB RST 18H: 16 bit compare
  6935. ; Compares DE with HL and set flags according to result
  6936. ; (like CP HL,DE)
  6937.  
  6938.   01C90 7C              LD      A,H             ;A = MSB of HL
  6939.   01C91 92              SUB     D               ;Subtract MSB of DE
  6940.                                                 ;H = D ?
  6941.   01C92 C0              RET     NZ              ;No: return
  6942.  
  6943.   01C93 7D              LD      A,L             ;Same with LSBs
  6944.   01C94 93              SUB     E
  6945.   01C95 C9              RET
  6946.  
  6947.  
  6948. ; SUB RST 08H: syntax check
  6949. ; Compare the byte in (HL) with the byte following the RST 08H in memory.
  6950. ; If they are equal then a RST 10H is executed and the routine returns to
  6951. ; the normal RET-address + 1 (because the compare byte is located at the
  6952. ; RET-address). If both bytes are not equal a ?SN Error is generated
  6953.  
  6954.   01C96 7E              LD      A,(HL)          ;A = text character
  6955.   01C97 E3              EX      (SP),HL         ;HL -> compare character
  6956.   01C98 BE              CP      (HL)            ;The same ?
  6957.   01C99 23              INC     HL              ;HL + 1 for RET-address
  6958.   01C9A E3              EX      (SP),HL         ;RET-address on stack
  6959.   01C9B CA781D          JP      Z,1D78H         ;Yes: equal so RST 10H
  6960.                                                 ;No:
  6961.   01C9E C39719          JP      1997H           ;?SN Error
  6962.  
  6963.  
  6964. ; FOR statement
  6965. ; -------------
  6966.  
  6967.   01CA1 3E64            LD      A,64H           ;A <> 0
  6968.   01CA3 32DC40          LD      (40DCH),A       ;Block array variables
  6969.   01CA6 CD211F          CALL    1F21H           ;Execute LET (set loop
  6970.                                                 ;variable and initialize it)
  6971.   01CA9 E3              EX      (SP),HL         ;Save PTP, HL = RET-address
  6972.                                                 ;DE = VARPTR on loop variable
  6973.   01CAA CD3619          CALL    1936H           ;Already a FOR-TO-loop active
  6974.                                                 ;using this variable ?
  6975.   01CAD D1              POP     DE              ;Restore PTP
  6976.   01CAE 2005            JR      NZ,1CB5H        ;No: continue at 1CB5H
  6977.                                                 ;Yes:
  6978.   01CB0 09              ADD     HL,BC           ;HL = HL + 14 (BC = 000EH)
  6979.   01CB1 F9              LD      SP,HL           ;Terminate previous loop using
  6980.                                                 ;this variable (delete from
  6981.                                                 ;stack)
  6982.   01CB2 22E840          LD      (40E8H),HL      ;Save new SP
  6983.   01CB5 EB              EX      DE,HL           ;HL = PTP
  6984.   01CB6 0E08            LD      C,08H           ;Are there 16 bytes (2*C) free?
  6985.   01CB8 CD6319          CALL    1963H           ;(17 bytes are needed)
  6986.   01CBB E5              PUSH    HL              ;Save PTP
  6987.   01CBC CD051F          CALL    1F05H           ;Increment PTP to next command
  6988.                                                 ;(PTP is then pointing on the
  6989.                                                 ;first command within the loop)
  6990.   01CBF E3              EX      (SP),HL         ;Save new PTP, HL = old PTP
  6991.   01CC0 E5              PUSH    HL              ;Save PTP
  6992.   01CC1 2AA240          LD      HL,(40A2H)      ;HL= current LN
  6993.   01CC4 E3              EX      (SP),HL         ;Save LN, restore PTP
  6994.   01CC5 CF              RST     08H             ;Next byte must be the
  6995.   01CC6 BD              DEFB    0BDH            ;'TO' token
  6996.   01CC7 E7              RST     20H             ;TSTTYP
  6997.                                                 ;STR type ?
  6998.   01CC8 CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  6999.                                                 ;DBL type ?
  7000.   01CCB D2F60A          JP      NC,0AF6H        ;Yes: ?TM Error
  7001.  
  7002.   01CCE F5              PUSH    AF              ;Save type code - 3
  7003.   01CCF CD3723          CALL    2337H           ;X = final value of loop
  7004.   01CD2 F1              POP     AF              ;Restore type code
  7005.   01CD3 E5              PUSH    HL              ;Save PTP
  7006.                                                 ;Loop in SNG format ?
  7007.   01CD4 F2EC1C          JP      P,1CECH         ;Yes: continue at 1CECH
  7008.  
  7009. ; INT loop
  7010.  
  7011.   01CD7 CD7F0A          CALL    0A7FH           ;Convert final value to INT
  7012.   01CDA E3              EX      (SP),HL         ;Save final value, restore PTP
  7013.   01CDB 110100          LD      DE,0001H        ;DE = default step value (1)
  7014.   01CDE 7E              LD      A,(HL)          ;A = next character
  7015.   01CDF FECC            CP      0CCH            ;'STEP' token ?
  7016.   01CE1 CC012B          CALL    Z,2B01H         ;Yes: DE = step value
  7017.   01CE4 D5              PUSH    DE              ;Save step value
  7018.   01CE5 E5              PUSH    HL              ;Save PTP
  7019.   01CE6 EB              EX      DE,HL           ;HL = step value
  7020.   01CE7 CD9E09          CALL    099EH           ;A = SGN (step value)
  7021.   01CEA 1822            JR      1D0EH           ;Continue at 1D0EH
  7022.  
  7023. ; SNG loop
  7024.  
  7025.   01CEC CDB10A          CALL    0AB1H           ;Convert final value to SGN
  7026.   01CEF CDBF09          CALL    09BFH           ;BCDE = X = final value
  7027.   01CF2 E1              POP     HL              ;Restore PTP
  7028.   01CF3 C5              PUSH    BC              ;Save final value
  7029.   01CF4 D5              PUSH    DE
  7030.   01CF5 010081          LD      BC,8100H        ;BCDE = default step value (1)
  7031.   01CF8 51              LD      D,C
  7032.   01CF9 5A              LD      E,D
  7033.   01CFA 7E              LD      A,(HL)          ;A = next character
  7034.   01CFB FECC            CP      0CCH            ;'STEP' token ?
  7035.   01CFD 3E01            LD      A,01H           ;A = SNG (default step value)
  7036.   01CFF 200E            JR      NZ,1D0FH        ;No: continue at 1D0FH
  7037.                                                 ;Yes:
  7038.   01D01 CD3823          CALL    2338H           ;X = step value
  7039.   01D04 E5              PUSH    HL              ;Save PTP
  7040.   01D05 CDB10A          CALL    0AB1H           ;X = CSGN (X)
  7041.   01D08 CDBF09          CALL    09BFH           ;BCDE = X = step value
  7042.   01D0B CD5509          CALL    0955H           ;A = SNG(step value)
  7043.  
  7044. ; FOR stack wrap up
  7045.  
  7046.   01D0E E1              POP     HL              ;Restore PTP
  7047.   01D0F C5              PUSH    BC              ;Save step value
  7048.   01D10 D5              PUSH    DE              ;(for SNG loop)
  7049.   01D11 4F              LD      C,A             ;C = SGN (step value)
  7050.   01D12 E7              RST     20H             ;TSTTYP (step value)
  7051.   01D13 47              LD      B,A             ;B = type code - 3
  7052.   01D14 C5              PUSH    BC              ;Save type code and
  7053.                                                 ;SNG (step value)
  7054.   01D15 E5              PUSH    HL              ;Save PTP
  7055.   01D16 2ADF40          LD      HL,(40DFH)      ;HL = VARPTR of loop variable
  7056.   01D19 E3              EX      (SP),HL         ;Save VARPTR, restore PTP
  7057.   01D1A 0681            LD      B,81H           ;B = 'FOR' token
  7058.   01D1C C5              PUSH    BC              ;Mark stack
  7059.   01D1D 33              INC     SP              ;Remove LSB
  7060.  
  7061. ; Program loop
  7062. ; Return address after execution of a command in the program
  7063. ; HL (PTP) must point to end of command (':') to end of line (00H)
  7064.  
  7065.   01D1E CD5803          CALL    0358H           ;Get key
  7066.   01D21 B7              OR      A               ;Key pressed ?
  7067.   01D22 C4A01D          CALL    NZ,1DA0H        ;Yes: <SHIFT>+<@> or <BREAK> ?
  7068.   01D25 22E640          LD      (40E6H),HL      ;Save PTP
  7069.   01D28 ED73E840        LD      (40E8H),SP      ;Save SP
  7070.   01D2C 7E              LD      A,(HL)          ;A = next character
  7071.   01D2D FE3A            CP      3AH             ;= ':' ?
  7072.   01D2F 2829            JR      Z,1D5AH         ;Yes: ok, continue at 1DA5H
  7073.                                                 ;No:
  7074.   01D31 B7              OR      A               ;= 00H ? (end of line)
  7075.   01D32 C29719          JP      NZ,1997H        ;No: ?SN Error
  7076.  
  7077. ; Start new line
  7078.  
  7079.   01D35 23              INC     HL              ;PTP + 1
  7080.  
  7081.   01D36 7E              LD      A,(HL)          ;Test pointer to next line
  7082.   01D37 23              INC     HL
  7083.   01D38 B6              OR      (HL)            ;= 0000H (end of program) ?
  7084.   01D39 CA7E19          JP      Z,197EH         ;Yes: end program.
  7085.                                                 ;No:
  7086.   01D3C 23              INC     HL              ;DE = line number
  7087.   01D3D 5E              LD      E,(HL)
  7088.   01D3E 23              INC     HL
  7089.   01D3F 56              LD      D,(HL)
  7090.   01D40 EB              EX      DE,HL           ;HL = LN, DE = PTP
  7091.   01D41 22A240          LD      (40A2H),HL      ;Store current LN
  7092.   01D44 3A1B41          LD      A,(411BH)       ;TRACE active ?
  7093.   01D47 B7              OR      A               ;(411BH) <> 0 ?
  7094.   01D48 280F            JR      Z,1D59H         ;No: continue at 1D59H
  7095.  
  7096. ; Execute TRACE
  7097.  
  7098.   01D4A D5              PUSH    DE              ;Save PTP
  7099.   01D4B 3E3C            LD      A,3CH           ;A = '<'
  7100.   01D4D CD2A03          CALL    032AH           ;Print it
  7101.   01D50 CDAF0F          CALL    0FAFH           ;Print HL as decimal number
  7102.                                                 ;(line number)
  7103.   01D53 3E3E            LD      A,3EH           ;A = '>'
  7104.   01D55 CD2A03          CALL    032AH           ;Print it
  7105.   01D58 D1              POP     DE              ;Restore PTP
  7106.   01D59 EB              EX      DE,HL           ;HL = PTP, DE = LN
  7107.  
  7108.   01D5A D7              RST     10H             ;Next character to A
  7109.   01D5B 111E1D          LD      DE,1D1EH        ;Set new RET address
  7110.   01D5E D5              PUSH    DE              ;to 1D1EH
  7111.   01D5F C8              RET     Z               ;RET when end of line reached
  7112.  
  7113.   01D60 D680            SUB     80H             ;Token found ?
  7114.   01D62 DA211F          JP      C,1F21H         ;No: interpret character as
  7115.                                                 ;a variable, continue at LET
  7116.   01D65 FE3C            CP      3CH             ;Statement or function ?
  7117.   01D67 C3C039          JP      39C0H           ;Colour-keyword found ?
  7118.  
  7119.   01D6A 07              RLCA                    ;A = token-number * 2
  7120.   01D6B 4F              LD      C,A             ;BC = offset for address table
  7121.   01D6C 0600            LD      B,00H
  7122.   01D6E EB              EX      DE,HL           ;DE = PTP
  7123.   01D6F 212218          LD      HL,1822H        ;HL -> address table
  7124.   01D72 09              ADD     HL,BC           ;Add offset
  7125.   01D73 4E              LD      C,(HL)          ;Load command address in BC
  7126.   01D74 23              INC     HL
  7127.   01D75 46              LD      B,(HL)
  7128.   01D76 C5              PUSH    BC              ;Put address on stack for RET
  7129.   01D77 EB              EX      DE,HL           ;HL = PTP
  7130.  
  7131.  
  7132. ; SUB RST 10H: increment PTP to next character <> 20H (space)
  7133. ;
  7134. ; I: HL = PTP
  7135. ; O: HL = PTP (+1 at least)
  7136. ;    A = character at (HL)
  7137. ;    C-flag = 1 if digit found
  7138. ;    Z-flag = 1 if end of command (':') or end of line (00H) found
  7139.  
  7140.   01D78 23              INC     HL              ;PTP + 1
  7141.   01D79 7E              LD      A,(HL)          ;A = next character
  7142.   01D7A FE3A            CP      ':'             ;Character > digit ?
  7143.   01D7C D0              RET     NC              ;Yes: done (Z-flag = 1 in
  7144.                                                 ;case of ':')
  7145.   01D7D FE20            CP      20H             ;Space ?
  7146.   01D7F CA781D          JP      Z,1D78H         ;Yes: get next character
  7147.  
  7148.   01D82 FE0B            CP      0BH             ;Character > 0AH ?
  7149.   01D84 3005            JR      NC,1D8BH        ;Yes: continue at 1D8BH
  7150.  
  7151.   01D86 FE09            CP      09H             ;Character > 08H ?
  7152.   01D88 D2781D          JP      NC,1D78H        ;Yes: get next character
  7153.  
  7154.   01D8B FE30            CP      '0'             ;Digit found?
  7155.   01D8D 3F              CCF                     ;Yes: C-flag = 1
  7156.   01D8E 3C              INC     A               ;End of line reached ?
  7157.   01D8F 3D              DEC     A               ;(A = 00H)
  7158.                                                 ;Yes: Z-flag = 1
  7159.   01D90 C9              RET
  7160.  
  7161.  
  7162. ; RESTORE statement
  7163. ; -----------------
  7164.  
  7165.   01D91 EB              EX      DE,HL           ;DE = PTP
  7166.   01D92 2AA440          LD      HL,(40A4H)      ;HL = start of program
  7167.   01D95 2B              DEC     HL              ;Hl = HL - 1
  7168.   01D96 22FF40          LD      (40FFH),HL      ;DATA pointer =
  7169.                                                 ;start of program - 1
  7170.   01D99 EB              EX      DE,HL           ;HL = PTP
  7171.   01D9A C9              RET
  7172.  
  7173.  
  7174. ; <SHIFT>+<@> or <BREAK> pressed ?
  7175.  
  7176.   01D9B CD5803          CALL    0358H           ;Get key
  7177.   01D9E B7              OR      A               ;Key pressed ?
  7178.   01D9F C8              RET     Z               ;No: return
  7179.  
  7180.   01DA0 FE60            CP      60H             ;<SHIFT>+<@> pressed ?
  7181.   01DA2 CC8403          CALL    Z,0384H         ;Yes: freeze until new key
  7182.                                                 ;pressed.
  7183.   01DA5 329940          LD      (4099H),A       ;Save ASCII code of last key
  7184.   01DA8 3D              DEC     A               ;<BREAK> pressed ? (01H)
  7185.  
  7186.  
  7187. ; STOP statement
  7188. ; --------------
  7189.  
  7190.   01DA9 C0              RET     NZ              ;No: done, return or ?SN Error
  7191.  
  7192. ; <BREAK> pressed
  7193.  
  7194.   01DAA 3C              INC     A               ;A = 01H (key code of BREAK)
  7195.   01DAB C3B41D          JP      1DB4H           ;Continue at 1DB4H
  7196.  
  7197.  
  7198. ; END statement
  7199. ; -------------
  7200.  
  7201.   01DAE C0              RET     NZ              ;?SN Error ?
  7202.  
  7203.   01DAF F5              PUSH    AF              ;Save A (A = 00H !)
  7204.   01DB0 CCBB41          CALL    Z,41BBH         ;DOS
  7205.   01DB3 F1              POP     AF              ;Restore A
  7206.  
  7207.   01DB4 22E640          LD      (40E6H),HL      ;Store PTP in system RAM
  7208.   01DB7 21B540          LD      HL,40B5H        ;Reset string table pointer
  7209.   01DBA 22B340          LD      (40B3H),HL
  7210.   01DBD 21F6FF          LD      HL,0FFF6H       ;--
  7211.  
  7212. ; Entry for STOP in case <BREAK> was pressed during INPUT
  7213.  
  7214. * 01DBE   F6FF          OR      0FFH            ;A <> 0, Z-flag = 0
  7215.   01DC0 C1              POP     BC              ;Remove RET-address
  7216.   01DC1 2AA240          LD      HL,(40A2H)      ;HL = current LN
  7217.   01DC4 E5              PUSH    HL              ;Save current LN
  7218.   01DC5 F5              PUSH    AF              ;Save flags
  7219.   01DC6 7D              LD      A,L             ;LN = 65535 ?
  7220.   01DC7 A4              AND     H               ;(Active command mode)
  7221.   01DC8 3C              INC     A
  7222.   01DC9 2809            JR      Z,1DD4H         ;Yes: no CONT possible
  7223.                                                 ;No:
  7224.   01DCB 22F540          LD      (40F5H),HL      ;Store LN and PTP for CONT
  7225.   01DCE 2AE640          LD      HL,(40E6H)
  7226.   01DD1 22F740          LD      (40F7H),HL
  7227.   01DD4 CD8B03          CALL    038BH           ;End output to printer
  7228.   01DD7 CDF920          CALL    20F9H           ;Start new line
  7229.   01DDA F1              POP     AF              ;Restore flags
  7230.   01DDB 213019          LD      HL,1930H        ;HL -> 'Break'
  7231.                                                 ;STOP ?
  7232.   01DDE C2061A          JP      NZ,1A06H        ;Yes: continue at 1A06H
  7233.                                                 ;No:
  7234.   01DE1 C3181A          JP      1A18H           ;END, back to active command
  7235.                                                 ;mode
  7236.  
  7237. ; CONT statement
  7238. ; --------------
  7239.  
  7240.   01DE4 2AF740          LD      HL,(40F7H)      ;HL = previous PTP
  7241.   01DE7 7C              LD      A,H             ;HL = 0000H ?
  7242.   01DE8 B5              OR      L
  7243.   01DE9 1E20            LD      E,20H           ;E = error code for ?CN Error
  7244.   01DEB CAA219          JP      Z,19A2H         ;Yes: ?CN Error
  7245.  
  7246.   01DEE EB              EX      DE,HL           ;DE = PTP
  7247.   01DEF 2AF540          LD      HL,(40F5H)      ;HL = previous LN
  7248.   01DF2 CDA038          CALL    38A0H           ;Save as current LN and program
  7249.                                                 ;CRTC on last values
  7250.   01DF5 EB              EX      DE,HL           ;HL = PTP
  7251.   01DF6 C9              RET                     ;Execute next command
  7252.  
  7253.  
  7254. ; TRON statement
  7255. ; --------------
  7256.  
  7257.   01DF7 3EAF            LD      A,AFH           ;A <> 0  (trace on)
  7258.  
  7259.  
  7260. ; TROFF statement
  7261. ; ---------------
  7262.  
  7263. * 01DF8   AF            XOR     A               ;A = 0  (trace off)
  7264.   01DF9 321B41          LD      (411BH),A       ;Update TRON/TROFF flag
  7265.   01DFC C9              RET
  7266.  
  7267.  
  7268.   01DFD F1              POP     AF              ;--
  7269.   01DFE E1              POP     HL
  7270.   01DFF C9              RET
  7271.  
  7272.  
  7273. ; DEFSTR statement
  7274. ; ----------------
  7275.  
  7276.   01E00 1E03            LD      E,03H           ;E = VT for STR
  7277.   01E02 011E02          LD      BC,021EH        ;--
  7278.  
  7279.  
  7280. ; DEFINT statement
  7281. ; ----------------
  7282.  
  7283. * 01E03   1E02          LD      E,02H           ;E = VT for INT
  7284.   01E05 011E04          LD      BC,041EH        ;--
  7285.  
  7286.  
  7287. ; DEFSNG statement
  7288. ; ----------------
  7289.  
  7290. * 01E06   1E04          LD      E,04H           ;E = VT for SNG
  7291.   01E08 011E08          LD      BC,081EH        ;--
  7292.  
  7293.  
  7294. ; DEFDBL statement
  7295. ; ----------------
  7296.  
  7297. * 01E09   1E08          LD      E,08H           ;E = VT for DBL
  7298.   01E0B CD3D1E          CALL    1E3DH           ;Check if valid var letter
  7299.   01E0E 019719          LD      BC,1997H        ;First set new return address
  7300.   01E11 C5              PUSH    BC              ;to 1997H (= ?SN Error)
  7301.   01E12 D8              RET     C               ;Invalid character: return
  7302.  
  7303.   01E13 D641            SUB     'A'             ;A = offset for table
  7304.   01E15 4F              LD      C,A             ;C = A
  7305.   01E16 47              LD      B,A             ;B = A
  7306.   01E17 D7              RST     10H             ;Get next character
  7307.   01E18 FECE            CP      0CEH            ;Token for '-' ?
  7308.   01E1A 2009            JR      NZ,1E25H        ;No: set table
  7309.                                                 ;Yes:
  7310.   01E1C D7              RST     10H             ;Get 2nd letter
  7311.   01E1D CD3D1E          CALL    1E3DH           ;Letter found ?
  7312.   01E20 D8              RET     C               ;No: ?SN Error
  7313.                                                 ;Yes
  7314.   01E21 D641            SUB     'A'             ;A = offset for table
  7315.   01E23 47              LD      B,A             ;B = A
  7316.   01E24 D7              RST     10H             ;Increment PTP to next char.
  7317.  
  7318.   01E25 78              LD      A,B             ;A = offset of 2nd letter
  7319.   01E26 91              SUB     C               ;- offset of 1st letter
  7320.   01E27 D8              RET     C               ;?SN Error if the letters were
  7321.                                                 ;not in alphabetical order
  7322.   01E28 3C              INC     A               ;A = counter
  7323.   01E29 E3              EX      (SP),HL         ;Save PTP, remove RET-address
  7324.   01E2A 210141          LD      HL,4101H        ;HL -> table
  7325.   01E2D 0600            LD      B,00H           ;BC = offset to 1st letter
  7326.   01E2F 09              ADD     HL,BC           ;HL -> 1st letter entry
  7327.   01E30 73              LD      (HL),E          ;Set VT in table
  7328.   01E31 23              INC     HL              ;Pointer + 1
  7329.   01E32 3D              DEC     A               ;Counter - 1
  7330.   01E33 20FB            JR      NZ,1E30H        ;Set next table entry
  7331.  
  7332.   01E35 E1              POP     HL              ;Restore PTP
  7333.   01E36 7E              LD      A,(HL)          ;A = next character
  7334.   01E37 FE2C            CP      ','             ;Comma indicated ?
  7335.   01E39 C0              RET     NZ              ;No: done, return
  7336.                                                 ;Yes:
  7337.   01E3A D7              RST     10H             ;Get next character
  7338.   01E3B 18CE            JR      1E0BH           ;And repeat DEF
  7339.  
  7340.  
  7341. ; Test if ASCII character at (HL) is an upper case letter (A-Z)
  7342. ;
  7343. ; I: HL -> ASCII character
  7344. ; O: A = ASCII value of character
  7345. ;    C-flag is 1 if not A-Z
  7346.  
  7347.   01E3D 7E              LD      A,(HL)          ;A = character
  7348.   01E3E FE41            CP      'A'             ;Can it be an letter ?
  7349.   01E40 D8              RET     C               ;No: return
  7350.  
  7351.   01E41 FE5B            CP      'Z'+1           ;Is it a upper case letter ?
  7352.   01E43 3F              CCF                     ;No: C-flag = 1
  7353.   01E44 C9              RET
  7354.  
  7355.  
  7356. ; Convert argument at (HL) to INT number. If negative number then ?FC Error
  7357.  
  7358.   01E45 D7              RST     10H             ;Increment PTP
  7359.   01E46 CD022B          CALL    2B02H           ;Get argument
  7360.   01E49 F0              RET     P               ;If positive then return
  7361.  
  7362. ; ?FC Error
  7363.  
  7364.   01E4A 1E08            LD      E,08H           ;E = error code for ?FC Error
  7365.   01E4C C3A219          JP      19A2H           ;Continue at error routine
  7366.  
  7367.  
  7368. ; Decode line number (number of '.')
  7369.  
  7370.   01E4F 7E              LD      A,(HL)          ;A = character
  7371.   01E50 FE2E            CP      '.'             ;'.' found ?
  7372.   01E52 EB              EX      DE,HL
  7373.   01E53 2AEC40          LD      HL,(40ECH)      ;DE = '.'-LN
  7374.   01E56 EB              EX      DE,HL
  7375.   01E57 CA781D          JP      Z,1D78H         ;Yes: execute RST 10H, done
  7376.  
  7377. ; Decode number at (HL) into DE
  7378.  
  7379.   01E5A 2B              DEC     HL              ;PTP - 1 (because of RST 10H)
  7380.   01E5B 110000          LD      DE,0000H        ;Result = 0
  7381.   01E5E D7              RST     10H             ;Get next character
  7382.                                                 ;Digit found ?
  7383.   01E5F D0              RET     NC              ;No: done, return
  7384.  
  7385.   01E60 E5              PUSH    HL              ;Save PTP
  7386.   01E61 F5              PUSH    AF              ;Save digit
  7387.   01E62 219819          LD      HL,1998H        ;HL = 6552
  7388.   01E65 DF              RST     18H             ;Result already larger than
  7389.                                                 ;6552 ?
  7390.   01E66 DA9719          JP      C,1997H         ;Yes: ?SN Error (with the
  7391.                                                 ;current digit DE would become
  7392.                                                 ;larger than 65529)
  7393.   01E69 62              LD      H,D             ;HL = previous result
  7394.   01E6A 6B              LD      L,E
  7395.   01E6B 19              ADD     HL,DE           ;HL = HL * 10
  7396.   01E6C 29              ADD     HL,HL
  7397.   01E6D 19              ADD     HL,DE
  7398.   01E6E 29              ADD     HL,HL
  7399.   01E6F F1              POP     AF              ;Restore digit
  7400.   01E70 D630            SUB     '0'             ;Convert to numberical value
  7401.   01E72 5F              LD      E,A             ;DE = value
  7402.   01E73 1600            LD      D,00H
  7403.   01E75 19              ADD     HL,DE           ;Add value
  7404.   01E76 EB              EX      DE,HL           ;DE = new result
  7405.   01E77 E1              POP     HL              ;Restore PTP
  7406.   01E78 18E4            JR      1E5EH           ;Get next digit
  7407.  
  7408.  
  7409. ; CLEAR statement
  7410. ; ---------------
  7411.                                                 ;Argument indicated ?
  7412.   01E7A CA611B          JP      Z,1B61H         ;No: continue ar 1B61H
  7413.  
  7414. ; CLEAR with argument
  7415.  
  7416.   01E7D CD461E          CALL    1E46H           ;Get INT-argument,
  7417.                                                 ;if argument < 0 then ?FC Error
  7418.   01E80 2B              DEC     HL              ;PTP - 1
  7419.   01E81 D7              RST     10H             ;End of command reached ?
  7420.   01E82 C0              RET     NZ              ;No: ?SN Error
  7421.  
  7422.   01E83 E5              PUSH    HL              ;Save PTP
  7423.   01E84 2AB140          LD      HL,(40B1H)      ;HL = TOPMEM
  7424.   01E87 7D              LD      A,L             ;DE -> TOPMEM - (size of new
  7425.   01E88 93              SUB     E               ;string space)
  7426.   01E89 5F              LD      E,A
  7427.   01E8A 7C              LD      A,H
  7428.   01E8B 9A              SBC     A,D
  7429.   01E8C 57              LD      D,A
  7430.                                                 ;String space too large ?
  7431.   01E8D DA7A19          JP      C,197AH         ;Yes: ?OM Error
  7432.  
  7433.   01E90 2AF940          LD      HL,(40F9H)      ;HL -> program end
  7434.   01E93 012800          LD      BC,0028H        ;BC = 40
  7435.   01E96 09              ADD     HL,BC           ;HL -> program end + 40
  7436.   01E97 DF              RST     18H             ;Compare HL and DE
  7437.                                                 ;At least 40 bytes remain free?
  7438.   01E98 D27A19          JP      NC,197AH        ;No: ?OM error
  7439.  
  7440.   01E9B EB              EX      DE,HL           ;HL -> new start of string
  7441.                                                 ;space
  7442.   01E9C 22A040          LD      (40A0H),HL      ;Save it in system RAM
  7443.   01E9F E1              POP     HL              ;Restore PTP
  7444.   01EA0 C3611B          JP      1B61H           ;Execute CLEAR, done.
  7445.  
  7446.  
  7447. ; RUN statement
  7448. ; -------------
  7449.                                                 ;Line number indicated ?
  7450.   01EA3 CA5D1B          JP      Z,1B5DH         ;No: continue at 1B5DH
  7451.   01EA6 CDC741          CALL    41C7H           ;DOS
  7452.   01EA9 CD611B          CALL    1B61H           ;CLEAR
  7453.   01EAC 011E1D          LD      BC,1D1EH        ;BC = RET-address on program
  7454.                                                 ;loop
  7455.   01EAF 1810            JR      1EC1H           ;Continue at GOTO
  7456.  
  7457.  
  7458. ; GOSUB statement
  7459. ; ---------------
  7460.  
  7461.   01EB1 0E03            LD      C,03H           ;Still 2 * C bytes free ?
  7462.   01EB3 CD6319          CALL    1963H           ;If not: ?OM Error
  7463.   01EB6 C1              POP     BC              ;BC = RET-address
  7464.   01EB7 E5              PUSH    HL              ;Save PTP (for GOSUB stack)
  7465.   01EB8 E5              PUSH    HL              ;Save PTP
  7466.   01EB9 2AA240          LD      HL,(40A2H)      ;HL = current LN
  7467.   01EBC E3              EX      (SP),HL         ;Save current LN, restore PTP
  7468.   01EBD 3E91            LD      A,91H           ;A = 'GOSUB' token
  7469.   01EBF F5              PUSH    AF              ;Save as marker on stack
  7470.   01EC0 33              INC     SP              ;Remove LSB from stack
  7471.   01EC1 C5              PUSH    BC              ;Put RET-address back
  7472.  
  7473.  
  7474. ; GOTO statement
  7475. ; --------------
  7476.  
  7477.   01EC2 CD5A1E          CALL    1E5AH           ;Decode LN, DE = LN
  7478.   01EC5 CD071F          CALL    1F07H           ;Increment PTP to end of line
  7479.   01EC8 E5              PUSH    HL              ;Save new PTP
  7480.   01EC9 2AA240          LD      HL,(40A2H)      ;HL = current LN
  7481.   01ECC DF              RST     18H             ;Compare current LN with
  7482.                                                 ;required LN
  7483.   01ECD E1              POP     HL              ;Restore new PTP
  7484.   01ECE 23              INC     HL              ;HL = pointer on next line
  7485.                                                 ;Required LN > current LN ?
  7486.   01ECF DC2F1B          CALL    C,1B2FH         ;Yes: search line DE from line
  7487.                                                 ;HL onwards
  7488.   01ED2 D42C1B          CALL    NC,1B2CH        ;No: search line DE from start
  7489.                                                 ;of program onwards
  7490.   01ED5 60              LD      H,B             ;HL = pointer on required line
  7491.   01ED6 69              LD      L,C
  7492.   01ED7 2B              DEC     HL              ;HL - 1
  7493.                                                 ;Line found ?
  7494.   01ED8 D8              RET     C               ;Yes: done, return
  7495.  
  7496. ; ?UL Error
  7497.  
  7498.   01ED9 1E0E            LD      E,0EH           ;E = error code for ?UL Error
  7499.   01EDB C3A219          JP      19A2H           ;Continue at error routine
  7500.  
  7501.  
  7502. ; RETURN statement
  7503. ; ----------------
  7504.  
  7505.   01EDE C0              RET     NZ              ;?SN Error ?
  7506.  
  7507.   01EDF 16FF            LD      D,0FFH          ;Flag <> 0
  7508.   01EE1 CD3619          CALL    1936H           ;Search GOSUB stack
  7509.   01EE4 F9              LD      SP,HL           ;SP = HL (delete GOSUB stack)
  7510.   01EE5 22E840          LD      (40E8H),HL      ;Save new SP in system RAM
  7511.   01EE8 FE91            CP      91H             ;GOSUB stack found ?
  7512.   01EEA 1E04            LD      E,04H           ;E = error code for ?RG Error
  7513.   01EEC C2A219          JP      NZ,19A2H        ;No: ?RG Error
  7514.  
  7515.   01EEF E1              POP     HL              ;HL = LN of gosub line
  7516.   01EF0 22A240          LD      (40A2H),HL      ;Save as current LN
  7517.   01EF3 23              INC     HL              ;LN = 65535 ?
  7518.   01EF4 7C              LD      A,H             ;(GOSUB came from active
  7519.   01EF5 B5              OR      L               ;command mode ?)
  7520.   01EF6 2007            JR      NZ,1EFFH        ;No: increment PTP to next
  7521.                                                 ;command following GOSUB and
  7522.                                                 ;continue program execution
  7523.                                                 ;from there
  7524.   01EF8 3ADD40          LD      A,(40DDH)       ;STOP flag = 0 ?
  7525.   01EFB B7              OR      A
  7526.   01EFC C2181A          JP      NZ,1A18H        ;No: back to active command
  7527.                                                 ;mode
  7528.   01EFF 211E1D          LD      HL,1D1EH        ;HL = RET_address for program
  7529.                                                 ;loop
  7530.   01F02 E3              EX      (SP),HL         ;HL = PTP, RET-address = 1D1EH
  7531.   01F03 3EE1            LD      A,0E1H          ;--
  7532. * 01F04   E1            POP     HL              ;Restore PTP
  7533.  
  7534.  
  7535. ; DATA statement
  7536. ; --------------
  7537.  
  7538. ; Increment PTP (HL) to next command
  7539.  
  7540.   01F05 013A0E          LD      BC,0E3AH        ;C = 3AH (':')
  7541.  
  7542.  
  7543. ; REM statement
  7544. ; -------------
  7545.  
  7546. ; Increment PTP (HL) to next line
  7547.  
  7548. * 01F07     0E00        LD      C,00H           ;C = 00H
  7549. * 01F08       00        NOP                     ;--
  7550.   01F09 0600            LD      B,00H           ;B = 00H
  7551.   01F0B 79              LD      A,C             ;A = search character (= 00H
  7552.                                                 ;when string found, PTP is then
  7553.                                                 ;incremented to end of line
  7554.   01F0C 48              LD      C,B             ;C = 00H (search character when
  7555.                                                 ;string found)
  7556.   01F0D 47              LD      B,A             ;B = search character
  7557.   01F0E 7E              LD      A,(HL)          ;A = next character
  7558.   01F0F B7              OR      A               ;End of line ?
  7559.   01F10 C8              RET     Z               ;Yes: done
  7560.  
  7561.   01F11 B8              CP      B               ;Character found ?
  7562.   01F12 C8              RET     Z               ;Yes: done
  7563.  
  7564.   01F13 23              INC     HL              ;PTP + 1
  7565.   01F14 FE22            CP      22H             ;String found ?
  7566.   01F16 28F3            JR      Z,1F0BH         ;Yes: increment PTP to end
  7567.                                                 ;of line
  7568.   01F18 D68F            SUB     8FH             ;'IF' found ?
  7569.   01F1A 20F2            JR      NZ,1F0EH        ;No: continue search
  7570.  
  7571.   01F1C B8              CP      B               ;If 3AH was searched C-flag = 1
  7572.   01F1D 8A              ADC     A,D             ;A = 00H + D + C-flag
  7573.   01F1E 57              LD      D,A             ;D = counter for nested
  7574.                                                 ;IF THEN ELSE statements
  7575.   01F1F 18ED            JR      1F0EH           ;Continue search
  7576.  
  7577.  
  7578. ; LET statement
  7579. ; -------------
  7580.  
  7581.   01F21 CD0D26          CALL    260DH           ;DE = VARPTR variable
  7582.   01F24 CF              RST     08H             ;Next character must be
  7583.   01F25 D5              DEFB    0D5H            ;the token for '='
  7584.   01F26 EB              EX      DE,HL
  7585.   01F27 22DF40          LD      (40DFH),HL      ;Store VARPTR
  7586.   01F2A EB              EX      DE,HL
  7587.   01F2B D5              PUSH    DE              ;Save VARPTR
  7588.   01F2C E7              RST     20H             ;TSTTYP
  7589.   01F2D F5              PUSH    AF              ;Save type code - 3
  7590.   01F2E CD3723          CALL    2337H           ;Expression to X
  7591.   01F31 F1              POP     AF              ;Restore type code - 3
  7592.  
  7593.   01F32 E3              EX      (SP),HL         ;Save PTP, restore VARPTR
  7594.   01F33 C603            ADD     A,03H           ;Adjust type code
  7595.   01F35 CD1928          CALL    2819H           ;Convert X into desired type
  7596.   01F38 CD030A          CALL    0A03H           ;DE -> LSB (X) for SNG, DBL
  7597.                                                 ;and INT, TSTTYP
  7598.   01F3B E5              PUSH    HL              ;Save VARPTR
  7599.                                                 ;STR type ?
  7600.   01F3C 2028            JR      NZ,1F66H        ;No: continue at 1FF6H
  7601.  
  7602. ; Variable assignment to string variable
  7603.  
  7604.   01F3E 2A2141          LD      HL,(4121H)      ;HL -> vector of new string
  7605.   01F41 E5              PUSH    HL              ;Save pointer
  7606.   01F42 23              INC     HL
  7607.   01F43 5E              LD      E,(HL)          ;DE -> new string
  7608.   01F44 23              INC     HL
  7609.   01F45 56              LD      D,(HL)
  7610.   01F46 2AA440          LD      HL,(40A4H)      ;HL -> start of BASIC program
  7611.   01F49 DF              RST     18H             ;String address < program start?
  7612.                                                 ;(string is in line buffer,
  7613.                                                 ;e.g. with INPUT)
  7614.   01F4A 300E            JR      NC,1F5AH        ;Yes: continue at 1F5AH
  7615.  
  7616.   01F4C 2AA040          LD      HL,(40A0H)      ;HL -> start of string space
  7617.   01F4F DF              RST     18H             ;String address < string space?
  7618.                                                 ;(String constant in program
  7619.                                                 ;text)
  7620.   01F50 D1              POP     DE              ;Restore string pointer
  7621.   01F51 300F            JR      NC,1F62H        ;Yes: continue at 1F62H
  7622.  
  7623.   01F53 2AF940          LD      HL,(40F9H)      ;HL -> start of variable space
  7624.   01F56 DF              RST     18H             ;Vector address of new string
  7625.                                                 ;variable space ? (new string
  7626.                                                 ;is a variable)
  7627.   01F57 3009            JR      NC,1F62H        ;No: continue at 1F62H
  7628.  
  7629.   01F59 3ED1            LD      A,0D1H          ;--
  7630. * 01F5A   D1            POP     DE              ;Restore string pointer
  7631.   01F5B CDF529          CALL    29F5H           ;BC -> last string in string
  7632.                                                 ;table
  7633.   01F5E EB              EX      DE,HL           ;DE -> vector of last string in
  7634.                                                 ;string table
  7635.                                                 ;HL -> vector of new string
  7636.   01F5F CD4328          CALL    2843H           ;Put new string in string space
  7637.                                                 ;and string vector in string
  7638.                                                 ;table
  7639.   01F62 CDF529          CALL    29F5H           ;DE -> vector of new string
  7640.   01F65 E3              EX      (SP),HL         ;HL -> vector of variables
  7641.                                                 ;Save vector of new string
  7642.   01F66 CDD309          CALL    09D3H           ;Copy VT bytes from (DE)
  7643.                                                 ;to (HL)
  7644.   01F69 D1              POP     DE              ;Restore vector to new string
  7645.   01F6A E1              POP     HL              ;Restore PTP
  7646.   01F6B C9              RET
  7647.  
  7648.  
  7649. ; ON statement
  7650. ; ------------
  7651.  
  7652.   01F6C FE9E            CP      9EH             ;Next token = 'ERROR'
  7653.   01F6E 2025            JR      NZ,1F95H        ;No: continue at 1F95H
  7654.  
  7655.  
  7656. ; ON ERROR GOTO
  7657.  
  7658.   01F70 D7              RST     10H             ;Get next non-space character
  7659.   01F71 CF              RST     08H             ;Next byte must be token
  7660.   01F72 8D              DEFB    8DH             ;for 'GOTO'
  7661.   01F73 CD5A1E          CALL    1E5AH           ;Decode line number
  7662.   01F76 7A              LD      A,D             ;Line number = 0 ?
  7663.   01F77 B3              OR      E
  7664.   01F78 2809            JR      Z,1F83H         ;Yes: continue at 1F83H
  7665.  
  7666.   01F7A CD2A1B          CALL    1B2AH           ;Save PTP and search line DE
  7667.   01F7D 50              LD      D,B             ;DE = address of line
  7668.   01F7E 59              LD      E,C
  7669.   01F7F E1              POP     HL              ;Restore PTP
  7670.                                                 ;Line found ?
  7671.   01F80 D2D91E          JP      NC,1ED9H        ;No: ?UL Error
  7672.  
  7673.   01F83 EB              EX      DE,HL
  7674.   01F84 22F040          LD      (40F0H),HL      ;Save line number
  7675.   01F87 EB              EX      DE,HL
  7676.                                                 ;Line number <> 0 ?
  7677.   01F88 D8              RET     C               ;Yes: done, return
  7678.  
  7679.   01F89 3AF240          LD      A,(40F2H)       ;A = error flag
  7680.   01F8C B7              OR      A               ;ON ERROR GOTO active ?
  7681.   01F8D C8              RET     Z               ;No: done, return
  7682.  
  7683.   01F8E 3A9A40          LD      A,(409AH)       ;A = last error code
  7684.   01F91 5F              LD      E,A             ;into E
  7685.   01F92 C3AB19          JP      19ABH           ;Process error
  7686.  
  7687.  
  7688. ; ON GOTO / GOSUB
  7689.  
  7690.   01F95 CD1C2B          CALL    2B1CH           ;DE = argument (0 - 255)
  7691.   01F98 7E              LD      A,(HL)          ;A = next character
  7692.   01F99 47              LD      B,A             ;B = A
  7693.   01F9A FE91            CP      91H             ;'GOSUB' token ?
  7694.   01F9C 2803            JR      Z,1FA1H         ;Yes: continue at 1F1AH
  7695.                                                 ;No:
  7696.   01F9E CF              RST     08H             ;It must be the
  7697.   01F9F 8D              DEFB    8DH             ;'GOTO' token
  7698.   01FA0 2B              DEC     HL              ;PTP - 1 (because of RST 08H)
  7699.   01FA1 4B              LD      C,E             ;C = argument (counter)
  7700.   01FA2 0D              DEC     C               ;Counter - 1
  7701.   01FA3 78              LD      A,B             ;A = token
  7702.                                                 ;Counter = 0 ?
  7703.   01FA4 CA601D          JP      Z,1D60H         ;Yes: HL points to required LN
  7704.                                                 ;so execute command
  7705.   01FA7 CD5B1E          CALL    1E5BH           ;By decoding the number at (HL)
  7706.                                                 ;increment PTP to the next
  7707.                                                 ;character following the number
  7708.   01FAA FE2C            CP      ','             ;Separator must be a comma
  7709.   01FAC C0              RET     NZ              ;Otherwise execute next command
  7710.  
  7711.   01FAD 18F3            JR      1FA2H           ;Required LN reached ?
  7712.  
  7713.  
  7714. ; RESUME statement
  7715. ; ----------------
  7716.  
  7717.   01FAF 11F240          LD      DE,40F2H        ;DE -> ON ERROR GOT flag
  7718.   01FB2 1A              LD      A,(DE)          ;A = ON ERROR GOTO flag
  7719.   01FB3 B7              OR      A               ;ON ERROR GOTO active ?
  7720.   01FB4 CAA019          JP      Z,19A0H         ;No: ?RW Error
  7721.  
  7722.   01FB7 3C              INC     A               ;A + 1
  7723.   01FB8 329A40          LD      (409AH),A       ;Set last error code <> 0
  7724.   01FBB 12              LD      (DE),A          ;Set error flag <> 0
  7725.   01FBC 7E              LD      A,(HL)          ;A = next program character
  7726.   01FBD FE87            CP      87H             ;'NEXT' token ?
  7727.   01FBF 280C            JR      Z,1FCDH         ;Yes: continue at 1FCDH
  7728.  
  7729.   01FC1 CD5A1E          CALL    1E5AH           ;Decode line number
  7730.   01FC4 C0              RET     NZ              ;?SN Error ?
  7731.  
  7732.   01FC5 7A              LD      A,D             ;Line number = 0 ?
  7733.   01FC6 B3              OR      E
  7734.   01FC7 C2C51E          JP      NZ,1EC5H        ;No: execute GOTO
  7735.                                                 ;Yes:
  7736.   01FCA 3C              INC     A               ;A = 1 (Z-flag = 0)
  7737.   01FCB 1802            JR      1FCFH           ;Execute RESUME NEXT
  7738.  
  7739. ; RESUME NEXT
  7740.  
  7741.   01FCD D7              RST     10H             ;Increment PTP
  7742.   01FCE C0              RET     NZ              ;?SN Error ?
  7743.  
  7744.   01FCF 2AEE40          LD      HL,(40EEH)      ;HL = PTP on next command
  7745.   01FD2 EB              EX      DE,HL           ;DE = HL
  7746.   01FD3 2AEA40          LD      HL,(40EAH)      ;HL = LN of error line
  7747.   01FD6 22A240          LD      (40A2H),HL      ;Store as current LN
  7748.   01FD9 EB              EX      DE,HL           ;HL = PTP
  7749.   01FDA C0              RET     NZ              ;Done if RESUME 0
  7750.  
  7751.   01FDB 7E              LD      A,(HL)          ;A = next character from
  7752.                                                 ;program text
  7753.   01FDC B7              OR      A               ;End of line ?
  7754.   01FDD 2004            JR      NZ,1FE3H        ;No: continue at 1FE3H
  7755.                                                 ;Yes:
  7756.   01FDF 23              INC     HL              ;Increment PTP to
  7757.   01FE0 23              INC     HL              ;next line
  7758.   01FE1 23              INC     HL
  7759.   01FE2 23              INC     HL
  7760.   01FE3 23              INC     HL
  7761.   01FE4 7A              LD      A,D             ;Error line = 65535 ?
  7762.   01FE5 A3              AND     E
  7763.   01FE6 3C              INC     A
  7764.   01FE7 C2051F          JP      NZ,1F05H        ;No: increment PTP to next
  7765.                                                 ;command and continue program
  7766.                                                 ;execution
  7767.   01FEA 3ADD40          LD      A,(40DDH)       ;A = STOP flag
  7768.   01FED 3D              DEC     A               ;A = 1 ?
  7769.   01FEE CABE1D          JP      Z,1DBEH         ;Yes: STOP
  7770.                                                 ;No:
  7771.   01FF1 C3051F          JP      1F05H           ;Continue program execution
  7772.                                                 ;at next command
  7773.  
  7774. ; ERROR statement
  7775. ; ---------------
  7776.  
  7777.   01FF4 CD1C2B          CALL    2B1CH           ;DE = argument (0 - 255)
  7778.   01FF7 C0              RET     NZ              ;?SN Error ?
  7779.  
  7780.   01FF8 B7              OR      A               ;ERROR 0 ?
  7781.   01FF9 CA4A1E          JP      Z,1E4AH         ;YEs: ?FC Error
  7782.  
  7783.   01FFC 3D              DEC     A               ;A = A - 1
  7784.   01FFD 87              ADD     A,A             ;A = A * 2
  7785.   01FFE 5F              LD      E,A             ;E = (argument - 1 ) * 2
  7786.   01FFF FE2D            CP      2DH             ;Errorcode > 44H (out of range)
  7787.   02001 3802            JR      C,2005H         ;No: process error code
  7788.                                                 ;Yes:
  7789.   02003 1E26            LD      E,26H           ;E = error code for ?UE Error
  7790.   02005 C3A219          JP      19A2H           ;Continue at error routine
  7791.  
  7792.  
  7793. ; AUTO statement
  7794. ; --------------
  7795.  
  7796.   02008 110A00          LD      DE,000AH        ;DE = default first LN
  7797.   0200B D5              PUSH    DE              ;Save first LN
  7798.                                                 ;Any numbers indicated ?
  7799.   0200C 2817            JR      Z,2025H         ;No: continue at 2025H
  7800.  
  7801.   0200E CD4F1E          CALL    1E4FH           ;DE = indicated first LN
  7802.   02011 EB              EX      DE,HL           ;HL = LN, DE = PTP
  7803.   02012 E3              EX      (SP),HL         ;Save first LN, HL = default
  7804.                                                 ;first LN. Distance indicated ?
  7805.   02013 2811            JR      Z,2026H         ;No: continue at 2026H
  7806.  
  7807.   02015 EB              EX      DE,HL           ;PTP back to HL
  7808.   02016 CF              RST     08H             ;Next character must be
  7809.   02017 2C              DEFB    ','             ;a comma
  7810.   02018 EB              EX      DE,HL
  7811.   02019 2AE440          LD      HL,(40E4H)      ;DE = previous distance
  7812.   0201C EB              EX      DE,HL
  7813.                                                 ;use previous distance ?
  7814.                                                 ;(AUTO XX, )
  7815.   0201D 2806            JR      Z,2025H         ;Yes: continue at 2025H
  7816.  
  7817.   0201F CD5A1E          CALL    1E5AH           ;DE = distance
  7818.   02022 C29719          JP      NZ,1997H        ;?SN Error
  7819.  
  7820.   02025 EB              EX      DE,HL           ;DE = PTP, HL = distance
  7821.   02026 7C              LD      A,H             ;Distance =0 ?
  7822.   02027 B5              OR      L
  7823.   02028 CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  7824.  
  7825.   0202B 22E440          LD      (40E4H),HL      ;Store distance in system RAM
  7826.   0202E 32E140          LD      (40E1H),A       ;Set AUTO flag <> 0
  7827.   02031 E1              POP     HL              ;Restore first LN
  7828.   02032 22E240          LD      (40E2H),HL      ;and store it in system RAM
  7829.   02035 C1              POP     BC              ;Delete RET-address
  7830.   02036 C3331A          JP      1A33H           ;Input line
  7831.  
  7832.  
  7833. ; IF statement
  7834. ; ------------
  7835.  
  7836.   02039 CD3723          CALL    2337H           ;X = expression
  7837.                                                 ;(if condition is false then
  7838.                                                 ;X = 0 else X = -1)
  7839.   0203C 7E              LD      A,(HL)          ;Get next character
  7840.   0203D FE2C            CP      ','             ;Is it a comma ?
  7841.   0203F CC781D          CALL    Z,1D78H         ;Yes: increment PTP (like
  7842.                                                 ;with 'THEN')
  7843.   02042 FECA            CP      0CAH            ;'THEN' token ?
  7844.   02044 CC781D          CALL    Z,1D78H         ;Yes: increment PTP
  7845.  
  7846.   02047 2B              DEC     HL              ;PTP - 1
  7847.   02048 E5              PUSH    HL              ;Save PTP
  7848.   02049 CD9409          CALL    0994H           ;TEST1
  7849.   0204C E1              POP     HL              ;Restore PTP
  7850.                                                 ;X = 0 ? (condition: false)
  7851.   0204D 2807            JR      Z,2056H         ;Yes: continue at 2056H
  7852.  
  7853. ; IF-condition is true
  7854.  
  7855.   0204F D7              RST     10H             ;Increment PTP
  7856.                                                 ;Number indicated ?
  7857.   02050 DAC21E          JP      C,1EC2H         ;Yes: continue at GOTO
  7858.                                                 ;No:
  7859.   02053 C35F1D          JP      1D5FH           ;execute command token
  7860.  
  7861. ; IF-condition is false
  7862.  
  7863.   02056 1601            LD      D,01H           ;D = counter for nested
  7864.                                                 ;IF-THEN-ELSE conditions
  7865.   02058 CD051F          CALL    1F05H           ;Increment PTP to next command
  7866.   0205B B7              OR      A               ;End of line reached ?
  7867.   0205C C8              RET     Z               ;Yes: done, return
  7868.  
  7869.   0205D D7              RST     10H             ;A = next character
  7870.   0205E FE95            CP      95H             ;'ELSE' token found ?
  7871.   02060 20F6            JR      NZ,2058H        ;No: continue search
  7872.  
  7873.   02062 15              DEC     D               ;Nesting level counter - 1
  7874.   02063 20F3            JR      NZ,2058H        ;Search outermost 'ELSE'
  7875.  
  7876.   02065 18E8            JR      204FH           ;Execute following command
  7877.  
  7878.  
  7879. ; LPRINT statement
  7880. ; ----------------
  7881.  
  7882.   02067 3E01            LD      A,01H           ;Set output flag to
  7883.   02069 329C40          LD      (409CH),A       ;printer output
  7884.   0206C C39B20          JP      209BH           ;Continue at PRINT
  7885.  
  7886.  
  7887. ; PRINT statement
  7888. ; ---------------
  7889.  
  7890.   0206F CDCA41          CALL    41CAH           ;DOS
  7891.   02072 FE40            CP      40H             ;PRINT@ ?
  7892.   02074 2019            JR      NZ,208FH        ;No: continue at 208FH
  7893.  
  7894.   02076 CD012B          CALL    2B01H           ;Get argument
  7895.   02079 E5              PUSH    HL              ;Save PTP
  7896.   0207A C3D430          JP      30D4H           ;Test argument and restore
  7897.   0207D 00              NOP                     ;PTP (returns to 207EH)
  7898.   0207E E5              PUSH    HL              ;Save PTP
  7899.   0207F 210044          LD      HL,4400H        ;HL -> start of screen mem
  7900.   02082 19              ADD     HL,DE           ;Add @-argument
  7901.   02083 222040          LD      (4020H),HL      ;Store new cursor position
  7902.   02086 CD2A36          CALL    362AH           ;Calculate new POS
  7903.   02089 32A640          LD      (40A6H),A       ;and save it in system RAM
  7904.   0208C E1              POP     HL              ;Restore PTP
  7905.   0208D CF              RST     08H             ;After @ must follow
  7906.   0208E 2C              DEFB    ','             :a comma.
  7907.   0208F FE23            CP      23H             ;PRINT# ?
  7908.   02091 2008            JR      NZ,209BH        ;No, continue at 209BH
  7909.  
  7910.   02093 CDA935          CALL    35A9H           ;Write leader and sync
  7911.   02096 3E80            LD      A,80H           ;Set output flag to
  7912.   02098 329C40          LD      (409CH),A       ;cassette output
  7913.   0209B 2B              DEC     HL              ;PTP - 1
  7914.   0209C D7              RST     10H             ;A = next non space character
  7915.   0209D CCFE20          CALL    Z,20FEH         ;End PRINT if no argument
  7916.   020A0 CA6921          JP      Z,2169H         ;and set next output to screen
  7917.  
  7918.   020A3 FEBF            CP      0BFH            ;'USING' token ?
  7919.   020A5 CABD2C          JP      Z,2CBDH         ;Yes: continue at 2CBDH
  7920.  
  7921.   020A8 FEBC            CP      0BCH            ;'TAB(' token ?
  7922.   020AA CA3721          JP      Z,2137H         ;Yes: continue at 2137H
  7923.  
  7924.   020AD E5              PUSH    HL              ;Save PTP
  7925.   020AE FE2C            CP      ','             ;',' found ?
  7926.   020B0 CA0821          JP      Z,2108H         ;Yes: continue at 2108H
  7927.  
  7928.   020B3 FE3B            CP      ';'             ;';' found ?
  7929.   020B5 CA6421          JP      Z,2164H         ;Yes: continue at 2164H
  7930.  
  7931.   020B8 C1              POP     BC              ;Correct stack
  7932.  
  7933.   020B9 CD3723          CALL    2337H           ;Put argument in X
  7934.   020BC E5              PUSH    HL              ;Save PTP
  7935.   020BD E7              RST     20H             ;TSTTYP
  7936.                                                 ;STR type?
  7937.   020BE 2832            JR      Z,20F2H         ;Yes: continue at 202FH
  7938.  
  7939.   020C0 CDBD0F          CALL    0FBDH           ;Convert number into string
  7940.   020C3 CD6528          CALL    2865H           ;Take string
  7941.   020C6 CDCD41          CALL    41CDH           ;DOS
  7942.   020C9 2A2141          LD      HL,(4121H)      ;HL -> string vector
  7943.   020CC 3A9C40          LD      A,(409CH)       ;A = output flag
  7944.   020CF B7              OR      A               ;Test output flag
  7945.                                                 ;Cassette output ?
  7946.   020D0 FAE920          JP      M,20E9H         ;Yes: continue at 20E9H
  7947.                                                 ;Screen output?
  7948.   020D3 2808            JR      Z,20DDH         ;Yes: continue at 20DDH
  7949.  
  7950. ; Printer output
  7951.  
  7952.   020D5 3A9B40          LD      A,(409BH)       ;A = printer-POS
  7953.   020D8 86              ADD     A,(HL)          ;A = printer-POS + string len
  7954.   020D9 FE84            CP      84H             ;> 132 ?
  7955.   020DB 1809            JR      20E6H           ;Yes: start new line
  7956.  
  7957. ; Screen output
  7958.  
  7959.   020DD 3A9D40          LD      A,(409DH)       ;A = maximum number of
  7960.                                                 ;characters/line
  7961.   020E0 47              LD      B,A             ;B = A
  7962.   020E1 3AA640          LD      A,(40A6H)       ;A = screen-POS
  7963.   020E4 86              ADD     A,(HL)          ;A = screen-POS + string length
  7964.   020E5 B8              CP      B               ;> length of 1 line ?
  7965.   020E6 D4FE20          CALL    NC,20FEH        ;Yes: start new line
  7966.  
  7967. ; Cassette output
  7968.  
  7969.   020E9 CDAA28          CALL    28AAH           ;Output string
  7970.   020EC 3E20            LD      A,20H           ;A = ' '
  7971.   020EE CD2A03          CALL    032AH           ;Print it for separation
  7972.   020F1 B7              OR      A               ;set Z-flag= 0: skip next CALL
  7973.   020F2 CCAA28          CALL    Z,28AAH         ;Print string when argument
  7974.                                                 ;was in STR format
  7975.   020F5 E1              POP     HL              ;Restore PTP
  7976.   020F6 C39B20          JP      209BH           ;Process next argument
  7977.  
  7978. ; Start a new line
  7979.  
  7980.   020F9 3AA640          LD      A,(40A6H)       ;A = screen-POS
  7981.   020FC B7              OR      A               ;POS = 0 ? (new line already
  7982.                                                 ;started)
  7983.   020FD C8              RET     Z               ;Yes: done
  7984.  
  7985.   020FE 3E0D            LD      A,0DH           ;A = Carriage Return
  7986.   02100 CD2A03          CALL    032AH           ;Print it
  7987.   02103 CDD041          CALL    41D0H           ;DOS
  7988.   02106 AF              XOR     A               ;A = 0
  7989.   02107 C9              RET
  7990.  
  7991. ; ',' with PRINT
  7992.  
  7993.   02108 CDD341          CALL    41D3H           ;DOS
  7994.   0210B 3A9C40          LD      A,(409CH)       ;A = output flag
  7995.   0210E B7              OR      A               ;Test output flag
  7996.                                                 ;Cassette output ?
  7997.   0210F F21921          JP      P,2119H         ;No: continue at 2119H
  7998.  
  7999. ; Cassette output
  8000.  
  8001.   02112 3E2C            LD      A,2CH           ;A = ','
  8002.   02114 CD2A03          CALL    032AH           ;Print it
  8003.   02117 184B            JR      2164H           ;Process next argument
  8004.  
  8005. ; PRINT ',' on screen or printer
  8006.  
  8007.   02119 2808            JR      Z,2123H         ;continue at 2123H if screen
  8008.                                                 ;output
  8009. ; Printer output
  8010.  
  8011.   0211B 3A9B40          LD      A,(409BH)       ;A = printer-POS
  8012.   0211E FE70            CP      70H             ;A > 112 ?
  8013.   02120 C32B21          JP      212BH           ;Yes: start new line
  8014.  
  8015. ; Screen output
  8016.  
  8017.   02123 3A9E40          LD      A,(409EH)       ;A = highest TAB position
  8018.   02126 47              LD      B,A             ;B = A
  8019.   02127 3AA640          LD      A,(40A6H)       ;A = screen-POS
  8020.   0212A B8              CP      B               ;POS above highest TAB
  8021.                                                 ;TAB position
  8022.   0212B D4FE20          CALL    NC,20FEH        ;Yes: start a new line
  8023.   0212E 3034            JR      NC,2164H        ;and process next argument
  8024.  
  8025.   02130 D60A            SUB     0AH             ;A = A MOD 10
  8026.   02132 30FC            JR      NC,2130H
  8027.   02134 2F              CPL
  8028.  
  8029.   02135 1823            JR      215AH           ;Print A times a space
  8030.  
  8031. ; PRINT TAB(
  8032.  
  8033.   02137 CD1B2B          CALL    2B1BH           ;DE = TAB argument
  8034.   0213A CDB230          CALL    30B2H           ;E = new POS value
  8035.   0213D CF              RST     08H             ;Next character must be
  8036.   0213E 29              DEFB    ')'             ;a ')'
  8037.   0213F 2B              DEC     HL              ;PTP - 1
  8038.   02140 E5              PUSH    HL              ;Save PTP
  8039.   02141 CDD341          CALL    41D3H           ;DOS
  8040.   02144 3A9C40          LD      A,(409CH)       ;A = output flag
  8041.   02147 B7              OR      A               ;Test output flag
  8042.                                                 ;Cassette output ?
  8043.   02148 FA4A1E          JP      M,1E4AH         ;Yes: ?FC Error
  8044.                                                 ;Screen output ?
  8045.   0214B CA5321          JP      Z,2153H         ;Yes: continue at 2153H
  8046.  
  8047. ; Printer output
  8048.  
  8049.   0214E 3A9B40          LD      A,(409BH)       ;A = printer-POS
  8050.   02151 1803            JR      2156H           ;Continue at 2156H
  8051.  
  8052. ; Screen output
  8053.  
  8054.   02153 3AA640          LD      A,(40A6H)       ;A = screen-POS
  8055.   02156 2F              CPL                     ;A = -A
  8056.   02157 83              ADD     A,E             ;A = E - A
  8057.                                                 ;(new POS - old POS)
  8058.   02158 300A            JR      NC,2164H        ;Done when required POS
  8059.                                                 ;already reached
  8060.   0215A 3C              INC     A               ;A + 1
  8061.   0215B 47              LD      B,A             ;into B as counter
  8062.   0215C 3E20            LD      A,20H           ;A = ' '
  8063.                                                 ;Repeat
  8064.   0215E CD2A03          CALL    032AH           ;Print space
  8065.   02161 05              DEC     B
  8066.   02162 20FA            JR      NZ,215EH        ;Until B = 0
  8067.  
  8068. ; ';' with PRINT, do not generate at new line
  8069.  
  8070.   02164 E1              POP     HL              ;Restore PTP
  8071.   02165 D7              RST     10H             ;Skip spaces/LF etc and get
  8072.                                                 ;next character
  8073.   02166 C3A020          JP      20A0H           ;Continue at 20A0H
  8074.  
  8075. ; Next output back to screen
  8076.  
  8077.   02169 3A9C40          LD      A,(409CH)       ;--
  8078.   0216C B7              OR      A               ;
  8079.   0216D 00              NOP
  8080.   0216E 00              NOP
  8081.   0216F 00              NOP
  8082.   02170 AF              XOR     A               ;Set output flag to
  8083.   02171 329C40          LD      (409CH),A       ;screen output
  8084.   02174 CDBE41          CALL    41BEH           ;DOS
  8085.   02177 C9              RET
  8086.  
  8087. ; Text '?REDO'
  8088.  
  8089.   02178 3F5245444F      DEFB    '?REDO'
  8090.   0217D 0D              DEFB    0DH             ;Carriage return
  8091.   0217E 00              DEFB    00H             ;End of string
  8092.  
  8093.  
  8094. ; Data with INPUT or READ not separated by ','
  8095.  
  8096.   0217F 3ADE40          LD      A,(40DEH)       ;A = READ/INPUT flag
  8097.   02182 B7              OR      A               ;READ?
  8098.   02183 C29119          JP      NZ,1991H        ;Yes: ?SN Error
  8099.  
  8100.   02186 CDB130          CALL    30B1H           ;Test E on correct TAB
  8101.                                                 ;value (???)
  8102.   02189 B7              OR      A               ;A = 0 (???)
  8103.   0218A 1E2A            LD      E,2AH           ;E = error code for ?FD Error
  8104.   0218C CAA219          JP      Z,19A2H         ;Yes: ?FD Error
  8105.  
  8106.   0218F C1              POP     BC              ;No: correct stack
  8107.   02190 217821          LD      HL,2178H        ;HL -> '?REDO'
  8108.   02193 CDA728          CALL    28A7H           ;Print text
  8109.   02196 2AE640          LD      HL,(40E6H)      ;HL = last PTP
  8110.   02199 C9              RET                     ;Repeat last command
  8111.  
  8112.  
  8113. ; INPUT statement
  8114. ; ---------------
  8115.  
  8116.   0219A CD2828          CALL    2828H           ;?ID Error?
  8117.   0219D 7E              LD      A,(HL)          ;A = next character
  8118.   0219E CDD641          CALL    41D6H           ;DOS
  8119.   021A1 D623            SUB     23H             ;Character = '#' ?
  8120.   021A3 32A940          LD      (40A9H),A       ;Flag = 0 when INPUT#
  8121.   021A6 7E              LD      A,(HL)          ;A = character
  8122.   021A7 2020            JR      NZ,21C9H        ;No: continue at 21C9H
  8123.  
  8124. ; INPUT#
  8125.  
  8126.   021A9 CDAF35          CALL    35AFH           ;Evaluate number and
  8127.                                                 ;search for sync and leader
  8128.   021AC E5              PUSH    HL              ;Save PTP
  8129.   021AD 06FA            LD      B,0FAH          ;B = 250 (record length)
  8130.   021AF 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  8131.   021B2 CDED01          CALL    01EDH           ;Read one byte
  8132.   021B5 77              LD      (HL),A          ;Store it in buffer
  8133.   021B6 23              INC     HL              ;Pointer + 1
  8134.   021B7 FE0D            CP      0DH             ;End of record ?
  8135.   021B9 2802            JR      Z,21BDH         ;Yes: continue at 21BDH
  8136.  
  8137.   021BB 10F5            DJNZ    21B2H           ;Read next byte
  8138.  
  8139.   021BD 2B              DEC     HL              ;Pointer - 1
  8140.   021BE 3600            LD      (HL),00H        ;Replace last byte with 00H
  8141.   021C0 00              NOP                     ;--
  8142.   021C1 00              NOP                     ;(I guess motor off call used
  8143.   021C2 00              NOP                     ;to be here in the TRS80)
  8144.   021C3 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  8145.   021C6 2B              DEC     HL              ;Pointer - 1
  8146.   021C7 1822            JR      21EBH           ;Process data
  8147.  
  8148. ; Normal INPUT
  8149.  
  8150.   021C9 01DB21          LD      BC,21DBH        ;Set new RET address
  8151.   021CC C5              PUSH    BC              ;to 21DBH
  8152.   021CD FE22            CP      22H             ;Text to be printed first?
  8153.   021CF C0              RET     NZ              ;No: continue at 21DBH
  8154.  
  8155.   021D0 CD6628          CALL    2866H           ;Get text
  8156.   021D3 CF              RST     08H             ;Text must be followed by
  8157.   021D4 3B              DEFB    ';'             ;a ';'
  8158.   021D5 E5              PUSH    HL              ;Save PTP
  8159.   021D6 CDAA28          CALL    28AAH           ;Print text
  8160.   021D9 E1              POP     HL              ;Restore PTP
  8161.   021DA C9              RET                     ;Continue at 21BDH
  8162.  
  8163.   021DB E5              PUSH    HL              ;Save PTP
  8164.   021DC CDB31B          CALL    1BB3H           ;Print '?' and goto line input
  8165.   021DF C1              POP     BC              ;BC = PTP
  8166.                                                 ;<BREAK> pressed ?
  8167.   021E0 DABE1D          JP      C,1DBEH         ;Yes: continue at STOP
  8168.  
  8169.   021E3 23              INC     HL              ;Pointer + 1
  8170.   021E4 7E              LD      A,(HL)          ;Get first character
  8171.   021E5 B7              OR      A               ;<RETURN> pressed ?
  8172.   021E6 2B              DEC     HL              ;Pointer - 1
  8173.   021E7 C5              PUSH    BC              ;Save PTP
  8174.   021E8 CA041F          JP      Z,1F04H         ;Yes: set PTP to next
  8175.                                                 ;instruction and leave
  8176.                                                 ;variables unchanged
  8177.   021EB 362C            LD      (HL),2CH        ;Put a comma in the buffer
  8178.                                                 ;as a separator
  8179.   021ED 1805            JR      21F4H           ;Continue at 21F4H
  8180.  
  8181.  
  8182. ; READ statement
  8183. ; --------------
  8184.  
  8185.   021EF E5              PUSH    HL              ;Save PTP
  8186.   021F0 2AFF40          LD      HL,(40FFH)      ;HL -> next data value
  8187.   021F3 F6AF            OR      AFH             ;set flag to READ
  8188.  
  8189. * 021F4   AF            XOR     A               ;set flag to INPUT
  8190.   021F5 32DE40          LD      (40DEH),A       ;Store READ/INPUT flag
  8191.   021F8 E3              EX      (SP),HL         ;Restore PTP, save DATA pointer
  8192.   021F9 1802            JR      21FDH           ;Continue at 21FDH
  8193.  
  8194.  
  8195. ; Use next data
  8196. ; DATA pointer = pointer on the data to use
  8197. ; PTP = pointer on variables
  8198.  
  8199.   021FB CF              RST     08H             ;Next character must be
  8200.   021FC 2C              DEFB    ','             ;a comma (separator)
  8201.  
  8202.   021FD CD0D26          CALL    260DH           ;Get address of variable
  8203.                                                 ;indicated after READ/INPUT
  8204.   02200 E3              EX      (SP),HL         ;Save PTP, restore READ pointer
  8205.   02201 D5              PUSH    DE              ;Save variable address
  8206.   02202 7E              LD      A,(HL)          ;A = data character
  8207.   02203 FE2C            CP      ','             ;Separator ?
  8208.   02205 2826            JR      Z,222DH         ;Yes: continue at 222DH
  8209.  
  8210. ; No separator (',') found
  8211.  
  8212.   02207 3ADE40          LD      A,(40DEH)       ;A = READ/INPUT flag
  8213.   0220A B7              OR      A               ;READ ?
  8214.   0220B C29622          JP      NZ,2296H        ;Yes: increment DATA pointer
  8215.                                                 ;to next DATA line
  8216.   0220E 3AA940          LD      A,(40A9H)       ;INPUT# ?
  8217.   02211 B7              OR      A
  8218.   02212 1E06            LD      E,06H           ;E = error code for ?OD Error
  8219.   02214 CAA219          JP      Z,19A2H         ;Yes: issue error
  8220.  
  8221.   02217 3E3F            LD      A,3FH           ;A = '?'
  8222.   02219 CD2A03          CALL    032AH           ;Print it
  8223.   0221C CDB31B          CALL    1BB3H           ;Request missing data ('??')
  8224.   0221F D1              POP     DE              ;Restore variable address
  8225.   02220 C1              POP     BC              ;Restore PTP
  8226.                                                 ;<BREAK> pressed ?
  8227.   02221 DABE1D          JP      C,1DBEH         ;Yes: continue at STOP
  8228.  
  8229.   02224 23              INC     HL              ;Pointer + 1
  8230.   02225 7E              LD      A,(HL)          ;A = first character
  8231.   02226 B7              OR      A               ;<RETURN> pressed ?
  8232.   02227 2B              DEC     HL              ;Pointer - 1
  8233.   02228 C5              PUSH    BC              ;Save PTP
  8234.   02229 CA041F          JP      Z,1F04H         ;Yes: set PTP to next
  8235.                                                 ;instruction and leave
  8236.                                                 ;variables unchanged
  8237.   0222C D5              PUSH    DE              ;No: Save variable address
  8238.   0222D CDDC41          CALL    41DCH           ;DOS
  8239.   02230 E7              RST     20H             ;TSTTYP
  8240.   02231 F5              PUSH    AF              ;Save VT - 3
  8241.                                                 ;STR type ?
  8242.   02232 2019            JR      NZ,224DH        ;No: continue at 224DH
  8243.  
  8244. ; Put data in string variable
  8245.  
  8246.   02234 D7              RST     10H             ;First non-space/LF character
  8247.                                                 ;to A
  8248.   02235 57              LD      D,A             ;D = A
  8249.   02236 47              LD      B,A             ;B = A
  8250.   02237 FE22            CP      22H             ;String delimited by '"'
  8251.   02239 2805            JR      Z,2240H         ;Yes: B = D = string end
  8252.  
  8253.   0223B 163A            LD      D,3AH           ;String end can be ':' (3AH)
  8254.   0223D 062C            LD      B,2CH           ;or ',' (2CH)
  8255.   0223F 2B              DEC     HL              ;Pointer - 1
  8256.   02240 CD6928          CALL    2869H           ;Get string
  8257.   02243 F1              POP     AF              ;Restore VT - 3
  8258.   02244 EB              EX      DE,HL           ;DE = DATA pointer
  8259.   02245 215A22          LD      HL,225AH        ;Set RET address to 225AH
  8260.   02248 E3              EX      (SP),HL         ;HL -> variable
  8261.   02249 D5              PUSH    DE              ;Save DATA pointer
  8262.   0224A C3331F          JP      1F33H           ;Copy new value into variable
  8263.                                                 ;and continue at 225AH
  8264. ; Put data in numerical variable
  8265.  
  8266.   0224D D7              RST     10H             ;Set pointer on first non-space
  8267.                                                 ;character
  8268.   0224E F1              POP     AF              ;Restore VT - 3
  8269.   0224F F5              PUSH    AF              ;And save it again
  8270.   02250 014322          LD      BC,2243H        ;Set new RET address to 2243H
  8271.   02253 C5              PUSH    BC              ;(copy value into variable)
  8272.                                                 ;INT or SNG value ?
  8273.   02254 DA6C0E          JP      C,0E6CH         ;Yes: get INT or SNG value
  8274.                                                 ;DBL value
  8275.   02257 D2650E          JP      NC,0E65H        ;Yes: get DBL VALUE
  8276.  
  8277. ; Continuation after getting data
  8278.  
  8279.   0225A 2B              DEC     HL              ;PTP - 1
  8280.   0225B D7              RST     10H             ;End of line reached ?
  8281.   0225C 2805            JR      Z,2263H         ;Yes: continue at 2263H
  8282.  
  8283.   0225E FE2C            CP      2CH             ;Separator found ?
  8284.   02260 C27F21          JP      NZ,217FH        ;No: process error at 217FH
  8285.  
  8286.   02263 E3              EX      (SP),HL         ;Save DATA pointer, HL = PTP
  8287.   02264 2B              DEC     HL              ;PTP - 1
  8288.   02265 D7              RST     10H             ;End of READ/INPUT ?
  8289.   02266 C2FB21          JP      NZ,21FBH        ;No: process next variable
  8290.  
  8291.   02269 D1              POP     DE              ;Yes: restore DATA pointer
  8292.   0226A 00              NOP                     ;--
  8293.   0226B 00              NOP
  8294.   0226C 00              NOP
  8295.   0226D 00              NOP
  8296.   0226E 00              NOP
  8297.   0226F 3ADE40          LD      A,(40DEH)       ;A = READ/INPUT flag
  8298.   02272 B7              OR      A               ;READ ?
  8299.   02273 EB              EX      DE,HL           ;DE = PTP, HL = DATA pointer
  8300.   02274 C2961D          JP      NZ,1D96H        ;Yes: continue at 1D96H
  8301.  
  8302.   02277 D5              PUSH    DE              ;No: save PTP
  8303.   02278 CDDF41          CALL    41DFH           ;DOS
  8304.   0227B B6              OR      (HL)            ;Still data remaining in
  8305.                                                 ;the buffer ?
  8306.   0227C 218622          LD      HL,2286H        ;HL -> '?Extra ignored'
  8307.   0227F C4A728          CALL    NZ,28A7H        ;Yes: print text
  8308.   02282 E1              POP     HL              ;Restore PTP
  8309.   02283 C36921          JP      2169H           ;Direct next output to screen
  8310.  
  8311.  
  8312. ; Text '?Extra ignored'
  8313.  
  8314.   02286 3F4578747261    DEFB    '?Extra'
  8315.   0228C 20              DEFB    ' '
  8316.   0228D 69676E6F726564  DEFB    'ignored'
  8317.   02294 0D              DEFB    0DH             ;Carriage Return
  8318.   02295 00              DEFB    00H             ;End of string
  8319.  
  8320.  
  8321. ; Increment DATA pointer to next DATA line
  8322.  
  8323.   02296 CD051F          CALL    1F05H           ;Increment HL to end of
  8324.                                                 ;command or end of line
  8325.   02299 B7              OR      A               ;End of line reached ?
  8326.   0229A 2012            JR      NZ,22AEH        ;No: continue at 22AEH
  8327.  
  8328.   0229C 23              INC     HL              ;Program end reached ?
  8329.   0229D 7E              LD      A,(HL)          ;(Line pointer = 0000H ?)
  8330.   0229E 23              INC     HL
  8331.   0229F B6              OR      (HL)
  8332.   022A0 1E06            LD      E,06H           ;E = error code for ?OD Error
  8333.   022A2 CAA219          JP      Z,19A2H         ;Yes: issue ?OD Error
  8334.                                                 ;No:
  8335.   022A5 23              INC     HL              ;DE = line number
  8336.   022A6 5E              LD      E,(HL)
  8337.   022A7 23              INC     HL
  8338.   022A8 56              LD      D,(HL)
  8339.   022A9 EB              EX      DE,HL           ;Store line number of
  8340.   022AA 22DA40          LD      (40DAH),HL      ;DATA line
  8341.   022AD EB              EX      DE,HL
  8342.   022AE D7              RST     10H             ;DATA line found ?
  8343.   022AF FE88            CP      88H             ;First char. = 'DATA' token ?
  8344.   022B1 20E3            JR      NZ,2296H        ;No: continue search
  8345.                                                 ;Yes:
  8346.   022B3 C32D22          JP      222DH           ;HL = new DATA pointer
  8347.  
  8348.  
  8349. ; NEXT statement
  8350. ; --------------
  8351.  
  8352.   022B6 110000          LD      DE,0000H        ;Default address in case no
  8353.                                                 ;loop variable is indicated
  8354.   022B9 C40D26          CALL    NZ,260DH        ;Get variable address in case
  8355.                                                 ;loop variable is indicated
  8356.   022BC 22DF40          LD      (40DFH),HL      ;Save PTP
  8357.   022BF CD3619          CALL    1936H           ;Search FOR stack with
  8358.                                                 ;VARPTR = DE (next FOR stack
  8359.                                                 ;in case DE = 0)
  8360.                                                 ;FOR stack found ?
  8361.   022C2 C29D19          JP      NZ,199DH        ;No: ?NF Error
  8362.                                                 ;Yes:
  8363.   022C5 F9              LD      SP,HL           ;SP = FOR stack pointer
  8364.   022C6 22E840          LD      (40E8H),HL      ;Save SP value
  8365.   022C9 D5              PUSH    DE              ;Save variable address
  8366.   022CA 7E              LD      A,(HL)          ;A = step-SGN
  8367.   022CB 23              INC     HL              ;Pointer + 1
  8368.   022CC F5              PUSH    AF              ;Save step-SGN
  8369.   022CD D5              PUSH    DE              ;Save variable address
  8370.   022CE 7E              LD      A,(HL)          ;Get variable type
  8371.   022CF 23              INC     HL              ;Pointer + 1
  8372.   022D0 B7              OR      A               ;Loop variable type INT ?
  8373.   022D1 FAEA22          JP      M,22EAH         ;Yes: continue at 22EAH
  8374.  
  8375. ; NEXT for SNG-variable
  8376.  
  8377.   022D4 CDB109          CALL    09B1H           ;X = BCDE + (HL)
  8378.                                                 ;(Step value into X)
  8379.   022D7 E3              EX      (SP),HL         ;Save pointer, HL = VARPTR
  8380.   022D8 E5              PUSH    HL              ;Save address of loop var.
  8381.   022D9 CD0B07          CALL    070BH           ;X = X + (HL), add step value
  8382.                                                 ;to loop variable
  8383.   022DC E1              POP     HL              ;Restore address
  8384.   022DD CDCB09          CALL    09CBH           ;(HL) = X (copy new value
  8385.                                                 ;into loop variable
  8386.   022E0 E1              POP     HL              ;Restore pointer (now on end
  8387.                                                 ;value)
  8388.   022E1 CDC209          CALL    09C2H           ;BCDE = (HL) = end value
  8389.   022E4 E5              PUSH    HL              ;Save pointer
  8390.   022E5 CD0C0A          CALL    0A0CH           ;CP X,BCDE (compare loop
  8391.                                                 ;variable with end value)
  8392.   022E8 1829            JR      2313H           ;Continue at 2313H
  8393.  
  8394. ; NEXT for INT-variable
  8395.  
  8396.   022EA 23              INC     HL              ;The first 4 bytes of the FOR
  8397.   022EB 23              INC     HL              ;stack are not used
  8398.   022EC 23              INC     HL
  8399.   022ED 23              INC     HL
  8400.   022EE 4E              LD      C,(HL)          ;BC = step value
  8401.   022EF 23              INC     HL
  8402.   022F0 46              LD      B,(HL)
  8403.   022F1 23              INC     HL
  8404.   022F2 E3              EX      (SP),HL         ;Save pointer, HL = address
  8405.   022F3 5E              LD      E,(HL)          ;DE = loop value
  8406.   022F4 23              INC     HL
  8407.   022F5 56              LD      D,(HL)
  8408.   022F6 E5              PUSH    HL              ;Save address + 1
  8409.   022F7 69              LD      L,C             ;HL = step value
  8410.   022F8 60              LD      H,B
  8411.   022F9 CDD20B          CALL    0BD2H           ;X = HL = HL + DE (add step
  8412.                                                 ;value to loop variable)
  8413.   022FC 3AAF40          LD      A,(40AFH)       ;Overflow ?
  8414.   022FF FE04            CP      04H             ;VT changed to SNG ?
  8415.   02301 CAB207          JP      Z,07B2H         ;Yes: ?OV Error
  8416.                                                 ;No:
  8417.   02304 EB              EX      DE,HL           ;DE = new loop value
  8418.   02305 E1              POP     HL              ;Restore address + 1
  8419.   02306 72              LD      (HL),D          ;Save new loop variable value
  8420.   02307 2B              DEC     HL
  8421.   02308 73              LD      (HL),E
  8422.   02309 E1              POP     HL              ;Restore pointer
  8423.   0230A D5              PUSH    DE              ;Save loop value
  8424.   0230B 5E              LD      E,(HL)          ;DE = end value
  8425.   0230C 23              INC     HL
  8426.   0230D 56              LD      D,(HL)
  8427.   0230E 23              INC     HL
  8428.   0230F E3              EX      (SP),HL         ;Save pointer, HL = loop value
  8429.   02310 CD390A          CALL    0A39H           ;CP HL,DE
  8430.  
  8431.   02313 E1              POP     HL              ;Restore pointer
  8432.   02314 C1              POP     BC              ;B = step-SGN
  8433.                                                 ;B = 01H: End loop if
  8434.                                                 ;         loop val. > end val.
  8435.                                                 ;B = FFH: End loop if
  8436.                                                 ;         loop val. < end val.
  8437.                                                 ;A = FFH: loop val. < end val.
  8438.                                                 ;A = 00H: loop val. = end val.
  8439.                                                 ;A = 01H: loop val. > end val.
  8440.   02315 90              SUB     B               ;Loop ended ?
  8441.   02316 CDC209          CALL    09C2H           ;BCDE = (HL)
  8442.                                                 ;(BC = LP on start of loop
  8443.                                                 ;DE = line number)
  8444.   02319 2809            JR      Z,2324H         ;Yes: continue at 2324H
  8445.  
  8446. ; Loop continues
  8447.  
  8448.   0231B EB              EX      DE,HL           ;HL = line number
  8449.   0231C 22A240          LD      (40A2H),HL      ;Save as current line number
  8450.   0231F 69              LD      L,C             ;HL = line pointer
  8451.   02320 60              LD      H,B
  8452.   02321 C31A1D          JP      1D1AH           ;Rebuild FOR stack and continue
  8453.                                                 ;program execution at (HL)
  8454. ; Loop ends
  8455.  
  8456.   02324 F9              LD      SP,HL           ;SP = end of stack (current
  8457.                                                 ;FOR stack deleted)
  8458.   02325 22E840          LD      (40E8H),HL      ;Save new SP in system RAM
  8459.   02328 2ADF40          LD      HL,(40DFH)      ;HL = PTP
  8460.   0232B 7E              LD      A,(HL)          ;A = next character
  8461.   0232C FE2C            CP      ','             ;Is it a comma ?
  8462.   0232E C21E1D          JP      NZ,1D1EH        ;No: continue program at (HL)
  8463.                                                 ;Yes:
  8464.   02331 D7              RST     10H             ;Increment PTP to next variable
  8465.   02332 CDB922          CALL    22B9H           ;and process next variable
  8466.  
  8467.  
  8468. ; Process expression in parentheses and store result in X
  8469.  
  8470.   02335 CF              RST     08H             ;Next character must be
  8471.   02336 28              DEFB    '('             ;a '('
  8472.  
  8473. ; Process expression and store result in X
  8474.  
  8475.   02337 2B              DEC     HL              ;PTP - 1
  8476.   02338 1600            LD      D,00H           ;Priority flag = 0
  8477.   0233A D5              PUSH    DE              ;Save flag
  8478.   0233B 0E01            LD      C,01H           ;Memory test: still 2 bytes
  8479.   0233D CD6319          CALL    1963H           ;free?
  8480.   02340 CD9F24          CALL    249FH           ;Get 1st argument
  8481.   02343 22F340          LD      (40F3H),HL      ;Save PTP in system RAM
  8482.   02346 2AF340          LD      HL,(40F3H)      ;Restore PTP
  8483.   02349 C1              POP     BC              ;Restore priority flag
  8484.   0234A 7E              LD      A,(HL)          ;A = token following argument
  8485.                                                 ;(operator code)
  8486.   0234B 1600            LD      D,00H           ;Operator flag = 0
  8487.   0234D D6D4            SUB     0D4H            ;Comparison operator ?
  8488.                                                 ;( < , = , > )
  8489.   0234F 3813            JR      C,2364H         ;No: continue at 2364H
  8490.  
  8491.   02351 FE03            CP      03H             ;Token value between
  8492.                                                 ;D4H and D7H ?
  8493.   02353 300F            JR      NC,2364H        ;No: continue at 2364H
  8494.  
  8495. ; Process operators for compare ( < , = , > , =< , => , <= , >= , <> )
  8496.  
  8497.   02355 FE01            CP      01H             ;C-flag = 1 in case of '<'
  8498.   02357 17              RLA                     ;'<' : A = 01 (bit 0 set)
  8499.                                                 ;'=' : A = 02 (bit 1 set)
  8500.                                                 ;'>' : A = 04 (bit 2 set)
  8501.   02358 AA              XOR     D               ;Xor A with last operator.
  8502.                                                 ;A = 00 in case the same
  8503.                                                 ;operator was indicated twice
  8504.   02359 BA              CP      D               ;C-flag = 1 in case A = 00H !!
  8505.   0235A 57              LD      D,A             ;D = new operator code
  8506.                                                 ;Twice the same operator ?
  8507.   0235B DA9719          JP      C,1997H         ;Yes: ?SN Error
  8508.  
  8509.   0235E 22D840          LD      (40D8H),HL      ;Save PTP
  8510.   02361 D7              RST     10H             ;Get next operator
  8511.   02362 18E9            JR      234DH           ;Another comparison operator ?
  8512.  
  8513. ; No operator for compare found
  8514.  
  8515.   02364 7A              LD      A,D             ;A = operator flag
  8516.   02365 B7              OR      A               ;Comparison operators found ?
  8517.   02366 C2EC23          JP      NZ,23ECH        ;Yes: continue at 23ECH
  8518.                                                 ;No:
  8519.   02369 7E              LD      A,(HL)          ;A = operator code
  8520.   0236A 22D840          LD      (40D8H),HL      ;Save PTP
  8521.   0236D D6CD            SUB     0CDH            ;Code found ?
  8522.   0236F D8              RET     C               ;No: done
  8523.  
  8524.   02370 FE07            CP      07H             ;Valid code ( + , - , * , / ,
  8525.                                                 ;arrow up , AND , OR ) ?
  8526.   02372 D0              RET     NC              ;No: done
  8527.  
  8528.   02373 5F              LD      E,A             ;DE = code offset
  8529.                                                 ;(0 = '+', 6 = 'OR')
  8530.   02374 3AAF40          LD      A,(40AFH)       ;A = VT of 1st argument
  8531.   02377 D603            SUB     03H             ;String argument ?
  8532.   02379 B3              OR      E               ;And code = '+' ?
  8533.   0237A CA8F29          JP      Z,298FH         ;Yes: string addition at 298FH
  8534.  
  8535. ; Priority test
  8536.  
  8537.   0237D 219A18          LD      HL,189AH        ;HL -> priority table
  8538.   02380 19              ADD     HL,DE           ;Add offset
  8539.   02381 78              LD      A,B             ;A = old priority
  8540.                                                 ;(at first = 0 !)
  8541.   02382 56              LD      D,(HL)          ;D = current priority
  8542.   02383 BA              CP      D               ;Compare both priorities
  8543.                                                 ;Old priority was higher ?
  8544.   02384 D0              RET     NC              ;Yes: first calculate
  8545.                                                 ;intermediate result
  8546.   02385 C5              PUSH    BC              ;Save old priority
  8547.   02386 014623          LD      BC,2346H        ;Set new RET-address
  8548.   02389 C5              PUSH    BC              ;to 2346H
  8549.   0238A 7A              LD      A,D             ;A = new priority
  8550.   0238B FE7F            CP      7FH             ;Power calculation (^) ?
  8551.   0238D CAD423          JP      Z,23D4H         ;Yes: continue at 23D4H
  8552.  
  8553.   02390 FE51            CP      51H             ;boolean operator (AND, OR) ?
  8554.   02392 DAE123          JP      C,23E1H         ;Yes: continue at 23E1H
  8555.  
  8556. ; Process operators '+', '-', '*' and '/'
  8557. ; Store 1st argument or intermediate result on stack
  8558.  
  8559.   02395 212141          LD      HL,4121H        ;HL -> X
  8560.   02398 B7              OR      A               ;C-flag = 0
  8561.   02399 3AAF40          LD      A,(40AFH)       ;A = VT
  8562.   0239C 3D              DEC     A               ;- 3
  8563.   0239D 3D              DEC     A
  8564.   0239E 3D              DEC     A               ;Type of X = STR ?
  8565.   0239F CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error (for strings
  8566.                                                 ;only '+' is allowed and this
  8567.                                                 ;operator has already been
  8568.                                                 ;checked)
  8569.   023A2 4E              LD      C,(HL)          ;BC = INT value
  8570.   023A3 23              INC     HL
  8571.   023A4 46              LD      B,(HL)
  8572.   023A5 C5              PUSH    BC              ;INT value on stack
  8573.                                                 ;Type of X = INT ?
  8574.   023A6 FAC523          JP      M,23C5H         ;Yes: done
  8575.  
  8576.   023A9 23              INC     HL              ;BC = MSBs of SNG value
  8577.   023AA 4E              LD      C,(HL)
  8578.   023AB 23              INC     HL
  8579.   023AC 46              LD      B,(HL)
  8580.   023AD C5              PUSH    BC              ;MSBs of SNG-value on stack
  8581.   023AE F5              PUSH    AF              ;Save VT - 3
  8582.   023AF B7              OR      A               ;Type of X = SNG ?
  8583.   023B0 E2C423          JP      PO,23C4H        ;Yes: done
  8584.  
  8585.   023B3 F1              POP     AF              ;Restore VT - 3
  8586.   023B4 23              INC     HL
  8587.   023B5 3803            JR      C,23BAH         ;Jump is never executed
  8588.   023B7 211D41          LD      HL,411DH        ;Save LSBs of DBL value
  8589.   023BA 4E              LD      C,(HL)          ;on stack
  8590.   023BB 23              INC     HL
  8591.   023BC 46              LD      B,(HL)
  8592.   023BD 23              INC     HL
  8593.   023BE C5              PUSH    BC
  8594.   023BF 4E              LD      C,(HL)
  8595.   023C0 23              INC     HL
  8596.   023C1 46              LD      B,(HL)
  8597.   023C2 C5              PUSH    BC
  8598.   023C3 06F1            LD      B,0F1H          ;--
  8599.  
  8600. * 023C4   F1            POP     AF              ;Restore VT - 3 (with INT- or
  8601.                                                 ;SNG-value)
  8602.   023C5 C603            ADD     A,03H           ;A = VT
  8603.   023C7 4B              LD      C,E             ;C = offset of operator code
  8604.   023C8 47              LD      B,A             ;B = VT
  8605.   023C9 C5              PUSH    BC              ;Save both on stack
  8606.   023CA 010624          LD      BC,2406H        ;Set new RET-address
  8607.   023CD C5              PUSH    BC              ;to 2406H
  8608.   023CE 2AD840          LD      HL,(40D8H)      ;Restore PTP
  8609.   023D1 C33A23          JP      233AH           ;Process next argument
  8610.  
  8611.   023D4 CDB10A          CALL    0AB1H           ;X = CSNG (X)
  8612.   023D7 CDA409          CALL    09A4H           ;(SP) = X
  8613.   023DA 01F213          LD      BC,13F2H        ;BC -> power function
  8614.   023DD 167F            LD      D,7FH           ;D = priority code
  8615.   023DF 18EC            JR      23CDH           ;Get 2nd argument and
  8616.                                                 ;execute function
  8617.  
  8618. ; Process logical expression
  8619.  
  8620.   023E1 D5              PUSH    DE              ;Save priority code
  8621.   023E2 CD7F0A          CALL    0A7FH           ;HL = X = CINT(X)
  8622.   023E5 D1              POP     DE              ;Restore priority code
  8623.   023E6 E5              PUSH    HL              ;Save 1st argument
  8624.   023E7 01E925          LD      BC,25E9H        ;BC -> AND/OR routine
  8625.   023EA 18E1            JR      23CDH           ;Get 2nd argument and
  8626.                                                 ;process logical expression
  8627.  
  8628. ; Process compare operators
  8629.  
  8630.   023EC 78              LD      A,B             ;A = priority code
  8631.   023ED FE64            CP      64H             ;Last operator of higher
  8632.                                                 ;priority?
  8633.   023EF D0              RET     NC              ;Yes: compute intermediate
  8634.                                                 ;result
  8635.   023F0 C5              PUSH    BC              ;No: save priority code
  8636.   023F1 D5              PUSH    DE              ;Save operator code
  8637.   023F2 110464          LD      DE,6404H        ;D = current priority
  8638.                                                 ;E = offset for basic
  8639.                                                 ;arithmatic (see table
  8640.                                                 ;at 18ABH)
  8641.   023F5 21B825          LD      HL,25B8H        ;Set new RET-address
  8642.   023F8 E5              PUSH    HL              ;to 25B8H
  8643.   023F9 E7              RST     20H             ;TSTTYP
  8644.                                                 ;STR type?
  8645.   023FA C29523          JP      NZ,2395H        ;No: continue at 2395H
  8646.                                                 ;Yes:
  8647.   023FD 2A2141          LD      HL,(4121H)      ;HL -> string vector
  8648.   02400 E5              PUSH    HL              ;Save pointer
  8649.   02401 018C25          LD      BC,258CH        ;Set RET-address to 258CH
  8650.                                                 ;(string compare)
  8651.   02404 18C7            JR      23CDH           ;Get 2nd argument and execute
  8652.                                                 ;function
  8653.  
  8654. ; Compute intermediate result
  8655. ; 1st value: on stack, 2nd value: in X
  8656.  
  8657.   02406 C1              POP     BC              ;Restore VT of 1st argument
  8658.   02407 79              LD      A,C             ;A = operator code offset
  8659.   02408 32B040          LD      (40B0H),A       ;Save in system RAM
  8660.   0240B 78              LD      A,B             ;A = VT
  8661.   0240C FE08            CP      08H             ;1st argument in DBL format ?
  8662.   0240E 2828            JR      Z,2438H         ;Yes: continue at 2438H
  8663.  
  8664.   02410 3AAF40          LD      A,(40AFH)       ;A = VT of 2nd argument
  8665.   02413 FE08            CP      08H             ;2nd argument in DBL format ?
  8666.   02415 CA6024          JP      Z,2460H         ;Yes: continue at 2460H
  8667.  
  8668.   02418 57              LD      D,A             ;D = VT of 2nd argument
  8669.   02419 78              LD      A,B             ;A = VT of 1st argument
  8670.   0241A FE04            CP      04H             ;1st argument in SNG format ?
  8671.   0241C CA7224          JP      Z,2472H         ;Yes: continue at 2472H
  8672.  
  8673.   0241F 7A              LD      A,D             ;A = VT of 2nd argument
  8674.   02420 FE03            CP      03H             ;2nd argument in STR format ?
  8675.   02422 CAF60A          JP      Z,0AF6H         ;Yes: ?TM Error
  8676.                                                 ;2nd argument in SNG format ?
  8677.   02425 D27C24          JP      NC,247CH        ;Yes: continue at 247CH
  8678.  
  8679. ; 1st or 2nd argument in INT format
  8680.  
  8681.   02428 21BF18          LD      HL,18BFH        ;HL -> jump table for basic
  8682.                                                 ;arithmatic (INT)
  8683.   0242B 0600            LD      B,00H           ;BC = operator offset
  8684.   0242D 09              ADD     HL,BC           ;Add offset twice
  8685.   0242E 09              ADD     HL,BC           ;(2 bytes address)
  8686.   0242F 4E              LD      C,(HL)          ;BC = address
  8687.   02430 23              INC     HL
  8688.   02431 46              LD      B,(HL)
  8689.   02432 D1              POP     DE              ;DE = 1st argument
  8690.   02433 2A2141          LD      HL,(4121H)      ;HL = 2nd argument
  8691.   02436 C5              PUSH    BC              ;Put address on stack
  8692.   02437 C9              RET                     ;and execute routine
  8693.  
  8694. ; 1st argument in DBL format
  8695.  
  8696.   02438 CDDB0A          CALL    0ADBH           ;X = CDBL (X) (convert 2nd
  8697.                                                 ;argument to DBL)
  8698.   0243B CDFC09          CALL    09FCH           ;Y = X
  8699.   0243E E1              POP     HL              ;Get 1st argument from stack
  8700.   0243F 221F41          LD      (411FH),HL      ;and load into X
  8701.   02442 E1              POP     HL
  8702.   02443 221D41          LD      (411DH),HL
  8703.   02446 C1              POP     BC
  8704.   02447 D1              POP     DE
  8705.   02448 CDB409          CALL    09B4H           ;X = BCDE
  8706.   0244B CDDB0A          CALL    0ADBH           ;X = CDBL (X) (convert 1st
  8707.                                                 ;argument to DBL)
  8708.   0244E 21AB18          LD      HL,18ABH        ;HL -> jump table for basic
  8709.                                                 ;arithmatic (DBL)
  8710.   02451 3AB040          LD      A,(40B0H)       ;A = operator code offset
  8711.   02454 07              RLCA                    ;* 2 (2 bytes offset)
  8712.   02455 C5              PUSH    BC              ;Save BC
  8713.   02456 4F              LD      C,A             ;BC = table offset
  8714.   02457 0600            LD      B,00H
  8715.   02459 09              ADD     HL,BC           ;Add offset
  8716.   0245A C1              POP     BC              ;Restore BC
  8717.   0245B 7E              LD      A,(HL)          ;HL -> routine
  8718.   0245C 23              INC     HL
  8719.   0245D 66              LD      H,(HL)
  8720.   0245E 6F              LD      L,A
  8721.   0245F E9              JP      (HL)            ;Execute routine
  8722.  
  8723. ; 2nd argument in DBL format
  8724.  
  8725.   02460 C5              PUSH    BC              ;Save VT offset
  8726.   02461 CDFC09          CALL    09FCH           ;Y = X (2nd argument to X)
  8727.   02464 F1              POP     AF              ;Restore VT
  8728.   02465 32AF40          LD      (40AFH),A       ;Save VT in system RAM
  8729.   02468 FE04            CP      04H             ;1st argument in SNG format ?
  8730.   0246A 28DA            JR      Z,2446H         ;Yes: get 1st argument from
  8731.                                                 ;stack and convert to DBL
  8732.   0246C E1              POP     HL              ;No: 1st argument is an INT:
  8733.                                                 ;Get 1st argument
  8734.   0246D 222141          LD      (4121H),HL      ;Put it into X
  8735.   02470 18D9            JR      244BH           ;and convert it to DBL
  8736.  
  8737. ; 1st argument in SNG format
  8738.  
  8739.   02472 CDB10A          CALL    0AB1H           ;X = CSNG (X) (convert 2nd
  8740.                                                 ;argument to SNG)
  8741.   02475 C1              POP     BC              ;Get 1st argument from stack
  8742.   02476 D1              POP     DE
  8743.   02477 21B518          LD      HL,18B5H        ;HL -> jump table
  8744.   0247A 18D5            JR      2451H           ;Execute function
  8745.  
  8746. ; 2nd argument in SNG format, 1st argument in INT format
  8747.  
  8748.   0247C E1              POP     HL              ;Restore 1st argument
  8749.   0247D CDA409          CALL    09A4H           ;(SP) = X (save 2nd argument
  8750.                                                 ;on stack)
  8751.   02480 CDCF0A          CALL    0ACFH           ;X = CSNG (HL)
  8752.   02483 CDBF09          CALL    09BFH           ;BCDE = X = 1st argument
  8753.   02486 E1              POP     HL              ;1st argument from stack
  8754.   02487 222341          LD      (4123H),HL      ;Put it in X
  8755.   0248A E1              POP     HL
  8756.   0248B 222141          LD      (4121H),HL
  8757.   0248E 18E7            JR      2477H           ;Continue at 2477H
  8758.  
  8759.  
  8760. ; IDIV: X = DE / HL
  8761. ; Divides two integer numbers with the result in SNG format
  8762. ;
  8763. ; I: DE = Dividend
  8764. ;    HL = Divisor
  8765. ; O: X = Quotient
  8766.  
  8767.   02490 E5              PUSH    HL              ;Save divisor
  8768.   02491 EB              EX      DE,HL           ;HL = dividend
  8769.   02492 CDCF0A          CALL    0ACFH           ;X = CSNG (DE) (X = dividend)
  8770.   02495 E1              POP     HL              ;Restore dividend
  8771.   02496 CDA409          CALL    09A4H           ;(SP) = X (dividend to stack)
  8772.   02499 CDCF0A          CALL    0ACFH           ;X = CSNG (HL) (X = divisor)
  8773.   0249C C3A008          JP      08A0H           ;X = (SP) / X (execute SDIV)
  8774.  
  8775.  
  8776. ; Decode argument at (PTP) and store result in X
  8777.  
  8778.   0249F D7              RST     10H             ;Operand indicated ?
  8779.   024A0 1E28            LD      E,28H           ;E = error code for ?MO Error
  8780.   024A2 CAA219          JP      Z,19A2H         ;No: ?MO Error
  8781.                                                 ;Yes: number indicated ?
  8782.   024A5 DA6C0E          JP      C,0E6CH         ;Yes: decode number and store
  8783.                                                 ;result in X
  8784.                                                 ;No:
  8785.   024A8 CD3D1E          CALL    1E3DH           ;Variable indicated ?
  8786.   024AB D24025          JP      NC,2540H        ;Yes: continue at 2540H
  8787.  
  8788.   024AE FECD            CP      0CDH            ;'+' token ? (positive sign)
  8789.   024B0 28ED            JR      Z,249FH         ;Yes: skip it, get next
  8790.                                                 ;character
  8791.   024B2 FE2E            CP      2EH             ;'.' ? (decimal point)
  8792.   024B4 CA6C0E          JP      Z,0E6CH         ;Yes: decode number
  8793.  
  8794.   024B7 FECE            CP      0CEH            ;'-' token ? (negative sign)
  8795.   024B9 CA3225          JP      Z,2532H         ;Yes: continue at 2532H
  8796.  
  8797.   024BC FE22            CP      22H             ;'"' ? (string constant)
  8798.   024BE CA6628          JP      Z,2866H         ;Yes: continue at 2866H
  8799.  
  8800.   024C1 FECB            CP      0CBH            ;'NOT' token ?
  8801.   024C3 CAC425          JP      Z,25C4H         ;Yes: continue at 25C4H
  8802.  
  8803.   024C6 FE26            CP      26H             ;'&' ? (hexadecimal constant)
  8804.   024C8 CAE334          JP      Z,34E3H         ;Yes: continue at 34E3H
  8805.  
  8806.   024CB FEC3            CP      0C3H            ;'ERR' token ?
  8807.   024CD 200A            JR      NZ,24D9H        ;No: continue at 24D9H
  8808.  
  8809.  
  8810.  
  8811. ; X = ERR
  8812. ; -------
  8813.  
  8814.   024CF D7              RST     10H             ;Adjust PTP
  8815.   024D0 3A9A40          LD      A,(409AH)       ;A = last error code
  8816.   024D3 E5              PUSH    HL              ;Save PTP
  8817.   024D4 CDF827          CALL    27F8H           ;Save A to X as INT
  8818.   024D7 E1              POP     HL              ;Restore PTP
  8819.   024D8 C9              RET
  8820.  
  8821.  
  8822. ; Not ERR
  8823.  
  8824.   024D9 FEC2            CP      0C2H            ;'ERL' token ?
  8825.   024DB 200A            JR      NZ,24E7H        ;No: continue at 24E7H
  8826.  
  8827.  
  8828. ; X = ERL
  8829. ; -------
  8830.  
  8831.   024DD D7              RST     10H             ;Adjust PTP
  8832.   024DE E5              PUSH    HL              ;Save PTP
  8833.   024DF 2AEA40          LD      HL,(40EAH)      ;HL = LN of last error line
  8834.   024E2 CD660C          CALL    0C66H           ;X = HL (SNG) (no sign)
  8835.   024E5 E1              POP     HL              ;Restore PTP
  8836.   024E6 C9              RET
  8837.  
  8838.  
  8839. ; not ERL
  8840.  
  8841.   024E7 FEC0            CP      0C0H            ;'VARPTR' toekn ?
  8842.   024E9 2014            JR      NZ,24FFH        ;No: continue at 24FFH
  8843.  
  8844.  
  8845. ; X = VARPTR()
  8846. ; ------------
  8847.  
  8848.   024EB D7              RST     10H             ;Adjust PTP
  8849.   024EC CF              RST     08H             ;Next character must be
  8850.   024ED 28              DEFB    '('             ;a '('
  8851.   024EE CD0D26          CALL    260DH           ;DE = address of indicated
  8852.                                                 ;variable. DE = 0000H in case
  8853.                                                 ;variable not found
  8854.   024F1 CF              RST     08H             ;Next character must be
  8855.   024F2 29              DEFB    ')'             ;a ')'
  8856.   024F3 E5              PUSH    HL              ;Save PTP
  8857.   024F4 EB              EX      DE,HL           ;HL -> variable
  8858.   024F5 7C              LD      A,H             ;Null pointer ?
  8859.   024F6 B5              OR      L
  8860.   024F7 CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  8861.                                                 ;No:
  8862.   024FA CD9A0A          CALL    0A9AH           ;X = HL (INT)
  8863.   024FD E1              POP     HL              ;Restore PTP
  8864.   024FE C9              RET
  8865.  
  8866.  
  8867. ; Not VARPTR
  8868.  
  8869.   024FF FEC1            CP      0C1H            ;'USR" token ?
  8870.   02501 C37A3F          JP      3F7AH           ;Intercept Colour BASIC token
  8871.                                                 ;and continue at 27FEH in case
  8872.                                                 ;'USR' token found
  8873.  
  8874.   02504 FEC5            CP      0C5H            ;'INSTR' token ?
  8875.   02506 CA9D41          JP      Z,419DH         ;Yes: continue at 41D9H
  8876.  
  8877.   02509 FEC8            CP      0C8H            ;'MEM' token ?
  8878.   0250B CAC927          JP      Z,27C9H         ;Yes: continue at 27C9H
  8879.  
  8880.   0250E FEC7            CP      0C7H            ;'TIME$' token ?
  8881.   02510 CA7641          JP      Z,4176H         ;Yes: continue at 4176H
  8882.  
  8883.   02513 FEC6            CP      0C6H            ;'CHECK' token ?
  8884.   02515 CA3201          JP      Z,0132H         ;Yes: continue at 0132H
  8885.  
  8886.   02518 FEC9            CP      0C9H            ;'INKEY$' token ?
  8887.   0251A CA9D01          JP      Z,019DH         ;Yes: continue at 01D9H
  8888.  
  8889.   0251D FEC4            CP      0C4H            ;'STRING$' token ?
  8890.   0251F CA2F2A          JP      Z,2A2FH         ;Yes: continue at 2A2FH
  8891.  
  8892.   02522 FEBE            CP      0BEH            ;'FN' token ?
  8893.   02524 CA5541          JP      Z,4155H         ;Yes: continue at 4155H
  8894.  
  8895.   02527 D6D7            SUB     0D7H            ;Function token found ?
  8896.   02529 D24E25          JP      NC,254EH        ;Yes: continue at 254EH
  8897.                                                 ;No:
  8898.   0252C CD3523          CALL    2335H           ;Since all other possibilities
  8899.                                                 ;have been tested, there can
  8900.                                                 ;only be an expresssion
  8901.                                                 ;enclosed in paranthesis
  8902.                                                 ;at (PTP)
  8903.   0252F CF              RST     08H             ;Next character must be
  8904.   02530 29              DEFB    ')'             ;a ')'
  8905.   02531 C9              RET
  8906.  
  8907.  
  8908. ; Process negative sign
  8909.  
  8910.   02532 167D            LD      D,7DH           ;D = priority code (next
  8911.                                                 ;priority after power function)
  8912.   02534 CD3A23          CALL    233AH           ;Argument to X
  8913.   02537 2AF340          LD      HL,(40F3H)      ;Get PTP
  8914.   0253A E5              PUSH    HL              ;And save it
  8915.   0253B CD7B09          CALL    097BH           ;Negate result (type conform)
  8916.   0253E E1              POP     HL              ;Restore PTP
  8917.   0253F C9              RET
  8918.  
  8919.  
  8920. ; Process variable as operand
  8921.  
  8922.   02540 CD0D26          CALL    260DH           ;Get address of variable
  8923.   02543 E5              PUSH    HL              ;Save PTP
  8924.   02544 EB              EX      DE,HL           ;HL = address
  8925.   02545 222141          LD      (4121H),HL      ;Save address
  8926.   02548 E7              RST     20H             ;TSTTYP
  8927.                                                 ;STR type ?
  8928.   02549 C4F709          CALL    NZ,09F7H        ;No: X = (HL) (type conform)
  8929.   0254C E1              POP     HL              ;Restore PTP
  8930.   0254D C9              RET
  8931.  
  8932.  
  8933. ; Function token found
  8934. ; A = 00H for SNG, 01H for INT,..., 22H for RIGHT$ and 24H for MID$
  8935. ; (see keyword and address table at 1608H)
  8936.  
  8937.   0254E 0600            LD      B,00H           ;MSB of offset = 00H
  8938.   02550 07              RLCA                    ;A = A * 2 (2 bytes address)
  8939.   02551 4F              LD      C,A             ;BC = offset of address table
  8940.   02552 C5              PUSH    BC              ;Save offset
  8941.   02553 D7              RST     10H             ;Adjust PTP
  8942.   02554 79              LD      A,C             ;A = offset LSB
  8943.   02555 FE41            CP      41H             ;LSB > 41H ?
  8944.   02557 3816            JR      C,256FH         ;No: continue at 256FH
  8945.  
  8946.  
  8947. ; Process 1st argument of LEFT$, RIGHT$ or MID$
  8948.  
  8949.   02559 CD3523          CALL    2335H           ;Process expression
  8950.   0255C CF              RST     08H             ;Next character must be
  8951.   0255D 2C              DEFB    ','             ;a ','
  8952.                                                 ;1st argument not a string ?
  8953.   0255E CDF40A          CALL    0AF4H           ;Yes: ?TM Error
  8954.   02561 EB              EX      DE,HL           ;DE = PTP
  8955.   02562 2A2141          LD      HL,(4121H)      ;HL -> 1st argument
  8956.   02565 E3              EX      (SP),HL         ;Save pointer
  8957.   02566 E5              PUSH    HL              ;Save offset to stack
  8958.   02567 EB              EX      DE,HL           ;HL = PTP
  8959.   02568 CD1C2B          CALL    2B1CH           ;DE = 2nd argument
  8960.   0256B EB              EX      DE,HL           ;DE = PTP, HL = 2nd argument
  8961.   0256C E3              EX      (SP),HL         ;Save 2nd argument, HL = offset
  8962.   0256D 1814            JR      2583H           ;Continue at 2583H
  8963.  
  8964.  
  8965. ; Get function argument and execute function
  8966.  
  8967.   0256F CD2C25          CALL    252CH           ;Process expression
  8968.   02572 E3              EX      (SP),HL         ;Save PTP, HL = offset
  8969.   02573 7D              LD      A,L             ;A = LSB of offset
  8970.   02574 FE0C            CP      0CH             ;LSB < 0CH ? (SNG/INT/ABS/FRE/
  8971.                                                 ;INP/POS)
  8972.   02576 3807            JR      C,257FH         ;Yes: execute function
  8973.                                                 ;No:
  8974.   02578 FE1B            CP      1BH             ;LSB < 1BH ? (SQR/RND/LOG/EXP/
  8975.                                                 ;COS/SIN/TAN/ATN)
  8976.   0257A E5              PUSH    HL              ;Save offset
  8977.   0257B DCB10A          CALL    C,0AB1H         ;Yes: X = CSNG (X)
  8978.   0257E E1              POP     HL              ;Restore offset
  8979.   0257F 113E25          LD      DE,253EH        ;Set new RET-address
  8980.   02582 D5              PUSH    DE              ;to 253EH
  8981.   02583 010816          LD      BC,1608H        ;BC -> jump table
  8982.   02586 09              ADD     HL,BC           ;Add offset
  8983.   02587 4E              LD      C,(HL)          ;Load jump address in HL
  8984.   02588 23              INC     HL
  8985.   02589 66              LD      H,(HL)
  8986.   0258A 69              LD      L,C
  8987.   0258B E9              JP      (HL)            ;Execute function
  8988.  
  8989.  
  8990. ; String compare
  8991. ;
  8992. ; I: (SP-2) = RET addres to 25B8H
  8993. ;    (SP)   = Vector address of 1st string
  8994. ;     X     = Vector address of 2nd string
  8995. ; O:  A     = FFH : 1st string < 2nd string
  8996. ;           = 00H : 1st string = 2nd string
  8997. ;           = 01H : 1st string > 2nd string
  8998.  
  8999.   0258C CDD729          CALL    29D7H           ;Test 2nd argument if STR
  9000.                                                 ;format, HL -> vector of
  9001.                                                 ;2nd string
  9002.   0258F 7E              LD      A,(HL)          ;A = LEN (2nd string)
  9003.   02590 23              INC     HL              ;BC = address (2nd string)
  9004.   02591 4E              LD      C,(HL)
  9005.   02592 23              INC     HL
  9006.   02593 46              LD      B,(HL)
  9007.   02594 D1              POP     DE              ;Restore vector address of
  9008.                                                 ;1st string in DE
  9009.   02595 C5              PUSH    BC              ;Save address and
  9010.   02596 F5              PUSH    AF              ;length of 2nd string
  9011.   02597 CDDE29          CALL    29DEH           ;HL -> vector of 2st string
  9012.   0259A D1              POP     DE              ;D = length of 2nd string
  9013.   0259B 5E              LD      E,(HL)          ;E = length of 1st string
  9014.   0259C 23              INC     HL              ;BC = address of 1st string
  9015.   0259D 4E              LD      C,(HL)
  9016.   0259E 23              INC     HL
  9017.   0259F 46              LD      B,(HL)
  9018.   025A0 E1              POP     HL              ;HL = address of 2nd string
  9019.   025A1 7B              LD      A,E             ;Both length counters zero ?
  9020.   025A2 B2              OR      D
  9021.   025A3 C8              RET     Z               ;Yes: done, return
  9022.  
  9023.   025A4 7A              LD      A,D             ;A = 2nd length counter
  9024.   025A5 D601            SUB     01H             ;Counter zero?
  9025.   025A7 D8              RET     C               ;Yes: done, return
  9026.  
  9027.   025A8 AF              XOR     A               ;A = 00H
  9028.   025A9 BB              CP      E               ;1st length counter zero ?
  9029.   025AA 3C              INC     A               ;A = 01H if so.
  9030.   025AB D0              RET     NC              ;Yes: done, return (A = 01H)
  9031.  
  9032.   025AC 15              DEC     D               ;2nd length counter - 1
  9033.   025AD 1D              DEC     E               ;1st length counter - 1
  9034.   025AE 0A              LD      A,(BC)          ;Compare next character of 1st
  9035.   025AF BE              CP      (HL)            ;string with character of 2nd
  9036.                                                 ;string
  9037.   025B0 23              INC     HL              ;2nd pointer + 1
  9038.   025B1 03              INC     BC              ;1st pointer + 1
  9039.                                                 ;Both characters identical ?
  9040.   025B2 28ED            JR      Z,25A1H         ;Yes: compare next characters
  9041.  
  9042.   025B4 3F              CCF                     ;Negate C-flag
  9043.   025B5 C36009          JP      0960H           ;A = FFH in case C-flag = 1
  9044.                                                 ;A = 00H in case C-flag = 0
  9045.  
  9046. ; Evaluate result of compare
  9047. ;
  9048. ; I: A = FFH : 1st argument < 2nd argument
  9049. ;      = 00H : 1st argument = 2nd argument
  9050. ;      = 01H : 1st argument > 2nd argument
  9051.  
  9052.   025B8 3C              INC     A               ;A = A + 1 (C-flag = 1 if A
  9053.                                                 ;was FFH)
  9054.   025B9 8F              ADC     A,A             ;A = 01H : 1st arg. < 2nd arg.
  9055.                                                 ;  = 02H : 1st arg. = 2nd arg.
  9056.                                                 ;  = 04H : 1st arg. > 2nd arg.
  9057.   025BA C1              POP     BC              ;Restore compare operator code
  9058.                                                 ;(see 2355H)
  9059.   025BB A0              AND     B               ;Set bits of operators done
  9060.   025BC C6FF            ADD     A,0FFH          ;A = A + FFH: C-flag = 1 if
  9061.                                                 ;a condition was true (overflow
  9062.                                                 ;in case A <> 0)
  9063.   025BE 9F              SBC     A,A             ;A = 00H = 0 in case false
  9064.                                                 ;else A = FFH = -1
  9065.   025BF CD8D09          CALL    098DH           ;X = HL = A (with sign)
  9066.   025C2 1812            JR      25D6H           ;Back to 2346H
  9067.  
  9068.  
  9069. ; NOT
  9070. ; ---
  9071.  
  9072.   025C4 165A            LD      D,5AH           ;Priority code = 5AH (higher
  9073.                                                 ;than AND and OR)
  9074.   025C6 CD3A23          CALL    233AH           ;Argument to X
  9075.   025C9 CD7F0A          CALL    0A7FH           ;HL = X = CINT (X)
  9076.   025CC 7D              LD      A,L             ;Negate result
  9077.   025CD 2F              CPL
  9078.   025CE 6F              LD      L,A
  9079.   025CF 7C              LD      A,H
  9080.   025D0 2F              CPL
  9081.   025D1 67              LD      H,A
  9082.   025D2 222141          LD      (4121H),HL      ;And write back
  9083.   025D5 C1              POP     BC              ;Remove RET-address
  9084.   025D6 C34623          JP      2346H           ;and back to 2346H
  9085.  
  9086.  
  9087. ; RST 20H
  9088. ; -------
  9089. ; TSTTYP: Test current VT and set corresponding flags
  9090. ;
  9091. ; I: -
  9092. ; O: A = VT - 3
  9093. ;    INT: A = FFH, Z-flag = 0, C-flag = 1, S-flag = 1
  9094. ;    STR: A = 00H, Z-flag = 1, C-flag = 1, S-flag = 0
  9095. ;    SNG: A = 01H, Z-flag = 0, C-flag = 1, S-flag = 0
  9096. ;    DBL: A = 05H, Z-flag = 0, C-flag = 0, S-flag = 0
  9097.  
  9098.   025D9 3AAF40          LD      A,(40AFH)       ;A = VT
  9099.   025DC FE08            CP      08H             ;DBL ?
  9100.   025DE 3005            JR      NC,25E5H        ;Yes: continue at 25E5H
  9101.                                                 ;No:
  9102.   025E0 D603            SUB     03H             ;A = VT - 3
  9103.   025E2 B7              OR      A               ;Set flags
  9104.   025E3 37              SCF                     ;C-flag = 1
  9105.   025E4 C9              RET
  9106.  
  9107.   025E5 D603            SUB     03H             ;A = VT - 3
  9108.   025E7 B7              OR      A               ;Set flags
  9109.   025E8 C9              RET
  9110.  
  9111.  
  9112. ; Process AND / OR
  9113.  
  9114.   025E9 C5              PUSH    BC              ;Save priority code
  9115.   025EA CD7F0A          CALL    0A7FH           ;HL = X = CINT (X) = 2nd arg.
  9116.   025ED F1              POP     AF              ;Restore priority code
  9117.   025EE D1              POP     DE              ;DE = 1st argument
  9118.   025EF 01FA27          LD      BC,27FAH        ;Set new RET-address
  9119.   025F2 C5              PUSH    BC              ;to 27FAH (X = HL (INT) )
  9120.   025F3 FE46            CP      46H             ;Priority code = 46H (OR) ?
  9121.   025F5 2006            JR      NZ,25FDH        ;No: execute AND at 25FDH
  9122.  
  9123.  
  9124. ; OR
  9125. ; --
  9126.  
  9127.   025F7 7B              LD      A,E             ;OR both arguments
  9128.   025F8 B5              OR      L
  9129.   025F9 6F              LD      L,A
  9130.   025FA 7C              LD      A,H
  9131.   025FB B2              OR      D
  9132.   025FC C9              RET                     ;Back to 27FAH
  9133.  
  9134.  
  9135. ; AND
  9136. ; ---
  9137.  
  9138.   025FD 7B              LD      A,E             ;AND both arguments
  9139.   025FE A5              AND     L
  9140.   025FF 6F              LD      L,A
  9141.   02600 7C              LD      A,H
  9142.   02601 A2              AND     D
  9143.   02602 C9              RET                     ;Back to 27FAH
  9144.  
  9145.  
  9146. ; Return after DIM
  9147.  
  9148.   02603 2B              DEC     HL              ;PTP - 1
  9149.   02604 D7              RST     10H             ;End of command ?
  9150.   02605 C8              RET     Z               ;Yes: done, return
  9151.                                                 ;No:
  9152.   02606 CF              RST     08H             ;Next character must be
  9153.   02607 2C              DEFB    ','             ;a comma
  9154.  
  9155.  
  9156. ; DIM statement
  9157. ; -------------
  9158.  
  9159.   02608 010326          LD      BC,2603H        ;Set new return address
  9160.  
  9161.   0260B C5              PUSH    BC              ;to 2603H
  9162.   0260C F6AF            OR      0AFH            ;A <> 0 for DIM
  9163.  
  9164. ; Find address of variable in (PTP) and create variable if it does not
  9165. ; exist.
  9166. ;
  9167. ; I: PTP -> variable name
  9168. ; O: DE -> searched variable (0000H if variable does not exist)
  9169.  
  9170. * 0260D   AF            XOR     A               ;A = 0 for address search
  9171.   0260E 32AE40          LD      (40AEH),A       ;Store flag
  9172.   02611 46              LD      B,(HL)          ;B = first character of name
  9173.  
  9174.   02612 CD3D1E          CALL    1E3DH           ;Is the character in (HL) a
  9175.                                                 ;upper case character ?
  9176.   02615 DA9719          JP      C,1997H         ;No: ?SN Error
  9177.  
  9178.   02618 AF              XOR     A               ;A = 00H
  9179.   02619 4F              LD      C,A             ;C = default second character
  9180.                                                 ;of variable name
  9181.   0261A D7              RST     10H             ;Second character present ?
  9182.   0261B 3805            JR      C,2622H         ;Jump when it is a number
  9183.  
  9184.   0261D CD3D1E          CALL    1E3DH           ;Upper case character present ?
  9185.   02620 3809            JR      C,262BH         ;No: keep variable name at
  9186.                                                 ;one character
  9187.   02622 4F              LD      C,A             ;C = second character of name
  9188.   02623 D7              RST     10H             ;Get next character
  9189.                                                 ;Is it a number ?
  9190.   02624 38FD            JR      C,2623H         ;Yes: get character
  9191.  
  9192.   02626 CD3D1E          CALL    1E3DH           ;Is it an upper case character ?
  9193.   02629 30F8            JR      NC,2623H        ;Yes: get character
  9194.  
  9195.   0262B 115226          LD      DE,2652H        ;Set new return address
  9196.   0262E D5              PUSH    DE              ;to 2652H
  9197.  
  9198.   0262F 1602            LD      D,02H           ;D = 2 (type code for INT)
  9199.   02631 FE25            CP      '%'             ;'%' (INT indentifier) found ?
  9200.   02633 C8              RET     Z               ;Yes: D = type code
  9201.  
  9202.   02634 14              INC     D               ;D = 3 (type code for STR)
  9203.   02635 FE24            CP      '$'             ;'$' (STR indentifier) found ?
  9204.   02637 C8              RET     Z               ;Yes: D = type code
  9205.  
  9206.   02638 14              INC     D               ;D = 4 (type code for SNG)
  9207.   02639 FE21            CP      '!'             ;'!' (SNG indentifier) found ?
  9208.   0263B C8              RET     Z               ;Yes: D = type code
  9209.  
  9210.   0263C 1608            LD      D,08H           ;D = 8 (type code for DBL)
  9211.   0263E FE23            CP      '#'             ;'#' (DBL indentifier) found ?
  9212.   02640 C8              RET     Z               ;Yes: D = type code
  9213.  
  9214. ; No type code indicated. Get type code from DEF table
  9215.  
  9216.   02641 78              LD      A,B             ;A = first character of name
  9217.   02642 D641            SUB     41H             ;A = offset for type code table
  9218.   02644 E67F            AND     7FH             ;Clear highest bit
  9219.  
  9220.   02646 5F              LD      E,A             ;DE = offset
  9221.   02647 1600            LD      D,00H
  9222.   02649 E5              PUSH    HL              ;Save PTP
  9223.   0264A 210141          LD      HL,4101H        ;HL -> type code table
  9224.   0264D 19              ADD     HL,DE           ;Add offset
  9225.   0264E 56              LD      D,(HL)          ;Get type code
  9226.   0264F E1              POP     HL              ;Restore PTP
  9227.   02650 2B              DEC     HL              ;PTP - 1
  9228.   02651 C9              RET
  9229.  
  9230. ; Search variable / create variable
  9231. ; BC = 1st and 2nd character of variable name
  9232. ; D = type code of variable
  9233.  
  9234.   02652 7A              LD      A,D             ;A = type code
  9235.   02653 32AF40          LD      (40AFH),A       ;Save as VT
  9236.   02656 D7              RST     10H             ;Skip spaces/LF etc
  9237.   02657 3ADC40          LD      A,(40DCH)       ;Array variables indicated ?
  9238.   0265A B7              OR      A               ;(see 1CA1H)
  9239.   0265B C26426          JP      NZ,2664H        ;No: create normal variable
  9240.  
  9241.   0265E 7E              LD      A,(HL)          ;A = next character following
  9242.                                                 ;variable name.
  9243.   0265F D628            SUB     28H             ;Is it a '('
  9244.   02661 CAE926          JP      Z,26E9H         ;Yes: process array variable
  9245.  
  9246.   02664 AF              XOR     A               ;A = 00H
  9247.   02665 32DC40          LD      (40DCH),A       ;Release array variable
  9248.  
  9249.   02668 E5              PUSH    HL              ;Save PTP
  9250.   02669 D5              PUSH    DE              ;Save type code
  9251.  
  9252.   0266A 2AF940          LD      HL,(40F9H)      ;HL -> start of variables
  9253.   0266D EB              EX      DE,HL           ;DE = HL
  9254.   0266E 2AFB40          LD      HL,(40FBH)      ;HL -> end of variables
  9255.   02671 DF              RST     18H             ;End reached ?
  9256.   02672 E1              POP     HL              ;Restore type code
  9257.   02673 2819            JR      Z,268EH         ;Yes: create new variable
  9258.  
  9259.   02675 1A              LD      A,(DE)          ;A = type code of variable
  9260.                                                 ;addressed by DE
  9261.   02676 6F              LD      L,A             ;L = type code
  9262.   02677 BC              CP      H               ;Is it the searched code ?
  9263.   02678 13              INC     DE              ;Pointer + 1
  9264.   02679 200B            JR      NZ,2686H        ;No: continue at 2686H
  9265.  
  9266.   0267B 1A              LD      A,(DE)          ;A = second character of name
  9267.   0267C B9              CP      C               ;Same as in searched name ?
  9268.   0267D 2007            JR      NZ,2686H        ;No: continue at 2686H
  9269.  
  9270.   0267F 13              INC     DE              :Pointer + 1
  9271.   02680 1A              LD      A,(DE)          ;A = first charachter of name
  9272.   02681 B8              CP      B               ;Same as in searched name ?
  9273.   02682 CACC26          JP      Z,26CCH         ;Yes: continue at 26CCH
  9274.  
  9275.   02685 3E13            LD      A,13H           ;--
  9276. * 02685   13            INC     DE              ;Pointer + 1
  9277.   02687 13              INC     DE              ;Pointer + 1
  9278.   02688 E5              PUSH    HL              ;Save type code
  9279.   02689 2600            LD      H,00H           ;HL = length of variable found
  9280.   0268B 19              ADD     HL,DE           ;Put HL on next variable
  9281.   0268C 18DF            JR      266DH           ;Repeat compare
  9282.  
  9283. ; Variable not found
  9284.  
  9285.   0268E 7C              LD      A,H             ;A = type code
  9286.   0268F E1              POP     HL              ;Restore PTP
  9287.   02690 E3              EX      (SP),HL         ;Save PTP, HL = RET address
  9288.   02691 F5              PUSH    AF              ;Save type code
  9289.   02692 D5              PUSH    DE              ;Save pointer on end of
  9290.                                                 ;variables
  9291.   02693 11F124          LD      DE,24F1H        ;Was routine CALLed by VARPTR
  9292.                                                 ;function ?
  9293.   02696 DF              RST     18H             ;(RET address = 24F1H)
  9294.   02697 2836            JR      Z,26CFH         ;Yes: continue at 26CFH
  9295.  
  9296.   02699 114325          LD      DE,2543H        ;CALLed from 2540H ?
  9297.   0269C DF              RST     18H             ;(RET address = 2543H)
  9298.   0269D D1              POP     DE              ;Restore pointer
  9299.   0269E 2835            JR      Z,26D5H         ;Yes: continue at 26D5H
  9300.  
  9301. ; Create new variable
  9302.  
  9303.   026A0 F1              POP     AF              ;Restore type code
  9304.   026A1 E3              EX      (SP),HL         ;Save RET address, restore PTP
  9305.   026A2 E5              PUSH    HL              ;Save PTP
  9306.   026A3 C5              PUSH    BC              ;Save variable name
  9307.   026A4 4F              LD      C,A             ;C = type code
  9308.   026A5 0600            LD      B,00H           ;B = 00H
  9309.   026A7 C5              PUSH    BC              ;Save type code
  9310.                                                 ;Calculate total length of var
  9311.   026A8 03              INC     BC              ;BC + 1 for type code
  9312.   026A9 03              INC     BC              ;BC + 2 for variable name
  9313.   026AA 03              INC     BC
  9314.   026AB 2AFD40          LD      HL,(40FDH)      ;HL -> start of free space
  9315.   026AE E5              PUSH    HL              ;Save pointer
  9316.   026AF 09              ADD     HL,BC           ;+ length = new start of
  9317.                                                 ;free space
  9318.   026B0 C1              POP     BC              ;Restore old start address
  9319.   026B1 E5              PUSH    HL              ;Save new start address
  9320.   026B2 CD5519          CALL    1955H           ;Test memory space and move
  9321.                                                 ;memory to create space for
  9322.                                                 ;new variable
  9323.   026B5 E1              POP     HL              ;Restore new start address
  9324.   026B6 22FD40          LD      (40FDH),HL      ;Save it in system RAM
  9325.   026B9 60              LD      H,B             ;HL = new end address of
  9326.   026BA 69              LD      L,C             ;memory with single variables
  9327.   026BB 22FB40          LD      (40FBH),HL      ;Save it in system RAM
  9328.   026BE 2B              DEC     HL              ;Pointer - 1
  9329.   026BF 3600            LD      (HL),00H        ;Clear memory space for new
  9330.                                                 ;variable
  9331.   026C1 DF              RST     18H             ;Done ?
  9332.   026C2 20FA            JR      NZ,26BEH        ;No: continue clear
  9333.  
  9334.   026C4 D1              POP     DE              ;Restore type code
  9335.                                                 ;HL -> new variable
  9336.   026C5 73              LD      (HL),E          ;Store type code
  9337.   026C6 23              INC     HL              ;Pointer + 1
  9338.   026C7 D1              POP     DE              ;Restore variable name
  9339.   026C8 73              LD      (HL),E          ;Store 2nd character
  9340.   026C9 23              INC     HL              ;Pointer + 1
  9341.   026CA 72              LD      (HL),D          ;Store 1st character
  9342.   026CB EB              EX      DE,HL           ;DE -> mantissa of new variable
  9343.   026CC 13              INC     DE              ;DE -> new variable
  9344.   026CD E1              POP     HL              ;Restore PTP
  9345.   026CE C9              RET
  9346.  
  9347.  
  9348. ; VARPTR function: variable not found
  9349.  
  9350.   026CF 57              LD      D,A             ;DE = 0000H
  9351.   026D0 5F              LD      E,A
  9352.   026D1 F1              POP     AF              ;Correct stack
  9353.   026D2 F1              POP     AF
  9354.   026D3 E3              EX      (SP),HL         ;Restore PTP, save RET address
  9355.   026D4 C9              RET                     ;Done
  9356.  
  9357.  
  9358. ; Variable at expression evaluation (2337H) not found
  9359. ; Set result to 0
  9360.  
  9361.   026D5 322441          LD      (4124H),A       ;Exp (X) = 0 -> X = 0
  9362.   026D8 C1              POP     BC              ;Correct stack
  9363.   026D9 67              LD      H,A             ;HL = 0000H
  9364.   026DA 6F              LD      L,A
  9365.   026DB 222141          LD      (4121H),HL      ;Set X (INT) to 0
  9366.   026DE E7              RST     20H             ;TSTTYP
  9367.                                                 ;STR type ?
  9368.   026DF 2006            JR      NZ,26E7H        ;No: X is ok, continue at 26E7H
  9369.                                                 ;Yes:
  9370.   026E1 212819          LD      HL,1928H        ;Vector address -> null string
  9371.   026E4 222141          LD      (4121H),HL      ;X = HL
  9372.   026E7 E1              POP     HL              ;Restore PTP
  9373.   026E8 C9              RET
  9374.  
  9375.  
  9376. ; Array variable recognized
  9377. ;
  9378. ; BC = variable name
  9379. ; HL = PTP
  9380. ; A  = 00H
  9381.  
  9382.   026E9 E5              PUSH    HL              ;Save PTP
  9383.   026EA 2AAE40          LD      HL,(40AEH)      ;L = type code, H = DIM flag
  9384.   026ED E3              EX      (SP),HL         ;Save HL, restore PTP
  9385.   026EE 57              LD      D,A             ;D = 0 (number of dimensions)
  9386.   026EF D5              PUSH    DE              ;Save registers
  9387.   026F0 C5              PUSH    BC
  9388.   026F1 CD451E          CALL    1E45H           ;Get next dimension at (PTP)
  9389.   026F4 C1              POP     BC              ;Restore registers
  9390.   026F5 F1              POP     AF
  9391.   026F6 EB              EX      DE,HL           ;DE = PTP, HL = dim. value
  9392.   026F7 E3              EX      (SP),HL         ;Save dim. value, restore HL
  9393.   026F8 E5              PUSH    HL              ;Save HL again
  9394.   026F9 EB              EX      DE,HL           ;HL = PTP
  9395.   026FA 3C              INC     A               ;Dimension counter + 1
  9396.   026FB 57              LD      D,A             ;D = counter
  9397.   026FC 7E              LD      A,(HL)          ;Another dimension indicated ?
  9398.   026FD FE2C            CP      ','             ;Separator found ?
  9399.   026FF 28EE            JR      Z,26EFH         ;Yes: get next dimension
  9400.                                                 ;No:
  9401.   02701 CF              RST     08H             ;Next character must be
  9402.   02702 29              DEFB    ')'             ;a ')'
  9403.   02703 22F340          LD      (40F3H),HL      ;Save PTP
  9404.   02706 E1              POP     HL              ;Restore type code and DIM flag
  9405.   02707 22AE40          LD      (40AEH),HL      ;and save in system RAM
  9406.   0270A D5              PUSH    DE              ;Save dimension counter
  9407.                                                 ;The stack now contains the
  9408.                                                 ;dimension values and the
  9409.                                                 ;number of dimensions
  9410.   0270B 2AFB40          LD      HL,(40FBH)      ;HL -> start of array variable
  9411.                                                 ;memory space
  9412.   0270E 3E19            LD      A,19H           ;--
  9413.  
  9414. * 0270F   19            ADD     HL,DE           ;Add array size. HL now
  9415.                                                 ;points on the next array
  9416.   02710 EB              EX      DE,HL
  9417.   02711 2AFD40          LD      HL,(40FDH)      ;DE -> end of array variable
  9418.                                                 ;memory space
  9419.   02714 EB              EX      DE,HL
  9420.   02715 DF              RST     18H             ;End of array vars reached ?
  9421.   02716 3AAF40          LD      A,(40AFH)       ;A = requested type code
  9422.   02719 2827            JR      Z,2742H         ;No: array variable not found.
  9423.                                                 ;HL points to free memory space
  9424.                                                 ;for the new array.
  9425.  
  9426.   0271B BE              CP      (HL)            ;Type code found ?
  9427.   0271C 23              INC     HL              ;Pointer + 1
  9428.   0271D 2008            JR      NZ,2727H        ;No: continue at 2727H
  9429.  
  9430.   0271F 7E              LD      A,(HL)          ;2nd character of var. name
  9431.   02720 B9              CP      C               ;and compare it
  9432.   02721 23              INC     HL              ;Pointer + 1
  9433.                                                 ;Characters the same ?
  9434.   02722 2004            JR      NZ,2728H        ;No: continue at 2728H
  9435.  
  9436.   02724 7E              LD      A,(HL)          ;Compare 1st character
  9437.   02725 B8              CP      B               ;The same ?
  9438.   02726 3E23            LD      A,23H           ;--
  9439.  
  9440. * 02726   23            INC     HL              ;Adjust pointer
  9441.   02728 23              INC     HL              ;Pointer + 1
  9442.   02729 5E              LD      E,(HL)          ;DE = total length
  9443.   0272A 23              INC     HL
  9444.   0272B 56              LD      D,(HL)
  9445.   0272C 23              INC     HL
  9446.   0272D 20E0            JR      NZ,270FH        ;Array not found: check next
  9447.  
  9448.   0272F 3AAE40          LD      A,(40AEH)       ;New array to be created ?
  9449.   02732 B7              OR      A
  9450.   02733 1E12            LD      E,12H           ;E = error code for ?DD Error
  9451.   02735 C2A219          JP      NZ,19A2H        ;Yes: the array already exists,
  9452.                                                 ;issue error.
  9453.  
  9454.   02738 F1              POP     AF              ;No: A = number of dimensions
  9455.   02739 96              SUB     (HL)            ;Does they match ?
  9456.   0273A CA9527          JP      Z,2795H         ;Yes: compute address of the
  9457.                                                 ;requested array element
  9458.  
  9459.   0273D 1E10            LD      E,10H           ;E = error code for ?BS Error
  9460.   0273F C3A219          JP      19A2H           ;Issue error
  9461.  
  9462.  
  9463. ; Array not found
  9464. ; HL -> free memory space for new array
  9465.  
  9466.   02742 77              LD      (HL),A          ;Save type code
  9467.   02743 23              INC     HL              ;Pointer + 1
  9468.   02744 5F              LD      E,A             ;DE = size of array element
  9469.   02745 1600            LD      D,00H
  9470.   02747 F1              POP     AF              ;A = number of dimensions
  9471.   02748 71              LD      (HL),C          ;Save array name
  9472.   02749 23              INC     HL
  9473.   0274A 70              LD      (HL),B
  9474.   0274B 23              INC     HL
  9475.   0274C 4F              LD      C,A             ;C = dimension counter
  9476.   0274D CD6319          CALL    1963H           ;Sufficient memory available ?
  9477.   02750 23              INC     HL              ;Pointer + 1
  9478.   02751 23              INC     HL              ;Pointer + 1
  9479.   02752 22D840          LD      (40D8H),HL      ;Save pointer in system RAM
  9480.   02755 71              LD      (HL),C          ;Save number of dimensions
  9481.   02756 23              INC     HL              ;Pointer + 1
  9482.   02757 3AAE40          LD      A,(40AEH)       ;A = DIM flag
  9483.   0275A 17              RLA                     ;If address search then
  9484.                                                 ;C-flag = 0.
  9485.                                                 ;If array create then
  9486.                                                 ;C-flag = 1
  9487.   0275B 79              LD      A,C             ;A = dimension counter
  9488.   0275C 010B00          LD      BC,000BH        ;BC = default dimension value
  9489.                                                 ;Address search ?
  9490.   0275F 3002            JR      NC,2763H        ; Yes: continue at 2763H
  9491.  
  9492.   02761 C1              POP     BC              ;Restore dimension value
  9493.   02762 03              INC     BC              ;+ 1 for zero element
  9494.   02763 71              LD      (HL),C          ;Save dimension value
  9495.   02764 23              INC     HL
  9496.   02765 70              LD      (HL),B
  9497.   02766 23              INC     HL
  9498.   02767 F5              PUSH    AF              ;Save counter
  9499.   02768 CDAA0B          CALL    0BAAH           ;DE = DE * BC: calculate the
  9500.                                                 ;total array size.
  9501.   0276B F1              POP     AF              ;Restore counter
  9502.   0276C 3D              DEC     A               ;Counter - 1
  9503.                                                 ;All dimensions done ?
  9504.   0276D 20ED            JR      NZ,275CH        ;No: next dimension
  9505.                                                 ;Yes:
  9506.   0276F F5              PUSH    AF              ;Save 00H on stack
  9507.   02770 42              LD      B,D             ;BC = array size
  9508.   02771 4B              LD      C,E
  9509.   02772 EB              EX      DE,HL           ;DE -> start of new array
  9510.                                                 ;HL = array size
  9511.   02773 19              ADD     HL,DE           ;HL -> new end of array
  9512.                                                 ;variable memory space
  9513.                                                 ;Memory overflow ?
  9514.   02774 38C7            JR      C,273DH         ;Yes: ?BS Error
  9515.  
  9516.   02776 CD6C19          CALL    196CH           ;Still free memory remaining ?
  9517.   02779 22FD40          LD      (40FDH),HL      ;Save new end address
  9518.   0277C 2B              DEC     HL              ;Pointer - 1
  9519.   0277D 3600            LD      (HL),00H        ;Clear array byte
  9520.   0277F DF              RST     18H             ;Start of array reached ?
  9521.   02780 20FA            JR      NZ,277CH        ;No: clear next byte
  9522.  
  9523.   02782 03              INC     BC              ;BC = number of required bytes
  9524.                                                 ;+ 1
  9525.   02783 57              LD      D,A             ;D = 0
  9526.   02784 2AD840          LD      HL,(40D8H)      ;HL -> number of dimensions
  9527.   02787 5E              LD      E,(HL)          ;DE = number of dimensions
  9528.   02788 EB              EX      DE,HL           ;HL = dimension counter
  9529.                                                 ;DE = pointer
  9530.   02789 29              ADD     HL,HL           ;HL = 2 * number of dimensions
  9531.                                                 ;(Every dimension value is
  9532.                                                 ;stored in 2 bytes)
  9533.   0278A 09              ADD     HL,BC           ;+ number of bytes required
  9534.                                                 ;by the array elements
  9535.                                                 ;= total size (in bytes)
  9536.   0278B EB              EX      DE,HL           ;DE = total size
  9537.   0278C 2B              DEC     HL              ;Pointer - 2
  9538.   0278D 2B              DEC     HL
  9539.   0278E 73              LD      (HL),E          ;Save total size
  9540.   0278F 23              INC     HL
  9541.   02790 72              LD      (HL),D
  9542.   02791 23              INC     HL
  9543.   02792 F1              POP     AF              ;Restore flags from stack
  9544.                                                 ;Creation of new array (DIM) ?
  9545.   02793 3830            JR      C,27C5H         ;Yes: done.
  9546.  
  9547. ; Compute address of required variable
  9548.  
  9549.   02795 47              LD      B,A             ;BC = 0000H
  9550.   02796 4F              LD      C,A
  9551.   02797 7E              LD      A,(HL)          ;A = dimension counter
  9552.   02798 23              INC     HL              ;Pointer + 1
  9553.   02799 16E1            LD      D,0E1H          ;--
  9554.  
  9555. * 0279A   E1            POP     HL              ;Restore pointer
  9556.   0279B 5E              LD      E,(HL)          ;DE = next dimension value
  9557.   0279C 23              INC     HL
  9558.   0279D 56              LD      D,(HL)
  9559.   0279E 23              INC     HL
  9560.   0279F E3              EX      (SP),HL         ;Save pointer, HL = requested
  9561.                                                 ;dimension
  9562.   027A0 F5              PUSH    AF              ;Save counter
  9563.   027A1 DF              RST     18H             ;Requested dimension found ?
  9564.                                                 ;Requested dim. > found dim. ?
  9565.   027A2 D23D27          JP      NC,273DH        ;Yes: ?BS Error
  9566.                                                 ;No:
  9567.   027A5 CDAA0B          CALL    0BAAH           ;DE = DE * BC = found dimension
  9568.                                                 ;* last value
  9569.   027A8 19              ADD     HL,DE           ;HL = requested dimension +
  9570.                                                 ;found dimension * last value
  9571.   027A9 F1              POP     AF              ;Restore counter
  9572.   027AA 3D              DEC     A               ;Counter - 1
  9573.   027AB 44              LD      B,H             ;BC = array element counter
  9574.   027AC 4D              LD      C,L
  9575.   027AD 20EB            JR      NZ,279AH        ;Process next dimension
  9576.  
  9577.   027AF 3AAF40          LD      A,(40AFH)       ;A = type code
  9578.   027B2 44              LD      B,H             ;BC = index value of array
  9579.   027B3 4D              LD      C,L             ;element
  9580.   027B4 29              ADD     HL,HL           ;* 2 = offset for INT
  9581.   027B5 D604            SUB     04H             ;Typecode = INT or STR ?
  9582.   027B7 3804            JR      C,27BDH         ;Yes: continue at 27BDH
  9583.                                                 ;No:
  9584.   027B9 29              ADD     HL,HL           ;* 2 = offset for SNG
  9585.                                                 ;Type code = SNG ?
  9586.   027BA 2806            JR      Z,27C2H         ;Yes: continue at 27C2H
  9587.                                                 ;No:
  9588.   027BC 29              ADD     HL,HL           ;* 2 = offset for DBL
  9589.   027BD B7              OR      A               ;Type code = INT or DBL ?
  9590.   027BE E2C227          JP      PO,27C2H        ;Yes: HL contains proper offset
  9591.                                                 ;No:
  9592.   027C1 09              ADD     HL,BC           ;Add again: offset for STR
  9593.  
  9594.   027C2 C1              POP     BC              ;Start address array to BC
  9595.   027C3 09              ADD     HL,BC           ;+ offset = requested address
  9596.   027C4 EB              EX      DE,HL           ;DE = address
  9597.   027C5 2AF340          LD      HL,(40F3H)      ;HL = PTP
  9598.   027C8 C9              RET
  9599.  
  9600.  
  9601. ; MEM = FRE (numerical variable )
  9602.  
  9603.   027C9 AF              XOR     A               ;A = 0
  9604.   027CA E5              PUSH    HL              ;Save PTP
  9605.   027CB 32AF40          LD      (40AFH),A       ;Set type code to 0
  9606.   027CE CDD427          CALL    27D4H           ;X = end of stack - start of
  9607.                                                 ;free memory = number of
  9608.                                                 ;free bytes
  9609.   027D1 E1              POP     HL              ;Restore PTP
  9610.   027D2 D7              RST     10H             ;Increase PTP
  9611.   027D3 C9              RET
  9612.  
  9613.  
  9614. ; X = FRE ( arg )
  9615. ; ---------------
  9616.  
  9617.   027D4 2AFD40          LD      HL,(40FDH)      ;HL = start of free memory
  9618.   027D7 EB              EX      DE,HL           ;DE = HL
  9619.   027D8 210000          LD      HL,0000H        ;HL = 0000H
  9620.   027DB 39              ADD     HL,SP           ;HL = 0 + SP = SP
  9621.   027DC E7              RST     20H             ;TSTTYP
  9622.                                                 ;STR ? (number of free bytes
  9623.                                                 ;in string space required)
  9624.   027DD 200D            JR      NZ,27ECH        ;No: continue at 27ECH
  9625.                                                 ;Yes:
  9626.   027DF CDDA29          CALL    29DAH           ;Clear argument from string
  9627.                                                 ;space
  9628.   027E2 CDE628          CALL    28E6H           ;Sort string space
  9629.   027E5 2AA040          LD      HL,(40A0H)      ;HL -> start of string space
  9630.   027E8 EB              EX      DE,HL           ;DE = HL
  9631.   027E9 2AD640          LD      HL,(40D6H)      ;HL -> last string in string
  9632.                                                 ;space
  9633.   027EC 7D              LD      A,L             ;Compute the number of free
  9634.   027ED 93              SUB     E               ;bytes; result in HL
  9635.   027EE 6F              LD      L,A
  9636.   027EF 7C              LD      A,H
  9637.   027F0 9A              SBC     A,D
  9638.   027F1 67              LD      H,A
  9639.   027F2 C3660C          JP      0C66H           ;X = HL
  9640.  
  9641.  
  9642. ; X = POS (dummy)
  9643. ; ---------------
  9644.  
  9645.   027F5 3AA640          LD      A,(40A6H)       ;A = cursor position in line
  9646.  
  9647.   027F8 6F              LD      L,A             ;HL = A (without sign)
  9648.   027F9 AF              XOR     A
  9649.   027FA 67              LD      H,A
  9650.   027FB C39A0A          JP      0A9AH           ;X =  HL (INT)
  9651.  
  9652.  
  9653. ; X = USR ( X )
  9654. ; -------------
  9655.  
  9656.   027FE CDA941          CALL    41A9H           ;DOS
  9657.   02801 D7              RST     10H             ;Increase PTP
  9658.   02802 CD2C25          CALL    252CH           ;Process expression
  9659.   02805 E5              PUSH    HL              ;Save PTP
  9660.   02806 219008          LD      HL,0890H        ;Set new RET address
  9661.   02809 E5              PUSH    HL              ;to 0890H
  9662.   0280A 3AAF40          LD      A,(40AFH)       ;A = type code
  9663.   0280D F5              PUSH    AF              ;Save type code
  9664.   0280E FE03            CP      03H             ;STR argument?
  9665.   02810 CCDA29          CALL    Z,29DAH         ;Yes: remove argument from
  9666.                                                 ;string space
  9667.   02813 F1              POP     AF              ;Restore type code
  9668.   02814 EB              EX      DE,HL           ;DE = 0890H (numerical
  9669.                                                 ;argument)
  9670.                                                 ;DE = vector address (string
  9671.                                                 ;argument)
  9672.   02815 2A8E40          LD      HL,(408EH)      ;HL -> USR routine
  9673.   02818 E9              JP      (HL)            ;Execute routine
  9674.  
  9675.  
  9676. ; Conversion of X into desired type
  9677. ;
  9678. ; I: A = desired type code
  9679. ; (see 1F35H)
  9680.  
  9681.   02819 E5              PUSH    HL              ;Save HL
  9682.   0281A E607            AND     07H             ;A = offset for jump table
  9683.                                                 ;INT: A = 2, STR A = 3
  9684.                                                 ;SNG: A = 4, DBL A = 0
  9685.   0281C 21A118          LD      HL,18A1H        ;HL -> jump table for type
  9686.                                                 ;conversion
  9687.   0281F 4F              LD      C,A             ;BC = offset
  9688.   02820 0600            LD      B,00H
  9689.   02822 09              ADD     HL,BC           ;Add offset to pointer
  9690.   02823 CD8625          CALL    2586H           ;Add offset again (2 bytes
  9691.                                                 ;per address) and execute
  9692.                                                 ;routine
  9693.   02826 E1              POP     HL              ;restore HL
  9694.   02827 C9              RET
  9695.  
  9696.  
  9697. ; SUB for INPUT
  9698. ; Test for ?ID Error
  9699. ; (see 219AH)
  9700.  
  9701.   02828 E5              PUSH    HL              ;Save PTP
  9702.   02829 2AA240          LD      HL,(40A2H)      ;HL = current LN
  9703.   0282C 23              INC     HL              ;Current LN = 65535 ?
  9704.   0282D 7C              LD      A,H             ;(HL + 1 = 0000H)
  9705.   0282E B5              OR      L               ;(65535 = LN for command mode)
  9706.   0282F E1              POP     HL              ;Restore PTP
  9707.   02830 C0              RET     NZ              ;No: ok, return
  9708.  
  9709.  
  9710. ; ?ID Error
  9711.  
  9712.   02831 1E16            LD      E,16H           ;E = error code for ?ID Error
  9713.   02833 C3A219          JP      19A2H           ;Issue error
  9714.  
  9715.  
  9716. ; X = STR$ ( X )
  9717. ; --------------
  9718.  
  9719.   02836 CDBD0F          CALL    0FBDH           ;Convert X into string from
  9720.                                                 ;4130H onwards
  9721.   02839 CD6528          CALL    2865H           ;Take string constant
  9722.   0283C CDDA29          CALL    29DAH           ;and remove from string space
  9723.                                                 ;HL -> string vector
  9724.   0283F 012B2A          LD      BC,2A2BH        ;Set new Return address
  9725.   02842 C5              PUSH    BC              ;to 2A2BH
  9726.  
  9727.  
  9728. ; Insert new string into string space
  9729. ;
  9730. ; I: HL -> vector to new string (somewhere in memory)
  9731. ; O: DE -> vector to new string (in string space)
  9732.  
  9733.   02843 7E              LD      A,(HL)          ;A = string length
  9734.   02844 23              INC     HL              ;Pointer + 1
  9735.   02845 E5              PUSH    HL              ;Save pointer
  9736.   02846 CDBF28          CALL    28BFH           ;Are there still A bytes free
  9737.                                                 ;in string space ?
  9738.                                                 ;Yes: DE -> 1st free byte for
  9739.                                                 ;the new string
  9740.                                                 ;No: ?OS Error
  9741.   02849 E1              POP     HL              ;Restore pointer
  9742.   0284A 4E              LD      C,(HL)          ;String address to BC
  9743.   0284B 23              INC     HL
  9744.   0284C 46              LD      B,(HL)
  9745.   0284D CD5A28          CALL    285AH           ;Store length and address of
  9746.                                                 ;free memory (in string space)
  9747.                                                 ;as last entry in string table
  9748.   02850 E5              PUSH    HL              ;Save vector address of new
  9749.                                                 ;string space
  9750.   02851 6F              LD      L,A             ;L = string length
  9751.   02852 CDCE29          CALL    29CEH           ;Copy string from (BC) to (DE):
  9752.                                                 ;Copy string to string space
  9753.   02855 D1              POP     DE              ;DE -> vector to new string
  9754.   02856 C9              RET
  9755.  
  9756.  
  9757. ; Search for room in string space
  9758. ;
  9759. ; I: A = length of new string
  9760. ; O: DE = start address in string space of the new string
  9761. ;    HL = 40D3H = vector address of free string
  9762.  
  9763.   02857 CDBF28          CALL    28BFH           ;Still A bytes free in string
  9764.                                                 ;space ?
  9765.                                                 ;Yes: DE -> 1st free byte
  9766.                                                 ;No: ?OS Error
  9767.   0285A 21D340          LD      HL,40D3H        ;HL -> vector of free string
  9768.   0285D E5              PUSH    HL              ;Save pointer
  9769.   0285E 77              LD      (HL),A          ;A = string length
  9770.   0285F 23              INC     HL
  9771.   02860 73              LD      (HL),E          ;Store address of free string
  9772.   02861 23              INC     HL              ;space memory
  9773.   02862 72              LD      (HL),D
  9774.   02863 E1              POP     HL              ;Retore vector address
  9775.   02864 C9              RET
  9776.  
  9777.  
  9778. ; Process string constant at (HL) and store in string table
  9779. ;
  9780. ; I: HL -> string constant (terminated with '"' or 00H
  9781. ; O: A = last string character
  9782. ;    HL -> end of string
  9783. ;    X = vector address of new string
  9784.  
  9785.   02865 2B              DEC     HL              ;Pointer - 1
  9786.   02866 0622            LD      B,22H           ;B = 1st delimiter char. ('"')
  9787.   02868 50              LD      D,B             ;D = 2nd delimiter character
  9788.   02869 E5              PUSH    HL              ;Save pointer
  9789.   0286A 0EFF            LD      C,0FFH          ;Set length counter to -1
  9790.   0286C 23              INC     HL              ;Pointer + 1
  9791.   0286D 7E              LD      A,(HL)          ;Get string character
  9792.   0286E 0C              INC     C               ;Counter + 1
  9793.   0286F B7              OR      A               ;End of string (00H) ?
  9794.   02870 2806            JR      Z,2878H         ;Yes: continue 2878H
  9795.  
  9796.   02872 BA              CP      D               ;2nd delimiter reached?
  9797.   02873 2803            JR      Z,2878H         ;Yes: end of string
  9798.                                                 ;No:
  9799.   02875 B8              CP      B               ;Start delimiter reached ?
  9800.   02876 20F4            JR      NZ,286CH        ;No: get next character
  9801.                                                 ;Yes:
  9802.   02878 FE22            CP      22H             ;String terminated by '"' ?
  9803.   0287A CC781D          CALL    Z,1D78H         ;Yes: increase pointer
  9804.  
  9805.   0287D E3              EX      (SP),HL         ;Save end pointer, HL = start
  9806.                                                 ;pointer
  9807.   0287E 23              INC     HL              ;Pointer + 1
  9808.   0287F EB              EX      DE,HL           ;DE = start pointer
  9809.   02880 79              LD      A,C             ;C = length
  9810.   02881 CD5A28          CALL    285AH           ;Store length and pointer as
  9811.                                                 ;last entry in string table
  9812.   02884 11D340          LD      DE,40D3H        ;DE = string vector
  9813.   02887 3ED5            LD      A,0D5H          ;--
  9814. * 02887   D5            PUSH    DE              ;Called from FN routine in DOS
  9815.  
  9816.   02889 2AB340          LD      HL,(40B3H)      ;HL = next free position in
  9817.                                                 ;string table
  9818.   0288C 222141          LD      (4121H),HL      ;Save vector address in X
  9819.   0288F 3E03            LD      A,03H           ;Set VT to STR
  9820.   02891 32AF40          LD      (40AFH),A
  9821.   02894 CDD309          CALL    09D3H           ;Copy 3 bytes from (DE) to (HL)
  9822.                                                 ;(copy length and address into
  9823.                                                 ;string table)
  9824.   02897 11D640          LD      DE,40D6H        ;End of string table reached ?
  9825.   0289A DF              RST     18H             ;(HL = 40D6H)
  9826.   0289B 22B340          LD      (40B3H),HL      ;Save table pointer
  9827.   0289E E1              POP     HL              ;Restore end pointer
  9828.   0289F 7E              LD      A,(HL)          ;A = last character
  9829.   028A0 C0              RET     NZ              ;No: done, return
  9830.                                                 ;Yes:
  9831.   028A1 1E1E            LD      E,1EH           ;E = error code for ?ST Error
  9832.   028A3 C3A219          JP      19A2H           ;Issue error
  9833.  
  9834.  
  9835. ; Process and output text constant at (HL)
  9836. ;
  9837. ; I: HL -> string constant
  9838.  
  9839.   028A6 23              INC     HL              ;Pointer + 1
  9840.   028A7 CD6528          CALL    2865H           ;Copy string constant
  9841.   028AA CDDA29          CALL    29DAH           ;and remove it from string
  9842.                                                 ;table
  9843.   028AD CDC409          CALL    09C4H           ;D = length, BC = start
  9844.                                                 ;address of string
  9845.   028B0 14              INC     D               ;Counter + 1
  9846.  
  9847.   028B1 15              DEC     D               ;Counter - 1; counter = 0 ?
  9848.   028B2 C8              RET     Z               ;Yes: done, return
  9849.  
  9850.   028B3 0A              LD      A,(BC)          ;Get character
  9851.   028B4 CD2A03          CALL    032AH           ;Output character
  9852.   028B7 FE0D            CP      0DH             ;CR/LF ?
  9853.   028B9 CC0321          CALL    Z,2103H         ;Yes: call DOS
  9854.   028BC 03              INC     BC              ;Pointer + 1
  9855.   028BD 18F2            JR      28B1H           ;Next character
  9856.  
  9857.  
  9858. ; Check for room in string space
  9859. ; In case there are less than A bytes free in string space, the string space
  9860. ; is sorted (garbage collection) and the routine is called again.
  9861. ; If the string space is full a ?OS Error is generated
  9862. ;
  9863. ; I: A = number of required bytes
  9864. ; O: DE -> A bytes free space in string space
  9865. ;    HL -> start of string space
  9866.  
  9867.   028BF B7              OR      A               ;Z-flag = 0: 1st run
  9868.   028C0 0EF1            LD      C,0F1H          ;--
  9869.  
  9870. ; Entry at 2nd call (after sorting)
  9871.  
  9872. * 028C0   F1            POP     AF              ;Restore AF (Z-flag is now 1 !)
  9873.   028C2 F5              PUSH    AF              ;Save AF
  9874.   028C3 2AA040          LD      HL,(40A0H)      ;HL -> start of string space
  9875.   028C6 EB              EX      DE,HL           ;DE = HL
  9876.   028C7 2AD640          LD      HL,(40D6H)      ;HL -> first free byte in
  9877.                                                 ;string space (the string space
  9878.                                                 ;is filled top down!)
  9879.   028CA 2F              CPL                     ;A = -A
  9880.   028CB 4F              LD      C,A             ;BC = number of required bytes
  9881.   028CC 06FF            LD      B,0FFH
  9882.   028CE 09              ADD     HL,BC           ;HL = HL + (-number of required
  9883.                                                 ;bytes) = start address for
  9884.                                                 ;the new string
  9885.   028CF 23              INC     HL              ;+ 1 (HL already pointed to a
  9886.                                                 ;free byte)
  9887.   028D0 DF              RST     18H             ;Still inside string space ?
  9888.   028D1 3807            JR      C,28DAH         ;No: sort string space and try
  9889.                                                 ;    again at 1st run
  9890.                                                 ;    ?OS Error at 2nd run
  9891.                                                 ;Yes:
  9892.   028D3 22D640          LD      (40D6H),HL      ;Store pointer
  9893.   028D6 23              INC     HL              ;Pointer + 1
  9894.   028D7 EB              EX      DE,HL           ;DE -> free space
  9895.   028D8 F1              POP     AF              ;Restore AF
  9896.   028D9 C9              RET
  9897.  
  9898.  
  9899. ; Sort string space (garbage collection)
  9900. ; Check if there are string in the string space, that belong to already
  9901. ; cleared variables. If so, remove this garbage and move the used string
  9902. ; space accordingly.
  9903.  
  9904.   028DA F1              POP     AF              ;Restore flag
  9905.                                                 ;Already the 2nd run ?
  9906.   028DB 1E1A            LD      E,1AH           ;E = error code for ?OS Error
  9907.   028DD CAA219          JP      Z,19A2H         ;Yes: issue error
  9908.  
  9909.   028E0 BF              CP      A               ;Z-flag = 1 (mark 2nd run)
  9910.   028E1 F5              PUSH    AF              ;Save flag
  9911.   028E2 01C128          LD      BC,28C1H        ;Set new RET address
  9912.   028E5 C5              PUSH    BC              ;to 28C1H
  9913.   028E6 2AB140          LD      HL,(40B1H)      ;HL -> End of string space
  9914.  
  9915.  
  9916. ; Search next highest string in string space and sort
  9917. ; (highest string is the string with the highest start address)
  9918. ; HL = address of string last inserted and sorted in string space
  9919. ; (= address of last string in string space at last run)
  9920.  
  9921.   028E9 22D640          LD      (40D6H),HL      ;Store address of last string
  9922.                                                 ;sorted
  9923.   028EC 210000          LD      HL,0000H        ;HL = 0000H (default address)
  9924.   028EF E5              PUSH    HL              ;Save it on stack
  9925.   028F0 2AA040          LD      HL,(40A0H)      ;HL -> start of string space
  9926.   028F3 E5              PUSH    HL              ;Save pointer on stack
  9927.  
  9928. ; Test on highest string starts at the string space start address and ends
  9929. ; on the last inserted and sorted string.
  9930. ; The highest string in this range is then sorted and the routine is
  9931. ; repeated until no highest string is found (all strings are sorted)
  9932. ; Looks like bubblesort is used!
  9933.  
  9934.   028F4 21B540          LD      HL,40B5H        ;HL -> start of string table
  9935.                                                 ;= vector address of the first
  9936.                                                 ;string in the string table
  9937.  
  9938. ; Test all string variables
  9939. ; First the entries in the string table, then the string (single) variables
  9940. ; and finally the string array variables are tested to see if they are
  9941. ; located in string space
  9942.  
  9943.   028F7 EB              EX      DE,HL
  9944.  
  9945.   028F8 2AB340          LD      HL,(40B3H)      ;DE -> next free location in
  9946.                                                 ;string space
  9947.   028FB EB              EX      DE,HL
  9948.   028FC DF              RST     18H             ;End of table reached?
  9949.   028FD 01F728          LD      BC,28F7H        ;BC = new RET address
  9950.   02900 C24A29          JP      NZ,294AH        ;No : continue at 29A4H
  9951.  
  9952.   02903 2AF940          LD      HL,(40F9H)      ;HL -> start of BASIC var space
  9953.   02906 EB              EX      DE,HL
  9954.   02907 2AFB40          LD      HL,(40FBH)
  9955.   0290A EB              EX      DE,HL           ;DE -> start of array variables
  9956.   0290B DF              RST     18H             ;End of single variables
  9957.                                                 ;storage space reached?
  9958.   0290C 2813            JR      Z,2921H         ;Yes: continue at 2921H
  9959.  
  9960.   0290E 7E              LD      A,(HL)          ;A = type code
  9961.   0290F 23              INC     HL              ;Pointer + 1 (type code)
  9962.   02910 23              INC     HL              ;Pointer + 2 (name)
  9963.   02911 23              INC     HL
  9964.   02912 FE03            CP      03H             ;String variable found ?
  9965.   02914 2004            JR      NZ,291AH        ;No: test next variable
  9966.                                                 ;Yes:
  9967.   02916 CD4B29          CALL    294BH           ;Test address
  9968.   02919 AF              XOR     A               ;A = 00H
  9969.   0291A 5F              LD      E,A             ;DE = offset to next variable
  9970.   0291B 1600            LD      D,00H
  9971.   0291D 19              ADD     HL,DE           ;Add offset:
  9972.                                                 ;HL -> vector of next variable
  9973.   0291E 18E6            JR      2906H           ;Test next variable
  9974.  
  9975. ; End of single variable space reached.
  9976. ; Test array variables.
  9977.  
  9978.   02920 C1              POP     BC              ;Correct stack
  9979.   02921 EB              EX      DE,HL
  9980.   02922 2AFD40          LD      HL,(40FDH)      ;DE -> end of array variable
  9981.   02925 EB              EX      DE,HL           ;space
  9982.   02926 DF              RST     18H             ;End reached ?
  9983.   02927 CA6B29          JP      Z,296BH         ;Yes: insert highest string
  9984.                                                 ;and sort
  9985.                                                 ;No:
  9986.   0292A 7E              LD      A,(HL)          ;A = type code of array element
  9987.   0292B 23              INC     HL              ;Pointer + 1
  9988.   0292C CDC209          CALL    09C2H           ;BCDE = (HL): DE = name,
  9989.                                                 ;BC = length of array
  9990.   0292F E5              PUSH    HL              ;Save pointer
  9991.   02930 09              ADD     HL,BC           ;HL = pointer on next array
  9992.   02931 FE03            CP      03H             ;String array found ?
  9993.   02933 20EB            JR      NZ,2920H        ;No: test next array
  9994.                                                 ;Yes:
  9995.   02935 22D840          LD      (40D8H),HL      ;Save pointer
  9996.   02938 E1              POP     HL              ;HL -> number of dimensions
  9997.   02939 4E              LD      C,(HL)          ;BC = number of dimensions
  9998.   0293A 0600            LD      B,00H
  9999.   0293C 09              ADD     HL,BC           ;Add twice (every dimension
  10000.   0293D 09              ADD     HL,BC           ;is stored in 2 bytes)
  10001.                                                 ;HL now contains the vector
  10002.                                                 ;address of the first array
  10003.                                                 ;element - 1
  10004.   0293E 23              INC     HL              ;HL + 1: vector
  10005.   0293F EB              EX      DE,HL           ;DE -> next array (= end address
  10006.   02940 2AD840          LD      HL,(40D8H)      ;of current array, see 2935H)
  10007.   02943 EB              EX      DE,HL
  10008.   02944 DF              RST     18H             ;End of array reached ?
  10009.   02945 28DA            JR      Z,2921H         ;Yes: test next array
  10010.                                                 ;No:
  10011.   02947 013F29          LD      BC,293FH        ;Set new RET address
  10012.   0294A C5              PUSH    BC              ;to 293FH
  10013.  
  10014.  
  10015. ; Check string address of found string variable
  10016. ; Two criteria exist:
  10017. ; 1. Is the string in string space and not reused again ?
  10018. ;    (Is the string address smaller than the address of the last string
  10019. ;     sorted and inserted ?)
  10020. ; 2. Is the string located at a higher position in string space than the
  10021. ;    string that is accepted to be the highest string in string space ?
  10022. ;    (Is the string address bigger than the string address of the string
  10023. ;     that is the highest accepted string ?)
  10024. ;
  10025. ; If both criteria are true, the address of the found string is stored on stack
  10026. ; as the highest address.
  10027. ;
  10028. ; I: HL = vector address of string variable
  10029. ; O: HL = vector address on the next variable
  10030.  
  10031.   0294B AF              XOR     A               ;A = 00H
  10032.   0294C B6              OR      (HL)            ;A = string length
  10033.   0294D 23              INC     HL              ;Pointer + 1
  10034.   0294E 5E              LD      E,(HL)          ;DE -> string
  10035.   0294F 23              INC     HL
  10036.   02950 56              LD      D,(HL)
  10037.   02951 23              INC     HL
  10038.                                                 ;String length = 0 ?
  10039.   02952 C8              RET     Z               ;Yes: done, return
  10040.                                                 ;No:
  10041. ; 1st criterium
  10042.  
  10043.   02953 44              LD      B,H             ;BC -> vector of next variable
  10044.   02954 4D              LD      C,L
  10045.   02955 2AD640          LD      HL,(40D6H)      ;HL = address of last inserted
  10046.                                                 ;variable
  10047.   02958 DF              RST     18H             ;Is the found string already
  10048.                                                 ;re-inserted into the string
  10049.                                                 ;space ?
  10050.                                                 ;(Is the string address greater
  10051.                                                 ;thean de address of the last
  10052.                                                 ;inserted string ?)
  10053.   02959 60              LD      H,B             ;HL -> vector
  10054.   0295A 69              LD      L,C
  10055.   0295B D8              RET     C               ;Yes: done, return
  10056.                                                 ;No:
  10057. ; 2nd criterium
  10058.  
  10059.   0295C E1              POP     HL              ;HL = RET address
  10060.   0295D E3              EX      (SP),HL         ;Save RET address
  10061.                                                 ;HL = address of last inserted
  10062.                                                 ;highest string (see 2968H)
  10063.   0295E DF              RST     18H             ;Start address of string > HL ?
  10064.   0295F E3              EX      (SP),HL         ;Put HL and RET adrress
  10065.   02960 E5              PUSH    HL              ;back on the stack
  10066.   02961 60              LD      H,B             ;HL -> vector
  10067.   02962 69              LD      L,C
  10068.   02963 D0              RET     NC              ;No: done, return
  10069.                                                 ;Yes:
  10070. ; New highest string found
  10071. ; Save string address and vector address on stack
  10072.  
  10073.   02964 C1              POP     BC              ;BC = RET address
  10074.   02965 F1              POP     AF              ;Remove start address of last
  10075.                                                 ;inserted highest string
  10076.   02966 F1              POP     AF              ;and its vector address
  10077.                                                 ;from stack
  10078.   02967 E5              PUSH    HL              ;Save current vector address
  10079.   02968 D5              PUSH    DE              ;and start address of string
  10080.                                                 ;on stack
  10081.   02969 C5              PUSH    BC              ;Put RET address back on stack
  10082.   0296A C9              RET
  10083.  
  10084. ; All stringvariables tested
  10085. ; Sort and insert new highest string behind last sorted and inserted string
  10086. ; and clear all garbage in between.
  10087.  
  10088.   0296B D1              POP     DE              ;Take address of highest string
  10089.   0296C E1              POP     HL              ;and the vector address
  10090.   0296D 7D              LD      A,L             ;Address changes since 28ECH ?
  10091.   0296E B4              OR      H               ;(new highest string found ?)
  10092.   0296F C8              RET     Z               ;No: done, return
  10093.  
  10094. ; A new highest string has been found
  10095.  
  10096.   02970 2B              DEC     HL              ;BC = address of new highest
  10097.   02971 46              LD      B,(HL)          ;string
  10098.   02972 2B              DEC     HL              ;( = DE because the address
  10099.   02973 4E              LD      C,(HL)          ;belongs to DE )
  10100.   02974 E5              PUSH    HL              ;Save address + 1
  10101.   02975 2B              DEC     HL              ;HL - 1 = vector address
  10102.                                                 ;of last tested string
  10103.   02976 6E              LD      L,(HL)
  10104.   02977 2600            LD      H,00H           ;HL = string length
  10105.   02979 09              ADD     HL,BC           ;HL = length + string address
  10106.   0297A 50              LD      D,B             ;DE = string address
  10107.   0297B 59              LD      E,C
  10108.   0297C 2B              DEC     HL              ;HL = end address of string
  10109.   0297D 44              LD      B,H             ;BC = HL
  10110.   0297E 4D              LD      C,L
  10111.   0297F 2AD640          LD      HL,(40D6H)      ;HL = address of last inserted
  10112.                                                 ;string
  10113.   02982 CD5819          CALL    1958H           ;Insert new string by attaching
  10114.                                                 ;the new string to the last
  10115.                                                 ;inserted string
  10116.   02985 E1              POP     HL              ;Restore vector address + 1
  10117.   02986 71              LD      (HL),C          ;Use new start address
  10118.   02987 23              INC     HL
  10119.   02988 70              LD      (HL),B
  10120.   02989 69              LD      L,C             ;HL = start address of inserted
  10121.   0298A 60              LD      H,B             ;string
  10122.   0298B 2B              DEC     HL              ;HL - 1 = address of last
  10123.                                                 ;inserted string
  10124.   0298C C3E928          JP      28E9H           ;Search next highest string
  10125.  
  10126.  
  10127. ; String addition
  10128. ;
  10129. ; I: HL = PTP on '+'
  10130. ;    BC = priority code
  10131. ;    X  = 1st string
  10132. ; O: X  = new string
  10133.  
  10134.   0298F C5              PUSH    BC              ;Save priority code
  10135.   02990 E5              PUSH    HL              ;Save PTP
  10136.   02991 2A2141          LD      HL,(4121H)      ;HL -> vector of 1st string
  10137.   02994 E3              EX      (SP),HL         ;Save vector adrress
  10138.                                                 ;and restore PTP
  10139.   02995 CD9F24          CALL    249FH           ;X = 2nd string
  10140.   02998 E3              EX      (SP),HL         ;Save PTP and restore vector
  10141.                                                 ;of 1st string
  10142.   02999 CDF40A          CALL    0AF4H           ;?TM Error if X is not STR type
  10143.   0299C 7E              LD      A,(HL)          ;A = length of 1st string
  10144.   0299D E5              PUSH    HL              ;Save vector address of
  10145.                                                 ;1st string
  10146.   0299E 2A2141          LD      HL,(4121H)      ;HL -> vector of 2nd string
  10147.   029A1 E5              PUSH    HL              ;Save vector address of
  10148.                                                 ;2nd string
  10149.   029A2 86              ADD     A,(HL)          ;A = total length of new
  10150.                                                 ;string
  10151.   029A3 1E1C            LD      E,1CH           ;E = error code for ?LS Error
  10152.                                                 ;Overflow ? (new string too
  10153.                                                 ;long)
  10154.   029A5 DAA219          JP      C,19A2H         ;Yes: ?LS Error
  10155.                                                 ;No:
  10156.   029A8 CD5728          CALL    2857H           ;Reserve A bytes of free space
  10157.                                                 ;in string space. HL -> vector
  10158.                                                 ;new string (space)
  10159.   029AB D1              POP     DE              ;Restore vector address of
  10160.                                                 ;2nd string
  10161.   029AC CDDE29          CALL    29DEH           ;Delete 2nd string from string
  10162.                                                 ;table and string space ?
  10163.   029AF E3              EX      (SP),HL         ;Save vector address of 2nd
  10164.                                                 ;string and restore vector
  10165.                                                 ;address of 1st string
  10166.   029B0 CDDD29          CALL    29DDH           ;Delete 1nd string from string
  10167.                                                 ;table and string space ?
  10168.   029B3 E5              PUSH    HL              ;Save vector address of
  10169.                                                 ;1st string
  10170.   029B4 2AD440          LD      HL,(40D4H)      ;HL = address of free space for
  10171.                                                 ;new string
  10172.   029B7 EB              EX      DE,HL           ;DE = HL
  10173.   029B8 CDC629          CALL    29C6H           ;Copy 1st string into new space
  10174.   029BB CDC629          CALL    29C6H           ;Copy 2nd string behind 1st
  10175.                                                 ;string
  10176.   029BE 214923          LD      HL,2349H        ;Jump back to decoding of
  10177.                                                 ;expression ar 2349H
  10178.   029C1 E3              EX      (SP),HL         ;New RET address on stack
  10179.   029C2 E5              PUSH    HL              ;Save old RET address on stack
  10180.   029C3 C38428          JP      2884H           ;Insert new string into
  10181.                                                 ;string table
  10182.  
  10183. ; Copy string to (DE)
  10184. ;
  10185. ; I: (SP - 2) -> string vector
  10186. ;          DE =  new string address
  10187.  
  10188.   029C6 E1              POP     HL              ;RET address to HL
  10189.   029C7 E3              EX      (SP),HL         ;Get vector address and put
  10190.                                                 ;RET address back on stack
  10191.   029C8 7E              LD      A,(HL)          ;A = length
  10192.   029C9 23              INC     HL
  10193.   029CA 4E              LD      C,(HL)          ;BC = string address
  10194.   029CB 23              INC     HL
  10195.   029CC 46              LD      B,(HL)
  10196.   029CD 6F              LD      L,A             ;L = counter
  10197.   029CE 2C              INC     L               ;Adjust counter
  10198.  
  10199.   029CF 2D              DEC     L               ;Counter - 1
  10200.                                                 ;Counter = 0 ?
  10201.   029D0 C8              RET     Z               ;Yes: done, return
  10202.                                                 ;No:
  10203.   029D1 0A              LD      A,(BC)          ;Copy byte from (BC)
  10204.   029D2 12              LD      (DE),A          ;to (DE)
  10205.   029D3 03              INC     BC              ;Source pointer + 1
  10206.   029D4 13              INC     DE              ;Destination pointer + 1
  10207.   029D5 18F8            JR      29CFH           ;Next byte
  10208.  
  10209.  
  10210. ; Remove string in X from string table and string space ?
  10211. ;
  10212. ; When using string-functions all string constants and intermediate results
  10213. ; are basically intermediate in the string table and string space.
  10214. ; Since these strings are only used by the function, they can be removed
  10215. ; after completion of the function
  10216. ;
  10217. ; I: X   = string
  10218. ; O: HL -> string vector
  10219.  
  10220.   029D7 CDF40A          CALL    0AF4H           ;Test if X in STR format
  10221.   029DA 2A2141          LD      HL,(4121H)      ;HL -> string vector
  10222.  
  10223. ; Remove from string table and string space ?
  10224.  
  10225.   029DD EB              EX      DE,HL           ;DE -> string vector
  10226.   029DE CDF529          CALL    29F5H           ;If the vector address points
  10227.                                                 ;to the last entry in the
  10228.                                                 ;string table, it will be
  10229.                                                 ;deleted (in this case it
  10230.                                                 ;is an intermediate result or
  10231.                                                 ;a string constant)
  10232.   029E1 EB              EX      DE,HL           ;HL -> string vector
  10233.                                                 ;Last entry deleted ?
  10234.   029E2 C0              RET     NZ              ;No: done, return
  10235.  
  10236.   029E3 D5              PUSH    DE              ;Save vector address
  10237.   029E4 50              LD      D,B             ;BC = string address
  10238.   029E5 59              LD      E,C
  10239.   029E6 1B              DEC     DE              ;DE - 1
  10240.   029E7 4E              LD      C,(HL)          ;C = string lenght
  10241.   029E8 2AD640          LD      HL,(40D6H)      ;HL = address of last string
  10242.                                                 ;in string space - 1
  10243.   029EB DF              RST     18H             ;Is the string the last one
  10244.                                                 ;in string space ? (this means
  10245.                                                 ;it is a string constant !)
  10246.   029EC 2005            JR      NZ,29F3H        ;No: done
  10247.                                                 ;Yes:
  10248.   029EE 47              LD      B,A             ;B = 0, BC = string length
  10249.   029EF 09              ADD     HL,BC           ;Add string length to HL
  10250.                                                 ;HL now points on the new last
  10251.                                                 ;string in string space
  10252.   029F0 22D640          LD      (40D6H),HL      ;Store address
  10253.                                                 ;(the string constant is now
  10254.                                                 ;removbed from string space)
  10255.   029F3 E1              POP     HL              ;Restore vector address
  10256.   029F4 C9              RET
  10257.  
  10258.  
  10259. ; Does DE point to the last entry of the string table ?
  10260. ; If it does, then the last entry is deleted.
  10261. ;
  10262. ; I: DE -> string vector
  10263. ; O: BC =  string address of last entry in string table
  10264. ;    DE -> string vector
  10265. ;    HL =  DE
  10266. ;    Z-flag = 1: the entry was deleted
  10267.  
  10268.   029F5 2AB340          LD      HL,(40B3H)      ;HL -> next free entry of
  10269.                                                 ;string table
  10270.   029F8 2B              DEC     HL
  10271.   029F9 46              LD      B,(HL)          ;BC = string address
  10272.   029FA 2B              DEC     HL
  10273.   029FB 4E              LD      C,(HL)
  10274.   029FC 2B              DEC     HL
  10275.   029FD DF              RST     18H             ;Does DE point to this entry ?
  10276.   029FE C0              RET     NZ              ;No: done, return
  10277.                                                 ;Yes:
  10278.   029FF 22B340          LD      (40B3H),HL      ;Save new address
  10279.                                                 ;(overwrite old entry)
  10280.   02A02 C9              RET
  10281.  
  10282.  
  10283. ; X = LEN ( X )
  10284. ; -------------
  10285.  
  10286.   02A03 01F827          LD      BC,27F8H        ;Set new return address
  10287.   02A06 C5              PUSH    BC              ;to 27F8H
  10288.  
  10289.   02A07 CDD729          CALL    29D7H           ;Test X has STR format
  10290.                                                 ;Remove string from string
  10291.                                                 ;table and string space ?
  10292.                                                 ;HL -> vector of string arg.
  10293.   02A0A AF              XOR     A               ;A = 00H
  10294.   02A0B 57              LD      D,A             ;D = 00H
  10295.   02A0C 7E              LD      A,(HL)          ;A = string length
  10296.   02A0D B7              OR      A               ;Set flags
  10297.   02A0E C9              RET                     ;Return to 27F8H: write A to X
  10298.                                                 ;as INT
  10299.  
  10300. ; X = ASC (  )
  10301. ; ------------
  10302.  
  10303.   02A0F 01F827          LD      BC,27F8H        ;Set new return address
  10304.   02A12 C5              PUSH    BC              ;to 27F8H
  10305.  
  10306.   02A13 CD072A          CALL    2A07H           ;Get string length and vector
  10307.                                                 ;address
  10308.                                                 ;Length zero ?
  10309.   02A16 CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  10310.                                                 ;HL -> string variable
  10311.   02A19 23              INC     HL
  10312.   02A1A 5E              LD      E,(HL)          ;DE -> string
  10313.   02A1B 23              INC     HL
  10314.   02A1C 56              LD      D,(HL)
  10315.  
  10316.   02A1D 1A              LD      A,(DE)          ;A = first string character
  10317.   02A1E C9              RET                     ;Return to 27F8H: write A to X
  10318.                                                 ;as INT
  10319.  
  10320. ; X = CHR$ (  )
  10321. ; -------------
  10322.  
  10323.   02A1F 3E01            LD      A,01H           ;A = length of resulting string
  10324.   02A21 CD5728          CALL    2857H           ;Create room in string space
  10325.   02A24 CD1F2B          CALL    2B1FH           ;Argument to DE
  10326.   02A27 2AD440          LD      HL,(40D4H)      ;HL -> string
  10327.   02A2A 73              LD      (HL),E          ;Write ASCII value into string
  10328.   02A2B C1              POP     BC              ;Remove RET address
  10329.   02A2C C38428          JP      2884H           ;Put string in string space
  10330.  
  10331.  
  10332. ; X = STRING$
  10333. ; -----------
  10334.  
  10335.   02A2F D7              RST     10H             ;Adjust PTP
  10336.   02A30 CF              RST     08H             ;Next character must be
  10337.   02A31 28              DEFB    '('             ;a '('
  10338.   02A32 CD1C2B          CALL    2B1CH           ;Get and save string length
  10339.   02A35 D5              PUSH    DE
  10340.   02A36 CF              RST     08H             ;Next character must be
  10341.   02A37 2C              DEFB    ','             ;a comma
  10342.   02A38 CD3723          CALL    2337H           ;Put argument into X
  10343.   02A3B CF              RST     08H             ;Next character must be
  10344.   02A3C 29              DEFB    ')'             ;a ')'
  10345.   02A3D E3              EX      (SP),HL         ;Save PTP, restore length
  10346.   02A3E E5              PUSH    HL              ;Save length
  10347.   02A3F E7              RST     20H             ;TSTTYP: STR argument ?
  10348.   02A40 2805            JR      Z,2A47H         ;Yes: continue at 2A47H
  10349.                                                 ;No:
  10350.   02A42 CD1F2B          CALL    2B1FH           ;Is the number in range
  10351.                                                 ;from 0 to 255 ? No: ?FC Error
  10352.   02A45 1803            JR      2A4AH           ;Continue at 2A4AH (A = number)
  10353.  
  10354.   02A47 CD132A          CALL    2A13H           ;A = ASCII value of first
  10355.                                                 ;character of string argument
  10356.   02A4A D1              POP     DE              ;Restore length
  10357.   02A4B F5              PUSH    AF              ;Dummy push because of jump
  10358.                                                 ;to 2A2BH after ending
  10359.   02A4C F5              PUSH    AF              ;Save character
  10360.   02A4D 7B              LD      A,E             ;A = length
  10361.   02A4E CD5728          CALL    2857H           ;Reserve space
  10362.   02A51 5F              LD      E,A             ;E = counter
  10363.   02A52 F1              POP     AF              ;Restore character
  10364.   02A53 1C              INC     E               ;Counter = 0?
  10365.   02A54 1D              DEC     E
  10366.   02A55 28D4            JR      Z,2A2BH         ;Yes: use string
  10367.                                                 ;No:
  10368.   02A57 2AD440          LD      HL,(40D4H)      ;HL -> free space
  10369.   02A5A 77              LD      (HL),A          ;Store character
  10370.   02A5B 23              INC     HL              ;Pointer + 1
  10371.   02A5C 1D              DEC     E               ;Counter - 1, counter = 0 ?
  10372.   02A5D 20FB            JR      NZ,2A5AH        ;No: next character
  10373.                                                 ;Yes:
  10374.   02A5F 18CA            JR      2A2BH           ;Use string
  10375.  
  10376.  
  10377. ; LEFT$
  10378. ; -----
  10379. ;
  10380. ; I: (SP - 2) -> vector of the string argument
  10381. ;    (SP)     =  number
  10382.  
  10383.   02A61 CDDF2A          CALL    2ADFH           ;')'?
  10384.                                                 ;B = number = new string length
  10385.   02A64 AF              XOR     A               ;Set starting point on 0
  10386.  
  10387. ; Entry for RIGHT$
  10388. ;
  10389. ; I: A = starting point of new string
  10390. ;    B = length of new string
  10391.  
  10392.   02A65 E3              EX      (SP),HL         ;Save PTP, HL -> vector
  10393.   02A66 4F              LD      C,A             ;C = A = starting point of
  10394.                                                 ;new string - 1
  10395.   02A67 3EE5            LD      A,0E5H          ;--
  10396.  
  10397. ; Entry for MID$
  10398. ;
  10399. ; I: B = length of new string
  10400. ;    C = starting point of new string inside string argument - 1
  10401.  
  10402. * 02A69   E5            PUSH    HL              ;Save vector address
  10403.  
  10404. ; Get substring from string argument (in X) and store in X
  10405. ;
  10406. ; I: X = string argument
  10407. ;    B = length of new string
  10408. ;    C = starting point of new string inside string argument - 1
  10409. ; O: X = requested substring
  10410.  
  10411.   02A69 E5              PUSH    HL              ;Save vector address
  10412.   02A6A 7E              LD      A,(HL)          ;A = length of argument string
  10413.   02A6B B8              CP      B               ;Substring longer than
  10414.                                                 ;argument string ?
  10415.   02A6C 3802            JR      C,2A70H         ;Yes: set C to 0 and use the
  10416.                                                 ;complete argument string
  10417.                                                 ;No:
  10418.   02A6E 78              LD      A,B             ;A = required length
  10419.   02A6F 110E00          LD      DE,000EH        ;--
  10420.  
  10421. * 02A70   0E00          LD      C,00H           ;Starting point = 0
  10422.   02A72 C5              PUSH    BC              ;Save BC
  10423.   02A73 CDBF28          CALL    28BFH           ;Free up A bytes of space
  10424.                                                 ;DE -> free space
  10425.   02A76 C1              POP     BC              ;Restore BC
  10426.   02A77 E1              POP     HL              ;Resore vector address
  10427.   02A78 E5              PUSH    HL              ;and save it again
  10428.   02A79 23              INC     HL              ;Vector address + 1
  10429.   02A7A 46              LD      B,(HL)          ;HL -> argument string
  10430.   02A7B 23              INC     HL
  10431.   02A7C 66              LD      H,(HL)
  10432.   02A7D 68              LD      L,B
  10433.   02A7E 0600            LD      B,00H           ;BC = offset to new starting
  10434.                                                 ;point
  10435.   02A80 09              ADD     HL,BC           ;HL -> start of substring
  10436.   02A81 44              LD      B,H             ;BC -> start of substring
  10437.   02A82 4D              LD      C,L
  10438.   02A83 CD5A28          CALL    285AH           ;Store length and address of
  10439.                                                 ;new string in string table
  10440.   02A86 6F              LD      L,A             ;L = length of substring
  10441.   02A87 CDCE29          CALL    29CEH           ;Copy characters of argument
  10442.                                                 ;string to (DE)
  10443.   02A8A D1              POP     DE              ;Restore vector address of
  10444.                                                 ;argument string
  10445.   02A8B CDDE29          CALL    29DEH           ;Delete argument string from
  10446.                                                 ;string space and string table?
  10447.   02A8E C38428          JP      2884H           ;Use new string
  10448.  
  10449.  
  10450. ; RIGHT$
  10451. ; ------
  10452. ;
  10453. ; I: (SP - 2) -> vector of the string argument
  10454. ;    (SP)     =  number (start of new string)
  10455.  
  10456.   02A91 CDDF2A          CALL    2ADFH           ;')'? B = number
  10457.   02A94 D1              POP     DE              ;DE = vector address
  10458.   02A95 D5              PUSH    DE              ;Back into stack
  10459.   02A96 1A              LD      A,(DE)          ;A = argument string length
  10460.   02A97 90              SUB     B               ;A = length - number =
  10461.                                                 ;starting point of new string
  10462.   02A98 18CB            JR      2A65H           ;Continue at 2A65H
  10463.  
  10464.  
  10465. ; MID$
  10466. ; ----
  10467. ; (on the right hand side of the equal sign
  10468. ;
  10469. ; I: (SP - 2) -> vector of the string argument
  10470. ;    (SP)     =  number (start of new string)
  10471.  
  10472.   02A9A EB              EX      DE,HL           ;HL = PTP
  10473.   02A9B 7E              LD      A,(HL)          ;A = next character
  10474.   02A9C CDE22A          CALL    2AE2H           ;B = starting point
  10475.   02A9F 04              INC     B               ;Starting point = 0 ?
  10476.   02AA0 05              DEC     B
  10477.   02AA1 CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  10478.                                                 ;No:
  10479.   02AA4 C5              PUSH    BC              ;Save starting point
  10480.   02AA5 1EFF            LD      E,0FFH          ;E = default length (in case
  10481.                                                 ;of missing length indication
  10482.                                                 ;the total string length
  10483.                                                 ;from starting point onwards
  10484.                                                 ;is used)
  10485.   02AA7 FE29            CP      ')'             ;Next character = ')' ?
  10486.   02AA9 2805            JR      Z,2AB0H         ;Yes: E = length
  10487.                                                 ;No:
  10488.   02AAB CF              RST     08H             ;Next character must be
  10489.   02AAC 2C              DEFB    ','             ;a comma
  10490.   02AAD CD1C2B          CALL    2B1CH           ;Get length and store in E
  10491.   02AB0 CF              RST     08H             ;Next character must be
  10492.   02AB1 29              DEFB    ')'             ;a ')'
  10493.   02AB2 F1              POP     AF              ;A = starting point
  10494.   02AB3 E3              EX      (SP),HL         ;Save PTP, restore vector addr.
  10495.   02AB4 01692A          LD      BC,2A69H        ;Set new RET address
  10496.   02AB7 C5              PUSH    BC              ;to 2A69H
  10497.   02AB8 3D              DEC     A               ;A = starting point - 1
  10498.   02AB9 BE              CP      (HL)            ;Compare with length of
  10499.                                                 ;argument string
  10500.   02ABA 0600            LD      B,00H           ;Length of new string = 0
  10501.   02ABC D0              RET     NC              ;Create string with length = 0
  10502.                                                 ;in case starting point is
  10503.                                                 ;larger than string length
  10504.   02ABD 4F              LD      C,A             ;C = starting point - 1
  10505.   02ABE 7E              LD      A,(HL)          ;A = length of arg. string
  10506.   02ABF 91              SUB     C               ;- statring point =
  10507.                                                 ;remaining string length
  10508.   02AC0 BB              CP      E               ;Requested length >
  10509.                                                 ;remaining length
  10510.   02AC1 47              LD      B,A             ;B = remaining length
  10511.   02AC2 D8              RET     C               ;Yes: B = length
  10512.                                                 ;No:
  10513.   02AC3 43              LD      B,E             ;B = requested length
  10514.   02AC4 C9              RET                     ;Use this
  10515.  
  10516.  
  10517. ; X = VAL (  )
  10518. ; ------------
  10519.  
  10520.   02AC5 CD072A          CALL    2A07H           ;Get vector address and length
  10521.                                                 ;of argument. D = 00H
  10522.                                                 ;String length zero ?
  10523.   02AC8 CAF827          JP      Z,27F8H         ;Yes: result = 0.
  10524.                                                 ;No:
  10525.   02ACB 5F              LD      E,A             ;E = length
  10526.   02ACC 23              INC     HL              ;Vector address + 1
  10527.   02ACD 7E              LD      A,(HL)          ;HL -> string
  10528.   02ACE 23              INC     HL
  10529.   02ACF 66              LD      H,(HL)
  10530.   02AD0 6F              LD      L,A
  10531.   02AD1 E5              PUSH    HL              ;Save pointer
  10532.   02AD2 19              ADD     HL,DE           ;Calculate string end address
  10533.   02AD3 46              LD      B,(HL)          ;Get next character
  10534.   02AD4 72              LD      (HL),D          ;and replace it by 00H
  10535.   02AD5 E3              EX      (SP),HL         ;Save end address, restore
  10536.                                                 ;start address
  10537.   02AD6 C5              PUSH    BC              ;Save character
  10538.   02AD7 7E              LD      A,(HL)          ;A = 1st character
  10539.   02AD8 CD650E          CALL    0E65H           ;Decode string, X = number
  10540.   02ADB C1              POP     BC              ;Restore characater
  10541.   02ADC E1              POP     HL              ;Restore end address
  10542.   02ADD 70              LD      (HL),B          ;Insert character
  10543.   02ADE C9              RET
  10544.  
  10545.  
  10546. ; SUB for LEFT$, RIGHT$ and MID$
  10547. ; Test for closing bracket and get first number from stack
  10548.  
  10549.   02ADF EB              EX      DE,HL           ;HL = PTP
  10550.   02AE0 CF              RST     08H             ;Next character must be
  10551.   02AE1 29              DEFB    ')'             ;a ')'
  10552.   02AE2 C1              POP     BC              ;BC = RET address
  10553.   02AE3 D1              POP     DE              ;DE = number
  10554.   02AE4 C5              PUSH    BC              ;Put RET address on stack
  10555.   02AE5 43              LD      B,E             ;B = number
  10556.   02AE6 C9              RET
  10557.  
  10558.  
  10559. ; Token found not in the range from 80H to BBH (see 1D67H)
  10560. ; (not a command)
  10561.  
  10562.   02AE7 FE7A            CP      7AH             ;Is it MID$ ?
  10563.                                                 ;(left of '=' sign !)
  10564.   02AE9 C29719          JP      NZ,1997H        ;No: ?SN Error
  10565.  
  10566.   02AEC C3D941          JP      41D9H           ;Yes: DOS
  10567.  
  10568.  
  10569. ; IN statement
  10570. ; ------------
  10571.  
  10572.   02AEF CD1F2B          CALL    2B1FH           ;A = port address
  10573.   02AF2 329440          LD      (4094H),A       ;Save it
  10574.   02AF5 CD9340          CALL    4093H           ;Perform IN
  10575.   02AF8 C3F827          JP      27F8H           ;Write A to X as INT
  10576.  
  10577.  
  10578. ; OUT statement
  10579. ; -------------
  10580.  
  10581.   02AFB CD0E2B          CALL    2B0EH           ;Get port address and value
  10582.   02AFE C39640          JP      4096H           ;Perform OUT
  10583.  
  10584.  
  10585. ; Decode argument at (HL) and store in X as an INT
  10586. ;
  10587. ; I: HL = PTP
  10588. ; O: DE = number
  10589. ;    A = MSB of number
  10590. ;    Z-flag = 1 if number < 256 (MSB = 0)
  10591.  
  10592.   02B01 D7              RST     10H             ;Get next non-space character
  10593.   02B02 CD3723          CALL    2337H           ;Decode argument
  10594.   02B05 E5              PUSH    HL              ;Save PTP
  10595.   02B06 CD7F0A          CALL    0A7FH           ;HL = X = CINT ( X )
  10596.   02B09 EB              EX      DE,HL           ;DE = INT number
  10597.   02B0A E1              POP     HL              ;Restore PTP
  10598.   02B0B 7A              LD      A,D             ;A = MSB
  10599.   02B0C B7              OR      A               ;MSB = 0 ?
  10600.   02B0D C9              RET
  10601.  
  10602.  
  10603. ; SUB for OUT
  10604. ; Get port address and prepare RAM at 4096H
  10605.  
  10606.   02B0E CD1C2B          CALL    2B1CH           ;Get port address
  10607.   02B11 329440          LD      (4094H),A       ;Store for IN
  10608.   02B14 329740          LD      (4097H),A       ;and OUT
  10609.   02B17 CF              RST     08H             ;Next character must be
  10610.   02B18 2C              DEFB    ','             ;a comma
  10611.   02B19 1801            JR      2B1CH           ;Get value and return
  10612.  
  10613.  
  10614. ; Decode argument at (HL) and store in X as INT
  10615. ; ?FC Error if the result is not in the range from 0 to 255
  10616. ;
  10617. ; I: HL = PTP
  10618. ; O: A = number
  10619. ;    DE = number
  10620.  
  10621.   02B1B D7              RST     10H             ;Get next non-space character
  10622.   02B1C CD3723          CALL    2337H           ;Decode argument
  10623.   02B1F CD052B          CALL    2B05H           ;In range [0..255] ?
  10624.   02B22 C24A1E          JP      NZ,1E4AH        ;No: ?FC Error
  10625.  
  10626.   02B25 2B              DEC     HL              ;PTP - 1
  10627.   02B26 D7              RST     10H             ;And increment again
  10628.   02B27 7B              LD      A,E             ;A = number
  10629.   02B28 C9              RET
  10630.  
  10631.  
  10632. ; LLIST statement
  10633. ; ---------------
  10634.  
  10635.   02B29 3E01            LD      A,01H           ;Set output flag to
  10636.   02B2B 329C40          LD      (409CH),A       ;printer output
  10637.  
  10638.  
  10639. ; LIST statement
  10640. ; --------------
  10641.  
  10642.   02B2E C1              POP     BC              ;Remove RET address
  10643.   02B2F CD101B          CALL    1B10H           ;Get line number
  10644.   02B32 C5              PUSH    BC              ;LP on first line
  10645.   02B33 21FFFF          LD      HL,0FFFFH       ;Set current LN to 65535
  10646.   02B36 22A240          LD      (40A2H),HL
  10647.   02B39 E1              POP     HL              ;HL = LP on line
  10648.   02B3A D1              POP     DE              ;DE = LN of last line
  10649.   02B3B 4E              LD      C,(HL)          ;BC = LP on next line
  10650.   02B3C 23              INC     HL
  10651.   02B3D 46              LD      B,(HL)
  10652.   02B3E 23              INC     HL
  10653.   02B3F 78              LD      A,B             ;End of program reached ?
  10654.   02B40 B1              OR      C
  10655.   02B41 CA191A          JP      Z,1A19H         ;Yes: done, continue at 1A19H
  10656.  
  10657.   02B44 CDDF41          CALL    41DFH           ;DOS
  10658.   02B47 CD9B1D          CALL    1D9BH           ;<SHIFT>+<@> or
  10659.                                                 ;<BREAK> pressed ?
  10660.   02B4A C5              PUSH    BC              ;Save LP
  10661.   02B4B 4E              LD      C,(HL)          ;BC = line number
  10662.   02B4C 23              INC     HL              ;of current line
  10663.   02B4D 46              LD      B,(HL)
  10664.   02B4E 23              INC     HL
  10665.   02B4F C5              PUSH    BC              ;Save line number
  10666.   02B50 E3              EX      (SP),HL         ;Save pointer, restore LN
  10667.   02B51 EB              EX      DE,HL           ;DE = current line number
  10668.                                                 ;HL = last line number
  10669.   02B52 DF              RST     18H             ;Current line number > last
  10670.                                                 ;line number ?
  10671.   02B53 C1              POP     BC              ;Pointer on current line
  10672.                                                 ;back in BC
  10673.   02B54 DA181A          JP      C,1A18H         ;Yes: done, continue at 1A18H
  10674.  
  10675.   02B57 E3              EX      (SP),HL         ;Save last line number
  10676.                                                 ;HL = LP on next line
  10677.   02B58 E5              PUSH    HL              ;Save LP on next line
  10678.   02B59 C5              PUSH    BC              ;Save LP on current line
  10679.   02B5A EB              EX      DE,HL           ;HL = current line number
  10680.   02B5B 22EC40          LD      (40ECH),HL      ;Save as '.'-line
  10681.   02B5E CDAF0F          CALL    0FAFH           ;Print HL as decimal number
  10682.   02B61 3E20            LD      A,20H           ;A = ' '
  10683.   02B63 E1              POP     HL              ;Restore pointer on current
  10684.                                                 ;line
  10685.   02B64 CD2A03          CALL    032AH           ;Print space
  10686.   02B67 CD7E2B          CALL    2B7EH           ;Decode line from (HL) onwards
  10687.                                                 ;and store in line buffer
  10688.   02B6A 2AA740          LD      HL,(40A7H)      ;HL -> start of line buffer
  10689.   02B6D CD752B          CALL    2B75H           ;Print line
  10690.   02B70 CDFE20          CALL    20FEH           ;Start new line
  10691.   02B73 18BE            JR      2B33H           ;Process next line
  10692.  
  10693.  
  10694. ; SUB for LIST
  10695. ; Print text from (HL) onwards, 00H = end of text
  10696. ;
  10697. ; I: HL -> text to be printed
  10698. ; O: HL -> end of text (00H)
  10699.  
  10700.   02B75 7E              LD      A,(HL)          ;A = next character
  10701.   02B76 B7              OR      A               ;End of text ?
  10702.   02B77 C8              RET     Z               ;Yes: done, return
  10703.  
  10704.   02B78 CD2A03          CALL    032AH           ;Print it
  10705.   02B7B 23              INC     HL              ;Pointer + 1
  10706.   02B7C 18F7            JR      2B75H           ;Next character
  10707.  
  10708.  
  10709. ; SUB for LIST and EDIT
  10710. ; Decode line from (HL) onwards and store result in line buffer
  10711. ;
  10712. ; I: HL -> program text
  10713.  
  10714.  
  10715.   02B7E E5              PUSH    HL              ;Save pointer
  10716.   02B7F 2AA740          LD      HL,(40A7H)      ;HL -> start of line buffer
  10717.   02B82 44              LD      B,H             ;BC -> start of line buffer
  10718.   02B83 4D              LD      C,L
  10719.   02B84 E1              POP     HL              ;Restore pointer
  10720.   02B85 16FF            LD      D,0FFH          ;D = counter (maximum length =
  10721.                                                 ;255 characters)
  10722.   02B87 1803            JR      2B8CH           ;Continue at 2B8CH
  10723.  
  10724. ; Decode next character
  10725.  
  10726.   02B89 03              INC     BC              ;Pointer + 1
  10727.   02B8A 15              DEC     D               ;Counter - 1
  10728.                                                 ;Maximum length reached ?
  10729.   02B8B C8              RET     Z               ;Yes: done, return
  10730.  
  10731. ; Decode line from (HL) onwards and store at (BC)
  10732.  
  10733.   02B8C 7E              LD      A,(HL)          ;A = next character
  10734.   02B8D B7              OR      A               ;End of line reached ?
  10735.   02B8E 23              INC     HL              ;Pointer + 1
  10736.   02B8F 02              LD      (BC),A          ;First store character
  10737.   02B90 C8              RET     Z               ;Yes: done, return
  10738.                                                 ;Token found ?
  10739.   02B91 F2D23F          JP      P,3FD2H         ;No: continue at 3FD2H
  10740.  
  10741.   02B94 FEFB            CP      0FBH            ;"'" token found ? (REM)
  10742.   02B96 2008            JR      NZ,2BA0H        ;No: continue at 2BA0H
  10743.  
  10744.   02B98 0B              DEC     BC              ;Yes: delete last 4 characters
  10745.   02B99 0B              DEC     BC              ;in line buffer because the
  10746.   02B9A 0B              DEC     BC              ;apostroph is stored as
  10747.   02B9B 0B              DEC     BC              ;'::REM'
  10748.   02B9C 14              INC     D               ;Counter + 1
  10749.   02B9D 14              INC     D
  10750.   02B9E 14              INC     D
  10751.   02B9F 14              INC     D
  10752.   02BA0 FE95            CP      95H             ;'ELSE' token found ?
  10753.   02BA2 CC240B          CALL    Z,0B24H         ;Yes: buffer pointer - 1
  10754.                                                 ;('ELSE' is stored as ':ELSE')
  10755.   02BA5 D67F            SUB     7FH             ;A = token value - 7FH
  10756.   02BA7 E5              PUSH    HL              ;Save pointer
  10757.   02BA8 5F              LD      E,A             ;E = token value
  10758.   02BA9 CDAD39          CALL    39ADH           ;Colour-Token found ?
  10759.                                                 ;HL -> start of corresponding
  10760.                                                 ;keyword table
  10761.   02BAC 7E              LD      A,(HL)          ;A = next character
  10762.   02BAD B7              OR      A               ;Next keyword reached ?
  10763.   02BAE 23              INC     HL              ;Pointer + 1
  10764.   02BAF F2AC2B          JP      P,2BACH         ;No: increment HL to next
  10765.                                                 ;keyword
  10766.   02BB2 1D              DEC     E               ;Token counter - 1
  10767.   02BB3 20F7            JR      NZ,2BACH        ;Increment pointer further
  10768.                                                 ;until right token is reached
  10769.   02BB5 E67F            AND     7FH             ;Clear bit 7
  10770.   02BB7 02              LD      (BC),A          ;Store character
  10771.   02BB8 03              INC     BC              ;Buffer pointer + 1
  10772.   02BB9 15              DEC     D               ;Counter - 1
  10773.                                                 ;Buffer full ?
  10774.   02BBA CAD828          JP      Z,28D8H         ;Yes: done, continue at 28D8H
  10775.  
  10776.   02BBD 7E              LD      A,(HL)          ;A = next character
  10777.   02BBE 23              INC     HL              ;Table pointer + 1
  10778.   02BBF B7              OR      A               ;Next keyword reached ?
  10779.   02BC0 F2B72B          JP      P,2BB7H         ;No: store character in buffer
  10780.  
  10781.   02BC3 E1              POP     HL              ;Yes: restore line pointer
  10782.   02BC4 18C6            JR      2B8CH           ;Process next character
  10783.  
  10784.  
  10785. ; DELETE statement
  10786. ; ----------------
  10787.  
  10788.   02BC6 CD101B          CALL    1B10H           ;Get start and end LN
  10789.   02BC9 D1              POP     DE              ;DE = LN of end line
  10790.   02BCA C5              PUSH    BC              ;Save pointer on start line
  10791.   02BCB C5              PUSH    BC              ;twice
  10792.   02BCC CD2C1B          CALL    1B2CH           ;Search end line
  10793.                                                 ;Does it exist?
  10794.   02BCF 3005            JR      NC,2BD6H        ;No: ?FC Error
  10795.                                                 ;Yes:
  10796.   02BD1 54              LD      D,H             ;DE = end line pointer
  10797.   02BD2 5D              LD      E,L
  10798.   02BD3 E3              EX      (SP),HL         ;Save pointer to end line
  10799.                                                 ;HL = pointer to start line
  10800.   02BD4 E5              PUSH    HL              ;Save it
  10801.   02BD5 DF              RST     18H             ;Compare both pointer
  10802.                                                 ;End >= start ?
  10803.   02BD6 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  10804.                                                 ;Yes:
  10805.   02BD9 212919          LD      HL,1929H        ;HL -> 'READY'
  10806.   02BDC CDA728          CALL    28A7H           ;Print text
  10807.   02BDF C1              POP     BC              ;Restore pointer to start line
  10808.   02BE0 21E81A          LD      HL,1AE8H        ;Set new RET address to 1AE8H
  10809.   02BE3 E3              EX      (SP),HL         ;and restore end line pointer
  10810.  
  10811. ; Delete line(s)
  10812. ;
  10813. ; I: BC -> start line
  10814. ;    HL -> line following end line
  10815. '
  10816.   02BE4 EB              EX      DE,HL           ;DE -> line following end
  10817.   02BE5 2AF940          LD      HL,(40F9H)      ;HL -> end of program
  10818.   02BE8 1A              LD      A,(DE)          ;Copy next line
  10819.   02BE9 02              LD      (BC),A          ;onto line to be deleted
  10820.   02BEA 03              INC     BC              ;Pointer + 1
  10821.   02BEB 13              INC     DE              ;Pointer + 1
  10822.   02BEC DF              RST     18H             ;End of program reached ?
  10823.   02BED 20F9            JR      NZ,2BE8H        ;No: continue copying
  10824.                                                 ;Yes:
  10825.   02BEF 60              LD      H,B             ;HL = new end address
  10826.   02BF0 69              LD      L,C             ;of program
  10827.   02BF1 22F940          LD      (40F9H),HL      ;Save it
  10828.   02BF4 C9              RET
  10829.  
  10830.  
  10831. ; CSAVE statement
  10832. ; ---------------
  10833.  
  10834.   02BF5 CD3723          CALL    2337H           ;Get filename
  10835.   02BF8 E5              PUSH    HL              ;Save PTP
  10836.   02BF9 CD132A          CALL    2A13H           ;DE -> filename
  10837.   02BFC F5              PUSH    AF              ;Save registers
  10838.   02BFD C5              PUSH    BC
  10839.   02BFE D5              PUSH    DE
  10840.   02BFF E5              PUSH    HL
  10841.   02C00 CD3F02          CALL    023FH           ;Write leader and sync
  10842.   02C03 E1              POP     HL              ;Restore registers
  10843.   02C04 D1              POP     DE
  10844.   02C05 C1              POP     BC
  10845.   02C06 F1              POP     AF
  10846.   02C07 1A              LD      A,(DE)          ;A = filename character
  10847.   02C08 CD1F02          CALL    021FH           ;Write to cassette
  10848.   02C0B 2AA440          LD      HL,(40A4H)      ;HL -> start of program
  10849.   02C0E EB              EX      DE,HL           ;DE = HL
  10850.   02C0F 2AF940          LD      HL,(40F9H)      ;HL -> end of program
  10851.  
  10852.   02C12 1A              LD      A,(DE)          ;A = byte from program text
  10853.   02C13 13              INC     DE              ;Program pointer + 1
  10854.   02C14 CD1F02          CALL    021FH           ;Write program byte to cassette
  10855.   02C17 DF              RST     18H             ;End of program reached ?
  10856.   02C18 20F8            JR      NZ,2C12H        ;No, write next byte
  10857.  
  10858.   02C1A 00              NOP                     ;--
  10859.   02C1B 00              NOP
  10860.   02C1C 00              NOP
  10861.   02C1D E1              POP     HL              ;Restore PTP
  10862.   02C1E C9              RET
  10863.  
  10864.  
  10865. ; CLOAD statement
  10866. ; ---------------
  10867.  
  10868.   02C1F 00              NOP                     ;--
  10869.   02C20 00              NOP
  10870.   02C21 00              NOP
  10871.   02C22 00              NOP
  10872.   02C23 00              NOP
  10873.   02C24 00              NOP
  10874.   02C25 00              NOP
  10875.   02C26 00              NOP
  10876.   02C27 AF              XOR     A               ;A = 00H
  10877.   02C28 012F23          LD      BC,232FH        ;--
  10878.  
  10879. ; Entry for VERIFY
  10880.  
  10881. * 02C29   2F            CPL                     ;A <> 00H
  10882. * 02C2A     23          INC     HL              ;PTP + 1
  10883.   02C2B F5              PUSH    AF              ;Save flag
  10884.   02C2C 2B              DEC     HL              ;PTP - 1
  10885.   02C2D D7              RST     10H             ;Filename indicated ?
  10886.   02C2E 3E00            LD      A,00H           ;A = default filename
  10887.   02C30 2807            JR      Z,2C39H         ;No: continue at 2C39H
  10888.                                                 ;Yes:
  10889.   02C32 CD3723          CALL    2337H           ;Put filename
  10890.   02C35 CD132A          CALL    2A13H           ;into A
  10891.   02C38 1A              LD      A,(DE)
  10892.   02C39 6F              LD      L,A             ;L = filename
  10893.   02C3A F1              POP     AF              ;Restore flag
  10894.   02C3B B7              OR      A               ;CLOAD ?
  10895.   02C3C 67              LD      H,A             ;H = flag
  10896.   02C3D 222141          LD      (4121H),HL      ;Save flag and filename
  10897.   02C40 CC4D1B          CALL    Z,1B4DH         ;Yes: NEW
  10898.   02C43 2A2141          LD      HL,(4121H)      ;Restore flag and filename
  10899.   02C46 EB              EX      DE,HL           ;D = flag, E = filename
  10900.   02C47 F5              PUSH    AF              ;Save registers
  10901.   02C48 C5              PUSH    BC
  10902.   02C49 D5              PUSH    DE
  10903.   02C4A E5              PUSH    HL
  10904.   02C4B CD4C02          CALL    024CH           ;Search for leader and sync
  10905.   02C4E E1              POP     HL              ;Restore registers
  10906.   02C4F D1              POP     DE
  10907.   02C50 C1              POP     BC
  10908.   02C51 F1              POP     AF
  10909.   02C52 CDED01          CALL    01EDH           ;Get filename char from tape
  10910.   02C55 1C              INC     E               ;Filename indicated ?
  10911.   02C56 1D              DEC     E
  10912.   02C57 2803            JR      Z,2C5CH         ;No: continue at 2C5CH
  10913.  
  10914.   02C59 BB              CP      E               ;Specified filename found ?
  10915.   02C5A 2037            JR      NZ,2C93H        ;No: search for next file
  10916.  
  10917.   02C5C 2AA440          LD      HL,(40A4H)      ;HL = program start address
  10918.   02C5F 0603            LD      B,03H           ;B = counter ( 3 times 00H
  10919.                                                 ;indicates program end)
  10920.   02C61 CDED01          CALL    01EDH           ;Read 1 byte
  10921.   02C64 5F              LD      E,A             ;E = byte
  10922.   02C65 96              SUB     (HL)            ;Does memory also contain this
  10923.                                                 ;byte ? Yes: A = 00H
  10924.   02C66 A2              AND     D               ;AND it with flag
  10925.                                                 ;A = 00H if CLOAD or VERIFY
  10926.                                                 ;is ok. A <> 00H if VERIFY is
  10927.                                                 ;bad.
  10928.                                                 ;Bad VERIFY ?
  10929.   02C67 2021            JR      NZ,2C8AH        ;Yes: continue at 2C8AH
  10930.  
  10931.   02C69 73              LD      (HL),E          ;Store byte
  10932.   02C6A CD6C19          CALL    196CH           ;Check free memory remaining.
  10933.   02C6D 7E              LD      A,(HL)          ;Byte read = 00H ?
  10934.   02C6E B7              OR      A
  10935.   02C6F 23              INC     HL              ;Pointer + 1
  10936.   02C70 20ED            JR      NZ,2C5FH        ;No: next byte
  10937.                                                 ;Yes:
  10938.   02C72 CDE401          CALL    01E4H           ;Blink '*'
  10939.   02C75 10EA            DJNZ    2C61H           ;Counter - 1, read next byte
  10940.  
  10941.   02C77 22F940          LD      (40F9H),HL      ;Store new end of program
  10942.   02C7A 212919          LD      HL,1929H        ;HL -> 'READY'
  10943.   02C7D CDA728          CALL    28A7H           ;Print text
  10944.   02C80 00              NOP                     ;--
  10945.   02C81 00              NOP
  10946.   02C82 00              NOP
  10947.   02C83 2AA440          LD      HL,(40A4H)      ;Reset PTP to start of BASIC
  10948.                                                 ;program
  10949.   02C86 E5              PUSH    HL              ;Save PTP
  10950.   02C87 C3E81A          JP      1AE8H           ;Renew all pointers in program
  10951.                                                 ;text, active command mode
  10952.  
  10953.  
  10954. ; Error at VERIFY
  10955.  
  10956.   02C8A 21A52C          LD      HL,2CA5H        ;HL -> 'BAD'
  10957.   02C8D CD7935          CALL    3579H           ;Print text and give tone
  10958.   02C90 C3181A          JP      1A18H           ;Back to active command mode
  10959.  
  10960.  
  10961. ; Filename not found
  10962.  
  10963.   02C93 322644          LD      (4426H),A       ;Print actual filename found
  10964.   02C96 0603            LD      B,03H           ;Seach for end of program
  10965.                                                 ;(3 times 00H)
  10966.   02C98 CDED01          CALL    01EDH           ;Read byte
  10967.   02C9B B7              OR      A               ;00H found ?
  10968.   02C9C 20F8            JR      NZ,2C96H        ;No: next byte
  10969.  
  10970.   02C9E 10F8            DJNZ    2C98H           ;Counter - 1: read next byte
  10971.  
  10972.   02CA0 00              NOP                     ;--
  10973.   02CA1 00              NOP
  10974.   02CA2 00              NOP
  10975.   02CA3 18A2            JR      2C47H           ;Retry CLOAD
  10976.  
  10977.  
  10978. ; Text 'BAD'
  10979.  
  10980.   02CA5 424144          DEFB    'BAD'
  10981.   02CA8 0D              DEFB    0DH
  10982.   02CA9 00              DEFB    00H             ;End of text
  10983.  
  10984.  
  10985. ; X = PEEK ( X )
  10986. ; --------------
  10987.  
  10988.   02CAA CD7F0A          CALL    0A7FH           ;HL = X = CINT (X) = address
  10989.   02CAD 7E              LD      A,(HL)          ;A = memory value
  10990.   02CAE C3F827          JP      27F8H           ;Save A to X as INT
  10991.  
  10992.  
  10993. ; POKE statement
  10994. ; --------------
  10995.  
  10996.   02CB1 CD022B          CALL    2B02H           ;DE = address
  10997.   02CB4 D5              PUSH    DE              ;Save address
  10998.   02CB5 CF              RST     08H             ;Next byte must be
  10999.   02CB6 2C              DEFB    ','             ;a comma
  11000.   02CB7 CD1C2B          CALL    2B1CH           ;Get poke value
  11001.   02CBA D1              POP     DE              ;Get address
  11002.   02CBB 12              LD      (DE),A          ;Store value in memory
  11003.   02CBC C9              RET
  11004.  
  11005.  
  11006. ; PRINT USING
  11007. ; -----------
  11008.  
  11009.   02CBD CD3823          CALL    2338H           ;Get format string
  11010.   02CC0 CDF40A          CALL    0AF4H           ;?TM Error if no string found
  11011.   02CC3 CF              RST     08H             ;Next character must
  11012.   02CC4 3B              DEFB    ';'             ;be ';'
  11013.   02CC5 EB              EX      DE,HL           ;DE = PTP
  11014.   02CC6 2A2141          LD      HL,(4121H)      ;HL -> string vector
  11015.   02CC9 1808            JR      2CD3H           ;Continue at 2CD3H
  11016.  
  11017. ; Re-entry in case multiple nuerical values have to be printed using the same
  11018. ; format string
  11019.  
  11020.   02CCB 3ADE40          LD      A,(40DEH)       ;A = character following
  11021.                                                 ;separator
  11022.   02CCE B7              OR      A               ;Separator followed by a
  11023.                                                 ;variable ?
  11024.   02CCF 280C            JR      Z,2CDDH         ;No: ?FC Error
  11025.                                                 ;Yes:
  11026.   02CD1 D1              POP     DE              ;Restore vector address of
  11027.                                                 ;format string
  11028.   02CD2 EB              EX      DE,HL           ;HL -> vector, DE = PTP
  11029.  
  11030. ; Execute PRINT USING
  11031. ;
  11032. ; I: DE =  PTP on variable to be printed
  11033. ;    HL -> format string vector
  11034.  
  11035.   02CD3 E5              PUSH    HL              ;Save vector address
  11036.   02CD4 AF              XOR     A               ;A = 0
  11037.   02CD5 32DE40          LD      (40DEH),A       ;Clear character
  11038.   02CD8 BA              CP      D               ;C-flag = 1, Z-flag = 0
  11039.   02CD9 F5              PUSH    AF              ;Save AF
  11040.   02CDA D5              PUSH    DE              ;Save PTP
  11041.   02CDB 46              LD      B,(HL)          ;B = format string length
  11042.   02CDC B0              OR      B               ;Null string ?
  11043.   02CDD CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  11044.                                                 ;No:
  11045.   02CE0 23              INC     HL              ;Vector address + 1
  11046.   02CE1 4E              LD      C,(HL)          ;HL = string pointer
  11047.   02CE2 23              INC     HL
  11048.   02CE3 66              LD      H,(HL)
  11049.   02CE4 69              LD      L,C
  11050.   02CE5 181C            JR      2D03H           ;Continue at 2D03H
  11051.  
  11052. ; '%' found
  11053. ;
  11054. ; I: B  = remaining string length
  11055. ;    HL = pointer on string (on next character following '%')
  11056. ; O: C  = number of spaces + 2 found between both '%' characters
  11057.  
  11058.   02CE7 58              LD      E,B             ;Save counter
  11059.   02CE8 E5              PUSH    HL              ;Save pointer
  11060.   02CE9 0E02            LD      C,02H           ;C = counter for spaces
  11061.                                                 ;(2 for the 2 '%' delimiters)
  11062.   02CEB 7E              LD      A,(HL)          ;Get next character
  11063.   02CEC 23              INC     HL              ;Pointer + 1
  11064.   02CED FE25            CP      '%'             ;2nd '%' found ?
  11065.   02CEF CA172E          JP      Z,2E17H         ;Yes: done, continue at 2E17H
  11066.                                                 ;No:
  11067.   02CF2 FE20            CP      20H             ;Space?
  11068.   02CF4 2003            JR      NZ,2CF9H        ;No: use '%' as text character
  11069.                                                 ;(only spaces are allowed
  11070.                                                 ;between '%' delimiters)
  11071.   02CF6 0C              INC     C               ;Counter + 1
  11072.   02CF7 10F2            DJNZ    2CEBH           ;Get next character
  11073.  
  11074.   02CF9 E1              POP     HL              ;Restore string pointer in case
  11075.                                                 ;no 2nd '%' was found
  11076.   02CFA 43              LD      B,E             ;Restore previous counter value
  11077.   02CFB 3E25            LD      A,25H           ;and use '%' as text character
  11078.  
  11079. ; The character found is not a formatting character but a part of a text to be
  11080. ; printed
  11081.  
  11082.   02CFD CD492E          CALL    2E49H           ;If D <> 0 then print '+'
  11083.   02D00 CD2A03          CALL    032AH           ;Print character
  11084.  
  11085. ; Process format string
  11086. ;
  11087. ; I: (SP - 4) -> format string vector
  11088. ;    (SP - 2) =  AF (A = 00H, C-flag = 1, Z-flag = 0. see 2CD9H)
  11089. ;    (SP)     =  PTP
  11090. ;     B       =  string length (remaining length)
  11091. ;     HL      =  string pointer
  11092.  
  11093.   02D03 AF              XOR     A               ;A = 00H
  11094.   02D04 5F              LD      E,A             ;E = number of positions before
  11095.                                                 ;the decimal point = 0
  11096.   02D05 57              LD      D,A             ;Format byte = 0
  11097.                                                 ;(For composition of format
  11098.                                                 ;byte see 0FBEH)
  11099.   02D06 CD492E          CALL    2E49H           ;If D <> 0 then print '+'
  11100.   02D09 57              LD      D,A             ;D = format byte
  11101.   02D0A 7E              LD      A,(HL)          ;Get next character
  11102.   02D0B 23              INC     HL              ;Pointer + 1
  11103.   02D0C FE21            CP      '!'             ;'!' found ?
  11104.   02D0E CA142E          JP      Z,2E14H         ;Yes: continue at 2E14H
  11105.  
  11106.   02D11 FE23            CP      '#'             ;'#' found ?
  11107.   02D13 2837            JR      Z,2D4CH         ;Yes: continue at 2D4CH
  11108.  
  11109.   02D15 05              DEC     B               ;Counter - 1
  11110.                                                 ;String end reached ?
  11111.   02D16 CAFE2D          JP      Z,2DFEH         ;Yes: continue at 2DFEH
  11112.  
  11113.   02D19 FE2B            CP      '+'             ;'+' found ?
  11114.   02D1B 3E08            LD      A,08H           ;Set bit 3 of format byte
  11115.   02D1D 28E7            JR      Z,2D06H         ;Yes: continue at 2D06H
  11116.  
  11117.   02D1F 2B              DEC     HL              ;Pointer - 1
  11118.   02D20 7E              LD      A,(HL)          ;Get character again
  11119.   02D21 23              INC     HL              ;Pointer + 1
  11120.   02D22 FE2E            CP      '.'             ;'.' found ?
  11121.   02D24 2840            JR      Z,2D66H         ;Yes: continue at 2D66H
  11122.  
  11123.   02D26 FE25            CP      '%'             ;'%' found ?
  11124.   02D28 28BD            JR      Z,2CE7H         ;Yes: continue at 2CE7H
  11125.  
  11126.   02D2A BE              CP      (HL)            ;Same character twice ?
  11127.   02D2B 20D0            JR      NZ,2CFDH        ;No: character not recognized
  11128.                                                 ;Yes:
  11129.   02D2D FE24            CP      '$'             ;'$$' found ?
  11130.   02D2F 2814            JR      Z,2D45H         ;Yes: continue at 2D45H
  11131.  
  11132.   02D31 FE2A            CP      '*'             ;'**' found ?
  11133.   02D33 20C8            JR      NZ,2CFDH        ;No: character not recognized
  11134.  
  11135. ; '**' found
  11136.  
  11137.   02D35 78              LD      A,B             ;A = remaining length
  11138.   02D36 FE02            CP      02H             ;Less than 2 bytes remaining?
  11139.   02D38 23              INC     HL              ;Pointer + 1
  11140.   02D39 3803            JR      C,2D3EH         ;Yes: continue at 2D3EH
  11141.  
  11142.   02D3B 7E              LD      A,(HL)          ;Get next character
  11143.   02D3C FE24            CP      '$'             ;Is it a '$' ?
  11144.   02D3E 3E20            LD      A,20H           ;Set bit 5 (for '*' output)
  11145.   02D40 2007            JR      NZ,2D49H        ;No: only '**' found
  11146.                                                 ;Yes:
  11147.   02D42 05              DEC     B               ;'**$' found, counter - 1
  11148.   02D43 1C              INC     E               ;Positions before decimal
  11149.                                                 ;point + 1
  11150.   02D44 FEAF            CP      AFH             ;--
  11151.  
  11152. ; '$$' found
  11153.  
  11154. * 02D45   AF            XOR     A               ;A = 0
  11155.   02D46 C610            ADD     A,10H           ;Set bit 4 ('$' in front)
  11156.   02D48 23              INC     HL              ;Pointer + 1
  11157.   02D49 1C              INC     E               ;Positions before decimal
  11158.                                                 ;point + 1
  11159.   02D4A 82              ADD     A,D             ;update A with format byte
  11160.   02D4B 57              LD      D,A             ;D = format byte
  11161.  
  11162. ; '#' found
  11163.  
  11164.   02D4C 1C              INC     E               ;Positions before decimal
  11165.                                                 ;point + 1
  11166.   02D4D 0E00            LD      C,00H           ;Clear counter for text output
  11167.   02D4F 05              DEC     B               ;String counter - 1
  11168.                                                 ;Last character ?
  11169.   02D50 2847            JR      Z,2D99H         ;Yes: continue at 2D99H
  11170.  
  11171.   02D52 7E              LD      A,(HL)          ;Get next character
  11172.   02D53 23              INC     HL              ;Pointer + 1
  11173.   02D54 FE2E            CP      '.'             ;Decimal point found ?
  11174.   02D56 2818            JR      Z,2D70H         ;Yes: continue at 2D70H
  11175.  
  11176.   02D58 FE23            CP      '#'             ;'#' found ?
  11177.   02D5A 28F0            JR      Z,2D4CH         ;Yes: back to 2D4CH
  11178.  
  11179.   02D5C FE2C            CP      ','             ;',' found ?
  11180.   02D5E 201A            JR      NZ,2D7AH        ;No: continue at 2D7AH
  11181.  
  11182. ; '.' found
  11183.  
  11184.   02D60 7A              LD      A,D             ;Set bit 6 of format byte
  11185.   02D61 F640            OR      40H
  11186.   02D63 57              LD      D,A             ;D = format byte
  11187.   02D64 18E6            JR      2D4CH           ;Get next character
  11188.  
  11189. ; '.' in front of '#' found
  11190.  
  11191.   02D66 7E              LD      A,(HL)          ;A = next character
  11192.   02D67 FE23            CP      '#'             ;Next character a '#' ?
  11193.   02D69 3E2E            LD      A,2EH           ;A = '.'
  11194.   02D6B 2090            JR      NZ,2CFDH        ;No: use '.' as text character
  11195.                                                 ;Yes:
  11196.   02D6D 0E01            LD      C,01H           ;Set counter for positions
  11197.                                                 ;behind the decimal point to 1
  11198.   02D6F 23              INC     HL              ;Pointer + 1
  11199.  
  11200. ; '.' behind '#' found
  11201.  
  11202.   02D70 0C              INC     C               ;Positions behind decimal
  11203.                                                 ;point + 1
  11204.   02D71 05              DEC     B               ;Counter - 1
  11205.                                                 ;String end ?
  11206.   02D72 2825            JR      Z,2D99H         ;Yes: continue at 2D99H
  11207.  
  11208.   02D74 7E              LD      A,(HL)          ;Get next character
  11209.   02D75 23              INC     HL              ;Pointer + 1
  11210.   02D76 FE23            CP      '#'             ;'#' ?
  11211.   02D78 28F6            JR      Z,2D70H         ;Yes: Positions behind decimal
  11212.                                                 ;point + 1
  11213. ; Exponential format specified ?
  11214. ; (4 consecutive times arrow up)
  11215.  
  11216.   02D7A D5              PUSH    DE              ;Save format byte and counter
  11217.   02D7B 11972D          LD      DE,2D97H        ;Set new RET address
  11218.   02D7E D5              PUSH    DE              ;to 2D97H
  11219.   02D7F 54              LD      D,H             ;DE = string pointer
  11220.   02D80 5D              LD      E,L
  11221.   02D81 FE5B            CP      '['             ;Arrow up found ?
  11222.   02D83 C0              RET     NZ              ;No: RET to 2D79H
  11223.  
  11224.   02D84 BE              CP      (HL)            ;2 times arrow up ?
  11225.   02D85 C0              RET     NZ              ;No: RET to 2D79H
  11226.  
  11227.   02D86 23              INC     HL              ;Pointer + 1
  11228.   02D87 BE              CP      (HL)            ;3 times arrow up ?
  11229.   02D88 C0              RET     NZ              ;No: RET to 2D79H
  11230.  
  11231.   02D89 23              INC     HL              ;Pointer + 1
  11232.   02D8A BE              CP      (HL)            ;4 times arrow up ?
  11233.   02D8B C0              RET     NZ              ;No: RET to 2D79H
  11234.  
  11235.   02D8C 23              INC     HL              ;Pointer + 1
  11236.   02D8D 78              LD      A,B             ;A = string counter
  11237.   02D8E D604            SUB     04H             ;Subtract 4, overflow ?
  11238.   02D90 D8              RET     C               ;Yes: the 4 arrow ups are not
  11239.                                                 ;all part of the format string
  11240.  
  11241. ; 4 consecutive arrow ups found: print number in exponential format
  11242.  
  11243.   02D91 D1              POP     DE              ;Remove RET address (2D97H)
  11244.   02D92 D1              POP     DE              ;Restore format byte and
  11245.                                                 ;number of positions before
  11246.                                                 ;the decimal point
  11247.   02D93 47              LD      B,A             ;B = string counter
  11248.   02D94 14              INC     D               ;Set bit 0 (exponential format)
  11249.   02D95 23              INC     HL              ;Pointer + 1
  11250.   02D96 CAEBD1          JP      Z,0D1EBH        ;Jump is never executed because
  11251.                                                 ;Z-flag <> 0 (because of 2D94H)
  11252. ; No exponential format output
  11253.  
  11254. * 02D97   EB            EX      DE,HL           ;HL = old string pointer
  11255. * 02D98     D1          POP     DE              ;Restore format byte and number
  11256.                                                 ;positions before decimal point
  11257. ; Last string character was '.' or '#'
  11258.  
  11259.   02D99 7A              LD      A,D             ;A = format byte
  11260.   02D9A 2B              DEC     HL              ;Pointer - 1
  11261.   02D9B 1C              INC     E               ;Positions before decimal
  11262.                                                 ;point + 1
  11263.   02D9C E608            AND     08H             ;Print positive sign ?
  11264.   02D9E 2015            JR      NZ,2DB5H        ;Yes: continue at 2DB5H
  11265.                                                 ;No:
  11266.   02DA0 1D              DEC     E               ;Positions before decimal
  11267.                                                 ;point - 1
  11268.   02DA1 78              LD      A,B             ;Any string characters left ?
  11269.   02DA2 B7              OR      A
  11270.   02DA3 2810            JR      Z,2DB5H         ;No: continue at 2DB5H
  11271.  
  11272.   02DA5 7E              LD      A,(HL)          ;A = next character
  11273.   02DA6 D62D            SUB     2DH             ;'-' found ?
  11274.   02DA8 2806            JR      Z,2DB0H         ;Yes: continue at 2DB0H
  11275.  
  11276.   02DAA FEFE            CP      0FEH            ;'+' found ?
  11277.   02DAC 2007            JR      NZ,2DB5H        ;No: continue at 2DB5H
  11278.  
  11279.   02DAE 3E08            LD      A,08H           ;Set bit 3 (print sign)
  11280.   02DB0 C604            ADD     A,04H           ;Set bit 2 (behind number)
  11281.   02DB2 82              ADD     A,D             ;Update with format byte
  11282.   02DB3 57              LD      D,A             ;D = format byte
  11283.   02DB4 05              DEC     B               ;String counter - 1
  11284.  
  11285. ; Processing of format string ready
  11286. ;
  11287. ; I: B = string counter
  11288. ;    C = number of positions behind decimal point + 1 (for decimal point)
  11289. ;    D = format byte
  11290. ;    E = number of positions in front of decimal point
  11291.  
  11292.   02DB5 E1              POP     HL              ;Restore PTP
  11293.   02DB6 F1              POP     AF              ;Restore flags (see 2CD9H)
  11294.   02DB7 2850            JR      Z,2E09H         ;Done when Z-flag = 0
  11295.  
  11296.   02DB9 C5              PUSH    BC              ;Save registers
  11297.   02DBA D5              PUSH    DE
  11298.   02DBB CD3723          CALL    2337H           ;Get value to be printed
  11299.   02DBE D1              POP     DE              ;Restore registers
  11300.   02DBF C1              POP     BC
  11301.   02DC0 C5              PUSH    BC              ;Save string counter
  11302.   02DC1 E5              PUSH    HL              ;Save PTP
  11303.   02DC2 43              LD      B,E             ;B = number of positions in
  11304.                                                 ;front of decimal point
  11305.   02DC3 78              LD      A,B             ;A = B
  11306.   02DC4 81              ADD     A,C             ;Add number of positions behind
  11307.                                                 ;the decimal point
  11308.   02DC5 FE19            CP      19H             ;More than 24 positions ?
  11309.   02DC7 D24A1E          JP      NC,1E4AH        ;Yes: ?FC Error, the maximum
  11310.                                                 ;number of positions is 24
  11311.                                                 ;( 1 for sign,
  11312.                                                 ; 17 for DBL format
  11313.                                                 ;  1 for decimal point
  11314.                                                 ;  4 for exponent
  11315.                                                 ;  1 for sign following number)
  11316.   02DCA 7A              LD      A,D             ;A = format byte
  11317.   02DCB F680            OR      80H             ;Execute formatting
  11318.                                                 ;(bit 7 set ?)
  11319.   02DCD CDBE0F          CALL    0FBEH           ;Store number in X as formatted
  11320.                                                 ;string at 4130H. HL -> string
  11321.   02DD0 CDA728          CALL    28A7H           ;Print string
  11322.   02DD3 E1              POP     HL              ;Restore PTP
  11323.   02DD4 2B              DEC     HL              ;Adjust PTP
  11324.   02DD5 D7              RST     10H             ;Get next character
  11325.   02DD6 37              SCF                     ;C-flag = 1
  11326.                                                 ;End of assignment reached ?
  11327.   02DD7 280D            JR      Z,2DE6H         ;Yes: continue at 2DE6H
  11328.  
  11329.   02DD9 32DE40          LD      (40DEH),A       ;Store next character
  11330.                                                 ;(see 2CCBH)
  11331.   02DDC FE3B            CP      3BH             ;';' found ?
  11332.   02DDE 2805            JR      Z,2DE5H         ;Yes: character ok
  11333.  
  11334.   02DE0 FE2C            CP      2CH             ;',' found ?
  11335.   02DE2 C29719          JP      NZ,1997H        ;No: ?SN Error
  11336.  
  11337.   02DE5 D7              RST     10H             ;Adjust PTP
  11338.   02DE6 C1              POP     BC              ;Restore string counter
  11339.   02DE7 EB              EX      DE,HL           ;DE = PTP
  11340.   02DE8 E1              POP     HL              ;HL -> format string vector
  11341.   02DE9 E5              PUSH    HL              ;Save vector address
  11342.   02DEA F5              PUSH    AF              ;Save next character
  11343.   02DEB D5              PUSH    DE              ;Save PTP
  11344.   02DEC 7E              LD      A,(HL)          ;A = format string length
  11345.   02DED 90              SUB     B               ;Subtract remaining length
  11346.   02DEE 23              INC     HL              ;Vector address + 1
  11347.   02DEF 4E              LD      C,(HL)          ;HL -> format string
  11348.   02DF0 23              INC     HL
  11349.   02DF1 66              LD      H,(HL)
  11350.   02DF2 69              LD      L,C
  11351.   02DF3 1600            LD      D,00H           ;DE = offset for next format
  11352.   02DF5 5F              LD      E,A             ;string (format character
  11353.                                                 ;for next value to print)
  11354.   02DF6 19              ADD     HL,DE           ;Add offset. HL now points
  11355.                                                 ;to next format indication
  11356.   02DF7 78              LD      A,B             ;A = remaining length
  11357.   02DF8 B7              OR      A               ;Length = 0 ?
  11358.   02DF9 C2032D          JP      NZ,2D03H        ;No: process format string
  11359.                                                 ;from (HL) onwards
  11360.   02DFC 1806            JR      2E04H           ;Yes: use old format string
  11361.                                                 ;also for the next value
  11362. ; End of string found
  11363.  
  11364.   02DFE CD492E          CALL    2E49H           ;If D <> 0 then print '+'
  11365.   02E01 CD2A03          CALL    032AH           ;Print last character
  11366.   02E04 E1              POP     HL              ;Restore PTP
  11367.   02E05 F1              POP     AF              ;Restore flags/next character
  11368.                                                 ;(see 2DEAH)
  11369.   02E06 C2CB2C          JP      NZ,2CCBH        ;Process next number with same
  11370.                                                 ;format string
  11371.   02E09 DCFE20          CALL    C,20FEH         ;End PRINT if end of assigment
  11372.                                                 ;is reached (see 2DD6H)
  11373.   02E0C E3              EX      (SP),HL         ;Save PTP, restore vector
  11374.                                                 ;address
  11375.   02E0D CDDD29          CALL    29DDH           ;Remove format string from
  11376.                                                 ;string table and string space
  11377.   02E10 E1              POP     HL              ;Restore PTP
  11378.   02E11 C36921          JP      2169H           ;Output back to screen
  11379.  
  11380. ; '!' found
  11381.  
  11382.   02E14 0E01            LD      C,01H           ;Print 1 text character
  11383.   02E16 3EF1            LD      A,0F1H          ;--
  11384.  
  11385. ; 2nd '%' character found
  11386. ; C = number of characters to be printer
  11387.  
  11388. * 02E17   F1            POP     AF              ;Remove string pointer from
  11389.                                                 ;stack
  11390.   02E18 05              DEC     B               ;Counter - 1
  11391.   02E19 CD492E          CALL    2E49H           ;If D <> 0 then print '+'
  11392.   02E1C E1              POP     HL              ;Restore PTP
  11393.   02E1D F1              POP     AF              ;Restore flags: done ?
  11394.   02E1E 28E9            JR      Z,2E09H         ;Yes: end PRINT operation
  11395.                                                 ;No:
  11396.   02E20 C5              PUSH    BC              ;Save string counter
  11397.   02E21 CD3723          CALL    2337H           ;Get string to be printed
  11398.   02E24 CDF40A          CALL    0AF4H           ;?TM Error if not a string
  11399.   02E27 C1              POP     BC              ;Restore string counter
  11400.   02E28 C5              PUSH    BC              ;Save string counter
  11401.   02E29 E5              PUSH    HL              ;Save PTP
  11402.   02E2A 2A2141          LD      HL,(4121H)      ;HL -> vector of string to be
  11403.                                                 ;printed
  11404.   02E2D 41              LD      B,C             ;B = number of characters to
  11405.                                                 ;be printed
  11406.   02E2E 0E00            LD      C,00H           ;C = 0: ouput is started at
  11407.                                                 ;start of string
  11408.   02E30 C5              PUSH    BC              ;Save counters
  11409.   02E31 CD682A          CALL    2A68H           ;Get characters to be printed
  11410.                                                 ;via LEFT$
  11411.   02E34 CDAA28          CALL    28AAH           ;Print characters
  11412.   02E37 2A2141          LD      HL,(4121H)      ;HL -> vector of printed string
  11413.   02E3A F1              POP     AF              ;A = number of printed chars
  11414.   02E3B 96              SUB     (HL)            ;- total string length
  11415.   02E3C 47              LD      B,A             ;B = remaining string length
  11416.   02E3D 3E20            LD      A,20H           ;A = space
  11417.   02E3F 04              INC     B               ;Counter + 1
  11418.   02E40 05              DEC     B               ;Counter reached 0 ?
  11419.   02E41 CAD32D          JP      Z,2DD3H         ;Yes: process next argument
  11420.                                                 ;No:
  11421.   02E44 CD2A03          CALL    032AH           ;Replace missing characters
  11422.                                                 ;by spaces
  11423.   02E47 18F7            JR      2E40H           ;next character
  11424.  
  11425.  
  11426. ; If D <> 0 then print '+'
  11427.  
  11428.   02E49 F5              PUSH    AF              ;Save AF
  11429.   02E4A 7A              LD      A,D             ;D <> 0 ?
  11430.   02E4B B7              OR      A
  11431.   02E4C 3E2B            LD      A,2BH           ;A = '+'
  11432.   02E4E C42A03          CALL    NZ,032AH        ;Yes: print '+'
  11433.   02E51 F1              POP     AF              ;Restore AF
  11434.   02E52 C9              RET
  11435.  
  11436.  
  11437. ; Entry for EDIT after ?SN Error
  11438.  
  11439.   02E53 329A40          LD      (409AH),A       ;Clear last error code
  11440.   02E56 2AEA40          LD      HL,(40EAH)      ;HL = ERL
  11441.   02E59 B4              OR      H               ;ERL = 65535 ?
  11442.   02E5A A5              AND     L               ;(Syntax error in active
  11443.   02E5B 3C              INC     A               ;command mmode)
  11444.   02E5C EB              EX      DE,HL           ;DE = ERL
  11445.   02E5D C8              RET     Z               ;Yes: no EDIT has to be done
  11446.                                                 ;No:
  11447.   02E5E 1804            JR      2E64H           ;Call EDIT with LN = DE
  11448.  
  11449.  
  11450. ; EDIT statement
  11451. ; --------------
  11452.  
  11453.   02E60 CD4F1E          CALL    1E4FH           ;Load line number into DE
  11454.   02E63 C0              RET     NZ              ;?SN Error ?
  11455.  
  11456.   02E64 E1              POP     HL              ;Remove RET address from stack
  11457.   02E65 EB              EX      DE,HL           ;HL = line number
  11458.   02E66 22EC40          LD      (40ECH),HL      ;Save as '.'
  11459.   02E69 EB              EX      DE,HL           ;DE = line number
  11460.   02E6A CD2C1B          CALL    1B2CH           ;Search for line DE
  11461.   02E6D D2D91E          JP      NC,1ED9H        ;?UL Error in case the line
  11462.                                                 ;does not exist
  11463.   02E70 60              LD      H,B             ;HL = line pointer
  11464.   02E71 69              LD      L,C
  11465.   02E72 23              INC     HL              ;HL + 2
  11466.   02E73 23              INC     HL              ;BC = line number
  11467.   02E74 4E              LD      C,(HL)
  11468.   02E75 23              INC     HL
  11469.   02E76 46              LD      B,(HL)
  11470.   02E77 23              INC     HL
  11471.   02E78 C5              PUSH    BC              ;Save LN
  11472.   02E79 CD7E2B          CALL    2B7EH           ;Decode line from (HL) onwards
  11473.                                                 ;and store in line buffer
  11474.   02E7C E1              POP     HL              ;Restore LN
  11475.   02E7D E5              PUSH    HL              ;And save it again
  11476.   02E7E CDAF0F          CALL    0FAFH           ;Print HL as decimal number
  11477.   02E81 3E20            LD      A,20H           ;A = ' '
  11478.   02E83 CD2A03          CALL    032AH           ;Print it
  11479.   02E86 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  11480.   02E89 3E0E            LD      A,0EH           ;Cursor on
  11481.   02E8B CD2A03          CALL    032AH
  11482.   02E8E E5              PUSH    HL              ;Save pointer
  11483.   02E8F 0EFF            LD      C,0FFH          ;C = counter (set to -1 because
  11484.                                                 ;of following INC C)
  11485.   02E91 0C              INC     C               ;Counter + 1
  11486.   02E92 7E              LD      A,(HL)          ;A = next characater
  11487.   02E93 B7              OR      A               ;End of line reached
  11488.   02E94 23              INC     HL              ;Pointer + 1
  11489.   02E95 20FA            JR      NZ,2E91H        ;No: continue counting
  11490.                                                 ;Yes:
  11491.   02E97 E1              POP     HL              ;Restore pointer
  11492.   02E98 47              LD      B,A             ;B = 00H (number of already
  11493.                                                 ;printed characaters)
  11494.                                                 ;C = length of line
  11495.   02E99 1600            LD      D,00H           ;D = 0 (repetition counter)
  11496.   02E9B CD8403          CALL    0384H           ;Get character from keyboard
  11497.   02E9E D630            SUB     30H             ;Can is be a digit ?
  11498.   02EA0 380E            JR      C,2EB0H         ;No: continue at 2EB0H
  11499.                                                 ;Yes:
  11500.   02EA2 FE0A            CP      0AH             ;Digit ?
  11501.   02EA4 300A            JR      NC,2EB0H        ;No: continue at 2EB0H
  11502.                                                 ;Yes:
  11503.   02EA6 5F              LD      E,A             ;E = number value (00H-09H)
  11504.   02EA7 7A              LD      A,D             ;A = last value
  11505.   02EA8 07              RLCA                    ;*2
  11506.   02EA9 07              RLCA                    ;*2 (*4 in total)
  11507.   02EAA 82              ADD     A,D             ;+value (*5 in total)
  11508.   02EAB 07              RLCA                    ;*2 (*10 in total)
  11509.   02EAC 83              ADD     A,E             ;Add next decimal position
  11510.   02EAD 57              LD      D,A             ;D = counter
  11511.   02EAE 18EB            JR      2E9BH           ;Get next character
  11512.  
  11513. ; No digit entered
  11514. ; D = repetition counter
  11515. ; A = ASCII code of entered character - 30H (!)
  11516.  
  11517.   02EB0 E5              PUSH    HL              ;Save pointer
  11518.   02EB1 21992E          LD      HL,2E99H        ;Set new RET address
  11519.   02EB4 E3              EX      (SP),HL         ;to 2E99H and restore pointer
  11520.   02EB5 15              DEC     D               ;Repetition required ?
  11521.   02EB6 14              INC     D               ;Is D <> 0 ?
  11522.   02EB7 C2BB2E          JP      NZ,2EBBH        ;Yes: leave D as it is
  11523.                                                 ;No:
  11524.   02EBA 14              INC     D               ;Set D to 1 (execute required
  11525.                                                 ;function at least once)
  11526.   02EBB FED8            CP      0D8H            ;08H = Backspace ?
  11527.   02EBD CAD22F          JP      Z,2FD2H         ;Yes: continue at 2FD2H
  11528.  
  11529.   02EC0 FEDD            CP      0DDH            ;0DH = RETURN ?
  11530.   02EC2 CAE02F          JP      Z,2FE0H         ;Yes: continue at 2FE0H
  11531.  
  11532.   02EC5 FEF0            CP      0F0H            ;20H = space ?
  11533.   02EC7 2841            JR      Z,2F0AH         ;Yes: continue at 2F0AH
  11534.  
  11535.   02EC9 FE31            CP      31H             ;Lower case (char > 60H) ?
  11536.   02ECB 3802            JR      C,2ECFH         ;No: character ok.
  11537.                                                 ;Yes:
  11538.   02ECD D620            SUB     20H             ;Convert to upper case
  11539.   02ECF FE21            CP      21H             ;51H = 'Q' ?
  11540.   02ED1 CAF62F          JP      Z,2FF6H         ;Yes: continue at 2FF6H
  11541.  
  11542.   02ED4 FE1C            CP      1CH             ;4CH = 'L' ?
  11543.   02ED6 CA402F          JP      Z,2F40H         ;Yes: continue at 2F40H
  11544.  
  11545.   02ED9 FE23            CP      23H             ;53H = 'S' ?
  11546.   02EDB 283F            JR      Z,2F1CH         ;Yes: continue at 2F1CH
  11547.  
  11548.   02EDD FE19            CP      19H             ;49H = 'I' ?
  11549.   02EDF CA7D2F          JP      Z,2F7DH         ;Yes: continue at 2F7DH
  11550.  
  11551.   02EE2 FE14            CP      14H             ;44H = 'D' ?
  11552.   02EE4 CA4A2F          JP      Z,2F4AH         ;Yes: continue at 2F4AH
  11553.  
  11554.   02EE7 FE13            CP      13H             ;43H = 'C' ?
  11555.   02EE9 CA652F          JP      Z,2F65H         ;Yes: continue at 2F65H
  11556.  
  11557.   02EEC FE15            CP      15H             ;45H = 'E' ?
  11558.   02EEE CAE32F          JP      Z,2FE3H         ;Yes: continue at 2FE3H
  11559.  
  11560.   02EF1 FE28            CP      28H             ;58H = 'X' ?
  11561.   02EF3 CA782F          JP      Z,2F78H         ;Yes: continue at 2F78H
  11562.  
  11563.   02EF6 FE1B            CP      1BH             ;4BH = 'K' ?
  11564.   02EF8 281C            JR      Z,2F16H         ;Yes: continue at 2F16H
  11565.  
  11566.   02EFA FE18            CP      18H             ;48H = 'H' ?
  11567.   02EFC CA752F          JP      Z,2F75H         ;Yes: continue at 2F75H
  11568.  
  11569.   02EFF FE11            CP      11H             ;41H = 'A' ?
  11570.   02F01 C0              RET     NZ              ;No: back to 2E99H
  11571.  
  11572. ; 'A': start again
  11573.  
  11574.   02F02 C1              POP     BC              ;Remove RET address
  11575.   02F03 D1              POP     DE              ;Restore LN
  11576.   02F04 CDFE20          CALL    20FEH           ;Start new line on screen
  11577.   02F07 C3652E          JP      2E65H           ;Restart EDIT
  11578.  
  11579. ; Space bar: print next character
  11580.  
  11581.   02F0A 7E              LD      A,(HL)          ;Get next character
  11582.   02F0B B7              OR      A               ;End of line reached ?
  11583.   02F0C C8              RET     Z               ;Yes: done, return
  11584.  
  11585.   02F0D 04              INC     B               ;Counter + 1
  11586.   02F0E CD2A03          CALL    032AH           ;Print character
  11587.   02F11 23              INC     HL              ;Pointer + 1
  11588.   02F12 15              DEC     D               ;Repeat ?
  11589.   02F13 20F5            JR      NZ,2F0AH        ;Yes: next character
  11590.  
  11591.   02F15 C9              RET
  11592.  
  11593. ; 'K': delete line up to character entered
  11594.  
  11595.   02F16 E5              PUSH    HL              ;Save pointer
  11596.   02F17 215F2F          LD      HL,2F5FH        ;Set new RET address to 2F5FH
  11597.                                                 ;(to print '!' at the end)
  11598.   02F1A E3              EX      (SP),HL         ;Restore pointer
  11599.   02F1B 37              SCF                     ;C-flag = 0 (indicator for
  11600.                                                 ;delete character)
  11601. ; 'S': search for character entered
  11602.  
  11603.   02F1C F5              PUSH    AF              ;Save flags
  11604.   02F1D CD8403          CALL    0384H           ;Get character
  11605.   02F20 5F              LD      E,A             ;E = character
  11606.   02F21 F1              POP     AF              ;Restore flags
  11607.   02F22 F5              PUSH    AF              ;And save them again
  11608.   02F23 DC5F2F          CALL    C,2F5FH         ;Print '!' for 'K' operation
  11609.   02F26 7E              LD      A,(HL)          ;A = next character
  11610.   02F27 B7              OR      A               ;End of line reached ?
  11611.   02F28 CA3E2F          JP      Z,2F3EH         ;Yes: done.
  11612.                                                 ;No:
  11613.   02F2B CD2A03          CALL    032AH           ;Print character
  11614.   02F2E F1              POP     AF              ;Restore flags
  11615.   02F2F F5              PUSH    AF              ;And save them again
  11616.                                                 ;'K' operation ?
  11617.   02F30 DCA12F          CALL    C,2FA1H         ;Yes: Delete character
  11618.   02F33 3802            JR      C,2F37H         ;Yes: Skip next operation
  11619.                                                 ;(counter and pointer are
  11620.                                                 ;adjusted at 2FA1H)
  11621.   02F35 23              INC     HL              ;Pointer + 1
  11622.   02F36 04              INC     B               ;Counter + 1
  11623.   02F37 7E              LD      A,(HL)          ;A = next character
  11624.   02F38 BB              CP      E               ;= searched character ?
  11625.   02F39 20EB            JR      NZ,2F26H        ;No: next character
  11626.                                                 ;Yes:
  11627.   02F3B 15              DEC     D               ;Repeat done ?
  11628.   02F3C 20E8            JR      NZ,2F26H        ;No: next character
  11629.                                                 ;Yes:
  11630.   02F3E F1              POP     AF              ;Restore flags
  11631.   02F3F C9              RET
  11632.  
  11633. ; 'L': list line and start again
  11634.  
  11635.   02F40 CD752B          CALL    2B75H           ;Print line
  11636.   02F43 CDFE20          CALL    20FEH           ;Start new line on screen
  11637.   02F46 C1              POP     BC              ;Remove RET address
  11638.   02F47 C37C2E          JP      2E7CH           ;Restart EDIT
  11639.  
  11640. ; 'D': Delete character
  11641.  
  11642.   02F4A 7E              LD      A,(HL)          ;A = character
  11643.   02F4B B7              OR      A               ;End of line reached?
  11644.   02F4C C8              RET     Z               ;Yes: done, return.
  11645.                                                 ;No:
  11646.   02F4D 3E21            LD      A,21H           ;A = '!'
  11647.   02F4F CD2A03          CALL    032AH           ;Print it
  11648.   02F52 7E              LD      A,(HL)          ;A = character
  11649.   02F53 B7              OR      A               ;End of line ?
  11650.   02F54 2809            JR      Z,2F5FH         ;Yes: done.
  11651.                                                 ;No:
  11652.   02F56 CD2A03          CALL    032AH           ;Print character
  11653.   02F59 CDA12F          CALL    2FA1H           ;and delete
  11654.   02F5C 15              DEC     D               ;Repeat ?
  11655.   02F5D 20F3            JR      NZ,2F52H        ;Yes: next character
  11656.  
  11657. ; Print '!'
  11658.  
  11659.   02F5F 3E21            LD      A,21H           ;A = '!'
  11660.   02F61 CD2A03          CALL    032AH           ;Print it
  11661.   02F64 C9              RET
  11662.  
  11663. ; 'C': change character
  11664.  
  11665.   02F65 7E              LD      A,(HL)          ;A = character
  11666.   02F66 B7              OR      A               ;End of line reached ?
  11667.   02F67 C8              RET     Z               ;Yes : done, return.
  11668.  
  11669.   02F68 CD8403          CALL    0384H           ;Get new character
  11670.   02F6B 77              LD      (HL),A          ;Use it
  11671.   02F6C CD2A03          CALL    032AH           ;And print it
  11672.   02F6F 23              INC     HL              ;Pointer + 1
  11673.   02F70 04              INC     B               ;Counter + 1
  11674.   02F71 15              DEC     D               ;Repeat ?
  11675.   02F72 20F1            JR      NZ,2F65H        ;Yes: next character
  11676.                                                 ;No:
  11677.   02F74 C9              RET                     ;Done.
  11678.  
  11679. ; 'H': Hack off rest of the line and jump to 'I'
  11680.  
  11681.   02F75 3600            LD      (HL),00H        ;Set end of line at current
  11682.                                                 ;position
  11683.   02F77 48              LD      C,B             ;C = number of already printed
  11684.                                                 ;characters = new line length
  11685. ; 'X': print rest of the line and jump to 'I'
  11686.  
  11687.   02F78 16FF            LD      D,0FFH          ;Execute 255 times the space
  11688.   02F7A CD0A2F          CALL    2F0AH           ;bar function to print the
  11689.                                                 ;remaining part of the line
  11690. ; 'I': insert new characters
  11691.  
  11692.   02F7D CD8403          CALL    0384H           ;Get new character
  11693.   02F80 B7              OR      A               ;(why ?)
  11694.   02F81 CA7D2F          JP      Z,2F7DH         ;(see 0384H)
  11695.  
  11696.   02F84 FE08            CP      08H             ;Arrow left ?
  11697.   02F86 280A            JR      Z,2F92H         ;Yes: delete character
  11698.  
  11699.   02F88 FE0D            CP      0DH             ;RETURN ?
  11700.   02F8A CAE02F          JP      Z,2FE0H         ;Yes: execute RETURN function
  11701.  
  11702.   02F8D FE1B            CP      1BH             ;SHIFT+arrow up ? (End I-func)
  11703.   02F8F C8              RET     Z               ;Yes: done, return
  11704.                                                 ;No:
  11705.   02F90 201E            JR      NZ,2FB0H        ;Insert character
  11706.  
  11707. ; Arrow left: Delete character in I-function
  11708.  
  11709.   02F92 3E08            LD      A,08H           ;A = ASCII code of delete
  11710.                                                 ;character = backspace
  11711.   02F94 05              DEC     B               ;Any character already printed?
  11712.   02F95 04              INC     B               ;(is there anything to delete)
  11713.   02F96 281F            JR      Z,2FB7H         ;No: done
  11714.                                                 ;Yes:
  11715.   02F98 CD2A03          CALL    032AH           ;Delete character from screen
  11716.   02F9B 2B              DEC     HL              ;Pointer - 1
  11717.   02F9C 05              DEC     B               ;Counter - 1
  11718.   02F9D 117D2F          LD      DE,2F7DH        ;Set new RET address
  11719.   02FA0 D5              PUSH    DE              ;to 2F7DH
  11720.  
  11721. ; Delete character at (HL)
  11722.  
  11723.   02FA1 E5              PUSH    HL              ;Save pointer
  11724.   02FA2 0D              DEC     C               ;Length of line - 1
  11725.   02FA3 7E              LD      A,(HL)          ;Get character
  11726.   02FA4 B7              OR      A               ;End of line reached ?
  11727.   02FA5 37              SCF                     ;C-flag = 1
  11728.   02FA6 CA9008          JP      Z,0890H         ;Yes: restore pointer and done
  11729.                                                 ;No:
  11730.   02FA9 23              INC     HL              ;Pointer + 1
  11731.   02FAA 7E              LD      A,(HL)          ;Get next character
  11732.   02FAB 2B              DEC     HL              ;pointer - 1
  11733.   02FAC 77              LD      (HL),A          ;And store at current position
  11734.   02FAD 23              INC     HL              ;Pointer + 1
  11735.   02FAE 18F3            JR      2FA3H           ;Shift line until end of line
  11736.                                                 ;is reached
  11737. ; Insert character  at (HL)
  11738.  
  11739.   02FB0 F5              PUSH    AF              ;Save character
  11740.   02FB1 79              LD      A,C             ;A = length of line
  11741.   02FB2 FEFF            CP      0FFH            ;Maximum length reached ?
  11742.   02FB4 3803            JR      C,2FB9H         ;No: insert character
  11743.                                                 ;Yes:
  11744.   02FB6 F1              POP     AF              ;restore character
  11745.   02FB7 18C4            JR      2F7DH           ;Back to the 'I'-function
  11746.  
  11747. ; Insert character
  11748.  
  11749.   02FB9 90              SUB     B               ;A = length of line - number
  11750.                                                 ;of characters already printed
  11751.                                                 ;= remaining length
  11752.   02FBA 0C              INC     C               ;Length of line + 1
  11753.   02FBB 04              INC     B               ;Printed characters + 1
  11754.   02FBC C5              PUSH    BC              ;Save counter
  11755.   02FBD EB              EX      DE,HL           ;DE = pointer
  11756.   02FBE 6F              LD      L,A             ;HL = remaining length
  11757.   02FBF 2600            LD      H,00H
  11758.   02FC1 19              ADD     HL,DE           ;HL = current length +
  11759.                                                 ;remaining length = pointer to
  11760.                                                 ;the end of line
  11761.   02FC2 44              LD      B,H             ;BC = end pointer
  11762.   02FC3 4D              LD      C,L
  11763.   02FC4 23              INC     HL              ;HL = end pointer + 1
  11764.   02FC5 CD5819          CALL    1958H           ;Shift line
  11765.   02FC8 C1              POP     BC              ;Restore counter
  11766.   02FC9 F1              POP     AF              ;Restore character
  11767.   02FCA 77              LD      (HL),A          ;Insert character
  11768.   02FCB CD2A03          CALL    032AH           ;Print character
  11769.   02FCE 23              INC     HL              ;ointer + 1
  11770.   02FCF C37D2F          JP      2F7DH           ;Back to the 'I'-function
  11771.  
  11772. ; Arrow left: delete character left from cursor
  11773.  
  11774.   02FD2 78              LD      A,B             ;A = number of printed
  11775.                                                 ;characters (+ number of
  11776.                                                 ;characters left from cursor)
  11777.   02FD3 B7              OR      A               ;Anything printed ?
  11778.   02FD4 C8              RET     Z               ;No: done, return
  11779.                                                 ;Yes:
  11780.   02FD5 05              DEC     B               ;Counter - 1
  11781.   02FD6 2B              DEC     HL              ;Pointer - 1
  11782.   02FD7 3E08            LD      A,08H           ;Delete character
  11783.   02FD9 CD2A03          CALL    032AH           ;on screen
  11784.   02FDC 15              DEC     D               ;Repeat ?
  11785.   02FDD 20F3            JR      NZ,2FD2H        ;Yes: next character
  11786.  
  11787.   02FDF C9              RET
  11788.  
  11789. ; RETURN: Print remaining part of line and end EDIT
  11790.  
  11791.   02FE0 CD752B          CALL    2B75H           ;Print remaining line
  11792.  
  11793. ; 'E': end EDIT
  11794.  
  11795.   02FE3 CDFE20          CALL    20FEH           ;Start new line on screen
  11796.   02FE6 C1              POP     BC              ;Remove RET address
  11797.   02FE7 D1              POP     DE              ;Restore LN
  11798.   02FE8 7A              LD      A,D             ;LN = 65535 ?
  11799.   02FE9 A3              AND     E               ;(set Z-flag for 2FEFH,
  11800.   02FEA 3C              INC     A               ;see 1A71H)
  11801.   02FEB 2AA740          LD      HL,(40A7H)      ;HL = line buffer address
  11802.   02FEE 2B              DEC     HL              ;Pointer - 1
  11803.                                                 ;LN = 65535 ?
  11804.   02FEF C8              RET     Z               ;Yes: done, return
  11805.  
  11806.   02FF0 37              SCF                     ;C-flag = 1
  11807.   02FF1 23              INC     HL              ;Pointer + 1
  11808.   02FF2 F5              PUSH    AF              ;Save flags
  11809.   02FF3 C3981A          JP      1A98H           ;Insert line at HL into
  11810.                                                 ;program text
  11811. ; 'Q': terminate edit, ignore changes
  11812.  
  11813.   02FF6 C1              POP     BC              ;Restore RET address
  11814.   02FF7 D1              POP     DE              ;Restore LN
  11815.   02FF8 C3191A          JP      1A19H           ;Back to active command mode
  11816.  
  11817.   02FFB DEC3            SBC     A,0C3H          ;--
  11818.   02FFD C344B2          JP      0B244H
  11819.  
  11820.  
  11821. ; Keyboard input with FKEY evaluation
  11822. ; (Is only used by line input (at 05D9H) and has
  11823. ; therefor 05E3H as return address!!
  11824.  
  11825.   03000 C31534          JP      3415H           ;Continue at 3415H
  11826.  
  11827. ; No Function Key pressed (start of 3458H)
  11828. ; CTRL + number 1 to 8 pressed for colour change ?
  11829.  
  11830.   03003 E5              PUSH    HL              ;Save pointer
  11831.   03004 211840          LD      HL,4018H        ;HL = CTRL byte
  11832.   03007 CB7E            BIT     7,(HL)          ;CTRL pressed ?
  11833.   03009 2812            JR      Z,301DH         ;No: done
  11834.                                                 ;Yes:
  11835.   0300B CBBE            RES     7,(HL)          ;Clear bit
  11836.   0300D FE31            CP      31H             ;A number pressed ?
  11837.   0300F 380C            JR      C,301DH         ;No: put character in A
  11838.  
  11839.   03011 FE39            CP      39H             ;Valid number ?
  11840.   03013 3008            JR      NC,301DH        ;No: done
  11841.                                                 ;Yes:
  11842.   03015 D631            SUB     31H             ;A = Colour code 0 to 7
  11843.   03017 CD2136          CALL    3621H           ;Store new colour code and
  11844.                                                 ;cursor on
  11845.   0301A E1              POP     HL              ;Restore pointer
  11846.   0301B 18E3            JR      3000H           ;Get new character
  11847.  
  11848. ; Give character in A to input routine
  11849.  
  11850.   0301D E1              POP     HL              ;Restore pointer
  11851.   0301E C3E305          JP      05E3H           ;Return to line input routine
  11852.  
  11853.   03021 FF              RST     38H             ;--
  11854.  
  11855. ; Graphic character (> 7FH) recognized
  11856.  
  11857.   03022 FEC0            CP      0C0H            ;This comparison was used in
  11858.                                                 ;a previous version to output
  11859.                                                 ;the characters 192-255
  11860.                                                 ;(C0H-FFH) as tab-function.
  11861.                                                 ;(output of 0 to 63 spaces)
  11862.                                                 ;In the current version all
  11863.                                                 ;values > 7FH are printed as
  11864.                                                 ;graphic characters.
  11865.   03024 C30531          JP      3105H           ;Print character
  11866.  
  11867. ; Previously used Tab-function (not used)
  11868.  
  11869.   03027 D6C0            SUB     0C0H            ;A = value - 192 (= number of
  11870.                                                 ;spaces to print)
  11871.   03029 CA0831          JP      Z,3108H         ;A = 0: done
  11872.  
  11873.   0302C 47              LD      B,A             ;B = counter
  11874.   0302D 3E20            LD      A,' '           ;A = space
  11875.   0302F C5              PUSH    BC              ;Save counter
  11876.   03030 CD8431          CALL    3184H           ;Print character
  11877.   03033 C1              POP     BC              ;Restore counter
  11878.   03034 10F7            DJNZ    302DH           ;Next character
  11879.   03036 C30831          JP      3108H
  11880.  
  11881. ; Move cursor one position to the right (= CHR$(25) )
  11882.  
  11883.   03039 23              INC     HL              ;Cursor address + 1
  11884.   0303A E5              PUSH    HL              ;Save cursor address
  11885.   0303B CD6531          CALL    3165H           ;HL -> actual line
  11886.   0303E EB              EX      DE,HL           ;DE = HL
  11887.   0303F E1              POP     HL              ;Restore cursor address
  11888.   03040 DF              RST     18H             ;Next line reached ?
  11889.   03041 C0              RET     NZ              ;No: done
  11890.                                                 ;Yes:
  11891.   03042 11D8FF          LD      DE,0FFD8H       ;DE = -40
  11892.   03045 19              ADD     HL,DE           ;Subtract 40 (length of 1
  11893.                                                 ;line) from new address.
  11894.                                                 ;The cursor is located at the
  11895.                                                 ;start of line again
  11896.   03046 C9              RET
  11897.  
  11898.  
  11899. ; This routine loads the CRTC register pair A and A + 1 with HL:
  11900. ; 16 bit load for CRTC register
  11901. ;
  11902. ; I: A = register number of lower register of register pair
  11903. ;    HL = value to be written into CRTC
  11904.  
  11905.   03047 C5              PUSH    BC              ;Save BC
  11906.   03048 E5              PUSH    HL              ;Save HL
  11907.   03049 0602            LD      B,02H           ;Counter = 2
  11908.  
  11909.   0304B 0EFA            LD      C,0FAH          ;C = CRTC select register
  11910.   0304D ED79            OUT     (C),A           ;Select register A
  11911.   0304F 0C              INC     C               ;C = CRTC data register
  11912.   03050 ED61            OUT     (C),H           ;Write data to CRTC register
  11913.   03052 3C              INC     A               ;Next register
  11914.   03053 65              LD      H,L             ;H = L
  11915.   03054 10F5            DJNZ    304BH           ;Loop
  11916.  
  11917.   03056 E1              POP     HL              ;Restore HL
  11918.   03057 C1              POP     BC              ;Restore BC
  11919.   03058 C9              RET
  11920.  
  11921. ; Cursor off (= CHR$(15) )
  11922.  
  11923.   03059 E5              PUSH    HL              ;Save address
  11924.   0305A 210720          LD      HL,2007H        ;Set H and L for CRTC
  11925.                                                 ;programming
  11926.   0305D 1804            JR      3063H           ;Continue at 3063H
  11927.  
  11928. ; Cursor on (= CHR$(14) )
  11929.  
  11930.   0305F E5              PUSH    HL              ;Save address
  11931.   03060 2A1940          LD      HL,(4019H)      ;H and L = value for CRTC
  11932.                                                 ;programming
  11933.  
  11934.   03063 3E0A            LD      A,0AH           ;Select CRTC registers 10+11
  11935.   03065 CD4730          CALL    3047H           ;Write HL into CRTC
  11936.   03068 7C              LD      A,H             ;A = cursor start scan line
  11937.   03069 E1              POP     HL              ;Restore address
  11938.   0306A E5              PUSH    HL              ;Save registers
  11939.   0306B D5              PUSH    DE
  11940.   0306C FE20            CP      20H             ;Cursor off ?
  11941.   0306E C41436          CALL    NZ,3614H        ;No: set colour of new cursor
  11942.  
  11943.   03071 00              NOP                     ;--
  11944.   03072 3E0E            LD      A,0EH           ;Select CRTC registers 14+15
  11945.                                                 ;(Cursor position)
  11946.   03074 CD4730          CALL    3047H           ;Write address into CRTC
  11947.   03077 D1              POP     DE              ;Restore registers
  11948.   03078 E1              POP     HL
  11949.   03079 C9              RET
  11950.  
  11951.  
  11952. ; Save registers and set colour
  11953. ;
  11954. ; I: A = character at (HL)
  11955. ;    HL -> screen location in LGR screen memory
  11956.  
  11957.   0307A E5              PUSH    HL              ;Save registers
  11958.   0307B D5              PUSH    DE
  11959.   0307C F5              PUSH    AF
  11960.   0307D C3F835          JP      35F8H           ;Continue at 35F8H
  11961.  
  11962.  
  11963. ; This routine updates the colour memory at position HL + DE with the current
  11964. ; text colour set (see 35F8H)
  11965. ;
  11966. ; I: HL + DE -> Colour memory byte to be updated
  11967. ; O: -
  11968.  
  11969.   03080 19              ADD     HL,DE           ;Set pointer into colour mem.
  11970.   03081 E5              PUSH    HL              ;Save colour memory pointer
  11971.   03082 219043          LD      HL,4390H        ;HL -> colour codes table
  11972.   03085 110000          LD      DE,0000H        ;DE = offset = 0;
  11973.   03088 3A2340          LD      A,(4023H)       ;Take current colour value
  11974.   0308B 5F              LD      E,A             ;Use it as offset in table
  11975.   0308C 19              ADD     HL,DE           ;Set pointer into table
  11976.   0308D 7E              LD      A,(HL)          ;A = colour code
  11977.   0308E E1              POP     HL              ;Restore colour memory pointer
  11978.  
  11979.   0308F 77              LD      (HL),A          ;Write value into colour mem.
  11980.   03090 F1              POP     AF              ;Restore registers
  11981.   03091 D1              POP     DE
  11982.   03092 E1              POP     HL
  11983.   03093 C9              RET
  11984.  
  11985.   03094 03              DEFB    03              ;--
  11986.   03095 01              DEFB    01
  11987.   03096 02              DEFB    02
  11988.   03097 04              DEFB    04
  11989.   03098 06              DEFB    06
  11990.   03099 08              DEFB    08
  11991.   0309A 09              DEFB    09
  11992.   0309B 0A              DEFB    0A
  11993.   0309C 05              DEFB    05
  11994.  
  11995. ; Calculate new POS value (AF)
  11996. ;
  11997. ; I: --
  11998. ; O: A = new POS value
  11999.  
  12000.   0309D E5              PUSH    HL              ;Save registers
  12001.   0309E D5              PUSH    DE
  12002.   0309F C5              PUSH    BC
  12003.   030A0 2A2040          LD      HL,(4020H)      ;HL = new cursor address
  12004.   030A3 E5              PUSH    HL              ;Save address
  12005.   030A4 CD6531          CALL    3165H           ;Calculate start of line
  12006.   030A7 EB              EX      DE,HL           ;DE -> start of line
  12007.   030A8 E1              POP     HL              ;HL = cursor address
  12008.   030A9 B7              OR      A               ;C-flag = 0
  12009.   030AA ED52            SBC     HL,DE           ;HL = cursor address - start
  12010.                                                 ;of line = cursor position
  12011.                                                 ;inside line
  12012.   030AC 7D              LD      A,L             ;A = POS value
  12013.   030AD C1              POP     BC              ;Restore registers
  12014.   030AE D1              POP     DE
  12015.   030AF E1              POP     HL
  12016.   030B0 C9              RET
  12017.  
  12018. ; Test TAB value
  12019.  
  12020.   030B1 7B              LD      A,E             ;A = TAB value
  12021.   030B2 5F              LD      E,A             ;E = TAB value
  12022.   030B3 3A9C40          LD      A,(409CH)       ;A = output flag
  12023.   030B6 B7              OR      A               ;Test output flag
  12024.                                                 ;Cassette output ?
  12025.   030B7 FA4A1E          JP      M,1E4AH         ;Yes: ?FC Error
  12026.                                                 ;Screen output ?
  12027.   030BA 2805            JR      Z,30C1H         ;Yes: continue at 30C1H
  12028.                                                 ;Printer output:
  12029.   030BC 3A9E40          LD      A,(409EH)       ;A = highest possible TAB
  12030.                                                 ;value.
  12031.   030BF 1803            JR      30C4H           ;Continue at 30C4H
  12032.  
  12033. ; Screen output
  12034.  
  12035.   030C1 3A9D40          LD      A,(409DH)       ;A = number of characters per
  12036.                                                 ;line
  12037.   030C4 BB              CP      E               ;Desired TAB value bigger as
  12038.                                                 ;length of line ?
  12039.   030C5 300B            JR      NC,30D2H        ;No: value ok
  12040.                                                 ;Yes:
  12041.   030C7 C5              PUSH    BC              ;Save BC
  12042.   030C8 43              LD      B,E             ;Exchange A and E
  12043.   030C9 5F              LD      E,A
  12044.   030CA 78              LD      A,B
  12045.   030CB C1              POP     BC              ;Restore BC
  12046.   030CC 93              SUB     E               ;Subtract line length
  12047.   030CD 30FD            JR      NC,30CCH        ;Continue when > 0
  12048.  
  12049.   030CF 83              ADD     A,E             ;Undo last subtract
  12050.   030D0 5F              LD      E,A             ;E = new TAB value (never
  12051.                                                 ;bigger then length of line)
  12052.   030D1 C9              RET
  12053.  
  12054. ; TAB value is ok.
  12055.  
  12056.   030D2 7B              LD      A,E             ;Put value in A
  12057.   030D3 C9              RET
  12058.  
  12059.  
  12060. ; SUB for PRINT@ (see 207AH)
  12061. ;
  12062. ; I: DE = @ argument
  12063.  
  12064.   030D4 21E703          LD      HL,03E7H        ;HL = 999
  12065.   030D7 DF              RST     18H             ;@ value > 999 ?
  12066.   030D8 E1              POP     HL              ;Restore PTP
  12067.   030D9 DA4A1E          JP      C,1E4AH         ;Yes: ?FC Error
  12068.                                                 ;No:
  12069.   030DC C37E20          JP      207EH           ;Done
  12070.  
  12071. ; Calculate new POS value (as 309DH but with value in E and A)
  12072.  
  12073.   030DF CD9D30          CALL    309DH           ;Calculate POS
  12074.   030E2 5F              LD      E,A             ;Put result also in E
  12075.   030E3 C9              RET
  12076.  
  12077.  
  12078. ; Screen routine (called via DCB call)
  12079. ;
  12080. ; I: IX -> Screen DCB (= 401DH)
  12081. ;    C = character to be written
  12082.  
  12083.   030E4 DD6E03          LD      L,(IX+03H)      ;HL = actual cursor address
  12084.   030E7 DD6604          LD      H,(IX+04H)      ;(from DCB)
  12085.                                                 ;False DCB type ?
  12086.   030EA DA7D31          JP      C,317DH         ;Yes: continue at 317DH
  12087.                                                 ;No:
  12088.   030ED DD7E05          LD      A,(IX+05H)      ;Cursor is on ?
  12089.   030F0 B7              OR      A
  12090.   030F1 2005            JR      NZ,30F8H        ;Yes: continue at 30F8H
  12091.                                                 ;No:
  12092.   030F3 CD5930          CALL    3059H           ;Cursor off
  12093.   030F6 1803            JR      30FBH           ;Continue at 30F8H
  12094.  
  12095.   030F8 CD5F30          CALL    305FH           ;Cursor on
  12096.   030FB 79              LD      A,C             ;A = character to be output
  12097.   030FC FE20            CP      20H             ;Control character
  12098.   030FE 3822            JR      C,3122H         ;Yes: continue at 3122H
  12099.                                                 ;No:
  12100.   03100 FE80            CP      80H             ;Graphic character ?
  12101.   03102 D22230          JP      NC,3022H        ;Yes: continue at 3022H
  12102.                                                 ;No:
  12103.   03105 CD8431          CALL    3184H           ;Output character
  12104.   03108 7E              LD      A,(HL)          ;A = character at new cursor
  12105.                                                 ;position
  12106.   03109 57              LD      D,A             ;D = character
  12107.   0310A DD7E05          LD      A,(IX+05H)      ;Cursor is on ?
  12108.   0310D B7              OR      A
  12109.   0310E 2808            JR      Z,3118H         ;No: continue at 3118H
  12110.                                                 ;Yes:
  12111.   03110 DD7205          LD      (IX+05H),D      ;Store character
  12112.   03113 CD5F30          CALL    305FH           ;Cursor on
  12113.   03116 1803            JR      311BH           ;Continue at 311BH
  12114.  
  12115.   03118 CD5930          CALL    3059H           ;Cursor off
  12116.   0311B DD7503          LD      (IX+03H),L      ;Save new cursor address
  12117.   0311E DD7404          LD      (IX+04H),H
  12118.   03121 C9              RET
  12119.  
  12120.  
  12121. ; Control character ( value < 32 ) recognized.
  12122. ;
  12123. ; I: A = control character
  12124. ;    HL = cursor address
  12125.  
  12126.   03122 110831          LD      DE,3108H        ;Set return address
  12127.   03125 D5              PUSH    DE              ;to 3108H
  12128.  
  12129.   03126 FE08            CP      08H             ;Backspace ?
  12130.   03128 CADF31          JP      Z,31DFH         ;Yes: continue at 31DFH
  12131.                                                 ;No:
  12132.   0312B FE0A            CP      0AH             ;Code < 10 ?
  12133.   0312D D8              RET     C               ;Yes: done
  12134.  
  12135.   0312E FE0E            CP      0EH             ;Cursor on ?
  12136.   03130 DAC931          JP      C,31C9H         ;Jump when character < 0EH
  12137.  
  12138.   03133 CAF831          JP      Z,31F8H         ;Yes: continue at 31F8H
  12139.                                                 ;No:
  12140.   03136 FE0F            CP      0FH             ;Cursor off ?
  12141.   03138 CAFD31          JP      Z,31FDH         ;Yes: continue at 31FDH
  12142.                                                 .No:
  12143.   0313B FE18            CP      18H             ;Character < 24
  12144.   0313D D8              RET     C               ;Yes: done
  12145.                                                 ,No:
  12146.   0313E FE18            CP      18H             ;Cursor left ?
  12147.   03140 CAE531          JP      Z,31E5H         ;Yes: continue at 31E5H
  12148.                                                 ;No:
  12149.   03143 FE19            CP      19H             ;Cursor right ?
  12150.   03145 CA3930          JP      Z,3039H         ;Yes: continue at 3039H
  12151.                                                 ;No:
  12152.   03148 FE1A            CP      1AH             ;Cursor down ?
  12153.   0314A CA0032          JP      Z,3200H         ;Yes: continue at 3200H
  12154.                                                 ;No:
  12155.   0314D FE1B            CP      1BH             ;Cursor up ?
  12156.   0314F CA1232          JP      Z,3212H         ;Yes: continue at 3212H
  12157.                                                 ;No:
  12158.   03152 FE1C            CP      1CH             ;Cursor home ?
  12159.   03154 CAD431          JP      Z,31D4H         ;Yes: continue at 31D4H
  12160.                                                 ;No:
  12161.   03157 FE1D            CP      1DH             ;Cursor to start of line ?
  12162.   03159 CAD931          JP      Z,31D9H         ;Yes: continue at 31D9H
  12163.                                                 ;No:
  12164.   0315C FE1E            CP      1EH             ;CLear until end of line ?
  12165.   0315E 285F            JR      Z,31BFH         ;Yes: continue at 31BFH
  12166.                                                 ;No:
  12167.   03160 FE1F            CP      1FH             ;Clear until end of screen ?
  12168.   03162 2845            JR      Z,31A9H         ;Yes: continue at 31A9H
  12169.  
  12170.   03164 C9              RET
  12171.  
  12172.  
  12173. ; Calculate start of line address of current line
  12174. ;
  12175. ; I: HL = current cursor address
  12176. ; O: HL = address of start of line
  12177.  
  12178.   03165 1100BC          LD      DE,0BC00H       ;DE = -4400H
  12179.   03168 0601            LD      B,01H           ;Counter = 1
  12180.   0316A 19              ADD     HL,DE           ;HL = current @ value
  12181.                                                 ;(= address - 4400H)
  12182.   0316B 112800          LD      DE,0028H        ;DE = 40 (length of line)
  12183.   0316E B7              OR      A               ;C-flag = 0
  12184.   0316F ED52            SBC     HL,DE           ;Subtract 40
  12185.   03171 3803            JR      C,3176H         ;Jump when < 0
  12186.  
  12187.   03173 04              INC     B               ;Counter + 1
  12188.   03174 18F5            JR      316BH           ;Again
  12189.  
  12190.   03176 21D843          LD      HL,43D8H        ;HL = 4400H - 28H
  12191.   03179 19              ADD     HL,DE           ;Add length of 1 line
  12192.   0317A 10FD            DJNZ    3179H           ;Loop B times
  12193.  
  12194.   0317C C9              RET
  12195.  
  12196.  
  12197. ; False DCB type detected
  12198.  
  12199.   0317D DD7E05          LD      A,(IX+05H)      ;Cursor is on ?
  12200.   03180 B7              OR      A
  12201.   03181 C0              RET     NZ              ;Yes: ok.
  12202.                                                 ;No:
  12203.   03182 7E              LD      A,(HL)          ;A = character at next
  12204.                                                 ;screen location
  12205.   03183 C9              RET
  12206.  
  12207.  
  12208. ; Print character on screen
  12209. ;
  12210. ; I: HL -> screen location
  12211. ;    A = character
  12212.  
  12213.   03184 CD7A30          CALL    307AH           ;Update character colour
  12214.   03187 77              LD      (HL),A          ;Put character in screen memory
  12215.   03188 23              INC     HL              ;Next screen location
  12216.   03189 11E847          LD      DE,47E8H        ;DE -> last screen location
  12217.                                                 ;on LGR screen + 1
  12218.   0318C DF              RST     18H             ;End of screen reached ?
  12219.   0318D D8              RET     C               ;No: done
  12220.  
  12221.   0318E 21E847          LD      HL,47E8H        ;HL = address of screen end
  12222.   03191 11D8FF          LD      DE,0FFD8H       ;DE = -40
  12223.   03194 19              ADD     HL,DE           ;HL = HL - 40 = start of last
  12224.                                                 ;line
  12225.   03195 E5              PUSH    HL              ;Save address
  12226.   03196 CD3936          CALL    3639H           ;Prepare registers
  12227.  
  12228.   03199 EDA0            LDI                     ;Move character memory
  12229.   0319B D9              EXX                     ;Switch registers
  12230.   0319C EDA0            LDI                     ;Move colour memory
  12231.   0319E D9              EXX                     ;Switch registers
  12232.   0319F 78              LD      A,B             ;Done ?
  12233.   031A0 B1              OR      C
  12234.   031A1 20F6            JR      NZ,3199H        ;No: move again
  12235.  
  12236.   031A3 D9              EXX                     ;Switch registers
  12237.   031A4 E1              POP     HL              ;Restore second registerset
  12238.   031A5 D1              POP     DE
  12239.   031A6 C1              POP     BC
  12240.   031A7 D9              EXX                     ;Switch registers
  12241.   031A8 E1              POP     HL              ;Restore start of last line
  12242.  
  12243. ; Clear until end of screen (= CHR$(31) )
  12244.  
  12245.   031A9 11E847          LD      DE,47E8H        ;DE = highest LGR screen
  12246.                                                 ;address + 1
  12247.   031AC E5              PUSH    HL              ;Save cursor address
  12248.  
  12249.   031AD CDE031          CALL    31E0H           ;Clear character at cursor
  12250.                                                 ;address and set colour
  12251.   031B0 23              INC     HL              ;Next address
  12252.   031B1 DF              RST     18H             ;End reached ?
  12253.   031B2 20F9            JR      NZ,31ADH        ;No: clear next
  12254.  
  12255.   031B4 E1              POP     HL              ;Restore cursor address
  12256.   031B5 C9              RET
  12257.  
  12258.  
  12259. ; RENUM statement
  12260. ; ---------------
  12261.  
  12262.   031B6 FDE5            PUSH    IY              ;Save IY
  12263.   031B8 1868            JR      3222H           ;Continue at 3222H
  12264.  
  12265. ; Return from RENUM
  12266.  
  12267.   031BA FDE1            POP     IY              ;Restore IY
  12268.   031BC C3832C          JP      2C83H           ;Renew all pointers in BASIC
  12269.                                                 ;program
  12270.  
  12271. ; Clear until end of line (= CHR$(30) )
  12272.  
  12273.   031BF E5              PUSH    HL              ;Save cursor address
  12274.   031C0 CD6531          CALL    3165H           ;Calculate start address of
  12275.                                                 ;current line
  12276.   031C3 EB              EX      DE,HL           ;result in DE, HL = 40
  12277.   031C4 19              ADD     HL,DE           ;HL = start address of next
  12278.                                                 ;line
  12279.   031C5 EB              EX      DE,HL           ;Result in DE
  12280.   031C6 E1              POP     HL              ;Restore cursor address
  12281.   031C7 18E3            JR      31ACH           ;Continue at 31ACH
  12282.  
  12283. ; Start new line (= CHR$(13) )
  12284.  
  12285.   031C9 CD1B36          CALL    361BH           ;Clear current line
  12286.   031CC 19              ADD     HL,DE           ;HL = start address next line
  12287.   031CD 11E847          LD      DE,47E8H        ;DE -> end of LGR screen + 1
  12288.   031D0 DF              RST     18H             ;End reached ?
  12289.   031D1 28BE            JR      Z,3191H         ;Yes: scroll
  12290.                                                 ;No:
  12291.   031D3 C9              RET                     ;done
  12292.  
  12293. ; Move cursor home (= CHR$(28) )
  12294.  
  12295.   031D4 210044          LD      HL,4400H        ;HL -> start of screen memory =
  12296.                                                 ;new cursor address
  12297.   031D7 1803            JR      31DCH           ;Set as new address
  12298.  
  12299. ; Move cursor to start of line (= CHR$(29) )
  12300.  
  12301.   031D9 CD6531          CALL    3165H           ;Calculate start of line
  12302.                                                 ;address
  12303.   031DC C35F30          JP      305FH           ;Program CRTC with new values
  12304.  
  12305. ; Backspace (= CHR$(8) )
  12306.  
  12307.   031DF 2B              DEC     HL              ;Cursor address - 1
  12308.  
  12309.   031E0 3E20            LD      A,20H           ;A = space character
  12310.   031E2 C3EE35          JP      35EEH           ;Continue at 35EEH
  12311.  
  12312. ; Move cursor one position to the left (= CHR$(24) )
  12313.  
  12314.   031E5 E5              PUSH    HL              ;Save cursor address
  12315.   031E6 CD6531          CALL    3165H           ;Calculate start of line
  12316.                                                 ;address
  12317.   031E9 EB              EX      DE,HL           ;Put result in DE
  12318.   031EA E1              POP     HL              ;Restore cursor address
  12319.   031EB DF              RST     18H             ;Are we at start of line ?
  12320.   031EC 2803            JR      Z,31F1H         ;Yes: add one line of
  12321.                                                 ;characters for wrap around
  12322.   031EE 2B              DEC     HL              ;Cursor address - 1
  12323.   031EF 18EB            JR      31DCH           ;Set as new address
  12324.  
  12325. ; Cursor to end of line
  12326.  
  12327.   031F1 2B              DEC     HL              ;HL - 1
  12328.   031F2 112800          LD      DE,0028H        ;DE = characters per line
  12329.   031F5 19              ADD     HL,DE           ;HL + 40
  12330.   031F6 18E4            JR      31DCH           ;Continue at 31DCH
  12331.  
  12332. ; Cursor on
  12333.  
  12334.   031F8 7E              LD      A,(HL)          ;A = character at cursor pos.
  12335.   031F9 DD7705          LD      (IX+05H),A      ;Store it in DCB
  12336.   031FC C9              RET
  12337.  
  12338. ; Cursor off
  12339.  
  12340.   031FD AF              XOR     A               ;A = 0
  12341.   031FE 18F9            JR      31F9H           ;Continue at 31F9H
  12342.  
  12343. ; Move cursor one line down (= CHR$(26) )
  12344.  
  12345.   03200 CD0736          CALL    3607H           ;Set screen colour and DE = 40
  12346.   03203 19              ADD     HL,DE           ;Set cursor address into next
  12347.                                                 ;line
  12348.   03204 B7              OR      A               ;Clear C-flag
  12349.   03205 3F              CCF                     ;C-flag = 1
  12350.   03206 11E847          LD      DE,47E8H        ;DE = highest LGR screen
  12351.                                                 ;address + 1
  12352.   03209 DF              RST     18H             ;Cursor now beyound LGR screen ?
  12353.   0320A 3804            JR      C,3210H         ;No: continue at 3210H
  12354.                                                 ;Yes:
  12355.   0320C 1140FC          LD      DE,0FC40H       ;DE = -960
  12356.   0320F 19              ADD     HL,DE           ;Add to cursor location to
  12357.                                                 ;create wrap around to top line
  12358.   03210 18CA            JR      31DCH           ;Continue at 31DCH
  12359.  
  12360. ; Move cursor one line up (= CHR$(27) )
  12361.  
  12362.   03212 CD0F36          CALL    360FH           ;Set screen colour and DE = -40
  12363.   03215 19              ADD     HL,DE           ;Set cursor address into
  12364.                                                 ;previous line
  12365.   03216 110044          LD      DE,4400H        ;DE = lowest LGR screen address
  12366.   03219 DF              RST     18H             ;Cursor now before LGR screen ?
  12367.   0321A 3004            JR      NC,3220H        ;No: continue at 3220H
  12368.                                                 ;Yes:
  12369.   0321C 11E803          LD      DE,03E8H        ;DE = 1000 (size of LGR screen)
  12370.   0321F 19              ADD     HL,DE           ;Add to cursor location to
  12371.                                                 ;create a wrap around to bottom
  12372.                                                 ;line
  12373.   03220 18BA            JR      31DCH           ;Continue at 31DCH
  12374.  
  12375.  
  12376. ; RENUM entry from 31B8H
  12377. ;
  12378. ; I: HL = PTP
  12379.  
  12380.   03222 2B              DEC     HL              ;PTP - 1
  12381.   03223 D7              RST     10H             ;Character indicated
  12382.   03224 2006            JR      NZ,322CH        ;Yes: continue at 323FH
  12383.  
  12384.   03226 110A00          LD      DE,000AH        ;Starting LN = 10
  12385.   03229 D5              PUSH    DE              ;Save it
  12386.   0322A 1813            JR      323FH           ;Continue at 323FH
  12387.  
  12388. ; Indicate starting line number and increment
  12389.  
  12390.   0322C D24A1E          JP      NC,1E4AH        ;?FC Error when not a number
  12391.  
  12392.   0322F CD5A1E          CALL    1E5AH           ;DE = starting LN
  12393.   03232 CF              RST     08H             ;Next character must be
  12394.   03233 2C              DEFB    ','             ;a comma
  12395.   03234 30F6            JR      NC,322CH        ;?FC Error when comma is not
  12396.                                                 ;followed by a number
  12397.   03236 D5              PUSH    DE              ;Save starting LN
  12398.   03237 CD5A1E          CALL    1E5AH           ;DE = increment
  12399.   0323A 7A              LD      A,D             ;Increment = 0 ?
  12400.   0323B B3              OR      E
  12401.   0323C CA4A1E          JP      Z,1E4AH         ;Yes: ?FC Error
  12402.  
  12403.   0323F ED53E440        LD      (40E4H),DE      ;Store increment in system RAM
  12404.   03243 D1              POP     DE
  12405.   03244 ED53E240        LD      (40E2H),DE      ;Store starting LN in
  12406.                                                 ;system RAM
  12407.   03248 FD2AF940        LD      IY,(40F9H)      ;IY -> End of program
  12408.   0324C 110001          LD      DE,0100H        ;DE = 256
  12409.   0324F FD19            ADD     IY,DE           ;IY = IY + 256
  12410.   03251 FDE5            PUSH    IY              ;Save end of program address
  12411.                                                 ;+ 256
  12412.   03253 2AA440          LD      HL,(40A4H)      ;HL -> start of program
  12413.   03256 E5              PUSH    HL              ;Save it
  12414.  
  12415. ; Build RENUM table
  12416.  
  12417.   03257 7E              LD      A,(HL)          ;End of program reached ?
  12418.   03258 23              INC     HL              ;Pointer + 1
  12419.   03259 B6              OR      (HL)            ;Next line pointer = 0 ?
  12420.   0325A 284A            JR      Z,32A6H         ;Yes: continue at 32A6H
  12421.  
  12422.   0325C 23              INC     HL              ;Pointer + 2
  12423.   0325D 23              INC     HL
  12424.  
  12425.   0325E CDBA33          CALL    33BAH           ;Search line for GOTO, GOSUB
  12426.                                                 ;etc.
  12427.   03261 23              INC     HL              ;Pointer + 1
  12428.   03262 28F3            JR      Z,3257H         ;Process next line when no
  12429.                                                 ;keyword was found
  12430.   03264 CDD833          CALL    33D8H           ;LN indicated? (in case of THEN
  12431.                                                 ;or ELSE there can be either a
  12432.                                                 ;command of a line number!)
  12433.   03267 2B              DEC     HL              ;Pointer - 1
  12434.   03268 28F4            JR      Z,325EH         ;No: continue search
  12435.  
  12436.   0326A 23              INC     HL              ;Pointer + 1
  12437.   0326B E5              PUSH    HL              ;Save program pointer
  12438.   0326C D5              PUSH    DE              ;Save table pointer
  12439.   0326D FDE5            PUSH    IY              ;Save end of program address
  12440.   0326F D1              POP     DE              ;and put it in DE
  12441.   03270 2AB140          LD      HL,(40B1H)      ;HL = TOPMEM
  12442.   03273 ED52            SBC     HL,DE           ;Subtract program end address
  12443.                                                 ;Free memory space available ?
  12444.   03275 DA7A19          JP      C,197AH         ;No: ?OM Error
  12445.                                                 ;Yes:
  12446.   03278 110400          LD      DE,0004H        ;Additional 4 bytes free ?
  12447.   0327B ED52            SBC     HL,DE
  12448.   0327D DA7A19          JP      C,197AH         ;No: ?OM Error
  12449.                                                 ;Yes:
  12450.   03280 FD7000          LD      (IY+00H),B      ;Store number of digits of
  12451.                                                 ;LN found
  12452.   03283 E1              POP     HL              ;Restore table pointer in HL
  12453.   03284 CD5A1E          CALL    1E5AH           ;DE = LN found
  12454.   03287 FD7301          LD      (IY+01H),E      ;Store LN
  12455.   0328A FD7202          LD      (IY+02H),D
  12456.   0328D FD360300        LD      (IY+03H),00H    ;And mark with 00H
  12457.   03291 CDB133          CALL    33B1H           ;Increase IY by 4
  12458.   03294 E1              POP     HL              ;Restore program pointer
  12459.   03295 2B              DEC     HL              ;Program pointer - 1
  12460.  
  12461.   03296 23              INC     HL              ;Program pointer + 1
  12462.   03297 7E              LD      A,(HL)          ;A = next character
  12463.   03298 FE20            CP      20H             ;Space found ?
  12464.   0329A 28FA            JR      Z,3296H         ;Yes: get next character
  12465.  
  12466.   0329C FE2C            CP      2CH             ;Comma found ? (with ON GOTO or
  12467.                                                 ;ON GOSUB)
  12468.   0329E 2803            JR      Z,32A3H         ;Yes: continue ar 32A3H
  12469.  
  12470.   032A0 2B              DEC     HL              ;Program pointer - 1
  12471.   032A1 18BB            JR      325EH           ;Continue searching line
  12472.  
  12473.   032A3 23              INC     HL              ;Program pointer + 1
  12474.   032A4 18BE            JR      3264H           ;Serach next LN
  12475.  
  12476. ; RENUM table completed
  12477. ; Now use the new line numbers from the table
  12478.  
  12479.   032A6 FD3600FF        LD      (IY+00H),0FFH   ;Table end
  12480.   032AA E1              POP     HL              ;Restore program start
  12481.   032AB FDE1            POP     IY              ;Restore table start
  12482.   032AD ED5BE240        LD      DE,(40E2H)      ;DE = starting LN
  12483.   032B1 D5              PUSH    DE              ;Save new LN
  12484.   032B2 FDE5            PUSH    IY              ;Save table start
  12485.   032B4 E5              PUSH    HL              ;Save program start
  12486.   032B5 D5              PUSH    DE              ;Save starting LN
  12487.   032B6 CDC209          CALL    09C2H           ;BCDE = (HL): DE = pointer to
  12488.                                                 ;next line, BC = line number
  12489.   032B9 7A              LD      A,D             ;Program end reached ?
  12490.   032BA B3              OR      E
  12491.   032BB 2841            JR      Z,32FEH         ;Yes: continue at 32FEH
  12492.  
  12493.   032BD EB              EX      DE,HL           ;HL -> next line
  12494.   032BE D1              POP     DE              ;Restore new LN
  12495.   032BF FDE5            PUSH    IY              ;Save table pointer
  12496.  
  12497.   032C1 FD7E00          LD      A,(IY+00H)      ;Table end reached ?
  12498.   032C4 3C              INC     A               ;(FFH + 1 = 00H)
  12499.   032C5 2821            JR      Z,32E8H         ;Yes: continue at 32E8H
  12500.  
  12501.   032C7 FD7E03          LD      A,(IY+03H)      ;A = table marker
  12502.   032CA B7              OR      A               ;Marker <> 0 ?
  12503.   032CB 2016            JR      NZ,32E3H        ;Yes: this entry has already
  12504.                                                 ;the new line number
  12505.   032CD FD7E01          LD      A,(IY+01H)      ;No: Is the current line number
  12506.   032D0 B9              CP      C               ;already in the table ?
  12507.   032D1 2010            JR      NZ,32E3H
  12508.  
  12509.   032D3 FD7E02          LD      A,(IY+02H)
  12510.   032D6 B8              CP      B
  12511.   032D7 200A            JR      NZ,32E3H        ;No: check next entry
  12512.                                                 ;Yes:
  12513.   032D9 FD7301          LD      (IY+01H),E      ;Insert new line number in
  12514.   032DC FD7202          LD      (IY+02H),D      ;the table
  12515.   032DF FD360301        LD      (IY+03H),01H    ;Mark entry
  12516.   032E3 CDB133          CALL    33B1H           ;Increase table pointer by 4
  12517.   032E6 18D9            JR      32C1H           ;Check next entry
  12518.  
  12519. ; Table end reached, insert next line number
  12520.  
  12521.   032E8 FDE1            POP     IY              ;Restore table start
  12522.   032EA E5              PUSH    HL              ;Save program pointer
  12523.   032EB 2AE440          LD      HL,(40E4H)      ;HL = increment
  12524.   032EE 19              ADD     HL,DE           ;HL = new LN + increment =
  12525.                                                 ;next new LN
  12526.                                                 ;Next LN > 65535 ?
  12527.   032EF DA7A19          JP      C,197AH         ;Yes: ?OM Error (wrong error
  12528.                                                 ;code !!)
  12529.   032F2 EB              EX      DE,HL           ;DE = new LN
  12530.   032F3 21F8FF          LD      HL,0FFF8H       ;New LN > 65529 ?
  12531.   032F6 ED52            SBC     HL,DE
  12532.   032F8 DA7A19          JP      C,197AH         ;Yes: ?OM Error (?!?)
  12533.  
  12534.   032FB E1              POP     HL              ;Restore program pointer
  12535.   032FC 18B7            JR      32B5H           ;Search for next line number in
  12536.                                                 ;table
  12537. ; Use new line numbers in program
  12538.  
  12539.   032FE D1              POP     DE              ;Correct stack
  12540.   032FF E1              POP     HL              ;Restore program start
  12541.   03300 FDE1            POP     IY              ;Restore table start
  12542.   03302 D1              POP     DE              ;Restore starting LN
  12543.  
  12544.   03303 7E              LD      A,(HL)          ;Program end reached ?
  12545.   03304 23              INC     HL
  12546.   03305 B6              OR      (HL)
  12547.   03306 CABA31          JP      Z,31BAH         ;Yes: RENUM done
  12548.  
  12549.   03309 23              INC     HL              ;Put new line number
  12550.   0330A 73              LD      (HL),E          ;into program
  12551.   0330B 23              INC     HL
  12552.   0330C 72              LD      (HL),D
  12553.   0330D CDBA33          CALL    33BAH           ;Search for GOTO, GOSUB etc.
  12554.   03310 23              INC     HL              ;Pointer + 1
  12555.   03311 2009            JR      NZ,331CH        ;Token found: continue at 33C1H
  12556.  
  12557.   03313 E5              PUSH    HL              ;Save pointer
  12558.   03314 2AE440          LD      HL,(40E4H)      ;HL = increment
  12559.   03317 19              ADD     HL,DE           ;Calculate next LN
  12560.   03318 EB              EX      DE,HL           ;DE = new LN
  12561.   03319 E1              POP     HL              ;Restore pointer
  12562.   0331A 18E7            JR      3303H           ;Put new LN into program
  12563.  
  12564. ; GOTO, GOSUB etc. found
  12565.  
  12566.   0331C E5              PUSH    HL              ;Save pointer
  12567.   0331D D5              PUSH    DE              ;Save new LN
  12568.   0331E CDD833          CALL    33D8H           ;Transfer LN found into line
  12569.                                                 ;buffer and determine the
  12570.                                                 ;number of digits
  12571.   03321 D1              POP     DE              ;Restore new LN
  12572.   03322 E1              POP     HL              ;Restore pointer
  12573.   03323 2B              DEC     HL              ;Pointer - 1
  12574.   03324 28E7            JR      Z,330DH         ;Search for next token if no LN
  12575.                                                 ;was found (e.g. after THEN
  12576.                                                 ;or ELSE)
  12577.   03326 23              INC     HL              ;Pointer + 1
  12578.   03327 7E              LD      A,(HL)          ;A = next character
  12579.   03328 FE20            CP      20H             ;Space ?
  12580.   0332A 28FA            JR      Z,3326H         ;Yes: update pointer and take
  12581.                                                 ;next character
  12582.   0332C D5              PUSH    DE              ;Save new LN
  12583.   0332D E5              PUSH    HL              ;Save pointer
  12584.   0332E FD6E01          LD      L,(IY+01H)      ;HL = new LN for this spot
  12585.   03331 FD6602          LD      H,(IY+02H)
  12586.   03334 CD9433          CALL    3394H           ;Put new LN in line buffer
  12587.                                                 ;(in ASCII format!)
  12588.   03337 FD4E00          LD      C,(IY+00H)      ;C = number of digits (=length)
  12589.                                                 ;of old LN
  12590.   0333A CDB133          CALL    33B1H           ;Increment IY to next entry
  12591.   0333D EB              EX      DE,HL           ;DE -> new LN in line buffer
  12592.   0333E E1              POP     HL              ;HL = program pointer
  12593.                                                 ;(points to old LN)
  12594.   0333F CDF833          CALL    33F8H           ;Replace old LN at this spot
  12595.   03342 D1              POP     DE              ;Restore new LN
  12596.   03343 2B              DEC     HL              ;Pointer - 1
  12597.  
  12598.   03344 23              INC     HL              ;Pointer + 1
  12599.   03345 7E              LD      A,(HL)          ;Increment pointer until
  12600.   03346 FE20            CP      20H             ;the next non-space
  12601.   03348 28FA            JR      Z,3344H         ;character is found
  12602.  
  12603.   0334A FE2C            CP      2CH             ;Comma found ?
  12604.   0334C 2803            JR      Z,3351H         ;Yes: check for following LN
  12605.                                                 ;No:
  12606.   0334E 2B              DEC     HL              ;Pointer - 1
  12607.   0334F 18BC            JR      330DH           ;Search for next token
  12608.  
  12609. ; ',' found (with ON GOTO or ON GOSUB)
  12610.  
  12611.   03351 23              INC     HL              ;Pointer + 1
  12612.   03352 18C8            JR      331CH           ;Replace next LN
  12613.  
  12614.  
  12615. ; Delete B characters from program
  12616. ;
  12617. ; I: B = number of characters to be deleted
  12618. ;    HL -> program text
  12619. ;    (from (HL) onwards, B bytes are removed from the program text)
  12620.  
  12621.   03354 D5              PUSH    DE              ;Save registers
  12622.   03355 C5              PUSH    BC
  12623.   03356 E5              PUSH    HL
  12624.   03357 E5              PUSH    HL
  12625.   03358 D1              POP     DE              ;DE -> program text
  12626.   03359 D5              PUSH    DE              ;Save DE twice
  12627.   0335A D5              PUSH    DE
  12628.   0335B 2AF940          LD      HL,(40F9H)      ;HL -> end of program
  12629.   0335E E5              PUSH    HL              ;Save it
  12630.  
  12631.   0335F 2B              DEC     HL              ;End address - 1
  12632.   03360 13              INC     DE              ;Program pointer + 1
  12633.   03361 10FC            DJNZ    335FH           ;Repeat until B = 0
  12634.  
  12635.   03363 22F940          LD      (40F9H),HL      ;Save new end address
  12636.   03366 E1              POP     HL              ;Restore old end address
  12637.   03367 C1              POP     BC              ;Restore program pointer
  12638.   03368 ED42            SBC     HL,BC           ;Calcute the number of bytes
  12639.   0336A 23              INC     HL              ;that have to be moved
  12640.   0336B E5              PUSH    HL              ;Put the resulting blocksize
  12641.   0336C C1              POP     BC              ;into BC
  12642.   0336D E1              POP     HL              ;Restore program pointer
  12643.   0336E EB              EX      DE,HL           ;DE -> characters to be deleted
  12644.                                                 ;HL -> remaining program text
  12645.                                                 ;(following the characters to
  12646.                                                 ;be deleted)
  12647.   0336F EDB0            LDIR                    ;Move program text
  12648.   03371 E1              POP     HL              ;Restore registers
  12649.   03372 C1              POP     BC
  12650.   03373 D1              POP     DE
  12651.   03374 C9              RET
  12652.  
  12653.  
  12654. ; Insert B characters into program
  12655. ;
  12656. ; I: B = number of characters to be inserted
  12657. ;    HL -> program text
  12658. ;    (from (HL) onwards, B bytes are inserted in the program text)
  12659.  
  12660.   03375 D5              PUSH    DE              ;Save registers
  12661.   03376 C5              PUSH    BC
  12662.   03377 E5              PUSH    HL
  12663.   03378 2AF940          LD      HL,(40F9H)      ;HL -> end of program
  12664.   0337B E5              PUSH    HL              ;Copy pointer
  12665.   0337C D1              POP     DE              ;into DE
  12666.  
  12667.   0337D 23              INC     HL              ;End address + 1
  12668.   0337E 10FD            DJNZ    337DH           ;Repeat until B = 0
  12669.  
  12670.   03380 22F940          LD      (40F9H),HL      ;Save new end address
  12671.   03383 C1              POP     BC              ;Restore program text pointer
  12672.   03384 C5              PUSH    BC              ;and save it again
  12673.   03385 E5              PUSH    HL              ;Save end address
  12674.   03386 B7              OR      A               ;C-flag = 0
  12675.   03387 ED42            SBC     HL,BC           ;Calculate the number of
  12676.                                                 ;remaining characters upto
  12677.                                                 ;program end
  12678.   03389 E5              PUSH    HL              ;Put the resulting blocksize
  12679.   0338A C1              POP     BC              ;into BC
  12680.   0338B 03              INC     BC              ;Adjust blocksize + 1
  12681.   0338C E1              POP     HL              ;Restore new end address
  12682.   0338D EB              EX      DE,HL           ;Put it in DE
  12683.   0338E EDB8            LDDR                    ;Move program text
  12684.   03390 E1              POP     HL
  12685.   03391 C1              POP     BC
  12686.   03392 D1              POP     DE
  12687.   03393 C9              RET
  12688.  
  12689.  
  12690. ; Convert line number to ASCII format and put it in line buffer
  12691. ;
  12692. ; I: HL = line number
  12693. ; O: B = number of digits (line number length)
  12694. ;    HL -> line number in ASCII format
  12695.  
  12696.   03394 D5              PUSH    DE              ;Save DE
  12697.   03395 222141          LD      (4121H),HL      ;X = HL
  12698.   03398 010000          LD      BC,0000H        ;No formatting
  12699.   0339B 2AA740          LD      HL,(40A7H)      ;HL -> line buffer
  12700.   0339E E5              PUSH    HL              ;Save line buffer pointer
  12701.   0339F CD2F13          CALL    132FH           ;Decode number
  12702.   033A2 E1              POP     HL              ;Restore line buffer pointer
  12703.   033A3 0605            LD      B,05H           ;B = maximum length
  12704.  
  12705.   033A5 7E              LD      A,(HL)          ;A = character from line buffer
  12706.   033A6 D630            SUB     30H             ;Leading zero ?
  12707.   033A8 2005            JR      NZ,33AFH        ;No: done
  12708.                                                 ;Yes:
  12709.   033AA 23              INC     HL              ;Line buffer pointer + 1
  12710.   033AB 10F8            DJNZ    33A5H           ;Get next character
  12711.  
  12712.   033AD 2B              DEC     HL              ;All digits = '0': pointer - 1
  12713.   033AE 04              INC     B               ;Counter + 1 (1 digit)
  12714.  
  12715.   033AF D1              POP     DE              ;Restore DE
  12716.   033B0 C9              RET
  12717.  
  12718.  
  12719. ; Increment table pointer to next entry
  12720.  
  12721.   033B1 FD23            INC     IY
  12722.   033B3 FD23            INC     IY
  12723.   033B5 FD23            INC     IY
  12724.   033B7 FD23            INC     IY
  12725.   033B9 C9              RET
  12726.  
  12727.  
  12728. ; Search line from (HL) for GOTO, GOSUB etc.
  12729. ;
  12730. ; I: HL -> line text
  12731. ; O: A = token found
  12732. ;    Z-flag = 1 if no token found
  12733. ;    HL -> token found or end of line
  12734.  
  12735.   033BA 23              INC     HL              ;pointer + 1
  12736.   033BB 7E              LD      A,(HL)          ;A = char from program text
  12737.   033BC B7              OR      A               ;End of line ?
  12738.   033BD C8              RET     Z               ;Yes: return
  12739.  
  12740.   033BE FE8D            CP      8DH             ;Token for GOTO ?
  12741.   033C0 280C            JR      Z,33CEH         ;Yes: continue at 33CEH
  12742.  
  12743.   033C2 FE91            CP      91H             ;Token for GOSUB ?
  12744.   033C4 2808            JR      Z,33CEH
  12745.  
  12746.   033C6 FECA            CP      0CAH            ;Token for THEN ?
  12747.   033C8 2804            JR      Z,33CEH
  12748.  
  12749.   033CA FE95            CP      95H             ;Token for ELSE
  12750.   033CC 20EC            JR      NZ,33BAH        ;No: next character
  12751.  
  12752. ; Token found
  12753.  
  12754.   033CE 2B              DEC     HL              ;Was it a Colour BASIC token ?
  12755.   033CF 7E              LD      A,(HL)          ;Does the previous byte
  12756.   033D0 FEFF            CP      0FFH            ;equal FFH ?
  12757.   033D2 23              INC     HL              ;Put pointer back
  12758.   033D3 7E              LD      A,(HL)          ;And character back
  12759.  
  12760.   033D4 28E4            JR      Z,33BAH         ;Yes: continue search
  12761.  
  12762.   033D6 A7              AND     A               ;No: Z-flag = 0
  12763.   033D7 C9              RET
  12764.  
  12765.  
  12766. ; Determine length of line number found and put line number in line buffer
  12767. ;
  12768. ; I: HL -> line number (= token found + 1)
  12769. ; O: B = number of digits (line number length)
  12770. ;    DE -> line buffer (line number is in line buffer from (DE) onwards)
  12771. ;    HL -> next character after the line number
  12772. ;    Z-flag = 0 in case of digits found
  12773.  
  12774.   033D8 ED5BA740        LD      DE,(40A7H)      ;DE -> line buffer
  12775.   033DC D5              PUSH    DE              ;Save DE
  12776.   033DD 0600            LD      B,00H           ;Counter = 0
  12777.  
  12778.   033DF 7E              LD      A,(HL)          ;Get next character
  12779.   033E0 FE20            CP      20H             ;Space ?
  12780.   033E2 280B            JR      Z,33EFH         ;Yes: get next character
  12781.  
  12782.   033E4 FE30            CP      30H             ;Possible digit ?
  12783.   033E6 380A            JR      C,33F2H         ;No: done.
  12784.  
  12785.   033E8 FE3A            CP      3AH             ;Digit ?
  12786.   033EA 3006            JR      NC,33F2H        ;No: done.
  12787.  
  12788.   033EC 04              INC     B               ;Counter + 1
  12789.   033ED 12              LD      (DE),A          ;Store digit
  12790.   033EE 13              INC     DE              ;Buffer pointer + 1
  12791.  
  12792.   033EF 23              INC     HL              ;Line pointer + 1
  12793.   033F0 18ED            JR      33DFH           ;Loop
  12794.  
  12795.   033F2 AF              XOR     A               ;A = 00H
  12796.   033F3 12              LD      (DE),A          ;Mark line number end in line
  12797.                                                 ;buffer
  12798.   033F4 D1              POP     DE              ;Restore buffer address
  12799.   033F5 04              INC     B               ;Set Z-flag
  12800.   033F6 05              DEC     B               ;Z-flag = 0 when digits found
  12801.   033F7 C9              RET
  12802.  
  12803.  
  12804. ; Shift program text in such a way, that a new line number can be inserted
  12805. ; and then insert the new line number
  12806. ;
  12807. ; I: B = new line number length
  12808. ;    C = old line number length
  12809. ;    HL -> program text
  12810. ;    (new line number is inserted from (HL) onwards)
  12811. ;    DE -> new line number in ASCII format
  12812.  
  12813.   033F8 C5              PUSH    BC              ;Save lengths
  12814.   033F9 78              LD      A,B             ;A = new length
  12815.   033FA 99              SBC     A,C             ;- old length
  12816.   033FB 2810            JR      Z,340DH         ;Insert new line number when
  12817.                                                 ;both lengths are identical
  12818.   033FD 05              DEC     B               ;New length - 1
  12819.   033FE 2808            JR      Z,3408H         ;Old length > new length:
  12820.                                                 ;continue at 3408H
  12821.   03400 0D              DEC     C               ;Old length - 1
  12822.   03401 20F6            JR      NZ,33F9H        ;New length > old length:
  12823.                                                 ;continue at 33F9H
  12824.   03403 CD7533          CALL    3375H           ;Insert character
  12825.   03406 1805            JR      340DH           ;And transfer line number
  12826.  
  12827. ; Old length is bigger than new length
  12828.  
  12829.   03408 41              LD      B,C             ;B = remaining length
  12830.   03409 05              DEC     B               ;B - 1
  12831.   0340A CD5433          CALL    3354H           ;Delete B characters
  12832.   0340D C1              POP     BC              ;Restore lengths
  12833.  
  12834.   0340E 1A              LD      A,(DE)          ;Transfer digit from line
  12835.   0340F 77              LD      (HL),A          ;buffer to program text
  12836.   03410 13              INC     DE              ;Line buffer pointer + 1
  12837.   03411 23              INC     HL              ;Program text pointer + 1
  12838.   03412 10FA            DJNZ    340EH           ;Repeat until B = 0
  12839.  
  12840.   03414 C9              RET
  12841.  
  12842.  
  12843. ; Get key from keyboard and check for FKEY (see 3000H)
  12844.  
  12845.   03415 CD4900          CALL    0049H           ;Wait for key pressed
  12846.   03418 FE5C            CP      5CH             ;Possible FKEY ?
  12847.   0341A 383C            JR      C,3458H         ;No: continue at 3458H
  12848.                                                 ;Yes:
  12849.   0341C FE60            CP      60H             ;Is an FKEY ?
  12850.   0341E 3038            JR      NC,3458H        ;No: continue at 3458H
  12851.  
  12852.   03420 D65B            SUB     5BH             ;A = FKEY number 1..4
  12853.  
  12854.  
  12855. ; Transfer FKEY text into line buffer
  12856. ; A = FKEY code (01 to 08 for FKEY1 to FKEY8)
  12857.  
  12858.   03422 57              LD      D,A             ;D = FKEY code
  12859.   03423 78              LD      A,B             ;A = maximum number of
  12860.                                                 ;characters (see 05D9H)
  12861.   03424 FE07            CP      07H             ;Are 7 characters still
  12862.                                                 ;allowed ?
  12863.   03426 38ED            JR      C,3415H         ;No: ignore FKEY and get next
  12864.                                                 ;key input
  12865.   03428 7A              LD      A,D             ;FKEY code back into A
  12866.   03429 C5              PUSH    BC              ;Save counter
  12867.   0342A E5              PUSH    HL              ;Save buffer pointer
  12868.  
  12869. ; Set HL as pointer to FKEY definition string belonging to FKEY pressed
  12870.  
  12871.   0342B 214943          LD      HL,4349H        ;HL -> FKEY definition strings
  12872.                                                 ;in system RAM - length of
  12873.                                                 ;one string
  12874.   0342E 110700          LD      DE,0007H        ;DE = length of FKEY string
  12875.  
  12876.   03431 19              ADD     HL,DE           ;HL -> next string
  12877.   03432 3D              DEC     A               ;Proper definition string ?
  12878.   03433 20FC            JR      NZ,3431H        ;No: next
  12879.  
  12880.   03435 D1              POP     DE              ;Restore buffer pointer
  12881.   03436 0607            LD      B,07H           ;B = length of FKEY string
  12882.  
  12883.   03438 7E              LD      A,(HL)          ;A = character of FKEY string
  12884.   03439 FE00            CP      00H             ;Is it a 00H (= execute a CR) ?
  12885.   0343B 2812            JR      Z,344FH         ;Yes, execute FKEY command
  12886.  
  12887.   0343D 12              LD      (DE),A          ;Store character
  12888.   0343E D5              PUSH    DE              ;Save buffer pointer
  12889.   0343F CD3300          CALL    0033H           ;Output byte
  12890.   03442 D1              POP     DE              ;Restore buffer pointer
  12891.   03443 23              INC     HL              ;Next position in FKEY string
  12892.   03444 13              INC     DE              ;Buffer pointer + 1
  12893.   03445 10F1            DJNZ    3438H           ;Process next
  12894.  
  12895.   03447 EB              EX      DE,HL           ;HL = buffer pointer
  12896.   03448 C1              POP     BC              ;Restore counter
  12897.   03449 78              LD      A,B             ;A = counter
  12898.   0344A D607            SUB     07H             ;Subtract 7 characters
  12899.   0344C 47              LD      B,A             ;Counter back in B
  12900.   0344D 18C6            JR      3415H           ;Wait for next key press
  12901.  
  12902. ; Insert a 'RETURN' to execute FKEY command
  12903.  
  12904.   0344F EB              EX      DE,HL           ;HL = buffer pointer
  12905.   03450 78              LD      A,B             ;A = remaining length of FKEY
  12906.                                                 ;text
  12907.   03451 C1              POP     BC              ;Restore counter
  12908.   03452 80              ADD     A,B             ;A = maximum number of
  12909.                                                 ;characters + remaining length
  12910.   03453 D607            SUB     07H             ;Subtract 7 characters
  12911.   03455 47              LD      B,A             ;New counter back in B
  12912.   03456 3E0D            LD      A,0DH           ;A = 'RETURN'
  12913.  
  12914. ; <SHIFT>+<FKEY> pressed ? (FKEY5 to FKEY8)
  12915.  
  12916.   03458 FE7C            CP      7CH             ;Possible FKEY pressed ?
  12917.   0345A DA0330          JP      C,3003H         ;No: continue at 3003H
  12918.  
  12919.   0345D FE80            CP      80H             ;FKEY pressed ?
  12920.   0345F D20330          JP      NC,3003H        ;No: continue at 3003H
  12921.  
  12922.   03462 D677            SUB     77H             ;A = FKEY code (05H to 08H)
  12923.   03464 18BC            JR      3422H           ;Continue at 3422H
  12924.  
  12925.  
  12926. ; FKEY statement
  12927. ; --------------
  12928.  
  12929.   03466 2B              DEC     HL              ;PTP - 1
  12930.   03467 D7              RST     10H             ;Get FKEY number
  12931.                                                 ;Digit ?
  12932.   03468 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  12933.  
  12934.   0346B D630            SUB     30H             ;Convert character to number
  12935.   0346D F5              PUSH    AF              ;Save FKEY number
  12936.   0346E 23              INC     HL              ;PTP + 1
  12937.   0346F CF              RST     08H             ;Next character must be
  12938.   03470 D5              DEFB    0D5H            ;the token for '='
  12939.   03471 CF              RST     08H             ;Next character must be
  12940.   03472 22              DEFB    22H             ;double quotes
  12941.   03473 F1              POP     AF              ;A = FKEY number
  12942.   03474 FE01            CP      01              ;FKEY number < 1
  12943.   03476 DA4A1E          JP      C,1E4AH         ;Yes: ?FC Error
  12944.  
  12945.   03479 FE09            CP      09H             ;FKEY number > 8
  12946.   0347B 30EB            JR      NC,3468H        ;Yes: ?FC Error
  12947.  
  12948.   0347D E5              PUSH    HL              ;Save PTP
  12949.   0347E 214943          LD      HL,4349H        ;HL -> FKEY definitions - 7
  12950.   03481 110700          LD      DE,0007H        ;DE = 7 characters length
  12951.   03484 19              ADD     HL,DE           ;HL -> FKEY definition
  12952.   03485 3D              DEC     A               ;FKEY number - 1
  12953.                                                 ;Down to 0 ?
  12954.   03486 20FC            JR      NZ,3484H        ;No: next FKEY text
  12955.  
  12956.   03488 EB              EX      DE,HL           ;DE -> proper FKEY definition
  12957.   03489 E1              POP     HL              ;HL = PTP
  12958.   0348A 2B              DEC     HL              ;PTP - 1
  12959.  
  12960.   0348B 0607            LD      B,07H           ;7 characters definition length
  12961.  
  12962.   0348D D7              RST     10H             ;Get next character
  12963.   0348E FE22            CP      22H             ;Is it double quotes ?
  12964.   03490 280C            JR      Z,349EH         ;Yes: end of new definition
  12965.  
  12966.   03492 FE00            CP      00H             ;Insert 'RETURN'
  12967.   03494 280A            JR      Z,34A0H         ;Yes: copy 00H and finish
  12968.  
  12969.   03496 12              LD      (DE),A          ;Copy into FKEY definition
  12970.   03497 13              INC     DE              ;Pointer + 1
  12971.  
  12972.   03498 10F3            DJNZ    348DH           ;Next character
  12973.  
  12974.   0349A 23              INC     HL              ;PTP + 1
  12975.   0349B CF              RST     08H             ;Next character must be
  12976.   0349C 22              DEFB    22H             ;double quotes
  12977.   0349D C9              RET
  12978.  
  12979. ; End of text reached: fill remaining characters with spaces
  12980.  
  12981.   0349E 3E20            LD      A,' '           ;A = space
  12982.  
  12983.   034A0 12              LD      (DE),A          ;Copy into FKEY definition
  12984.  
  12985.   034A1 05              DEC     B               ;Next position in definition
  12986.                                                 ;All done ?
  12987.   034A2 2803            JR      Z,34A7H         ;Yes: continue ar 34A7H
  12988.  
  12989.   034A4 13              INC     DE              ;Pointer + 1
  12990.   034A5 18F9            JR      34A0H           ;Loop
  12991.  
  12992.   034A7 B7              OR      A               ;A = 00H ?
  12993.   034A8 C8              RET     Z               ;Yes: done
  12994.  
  12995.   034A9 18F0            JR      349BH           ;Test on double quotes
  12996.  
  12997.  
  12998. ; Default definition for FKEY 1 to 8. (This table is copied into system RAM
  12999. ; from 4350H onwards)
  13000.  
  13001.   034AB 4C495354202020  DEFB    'LIST   '       ;FKEY 1 = "LIST   "
  13002.   034B2 52554E20202020  DEFB    'RUN    '       ;FKEY 2 = "RUN    "
  13003.   034B9 4155544F202020  DEFB    'AUTO   '       ;FKEY 3 = "AUTO   "
  13004.   034C0 45444954202020  DEFB    'EDIT   '       ;FKEY 4 = "EDIT   "
  13005.   034C7 52454E554D2020  DEFB    'RENUM  '       ;FKEY 5 = "RENUM  "
  13006.   034CE 53595354454D00  DEFB    'SYSTEM',00H    ;FKEY 6 = "SYSTEM
  13007.   034D5 434C4F41442020  DEFB    'CLOAD  '       ;FKEY 7 = "CLOAD  "
  13008.   034DC 43534156452022  DEFB    'CSAVE "'       ;FKEY 8 = "CSAVE ""
  13009.  
  13010.  
  13011. ; &H
  13012.  
  13013.   034E3 D7              RST     10H             ;Get character following &
  13014.   034E4 FE48            CP      48H             ;Is it 'H' (hexadecimal)
  13015.   034E6 203D            JR      NZ,3528H        ;No: test for &O
  13016.  
  13017. ; Decode hexadecimal constant
  13018.  
  13019.   034E8 23              INC     HL              ;PTP + 1
  13020.   034E9 CD1635          CALL    34F3H           ;DE = constant
  13021.   034EC E5              PUSH    HL              ;Save PTP
  13022.   034ED EB              EX      DE,HL           ;HL = constant
  13023.   034EE CD9A0A          CALL    0A9AH           ;Write HL to X as INT
  13024.   034F1 E1              POP     HL              ;Restore PTP
  13025.   034F2 C9              RET
  13026.  
  13027. ; DE = &H (HL)
  13028.  
  13029.   034F3 110000          LD      DE,0000H        ;Result = 0
  13030.   034F6 CD0135          CALL    3501H           ;Decode MSB
  13031.   034F9 57              LD      D,A             ;D = MSB
  13032.   034FB CD0135          CALL    3501H           ;Decode LSB
  13033.   034FE 5F              LD      E,A             ;E = LSB
  13034.   034FF 23              INC     HL              ;PTP + 1
  13035.   03500 C9              RET
  13036.  
  13037. ; A = &H (HL)
  13038.  
  13039.   03501 CD0F35          CALL    350FH           ;Decode nibble
  13040.   03504 07              RLCA                    ;Shift bits 4-7
  13041.   03505 07              RLCA
  13042.   03506 07              RLCA
  13043.   03507 07              RLCA
  13044.   03508 23              INC     HL              ;PTP + 1
  13045.   03509 47              LD      B,A             ;Temporary result in B
  13046.   0350A CD0F35          CALL    350FH           ;Decode second nibble
  13047.   0350D 80              ADD     A,B             ;And add previous result
  13048.   0350E C9              RET
  13049.  
  13050. ; Decode a hexadecimal digit
  13051.  
  13052.   0350F 7E              LD      A,(HL)          ;A = character
  13053.   03510 D630            SUB     '0'             ;Character < '0'
  13054.   03512 DA4A1E          JP      C,1E4AH         ;Yes: ?FC Error
  13055.   03515 FE0A            CP      10H             ;Character > '9'
  13056.   03517 D8              RET     C               ;No: value ok, done
  13057.  
  13058.   03518 D607            SUB     07H             ;Character < 'A'
  13059.   0351A DA4A1E          JP      C,1E4AH         ;Yes: ?FC Error
  13060.   0351D FE10            CP      10H             ;Character > 'F'
  13061.   0351F D24A1E          JP      NC,1E4AH        ;Yes: ?FC Error
  13062.   03522 C9              RET
  13063.  
  13064.  
  13065. ; &O
  13066.  
  13067.   03523 FE4F            CP      4FH             ;Is it 'O' (octal)
  13068.   03525 C29719          JP      NZ,1997H        ;No: ?SN Error
  13069.  
  13070. ; Decode octal constant
  13071.  
  13072.   03528 110000          LD      DE,0000H        ;Result = 0
  13073.   0352B 23              INC     HL              ;PTP + 1
  13074.   0352C CD4535          CALL    3545H           ;Decode highest octal digit
  13075.   0352F 07              RLCA                    ;Shift free bits 0-2
  13076.   03530 07              RLCA
  13077.   03531 07              RLCA
  13078.   03532 23              INC     HL              ;PTP + 1
  13079.   03533 47              LD      B,A             ;Temporary result in B
  13080.   03534 CD4535          CALL    3545H           ;Decode next octal digit
  13081.   03537 80              ADD     A,B             ;Add previous result
  13082.   03538 07              RLCA                    ;Shift free bits 0-2
  13083.   03539 07              RLCA
  13084.   0353A 07              RLCA
  13085.   0353B 23              INC     HL              ;PTP + 1
  13086.   0353C 47              LD      B,A             ;Temporary result in B
  13087.   0353D CD4535          CALL    3545H           ;Decode last octal digit
  13088.   03540 80              ADD     A,B             ;Add previous result
  13089.   03541 5F              LD      E,A             ;LSB value into E
  13090.   03542 23              INC     HL              ;PTP + 1
  13091.   03543 18A7            JR      34ECH           ;Write HL to X as INT
  13092.  
  13093. ; Decode highest octal digit ('0' - '3')
  13094.  
  13095.   03545 7E              LD      A,(HL)          ;A = character
  13096.   03546 D630            SUB     '0'             ;Character < '0'
  13097.   03548 380D            JR      C,3557H         ;Yes: ?FC Error
  13098.   0354A FE04            CP      04H             ;Character < '4'
  13099.   0354C D8              RET     C               ;Yes: value ok, done
  13100.   0354D 1808            JR      3557H           ;No: ?FC Error
  13101.  
  13102. ; Decode lower octal digit ('0' - '7')
  13103.  
  13104.   0354F 7E              LD      A,(HL)          ;A = character
  13105.   03550 D630            SUB     '0'             ;Character < '0'
  13106.   03552 3803            JR      C,3557H         ;Yes: ?FC Error
  13107.   03554 FE08            CP      08H             ;Character < '8'
  13108.   03556 D8              RET     C               ;Yes: value ok, done
  13109.   03557 C34A1E          JP      1E4AH           ;No: ?FC Error
  13110.  
  13111.  
  13112. ; CALL statement
  13113. ; --------------
  13114.  
  13115.   0355A CDFE34          CALL    34F3H           ;DE = address following CALL
  13116.   0355D E5              PUSH    HL              ;Save PTP
  13117.   0355E EB              EX      DE,HL           ;HL = address of routine
  13118.   0355F 116735          LD      DE,3567H        ;Set return address for routine
  13119.   03562 D5              PUSH    DE              ;that is called to 3567H
  13120.   03563 E9              JP      (HL)            ;activate routine
  13121.  
  13122.   03564 FF              RST     38H             ;--
  13123.   03565 FF              RST     38H
  13124.   03566 FF              RST     38H
  13125.  
  13126. ; Return from CALL-routine
  13127.  
  13128.   03567 E1              POP     HL              ;Restore PTP
  13129.   03568 C9              RET
  13130.  
  13131.  
  13132. ; Translation table for colour codes (is copied into system RAM from 4390H
  13133. ; onwards). With the first Colour Genies, the COLOUR value was directly
  13134. ; written into the colour-RAM (from F000H onwards).
  13135. ; Due to hardware changes in later models, colour codes changed. In order to
  13136. ; keep it compatible this table is used to regain the original colour settings.
  13137. ; This table indicates for every COLOUR value, which value has to be
  13138. ; written into the colour-RAM in order to regain the old order.
  13139. ;
  13140. ;                    Value in colour-RAM        COLOUR value
  13141.  
  13142.   03569 03              DEFB    03H             ;     1
  13143.   0356A 05              DEFB    05H             ;     2
  13144.   0356B 02              DEFB    02H             ;     3
  13145.   0356C 04              DEFB    04H             ;     4
  13146.   0356D 06              DEFB    06H             ;     5
  13147.   0356E 08              DEFB    08H             ;     6
  13148.   0356F 01              DEFB    01H             ;     7
  13149.   03560 0E              DEFB    0EH             ;     8
  13150.   03561 09              DEFB    09H             ;     9
  13151.   03572 10              DEFB    10H             ;    10
  13152.   03573 07              DEFB    07H             ;    11
  13153.   03574 0B              DEFB    0BH             ;    12
  13154.   03575 0C              DEFB    0CH             ;    13
  13155.   03576 0D              DEFB    0DH             ;    14
  13156.   03577 0A              DEFB    0AH             ;    15
  13157.   03578 0F              DEFB    0FH             ;    16
  13158.  
  13159.  
  13160. ; Print text from (HL) onwards and generate sound (with error message)
  13161.  
  13162.   03579 CDA728          CALL    28A7H           ;Print text
  13163.   0357C 110900          LD      DE,0009H        ;Set various values to write
  13164.   0357F D5              PUSH    DE              ;into the PSG
  13165.   03580 110008          LD      DE,0800H
  13166.   03583 D5              PUSH    DE
  13167.   03584 113E10          LD      DE,103EH
  13168.   03587 D5              PUSH    DE
  13169.   03588 117800          LD      DE,0078H        ;DE = PSG value
  13170.   0358B 3E01            LD      A,01H           ;Write 0078H in frequency reg
  13171.   0358D CD2A3E          CALL    3E2AH           ;of channel A
  13172.   03590 D1              POP     DE
  13173.   03591 3E08            LD      A,08H           :Set amplitude of channel A to
  13174.                                                 ;envelope shape use and
  13175.   03593 CD2A3E          CALL    3E2AH           ;switch on channel A
  13176.   03596 D1              POP     DE
  13177.   03597 3E0C            LD      A,0CH           ;Amplitude channel B = 8
  13178.   03599 CD2A3E          CALL    3E2AH           ;Amplitude channel C = 0
  13179.   0359C D1              POP     DE
  13180.   0359D 3E0D            LD      A,0DH
  13181.   0359F C3323E          JP      3E32H           ;Set envelope and return
  13182.  
  13183.  
  13184. ; SUB for SHAPE (see 3CF2H).
  13185. ; Load HL with the start address of the SHAPE table
  13186. ;
  13187. ; I:
  13188. ; O: HL -> start of SHAPE table
  13189.  
  13190.   035A2 2AB140          LD      HL,(40B1H)      ;HL = TOPMEM
  13191.   035A5 23              INC     HL              ;HL -> start of SHAPE table
  13192.   035A6 C3F53C          JP      3CF5H           ;Continue at 3CF5H
  13193.  
  13194.  
  13195. ; SUB for PRINT#
  13196.  
  13197.   035A9 CDB535          CALL    35B5H           ;Decode number
  13198.   035AC C33F02          JP      023FH           ;Write leader and sync
  13199.  
  13200.  
  13201. ; SUB for INPUT#
  13202.  
  13203.   035AF CDB535          CALL    35B5H           ;Decode number
  13204.   035B2 C34C02          JP      024CH           ;Search for leader and sync
  13205.  
  13206.  
  13207. ; Decode number at PRINT# and INPUT#
  13208.  
  13209.   035B5 AF              XOR     A               ;A = 0
  13210.   035B6 CD012B          CALL    2B01H           ;DE = number
  13211.   035B9 CF              RST     08H             ;Next character must be
  13212.   035BA 2C              DEFB    ','             ;a comma
  13213.   035BB 7B              LD      A,E             ;A = LSB
  13214.   035BC A2              AND     D               ;AND with MSB. MSB must be
  13215.                                                 ;FFH because a negative number
  13216.                                                 ;is required.
  13217.   035BD C602            ADD     A,02H           ;Number was -1 or -2 ?
  13218.   035BF D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  13219.  
  13220.   035C2 C9              RET
  13221.  
  13222.  
  13223. ; Set volume of indicated channel to 0 (not used)
  13224. ; Is the value of the argument between 1 and 3, then the corresponding PSG
  13225. ; channel volume will be set to 0. If no argument is given, the volume of
  13226. ; all PSG channels will be set to 0.
  13227. ; This routine can be used by redirecting e.g. the KILL command
  13228. ; (so the command will be KILL n or KILL).
  13229. ; POKE, &H4192,195 : POKE &H4193,53
  13230. ;
  13231. ; I: HL = PTP on argument or command/line end
  13232.  
  13233.  
  13234.   035C3 2B              DEC     HL              ;PTP -1
  13235.   035C4 D7              RST     10H             ;Channel number provided ?
  13236.   035C5 281A            JR      Z,35E1H         ;No: continue at 35E1H
  13237.  
  13238.   035C7 CD1C2B          CALL    2B1CH           ;A = Channel number
  13239.   035CA 0608            LD      B,08H           ;Register = 8
  13240.   035CC FE01            CP      01H             ;Channel 1 ?
  13241.   035CE 280B            JR      Z,35DBH         ;Yes: register number is ok
  13242.  
  13243.   035D0 04              INC     B               ;Register = 9
  13244.   035D1 FE02            CP      02H             ;Channel 2 ?
  13245.   035D3 2806            JR      Z,35DBH         ;Yes: register number is ok
  13246.  
  13247.   035D5 04              INC     B               ;Register = 10
  13248.   035D6 FE03            CP      03H             ;Channel 3 ?
  13249.   035D8 C24A1E          JP      NZ,1E4AH        ;No: ?FC Error
  13250.  
  13251.   035DB 78              LD      A,B             ;A = register number
  13252.   035DC 1E00            LD      E,00H           ;E = 00H
  13253.   035DE C3323E          JP      3E32H           ;Write to PSG (SOUND A,E)
  13254.  
  13255.  
  13256. ; No channel number provided: set volume of all channels to 0.
  13257.  
  13258.   035E1 3E0A            LD      A,0AH           ;Switch off channel 2 and 3
  13259.   035E3 110000          LD      DE,0000H
  13260.   035E6 CD2A3E          CALL    3E2AH
  13261.   035E9 3E08            LD      A,08H           ;Switch off channel 1
  13262.   035EB C3323E          JP      3E32H
  13263.  
  13264.  
  13265. ; SUB for screen output
  13266. ; Write character in A at (HL) and update screen colour
  13267. ;
  13268. ; I: A = character
  13269. ;    HL -> screen position
  13270.  
  13271.   035EE 77              LD      (HL),A          ;Write character
  13272.   035EF CD7A30          CALL    307AH           ;Update character colour
  13273.   035F2 23              INC     HL              ;Next position
  13274.   035F3 CD7A30          CALL    307AH           ;Update character colour
  13275.   035F6 2B              DEC     HL              ;Back to original position
  13276.   035F7 C9              RET
  13277.  
  13278.  
  13279. ; SUB for screen output
  13280. ; Update the colour of a screen location addressed by HL
  13281. ;
  13282. ; I: HL -> screen location of character in LGR screen memory
  13283.  
  13284.   035F8 1100AC          LD      DE,0AC00H       ;DE = difference between
  13285.                                                 ;LGR screen memory (4400H)
  13286.                                                 ;and corresponding colour
  13287.                                                 ;memory (0F000H)
  13288.   035FB FE20            CP      ' '             ;Space character ?
  13289.   035FD C28030          JP      NZ,3080H        ;No: set colour according
  13290.                                                 ;to current colour setting
  13291.  
  13292. ; Spaces get the first colour in the colour code table
  13293.  
  13294.   03600 3A9043          LD      A,(4390H)       ;A = first entry in colour code
  13295.                                                 ;table
  13296.   03603 19              ADD     HL,DE           ;Set pointer into colour mem
  13297.   03604 C38F30          JP      308FH           ;Set colour
  13298.  
  13299.  
  13300. ; SUB for 'cursor one line down'
  13301.  
  13302.   03607 112800          LD      DE,0028H        ;DE = 40 (characters per line)
  13303.   0360A 3E20            LD      A,20H           ;Character = ' '
  13304.   0360C C37A30          JP      307AH           ;Output space (delete cursor)
  13305.  
  13306.  
  13307. ; SUB for 'cursor one line up'
  13308.  
  13309.   0360F 11D8FF          LD      DE,0FFD8H       ;DE = -40
  13310.   03612 18F6            JR      360AH           ;Continue at 360AH
  13311.  
  13312.  
  13313. ; SUB for 'cursor on/off'
  13314.  
  13315.   03614 2002            JR      NZ,3618H        ;Continue at 3618H (see 306EH)
  13316.  
  13317.   03616 3E20            LD      A,20H           ;--
  13318.   03618 C37A30          JP      307AH           ;Output character in A
  13319.  
  13320.  
  13321. ; SUB for 'Carriage Return'
  13322.  
  13323.   0361B CD0A36          CALL    360AH           ;Delete cursor character
  13324.   0361E C36531          JP      3165H           ;Continue at 3165H
  13325.  
  13326.  
  13327. ; SUB for COLOUR change (see 3017H)
  13328.  
  13329.   03621 322340          LD      (4023H),A       ;Save new COLOUR value
  13330.   03624 2A2040          LD      HL,(4020H)      ;HL = actual cursor address
  13331.   03627 C35F30          JP      305FH           ;Switch on cursor
  13332.  
  13333.  
  13334. ; SUB for PRINT@ (see 2086H)
  13335. ; Calculate new screen-POS
  13336. ;
  13337. ; I: DE = @ argument
  13338. ; O: A = POS value
  13339.  
  13340.   0362A E5              PUSH    HL              ;Save HL
  13341.   0362B EB              EX      DE,HL           ;HL = @ argument
  13342.   0362C 112800          LD      DE,0028H        ;DE = 40 (length of 1 line)
  13343.   0362F 19              ADD     HL,DE           ;Add line length
  13344.   03630 B7              OR      A               ;C-flag = 0
  13345.   03631 ED52            SBC     HL,DE           ;Subtract line length until
  13346.   03633 30FC            JR      NC,3631H        ;result becomes negative
  13347.  
  13348.   03635 19              ADD     HL,DE           ;Compensate last subtract
  13349.   03636 7D              LD      A,L             ;A = L = actual POS
  13350.   03637 E1              POP     HL              ;Restore HL
  13351.   03638 C9              RET
  13352.  
  13353.  
  13354. ; SUB for scroll (see 3196H)
  13355.  
  13356.   03639 D9              EXX                     ;Switch registers
  13357.   0363A F1              POP     AF              ;RET-address to AF
  13358.   0363B C5              PUSH    BC              ;Save registers
  13359.   0363C D5              PUSH    DE
  13360.   0363D E5              PUSH    HL
  13361.   0363E 1100F0          LD      DE,0F000H       ;Prepare second register set
  13362.   03641 2128F0          LD      HL,0F028H       ;for scroll of colour memory
  13363.   03644 01C003          LD      BC,03C0H
  13364.   03647 C5              PUSH    BC              ;Save BC as counter
  13365.   03648 D9              EXX                     ;Switch back registers
  13366.   03649 110044          LD      DE,4400H        ;Prepare first register set
  13367.   0364C 212844          LD      HL,4428H        ;for scroll of LGR screen mem.
  13368.   0364F C1              POP     BC              ;Restore counter
  13369.   03650 F5              PUSH    AF              ;RET address back on stack
  13370.   03651 C9              RET
  13371.  
  13372.  
  13373. ; JOY statement
  13374. ; -------------
  13375.  
  13376.   03652 7E              LD      A,(HL)          ;A = char. from program text
  13377.   03653 23              INC     HL              ;PTP + 1
  13378.   03654 FEDB            CP      0DBH            ;Token for INP ?
  13379.   03656 CAC536          JP      Z,36C5H         ;Yes: continue at 36C5H
  13380.  
  13381.   03659 FEA0            CP      0A0H            ;Token for OUT ?
  13382.   0365B CAB336          JP      Z,36B3H         ;Yes: continue at 36B3H
  13383.  
  13384.   0365E C39719          JP      1997H           ;?SN Error
  13385.  
  13386.  
  13387. ; COLOUR function (right of = sign)
  13388. :
  13389. ; *** WARNING: NEVER USE THIS! ***
  13390. ; It can lead to a system crash or total program destruction
  13391. ; If you want to perform this function do a PEEK(&H4023) + 1. Much safer....
  13392.  
  13393.   03661 3A2340          LD      A,(4023H)       ;A = colour value
  13394.   03664 3C              INC     A               ;Adjust to get proper value
  13395.                                                 ;as with COLOUR n (1-16)
  13396.                                                 ;Colour value zero ?
  13397.   03665 CA0D37          JP      Z,370DH         ;Yes: continue at 370DH
  13398.  
  13399.                                                 ;Note: It would have been ok
  13400.                                                 ;if the jump would have been
  13401.                                                 ;unconditional! :-(
  13402.  
  13403.   03668 B8              CP      B               ;Compare with what ?
  13404.   03669 77              LD      (HL),A          ;Write A using PTP! Yikes!!!
  13405.  
  13406.  
  13407.  
  13408. ; Read a BASIC program from I/O port (JOYINP)
  13409.  
  13410.   0366A 2AA440          LD      HL,(40A4H)      ;HL -> start of user BASIC RAM
  13411.   0366D 2B              DEC     HL              ;Pointer - 1
  13412.  
  13413.   0366E 0603            LD      B,03H           ;B = counter
  13414.  
  13415.   03670 E5              PUSH    HL              ;Save pointer
  13416.   03671 260F            LD      H,0FH           ;Select register 15 (port B)
  13417.  
  13418.   03673 CDBB3A          CALL    3ABBH           ;A = PSG port B
  13419.   03676 CB57            BIT     2,A             ;Strobe signal high ?
  13420.   03678 28F9            JR      Z,3673H         ;No, wait for strobe pulse
  13421.  
  13422.   0367A CDBB3A          CALL    3ABBH           ;A = contents of PSG register
  13423.   0367D CB57            BIT     2,A             ;Stobe signal low again ?
  13424.   0367F 20F9            JR      NZ,367AH        ;No, wait for strobe pulse end
  13425.  
  13426.   03681 260E            LD      H,0EH           ;Select register 14 (port A)
  13427.   03683 CDBB3A          CALL    3ABBH           ;A = PSG port A
  13428.   03686 E1              POP     HL              ;Restore pointer
  13429.   03687 77              LD      (HL),A          ;Save in memory
  13430.   03688 B7              OR      A               ;Set flags
  13431.   03689 23              INC     HL              ;Update memory pointer
  13432.                                                 ;A = zero ?
  13433.   0368A 20E2            JR      NZ,366EH        ;No: next character of program
  13434.                                                 ;3 consecutive zeros ?
  13435.   0368C 10E2            DJNZ    3670H           ;No: continue read
  13436.  
  13437.   0368E C9              RET
  13438.  
  13439.  
  13440. ; Write a BASIC program to I/O port (JOYOUT)
  13441.  
  13442.   0368F 2AA440          LD      HL,(40A4H)      ;HL -> start of user BASIC RAM
  13443.   03692 2B              DEC     HL              ;Pointer - 1
  13444.  
  13445.   03693 ED5BF940        LD      DE,(40F9H)      ;DE -> end of program text + 2
  13446.   03697 7E              LD      A,(HL)          ;Get byte from program text
  13447.   03698 E5              PUSH    HL              ;Save pointer
  13448.   03699 6F              LD      L,A             ;Data in L
  13449.   0369A 260E            LD      H,0EH           ;Use PSG port A register
  13450.   0369C CDB23A          CALL    3AB2H           ;Output data by port A
  13451.   0369F 24              INC     H               ;Use PSG port B register
  13452.   036A0 3E04            LD      A,04H           ;Start strobe pulse
  13453.   036A2 CDB23A          CALL    3AB2H
  13454.   036A5 060A            LD      B,0AH           ;B = delay time
  13455.   036A7 10FE            DJNZ    36A7H           ;Wait
  13456.  
  13457.   036A9 AF              XOR     A               ;End strobe pulse
  13458.   036AA CDB23A          CALL    3AB2H           ;Output A
  13459.   036AD E1              POP     HL              ;Restore pointer
  13460.   036AE 23              INC     HL              ;Pointer + 1
  13461.   036AF DF              RST     18H             ;End of program reached ?
  13462.   036B0 20E5            JR      NZ,3697H        ;No, send next byte of program
  13463.  
  13464.   036B2 C9              RET
  13465.  
  13466.  
  13467. ; JOYOUT
  13468. ; ------
  13469.  
  13470.   036B3 E5              PUSH    HL              ;Save PTP
  13471.   036B4 2607            LD      H,07H           ;Select PSG enable register
  13472.   036B6 CDBB3A          CALL    3ABBH           ;A = contents of PSG register
  13473.   036B9 E63F            AND     3FH             ;Set port A and B to output
  13474.   036BB F6C0            OR      0C0H
  13475.   036BD CDB23A          CALL    3AB2H           ;Write to PSG enable register
  13476.   036C0 CD8F36          CALL    368FH           ;Write BASIC program
  13477.   036C3 E1              POP     HL              ;Restore PTP
  13478.   036C4 C9              RET
  13479.  
  13480.  
  13481. ; JOYINP
  13482. ; ------
  13483.  
  13484.   036C5 3E07            LD      A,07H           ;Select PSG enable register
  13485.   036C7 D3F8            OUT     (0F8H),A
  13486.   036C9 3E3F            LD      A,3FH           ;Set Port A and B to input
  13487.   036CB D3F9            OUT     (0F9H),A
  13488.   036CD CD6A36          CALL    366AH           ;Read BASIC program
  13489.   036D0 C3772C          JP      2C77H           ;Renew all pointers in program
  13490.  
  13491.  
  13492. ; SWAP statement
  13493. ; --------------
  13494.  
  13495.   036D3 CD0D26          CALL    260DH           ;Find or create 1st variable
  13496.   036D6 D5              PUSH    DE              ;Save pointer on 1st variable
  13497.   036D7 3AAF40          LD      A,(40AFH)       ;A = variable type of X
  13498.   036DA F5              PUSH    AF              ;Save it
  13499.   036DB CF              RST     08H             ;Next character must be
  13500.   036DC 2C              DEFB    ','             ;a comma
  13501.   036DD CD0D26          CALL    260DH           ;Find or create 2nd variable
  13502.   036E0 C1              POP     BC              ;Restore 1st variable type
  13503.   036E1 3AAF40          LD      A,(40AFH)       ;Take variable type of X
  13504.   036E4 B8              CP      B               ;Both variables same type ?
  13505.   036E5 C24A1E          JP      NZ,1E4AH        ;No: ?FC Error
  13506.  
  13507. ; Now swap all bytes from both variables. The number of bytes to swap is
  13508. ; indicated by the VT in B
  13509.  
  13510.   036E8 E3              EX      (SP),HL         ;HL -> 1st variable
  13511.   036E9 4E              LD      C,(HL)          ;C = byte from 1st var
  13512.   036EA 1A              LD      A,(DE)          ;A = byte from 2nd var
  13513.   036EB 77              LD      (HL),A          ;Byte from 2nd var in 1st var
  13514.   036EC 79              LD      A,C             ;A = byte from 1st var
  13515.   036ED 12              LD      (DE),A          ;Byte from 1st var in 2nd var
  13516.   036EE 23              INC     HL              ;Pointer on 1st var + 1
  13517.   036EF 13              INC     DE              ;Pointer on 2nd var + 1
  13518.   036F0 10F7            DJNZ    36E9H           ;Until all var bytes swapped
  13519.  
  13520.   036F2 E1              POP     HL              ;Restore PTP
  13521.   036F3 C9              RET
  13522.  
  13523.  
  13524. ; X = SOUND(register)
  13525.  
  13526.   036F4 CF              RST     08H             ;Next character must be
  13527.   036F5 28              DEFB    '('             ;a '('
  13528.   036F6 CD1C2B          CALL    2B1CH           ;A = register parameter value
  13529.   036F9 F5              PUSH    AF              ;Save it
  13530.   036FA CF              RST     08H             ;Next character must be
  13531.   036FB 29              DEFB    ')'             ;a ')'
  13532.   036FC F1              POP     AF              ;Restore parameter value
  13533.   036FD FE10            CP      10H             ;In range (0 to 15)
  13534.   036FF D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  13535.  
  13536.   03702 E5              PUSH    HL              ;Save PTP
  13537.   03703 67              LD      H,A             ;H = PSG register number
  13538.   03704 CDBB3A          CALL    3ABBH           ;A = contents PSG register H
  13539.   03707 C3723F          JP      3F72H           ;Put A in X and return
  13540.  
  13541.  
  13542. ; X = SCALE
  13543.  
  13544.   0370A 3A1443          LD      A,(4314H)       ;A = current scale value
  13545.   0370D E5              PUSH    HL              ;Save PTP
  13546.   0370E C3723F          JP      3F72H           ;Put A in X as INT and return
  13547.  
  13548.  
  13549. ; Table with colour codes (not used)
  13550.  
  13551.   03711 10              DEFB    10H
  13552.   03712 0D              DEFB    0DH
  13553.   03713 0E              DEFB    0EH
  13554.   03714 04              DEFB    04H
  13555.   03715 06              DEFB    06H
  13556.   03716 03              DEFB    03H
  13557.   03717 01              DEFB    01H
  13558.   03718 02              DEFB    02H
  13559.   03719 05              DEFB    05H
  13560.   0371A 07              DEFB    07H
  13561.   0371B 08              DEFB    08H
  13562.   0371C 09              DEFB    09H
  13563.   0371D 0A              DEFB    0AH
  13564.   0371E 0B              DEFB    0BH
  13565.   0371F 0C              DEFB    0CH
  13566.   03720 0F              DEFB    0FH
  13567.  
  13568. ; Table with colour codes (not used)
  13569.  
  13570.   03721 10              DEFB    10H
  13571.   03722 0D              DEFB    0DH
  13572.   03723 06              DEFB    06H
  13573.   03724 04              DEFB    04H
  13574.   03725 0F              DEFB    0FH
  13575.   03726 03              DEFB    03H
  13576.   03727 09              DEFB    09H
  13577.   03728 02              DEFB    02H
  13578.   03729 01              DEFB    01H
  13579.   0372A 05              DEFB    05H
  13580.   0372B 07              DEFB    07H
  13581.   0372C 08              DEFB    08H
  13582.   0372D 0A              DEFB    0AH
  13583.   0372E 0B              DEFB    0BH
  13584.   0372F 0C              DEFB    0CH
  13585.   03730 0E              DEFB    0EH
  13586.  
  13587.  
  13588. : Table with colour codes (not used)
  13589.  
  13590.   03731 10              DEFB    10H
  13591.   03732 05              DEFB    05H
  13592.   03733 02              DEFB    02H
  13593.   03734 04              DEFB    04H
  13594.   03735 0E              DEFB    0EH
  13595.   03736 09              DEFB    09H
  13596.   03737 01              DEFB    01H
  13597.   03738 0A              DEFB    0AH
  13598.   03739 07              DEFB    07H
  13599.   0373A 06              DEFB    06H
  13600.   0373B 0D              DEFB    0DH
  13601.   0373C 03              DEFB    03H
  13602.   0373D 08              DEFB    08H
  13603.   0373E 0B              DEFB    0BH
  13604.   0373F 0C              DEFB    0CH
  13605.   03740 0F              DEFB    0FH
  13606.  
  13607.  
  13608. ; Unused ROM space
  13609.  
  13610.   03741 FF              RST     38H             ;--
  13611.   03742 FF              RST     38H
  13612.   03743 FF              RST     38H
  13613.   03744 FF              RST     38H
  13614.   03745 FF              RST     38H
  13615.   03746 FF              RST     38H
  13616.   03747 FF              RST     38H
  13617.   03748 FF              RST     38H
  13618.   03749 FF              RST     38H
  13619.   0374A FF              RST     38H
  13620.   0374B FF              RST     38H
  13621.   0374C FF              RST     38H
  13622.   0374D FF              RST     38H
  13623.   0374E FF              RST     38H
  13624.   0374F FF              RST     38H
  13625.   03750 FF              RST     38H
  13626.   03751 FF              RST     38H
  13627.   03752 FF              RST     38H
  13628.   03753 FF              RST     38H
  13629.   03754 FF              RST     38H
  13630.   03755 FF              RST     38H
  13631.   03756 FF              RST     38H
  13632.   03757 FF              RST     38H
  13633.   03758 FF              RST     38H
  13634.   03759 FF              RST     38H
  13635.   0375A FF              RST     38H
  13636.   0375B FF              RST     38H
  13637.   0375C FF              RST     38H
  13638.   0375D FF              RST     38H
  13639.   0375E FF              RST     38H
  13640.   0375F FF              RST     38H
  13641.   03760 FF              RST     38H
  13642.   03761 FF              RST     38H
  13643.   03762 FF              RST     38H
  13644.   03763 FF              RST     38H
  13645.   03764 FF              RST     38H
  13646.   03765 FF              RST     38H
  13647.   03766 FF              RST     38H
  13648.   03767 FF              RST     38H
  13649.   03768 FF              RST     38H
  13650.   03769 FF              RST     38H
  13651.   0376A FF              RST     38H
  13652.   0376B FF              RST     38H
  13653.   0376C FF              RST     38H
  13654.   0376D FF              RST     38H
  13655.   0376E FF              RST     38H
  13656.   0376F FF              RST     38H
  13657.   03770 FF              RST     38H
  13658.   03771 FF              RST     38H
  13659.   03772 FF              RST     38H
  13660.   03773 FF              RST     38H
  13661.   03774 FF              RST     38H
  13662.   03775 FF              RST     38H
  13663.   03776 FF              RST     38H
  13664.   03777 FF              RST     38H
  13665.   03778 FF              RST     38H
  13666.   03779 FF              RST     38H
  13667.   0377A FF              RST     38H
  13668.   0377B FF              RST     38H
  13669.   0377C FF              RST     38H
  13670.   0377D FF              RST     38H
  13671.   0377E FF              RST     38H
  13672.   0377F FF              RST     38H
  13673.   03780 FF              RST     38H
  13674.   03781 FF              RST     38H
  13675.   03782 FF              RST     38H
  13676.   03783 FF              RST     38H
  13677.   03784 FF              RST     38H
  13678.   03785 FF              RST     38H
  13679.   03786 FF              RST     38H
  13680.   03787 FF              RST     38H
  13681.   03788 FF              RST     38H
  13682.   03789 FF              RST     38H
  13683.   0378A FF              RST     38H
  13684.   0378B FF              RST     38H
  13685.   0378C FF              RST     38H
  13686.   0378D FF              RST     38H
  13687.   0378E FF              RST     38H
  13688.   0378F FF              RST     38H
  13689.   03790 FF              RST     38H
  13690.   03791 FF              RST     38H
  13691.   03792 FF              RST     38H
  13692.   03793 FF              RST     38H
  13693.   03794 FF              RST     38H
  13694.   03795 FF              RST     38H
  13695.   03796 FF              RST     38H
  13696.   03797 FF              RST     38H
  13697.   03798 FF              RST     38H
  13698.   03799 FF              RST     38H
  13699.   0379A FF              RST     38H
  13700.   0379B FF              RST     38H
  13701.   0379C FF              RST     38H
  13702.   0379D FF              RST     38H
  13703.   0379E FF              RST     38H
  13704.   0379F FF              RST     38H
  13705.   037A0 FF              RST     38H
  13706.   037A1 FF              RST     38H
  13707.   037A2 FF              RST     38H
  13708.   037A3 FF              RST     38H
  13709.   037A4 FF              RST     38H
  13710.   037A5 FF              RST     38H
  13711.   037A6 FF              RST     38H
  13712.   037A7 FF              RST     38H
  13713.   037A8 FF              RST     38H
  13714.   037A9 FF              RST     38H
  13715.   037AA FF              RST     38H
  13716.   037AB FF              RST     38H
  13717.   037AC FF              RST     38H
  13718.   037AD FF              RST     38H
  13719.   037AE FF              RST     38H
  13720.   037AF FF              RST     38H
  13721.   037B0 FF              RST     38H
  13722.   037B1 FF              RST     38H
  13723.   037B2 FF              RST     38H
  13724.   037B3 FF              RST     38H
  13725.   037B4 FF              RST     38H
  13726.   037B5 FF              RST     38H
  13727.   037B6 FF              RST     38H
  13728.   037B7 FF              RST     38H
  13729.   037B8 FF              RST     38H
  13730.   037B9 FF              RST     38H
  13731.   037BA FF              RST     38H
  13732.   037BB FF              RST     38H
  13733.   037BC FF              RST     38H
  13734.   037BD FF              RST     38H
  13735.   037BE FF              RST     38H
  13736.   037BF FF              RST     38H
  13737.   037C0 FF              RST     38H
  13738.   037C1 FF              RST     38H
  13739.   037C2 FF              RST     38H
  13740.   037C3 FF              RST     38H
  13741.   037C4 FF              RST     38H
  13742.   037C5 FF              RST     38H
  13743.   037C6 FF              RST     38H
  13744.   037C7 FF              RST     38H
  13745.   037C8 FF              RST     38H
  13746.   037C9 FF              RST     38H
  13747.   037CA FF              RST     38H
  13748.  
  13749.  
  13750. ; Print text on screen (not used)
  13751. ;
  13752. ; I: HL -> text
  13753.  
  13754.   037CB D5              PUSH    DE              ;Save DE
  13755.   037CC 111D40          LD      DE,401DH        ;DE -> screen DCB
  13756.   037CF 1804            JR      37D5H           ;Continue at 36D5H
  13757.  
  13758. ; Print text on printer (not used)
  13759. ; The end of text must be indicated with 03H or 0DH.
  13760. ; The 0DH is printed, the 03H not.
  13761. ;
  13762. ; I: HL -> text
  13763.  
  13764.   037D1 D5              PUSH    DE              ;Save DE
  13765.   037D2 112540          LD      DE,4025H        ;DE -> printer DCB
  13766.  
  13767.   037D5 E5              PUSH    HL              ;Save text pointer
  13768.  
  13769.   037D6 7E              LD      A,(HL)          ;A = character from text
  13770.   037D7 FE03            CP      03H             ;End of text ?
  13771.   037D9 2809            JR      Z,37E4H         ;Yes: continue at 37E4H
  13772.  
  13773.   037DB CD1B00          CALL    001BH           ;Print character (using DCB)
  13774.   037DE 7E              LD      A,(HL)          ;A = character from text
  13775.   037DF FE0D            CP      0DH             ;End of text ?
  13776.   037E1 23              INC     HL              ;Pointer + 1
  13777.   037E2 20F2            JR      NZ,37D6H        ;No: next character
  13778.  
  13779.   037E4 E1              POP     HL              ;Restore original text pointer
  13780.   037E5 D1              POP     DE              ;Restore DE
  13781.   037E6 C9              RET
  13782.  
  13783.  
  13784. ; Conversion of the binary value in DE to 4 hex digits
  13785. ; Used by DOS system call $HEXDE
  13786. ; (reverse of the &H command)
  13787. ;
  13788. ; I: DE = binary value
  13789. ;    HL -> memory space for storage of the 4 hex characters
  13790.  
  13791.   037E7 7A              LD      A,D             ;A = D
  13792.   037E8 CDEC37          CALL    37ECH           ;Convert A into 2 hex chars
  13793.   037EB 7B              LD      A,E             ;A = E
  13794.  
  13795.   037EC F5              PUSH    AF              ;Save binary value
  13796.   037ED 0F              RRCA                    ;Isolate upper nibble
  13797.   037EE 0F              RRCA
  13798.   037EF 0F              RRCA
  13799.   037F0 0F              RRCA
  13800.   037F1 CDF537          CALL    37F5H
  13801.   037F4 F1              POP     AF              ;Restore binary value
  13802.   037F5 E60F            AND     0FH             ;Isolate lower nibble
  13803.   037F7 C690            ADD     A,90H           ;Make hex character out of
  13804.   037F9 27              DAA                     ;the binary value 0-15
  13805.   037FA CE40            ADC     A,40H
  13806.   037FC 27              DAA
  13807.   037FD 77              LD      (HL),A          ;Save hex character
  13808.   037FE 23              INC     HL              ;Update pointer
  13809.   037FF C9              RET
  13810.  
  13811.  
  13812. ; CTRC programming table for PAL standard
  13813. ; The table entries are written into the CTRC register 15 downwards to
  13814. ; register 0
  13815. ; The table is copied into system RAM from 42F0H onwards
  13816.  
  13817. ; LGR mode
  13818.  
  13819.   03800 01              DEFB    01H             ;Cursor position LSB
  13820.   03801 00              DEFB    00H             ;                MSB
  13821.   03802 00              DEFB    00H             ;Start address of page LSB
  13822.   03803 04              DEFB    04H             ;Start address of page MSB
  13823.   03804 07              DEFB    07H             ;Cursor stop scan line
  13824.   03805 C4              DEFB    C4H             ;Cursor start scan line
  13825.   03806 07              DEFB    07H             ;Scan lines/row
  13826.   03807 A0              DEFB    A0H             ;Interlace mode
  13827.   03808 1F              DEFB    1FH             ;VSYNC position
  13828.   03809 19              DEFB    19H             ;Character rows/frame
  13829.   0380A 00              DEFB    00H             ;VSYNC adjust
  13830.   0380B 26              DEFB    26H             ;Vertical total
  13831.   0380C 96              DEFB    96H             ;HSYNC width
  13832.   0380D 34              DEFB    34H             ;HSYNC position
  13833.   0380E 28              DEFB    28H             ;Characters/row
  13834.   0380F 46              DEFB    46H             ;Horizontal total
  13835.  
  13836. ; FGR mode
  13837.  
  13838.   03810 00              DEFB    00H
  13839.   03811 00              DEFB    00H
  13840.   03812 00              DEFB    00H
  13841.   03813 08              DEFB    08H
  13842.   03814 00              DEFB    00H
  13843.   03815 20              DEFB    20H
  13844.   03816 01              DEFB    01H
  13845.   03817 20              DEFB    20H
  13846.   03818 74              DEFB    74H
  13847.   03819 66              DEFB    66H
  13848.   0381A 1F              DEFB    1FH
  13849.   0381B 7E              DEFB    7EH
  13850.   0381C 96              DEFB    96H
  13851.   0381D 34              DEFB    34H
  13852.   0381E 28              DEFB    28H
  13853.   0381F 46              DEFB    46H
  13854.  
  13855. ; The next 3 bytes determine the baudrate at which characters are read/written
  13856. ; from/to cassette tape. These values are copies into system RAM at
  13857. ; 4310H, 4311H and 4312H. See also routines at 01FAH and 021FH.
  13858.  
  13859.   03820 46              DEFB    46H
  13860.   03821 4B              DEFB    4BH
  13861.   03822 69              DEFB    69H
  13862.  
  13863.  
  13864. ; CTRC programming table for NTSC standard (not used)
  13865. ; Using NTSC requires also hardware alterations on the video
  13866. ; hardware. Therefor, this table is useless for non-NTSC
  13867. ; computers
  13868.  
  13869. ; LGR mode
  13870.  
  13871.   03823 01              DEFB    01H
  13872.   03824 00              DEFB    00H
  13873.   03825 00              DEFB    00H
  13874.   03826 04              DEFB    04H
  13875.   03827 07              DEFB    07H
  13876.   03828 C4              DEFB    C4H
  13877.   03829 07              DEFB    07H
  13878.   0382A A0              DEFB    A0H
  13879.   0382B 1B              DEFB    1BH
  13880.   0382C 19              DEFB    19H
  13881.   0382D 06              DEFB    06H
  13882.   0382E 1F              DEFB    1FH
  13883.   0382F 34              DEFB    34H
  13884.   03830 2E              DEFB    2EH
  13885.   03831 28              DEFB    28H
  13886.   03832 38              DEFB    38H
  13887.  
  13888. ; FGR mode, NTSC standard
  13889.  
  13890.   03833 00              DEFB    00H
  13891.   03834 00              DEFB    00H
  13892.   03835 00              DEFB    00H
  13893.   03836 08              DEFB    08H
  13894.   03837 00              DEFB    00H
  13895.   03838 20              DEFB    20H
  13896.   03839 01              DEFB    01H
  13897.   0383A 20              DEFB    20H
  13898.   0383B 6E              DEFB    6EH
  13899.   0383C 66              DEFB    66H
  13900.   0383D 08              DEFB    08H
  13901.   0383E 7F              DEFB    7FH
  13902.   0383F 34              DEFB    34H
  13903.   03840 2E              DEFB    2EH
  13904.   03841 28              DEFB    28H
  13905.   03842 38              DEFB    38H
  13906.  
  13907. ; Default values for timing of cassette I/O (not used)
  13908. ; These values are used for the American version of the
  13909. ; Colour Genie
  13910.  
  13911.   03843 4C              DEFB    4CH
  13912.   03844 51              DEFB    51H
  13913.   03845 71              DEFB    71H
  13914.  
  13915.  
  13916. ; FCLS (without parameter)
  13917.  
  13918.   03846 3E00            LD      A,00H           ;A = FCOLOUR code (0)
  13919.   03848 1808            JR      3852H           ;Execute FCLS
  13920.  
  13921. ; Continuation of FCLS routine of 3C87H (with parameter)
  13922.  
  13923.   0384A C4C23F          CALL    NZ,3FC2H        ;If there is a parameter:
  13924.                                                 ;get parameter
  13925.   0384D FE04            CP      04H             ;Value in range (1 to 4)
  13926.   0384F D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  13927.  
  13928.  
  13929. ; SUB for FCLS (AF,BC,DE,HL)
  13930. ; Perform an FCLS A
  13931. ;
  13932. ; I: A = FCOLOUR value
  13933.  
  13934.   03852 4F              LD      C,A             ;Value in C
  13935.                                                 ;Build byte value for FGR
  13936.                                                 ;memory to FCLS with the proper
  13937.                                                 ;colour
  13938.   03853 0603            LD      B,03H           ;4 pixels per byte
  13939.   03855 07              RLCA                    ;Shift 2 positions
  13940.   03856 07              RLCA
  13941.   03857 B1              OR      C               ;or with C
  13942.   03858 10FB            DJNZ    3855H
  13943.  
  13944.   0385A 4F              LD      C,A             ;FCLS byte value in C
  13945.   0385B E5              PUSH    HL              ;Save PTP
  13946.   0385C 2AA440          LD      HL,(40A4H)      ;HL -> start of user BASIC RAM
  13947.   0385F 110148          LD      DE,4801H        ;DE -> start of FGR memory + 1
  13948.   03862 DF              RST     18H             ;FGR mode disabled at startup
  13949.                                                 ;with <MOD SEL> ?
  13950.   03863 2809            JR      Z,386EH         ;Yes, nothing to do so return
  13951.  
  13952.   03865 210048          LD      HL,4800H        ;HL -> start of FGR memory
  13953.   03868 71              LD      (HL),C          ;Put FCLS value in first byte
  13954.   03869 01FF0F          LD      BC,0FFFH        ;BC = bytes to fill
  13955.   0386C EDB0            LDIR                    ;Fill all bytes in FGR memory
  13956.  
  13957.   0386E E1              POP     HL              ;Restore PTP
  13958.   0386F C9              RET
  13959.  
  13960.  
  13961. ; This routine initializes the CRTC. The screen mode (LGR or FGR) is taken
  13962. ; from the port 255 status byte
  13963.  
  13964.   03870 3A1C43          LD      A,(431CH)       ;Take port 255 status byte
  13965.  
  13966. ; Entry with port 255 status byte already in A
  13967.  
  13968.   03873 E5              PUSH    HL              ;Save HL
  13969.   03874 21F042          LD      HL,42F0H        ;HL -> CRTC table for LGR mode
  13970.   03877 CB6F            BIT     5,A             ;LGR mode ?
  13971.   03879 2803            JR      Z,387EH         ;Yes: continue at 387EH
  13972.  
  13973.   0387B 210043          LD      HL,4300H        ;HL -> CRTC table for FGR mode
  13974.  
  13975.   0387E D3FF            OUT     (0FFH),A        ;Set CRTC mode
  13976.   03880 321C43          LD      (431CH),A       :Save port 255 output status
  13977.  
  13978.   03883 0610            LD      B,10H           ;B = CRTC registers 16
  13979.   03885 0EFA            LD      C,0FAH          ;C = CTRC register select port
  13980.   03887 05              DEC     B               ;register counter - 1
  13981.   03888 ED41            OUT     (C),B           ;Select register
  13982.   0388A 04              INC     B               ;Adjust B for OUTI
  13983.   0388B 0C              INC     C               ;C = CTRC data register port
  13984.   0388C EDA3            OUTI                    ;Write value from CRTC table
  13985.                                                 ;to register.
  13986.                                                 ;All registers done ?
  13987.   0388E 20F5            JR      NZ,3885H        ;No: update next register
  13988.   03890 E1              POP     HL              ;Restore HL
  13989.   03891 C9              RET
  13990.  
  13991.  
  13992. ; This routine sets the screen to LGR and NBGRD. Then it prints the text
  13993. : addressed by HL
  13994. ;
  13995. ; I: HL -> text
  13996. ; O: -
  13997.  
  13998.   03892 D9              EXX                     ;Save registers
  13999.   03893 3A1C43          LD      A,(431CH)       ;Take port 255 ouput status
  14000.   03896 E6DB            AND     0DBH            ;Set LGR and NBGRD. Leave other
  14001.                                                 ;bits as they are
  14002.   03898 CD7338          CALL    3873H           ;Initialize CRTC
  14003.   0389B D9              EXX                     ;Restore registers
  14004.   0389C CDA728          CALL    28A7H           ;Print text
  14005.   0389F C9              RET
  14006.  
  14007.  
  14008. ; SUB for CONT (see 1DF2H)
  14009. ; Program CRTC on last value and store last LN as actual LN
  14010.  
  14011.  
  14012.   038A0 D9              EXX                     ;Save registers
  14013.   038A1 CD7038          CALL    3870H           ;Initialize CRTC
  14014.   038A4 D9              EXX                     ;Restore registers
  14015.   038A5 22A240          LD      (40A2H),HL      ;Renew actual LN
  14016.   038A8 C9              RET
  14017.  
  14018.  
  14019. ; FGR statement
  14020. ; -------------
  14021.  
  14022.   038A9 3A1C43          LD      A,(431CH)       ;A = port 255 output status
  14023.   038AC CBEF            SET     5,A             ;FGR/LGR bit to FGR
  14024.   038AE 18C3            JR      3873H           ;Initialize CTRC
  14025.  
  14026.  
  14027. ; LGR statement
  14028. ; -------------
  14029.  
  14030.   038B0 3A1C43          LD      A,(431CH)       ;A = port 255 output status
  14031.   038B3 CBAF            RES     5,A             ;FGR/LGR bit to LGR
  14032.   038B5 18BC            JR      3873H           ;Initialize CRTC
  14033.  
  14034.  
  14035. ; Old BRGD routine. Replaced by BGRD n at 3FE4H.
  14036.  
  14037.   038B7 0604            LD      B,04H           ;--
  14038.   038B9 1802            JR      38BDH
  14039.  
  14040.  
  14041. ; NBGRD statement
  14042. ; ---------------
  14043.  
  14044.   038BB 0600            LD      B,00H           ;BGRD value = 0
  14045.  
  14046.   038BD 3A1C43          LD      A,(431CH)       ;A = port 255 output status
  14047.   038C0 E6FB            AND     0FBH            ;Clear old BGRD bits
  14048.   038C2 B0              OR      B               ;Set new BGRD bits
  14049.   038C3 321C43          LD      (431CH),A       ;Store status in system RAM
  14050.   038C6 D3FF            OUT     (0FFH),A        ;Set new BGRD
  14051.   038C8 C9              RET
  14052.  
  14053.  
  14054. ; COLOUR statement
  14055. ; ----------------
  14056.  
  14057.   038C9 CDC23F          CALL    3FC2H           ;A = colour value - 1
  14058.   038CC FE10            CP      10H             ;Value in range (1 to 16) ?
  14059.   038CE D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  14060.  
  14061.   038D1 322340          LD      (4023H),A       ;Store new value in system RAM
  14062.   038D4 C9              RET
  14063.  
  14064.  
  14065. ; FCOLOUR statement
  14066. ; -----------------
  14067. ; The FCOLOUR token is stored in 3 bytes! (FFH, 81H, 52H)
  14068. ; Because of an error in the old ROM, only the keyword FCOLOU was recognized.
  14069. ; The missing 'R' had to be stored seperately.
  14070.  
  14071.   038D5 CF              RST     08H             ;Next character must be a 'R'
  14072.   038D6 52              DEFB    'R'             ;becuase token is only FCOLOU
  14073.   038D7 CDC23F          CALL    3FC2H           ;A = fcolour value - 1
  14074.   038DA FE04            CP      04H             ;Value in range (1 to 4) ?
  14075.   038DC 30F0            JR      NC,38CEH        ;No: ?FC Error
  14076.  
  14077.   038DE 321343          LD      (4313H),A       ;Store new FCOLOUR value
  14078.   038E1 C9              RET
  14079.  
  14080.  
  14081. ; SUB for tokenizing (see 1C15H)
  14082. ; Scan Colour-keywords table when the end of the normal keyword table
  14083. ; has been reached.
  14084. ;
  14085. ; I: A = current table character
  14086. ;    B = token counter
  14087. ;    C = current text character
  14088. ;    DE = text pointer
  14089. ;    HL = table pointer
  14090.  
  14091.   038E2 E67F            AND     7FH             ;Table end reached ?
  14092.   038E4 C0              RET     NZ              ;No: return
  14093.  
  14094.   038E5 EB              EX      DE,HL           ;HL = text pointer
  14095.   038E6 112F39          LD      DE,392FH        ;DE -> Colour keyword table
  14096.   038E9 C5              PUSH    BC              ;Save token counter
  14097.   038EA 067F            LD      B,7FH           ;Set new token counter
  14098.   038EC 7E              LD      A,(HL)          ;A = current text character
  14099.   038ED FE61            CP      61H             ;Possible lower case ?
  14100.   038EF 3806            JR      C,38F7H         ;No: ok, continue at 38F7H
  14101.  
  14102.   038F1 FE7B            CP      7BH             ;Lower case ?
  14103.   038F3 3002            JR      NC,38F7H        ;No: ok, continue at 38F7H
  14104.  
  14105.   038F5 E65F            AND     5FH             ;Convert to upper case
  14106.   038F7 4E              LD      C,(HL)          ;C = current text character
  14107.   038F8 EB              EX      DE,HL           ;HL = table pointer
  14108.   038F9 23              INC     HL              ;Table pointer + 1
  14109.   038FA B6              OR      (HL)            ;Next keyword reached ?
  14110.   038FB F2F938          JP      P,38F9H         ;No: increment pointer until
  14111.                                                 ;next keyword reached
  14112.   038FE 04              INC     B               ;Token counter + 1
  14113.   038FF 7E              LD      A,(HL)          ;A = keyword table character
  14114.   03900 E67F            AND     7FH             ;End of keyword table reached ?
  14115.   03902 2829            JR      Z,392DH         ;Yes: return
  14116.  
  14117.   03904 B9              CP      C               ;Compare with text character
  14118.                                                 ;The same ?
  14119.   03905 20F2            JR      NZ,38F9H        ;No: try next keyword
  14120.  
  14121.   03907 EB              EX      DE,HL           ;DE = keyword table pointer
  14122.   03908 E5              PUSH    HL              ;Save text pointer
  14123.   03909 13              INC     DE              ;Table pointer + 1
  14124.   0390A 1A              LD      A,(DE)          ;A = next table character
  14125.   0390B B7              OR      A               ;Next keyword reached ?
  14126.   0390C FA1E39          JP      M,391EH         ;Yes: all characters match!
  14127.                                                 ;continue ar 391EH
  14128.   0390F 4F              LD      C,A             ;No: C = table character
  14129.   03910 23              INC     HL              ;Text pointer + 1
  14130.   03911 7E              LD      A,(HL)          ;A = next text character
  14131.   03912 FE61            CP      61H             ;Convert to upper case
  14132.   03914 3802            JR      C,3918H
  14133.  
  14134.   03916 E65F            AND     5FH
  14135.   03918 B9              CP      C               ;Compare to table character
  14136.   03919 28EE            JR      Z,3909H         ;Next character when indentical
  14137.  
  14138.   0391B E1              POP     HL              ;Restore old text pointer
  14139.   0391C 18D9            JR      38F7H           ;And compare next keyword
  14140.  
  14141. ; Keyword found
  14142.  
  14143.   0391E F1              POP     AF              ;Text pointer
  14144.   0391F F1              POP     AF              ;Old token counter
  14145.   03920 F1              POP     AF              ;Remove RET address to 1C18H
  14146.   03921 D1              POP     DE              ;and to 1C3DH
  14147.   03922 D1              POP     DE              ;Restore counter of characters
  14148.                                                 ;per line
  14149.   03923 E3              EX      (SP),HL         ;Save text pointer, restore
  14150.                                                 ;buffer pointer
  14151.   03924 36FF            LD      (HL),0FFH       ;Store FFH in buffer as
  14152.                                                 ;Colour-keyword identifier
  14153.   03926 78              LD      A,B             ;A = token value of keyword
  14154.                                                 ;that has been found
  14155.   03927 42              LD      B,D             ;BC = DE
  14156.   03928 4B              LD      C,E
  14157.   03929 D1              POP     DE              ;Restore text pointer in DE
  14158.   0392A C3573D          JP      3D57H           ;Store token
  14159.  
  14160. ; End of keyword table reached, no keyword matched
  14161.  
  14162.   0392D C1              POP     BC              ;Restore old counters
  14163.   0392E F1              POP     AF              ;Remove RET address to 1C18H
  14164.   0392F C9              RET                     ;Return to 1C3DH
  14165.  
  14166.  
  14167. ; Keyword table for Colour BASIC statements
  14168.  
  14169.   03930 C34F4C4F5552    DEFB    80H+'C','OLOUR'         ;COLOUR
  14170.   03936 C6434F4C4F55    DEFB    80H+'F','COLOU'         ;FCOLOUR
  14171.   0393C CB4559504144    DEFB    80H+'K','EYPAD'         ;KEYPAD
  14172.   03942 CA4F59          DEFB    80H+'J','OY'            ;JOY
  14173.   03945 D04C4F54        DEFB    80H+'P','LOT'           ;PLOT
  14174.   03949 C64752          DEFB    80H+'F','GR'            ;FGR
  14175.   0394C CC4752          DEFB    80H+'L','GR'            ;LGR
  14176.   0394F C6434C53        DEFB    80H+'F','CLS'           ;FCLS
  14177.   03953 D04C4159        DEFB    80H+'P','LAY'           ;PLAY
  14178.   03957 C34952434C45    DEFB    80H+'C','IRCLE'         ;CIRCLE
  14179.   0395D D343414C45      DEFB    80H+'S ,'CALE'          ;SCALE
  14180.   03962 D348415045      DEFB    80H+'S','HAPE'          ;SHAPE
  14181.   03967 CE5348415045    DEFB    80H+'N','SHAPE'         ;NSHAPE
  14182.   0396D D85348415045    DEFB    80H+'X','SHAPE'         ;XSHAPE
  14183.   03973 D041494E54      DEFB    80H+'P','AINT'          ;PAINT
  14184.   03978 C3504F494E54    DEFB    80H+'C','POINT'         ;CPOINT
  14185.   0397E CE504C4F54      DEFB    80H+'N','PLOT'          ;NPLOT
  14186.   03983 D34F554E44      DEFB    80H+'S','OUND'          ;SOUND
  14187.   03988 C3484152        DEFB    80H+'C','HAR'           ;CHAR
  14188.   0398C D2454E554D      DEFB    80H+'R','ENUM'          ;RENUM
  14189.   03991 D3574150        DEFB    80H+'S','WAP'           ;SWAP
  14190.   03995 C64B4559        DEFB    80H+'F','KEY'           ;FKEY
  14191.   03999 C3414C4C        DEFB    80H+'C','ALL'           ;CALL
  14192.   0399D D64552494659    DEFB    80H+'V','ERIFY'         ;VERIFY
  14193.   039A3 C2475244        DEFB    80H+'B','GRD'           ;BGRD
  14194.   039A7 CE42475244      DEFB    80H+'N','BGRD'          ;NBGRD
  14195.   039AC 80              DEFB    80H                     ;End of table marker
  14196.  
  14197.  
  14198. ; SUB for LIST (see 2BA9H)
  14199. ; Establish start of keyword table.
  14200. ;
  14201. ; O: HL -> keyword table
  14202.  
  14203.   039AD FE80            CP      80H             ;Token value = FFH? (FFH - 7FH)
  14204.   039AF 215016          LD      HL,1650H        ;HL -> BASIC keyword table
  14205.   039B2 C0              RET     NZ              ;No: HL is set properly: return
  14206.  
  14207.   039B3 E1              POP     HL              ;RET address to HL
  14208.   039B4 E3              EX      (SP),HL         ;RET address back,
  14209.                                                 ;Line pointer to HL
  14210.   039B5 7E              LD      A,(HL)          ;A = next character
  14211.   039B6 D67F            SUB     7FH             ;Is it a token ?
  14212.   039B8 5F              LD      E,A             ;E = character - 7FH
  14213.   039B9 23              INC     HL              ;Pointer + 1
  14214.   039BA E3              EX      (SP),HL         ;Save pointer,
  14215.                                                 ;RET address in HL
  14216.   039BB E5              PUSH    HL              ;Save RET address
  14217.   039BC 2A8C43          LD      HL,(438CH)      ;HL -> Colour keyword table
  14218.   039BF C9              RET
  14219.  
  14220.  
  14221. ; SUB for program loop (see 1D67H)
  14222. ; Establish start address of jump address table
  14223.  
  14224.   039C0 FE7F            CP      7FH             ;Colour token found ?
  14225.   039C2 2808            JR      Z,39CCH         ;Yes: continue at 39CCH
  14226.  
  14227.   039C4 FE3C            CP      3CH             ;Command found ?
  14228.   039C6 D2E72A          JP      NC,2AE7H        ;No: continue at 2AE7H
  14229.  
  14230.   039C9 C36A1D          JP      1D6AH           ;Execute command
  14231.  
  14232. ; Colour token found
  14233.  
  14234.   039CC 23              INC     HL              ;PTP + 1
  14235.   039CD 7E              LD      A,(HL)          ;Get real token value
  14236.   039CE D680            SUB     80H             ;Subtract 80H
  14237.   039D0 07              RLCA                    ;*2 = table offset
  14238.   039D1 4F              LD      C,A             ;BC = table offset
  14239.   039D2 0600            LD      B,00H
  14240.   039D4 EB              EX      DE,HL           ;DE = PTP
  14241.   039D5 2A8E43          LD      HL,(438EH)      ;HL  -> Colour jump table
  14242.   039D8 C3721D          JP      1D72H           ;Get jump address as execute
  14243.                                                 ;routine
  14244.  
  14245. ; Address table for Colour BASIC statements
  14246.  
  14247.   039DB C938            DEFW    38C9H           ;COLOUR
  14248.   039DD D538            DEFW    38D5H           ;FCOLOUR
  14249.   039DF 9719            DEFW    1997H           ;KEYPAD  (= ?SN Error!)
  14250.   039E1 5236            DEFW    3652H           ;JOY
  14251.   039E3 C13B            DEFW    3BC1H           ;PLOT
  14252.   039E5 A938            DEFW    38A9H           ;FGR
  14253.   039E7 B038            DEFW    38B0H           ;LGR
  14254.   039E9 833C            DEFW    3C83H           ;FCLS
  14255.   039EB 613D            DEFW    3D61H           ;PLAY
  14256.   039ED F83A            DEFW    3AF8H           ;CIRCLE
  14257.   039EF F13A            DEFW    3AF1H           ;SCALE
  14258.   039F1 DD3C            DEFW    3CDDH           ;SHAPE
  14259.   039F3 D83C            DEFW    3CD8H           ;NSHAPE
  14260.   039F5 D33C            DEFW    3CD3H           ;XSHAPE
  14261.   039F7 383E            DEFW    3E38H           ;PAINT
  14262.   039F9 9719            DEFW    1997H           ;CPOINT  (= ?SN Error!)
  14263.   039FB BE3B            DEFW    3BBEH           ;NPLOT
  14264.   039FD 953F            DEFW    3F95H           ;SOUND
  14265.   039FF A83F            DEFW    3FA8H           ;CHAR
  14266.   03A01 B631            DEFW    31B6H           ;RENUM
  14267.   03A03 D336            DEFW    36D3H           ;SWAP
  14268.   03A05 6634            DEFW    3466H           ;FKEY
  14269.   03A07 5A35            DEFW    355AH           ;CALL
  14270.   03A09 4935            DEFW    3F33H           ;VERIFY
  14271.   03A0B E43F            DEFW    3FE4H           ;BGRD
  14272.   03A0D BB38            DEFW    38BBH           ;NBGRD
  14273.  
  14274.  
  14275. ; X = KEYPAD1, KEYPAD2, or KEYPAD(n)
  14276.  
  14277.   03A0F 7E              LD      A,(HL)          ;A = char from program text
  14278.   03A10 23              INC     HL              ;PTP + 1
  14279.   03A11 E5              PUSH    HL              ;Save PTP
  14280.   03A12 FE31            CP      '1'             ;Is it a '1' ? (KEYPAD1)
  14281.   03A14 CAD83A          JP      Z,3AD8H         ;Yes: continue at 3AD8H
  14282.  
  14283.   03A17 FE32            CP      '2'             ;Is it a '2' ? (KEYPAD2)
  14284.   03A19 CADC3A          JP      Z,3ADCH         ;Yes: continue at 3ADCH
  14285.  
  14286.   03A1C C3C33A          JP      3AC3H           ;Check if (n) set
  14287.  
  14288.  
  14289. ; X = JOY1X, JOY1Y, JOY2X, JOY2Y or JOY(n)
  14290.  
  14291.   03A1F 7E              LD      A,(HL)          ;A = character at PTP
  14292.   03A20 FE28            CP      '('             ;Is it a '('
  14293.   03A22 2826            JR      Z,3A4AH         ;Yes: continue at 3A4AH
  14294.  
  14295.   03A24 1604            LD      D,04H
  14296.   03A26 FE32            CP      '2'             ;Is it a '2' ? (JOY2)
  14297.   03A28 2807            JR      Z,3A31H         ;Yes: continue at 3A31H
  14298.  
  14299.   03A2A CB3A            SRL     D               ;D = D / 2
  14300.   03A2C FE31            CP      '1'             ;Is it a '1' ? (JOY1)
  14301.   03A2E C29719          JP      NZ,1997H        ;No: ?SN Error
  14302.  
  14303.   03A31 D7              RST     10H             ;Get next non-space character
  14304.   03A32 FE59            CP      'Y'             ;Char = 'Y' ? (JOY1Y,JOY2Y)
  14305.   03A34 2805            JR      Z,3A3BH         ;Yes: continue at 3A3BH
  14306.  
  14307.   03A36 15              DEC     D               ;D = D - 1
  14308.   03A37 FE58            CP      'X'             ;Char = 'X' ? (JOY1X,JOY2X)
  14309.   03A39 20F3            JR      NZ,3A2EH        ;No: ?SN Error
  14310.  
  14311.                                                 ;JOY1X: D = 1   JOY1Y: D = 2
  14312.                                                 ;JOY2X: D = 3   JOY2Y: D = 4
  14313.  
  14314.   03A3B 23              INC     HL              ;PTP + 1
  14315.   03A3C E5              PUSH    HL              ;Save PTP
  14316.   03A3D CD5E3A          CALL    3A5EH           ;Get joystick value
  14317.   03A40 E63F            AND     3FH             ;Set to value between  0-63
  14318.   03A42 3C              INC     A               ;Adjust to 1 to 64
  14319.   03A43 1816            JR      3A5BH           ;Put A in X as INT and return
  14320.  
  14321.  
  14322. ; Not used
  14323.  
  14324.   03A45 18F4            JR      3A3BH           ;Get joystick value
  14325.   03A47 00              NOP                     ;--
  14326.   03A48 00              NOP
  14327.   03A49 00              NOP
  14328.  
  14329.  
  14330. ; JOY (n)
  14331.  
  14332.   03A4A 23              INC     HL              ;PTP + 1
  14333.   03A4B CD1C2B          CALL    2B1CH           ;Get parameter
  14334.   03A4E FE08            CP      08H             ;In range (0-7)
  14335.   03A50 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  14336.  
  14337.   03A53 57              LD      D,A             ;Parameter in D
  14338.   03A54 CF              RST     08H             ;Next character must be
  14339.   03A55 29              DEFB    ')'             ;a ')'
  14340.   03A56 14              INC     D               ;D + 1
  14341.   03A57 E5              PUSH    HL              ;Save PTP
  14342.   03A58 CD5E3A          CALL    3A5EH           ;Get joystick value
  14343.   03A5B C3723F          JP      3F72H           ;Put A in X as INT and return
  14344.  
  14345.  
  14346. ; Get joystick value
  14347. ; I: D = parameter with joystick ID (range: 1 to 8)
  14348. ;        D = 1: JOY1X / D = 2: JOY1Y / D = 3: JOY2X / D = 4: JOY2Y
  14349. ;        The remaining values (5 to 8) can be used for a second pair
  14350. ;        of joysticks (or 4 other analogue inputs)
  14351. ; O: Value return by joystick
  14352.  
  14353.   03A5E CDA93A          CALL    3AA9H           ;Set port A to output,
  14354.                                                 ;port B to input
  14355.   03A61 AF              XOR     A               ;A = 0;
  14356.   03A62 1E80            LD      E,80H           ;E,7 = 1
  14357.  
  14358.   03A64 B3              OR      E               ;Set bit in A
  14359.   03A65 6F              LD      L,A             ;L = A
  14360.   03A66 260E            LD      H,0EH           ;Select PSG port A
  14361.   03A68 CDB33A          CALL    3AB3H           ;Write A to port A
  14362.   03A6B 24              INC     H               ;Select PSG Port B
  14363.   03A6C CDBB3A          CALL    3ABBH           ;A = contents of port B
  14364.   03A6F D5              PUSH    DE              ;Save counter D
  14365.  
  14366.   03A70 17              RLA                     ;Shift required bit into C-flag
  14367.   03A71 15              DEC     D               ;Required bit in C-flag ?
  14368.   03A72 20FC            JR      NZ,3A70H        ;No: shift again
  14369.  
  14370.   03A74 D1              POP     DE              ;Restore counter
  14371.   03A75 7D              LD      A,L             ;Last output value in A
  14372.                                                 ;Required bit = '1' ?
  14373.   03A76 3803            JR      C,3A7BH         ;Yes: continue at 3A7BH
  14374.  
  14375.   03A78 7B              LD      A,E             ;A = bitmask
  14376.   03A79 2F              CPL                     ;Invert it
  14377.   03A7A A5              AND     L               ;And with L
  14378.   03A7B CB3B            SRL     E               ;Shift E 1 bit to the right
  14379.                                                 ;All 8 bits done ?
  14380.   03A7D 30E5            JR      NC,3A64H        ;No: loop
  14381.  
  14382.   03A7F C9              RET
  14383.  
  14384.  
  14385. ; Old KEYPAD routine (now at 3A0FH)
  14386.  
  14387.   03A80 C3143F          JP      3F14H           ;Was: KEYPAD1
  14388.   03A83 00              NOP                     ;--
  14389.   03A84 C3183F          JP      3F18H           ;Was: KEYPAD2
  14390.  
  14391. ; KEYPAD routine
  14392. ; Read value from keypad and store in A
  14393. ;
  14394. ; I: D = keypad bit mask (FEH for KEYPAD1, F7H for KEYPAD2)
  14395. ; O: A = value from keypad
  14396.  
  14397.   03A87 CDA93A          CALL    3AA9H           ;Set port A to output,
  14398.                                                 ;port B to input
  14399.   03A8A 1EE4            LD      E,0E4H          ;E = LSB of start address
  14400.                                                 ;of KEYPAD table
  14401.   03A8C 0603            LD      B,03H           ;Test 2 columns
  14402.  
  14403.   03A8E 6A              LD      L,D             ;L = keypad address
  14404.   03A8F 260E            LD      H,0EH           ;Select port A
  14405.   03A91 CDB33A          CALL    3AB3H           ;Select keypad
  14406.   03A94 24              INC     H               ;Next PSG register
  14407.   03A95 CDBB3A          CALL    3ABBH           ;Get keypad value
  14408.   03A98 0E04            LD      C,04H           ;Test 4 rows
  14409.   03A9A 1F              RRA                     ;Shift next bit into C-flag
  14410.                                                 ;Key pressed ?
  14411.   03A9B 3008            JR      NC,3AA5H        ;Yes: continue at 3AA5H
  14412.  
  14413.   03A9D 1C              INC     E               ;Table offset + 1
  14414.   03A9E 0D              DEC     C               ;Row counter - 1
  14415.   03A9F 20F9            JR      NZ,3A9AH        ;Check next row
  14416.  
  14417.   03AA1 CB02            RLC     D               ;Address next column
  14418.   03AA3 10E9            DJNZ    3A8EH           ;Read next column
  14419.  
  14420.   03AA5 163A            LD      D,3AH           ;DE -> keypad table at proper
  14421.                                                 ;offset
  14422.   03AA7 1A              LD      A,(DE)          ;A = keypad value
  14423.   03AA8 C9              RET
  14424.  
  14425. ; Prepare port A for output and port B for input
  14426.  
  14427.   03AA9 2607            LD      H,07H           ;Select PSG enable register
  14428.   03AAB CDBB3A          CALL    3ABBH           ;A = contents of PSG register
  14429.   03AAE E63F            AND     3FH             ;Set port A to output (bit 7)
  14430.   03AB0 F640            OR      40H             ;and port B to input (bit 6)
  14431.  
  14432. ; Write A into PSG register addressed by H
  14433. ;
  14434. ; I: H = PSG register number
  14435. ;    A = data to write into register
  14436.  
  14437.   03AB2 6F              LD      L,A             ;L = A
  14438.  
  14439.   03AB3 0EF8            LD      C,0F8H
  14440.   03AB5 ED61            OUT     (C),H           ;Select PSG register
  14441.   03AB7 0C              INC     C
  14442.   03AB8 ED69            OUT     (C),L           ;Write new contents in PSG
  14443.   03ABA C9              RET
  14444.  
  14445.  
  14446. ; Read PSG register addressed by H
  14447. ;
  14448. ; I: H = PSG register number
  14449. ; O: A = contents of data register
  14450.  
  14451.   03ABB 0EF8            LD      C,0F8H          ;C = PSG select register
  14452.   03ABD ED61            OUT     (C),H           ;Select register H
  14453.   03ABF 0C              INC     C               ;C = PSG data register
  14454.   03AC0 ED78            IN      A,(C)           ;A = data from data register
  14455.   03AC2 C9              RET
  14456.  
  14457. ; KEYPAD(n)
  14458.  
  14459.   03AC3 E1              POP     HL              ;Restore PTP
  14460.   03AC4 2B              DEC     HL              ;Adjust PTP
  14461.   03AC5 CF              RST     08H             ;Next character must be
  14462.   03AC6 28              DEFB    '('             ;a '('
  14463.   03AC7 CDC23F          CALL    3FC2H           ;Get n - 1
  14464.   03ACA FE02            CP      02H             ;In range (1 or 2)
  14465.   03ACC D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  14466.   03ACF F5              PUSH    AF              ;save n
  14467.   03AD0 CF              RST     08H             ;Next character must be
  14468.   03AD1 29              DEFB    ')'             ;a ')'
  14469.   03AD2 F1              POP     AF              ;Restore parameter
  14470.   03AD3 E5              PUSH    HL              ;Save PTP
  14471.   03AD4 FE01            CP      01H             ;n = 1 ?
  14472.   03AD6 2804            JR      Z,3ADCH         ;Yes: continue at 3ADCH
  14473.  
  14474. ; KEYPAD1
  14475.  
  14476.   03AD8 16FE            LD      D,0FEH          ;Set bitmask for keypad 1
  14477.   03ADA 1802            JR      3ADEH           ;Continue at 3ADEH
  14478.  
  14479. ; KEYPAD2
  14480.  
  14481.   03ADC 16F7            LD      D,0F7H          ;Set bitmask for keypad 2
  14482.  
  14483.   03ADE CD873A          CALL    3A87H           ;A = value from keypad
  14484.   03AE1 C3723F          JP      3F72H           ;Put A in X and return
  14485.  
  14486.  
  14487. ; Keypad table with the return values for the 12 keys on the keypad
  14488.  
  14489.   03AE4 03              DEFB    03H             ;'3'
  14490.   03AE5 06              DEFB    06H             ;'6'
  14491.   03AE6 09              DEFB    09H             ;'9'
  14492.   03AE7 0C              DEFB    0CH             ;Fire button (right)
  14493.   03AE8 02              DEFB    02H             ;'2'
  14494.   03AE9 05              DEFB    05H             ;'5'
  14495.   03AEA 08              DEFB    08H             ;'8'
  14496.   03AEB 0A              DEFB    0AH             ;'0'
  14497.   03AEC 01              DEFB    01H             ;'1'
  14498.   03AED 04              DEFB    04H             ;'4'
  14499.   03AEE 07              DEFB    07H             ;'7'
  14500.   03AEF 0B              DEFB    0BH             ;Fire button (left)
  14501.   03AF0 00              DEFB    00H             ;No key pressed
  14502.  
  14503.  
  14504. ; SCALE statement
  14505. ; ---------------
  14506.  
  14507.   03AF1 CD1C2B          CALL    2B1CH           ;Get new scale value
  14508.   03AF4 321443          LD      (4314H),A       ;and save it in system RAM
  14509.   03AF7 C9              RET
  14510.  
  14511.  
  14512. ; CIRCLE statement
  14513. ; ----------------
  14514.  
  14515.   03AF8 CD1C2B          CALL    2B1CH           ;Get X-coordinate
  14516.   03AFB F5              PUSH    AF              ;Save X-coordinate
  14517.   03AFC CF              RST     08H             ;Next character must be
  14518.   03AFD 2C              DEFB    ','             ;a comma
  14519.   03AFE CD1C2B          CALL    2B1CH           ;Get Y-coordinate
  14520.   03B01 F5              PUSH    AF              ;Save Y-coordinate
  14521.   03B02 CF              RST     08H             ;Next character must be
  14522.   03B03 2C              DEFB    ','             ;a comma
  14523.   03B04 CD1C2B          CALL    2B1CH           ;Get radius
  14524.   03B07 D1              POP     DE              ;D = Y-coordinate
  14525.   03B08 C1              POP     BC              ;B = X-coordinate
  14526.   03B09 4A              LD      C,D             ;C = Y-coordinate
  14527.  
  14528. ; B = X-coordinate, C = Y-coordinate, A = radius
  14529.  
  14530.   03B0A E5              PUSH    HL              ;Save PTP
  14531.   03B0B 57              LD      D,A             ;Radius in D = Y-distance
  14532.   03B0C 1E00            LD      E,00H           ;X-distance = 0
  14533.   03B0E 2680            LD      H,80H           ;H = step value
  14534.  
  14535. ; Plot next 8 points
  14536.                                                 ;Y-distance < X-distance
  14537.                                                 ;(circle is closed) ?
  14538.   03B10 FA523B          JP      M,3B52H         ;Yes: restore PTP and return
  14539.  
  14540.   03B13 CD7A3B          CALL    3B7AH           ;Plot 4 points and swap X-
  14541.                                                 ;and Y-distance
  14542.   03B16 CD7A3B          CALL    3B7AH           ;Plot 4 points and swap back
  14543.   03B19 CD5E3B          CALL    3B5EH           ;Negate distances
  14544.   03B1C C5              PUSH    BC              ;Save registers
  14545.   03B1D D5              PUSH    DE
  14546.   03B1E E5              PUSH    HL
  14547.   03B1F 2E80            LD      L,80H           ;Bit 7 of L = 1: This causes
  14548.                                                 ;an overflow with the first
  14549.                                                 ;ADD HL,HL to bit 0 of H
  14550.   03B21 63              LD      H,E             ;H = X-distance
  14551.   03B22 0608            LD      B,08H           ;Process 8 bits
  14552.   03B24 1E00            LD      E,00H           ;E = 0, D = Y-distance
  14553.  
  14554. ; Calculate next step for Y distance
  14555.  
  14556.   03B26 29              ADD     HL,HL           ;Shift next bit from H
  14557.                                                 ;to C-flag
  14558.   03B27 ED52            SBC     HL,DE           ;HL = HL - DE - C-flag
  14559.   03B29 3803            JR      C,3B2EH         ;Jump when underflow. Because
  14560.                                                 ;E = 0, this can only happen
  14561.                                                 ;when H < D.
  14562.   03B2B 23              INC     HL              ;HL + 1 (= L + 1)
  14563.   03B2C 1801            JR      3B2FH           ;Loop
  14564.  
  14565.   03B2E 19              ADD     HL,DE           ;Reverse subtract
  14566.   03B2F 10F5            DJNZ    3B26H           ;Loop
  14567.  
  14568.   03B31 7D              LD      A,L             ;A = L
  14569.   03B32 E1              POP     HL              ;Restore registers
  14570.   03B33 D1              POP     DE
  14571.   03B34 C1              POP     BC
  14572.   03B35 84              ADD     A,H             ;A = A + step value
  14573.                                                 ;C-flag = 1 if A + H > 255
  14574.   03B36 67              LD      H,A             ;H = new step value
  14575.   03B37 7A              LD      A,D             ;A = Y-distance
  14576.   03B38 2E00            LD      L,00H           ;L = 0;
  14577.   03B3A 9D              SBC     A,L             ;A = Y-distance - C-flag
  14578.   03B3B 57              LD      D,A             ;D = new Y-distance
  14579.   03B3C 1C              INC     E               ;X-distance + 1
  14580.   03B3D 7A              LD      A,D             ;A = Y-distance
  14581.   03B3E BB              CP      E               ;X-distance > Y-distance ?
  14582.   03B3F C3103B          JP      3B10H           ;Continue at 3B10H
  14583.  
  14584. ; PLOT B + E , C + D
  14585.  
  14586.   03B42 C5              PUSH    BC              ;Save registers
  14587.   03B43 D5              PUSH    DE
  14588.   03B44 E5              PUSH    HL
  14589.   03B45 7B              LD      A,E             ;A = X-distance
  14590.   03B46 80              ADD     A,B             ;+ centre-coordinates
  14591.   03B47 6F              LD      L,A             ;Results in X-coordinate
  14592.   03B48 7A              LD      A,D             ;A = Y-distance
  14593.   03B49 81              ADD     A,C             ;+ centre-coordinates
  14594.   03B4A 67              LD      H,A             ;Results in Y-coordinate
  14595.   03B4B CD8A3B          CALL    3B8AH           ;PLOT L , H
  14596.   03B4E E1              POP     HL              ;Restore registers
  14597.   03B4F D1              POP     DE
  14598.   03B50 C1              POP     BC
  14599.   03B51 C9              RET
  14600.  
  14601. ; End CIRCLE
  14602.  
  14603.   03B52 E1              POP     HL              ;Restore PTP
  14604.   03B53 C9              RET
  14605.  
  14606. ; Negate X-distance (E = -E)
  14607.  
  14608.   03B54 7B              LD      A,E             ;A = E
  14609.   03B55 ED44            NEG                     ;A = -A
  14610.   03B57 5F              LD      E,A             ;new value back in E
  14611.   03B58 C9              RET
  14612.  
  14613. ; Negate Y-distance (D = -D)
  14614.  
  14615.   03B59 7A              LD      A,D             ;A = D
  14616.   03B5A ED44            NEG                     ;A = -A
  14617.   03B5C 57              LD      D,A             ;new value back in D
  14618.   03B5D C9              RET
  14619.  
  14620. ; Negate X- and Y-distance
  14621.  
  14622.   03B5E CD543B          CALL    3B54H           ;Negate X-distance
  14623.   03B61 CD593B          CALL    3B59H           ;Negate Y-distance
  14624.   03B64 C9              RET
  14625.  
  14626. ; PLOT B - E , C + D
  14627.  
  14628.   03B65 CD543B          CALL    3B54H           ;Negate X-distance
  14629.   03B68 CD423B          CALL    3B42H           ;PLOT B + E , C + D
  14630.   03B6B C9              RET
  14631.  
  14632. ; PLOT B + E , C - D
  14633.  
  14634.   03B6C CD593B          CALL    3B59H           ;Negate Y-distance
  14635.   03B6F CD423B          CALL    3B42H           ;PLOT B + E , C + D
  14636.   03B72 C9              RET
  14637.  
  14638. ; Swap X- and Y-distance
  14639.  
  14640.   03B73 7B              LD      A,E             ;L = E
  14641.   03B74 6F              LD      L,A
  14642.   03B75 7A              LD      A,D             ;E = D
  14643.   03B76 5F              LD      E,A
  14644.   03B77 7D              LD      A,L             ;D = L
  14645.   03B78 57              LD      D,A
  14646.   03B79 C9              RET
  14647.  
  14648. ; Plot 4 points and exchange X- and Y-distance
  14649.  
  14650.   03B7A CD423B          CALL    3B42H           ;PLOT B + E, C + D
  14651.                                                 ;(bottom right)
  14652.   03B7D CD653B          CALL    3B65H           ;PLOT B - E, C + D
  14653.                                                 ;(bottom left)
  14654.   03B80 CD6C3B          CALL    3B6CH           ;PLOT B - E, C - D
  14655.                                                 ;(top left)
  14656.   03B83 CD653B          CALL    3B65H           ;PLOT B + E, C - D
  14657.                                                 ;(top right)
  14658.   03B86 CD733B          CALL    3B73H           ;Exchange distances
  14659.                                                 ;for second call, see 3B13H)
  14660.   03B89 C9              RET
  14661.  
  14662.  
  14663. ; SUB for PLOT, PAINT, SHAPE and CIRCLE
  14664. ; PLOT L , H (AF,BC,DE,HL)
  14665. ;
  14666. ; I: L = X-coordinate
  14667. ;    H = Y-coordinate
  14668. ;    (4313H) = FCOLOUR value
  14669.  
  14670.   03B8A 3A1343          LD      A,(4313H)       ;A = FCOLOUR value
  14671.   03B8D E603            AND     03H             ;Clear unused bits
  14672.  
  14673. ; PLOT L , H
  14674. ; as 3B8AH but A = FCOLOUR value
  14675.  
  14676.   03B8F 4F              LD      C,A             ;C = FCOLOUR value
  14677.   03B90 3E9F            LD      A,9FH           ;A = X-max. (159)
  14678.   03B92 BD              CP      L               ;Beyond X-max ?
  14679.   03B93 D8              RET     C               ;Yes: done
  14680.  
  14681.   03B94 3E65            LD      A,65H           ;A = Y-max (101)
  14682.   03B96 BC              CP      H               ;Beyond Y-max ?
  14683.   03B97 D8              RET     C               ;Yes: done
  14684.  
  14685.   03B98 7D              LD      A,L             ;A = X-coordinate
  14686.   03B99 6C              LD      L,H             ;L = Y-coordinate
  14687.   03B9A 2600            LD      H,00H           ;HL = Y-coordinate
  14688.   03B9C 54              LD      D,H             ;DE = HL
  14689.   03B9D 5D              LD      E,L
  14690.   03B9E 29              ADD     HL,HL           ;HL = HL * 40
  14691.   03B9F 29              ADD     HL,HL           ;(one row in FGR mode
  14692.   03BA0 19              ADD     HL,DE           ;corresponds with 40 bytes
  14693.   03BA1 29              ADD     HL,HL           ;in FGR screen memory)
  14694.   03BA2 29              ADD     HL,HL
  14695.   03BA3 29              ADD     HL,HL
  14696.   03BA4 5F              LD      E,A             ;E = X-coordinate
  14697.   03BA5 CB3B            SRL     E               ;E = E * 4
  14698.   03BA7 CB3B            SRL     E               ;(one byte in FGR memory
  14699.                                                 ;contains 4 pixels)
  14700.   03BA9 1648            LD      D,48H           ;DE = start of FGR memory +
  14701.                                                 ;X-coordinate * 4
  14702.   03BAB 19              ADD     HL,DE           ;Add Y-coordinate * 40 to get
  14703.                                                 ;the byte in FGR screen memory
  14704.                                                 ;that has to be used
  14705.   03BAC E603            AND     03H             ;Compute position of pixel
  14706.   03BAE 3C              INC     A               ;inside byte
  14707.   03BAF 47              LD      B,A             ;B = X-coordinate MOD 4
  14708.   03BB0 3EFC            LD      A,0FCH          ;A = 11111100 (2 bits/pixel)
  14709.   03BB2 0F              RRCA                    ;Shift the 2 zero bits to the
  14710.   03BB3 0F              RRCA                    ;required position
  14711.   03BB4 CB09            RRC     C               ;Shift FCOLOUR code along
  14712.   03BB6 CB09            RRC     C
  14713.   03BB8 10F8            DJNZ    3BB2H           ;Repeat until counter = 0
  14714.  
  14715.   03BBA A6              AND     (HL)            ;Get byte of required pixel
  14716.   03BBB B1              OR      C               ;and set new colour
  14717.   03BBC 77              LD      (HL),A          ;Save new byte value
  14718.   03BBD C9              RET
  14719.  
  14720.  
  14721. ; NPLOT statement
  14722. ; ---------------
  14723.  
  14724.   03BBE 0600            LD      B,00H           ;Set NPLOT
  14725.   03BC0 3A0603          LD      A,(0306H)       ;--
  14726.  
  14727.  
  14728. ; PLOT statement
  14729. ; --------------
  14730.  
  14731. * 03BC1   0603          LD      B,03H           ;Set PLOT
  14732.   03BC3 3A1343          LD      A,(4313H)       ;A = FCOLOUR value
  14733.   03BC6 F5              PUSH    AF              ;Save it on stack
  14734.   03BC7 A0              AND     B               ;And it with B to get either
  14735.                                                 ;black (NPLOT) or the current
  14736.                                                 ;value (PLOT)
  14737.   03BC8 321343          LD      (4313H),A       ;Save new FCOLOUR to use
  14738.   03BCB CD7B3C          CALL    3C7BH           ;'TO' token indicated ?
  14739.   03BCE 382F            JR      C,3BFFH         ;No: continue at 3BFFH
  14740.  
  14741.   03BD0 3A1543          LD      A,(4315H)       ;Get last X-value and
  14742.   03BD3 F5              PUSH    AF              ;save it on stack
  14743.   03BD4 3A1643          LD      A,(4316H)       ;Get last Y-value and
  14744.   03BD7 F5              PUSH    AF              ;save it on stack
  14745.   03BD8 CD1C2B          CALL    2B1CH           ;Get next X-value and
  14746.   03BDB 321543          LD      (4315H),A       ;save it in system RAM
  14747.   03BDE F5              PUSH    AF              ;and on stack
  14748.   03BDF CF              RST     08H             ;Next character must be
  14749.   03BE0 2C              DEFB    ','             ;a comma
  14750.   03BE1 CD1C2B          CALL    2B1CH           ;Get next Y-value and
  14751.   03BE4 321643          LD      (4316H),A       ;save it in system RAM
  14752.   03BE7 D9              EXX                     ;switch register set
  14753.   03BE8 6F              LD      L,A             ;L' = Y2
  14754.   03BE9 D1              POP     DE              ;Restore X-value
  14755.   03BEA 62              LD      H,D             ;H' = X2
  14756.   03BEB C1              POP     BC              ;Restore last Y-value
  14757.   03BEC D1              POP     DE              ;Restore last X-value
  14758.   03BED 58              LD      E,B             ;E' = Y1, D' = X1
  14759.   03BEE D9              EXX                     ;Switch register set
  14760.   03BEF E5              PUSH    HL              ;Save PTP
  14761.   03BF0 D9              EXX                     ;Switch register set
  14762.   03BF1 CD1F3C          CALL    3C1FH           ;PLOT  D , E  TO  H , L
  14763.   03BF4 E1              POP     HL              ;Restore PTP
  14764.   03BF5 CD7B3C          CALL    3C7BH           ;'TO' indicated ?
  14765.   03BF8 28D6            JR      Z,3BD0H         ;Yes, get next X,Y-coordinates
  14766.  
  14767.   03BFA F1              POP     AF              ;Restore old FCOLOUR value
  14768.   03BFB 321343          LD      (4313H),A       ;and store it in system RAM
  14769.   03BFE C9              RET
  14770.  
  14771. ; Get new X,Y-coordinates ('TO' not at the start)
  14772.  
  14773.   03BFF CD1C2B          CALL    2B1CH           ;Get X-coordinate
  14774.   03C02 F5              PUSH    AF              ;Save X-coordinate
  14775.   03C03 CF              RST     08H             ;Next character must be
  14776.   03C04 2C              DEFB    ','             ;a comma
  14777.   03C05 CD1C2B          CALL    2B1CH           ;Get Y-coordinate
  14778.   03C08 F5              PUSH    AF              ;Save Y-coordinate
  14779.   03C09 CD7B3C          CALL    3C7BH           ;'TO' indicated ?
  14780.   03C0C 30CA            JR      NC,3BD8H        ;Yes: get second pair of
  14781.                                                 ;X,Y-coordinates
  14782.   03C0E F1              POP     AF              ;No: Restore Y-coordinate
  14783.   03C0F 321643          LD      (4316H),A       ;Save it in system RAM
  14784.   03C12 57              LD      D,A             ;D = Y-coordinate
  14785.   03C13 F1              POP     AF              ;Restore X-coordinate
  14786.   03C14 321543          LD      (4315H),A       ;Save it in system RAM
  14787.   03C17 5F              LD      E,A             ;E = X-coordinate
  14788.   03C18 EB              EX      DE,HL           ;DE = PTP, H = Y-, L = X-coor.
  14789.   03C19 D5              PUSH    DE              ;Save PTP
  14790.   03C1A CD8A3B          CALL    3B8AH           ;PLOT  L , H
  14791.   03C1D 18D5            JR      3BF4H           ;Restore PTP and previous
  14792.                                                 ;FCOLOUR value
  14793.  
  14794. ; SUB for PLOT
  14795. ; PLOT  D , E  TO  H , L  (AF,BC,DE,HL)
  14796. ; I: D = X-Coordinate of starting point
  14797. ;    E = Y-Coordinate of starting point
  14798. ;    H = X-Coordinate of ending point
  14799. ;    L = Y-Coordinate of ending point
  14800.  
  14801.   03C1F CDC63C          CALL    3CC6H           ;PLOT  H , L (plot end point)
  14802.   03C22 DF              RST     18H             ;HL = DE ? (start and end are
  14803.                                                 ;identical)
  14804.   03C23 C8              RET     Z               ;Yes: done.
  14805.  
  14806.   03C24 00              NOP                     ;--
  14807.   03C25 00              NOP
  14808.   03C26 D5              PUSH    DE              ;Save X1,Y1
  14809.   03C27 7B              LD      A,E             ;A = Y1
  14810.   03C28 95              SUB     L               ;A = Y1 - Y2
  14811.                                                 ;Negative result ?
  14812.   03C29 DC8B3C          CALL    C,3C8BH         ;Yes: negate result and set
  14813.                                                 ;C-flag = 1
  14814.   03C2C CB19            RR      C               ;Put C-flag in bit 7 of C
  14815.   03C2E CB39            SRL     C               ;Shift C to the right
  14816.   03C30 47              LD      B,A             ;B = Y-diff (always positive!)
  14817.   03C31 CB39            SRL     C               ;Shift C to the right
  14818.   03C33 CB39            SRL     C               ;Shift C to the right
  14819.   03C35 7A              LD      A,D             ;A = X1
  14820.   03C36 94              SUB     H               ;A = X1 - X2
  14821.                                                 ;Negative result ?
  14822.   03C37 DC8B3C          CALL    C,3C8BH         ;Yes: negate result and set
  14823.                                                 ;C-flag = 1
  14824.   03C3A CB19            RR      C               ;Put C-flag in bit 7 of C
  14825.   03C3C 37              SCF                     ;C-flag = 1
  14826.   03C3D CB19            RR      C               ;Shift C to the right, bit 7=1
  14827.   03C3F B8              CP      B               ;X-diff < Y-diff ?
  14828.   03C40 384D            JR      C,3C8FH         ;Yes: swap lower and upper
  14829.                                                 ;nibble of C, E = X-diff,
  14830.                                                 ;D = Y-diff
  14831.   03C42 57              LD      D,A             ;No: D = X-diff
  14832.   03C43 78              LD      A,B
  14833.   03C44 5F              LD      E,A             ;E = Y-diff
  14834.  
  14835. ; D now contains the bigger difference and E contains the smaller difference.
  14836. ; Both nibbles of C indicate the angle; the upper nibble for D
  14837. ; and the lower nibble for E
  14838.  
  14839.   03C45 C5              PUSH    BC              ;Save angle
  14840.   03C46 E5              PUSH    HL              ;Save X2,Y2
  14841.   03C47 7A              LD      A,D             ;A = bigger difference
  14842.   03C48 4F              LD      C,A             ;C = A
  14843.   03C49 3E00            LD      A,00H           ;A = 0
  14844.   03C4B 57              LD      D,A             ;D = 0
  14845.   03C4C 47              LD      B,A             ;B = 0, so BC = bigger diff.
  14846.   03C4D 67              LD      H,A             ;H = 0
  14847.   03C4E 7B              LD      A,E             ;A = smaller difference
  14848.   03C4F 6F              LD      L,A             ;L = A, so HL = smaller diff
  14849.   03C50 CB25            SLA     L               ;HL = HL * 2
  14850.   03C52 CB14            RL      H
  14851.   03C54 ED42            SBC     HL,BC           ;HL = smaller difference * 2 -
  14852.                                                 ;bigger difference
  14853.   03C56 CB21            SLA     C               ;BC = BC * 2
  14854.   03C58 CB10            RL      B
  14855.   03C5A CB23            SLA     E               ;E = smaller difference * 2
  14856.   03C5C CB12            RL      D               ;D = bigger difference * 2
  14857.   03C5E 7C              LD      A,H             ;A = H
  14858.   03C5F D9              EXX                     ;Switch register sets
  14859.   03C60 E1              POP     HL              ;Restore X2,Y2
  14860.   03C61 C1              POP     BC              ;Restore angle
  14861.   03C62 D1              POP     DE              ;Restore X1,Y1
  14862.  
  14863. ; Calculate and plot next point
  14864.  
  14865.   03C63 CB27            SLA     A               ;A,7 into C-flag
  14866.                                                 ;C-flag = 1 ?
  14867.   03C65 D4A03C          CALL    NC,3CA0H        ;No: change X2 and Y2
  14868.                                                 ;according to the angle of
  14869.                                                 ;the smaller difference
  14870.   03C68 CDAF3C          CALL    3CAFH           ;Change X2 and Y2 according to
  14871.                                                 ;the angle of the bigger diff.
  14872.   03C6B CDC63C          CALL    3CC6H           ;PLOT  H , L
  14873.   03C6E 7A              LD      A,D             ;A = X1
  14874.   03C6F BC              CP      H               ;X1 = X2 ?
  14875.   03C70 2003            JR      NZ,3C75H        ;No: set next point
  14876.  
  14877.   03C72 7B              LD      A,E             ;A = Y1
  14878.   03C73 BD              CP      L               ;Y1 = Y2 ?
  14879.   03C74 C8              RET     Z               ;Yes: done.
  14880.  
  14881.   03C75 D9              EXX                     ;Switch register sets
  14882.   03C76 19              ADD     HL,DE           ;HL' = HL' + DE'
  14883.   03C77 7C              LD      A,H             ;A = H'
  14884.   03C78 D9              EXX                     ;Switch register sets
  14885.   03C79 18E8            JR      3C63H           ;Set next point
  14886.  
  14887. ; 'TO' indicated ?
  14888.  
  14889.   03C7B 7E              LD      A,(HL)          ;A = next character
  14890.   03C7C FEBD            CP      0BDH            ;'TO' token ?
  14891.   03C7E 200D            JR      NZ,3C8DH        ;No: C-flag = 1
  14892.  
  14893.   03C80 23              INC     HL              ;PTP + 1
  14894.   03C81 AF              XOR     A               ;C-flag = 0
  14895.   03C82 C9              RET
  14896.  
  14897.  
  14898. ; FCLS statement
  14899. ; --------------
  14900.  
  14901.   03C83 2B              DEC     HL              ;Adjust PTP
  14902.   03C84 D7              RST     10H             ;Get next non-space character
  14903.   03C85 3E00            LD      A,00H           ;A = 00H
  14904.   03C87 C34A38          JP      384AH           ;Continue ar 384AH
  14905.  
  14906.   03C8A 00              NOP                     ;--
  14907.  
  14908. ; A = -A, C-flag = 1
  14909.  
  14910.   03C8B ED44            NEG                     ;A = -A
  14911.   03C8D 37              SCF                     ;C-flag = 1
  14912.   03C8E C9              RET
  14913.  
  14914. ; Swap upper and lower nibble of C, E = X-diff, D = Y-diff (see 3C40H)
  14915.  
  14916.   03C8F CD973C          CALL    3C97H           ;Swap nibbles of C
  14917.   03C92 5F              LD      E,A             ;E = X-diff
  14918.   03C93 78              LD      A,B             ;A = Y-diff
  14919.   03C94 57              LD      D,A             ;D = A
  14920.   03C95 18AE            JR      3C45H           ;Back to 3C45H
  14921.  
  14922. ; Swap upper and lower nibble of C
  14923.  
  14924.   03C97 CB09            RRC     C               ;Rotate C left 4 times
  14925.   03C99 CB09            RRC     C
  14926.   03C9B CB09            RRC     C
  14927.   03C9D CB09            RRC     C
  14928.   03C9F C9              RET
  14929.  
  14930. ; Change X2 and Y2 according to the angle of the smaller difference
  14931.  
  14932.   03CA0 CD973C          CALL    3C97H           ;Swap nibbles of C
  14933.   03CA3 CDAF3C          CALL    3CAFH           ;Change X2 and Y2
  14934.   03CA6 CD973C          CALL    3C97H           ;Swap nibbles of C
  14935.   03CA9 D9              EXX                     ;Switch register sets
  14936.   03CAA B7              OR      A               ;C-flag = 0
  14937.   03CAB ED42            SBC     HL,BC           ;HL' = HL' - BC'
  14938.   03CAD D9              EXX                     ;Switch register sets
  14939.   03CAE C9              RET
  14940.  
  14941. ; Change X2 and Y2 according to the angle of the bigger difference
  14942.  
  14943.   03CAF CB79            BIT     7,C             ;Change Y2 ?
  14944.   03CB1 CABD3C          JP      Z,3CBDH         ;Yes: continue at 3CBDH
  14945.  
  14946.   03CB4 CB71            BIT     6,C             ;Increase X2 ?
  14947.   03CB6 C2BB3C          JP      NZ,3CBBH        ;No: continue at 3CBBH
  14948.  
  14949.   03CB9 24              INC     H               ;X2 + 1
  14950.   03CBA C9              RET
  14951.  
  14952.   03CBB 25              DEC     H               ;X2 - 1
  14953.   03CBC C9              RET
  14954.  
  14955.   03CBD CB71            BIT     6,C             ;Increment Y2 ?
  14956.   03CBF C2C43C          JP      NZ,3CC4H        ;No: continue at 3CC4H
  14957.  
  14958.   03CC2 2C              INC     L               ;Y2 + 1
  14959.   03CC3 C9              RET
  14960.  
  14961.   03CC4 2D              DEC     L               ;Y2 - 1
  14962.   03CC5 C9              RET
  14963.  
  14964.  
  14965. ; SUB for PLOT
  14966. ; PLOT  H , L  (AF)
  14967. ;
  14968. ; I: H = X-coordinate
  14969. ;    L = Y-coordinate
  14970.  
  14971.   03CC6 C5              PUSH    BC              ;Save registers
  14972.   03CC7 D5              PUSH    DE
  14973.   03CC8 E5              PUSH    HL
  14974.   03CC9 7C              LD      A,H             ;Swap H and L
  14975.   03CCA 65              LD      H,L
  14976.   03CCB 6F              LD      L,A
  14977.   03CCC CD8A3B          CALL    3B8AH           ;PLOT  H , L
  14978.   03CCF E1              POP     HL              ;Restore registers
  14979.   03CD0 D1              POP     DE
  14980.   03CD1 C1              POP     BC
  14981.   03CD2 C9              RET
  14982.  
  14983.  
  14984. ; XSHAPE statement
  14985. ; ----------------
  14986.   03CD3 110303          LD      DE,0303H        ;Set bit mask for XSHAPE
  14987.   03CD6 1808            JR      3CE0H           ;Continue at 3CE0H
  14988.  
  14989.  
  14990. ; NSHAPE statement
  14991. ; ----------------
  14992.  
  14993.   03CD8 110000          LD      DE,0000H        ;Set bit mask for NSHAPE
  14994.   03CDB 1803            JR      3CE0H           ;Continue at 3CE0H
  14995.  
  14996.  
  14997. ; SHAPE statement
  14998. ; ---------------
  14999.  
  15000.   03CDD 110003          LD      DE,0300H        ;Set bit mask for SHAPE
  15001.  
  15002.   03CE0 ED531743        LD      (4317H),DE      ;Save bit mask
  15003.   03CE4 2B              DEC     HL              ;Adjust PTP
  15004.   03CE5 D7              RST     10H             ;Get next non-space character
  15005.   03CE6 CD1C2B          CALL    2B1CH           ;Get X coordinate
  15006.   03CE9 F5              PUSH    AF              ;Save X coordinate
  15007.   03CEA CF              RST     08H             ;Next character must be
  15008.   03CEB 2C              DEFB    ','             ;a comma
  15009.   03CEC CD1C2B          CALL    2B1CH           ;Get Y coordinate
  15010.   03CEF D1              POP     DE              ;D = X coordinate
  15011.   03CF0 5F              LD      E,A             ;E = Y coordinate
  15012.   03CF1 E5              PUSH    HL              ;Save PTP
  15013.   03CF2 C3A235          JP      35A2H           ;Continue at 35A2H
  15014.  
  15015.   03CF5 46              LD      B,(HL)          ;B = length of SHAPE table
  15016.   03CF6 23              INC     HL              ;Update SHAPE table pointer
  15017.   03CF7 3A1443          LD      A,(4314H)       ;A = SCALE factor
  15018.   03CFA B7              OR      A               ;SCALE = 0 ?
  15019.   03CFB 284B            JR      Z,3D48H         ;Yes: done.
  15020.  
  15021.   03CFD 4F              LD      C,A             ;C = SCALE factor
  15022.   03CFE 7E              LD      A,(HL)          ;A = entry from SHAPE table
  15023.   03CFF CB2F            SRA     A               ;Take upper nibble
  15024.   03D01 CB2F            SRA     A
  15025.   03D03 CB2F            SRA     A
  15026.   03D05 CB2F            SRA     A
  15027.  
  15028.   03D07 08              EX      AF,AF'
  15029.   03D08 3E01            LD      A,01H           ;A' = 1 = flag for processing
  15030.                                                 ;of first segment (every table
  15031.                                                 ;entry contains 2 segments)
  15032.   03D0A 08              EX      AF,AF'
  15033.  
  15034.   03D0B F5              PUSH    AF              ;Save table value
  15035.   03D0C CB2F            SRA     A               ;Direction: vertical ? (bit0=1)
  15036.   03D0E 383D            JR      C,3D4DH         ;Yes: continue at 3D4DH
  15037.  
  15038.   03D10 CB2F            SRA     A               ;Direction: left ? (bit1=1)
  15039.   03D12 3836            JR      C,3D4AH         ;Yes: continue at 3D4AH
  15040.  
  15041.   03D14 14              INC     D               ;X + 1 (direction: right)
  15042.   03D15 C5              PUSH    BC              ;Save registers
  15043.   03D16 D5              PUSH    DE
  15044.   03D17 E5              PUSH    HL
  15045.   03D18 6F              LD      L,A             ;L = table value
  15046.   03D19 3A1343          LD      A,(4313H)       ;A = FCOLOUR value
  15047.   03D1C F5              PUSH    AF              ;Save it
  15048.   03D1D 7D              LD      A,L             ;Table value bacl to A
  15049.   03D1E 2A1743          LD      HL,(4317H)      ;Take bit mask for operation
  15050.   03D21 A4              AND     H               ;Mask colour value
  15051.   03D22 AD              XOR     L
  15052.   03D23 321343          LD      (4313H),A       ;Store as new FCOLOUR value
  15053.   03D26 6A              LD      L,D             ;Coordinates in HL
  15054.   03D27 63              LD      H,E
  15055.   03D28 CD8A3B          CALL    3B8AH           ;PLOT  L , H
  15056.   03D2B F1              POP     AF              ;Restore original FCOLOUR value
  15057.   03D2C 321343          LD      (4313H),A       ;Store it in system RAM
  15058.   03D2F E1              POP     HL              ;Restore registers
  15059.   03D30 D1              POP     DE
  15060.   03D31 C1              POP     BC
  15061.   03D32 F1              POP     AF              ;Restore table value
  15062.   03D33 0D              DEC     C               ;SCALE factor > 1 ?
  15063.   03D34 20D5            JR      NZ,3D0BH        ;Yes: process same table value
  15064.                                                 ;again (C times)
  15065.   03D36 C5              PUSH    BC              ;Save table counter
  15066.   03D37 08              EX      AF,AF'
  15067.   03D38 3D              DEC     A               ;Flag = 1 ?
  15068.   03D39 47              LD      B,A             ;B = flag - 1
  15069.   03D3A 08              EX      AF,AF'
  15070.   03D3B 04              INC     B               ;B = 0 ?
  15071.   03D3C 05              DEC     B               ;(then flag was 1)
  15072.   03D3D C1              POP     BC              ;Restore table counter
  15073.   03D3E 3A1443          LD      A,(4314H)       ;A = SCALE factor
  15074.   03D41 4F              LD      C,A             ;C = SCALE factor
  15075.   03D42 7E              LD      A,(HL)          ;A = table value
  15076.                                                 ;Flag was 1 ?
  15077.   03D43 28C6            JR      Z,3D0BH         ;Yes: process second segment
  15078.  
  15079.   03D45 23              INC     HL              ;Table pointer + 1
  15080.   03D46 10AF            DJNZ    3CF7H           ;Next entry in SHAPE table
  15081.  
  15082.   03D48 E1              POP     HL              ;Restore PTP
  15083.   03D49 C9              RET
  15084.  
  15085. ; Direction: left
  15086.  
  15087.   03D4A 15              DEC     D               ;X-coordinate - 1
  15088.   03D4B 18C8            JR      3D15H           ;Back to SCALE routine
  15089.  
  15090. ; Direction: vertical
  15091.  
  15092.   03D4D CB2F            SRA     A               ;Direction: up ? (Bit1=1)
  15093.   03D4F 3803            JR      C,3D54H         ;Yes: continue at 3D45H
  15094.  
  15095.   03D51 1C              INC     E               ;Y-coordinate + 1 (down)
  15096.   03D52 18C1            JR      3D15H           ;Back to SCALE routine
  15097.  
  15098. ; Direction: up
  15099.  
  15100.   03D54 1D              DEC     E               ;Y-coordinate - 1
  15101.   03D55 18BE            JR      3D15H           ;Back to SCALE routine
  15102.  
  15103.  
  15104. ; SUB for tokenizing (see 392AH)
  15105. ; Store Colour-Token into coded line.
  15106.  
  15107.   03D57 0C              INC     C               ;Character counter + 1
  15108.   03D58 23              INC     HL              ;Buffer pointer + 1
  15109.   03D59 EB              EX      DE,HL           ;DE = buffer pointer (on coded
  15110.                                                 ;text)
  15111.                                                 ;HL = text pointer (on uncoded
  15112.                                                 ;text)
  15113.   03D5A 23              INC     HL              ;Text pointer + 1
  15114.   03D5B 12              LD      (DE),A          ;Store token in buffer
  15115.   03D5C 13              INC     DE              ;Buffer pointer + 1
  15116.   03D5D 0C              INC     C               ;Character counter + 1
  15117.   03D5E C3CC1B          JP      1BCCH           ;Continue at 1BCCH
  15118.  
  15119.  
  15120. ; PLAY statement
  15121. ; --------------
  15122.  
  15123.   03D61 CF              RST     08H             ;Next character must be
  15124.   03D62 28              DEFB    '('             ;a '('
  15125.   03D63 CDC23F          CALL    3FC2H           ;Get channel number - 1
  15126.   03D66 FE03            CP      03H             ;In range (1 to 3) ?
  15127.   03D68 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15128.  
  15129.   03D6B F5              PUSH    AF              ;Save channel number
  15130.   03D6C CF              RST     08H             ;Next character must be
  15131.   03D6D 2C              DEFB    ','             ;a comma.
  15132.   03D6E CDC43F          CALL    3FC4H           ;Get octave number - 1
  15133.   03D71 FE08            CP      08H             ;In range (1 to 8) ?
  15134.   03D73 30F3            JR      NC,3D68H        ;No: ?FC Error
  15135.  
  15136.   03D75 3C              INC     A               ;Adjust octave number
  15137.   03D76 F5              PUSH    AF              ;Save octave number
  15138.   03D77 CF              RST     08H             ;Next character must be
  15139.   03D78 2C              DEFB    ','             ;a comma
  15140.   03D79 CD1C2B          CALL    2B1CH           ;Get note number
  15141.   03D7C B7              OR      A               ;Note number zero ?
  15142.   03D7D 283F            JR      Z,3DBEH         ;Yes: continue at 3DBEH
  15143.  
  15144.   03D7F FE1E            CP      1EH             ;In range (1 to 29)
  15145.   03D81 30E5            JR      NC,3D68H        ;No: ?FC Error
  15146.  
  15147.   03D83 CB27            SLA     A               ;A * 2 to get offset
  15148.   03D85 5F              LD      E,A             ;DE = note table offset
  15149.   03D86 1600            LD      D,00H
  15150.   03D88 CF              RST     08H             ;Next character must be
  15151.   03D89 2C              DEFB    ','             ;a comma
  15152.   03D8A E5              PUSH    HL              ;Save PTP
  15153.  
  15154.   03D8B 21CF3D          LD      HL,3DCFH        ;HL -> note table
  15155.   03D8E 19              ADD     HL,DE           ;Add offset
  15156.   03D8F 5E              LD      E,(HL)          ;DE = note frequency
  15157.   03D90 23              INC     HL
  15158.   03D91 56              LD      D,(HL)
  15159.   03D92 E1              POP     HL              ;Restore PTP
  15160.   03D93 C1              POP     BC              ;B = octave number
  15161.   03D94 05              DEC     B               ;Adjust to use as counter
  15162.                                                 ;Counter zero ?
  15163.   03D95 2806            JR      Z,3D9DH         ;Yes: continue at 3D9DH
  15164.  
  15165.   03D97 CB3A            SRL     D               ;Do DE = DE / 2 for every
  15166.   03D99 CB1B            RR      E               ;octave: double frequency
  15167.   03D9B 10FA            DJNZ    3D97H           ;Loop
  15168.  
  15169.   03D9D F1              POP     AF              ;A = channel number
  15170.   03D9E F5              PUSH    AF              ;Save it again
  15171.   03D9F CB27            SLA     A               ;Calculate register address
  15172.   03DA1 3C              INC     A               ;A = A * 2 + 1
  15173.   03DA2 CD2A3E          CALL    3E2AH           ;Write note freq. into PSG
  15174.   03DA5 CD1C2B          CALL    2B1CH           ;Get volume value
  15175.   03DA8 FE11            CP      11H             ;In range (0 to 16) ?
  15176.   03DAA 30BC            JR      NC,3D68H        ;No: ?FC Error
  15177.  
  15178.   03DAC D5              PUSH    DE              ;Save frequency setting
  15179.   03DAD 1E38            LD      E,38H           ;Select:
  15180.   03DAF 3E07            LD      A,07H           ;A = PSG register 7
  15181.   03DB1 CD323E          CALL    3E32H           ;Write to PSG
  15182.   03DB4 D1              POP     DE              ;Restore frequency setting
  15183.   03DB5 F1              POP     AF              ;Restore channel number
  15184.  
  15185.   03DB6 C608            ADD     A,08H           ;Calculate register address
  15186.   03DB8 CD323E          CALL    3E32H           ;Write to PSG
  15187.   03DBB CF              RST     08H             ;Next character must be
  15188.   03DBC 29              DEFB    ')'             ;a ')'
  15189.   03DBD C9              RET
  15190.  
  15191.   03DBE CF              RST     08H             ;Next character must be
  15192.   03DBF 2C              DEFB    ','             ;a comma
  15193.   03DC0 CD1C2B          CALL    2B1CH           ;Get amplitude value
  15194.   03DC3 FE11            CP      11H             ;In range (0 to 16) ?
  15195.   03DC5 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15196.  
  15197.   03DC8 F1              POP     AF              ;Correct stack
  15198.   03DC9 F1              POP     AF              ;Get channel number
  15199.   03DCA 1E00            LD      E,00H           ;Amplitude = 0
  15200.   03DCC C3B63D          JP      3DB6H           ;Continue at 3DB6H
  15201.  
  15202.  
  15203. ; Note table containing the frequency register values for the PSG
  15204.  
  15205.   03DCF 0000            DEFW    0000H
  15206.   03DD1 5D0D            DEFW    0D5DH
  15207.   03DD3 E70B            DEFW    0BE7H
  15208.   03DD5 9B0A            DEFW    0A9BH
  15209.   03DD7 020A            DEFW    0A02H
  15210.   03DD9 EB08            DEFW    08EBH
  15211.   03DDB F207            DEFW    07F2H
  15212.   03DDD 1407            DEFW    0714H
  15213.   03DDF 9C0C            DEFW    0C9CH
  15214.   03DE1 3C0B            DEFW    0B3CH
  15215.   03DE3 7309            DEFW    0973H
  15216.   03DE5 6B08            DEFW    086BH
  15217.   03DE7 8007            DEFW    0780H
  15218.   03DE9 5D0D            DEFW    0D5DH
  15219.   03DEB 5D0D            DEFW    0D5DH
  15220.   03DED 5D0D            DEFW    0D5DH
  15221.   03DEF 4A09            DEFW    094AH
  15222.   03DF1 9010            DEFW    1090H
  15223.   03DF3 C00E            DEFW    0EC0H
  15224.   03DF5 240D            DEFW    0D24H
  15225.   03DF7 680C            DEFW    0C68H
  15226.   03DF9 0C0B            DEFW    0B0CH
  15227.   03DFB D809            DEFW    09D8H
  15228.   03DFD C808            DEFW    08C8H
  15229.   03DFF A00F            DEFW    0FA0H
  15230.   03E01 EB0D            DEFW    0DEBH
  15231.   03E03 B40B            DEFW    0BB4H
  15232.   03E05 700A            DEFW    0A70H
  15233.   03E07 4A09            DEFW    094AH
  15234.   03E09 4808            DEFW    0848H
  15235.  
  15236.   03E0B F6B7            OR      0B7H            ;--
  15237.   03E0D C9              RET
  15238.  
  15239.  
  15240. ; SUB for ? (not used)
  15241. ; Write DE in X. If DE > 0 then VT = INT else VT = SNG
  15242.  
  15243.   03E0E CB7A            BIT     7,D             ;D negative ?
  15244.   03E10 2813            JR      Z,3E25H         ;No: Write DE in X as INT
  15245.  
  15246.   03E12 CDEF0A          CALL    0AEFH           ;VT = SNG
  15247.   03E15 7A              LD      A,D             ;A <- D <- E <- 00H
  15248.   03E16 53              LD      D,E             ;(Shift DE 8 bits to the left,
  15249.   03E17 1E00            LD      E,00H           ;overflow in A)
  15250.   03E19 B7              OR      A               ;C-flag = 0
  15251.   03E1A 1F              RRA                     ;ADE back 1 bit to the right
  15252.   03E1B CB1A            RR      D
  15253.   03E1D CB1B            RR      E
  15254.   03E1F 0691            LD      B,91H           ;B = Exp for 2 ^ 17
  15255.   03E21 CD6909          CALL    0969H           ;SFLOAT (X)
  15256.   03E24 C9              RET
  15257.  
  15258.   03E25 EB              EX      DE,HL           ;HL = DE
  15259.   03E26 CD9A0A          CALL    0A9AH           ;Write HL to X as INT
  15260.   03E29 C9              RET
  15261.  
  15262.  
  15263. ; SUB for PSG programming
  15264. ; Wwrites the 16 bit value in DE into PSG registers addressed by A
  15265. ;
  15266. ; I: A = register number of MSB data register
  15267. ;    DE = register data
  15268.  
  15269.   03E2A 47              LD      B,A             ;Save register number in B
  15270.   03E2B D3F8            OUT     (0F8H),A        ;Select PSG register
  15271.   03E2D 7A              LD      A,D             ;Register data from D to A
  15272.   03E2E D3F9            OUT     (0F9H),A        ;Write data in PSG register
  15273.   03E30 05              DEC     B               ;register number - 1
  15274.   03E31 78              LD      A,B             ;Put it in A
  15275.  
  15276. ; Now write LSB register with value in E
  15277.  
  15278.   03E32 D3F8            OUT     (0F8H),A        ;Select PSG register
  15279.   03E34 7B              LD      A,E             ;Register data from E to A
  15280.   03E35 D3F9            OUT     (0F9H),A        ;Write data in PSG register
  15281.   03E37 C9              RET
  15282.  
  15283.  
  15284. ; PAINT statement
  15285. ; ---------------
  15286.  
  15287.   03E38 AF              XOR     A               ;Number of parameters = 0
  15288.   03E39 2B              DEC     HL              ;Adjust PTP
  15289.  
  15290.   03E3A 3C              INC     A               ;Number of parameters + 1
  15291.   03E3B 08              EX      AF,AF'          ;A' = counter
  15292.   03E3C D7              RST     10H             ;Get next non-space character
  15293.   03E3D CD1C2B          CALL    2B1CH           ;Get next parameter
  15294.   03E40 F5              PUSH    AF              ;Save parameter on stack
  15295.   03E41 2B              DEC     HL              ;Adjust PTP
  15296.   03E42 D7              RST     10H             ;Get next non-space character
  15297.                                                 ;End reached ?
  15298.   03E43 280C            JR      Z,3E51H         ;Yes: continue at 3E51H
  15299.  
  15300.   03E45 FE2C            CP      2CH             ;Comma ?
  15301.   03E47 2005            JR      NZ,3E4EH        ;No: ?SN Error
  15302.  
  15303.   03E49 08              EX      AF,AF'          ;A = counter
  15304.   03E4A FE05            CP      05H             ;Maximum number of parameters
  15305.                                                 ;(4) exceeded ?
  15306.   03E4C 38EC            JR      C,3E3AH         ;No: continue at 3E3AH
  15307.  
  15308.   03E4E C39719          JP      1997H           ;?SN Error
  15309.  
  15310. ; Evaluate the parameters on stack
  15311.  
  15312.   03E51 08              EX      AF,AF'          ;A = counter
  15313.   03E52 FE03            CP      03H             ;At least 2 parameters ?
  15314.   03E54 38F8            JR      C,3E4EH         ;No: ?SN error
  15315.  
  15316.   03E56 3D              DEC     A               ;Correct A to get the number
  15317.   03E57 3D              DEC     A               ;border colours
  15318.   03E58 321D43          LD      (431DH),A       ;Store this in A
  15319.   03E5B 111843          LD      DE,4318H        ;DE -> system RAM area
  15320.                                                 ;for PAINT statement
  15321.   03E5E 47              LD      B,A             ;Number of border colours
  15322.   03E5F 83              ADD     A,E             ;DE -> last byte in
  15323.   03E60 5F              LD      E,A             ;system RAM area for PAINT
  15324.  
  15325.   03E61 F1              POP     AF              ;Get value from stack
  15326.   03E62 3D              DEC     A               ;-1
  15327.   03E63 FE04            CP      04H             ;Colour value in range ?
  15328.   03E65 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15329.  
  15330.   03E68 12              LD      (DE),A          ;Store in system RAM
  15331.   03E69 1B              DEC     DE              ;pointer - 1
  15332.   03E6A 10F5            DJNZ    3E61H           ;Next parameter
  15333.  
  15334.   03E6C F1              POP     AF              ;A = Y coordinate
  15335.   03E6D FE66            CP      66H             ;Y coordinate in range ?
  15336.   03E6F 30F4            JR      NC,3E65H        ;No: ?FC Error
  15337.  
  15338.   03E71 57              LD      D,A             ;D = Y coordinate
  15339.   03E72 F1              POP     AF              ;A = X coordinate
  15340.   03E73 FEA0            CP      0A0H            ;X coordinate in range ?
  15341.   03E75 30EE            JR      NC,3E65H        ;No: ?FC Error
  15342.  
  15343.   03E77 5F              LD      E,A             ;E = X coordinate
  15344.   03E78 E5              PUSH    HL              ;Save PTP
  15345.   03E79 21FFFF          LD      HL,0FFFFH       ;HL = -1 (flag for routine end)
  15346.   03E7C E5              PUSH    HL              ;Save flag
  15347.   03E7D EB              EX      DE,HL           ;HL = Y,X
  15348.  
  15349. ; Set next point
  15350.  
  15351.   03E7E 221E43          LD      (431EH),HL      ;Save coordinates in system RAM
  15352.   03E81 CD043F          CALL    3F04H           ;Set FCOLOUR if border not
  15353.                                                 ;reached
  15354.   03E84 2806            JR      Z,3E8CH         ;Continue at 3E8CH when border
  15355.                                                 ;has been reached
  15356.   03E86 2D              DEC     L               ;X - 1
  15357.   03E87 3EFF            LD      A,0FFH          ;A = -1
  15358.   03E89 BD              CP      L               ;X = -1 ?
  15359.   03E8A 20F5            JR      NZ,3E81H        ;No: next point
  15360.  
  15361.   03E8C 2C              INC     L               ;X + 1
  15362.   03E8D 7D              LD      A,L             ;A = X-coordinate
  15363.   03E8E 322043          LD      (4320H),A       ;Save it (left edge)
  15364.   03E91 2A1E43          LD      HL,(431EH)      ;Last coordinates to HL
  15365.   03E94 2C              INC     L               ;X + 1
  15366.   03E95 3EA0            LD      A,0A0H          ;A = 160
  15367.   03E97 BD              CP      L               ;X = 160 ?
  15368.   03E98 2805            JR      Z,3E9FH         ;Yes: continue at 3E9FH
  15369.  
  15370.   03E9A CD043F          CALL    3F04H           ;Set FCOLOUR if border not
  15371.                                                 ;reached
  15372.   03E9D 20F5            JR      NZ,3E94H        ;If border not reached:
  15373.                                                 ;next point
  15374.   03E9F 2D              DEC     L               ;X - 1
  15375.   03EA0 7D              LD      A,L             ;A = X-coordinate
  15376.   03EA1 322143          LD      (4321H),A       ;Save it (right edge)
  15377.   03EA4 3A1F43          LD      A,(431FH)       ;A = Y-coordinate
  15378.   03EA7 A7              AND     A               ;Y = 0 ?
  15379.   03EA8 280C            JR      Z,3EB6H         ;Yes: continue at 3EB6H
  15380.  
  15381.   03EAA 3D              DEC     A               ;Y - 1
  15382.   03EAB 67              LD      H,A             ;H = Y-coordinate
  15383.   03EAC CDC33E          CALL    3EC3H           ;Check Y-coordinate
  15384.   03EAF 3A1F43          LD      A,(431FH)       ;A = Y-coordinate
  15385.   03EB2 FE65            CP      65H             ;Y = 102 ?
  15386.   03EB4 2805            JR      Z,3EBBH         ;Yes: continue at 3EBBH
  15387.  
  15388.   03EB6 3C              INC     A               ;Y + 1
  15389.   03EB7 67              LD      H,A             ;H = Y-coordinate
  15390.   03EB8 CDC33E          CALL    3EC3H           ;Check Y-coordinate
  15391.   03EBB 3EFF            LD      A,0FFH          ;A = -1
  15392.   03EBD E1              POP     HL              ;Restore Y-coordinate
  15393.   03EBE BC              CP      H               ;Flag reached ?
  15394.   03EBF 20BD            JR      NZ,3E7EH        ;No: next point
  15395.  
  15396.   03EC1 E1              POP     HL              ;Restore PTP
  15397.   03EC2 C9              RET
  15398.  
  15399.  
  15400. ; Check Y-coordinate
  15401. ;
  15402. ; I: H = Y-coordinate
  15403.  
  15404.   03EC3 0EFF            LD      C,0FFH          ;C = 255 (flag)
  15405.   03EC5 3A2043          LD      A,(4320H)       ;A = X-coordinate of left edge
  15406.   03EC8 6F              LD      L,A             ;L = left edge
  15407.   03EC9 3A2143          LD      A,(4321H)       ;A = X-coordinate right edge
  15408.   03ECC 95              SUB     L               ;A = distance between left
  15409.                                                 ;and right edge
  15410.   03ECD 47              LD      B,A             ;B = distance
  15411.   03ECE 04              INC     B               ;B + 1
  15412.   03ECF C8              RET     Z               ;Done when both edges are
  15413.                                                 ;identical
  15414.  
  15415. ; Check complete line of new Y-coordinate
  15416.  
  15417.   03ED0 C5              PUSH    BC              ;Save register
  15418.   03ED1 E5              PUSH    HL
  15419.   03ED2 CDF13E          CALL    3EF1H           ;Border colour at L,H reached ?
  15420.   03ED5 E1              POP     HL              ;Restore registers
  15421.   03ED6 C1              POP     BC
  15422.   03ED7 2006            JR      NZ,3EDFH        ;No: continue at 3EDFH
  15423.  
  15424.   03ED9 0EFF            LD      C,0FFH          ;Yes: flag = 255
  15425.   03EDB 2C              INC     L               ;X + 1
  15426.   03EDC 10F2            DJNZ    3ED0H           ;Check next X-coordinate
  15427.  
  15428.   03EDE C9              RET
  15429.  
  15430.  
  15431. ; Border colour not reaches at new Y-coordinate
  15432.  
  15433.   03EDF AF              XOR     A               ;A = 0
  15434.   03EE0 B9              CP      C               ;Flag = 0 ?
  15435.                                                 ;(This area already recognized)
  15436.   03EE1 28F8            JR      Z,3EDBH         ;Yes: continue at 3EDBH
  15437.  
  15438.   03EE3 0E02            LD      C,02H           ;C = 2 (for 1963H)
  15439.   03EE5 C5              PUSH    BC              ;Save BC
  15440.  
  15441.   03EE6 CD6319          CALL    1963H           ;Stack has still room enough ?
  15442.   03EE9 C1              POP     BC              ;Restore BC
  15443.   03EEA D1              POP     DE              ;RET address to DE
  15444.   03EEB E5              PUSH    HL              ;Save X,Y coordinates
  15445.   03EEC D5              PUSH    DE              ;Put RET address back on stack
  15446.   03EED 0E00            LD      C,00H           ;Flag = 0
  15447.   03EEF 18EA            JR      3EDBH           ;Continue at 3EDBH
  15448.  
  15449.  
  15450. ; Border colour reached at point L,H ?
  15451. ; Z-flag = 0: No
  15452. ; Z-flag = 1: Yes
  15453.  
  15454.   03EF1 CD3A3F          CALL    3F3AH           ;A = CPOINT( L,H )
  15455.   03EF4 E5              PUSH    HL              ;Save coordinated
  15456.   03EF5 211D43          LD      HL,431DH        ;HL -> number of border colours
  15457.   03EF8 46              LD      B,(HL)          ;B = number of border colours
  15458.   03EF9 211943          LD      HL,4319H        ;HL -> border colour table
  15459.   03EFC BE              CP      (HL)            ;Border colour reached ?
  15460.   03EFD 2803            JR      Z,3F02H         ;Yes: done.
  15461.  
  15462.   03EFF 23              INC     HL              ;Pointer + 1
  15463.   03F00 10FA            DJNZ    3EFCH           ;Check next colour
  15464.  
  15465.   03F02 E1              POP     HL              ;Restore coordinates
  15466.   03F03 C9              RET
  15467.  
  15468.  
  15469. ; Set new colour for point L,H in case border colour is not reached yet.
  15470.  
  15471.   03F04 E5              PUSH    HL              ;Save coordinates
  15472.   03F05 CDF13E          CALL    3EF1H           ;Border colour reached ?
  15473.   03F08 F5              PUSH    AF              ;Save flags
  15474.   03F09 41              LD      B,C             ;B = X MOD 4 (from CPOINT)
  15475.   03F0A 3A1943          LD      A,(4319H)       ;A = new FCOLOUR
  15476.   03F0D 4F              LD      C,A             ;C = A
  15477.   03F0E C4B03B          CALL    NZ,3BB0H        ;If border colour not reached:
  15478.                                                 ;set new colour
  15479.   03F11 F1              POP     AF              ;Restore flags
  15480.   03F12 E1              POP     HL              ;Restore coordinates
  15481.   03F13 C9              RET
  15482.  
  15483.  
  15484. ; Old KEYPAD1 routine (no longer used)
  15485.  
  15486.   03F14 16FE            LD      D,0FEH
  15487.   03F16 1802            JR      3F1AH
  15488.  
  15489. ; Old KEYPAD2 routine (no longer used)
  15490.  
  15491.   03F18 16F7            LD      D,0F7H
  15492.   03F1A CD873A          CALL    3A87H
  15493.   03F1D 6F              LD      L,A
  15494.   03F1E 2600            LD      H,00H
  15495.   03F20 C9              RET
  15496.  
  15497.  
  15498. ; SUB for processing expressions (at 2337H)
  15499. ; Continuation of 3F7AH: interception of Colour-BASIC functions
  15500.  
  15501.   03F21 FE91            CP      91H             ;Token for SOUND ?
  15502.   03F23 CAF436          JP      Z,36F4H         ;Yes: continue at 36F4H
  15503.  
  15504.   03F26 FE8A            CP      8AH             ;Token for SCALE ?
  15505.   03F28 CA0A37          JP      Z,370AH         ;Yes: continue at 370AH
  15506.  
  15507.   03F2B FE80            CP      80H             ;Token for COLOUR ?
  15508.   03F2D CA6136          JP      Z,3661H         ;Yes: continue at 3661H
  15509.  
  15510.   03F30 C30525          JP      2505H           ;Continue at 2505H
  15511.  
  15512.  
  15513. ; VERIFY statement
  15514. ; ----------------
  15515.  
  15516.   03F33 23              INC     HL              ;PTP + 1
  15517.   03F34 C3292C          JP      2C29H           ;Continue at 2C29H
  15518.  
  15519.   03F37 6E              LD      L,(HL)          ;--
  15520.   03F38 63              LD      H,E
  15521.   03F39 77              LD      (HL),A
  15522.  
  15523.  
  15524. ; SUB for CPOINT and PAINT
  15525. ; This routine takes the colour value for pixel at (X,Y)
  15526. ;
  15527. ; I: L = X coordinate
  15528. ;    H = Y coordinate
  15529. ; O: A = colour value (1 to 4) - 1
  15530.  
  15531.   03F3A 7D              LD      A,L             ;A = X coordinate
  15532.   03F3B 6C              LD      L,H             ;HL = Y coordinate
  15533.   03F3C 2600            LD      H,00H
  15534.   03F3E 54              LD      D,H             ;DE = Y coordinate
  15535.   03F3F 5D              LD      E,L
  15536.   03F40 29              ADD     HL,HL           ;Multiply Y coordinate by 40
  15537.   03F41 29              ADD     HL,HL           ;(40 bytes/ line)
  15538.   03F42 19              ADD     HL,DE           ;Result in HL
  15539.   03F43 29              ADD     HL,HL           ;HL = offset from start of FGR
  15540.   03F44 29              ADD     HL,HL           ;page to start of line (Y)
  15541.   03F45 29              ADD     HL,HL
  15542.   03F46 5F              LD      E,A             ;Multiply X coordinate by 4
  15543.   03F47 CB3B            SRL     E               ;(4 pixels/byte)
  15544.   03F49 CB3B            SRL     E               ;E = offset from start of line
  15545.                                                 ;to X
  15546.   03F4B 1648            LD      D,48H           ;DE = start of FGR page + E
  15547.   03F4D 19              ADD     HL,DE           ;HL -> FGR memory location
  15548.                                                 ;that contains (X,Y)
  15549.   03F4E E603            AND     03H             ;Now make counter for
  15550.   03F50 3C              INC     A               ;getting the proper 2 bits for
  15551.   03F51 4F              LD      C,A             ;X coordinate within the byte
  15552.   03F52 47              LD      B,A             ;Result in B as counter
  15553.   03F53 7E              LD      A,(HL)          ;Load byte from FGR memory
  15554.  
  15555.   03F54 07              RLCA                    ;Rotate until the proper 2
  15556.   03F55 07              RLCA                    ;bits are in bit 1 and 0
  15557.   03F56 10FC            DJNZ    3F54H
  15558.  
  15559.   03F58 E603            AND     03H             ;Clear bits 7 to 2
  15560.   03F5A C9              RET
  15561.  
  15562.   03F5B 00              NOP                     ;--
  15563.  
  15564.  
  15565. ; X = CPOINT(x-coordinate,y-coordinate)
  15566.  
  15567.   03F5C CF              RST     08H             ;Next character must be
  15568.   03F5D 28CD            DEFB    ')'             ;a '('
  15569.   03F5E CD1C2B          CALL    2B1CH           ;Get X coordinate
  15570.   03F61 F5              PUSH    AF              ;and put it on stack
  15571.   03F62 CF              RST     08H             ;Next character must be
  15572.   03F63 2C              DEFB    ','             ;a comma
  15573.   03F64 CD1C2B          CALL    2B1CH           ;Get Y coordinate
  15574.   03F67 F5              PUSH    AF              ;and put it on stack
  15575.   03F68 CF              RST     08H             ;Next character must be
  15576.   03F69 29              DEFB    ')'             ;a ')'
  15577.   03F6A D1              POP     DE              ;D = Y coordinate
  15578.   03F6B F1              POP     AF              ;A = X coordinate
  15579.   03F6C EB              EX      DE,HL           ;H = Y coordinate, DE = PTP
  15580.   03F6D D5              PUSH    DE              ;Save PTP
  15581.   03F6E 6F              LD      L,A             ;L = X coordinate
  15582.   03F6F CD3A3F          CALL    3F3AH           ;Get FGR memory contents for
  15583.                                                 ;specified coordinate
  15584.  
  15585.   03F72 6F              LD      L,A             ;HL = A
  15586.   03F73 2600            LD      H,00H
  15587.   03F75 CD9A0A          CALL    0A9AH           ;Write HL to X as INT
  15588.   03F78 E1              POP     HL              ;Restore PTP
  15589.   03F79 C9              RET
  15590.  
  15591.  
  15592. ; SUB for processing of expressions
  15593. ; Interception of Colour-BASIC functions
  15594.  
  15595.   03F7A CAFE27          JP      Z,27FEH         ;USR token found:
  15596.                                                 ;Continue at 27FEH
  15597.   03F7D FEFF            CP      0FFH            ;Colour-BASIC token ?
  15598.   03F7F C20425          JP      NZ,2504H        ;No: continue at 2504H
  15599.  
  15600.   03F82 D7              RST     10H             ;Increase PTP, next token in A
  15601.   03F83 23              INC     HL              ;PTP + 1
  15602.  
  15603.   03F84 FE82            CP      82H             ;Token for KEYPAD ?
  15604.   03F86 CA0F3A          JP      Z,3A0FH         ;Yes: continue at 3A0FH
  15605.  
  15606.   03F89 FE8F            CP      8FH             ;Token for CPOINT ?
  15607.   03F8B 28CF            JR      Z,3F5CH         ;Yes: continue at 3F5CH
  15608.  
  15609.   03F8D FE83            CP      83H             ;Token for JOY ?
  15610.   03F8F CA1F3A          JP      Z,3A1FH         ;Yes: continue at 3A1FH
  15611.  
  15612.   03F92 C3213F          JP      3F21H           ;Continue at 3F21H
  15613.  
  15614.  
  15615. ; SOUND statement
  15616. ; ---------------
  15617.  
  15618.   03F95 CD1C2B          CALL    2B1CH           ;Get PSG register number
  15619.   03F98 FE10            CP      10H             ;In range (0 to 15) ?
  15620.   03F9A D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15621.  
  15622.   03F9D F5              PUSH    AF              ;Save PSG register number
  15623.   03F9E CF              RST     08H             ;Next character must be
  15624.   03F9F 2C              DEFB    ','             ;a comma
  15625.   03FA0 CD1C2B          CALL    2B1CH           ;Get register data
  15626.   03FA3 5F              LD      E,A             ;E = register data
  15627.   03FA4 F1              POP     AF              ;A = PSG register number
  15628.   03FA5 C3323E          JP      3E32H           :Write to PSG
  15629.  
  15630.  
  15631. ; CHAR statement
  15632. ; --------------
  15633.  
  15634.   03FA8 CDC23F          CALL    3FC2H           ;A = char value - 1
  15635.   03FAB FE04            CP      04H             ;Value in range (1 to 4) ?
  15636.   03FAD D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15637.   03FB0 E603            AND     03H             ;Mask bits
  15638.   03FB2 07              RLCA                    ;Rotate to proper position
  15639.   03FB3 07              RLCA
  15640.   03FB4 07              RLCA
  15641.   03FB5 47              LD      B,A             ;Put it in B
  15642.   03FB6 3A1C43          LD      A,(431CH)       ;A = port 255 output status
  15643.   03FB9 E6E7            AND     0E7H            ;Clear the old bits for CHAR
  15644.   03FBB B0              OR      B               ;Set the new bits
  15645.   03FBC D3FF            OUT     (0FFH),A        ;Activate new CHAR
  15646.   03FBE 321C43          LD      (431CH),A       ;and store status in system RAM
  15647.   03FC1 C9              RET
  15648.  
  15649.  
  15650. ; Process expression at (HL) and return result - 1 in A
  15651. ;
  15652. ; I: HL = PTP on BASIC expression
  15653. ; O: A = result - 1
  15654.  
  15655.   03FC2 2B              DEC     HL              ;PTP - 1
  15656.   03FC3 D7              RST     10H             ;Get next non-space character
  15657.   03FC4 CD1C2B          CALL    2B1CH           ;Get result
  15658.   03FC7 3D              DEC     A               ;A - 1
  15659.   03FC8 C9              RET
  15660.  
  15661.  
  15662. ; Set character and cursor colour (not used)
  15663.  
  15664.   03FC9 CD7A30          CALL    307AH
  15665.   03FCC 23              INC     HL
  15666.   03FCD CD7A30          CALL    307AH
  15667.   03FD0 2B              DEC     HL
  15668.   03FD1 C9              RET
  15669.  
  15670.  
  15671. ; SUB for LIST (see 2B91H)
  15672. ; No token found. Output a text-constant ?
  15673.  
  15674.   03FD2 FE22            CP      22H             ;Text-constant ?
  15675.   03FD4 C2892B          JP      NZ,2B89H        ;No: return to LIST
  15676.  
  15677.   03FD7 03              INC     BC              ;Buffer pointer + 1
  15678.   03FD8 15              DEC     D               ;Counter - 1
  15679.   03FD9 C8              RET     Z               ;Return when line buffer full
  15680.  
  15681.   03FDA 7E              LD      A,(HL)          ;Get character
  15682.   03FDB FE22            CP      22H             ;End of text reached ?
  15683.   03FDD 23              INC     HL              ;Text pointer + 1
  15684.   03FDE 02              LD      (BC),A          ;Put character in buffer
  15685.   03FDF CA892B          JP      Z,2B89H         ;Yes: return to LIST
  15686.  
  15687.   03FE2 18F3            JR      3FD7H           ;Next character
  15688.  
  15689.  
  15690. ; BGRD statement
  15691. ; --------------
  15692.  
  15693.   03FE4 2B              DEC     HL              ;Adjust PTP
  15694.   03FE5 D7              RST     10H             ;Get next non-space character
  15695.   03FE6 0604            LD      B,04H           ;Set BGRD value to 4
  15696.                                                 ;parameter present after BGRD ?
  15697.   03FE8 CABD38          JP      Z,38BDH         ;No: continue at 38BDH
  15698.  
  15699.   03FEB CDC23F          CALL    3FC2H           ;A = bgrd value - 1
  15700.   03FEE FE04            CP      04H             ;Value in range (1 to 4) ?
  15701.   03FF0 D24A1E          JP      NC,1E4AH        ;No: ?FC Error
  15702.  
  15703.   03FF3 E603            AND     03H             ;Mask bits
  15704.   03FF5 0F              RRCA                    ;Rotate to proper position
  15705.   03FF6 0F              RRCA
  15706.   03FF7 47              LD      B,A             ;Put result in B
  15707.   03FF8 3A1C43          LD      A,(431CH)       ;A = port 255 output status
  15708.   03FFB E63F            AND     3FH             ;Clear old bits for BGRD n
  15709.   03FFD C3C238          JP      38C2H           ;Continue at 38C2H
  15710.  
  15711. ; End of BASIC interpreter
  15712.