Subversion Repositories pentevo

Rev

Rev 403 | Rev 533 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. ;www.fruitcake.plus.com
  3.  
  4. ;LAST UPDATE: 16.05.2011 savelij
  5.  
  6.                 include rst8_equ.a80
  7.  
  8. AY_PRINTER      EQU 1
  9. TAP_EMUL        EQU 1
  10. PRESS_MAGIC     EQU 1
  11.  
  12. ; **************************************
  13. ; *** SPECTRUM 128 ROM 1 DISASSEMBLY ***
  14. ; **************************************
  15.  
  16. ; The Spectrum ROMs are copyright Amstrad, who have kindly given permission
  17. ; to reverse engineer and publish ROM disassemblies.
  18.  
  19.  
  20. ; =====
  21. ; NOTES
  22. ; =====
  23.  
  24. ; ------------
  25. ; Release Date
  26. ; ------------
  27. ; 23rd May 2009
  28.  
  29.  
  30. ; =================
  31. ; ASSEMBLER DEFINES
  32. ; =================
  33.  
  34. ;TASM directives:
  35.  
  36. ;#define DB .BYTE      
  37. ;#define DEFW .WORD
  38. ;#define DEFM .TEXT
  39. ;#DEFINE DEFS .FILL
  40. ;#define END  .END
  41. ;#define EQU  .EQU
  42. ;#define ORG  .ORG
  43.  
  44. ; The Sinclair Interface1 ROM written by Dr. Ian Logan calls numerous
  45. ; routines in this ROM. Non-standard entry points have a label beginning
  46. ; with X.
  47.  
  48.         ORG     $0000
  49.  
  50. ;*****************************************
  51. ;** Part 1. RESTART ROUTINES AND TABLES **
  52. ;*****************************************
  53.  
  54. ; -----------
  55. ; THE 'START'
  56. ; -----------
  57. ; At switch on, the Z80 chip is in interrupt mode 0.
  58. ; This location can also be 'called' to reset the machine.
  59. ; Typically with PRINT USR 0.
  60.  
  61. ;; START
  62. L0000:  DI                      ; disable interrupts.
  63.         XOR     A               ; signal coming from START.
  64.         LD      DE,$FFFF        ; top of possible physical RAM.
  65.         JP      L11CB           ; jump forward to common code at START-NEW.
  66.  
  67. ; -------------------
  68. ; THE 'ERROR' RESTART
  69. ; -------------------
  70. ; The error pointer is made to point to the position of the error to enable
  71. ; the editor to show the error if it occurred during syntax checking.
  72. ; It is used at 37 places in the program.
  73. ; An instruction fetch on address $0008 may page in a peripheral ROM
  74. ; such as the Sinclair Interface 1 or Disciple Disk Interface.
  75. ; This was not however an original design concept and not all errors pass
  76. ; through here.
  77.  
  78. ;; ERROR-1
  79. L0008
  80. ;               LD HL,($5C5D)           ; fetch the character address from CH_ADD.
  81.                 JP RST8_CMP
  82.  
  83. L000B           LD ($5C5F),HL           ; copy it to the error pointer X_PTR.
  84.                 JR L0053                ; forward to continue at ERROR-2.
  85.  
  86. ; -----------------------------
  87. ; THE 'PRINT CHARACTER' RESTART
  88. ; -----------------------------
  89. ; The A register holds the code of the character that is to be sent to
  90. ; the output stream of the current channel.
  91. ; The alternate register set is used to output a character in the A register
  92. ; so there is no need to preserve any of the current registers (HL,DE,BC).
  93. ; This restart is used 21 times.
  94.  
  95. ;; PRINT-A
  96. L0010:  JP      L15F2           ; jump forward to continue at PRINT-A-2.
  97.  
  98. ; ---
  99.  
  100. L0013
  101.                 OUT (C),A
  102.                 NOP
  103.                 NOP
  104.                 RET
  105.  
  106.                 DUPL 0X0018-$,0XFF
  107.  
  108. ;        DB    $FF             ; this byte is used by the SPECTRUM command in
  109.                                 ; ROM 0 to generate an error report "0 OK".
  110. ;        DB    $FF, $FF        ; four unused locations.
  111. ;        DB    $FF, $FF        ;
  112.  
  113. ; -------------------------------
  114. ; THE 'COLLECT CHARACTER' RESTART
  115. ; -------------------------------
  116. ; The contents of the location currently addressed by CH_ADD are fetched.
  117. ; A return is made if the value represents a character that has
  118. ; relevance to the BASIC parser. Otherwise CH_ADD is incremented and the
  119. ; tests repeated. CH_ADD will be addressing somewhere -
  120. ; 1) in the BASIC program area during line execution.
  121. ; 2) in workspace if evaluating, for example, a string expression.
  122. ; 3) in the edit buffer if parsing a direct command or a new BASIC line.
  123. ; 4) in workspace if accepting input but not that from INPUT LINE.
  124.  
  125. ;; GET-CHAR
  126. L0018:  LD      HL,($5C5D)      ; fetch the address from CH_ADD.
  127.         LD      A,(HL)          ; use it to pick up current character.
  128.  
  129. ;; TEST-CHAR
  130. L001C:  CALL    L007D           ; routine SKIP-OVER tests if the character
  131.         RET     NC              ; is relevant. Return if it is so.
  132.  
  133. ; ------------------------------------
  134. ; THE 'COLLECT NEXT CHARACTER' RESTART
  135. ; ------------------------------------
  136. ; As the BASIC commands and expressions are interpreted, this routine is
  137. ; called repeatedly to step along the line. It is used 83 times.
  138.  
  139. ;; NEXT-CHAR
  140. L0020:  CALL    L0074           ; routine CH-ADD+1 fetches the next immediate
  141.                                 ; character.
  142.         JR      L001C           ; jump back to TEST-CHAR until a valid
  143.                                 ; character is found.
  144.  
  145. ; ---
  146.  
  147.         DB    $FF, $FF, $FF   ; unused
  148.  
  149. ; -----------------------
  150. ; THE 'CALCULATE' RESTART
  151. ; -----------------------
  152. ; This restart enters the Spectrum's internal, floating-point,
  153. ; stack-based, FORTH-like language.
  154. ; It is further used recursively from within the calculator.
  155. ; It is used on 77 occasions.
  156.  
  157. ;; FP-CALC
  158. L0028:  JP      L335B           ; jump forward to the CALCULATE routine.
  159.  
  160. ; ---
  161.  
  162.         DB    $FF, $FF, $FF   ; spare - note that on the ZX81, space being a
  163.         DB    $FF, $FF        ; little cramped, these same locations were
  164.                                 ; used for the five-byte end-calc literal.
  165.  
  166. ; ------------------------------
  167. ; THE 'CREATE BC SPACES' RESTART
  168. ; ------------------------------
  169. ; This restart is used on only 12 occasions to create BC spaces
  170. ; between workspace and the calculator stack.
  171.  
  172. ;; BC-SPACES
  173. L0030:  PUSH    BC              ; save number of spaces.
  174.         LD      HL,($5C61)      ; fetch WORKSP.
  175.         PUSH    HL              ; save address of workspace.
  176.         JP      L169E           ; jump forward to continuation code RESERVE.
  177.  
  178. ; --------------------------------
  179. ; THE 'MASKABLE INTERRUPT' ROUTINE
  180. ; --------------------------------
  181. ; This routine increments the Spectrum's three-byte FRAMES counter
  182. ; fifty times a second (sixty times a second in the USA ).
  183. ; Both this routine and the called KEYBOARD subroutine use
  184. ; the IY register to access system variables and flags so a user-written
  185. ; program must disable interrupts to make use of the IY register.
  186.  
  187. ;; MASK-INT
  188. L0038:  PUSH    AF              ; save the registers.
  189.         PUSH    HL              ; but not IY unfortunately.
  190.         LD      HL,($5C78)      ; fetch two bytes at FRAMES1.
  191.         INC     HL              ; increment lowest two bytes of counter.
  192.         LD      ($5C78),HL      ; place back in FRAMES1.
  193.         LD      A,H             ; test if the result
  194.         OR      L               ; was zero.
  195.         JR      NZ,L0048        ; forward to KEY-INT if not.
  196.  
  197.         INC     (IY+$40)        ; otherwise increment FRAMES3 the third byte.
  198.  
  199. ; now save the rest of the main registers and read and decode the keyboard.
  200.  
  201. ;; KEY-INT
  202. L0048:  PUSH    BC              ; save the other
  203.         PUSH    DE              ; main registers.
  204.  
  205.                 IF BAS48_ONLY
  206.                 CALL L02BF
  207.                 ELSE
  208.                 CALL L386E              ; Spectrum 128 patch: read the keypad and keyboard
  209.                                         ; in the process of reading a key-press.
  210.                 ENDIF
  211.  
  212. L004D:  POP     DE              ;
  213.         POP     BC              ; restore registers.
  214.  
  215.         POP     HL              ;
  216.         POP     AF              ;
  217.         EI                      ; enable interrupts.
  218.         RET                     ; return.
  219.  
  220. ; ---------------------
  221. ; THE 'ERROR-2' ROUTINE
  222. ; ---------------------
  223. ; A continuation of the code at 0008.
  224. ; The error code is stored and after clearing down stacks,
  225. ; an indirect jump is made to MAIN-4, etc. to handle the error.
  226.  
  227. ;; ERROR-2
  228. L0053:  POP     HL              ; drop the return address - the location
  229.                                 ; after the RST 08H instruction.
  230.         LD      L,(HL)          ; fetch the error code that follows.
  231.                                 ; (nice to see this instruction used.)
  232.  
  233. ; Note. this entry point is used when out of memory at REPORT-4.
  234. ; The L register has been loaded with the report code but X-PTR is not
  235. ; updated.
  236.  
  237. ;; ERROR-3
  238. L0055:  LD      (IY+$00),L      ; store it in the system variable ERR_NR.
  239.         LD      SP,($5C3D)      ; ERR_SP points to an error handler on the
  240.                                 ; machine stack. There may be a hierarchy
  241.                                 ; of routines.
  242.                                 ; to MAIN-4 initially at base.
  243.                                 ; or REPORT-G on line entry.
  244.                                 ; or  ED-ERROR when editing.
  245.                                 ; or   ED-FULL during ed-enter.
  246.                                 ; or  IN-VAR-1 during runtime input etc.
  247.  
  248.         JP      L16C5           ; jump to SET-STK to clear the calculator
  249.                                 ; stack and reset MEM to usual place in the
  250.                                 ; systems variables area.
  251.                                 ; and then indirectly to MAIN-4, etc.
  252.  
  253. ; ---
  254.  
  255.         DB    $FF, $FF, $FF   ; unused locations
  256.         DB    $FF, $FF, $FF   ; before the fixed-position
  257.         DB    $FF             ; NMI routine.
  258.  
  259. ; ------------------------------------
  260. ; THE 'NON-MASKABLE INTERRUPT' ROUTINE
  261. ; ------------------------------------
  262. ; There is no NMI switch on the standard Spectrum.
  263. ; When activated, a location in the system variables is tested
  264. ; and if the contents are zero a jump made to that location else
  265. ; a return is made. Perhaps a disabled development feature but
  266. ; if the logic was reversed, no program would be safe from
  267. ; copy-protection and the Spectrum would have had no software base.
  268. ; The location NMIADD was later used by Interface 1 for other purposes.
  269. ; On later Spectrums, and the Brazilian Spectrum, the logic of this
  270. ; routine was reversed.
  271.  
  272. ;; RESET
  273. L0066           PUSH AF         ; save the
  274.                 PUSH HL         ; registers.
  275.                 LD HL,($5CB0)   ; fetch the system variable NMIADD.
  276.                 LD A,H          ; test address
  277.                 OR L            ; for zero.
  278. ;               JR NZ,L0070     ; skip to NO-RESET if NOT ZERO
  279.                 JR Z,L0070
  280.                 JP (HL)         ; jump to routine ( i.e. L0000 )
  281.  
  282. ;; NO-RESET
  283. L0070           POP HL          ; restore the
  284.                 POP AF          ; registers.
  285.                 RETN            ; return to previous interrupt state.
  286.  
  287. ; ---------------------------
  288. ; THE 'CH ADD + 1' SUBROUTINE
  289. ; ---------------------------
  290. ; This subroutine is called from RST 20, and three times from elsewhere
  291. ; to fetch the next immediate character following the current valid character
  292. ; address and update the associated system variable.
  293. ; The entry point TEMP-PTR1 is used from the SCANNING routine.
  294. ; Both TEMP-PTR1 and TEMP-PTR2 are used by the READ command routine.
  295.  
  296. ;; CH-ADD+1
  297. L0074:  LD      HL,($5C5D)      ; fetch address from CH_ADD.
  298.  
  299. ;; TEMP-PTR1
  300. L0077:  INC     HL              ; increase the character address by one.
  301.  
  302. ;; TEMP-PTR2
  303. L0078:  LD      ($5C5D),HL      ; update CH_ADD with character address.
  304.  
  305. X007B:  LD      A,(HL)          ; load character to A from HL.
  306.         RET                     ; and return.
  307.  
  308. ; --------------------------
  309. ; THE 'SKIP OVER' SUBROUTINE
  310. ; --------------------------
  311. ; This subroutine is called once from RST 18 to skip over white-space and
  312. ; other characters irrelevant to the parsing of a BASIC line etc. .
  313. ; Initially the A register holds the character to be considered
  314. ; and HL holds its address which will not be within quoted text
  315. ; when a BASIC line is parsed.
  316. ; Although the 'tab' and 'at' characters will not appear in a BASIC line,
  317. ; they could be present in a string expression, and in other situations.
  318. ; Note. although white-space is usually placed in a program to indent loops
  319. ; and make it more readable, it can also be used for the opposite effect and
  320. ; spaces may appear in variable names although the parser never sees them.
  321. ; It is this routine that helps make the variables 'Anum bEr5 3BUS' and
  322. ; 'a number 53 bus' appear the same to the parser.
  323.  
  324. ;; SKIP-OVER
  325. L007D:  CP      $21             ; test if higher than space.
  326.         RET     NC              ; return with carry clear if so.
  327.  
  328.         CP      $0D             ; carriage return ?
  329.         RET     Z               ; return also with carry clear if so.
  330.  
  331.                                 ; all other characters have no relevance
  332.                                 ; to the parser and must be returned with
  333.                                 ; carry set.
  334.  
  335.         CP      $10             ; test if 0-15d
  336.         RET     C               ; return, if so, with carry set.
  337.  
  338.         CP      $18             ; test if 24-32d
  339.         CCF                     ; complement carry flag.
  340.         RET     C               ; return with carry set if so.
  341.  
  342.                                 ; now leaves 16d-23d
  343.  
  344.         INC     HL              ; all above have at least one extra character
  345.                                 ; to be stepped over.
  346.  
  347.         CP      $16             ; controls 22d ('at') and 23d ('tab') have two.
  348.         JR      C,L0090         ; forward to SKIPS with ink, paper, flash,
  349.                                 ; bright, inverse or over controls.
  350.                                 ; Note. the high byte of tab is for RS232 only.
  351.                                 ; it has no relevance on this machine.
  352.  
  353.         INC     HL              ; step over the second character of 'at'/'tab'.
  354.  
  355. ;; SKIPS
  356. L0090:  SCF                     ; set the carry flag
  357.         LD      ($5C5D),HL      ; update the CH_ADD system variable.
  358.         RET                     ; return with carry set.
  359.  
  360.  
  361. ; ------------------
  362. ; THE 'TOKEN TABLES'
  363. ; ------------------
  364. ; The tokenized characters 134d (RND) to 255d (COPY) are expanded using
  365. ; this table. The last byte of a token is inverted to denote the end of
  366. ; the word. The first is an inverted step-over byte.
  367.  
  368. ;; TKN-TABLE
  369. L0095           DC "?"          ;DB    '?'+$80
  370.                 DC "RND"        ;DEFM    "RN"
  371.                                 ;DB    'D'+$80
  372.                 DC "INKEY$"     ;DEFM    "INKEY"
  373.                                 ;DB    '$'+$80
  374.                 DC "PI"         ;DB    'P','I'+$80
  375.                 DC "FN"         ;DB    'F','N'+$80
  376.                 DC "POINT"      ;DEFM    "POIN"
  377.                                 ;DB    'T'+$80
  378.                 DC "SCREEN$"    ;DEFM    "SCREEN"
  379.                                 ;DB    '$'+$80
  380.                 DC "ATTR"       ;DEFM    "ATT"
  381.                                 ;DB    'R'+$80
  382.                 DC "AT"         ;DB    'A','T'+$80
  383.                 DC "TAB"        ;DEFM    "TA"
  384.                                 ;DB    'B'+$80
  385.                 DC "VAL$"       ;DEFM    "VAL"
  386.                                 ;DB    '$'+$80
  387.                 DC "CODE"       ;DEFM    "COD"
  388.                                 ;DB    'E'+$80
  389.                 DC "VAL"        ;DEFM    "VA"
  390.                                 ;DB    'L'+$80
  391.                 DC "LEN"        ;DEFM    "LE"
  392.                                 ;DB    'N'+$80
  393.                 DC "SIN"        ;DEFM    "SI"
  394.                                 ;DB    'N'+$80
  395.                 DC "COS"        ;DEFM    "CO"
  396.                                 ;DB    'S'+$80
  397.                 DC "TAN"        ;DEFM    "TA"
  398.                                 ;DB    'N'+$80
  399.                 DC "ASN"        ;DEFM    "AS"
  400.                                 ;DB    'N'+$80
  401.                 DC "ACS"        ;DEFM    "AC"
  402.                                 ;DB    'S'+$80
  403.                 DC "ATN"        ;DEFM    "AT"
  404.                                 ;DB    'N'+$80
  405.                 DC "LN"         ;DB    'L','N'+$80
  406.                 DC "EXP"        ;DEFM    "EX"
  407.                                 ;DB    'P'+$80
  408.                 DC "INT"        ;DEFM    "IN"
  409.                                 ;DB    'T'+$80
  410.                 DC "SQR"        ;DEFM    "SQ"
  411.                                 ;DB    'R'+$80
  412.                 DC "SGN"        ;DEFM    "SG"
  413.                                 ;DB    'N'+$80
  414.                 DC "ABS"        ;DEFM    "AB"
  415.                                 ;DB    'S'+$80
  416.                 DC "PEEK"       ;DEFM    "PEE"
  417.                                 ;DB    'K'+$80
  418.                 DC "IN"         ;DB    'I','N'+$80
  419.                 DC "USR"        ;DEFM    "US"
  420.                                 ;DB    'R'+$80
  421.                 DC "STR$"       ;DEFM    "STR"
  422.                                 ;DB    '$'+$80
  423.                 DC "CHR$"       ;DEFM    "CHR"
  424.                                 ;DB    '$'+$80
  425.                 DC "NOT"        ;DEFM    "NO"
  426.                                 ;DB    'T'+$80
  427.                 DC "BIN"        ;DEFM    "BI"
  428.                                 ;DB    'N'+$80
  429.  
  430. ;   The previous 32 function-type words are printed without a leading space
  431. ;   The following have a leading space if they begin with a letter
  432.  
  433.                 DC "OR"         ;DB    'O','R'+$80
  434.                 DC "AND"        ;DEFM    "AN"
  435.                                 ;DB    'D'+$80
  436.                 DC "<="         ;DB    $3C,'='+$80             ; <=
  437.                 DC ">="         ;DB    $3E,'='+$80             ; >=
  438.                 DC "<>"         ;DB    $3C,$3E+$80             ; <>
  439.                 DC "LINE"       ;DEFM    "LIN"
  440.                                 ;DB    'E'+$80
  441.                 DC "THEN"       ;DEFM    "THE"
  442.                                 ;DB    'N'+$80
  443.                 DC "TO"         ;DB    'T','O'+$80
  444.                 DC "STEP"       ;DEFM    "STE"
  445.                                 ;DB    'P'+$80
  446.                 DC "DEF FN"     ;DEFM    "DEF F"
  447.                                 ;DB    'N'+$80
  448.                 DC "CAT"        ;DEFM    "CA"
  449.                                 ;DB    'T'+$80
  450.                 DC "FORMAT"     ;DEFM    "FORMA"
  451.                                 ;DB    'T'+$80
  452.                 DC "MOVE"       ;DEFM    "MOV"
  453.                                 ;DB    'E'+$80
  454.                 DC "ERASE"      ;DEFM    "ERAS"
  455.                                 ;DB    'E'+$80
  456.                 DC "OPEN #"     ;DEFM    "OPEN "
  457.                                 ;DB    '#'+$80
  458.                 DC "CLOSE #"    ;DEFM    "CLOSE "
  459.                                 ;DB    '#'+$80
  460.                 DC "MERGE"      ;DEFM    "MERG"
  461.                                 ;DB    'E'+$80
  462.                 DC "VERIFY"     ;DEFM    "VERIF"
  463.                                 ;DB    'Y'+$80
  464.                 DC "BEEP"       ;DEFM    "BEE"
  465.                                 ;DB    'P'+$80
  466.                 DC "CIRCLE"     ;DEFM    "CIRCL"
  467.                                 ;DB    'E'+$80
  468.                 DC "INK"        ;DEFM    "IN"
  469.                                 ;DB    'K'+$80
  470.                 DC "PAPER"      ;DEFM    "PAPE"
  471.                                 ;DB    'R'+$80
  472.                 DC "FLASH"      ;DEFM    "FLAS"
  473.                                 ;DB    'H'+$80
  474.                 DC "BRIGHT"     ;DEFM    "BRIGH"
  475.                                 ;DB    'T'+$80
  476.                 DC "INVERSE"    ;DEFM    "INVERS"
  477.                                 ;DB    'E'+$80
  478.                 DC "OVER"       ;DEFM    "OVE"
  479.                                 ;DB    'R'+$80
  480.                 DC "OUT"        ;DEFM    "OU"
  481.                                 ;DB    'T'+$80
  482.                 DC "LPRINT"     ;DEFM    "LPRIN"
  483.                                 ;DB    'T'+$80
  484.                 DC "LLIST"      ;DEFM    "LLIS"
  485.                                 ;DB    'T'+$80
  486.                 DC "STOP"       ;DEFM    "STO"
  487.                                 ;DB    'P'+$80
  488.                 DC "READ"       ;DEFM    "REA"
  489.                                 ;DB    'D'+$80
  490.                 DC "DATA"       ;DEFM    "DAT"
  491.                                 ;DB    'A'+$80
  492.                 DC "RESTORE"    ;DEFM    "RESTOR"
  493.                                 ;DB    'E'+$80
  494.                 DC "NEW"        ;DEFM    "NE"
  495.                                 ;DB    'W'+$80
  496.                 DC "BORDER"     ;DEFM    "BORDE"
  497.                                 ;DB    'R'+$80
  498.                 DC "CONTINUE"   ;DEFM    "CONTINU"
  499.                                 ;DB    'E'+$80
  500.                 DC "DIM"        ;DEFM    "DI"
  501.                                 ;DB    'M'+$80
  502.                 DC "REM"        ;DEFM    "RE"
  503.                                 ;DB    'M'+$80
  504.                 DC "FOR"        ;DEFM    "FO"
  505.                                 ;DB    'R'+$80
  506.                 DC "GO TO"      ;DEFM    "GO T"
  507.                                 ;DB    'O'+$80
  508.                 DC "GO SUB"     ;DEFM    "GO SU"
  509.                                 ;DB    'B'+$80
  510.                 DC "INPUT"      ;DEFM    "INPU"
  511.                                 ;DB    'T'+$80
  512.                 DC "LOAD"       ;DEFM    "LOA"
  513.                                 ;DB    'D'+$80
  514.                 DC "LIST"       ;DEFM    "LIS"
  515.                                 ;DB    'T'+$80
  516.                 DC "LET"        ;DEFM    "LE"
  517.                                 ;DB    'T'+$80
  518.                 DC "PAUSE"      ;DEFM    "PAUS"
  519.                                 ;DB    'E'+$80
  520.                 DC "NEXT"       ;DEFM    "NEX"
  521.                                 ;DB    'T'+$80
  522.                 DC "POKE"       ;DEFM    "POK"
  523.                                 ;DB    'E'+$80
  524.                 DC "PRINT"      ;DEFM    "PRIN"
  525.                                 ;DB    'T'+$80
  526.                 DC "PLOT"       ;DEFM    "PLO"
  527.                                 ;DB    'T'+$80
  528.                 DC "RUN"        ;DEFM    "RU"
  529.                                 ;DB    'N'+$80
  530.                 DC "SAVE"       ;DEFM    "SAV"
  531.                                 ;DB    'E'+$80
  532.                 DC "RANDOMIZE"  ;DEFM    "RANDOMIZ"
  533.                                 ;DB    'E'+$80
  534.                 DC "IF"         ;DB    'I','F'+$80
  535.                 DC "CLS"        ;DEFM    "CL"
  536.                                 ;DB    'S'+$80
  537.                 DC "DRAW"       ;DEFM    "DRA"
  538.                                 ;DB    'W'+$80
  539.                 DC "CLEAR"      ;DEFM    "CLEA"
  540.                                 ;DB    'R'+$80
  541.                 DC "RETURN"     ;DEFM    "RETUR"
  542.                                 ;DB    'N'+$80
  543.                 DC "COPY"       ;DEFM    "COP"
  544.                                 ;DB    'Y'+$80
  545.  
  546. ; ----------------
  547. ; THE 'KEY' TABLES
  548. ; ----------------
  549. ; These six look-up tables are used by the keyboard reading routine
  550. ; to decode the key values.
  551.  
  552. ; The first table contains the maps for the 39 keys of the standard
  553. ; 40-key Spectrum keyboard. The remaining key [SHIFT $27] is read directly.
  554. ; The keys consist of the 26 upper-case alphabetic characters, the 10 digit
  555. ; keys and the space, ENTER and symbol shift key.
  556. ; Unshifted alphabetic keys have $20 added to the value.
  557. ; The keywords for the main alphabetic keys are obtained by adding $A5 to
  558. ; the values obtained from this table.
  559.  
  560. ;; MAIN-KEYS
  561. L0205:  DB    $42             ; B
  562.         DB    $48             ; H
  563.         DB    $59             ; Y
  564.         DB    $36             ; 6
  565.         DB    $35             ; 5
  566.         DB    $54             ; T
  567.         DB    $47             ; G
  568.         DB    $56             ; V
  569.         DB    $4E             ; N
  570.         DB    $4A             ; J
  571.         DB    $55             ; U
  572.         DB    $37             ; 7
  573.         DB    $34             ; 4
  574.         DB    $52             ; R
  575.         DB    $46             ; F
  576.         DB    $43             ; C
  577.         DB    $4D             ; M
  578.         DB    $4B             ; K
  579.         DB    $49             ; I
  580.         DB    $38             ; 8
  581.         DB    $33             ; 3
  582.         DB    $45             ; E
  583.         DB    $44             ; D
  584.         DB    $58             ; X
  585.         DB    $0E             ; SYMBOL SHIFT
  586.         DB    $4C             ; L
  587.         DB    $4F             ; O
  588.         DB    $39             ; 9
  589.         DB    $32             ; 2
  590.         DB    $57             ; W
  591.         DB    $53             ; S
  592.         DB    $5A             ; Z
  593.         DB    $20             ; SPACE
  594.         DB    $0D             ; ENTER
  595.         DB    $50             ; P
  596.         DB    $30             ; 0
  597.         DB    $31             ; 1
  598.         DB    $51             ; Q
  599.         DB    $41             ; A
  600.  
  601.  
  602. ;; E-UNSHIFT
  603. ;  The 26 unshifted extended mode keys for the alphabetic characters.
  604. ;  The green keywords on the original keyboard.
  605. L022C:  DB    $E3             ; READ
  606.         DB    $C4             ; BIN
  607.         DB    $E0             ; LPRINT
  608.         DB    $E4             ; DATA
  609.         DB    $B4             ; TAN
  610.         DB    $BC             ; SGN
  611.         DB    $BD             ; ABS
  612.         DB    $BB             ; SQR
  613.         DB    $AF             ; CODE
  614.         DB    $B0             ; VAL
  615.         DB    $B1             ; LEN
  616.         DB    $C0             ; USR
  617.         DB    $A7             ; PI
  618.         DB    $A6             ; INKEY$
  619.         DB    $BE             ; PEEK
  620.         DB    $AD             ; TAB
  621.         DB    $B2             ; SIN
  622.         DB    $BA             ; INT
  623.         DB    $E5             ; RESTORE
  624.         DB    $A5             ; RND
  625.         DB    $C2             ; CHR$
  626.         DB    $E1             ; LLIST
  627.         DB    $B3             ; COS
  628.         DB    $B9             ; EXP
  629.         DB    $C1             ; STR$
  630.         DB    $B8             ; LN
  631.  
  632.  
  633. ;; EXT-SHIFT
  634. ;  The 26 shifted extended mode keys for the alphabetic characters.
  635. ;  The red keywords below keys on the original keyboard.
  636. L0246:  DB    $7E             ; ~
  637.         DB    $DC             ; BRIGHT
  638.         DB    $DA             ; PAPER
  639.         DB    $5C             ;
  640.         DB    $B7             ; ATN
  641.         DB    $7B             ; {
  642.         DB    $7D             ; }
  643.         DB    $D8             ; CIRCLE
  644.         DB    $BF             ; IN
  645.         DB    $AE             ; VAL$
  646.         DB    $AA             ; SCREEN$
  647.         DB    $AB             ; ATTR
  648.         DB    $DD             ; INVERSE
  649.         DB    $DE             ; OVER
  650.         DB    $DF             ; OUT
  651.         DB    $7F             ; (Copyright character)
  652.         DB    $B5             ; ASN
  653.         DB    $D6             ; VERIFY
  654.         DB    $7C             ; |
  655.         DB    $D5             ; MERGE
  656.         DB    $5D             ; ]
  657.         DB    $DB             ; FLASH
  658.         DB    $B6             ; ACS
  659.         DB    $D9             ; INK
  660.         DB    $5B             ; [
  661.         DB    $D7             ; BEEP
  662.  
  663.  
  664. ;; CTL-CODES
  665. ;  The ten control codes assigned to the top line of digits when the shift
  666. ;  key is pressed.
  667. L0260:  DB    $0C             ; DELETE
  668.         DB    $07             ; EDIT
  669.         DB    $06             ; CAPS LOCK
  670.         DB    $04             ; TRUE VIDEO
  671.         DB    $05             ; INVERSE VIDEO
  672.         DB    $08             ; CURSOR LEFT
  673.         DB    $0A             ; CURSOR DOWN
  674.         DB    $0B             ; CURSOR UP
  675.         DB    $09             ; CURSOR RIGHT
  676.         DB    $0F             ; GRAPHICS
  677.  
  678.  
  679. ;; SYM-CODES
  680. ;  The 26 red symbols assigned to the alphabetic characters of the keyboard.
  681. ;  The ten single-character digit symbols are converted without the aid of
  682. ;  a table using subtraction and minor manipulation.
  683. L026A:  DB    $E2             ; STOP
  684.         DB    $2A             ; *
  685.         DB    $3F             ; ?
  686.         DB    $CD             ; STEP
  687.         DB    $C8             ; >=
  688.         DB    $CC             ; TO
  689.         DB    $CB             ; THEN
  690.         DB    $5E             ; ^
  691.         DB    $AC             ; AT
  692.         DB    $2D             ; -
  693.         DB    $2B             ; +
  694.         DB    $3D             ; =
  695.         DB    $2E             ; .
  696.         DB    $2C             ; ,
  697.         DB    $3B             ; ;
  698.         DB    $22             ; "
  699.         DB    $C7             ; <=
  700.         DB    $3C             ; <
  701.         DB    $C3             ; NOT
  702.         DB    $3E             ; >
  703.         DB    $C5             ; OR
  704.         DB    $2F             ; /
  705.         DB    $C9             ; <>
  706.         DB    $60             ; pound
  707.         DB    $C6             ; AND
  708.         DB    $3A             ; :
  709.  
  710. ;; E-DIGITS
  711. ;  The ten keywords assigned to the digits in extended mode.
  712. ;  The remaining red keywords below the keys.
  713. L0284:  DB    $D0             ; FORMAT
  714.         DB    $CE             ; DEF FN
  715.         DB    $A8             ; FN
  716.         DB    $CA             ; LINE
  717.         DB    $D3             ; OPEN#
  718.         DB    $D4             ; CLOSE#
  719.         DB    $D1             ; MOVE
  720.         DB    $D2             ; ERASE
  721.         DB    $A9             ; POINT
  722.         DB    $CF             ; CAT
  723.  
  724.  
  725. ;*******************************
  726. ;** Part 2. KEYBOARD ROUTINES **
  727. ;*******************************
  728.  
  729. ; Using shift keys and a combination of modes the Spectrum 40-key keyboard
  730. ; can be mapped to 256 input characters
  731.  
  732. ; ---------------------------------------------------------------------------
  733. ;
  734. ;         0     1     2     3     4 -Bits-  4     3     2     1     0
  735. ; PORT                                                                    PORT
  736. ;
  737. ; F7FE  [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]  |  [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 0 ]   EFFE
  738. ;  ^                                   |                                   v
  739. ; FBFE  [ Q ] [ W ] [ E ] [ R ] [ T ]  |  [ Y ] [ U ] [ I ] [ O ] [ P ]   DFFE
  740. ;  ^                                   |                                   v
  741. ; FDFE  [ A ] [ S ] [ D ] [ F ] [ G ]  |  [ H ] [ J ] [ K ] [ L ] [ ENT ] BFFE
  742. ;  ^                                   |                                   v
  743. ; FEFE  [SHI] [ Z ] [ X ] [ C ] [ V ]  |  [ B ] [ N ] [ M ] [sym] [ SPC ] 7FFE
  744. ;  ^     $27                                                 $18           v
  745. ; Start                                                                   End
  746. ;        00100111                                            00011000
  747. ;
  748. ; ---------------------------------------------------------------------------
  749. ; The above map may help in reading.
  750. ; The neat arrangement of ports means that the B register need only be
  751. ; rotated left to work up the left hand side and then down the right
  752. ; hand side of the keyboard. When the reset bit drops into the carry
  753. ; then all 8 half-rows have been read. Shift is the first key to be
  754. ; read. The lower six bits of the shifts are unambiguous.
  755.  
  756. ; -------------------------------
  757. ; THE 'KEYBOARD SCANNING' ROUTINE
  758. ; -------------------------------
  759. ; from keyboard and s-inkey$
  760. ; returns 1 or 2 keys in DE, most significant shift first if any
  761. ; key values 0-39 else 255
  762.  
  763. ;; KEY-SCAN
  764. L028E:  LD      L,$2F           ; initial key value
  765.                                 ; valid values are obtained by subtracting
  766.                                 ; eight five times.
  767.         LD      DE,$FFFF        ; a buffer to receive 2 keys.
  768.  
  769.         LD      BC,$FEFE        ; the commencing port address
  770.                                 ; B holds 11111110 initially and is also
  771.                                 ; used to count the 8 half-rows
  772. ;; KEY-LINE
  773. L0296:  IN      A,(C)           ; read the port to A - bits will be reset
  774.                                 ; if a key is pressed else set.
  775.         CPL                     ; complement - pressed key-bits are now set
  776.         AND     $1F             ; apply 00011111 mask to pick up the
  777.                                 ; relevant set bits.
  778.  
  779.         JR      Z,L02AB         ; forward to KEY-DONE if zero and therefore
  780.                                 ; no keys pressed in row at all.
  781.  
  782.         LD      H,A             ; transfer row bits to H
  783.         LD      A,L             ; load the initial key value to A
  784.  
  785. ;; KEY-3KEYS
  786. L029F:  INC     D               ; now test the key buffer
  787.         RET     NZ              ; if we have collected 2 keys already
  788.                                 ; then too many so quit.
  789.  
  790. ;; KEY-BITS
  791. L02A1:  SUB     $08             ; subtract 8 from the key value
  792.                                 ; cycling through key values (top = $27)
  793.                                 ; e.g. 2F>   27>1F>17>0F>07
  794.                                 ;      2E>   26>1E>16>0E>06
  795.         SRL     H               ; shift key bits right into carry.
  796.         JR      NC,L02A1        ; back to KEY-BITS if not pressed
  797.                                 ; but if pressed we have a value (0-39d)
  798.  
  799.         LD      D,E             ; transfer a possible previous key to D
  800.         LD      E,A             ; transfer the new key to E
  801.         JR      NZ,L029F        ; back to KEY-3KEYS if there were more
  802.                                 ; set bits - H was not yet zero.
  803.  
  804. ;; KEY-DONE
  805. L02AB:  DEC     L               ; cycles 2F>2E>2D>2C>2B>2A>29>28 for
  806.                                 ; each half-row.
  807.         RLC     B               ; form next port address e.g. FEFE > FDFE
  808.         JR      C,L0296         ; back to KEY-LINE if still more rows to do.
  809.  
  810.         LD      A,D             ; now test if D is still FF ?
  811.         INC     A               ; if it is zero we have at most 1 key
  812.                                 ; range now $01-$28  (1-40d)
  813.         RET     Z               ; return if one key or no key.
  814.  
  815.         CP      $28             ; is it capsshift (was $27) ?
  816.         RET     Z               ; return if so.
  817.  
  818.         CP      $19             ; is it symbol shift (was $18) ?
  819.         RET     Z               ; return also
  820.  
  821.         LD      A,E             ; now test E
  822.         LD      E,D             ; but first switch
  823.         LD      D,A             ; the two keys.
  824.         CP      $18             ; is it symbol shift ?
  825.         RET                     ; return (with zero set if it was).
  826.                                 ; but with symbol shift now in D
  827.  
  828. ; ------------------------------
  829. ; Scan keyboard and decode value
  830. ; ------------------------------
  831. ; from interrupt 50 times a second
  832. ;
  833.  
  834. ;; KEYBOARD
  835. L02BF:  CALL    L028E           ; routine KEY-SCAN
  836.         RET     NZ              ; return if invalid combinations
  837.  
  838. ; then decrease the counters within the two key-state maps
  839. ; as this could cause one to become free.
  840. ; if the keyboard has not been pressed during the last five interrupts
  841. ; then both sets will be free.
  842.  
  843.  
  844.         LD      HL,$5C00        ; point to KSTATE-0
  845.  
  846. ;; K-ST-LOOP
  847. L02C6:  BIT     7,(HL)          ; is it free ?  ($FF)
  848.         JR      NZ,L02D1        ; forward to K-CH-SET if so
  849.  
  850.         INC     HL              ; address 5-counter
  851.         DEC     (HL)            ; decrease counter
  852.         DEC     HL              ; step back
  853.         JR      NZ,L02D1        ; forward to K-CH-SET if not at end of count
  854.  
  855.         LD      (HL),$FF        ; else mark it free.
  856.  
  857. ;; K-CH-SET
  858. L02D1:  LD      A,L             ; store low address byte.
  859.         LD      HL,$5C04        ; point to KSTATE-4
  860.                                 ; (ld l, $04)
  861.         CP      L               ; have 2 been done ?
  862.         JR      NZ,L02C6        ; back to K-ST-LOOP to consider this 2nd set
  863.  
  864. ; now the raw key (0-38) is converted to a main key (uppercase).
  865.  
  866.         CALL    L031E           ; routine K-TEST to get main key in A
  867.         RET     NC              ; return if single shift
  868.  
  869.         LD      HL,$5C00        ; point to KSTATE-0
  870.         CP      (HL)            ; does it match ?
  871.         JR      Z,L0310         ; forward to K-REPEAT if so
  872.  
  873. ; if not consider the second key map.
  874.  
  875.         EX      DE,HL           ; save kstate-0 in de
  876.         LD      HL,$5C04        ; point to KSTATE-4
  877.         CP      (HL)            ; does it match ?
  878.         JR      Z,L0310         ; forward to K-REPEAT if so
  879.  
  880. ; having excluded a repeating key we can now consider a new key.
  881. ; the second set is always examined before the first.
  882.  
  883.         BIT     7,(HL)          ; is it free ?
  884.         JR      NZ,L02F1        ; forward to K-NEW if so.
  885.  
  886.         EX      DE,HL           ; bring back kstate-0
  887.         BIT     7,(HL)          ; is it free ?
  888.         RET     Z               ; return if not.
  889.                                 ; as we have a key but nowhere to put it yet.
  890.  
  891. ; continue or jump to here if one of the buffers was free.
  892.  
  893. ;; K-NEW
  894. L02F1:  LD      E,A             ; store key in E
  895.         LD      (HL),A          ; place in free location
  896.         INC     HL              ; advance to interrupt counter
  897.         LD      (HL),$05        ; and initialize to 5
  898.         INC     HL              ; advance to delay
  899.         LD      A,($5C09)       ; pick up system variable REPDEL
  900.         LD      (HL),A          ; and insert that for first repeat delay.
  901.         INC     HL              ; advance to last location of state map.
  902.  
  903.         LD      C,(IY+$07)      ; pick up MODE  (3 bytes)
  904.         LD      D,(IY+$01)      ; pick up FLAGS (3 bytes)
  905.         PUSH    HL              ; save state map location
  906.                                 ; Note. could now have used.
  907.                                 ; ld l,$41; ld c,(hl); ld l,$3B; ld d,(hl).
  908.                                 ; six and two threes of course.
  909.         CALL    L0333           ; routine K-DECODE
  910.         POP     HL              ; restore map pointer
  911.         LD      (HL),A          ; put decoded key in last location of map.
  912.  
  913. ;; K-END
  914. L0308:  LD      ($5C08),A       ; update LASTK system variable.
  915.         SET     5,(IY+$01)      ; update FLAGS  - signal new key.
  916.         RET                     ; done
  917.  
  918. ; ---------------------------
  919. ; THE 'REPEAT KEY' SUBROUTINE
  920. ; ---------------------------
  921. ; A possible repeat has been identified. HL addresses the raw (main) key.
  922. ; The last location holds the decoded key (from the first context).
  923.  
  924. ;; K-REPEAT
  925. L0310:  INC     HL              ; advance
  926.         LD      (HL),$05        ; maintain interrupt counter at 5
  927.         INC     HL              ; advance
  928.         DEC     (HL)            ; decrease REPDEL value.
  929.         RET     NZ              ; return if not yet zero.
  930.  
  931.         LD      A,($5C0A)       ; REPPER
  932.         LD      (HL),A          ; but for subsequent repeats REPPER will be used.
  933.         INC     HL              ; advance
  934.                                 ;
  935.         LD      A,(HL)          ; pick up the key decoded possibly in another
  936.                                 ; context.
  937.         JR      L0308           ; back to K-END
  938.  
  939. ; --------------
  940. ; Test key value
  941. ; --------------
  942. ; also called from s-inkey$
  943. ; begin by testing for a shift with no other.
  944.  
  945. ;; K-TEST
  946. L031E:  LD      B,D             ; load most significant key to B
  947.                                 ; will be $FF if not shift.
  948.         LD      D,$00           ; and reset D to index into main table
  949.         LD      A,E             ; load least significant key from E
  950.         CP      $27             ; is it higher than 39d   i.e. FF
  951.         RET     NC              ; return with just a shift (in B now)
  952.  
  953.         CP      $18             ; is it symbol shift ?
  954.         JR      NZ,L032C        ; forward to K-MAIN if not
  955.  
  956. ; but we could have just symbol shift and no other
  957.  
  958.         BIT     7,B             ; is other key $FF (ie not shift)
  959.         RET     NZ              ; return with solitary symbol shift
  960.  
  961.  
  962. ;; K-MAIN
  963. L032C:  LD      HL,L0205        ; address: MAIN-KEYS
  964.         ADD     HL,DE           ; add offset 0-38
  965.         LD      A,(HL)          ; pick up main key value
  966.         SCF                     ; set carry flag
  967.         RET                     ; return    (B has other key still)
  968.  
  969. ; -----------------
  970. ; Keyboard decoding
  971. ; -----------------
  972. ; also called from s-inkey$
  973.  
  974. ;; K-DECODE
  975. L0333:  LD      A,E             ; pick up the stored main key
  976.         CP      $3A             ; an arbitrary point between digits and letters
  977.         JR      C,L0367         ; forward to K-DIGIT with digits, space, enter.
  978.  
  979.         DEC     C               ; decrease MODE ( 0='KLC', 1='E', 2='G')
  980.  
  981.         JP      M,L034F         ; to K-KLC-LET if was zero
  982.  
  983.         JR      Z,L0341         ; to K-E-LET if was 1 for extended letters.
  984.  
  985. ; proceed with graphic codes.
  986. ; Note. should selectively drop return address if code > 'U' ($55).
  987. ; i.e. abort the KEYBOARD call.
  988. ; e.g. cp 'V'; jr c addit; pop af; ;;addit etc. (5 bytes of instruction).
  989. ; (s-inkey$ never gets into graphics mode.)
  990.  
  991. ;; addit
  992.         ADD     A,$4F           ; add offset to augment 'A' to graphics A say.
  993.         RET                     ; return.
  994.                                 ; Note. ( but [GRAPH] V gives RND, etc ).
  995.  
  996. ; ---
  997.  
  998. ; the jump was to here with extended mode with uppercase A-Z.
  999.  
  1000. ;; K-E-LET
  1001. L0341:  LD      HL,L022C-$41    ; base address of E-UNSHIFT L022c
  1002.                                 ; ( $01EB in standard ROM )
  1003.         INC     B               ; test B is it empty i.e. not a shift
  1004.         JR      Z,L034A         ; forward to K-LOOK-UP if neither shift
  1005.  
  1006.         LD      HL,L0246-$41    ; Address: $0205 L0246-$41 EXT-SHIFT base
  1007.  
  1008. ;; K-LOOK-UP
  1009. L034A:  LD      D,$00           ; prepare to index
  1010.         ADD     HL,DE           ; add the main key value
  1011.         LD      A,(HL)          ; pick up other mode value
  1012.         RET                     ; return
  1013.  
  1014. ; ---
  1015.  
  1016. ; the jump was here with mode = 0
  1017.  
  1018. ;; K-KLC-LET
  1019. L034F:  LD      HL,L026A-$41    ; prepare base of sym-codes
  1020.         BIT     0,B             ; shift=$27 sym-shift=$18
  1021.         JR      Z,L034A         ; back to K-LOOK-UP with symbol-shift
  1022.  
  1023.         BIT     3,D             ; test FLAGS is it 'K' mode (from OUT-CURS)
  1024.         JR      Z,L0364         ; skip to K-TOKENS if so
  1025.  
  1026.         BIT     3,(IY+$30)      ; test FLAGS2 - consider CAPS LOCK ?
  1027.         RET     NZ              ; return if so with main code.
  1028.  
  1029.         INC     B               ; is shift being pressed ?
  1030.                                 ; result zero if not
  1031.         RET     NZ              ; return if shift pressed.
  1032.  
  1033.         ADD     A,$20           ; else convert the code to lower case.
  1034.         RET                     ; return.
  1035.  
  1036. ; ---
  1037.  
  1038. ; the jump was here for tokens
  1039.  
  1040. ;; K-TOKENS
  1041. L0364:  ADD     A,$A5           ; add offset to main code so that 'A'
  1042.                                 ; becomes 'NEW' etc.
  1043.         RET                     ; return
  1044.  
  1045. ; ---
  1046.  
  1047. ; the jump was here with digits, space, enter and symbol shift (< $xx)
  1048.  
  1049. ;; K-DIGIT
  1050. L0367:  CP      $30             ; is it '0' or higher ?
  1051.         RET     C               ; return with space, enter and symbol-shift
  1052.  
  1053.         DEC     C               ; test MODE (was 0='KLC', 1='E', 2='G')
  1054.         JP      M,L039D         ; jump to K-KLC-DGT if was 0.
  1055.  
  1056.         JR      NZ,L0389        ; forward to K-GRA-DGT if mode was 2.
  1057.  
  1058. ; continue with extended digits 0-9.
  1059.  
  1060.         LD      HL,L0284-$30    ; $0254 - base of E-DIGITS
  1061.         BIT     5,B             ; test - shift=$27 sym-shift=$18
  1062.         JR      Z,L034A         ; to K-LOOK-UP if sym-shift
  1063.  
  1064.         CP      $38             ; is character '8' ?
  1065.         JR      NC,L0382        ; to K-8-&-9 if greater than '7'
  1066.  
  1067.         SUB     $20             ; reduce to ink range $10-$17
  1068.         INC     B               ; shift ?
  1069.         RET     Z               ; return if not.
  1070.  
  1071.         ADD     A,$08           ; add 8 to give paper range $18 - $1F
  1072.         RET                     ; return
  1073.  
  1074. ; ---
  1075.  
  1076. ; 89
  1077.  
  1078. ;; K-8-&-9
  1079. L0382:  SUB     $36             ; reduce to 02 and 03  bright codes
  1080.         INC     B               ; test if shift pressed.
  1081.         RET     Z               ; return if not.
  1082.  
  1083.         ADD     A,$FE           ; subtract 2 setting carry
  1084.         RET                     ; to give 0 and 1    flash codes.
  1085.  
  1086. ; ---
  1087.  
  1088. ;  graphics mode with digits
  1089.  
  1090. ;; K-GRA-DGT
  1091. L0389:  LD      HL,L0260-$30    ; $0230 base address of CTL-CODES
  1092.  
  1093.         CP      $39             ; is key '9' ?
  1094.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0F, GRAPHICS.
  1095.  
  1096.         CP      $30             ; is key '0' ?
  1097.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0C, delete.
  1098.  
  1099. ; for keys '0' - '7' we assign a mosaic character depending on shift.
  1100.  
  1101.         AND     $07             ; convert character to number. 0 - 7.
  1102.         ADD     A,$80           ; add offset - they start at $80
  1103.  
  1104.         INC     B               ; destructively test for shift
  1105.         RET     Z               ; and return if not pressed.
  1106.  
  1107.         XOR     $0F             ; toggle bits becomes range $88-$8F
  1108.         RET                     ; return.
  1109.  
  1110. ; ---
  1111.  
  1112. ; now digits in 'KLC' mode
  1113.  
  1114. ;; K-KLC-DGT
  1115. L039D:  INC     B               ; return with digit codes if neither
  1116.         RET     Z               ; shift key pressed.
  1117.  
  1118.         BIT     5,B             ; test for caps shift.
  1119.  
  1120.         LD      HL,L0260-$30    ; prepare base of table CTL-CODES.
  1121.         JR      NZ,L034A        ; back to K-LOOK-UP if shift pressed.
  1122.  
  1123. ; must have been symbol shift
  1124.  
  1125.         SUB     $10             ; for ASCII most will now be correct
  1126.                                 ; on a standard typewriter.
  1127.         CP      $22             ; but '@' is not - see below.
  1128.         JR      Z,L03B2         ; forward to to K-@-CHAR if so
  1129.  
  1130.         CP      $20             ; '_' is the other one that fails
  1131.         RET     NZ              ; return if not.
  1132.  
  1133.         LD      A,$5F           ; substitute ASCII '_'
  1134.         RET                     ; return.
  1135.  
  1136. ; ---
  1137.  
  1138. ;; K-@-CHAR
  1139. L03B2:  LD      A,$40           ; substitute ASCII '@'
  1140.         RET                     ; return.
  1141.  
  1142.  
  1143. ; ------------------------------------------------------------------------
  1144. ; The Spectrum Input character keys. One or two are abbreviated.
  1145. ; From $00 Flash 0 to $FF COPY. The routine above has decoded all these.
  1146.  
  1147. ;  | 00 Fl0| 01 Fl1| 02 Br0| 03 Br1| 04 In0| 05 In1| 06 CAP| 07 EDT|
  1148. ;  | 08 LFT| 09 RIG| 0A DWN| 0B UP | 0C DEL| 0D ENT| 0E SYM| 0F GRA|
  1149. ;  | 10 Ik0| 11 Ik1| 12 Ik2| 13 Ik3| 14 Ik4| 15 Ik5| 16 Ik6| 17 Ik7|
  1150. ;  | 18 Pa0| 19 Pa1| 1A Pa2| 1B Pa3| 1C Pa4| 1D Pa5| 1E Pa6| 1F Pa7|
  1151. ;  | 20 SP | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  ' |
  1152. ;  | 28  ( | 29  ) | 2A  * | 2B  + | 2C  , | 2D  - | 2E  . | 2F  / |
  1153. ;  | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |
  1154. ;  | 38  8 | 39  9 | 3A  : | 3B  ; | 3C  < | 3D  = | 3E  > | 3F  ? |
  1155. ;  | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |
  1156. ;  | 48  H | 49  I | 4A  J | 4B  K | 4C  L | 4D  M | 4E  N | 4F  O |
  1157. ;  | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |
  1158. ;  | 58  X | 59  Y | 5A  Z | 5B  [ | 5C  \ | 5D  ] | 5E  ^ | 5F  _ |
  1159. ;  | 60 ukp| 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |
  1160. ;  | 68  h | 69  i | 6A  j | 6B  k | 6C  l | 6D  m | 6E  n | 6F  o |
  1161. ;  | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |
  1162. ;  | 78  x | 79  y | 7A  z | 7B  { | 7C  | | 7D  } | 7E  ~ | 7F (c)|
  1163. ;  | 80 128| 81 129| 82 130| 83 131| 84 132| 85 133| 86 134| 87 135|
  1164. ;  | 88 136| 89 137| 8A 138| 8B 139| 8C 140| 8D 141| 8E 142| 8F 143|
  1165. ;  | 90 [A]| 91 [B]| 92 [C]| 93 [D]| 94 [E]| 95 [F]| 96 [G]| 97 [H]|
  1166. ;  | 98 [I]| 99 [J]| 9A [K]| 9B [L]| 9C [M]| 9D [N]| 9E [O]| 9F [P]|
  1167. ;  | A0 [Q]| A1 [R]| A2 [S]| A3 [T]| A4 [U]| A5 RND| A6 IK$| A7 PI |
  1168. ;  | A8 FN | A9 PNT| AA SC$| AB ATT| AC AT | AD TAB| AE VL$| AF COD|
  1169. ;  | B0 VAL| B1 LEN| B2 SIN| B3 COS| B4 TAN| B5 ASN| B6 ACS| B7 ATN|
  1170. ;  | B8 LN | B9 EXP| BA INT| BB SQR| BC SGN| BD ABS| BE PEK| BF IN |
  1171. ;  | C0 USR| C1 ST$| C2 CH$| C3 NOT| C4 BIN| C5 OR | C6 AND| C7 <= |
  1172. ;  | C8 >= | C9 <> | CA LIN| CB THN| CC TO | CD STP| CE DEF| CF CAT|
  1173. ;  | D0 FMT| D1 MOV| D2 ERS| D3 OPN| D4 CLO| D5 MRG| D6 VFY| D7 BEP|
  1174. ;  | D8 CIR| D9 INK| DA PAP| DB FLA| DC BRI| DD INV| DE OVR| DF OUT|
  1175. ;  | E0 LPR| E1 LLI| E2 STP| E3 REA| E4 DAT| E5 RES| E6 NEW| E7 BDR|
  1176. ;  | E8 CON| E9 DIM| EA REM| EB FOR| EC GTO| ED GSB| EE INP| EF LOA|
  1177. ;  | F0 LIS| F1 LET| F2 PAU| F3 NXT| F4 POK| F5 PRI| F6 PLO| F7 RUN|
  1178. ;  | F8 SAV| F9 RAN| FA IF | FB CLS| FC DRW| FD CLR| FE RET| FF CPY|
  1179.  
  1180. ; Note that for simplicity, Sinclair have located all the control codes
  1181. ; below the space character.
  1182. ; ASCII DEL, $7F, has been made a copyright symbol.
  1183. ; Also $60, '`', not used in BASIC but used in other languages, has been
  1184. ; allocated the local currency symbol for the relevant country -
  1185. ; ukp in most Spectrums.
  1186.  
  1187. ; ------------------------------------------------------------------------
  1188.  
  1189. ;**********************************
  1190. ;** Part 3. LOUDSPEAKER ROUTINES **
  1191. ;**********************************
  1192.  
  1193.  
  1194. ; Documented by Alvin Albrecht.
  1195.  
  1196.  
  1197. ; ------------------------------
  1198. ; Routine to control loudspeaker
  1199. ; ------------------------------
  1200. ; Outputs a square wave of given duration and frequency
  1201. ; to the loudspeaker.
  1202. ;   Enter with: DE = #cycles - 1
  1203. ;               HL = tone period as described next
  1204. ;
  1205. ; The tone period is measured in T states and consists of
  1206. ; three parts: a coarse part (H register), a medium part
  1207. ; (bits 7..2 of L) and a fine part (bits 1..0 of L) which
  1208. ; contribute to the waveform timing as follows:
  1209. ;
  1210. ;                          coarse    medium       fine
  1211. ; duration of low  = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1212. ; duration of hi   = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1213. ; Tp = tone period = 236 + 2048*H + 32*(L>>2) + 8*(L&0x3)
  1214. ;                  = 236 + 2048*H + 8*L = 236 + 8*HL
  1215. ;
  1216. ; As an example, to output five seconds of middle C (261.624 Hz):
  1217. ;   (a) Tone period = 1/261.624 = 3.822ms
  1218. ;   (b) Tone period in T-States = 3.822ms*fCPU = 13378
  1219. ;         where fCPU = clock frequency of the CPU = 3.5MHz
  1220. ;   (c) Find H and L for desired tone period:
  1221. ;         HL = (Tp - 236) / 8 = (13378 - 236) / 8 = 1643 = 0x066B
  1222. ;   (d) Tone duration in cycles = 5s/3.822ms = 1308 cycles
  1223. ;         DE = 1308 - 1 = 0x051B
  1224. ;
  1225. ; The resulting waveform has a duty ratio of exactly 50%.
  1226. ;
  1227. ;
  1228. ;; BEEPER
  1229. L03B5:  DI                      ; Disable Interrupts so they don't disturb timing
  1230.         LD      A,L             ;
  1231.         SRL     L               ;
  1232.         SRL     L               ; L = medium part of tone period
  1233.         CPL                     ;
  1234.         AND     $03             ; A = 3 - fine part of tone period
  1235.         LD      C,A             ;
  1236.         LD      B,$00           ;
  1237.         LD      IX,L03D1        ; Address: BE-IX+3
  1238.         ADD     IX,BC           ;   IX holds address of entry into the loop
  1239.                                 ;   the loop will contain 0-3 NOPs, implementing
  1240.                                 ;   the fine part of the tone period.
  1241.         LD      A,($5C48)       ; BORDCR
  1242.         AND     $38             ; bits 5..3 contain border colour
  1243.         RRCA                    ; border colour bits moved to 2..0
  1244.         RRCA                    ;   to match border bits on port #FE
  1245.         RRCA                    ;
  1246.         OR       $08            ; bit 3 set (tape output bit on port #FE)
  1247.                                 ;   for loud sound output
  1248. ;; BE-IX+3
  1249. L03D1:  NOP              ;(4)   ; optionally executed NOPs for small
  1250.                                 ;   adjustments to tone period
  1251. ;; BE-IX+2
  1252. L03D2:  NOP              ;(4)   ;
  1253.  
  1254. ;; BE-IX+1
  1255. L03D3:  NOP              ;(4)   ;
  1256.  
  1257. ;; BE-IX+0
  1258. L03D4:  INC     B        ;(4)   ;
  1259.         INC     C        ;(4)   ;
  1260.  
  1261. ;; BE-H&L-LP
  1262. L03D6:  DEC     C        ;(4)   ; timing loop for duration of
  1263.         JR      NZ,L03D6 ;(12/7);   high or low pulse of waveform
  1264.  
  1265.         LD      C,$3F    ;(7)   ;
  1266.         DEC     B        ;(4)   ;
  1267.         JP      NZ,L03D6 ;(10)  ; to BE-H&L-LP
  1268.  
  1269.         XOR     $10      ;(7)   ; toggle output beep bit
  1270.         OUT     ($FE),A  ;(11)  ; output pulse
  1271.         LD      B,H      ;(4)   ; B = coarse part of tone period
  1272.         LD      C,A      ;(4)   ; save port #FE output byte
  1273.         BIT     4,A      ;(8)   ; if new output bit is high, go
  1274.         JR      NZ,L03F2 ;(12/7);   to BE-AGAIN
  1275.  
  1276.         LD      A,D      ;(4)   ; one cycle of waveform has completed
  1277.         OR      E        ;(4)   ;   (low->low). if cycle countdown = 0
  1278.         JR      Z,L03F6  ;(12/7);   go to BE-END
  1279.  
  1280.         LD      A,C      ;(4)   ; restore output byte for port #FE
  1281.         LD      C,L      ;(4)   ; C = medium part of tone period
  1282.         DEC     DE       ;(6)   ; decrement cycle count
  1283.         JP      (IX)     ;(8)   ; do another cycle
  1284.  
  1285. ;; BE-AGAIN                     ; halfway through cycle
  1286. L03F2:  LD      C,L      ;(4)   ; C = medium part of tone period
  1287.         INC     C        ;(4)   ; adds 16 cycles to make duration of high = duration of low
  1288.         JP      (IX)     ;(8)   ; do high pulse of tone
  1289.  
  1290. ;; BE-END
  1291. L03F6:  EI                      ; Enable Interrupts
  1292.         RET                     ;
  1293.  
  1294.  
  1295. ; -------------------
  1296. ; Handle BEEP command
  1297. ; -------------------
  1298. ; BASIC interface to BEEPER subroutine.
  1299. ; Invoked in BASIC with:
  1300. ;   BEEP dur, pitch
  1301. ;   where dur   = duration in seconds
  1302. ;         pitch = # of semitones above/below middle C
  1303. ;
  1304. ; Enter with: pitch on top of calculator stack
  1305. ;             duration next on calculator stack
  1306. ;
  1307. ;; beep
  1308. L03F8:  RST     28H             ;; FP-CALC
  1309.         DB    $31             ;;duplicate                  ; duplicate pitch
  1310.         DB    $27             ;;int                        ; convert to integer
  1311.         DB    $C0             ;;st-mem-0                   ; store integer pitch to memory 0
  1312.         DB    $03             ;;subtract                   ; calculate fractional part of pitch = fp_pitch - int_pitch
  1313.         DB    $34             ;;stk-data                   ; push constant
  1314.         DB    $EC             ;;Exponent: $7C, Bytes: 4    ; constant = 0.05762265
  1315.         DB    $6C,$98,$1F,$F5 ;;($6C,$98,$1F,$F5)
  1316.         DB    $04             ;;multiply                   ; compute:
  1317.         DB    $A1             ;;stk-one                    ; 1 + 0.05762265 * fraction_part(pitch)
  1318.         DB    $0F             ;;addition
  1319.         DB    $38             ;;end-calc                   ; leave on calc stack
  1320.  
  1321.         LD      HL,$5C92        ; MEM-0: number stored here is in 16 bit integer format (pitch)
  1322.                                 ;   0, 0/FF (pos/neg), LSB, MSB, 0
  1323.                                 ;   LSB/MSB is stored in two's complement
  1324.                                 ; In the following, the pitch is checked if it is in the range -128<=p<=127
  1325.         LD      A,(HL)          ; First byte must be zero, otherwise
  1326.         AND     A               ;   error in integer conversion
  1327.         JR      NZ,L046C        ; to REPORT-B
  1328.  
  1329.         INC     HL              ;
  1330.         LD      C,(HL)          ; C = pos/neg flag = 0/FF
  1331.         INC     HL              ;
  1332.         LD      B,(HL)          ; B = LSB, two's complement
  1333.         LD      A,B             ;
  1334.         RLA                     ;
  1335.         SBC     A,A             ; A = 0/FF if B is pos/neg
  1336.         CP      C               ; must be the same as C if the pitch is -128<=p<=127
  1337.         JR      NZ,L046C        ; if no, error REPORT-B
  1338.  
  1339.         INC     HL              ; if -128<=p<=127, MSB will be 0/FF if B is pos/neg
  1340.         CP      (HL)            ; verify this
  1341.         JR      NZ,L046C        ; if no, error REPORT-B
  1342.                                 ; now we know -128<=p<=127
  1343.         LD      A,B             ; A = pitch + 60
  1344.         ADD     A,$3C           ; if -60<=pitch<=67,
  1345.         JP      P,L0425         ;   goto BE-i-OK
  1346.  
  1347.         JP      PO,L046C        ; if pitch <= 67 goto REPORT-B
  1348.                                 ;   lower bound of pitch set at -60
  1349.  
  1350. ;; BE-I-OK                      ; here, -60<=pitch<=127
  1351.                                 ; and A=pitch+60 -> 0<=A<=187
  1352.  
  1353. L0425:  LD      B,$FA           ; 6 octaves below middle C
  1354.  
  1355. ;; BE-OCTAVE                    ; A=# semitones above 5 octaves below middle C
  1356. L0427:  INC     B               ; increment octave
  1357.         SUB     $0C             ; 12 semitones = one octave
  1358.         JR      NC,L0427        ; to BE-OCTAVE
  1359.  
  1360.         ADD     A,$0C           ; A = # semitones above C (0-11)
  1361.         PUSH    BC              ; B = octave displacement from middle C, 2's complement: -5<=B<=10
  1362.         LD      HL,L046E        ; Address: semi-tone
  1363.         CALL    L3406           ; routine LOC-MEM
  1364.                                 ;   HL = 5*A + $046E
  1365.         CALL    L33B4           ; routine STACK-NUM
  1366.                                 ;   read FP value (freq) from semitone table (HL) and push onto calc stack
  1367.  
  1368.         RST     28H             ;; FP-CALC
  1369.         DB    $04             ;;multiply   mult freq by 1 + 0.0576 * fraction_part(pitch) stacked earlier
  1370.                                 ;;             thus taking into account fractional part of pitch.
  1371.                                 ;;           the number 0.0576*frequency is the distance in Hz to the next
  1372.                                 ;;             note (verify with the frequencies recorded in the semitone
  1373.                                 ;;             table below) so that the fraction_part of the pitch does
  1374.                                 ;;             indeed represent a fractional distance to the next note.
  1375.         DB    $38             ;;end-calc   HL points to first byte of fp num on stack = middle frequency to generate
  1376.  
  1377.         POP     AF              ; A = octave displacement from middle C, 2's complement: -5<=A<=10
  1378.         ADD     A,(HL)          ; increase exponent by A (equivalent to multiplying by 2^A)
  1379.         LD      (HL),A          ;
  1380.  
  1381.         RST     28H             ;; FP-CALC
  1382.         DB    $C0             ;;st-mem-0          ; store frequency in memory 0
  1383.         DB    $02             ;;delete            ; remove from calc stack
  1384.         DB    $31             ;;duplicate         ; duplicate duration (seconds)
  1385.         DB    $38             ;;end-calc
  1386.  
  1387.         CALL    L1E94           ; routine FIND-INT1 ; FP duration to A
  1388.         CP      $0B             ; if dur > 10 seconds,
  1389.         JR      NC,L046C        ;   goto REPORT-B
  1390.  
  1391.         ;;; The following calculation finds the tone period for HL and the cycle count
  1392.         ;;; for DE expected in the BEEPER subroutine.  From the example in the BEEPER comments,
  1393.         ;;;
  1394.         ;;; HL = ((fCPU / f) - 236) / 8 = fCPU/8/f - 236/8 = 437500/f -29.5
  1395.         ;;; DE = duration * frequency - 1
  1396.         ;;;
  1397.         ;;; Note the different constant (30.125) used in the calculation of HL
  1398.         ;;; below.  This is probably an error.
  1399.  
  1400.         RST     28H             ;; FP-CALC
  1401.         DB    $E0             ;;get-mem-0                 ; push frequency
  1402.         DB    $04             ;;multiply                  ; result1: #cycles = duration * frequency
  1403.         DB    $E0             ;;get-mem-0                 ; push frequency
  1404.         DB    $34             ;;stk-data                  ; push constant
  1405.         DB    $80             ;;Exponent $93, Bytes: 3    ; constant = 437500
  1406.         DB    $43,$55,$9F,$80 ;;($55,$9F,$80,$00)
  1407.         DB    $01             ;;exchange                  ; frequency on top
  1408.         DB    $05             ;;division                  ; 437500 / frequency
  1409.         DB    $34             ;;stk-data                  ; push constant
  1410.         DB    $35             ;;Exponent: $85, Bytes: 1   ; constant = 30.125
  1411.         DB    $71             ;;($71,$00,$00,$00)
  1412.         DB    $03             ;;subtract                  ; result2: tone_period(HL) = 437500 / freq - 30.125
  1413.         DB    $38             ;;end-calc
  1414.  
  1415.         CALL    L1E99           ; routine FIND-INT2
  1416.         PUSH    BC              ;   BC = tone_period(HL)
  1417.         CALL    L1E99           ; routine FIND-INT2, BC = #cycles to generate
  1418.         POP     HL              ; HL = tone period
  1419.         LD      D,B             ;
  1420.         LD      E,C             ; DE = #cycles
  1421.         LD      A,D             ;
  1422.         OR      E               ;
  1423.         RET     Z               ; if duration = 0, skip BEEP and avoid 65536 cycle
  1424.                                 ;   boondoggle that would occur next
  1425.         DEC     DE              ; DE = #cycles - 1
  1426.         JP      L03B5           ; to BEEPER
  1427.  
  1428. ; ---
  1429.  
  1430.  
  1431. ;; REPORT-B
  1432. L046C:  RST     08H             ; ERROR-1
  1433.         DB    $0A             ; Error Report: Integer out of range
  1434.  
  1435.  
  1436.  
  1437. ; ---------------
  1438. ; Semi-tone table
  1439. ; ---------------
  1440. ;
  1441. ; Holds frequencies corresponding to semitones in middle octave.
  1442. ; To move n octaves higher or lower, frequencies are multiplied by 2^n.
  1443.  
  1444. ;; semi-tone         five byte fp         decimal freq     note (middle)
  1445. L046E:  DB    $89, $02, $D0, $12, $86;  261.625565290         C
  1446.         DB    $89, $0A, $97, $60, $75;  277.182631135         C#
  1447.         DB    $89, $12, $D5, $17, $1F;  293.664768100         D
  1448.         DB    $89, $1B, $90, $41, $02;  311.126983881         D#
  1449.         DB    $89, $24, $D0, $53, $CA;  329.627557039         E
  1450.         DB    $89, $2E, $9D, $36, $B1;  349.228231549         F
  1451.         DB    $89, $38, $FF, $49, $3E;  369.994422674         F#
  1452.         DB    $89, $43, $FF, $6A, $73;  391.995436072         G
  1453.         DB    $89, $4F, $A7, $00, $54;  415.304697513         G#
  1454.         DB    $89, $5C, $00, $00, $00;  440.000000000         A
  1455.         DB    $89, $69, $14, $F6, $24;  466.163761616         A#
  1456.         DB    $89, $76, $F1, $10, $05;  493.883301378         B
  1457.  
  1458.  
  1459. ;****************************************
  1460. ;** Part 4. CASSETTE HANDLING ROUTINES **
  1461. ;****************************************
  1462.  
  1463. ; These routines begin with the service routines followed by a single
  1464. ; command entry point.
  1465. ; The first of these service routines is a curiosity.
  1466.  
  1467. ; -----------------------
  1468. ; THE 'ZX81 NAME' ROUTINE
  1469. ; -----------------------
  1470. ;   This routine fetches a filename in ZX81 format and is not used by the
  1471. ;   cassette handling routines in this ROM.
  1472.  
  1473. ;; zx81-name
  1474. L04AA:  CALL    L24FB           ; routine SCANNING to evaluate expression.
  1475.         LD      A,($5C3B)       ; fetch system variable FLAGS.
  1476.         ADD     A,A             ; test bit 7 - syntax, bit 6 - result type.
  1477.         JP      M,L1C8A         ; to REPORT-C if not string result
  1478.                                 ; 'Nonsense in BASIC'.
  1479.  
  1480.         POP     HL              ; drop return address.
  1481.         RET     NC              ; return early if checking syntax.
  1482.  
  1483.         PUSH    HL              ; re-save return address.
  1484.         CALL    L2BF1           ; routine STK-FETCH fetches string parameters.
  1485.         LD      H,D             ; transfer start of filename
  1486.         LD      L,E             ; to the HL register.
  1487.         DEC     C               ; adjust to point to last character and
  1488.         RET     M               ; return if the null string.
  1489.                                 ; or multiple of 256!
  1490.  
  1491.         ADD     HL,BC           ; find last character of the filename.
  1492.                                 ; and also clear carry.
  1493.         SET     7,(HL)          ; invert it.
  1494.         RET                     ; return.
  1495.  
  1496. ; =========================================
  1497. ;
  1498. ; PORT 254 ($FE)
  1499. ;
  1500. ;                      spk mic { border  }  
  1501. ;          ___ ___ ___ ___ ___ ___ ___ ___
  1502. ; PORT    |   |   |   |   |   |   |   |   |
  1503. ; 254     |   |   |   |   |   |   |   |   |
  1504. ; $FE     |___|___|___|___|___|___|___|___|
  1505. ;           7   6   5   4   3   2   1   0
  1506. ;
  1507.  
  1508. ; ----------------------------------
  1509. ; Save header and program/data bytes
  1510. ; ----------------------------------
  1511. ; This routine saves a section of data. It is called from SA-CTRL to save the
  1512. ; seventeen bytes of header data. It is also the exit route from that routine
  1513. ; when it is set up to save the actual data.
  1514. ; On entry -
  1515. ; HL points to start of data.
  1516. ; IX points to descriptor.
  1517. ; The accumulator is set to  $00 for a header, $FF for data.
  1518.  
  1519. ;; SA-BYTES
  1520. L04C2:  LD      HL,L053F        ; address: SA/LD-RET
  1521.         PUSH    HL              ; is pushed as common exit route.
  1522.                                 ; however there is only one non-terminal exit
  1523.                                 ; point.
  1524.  
  1525.         LD      HL,$1F80        ; a timing constant H=$1F, L=$80
  1526.                                 ; inner and outer loop counters
  1527.                                 ; a five second lead-in is used for a header.
  1528.  
  1529.         BIT     7,A             ; test one bit of accumulator.
  1530.                                 ; (AND A ?)
  1531.         JR      Z,L04D0         ; skip to SA-FLAG if a header is being saved.
  1532.  
  1533. ; else is data bytes and a shorter lead-in is used.
  1534.  
  1535.         LD      HL,$0C98        ; another timing value H=$0C, L=$98.
  1536.                                 ; a two second lead-in is used for the data.
  1537.  
  1538.  
  1539. ;; SA-FLAG
  1540. L04D0:  EX      AF,AF'          ; save flag
  1541.        INC     DE              ; increase length by one.
  1542.        DEC     IX              ; decrease start.
  1543.  
  1544.        DI                      ; disable interrupts
  1545.  
  1546.        LD      A,$02           ; select red for border, microphone bit on.
  1547.        LD      B,A             ; also does as an initial slight counter value.
  1548.  
  1549. ;; SA-LEADER
  1550. L04D8:  DJNZ    L04D8           ; self loop to SA-LEADER for delay.
  1551.                                ; after initial loop, count is $A4 (or $A3)
  1552.  
  1553.        OUT     ($FE),A         ; output byte $02/$0D to tape port.
  1554.  
  1555.        XOR     $0F             ; switch from RED (mic on) to CYAN (mic off).
  1556.  
  1557.        LD      B,$A4           ; hold count. also timed instruction.
  1558.  
  1559.        DEC     L               ; originally $80 or $98.
  1560.                                ; but subsequently cycles 256 times.
  1561.        JR      NZ,L04D8        ; back to SA-LEADER until L is zero.
  1562.  
  1563. ; the outer loop is counted by H
  1564.  
  1565.        DEC     B               ; decrement count
  1566.        DEC     H               ; originally  twelve or thirty-one.
  1567.        JP      P,L04D8         ; back to SA-LEADER until H becomes $FF
  1568.  
  1569. ; now send a synch pulse. At this stage mic is off and A holds value
  1570. ; for mic on.
  1571. ; A synch pulse is much shorter than the steady pulses of the lead-in.
  1572.  
  1573.        LD      B,$2F           ; another short timed delay.
  1574.  
  1575. ;; SA-SYNC-1
  1576. L04EA:  DJNZ    L04EA           ; self loop to SA-SYNC-1
  1577.  
  1578.        OUT     ($FE),A         ; switch to mic on and red.
  1579.        LD      A,$0D           ; prepare mic off - cyan
  1580.        LD      B,$37           ; another short timed delay.
  1581.  
  1582. ;; SA-SYNC-2
  1583. L04F2:  DJNZ    L04F2           ; self loop to SA-SYNC-2
  1584.  
  1585.        OUT     ($FE),A         ; output mic off, cyan border.
  1586.        LD      BC,$3B0E        ; B=$3B time(*), C=$0E, YELLOW, MIC OFF.
  1587.  
  1588. ;
  1589.  
  1590.        EX      AF,AF'          ; restore saved flag
  1591.                                 ; which is 1st byte to be saved.
  1592.  
  1593.         LD      L,A             ; and transfer to L.
  1594.                                 ; the initial parity is A, $FF or $00.
  1595.         JP      L0507           ; JUMP forward to SA-START     ->
  1596.                                 ; the mid entry point of loop.
  1597.  
  1598. ; -------------------------
  1599. ; During the save loop a parity byte is maintained in H.
  1600. ; the save loop begins by testing if reduced length is zero and if so
  1601. ; the final parity byte is saved reducing count to $FFFF.
  1602.  
  1603. ;; SA-LOOP
  1604. L04FE:  LD      A,D             ; fetch high byte
  1605.         OR      E               ; test against low byte.
  1606.         JR      Z,L050E         ; forward to SA-PARITY if zero.
  1607.  
  1608.         LD      L,(IX+$00)      ; load currently addressed byte to L.
  1609.  
  1610. ;; SA-LOOP-P
  1611. L0505:  LD      A,H             ; fetch parity byte.
  1612.         XOR     L               ; exclusive or with new byte.
  1613.  
  1614. ; -> the mid entry point of loop.
  1615.  
  1616. ;; SA-START
  1617. L0507:  LD      H,A             ; put parity byte in H.
  1618.         LD      A,$01           ; prepare blue, mic=on.
  1619.         SCF                     ; set carry flag ready to rotate in.
  1620.         JP      L0525           ; JUMP forward to SA-8-BITS            -8->
  1621.  
  1622. ; ---
  1623.  
  1624. ;; SA-PARITY
  1625. L050E:  LD      L,H             ; transfer the running parity byte to L and
  1626.         JR      L0505           ; back to SA-LOOP-P
  1627.                                 ; to output that byte before quitting normally.
  1628.  
  1629. ; ---
  1630.  
  1631. ; entry point to save yellow part of bit.
  1632. ; a bit consists of a period with mic on and blue border followed by
  1633. ; a period of mic off with yellow border.
  1634. ; Note. since the DJNZ instruction does not affect flags, the zero flag is used
  1635. ; to indicate which of the two passes is in effect and the carry maintains the
  1636. ; state of the bit to be saved.
  1637.  
  1638. ;; SA-BIT-2
  1639. L0511:  LD      A,C             ; fetch 'mic on and yellow' which is
  1640.                                 ; held permanently in C.
  1641.         BIT     7,B             ; set the zero flag. B holds $3E.
  1642.  
  1643. ; entry point to save 1 entire bit. For first bit B holds $3B(*).
  1644. ; Carry is set if saved bit is 1. zero is reset NZ on entry.
  1645.  
  1646. ;; SA-BIT-1
  1647. L0514:  DJNZ    L0514           ; self loop for delay to SA-BIT-1
  1648.  
  1649.         JR      NC,L051C        ; forward to SA-OUT if bit is 0.
  1650.  
  1651. ; but if bit is 1 then the mic state is held for longer.
  1652.  
  1653.         LD      B,$42           ; set timed delay. (66 decimal)
  1654.  
  1655. ;; SA-SET
  1656. L051A:  DJNZ    L051A           ; self loop to SA-SET
  1657.                                 ; (roughly an extra 66*13 clock cycles)
  1658.  
  1659. ;; SA-OUT
  1660. L051C:  OUT     ($FE),A         ; blue and mic on OR  yellow and mic off.
  1661.  
  1662.         LD      B,$3E           ; set up delay
  1663.         JR      NZ,L0511        ; back to SA-BIT-2 if zero reset NZ (first pass)
  1664.  
  1665. ; proceed when the blue and yellow bands have been output.
  1666.  
  1667.         DEC     B               ; change value $3E to $3D.
  1668.         XOR     A               ; clear carry flag (ready to rotate in).
  1669.         INC     A               ; reset zero flag ie. NZ.
  1670.  
  1671. ; -8->
  1672.  
  1673. ;; SA-8-BITS
  1674. L0525:  RL      L               ; rotate left through carry
  1675.                                 ; C<76543210<C  
  1676.         JP      NZ,L0514        ; JUMP back to SA-BIT-1
  1677.                                 ; until all 8 bits done.
  1678.  
  1679. ; when the initial set carry is passed out again then a byte is complete.
  1680.  
  1681.         DEC     DE              ; decrease length
  1682.         INC     IX              ; increase byte pointer
  1683.         LD      B,$31           ; set up timing.
  1684.  
  1685.         LD      A,$7F           ; test the space key and
  1686.         IN      A,($FE)         ; return to common exit (to restore border)
  1687.         RRA                     ; if a space is pressed
  1688.         RET     NC              ; return to SA/LD-RET.   - - >
  1689.  
  1690. ; now test if byte counter has reached $FFFF.
  1691.  
  1692.         LD      A,D             ; fetch high byte
  1693.         INC     A               ; increment.
  1694.         JP      NZ,L04FE        ; JUMP to SA-LOOP if more bytes.
  1695.  
  1696.         LD      B,$3B           ; a final delay.
  1697.  
  1698. ;; SA-DELAY
  1699. L053C:  DJNZ    L053C           ; self loop to SA-DELAY
  1700.  
  1701.         RET                     ; return - - >
  1702.  
  1703. ; --------------------------------------------------
  1704. ; Reset border and check BREAK key for LOAD and SAVE
  1705. ; --------------------------------------------------
  1706. ; the address of this routine is pushed on the stack prior to any load/save
  1707. ; operation and it handles normal completion with the restoration of the
  1708. ; border and also abnormal termination when the break key, or to be more
  1709. ; precise the space key is pressed during a tape operation.
  1710. ; - - >
  1711.  
  1712. ;; SA/LD-RET
  1713. L053F:  PUSH    AF              ; preserve accumulator throughout.
  1714.         LD      A,($5C48)       ; fetch border colour from BORDCR.
  1715.         AND     $38             ; mask off paper bits.
  1716.         RRCA                    ; rotate
  1717.         RRCA                    ; to the
  1718.         RRCA                    ; range 0-7.
  1719.  
  1720. ;===============================
  1721. ;               IF TAP_EMUL
  1722. ;               DB 0,0
  1723. ;               ELSE
  1724.                 OUT ($FE),A     ; change the border colour.
  1725. ;               ENDIF
  1726. ;===============================
  1727.  
  1728.         LD      A,$7F           ; read from port address $7FFE the
  1729.         IN      A,($FE)         ; row with the space key at outside.
  1730.  
  1731.         RRA                     ; test for space key pressed.
  1732.         EI                      ; enable interrupts
  1733.         JR      C,L0554         ; forward to SA/LD-END if not
  1734.  
  1735.  
  1736. ;; REPORT-Da
  1737. L0552:  RST     08H             ; ERROR-1
  1738.         DB    $0C             ; Error Report: BREAK - CONT repeats
  1739.  
  1740. ; ---
  1741.  
  1742. ;; SA/LD-END
  1743. L0554:  POP     AF              ; restore the accumulator.
  1744.         RET                     ; return.
  1745.  
  1746. ; ------------------------------------
  1747. ; Load header or block of information
  1748. ; ------------------------------------
  1749. ; This routine is used to load bytes and on entry A is set to $00 for a
  1750. ; header or to $FF for data.  IX points to the start of receiving location
  1751. ; and DE holds the length of bytes to be loaded. If, on entry the carry flag
  1752. ; is set then data is loaded, if reset then it is verified.
  1753.  
  1754. ;; LD-BYTES
  1755. L0556:  INC     D               ; reset the zero flag without disturbing carry.
  1756.         EX      AF,AF'          ; preserve entry flags.
  1757.        DEC     D               ; restore high byte of length.
  1758.  
  1759.        DI                      ; disable interrupts
  1760.  
  1761.        LD      A,$0F           ; make the border white and mic off.
  1762.  
  1763. ;===============================
  1764. ;               IF TAP_EMUL
  1765. ;               DB 0,0
  1766. ;               ELSE
  1767.                 OUT ($FE),A     ; output to port.
  1768. ;               ENDIF
  1769. ;===============================
  1770.  
  1771.        LD      HL,L053F        ; Address: SA/LD-RET
  1772.        PUSH    HL              ; is saved on stack as terminating routine.
  1773.  
  1774. ; the reading of the EAR bit (D6) will always be preceded by a test of the
  1775. ; space key (D0), so store the initial post-test state.
  1776.  
  1777.        IN      A,($FE)         ; read the ear state - bit 6.
  1778.        RRA                     ; rotate to bit 5.
  1779.        AND     $20             ; isolate this bit.
  1780.        OR      $02             ; combine with red border colour.
  1781.  
  1782. ;===============================
  1783.                 IF TAP_EMUL
  1784.                 RST 0X08
  1785.                 DB TAPE_EMUL
  1786.                 ELSE
  1787.                 LD C,A          ; and store initial state long-term in C.
  1788.                 CP A            ; set the zero flag.
  1789.                 ENDIF
  1790. ;===============================
  1791.  
  1792. ;; LD-BREAK
  1793. L056B:  RET     NZ              ; return if at any time space is pressed.
  1794.  
  1795. ;; LD-START
  1796. L056C:  CALL    L05E7           ; routine LD-EDGE-1
  1797.        JR      NC,L056B        ; back to LD-BREAK with time out and no
  1798.                                ; edge present on tape.
  1799.  
  1800. ; but continue when a transition is found on tape.
  1801.  
  1802.        LD      HL,$0415        ; set up 16-bit outer loop counter for
  1803.                                ; approx 1 second delay.
  1804.  
  1805. ;; LD-WAIT
  1806. L0574:  DJNZ    L0574           ; self loop to LD-WAIT (for 256 times)
  1807.  
  1808.        DEC     HL              ; decrease outer loop counter.
  1809.        LD      A,H             ; test for
  1810.        OR      L               ; zero.
  1811.        JR      NZ,L0574        ; back to LD-WAIT, if not zero, with zero in B.
  1812.  
  1813. ; continue after delay with H holding zero and B also.
  1814. ; sample 256 edges to check that we are in the middle of a lead-in section.
  1815.  
  1816.        CALL    L05E3           ; routine LD-EDGE-2
  1817.        JR      NC,L056B        ; back to LD-BREAK
  1818.                                ; if no edges at all.
  1819.  
  1820. ;; LD-LEADER
  1821. L0580:  LD      B,$9C           ; set timing value.
  1822.        CALL    L05E3           ; routine LD-EDGE-2
  1823.        JR      NC,L056B        ; back to LD-BREAK if time-out
  1824.  
  1825.        LD      A,$C6           ; two edges must be spaced apart.
  1826.        CP      B               ; compare
  1827.        JR      NC,L056C        ; back to LD-START if too close together for a
  1828.                                ; lead-in.
  1829.  
  1830.        INC     H               ; proceed to test 256 edged sample.
  1831.        JR      NZ,L0580        ; back to LD-LEADER while more to do.
  1832.  
  1833. ; sample indicates we are in the middle of a two or five second lead-in.
  1834. ; Now test every edge looking for the terminal synch signal.
  1835.  
  1836. ;; LD-SYNC
  1837. L058F:  LD      B,$C9           ; initial timing value in B.
  1838.        CALL    L05E7           ; routine LD-EDGE-1
  1839.        JR      NC,L056B        ; back to LD-BREAK with time-out.
  1840.  
  1841.        LD      A,B             ; fetch augmented timing value from B.
  1842.        CP      $D4             ; compare
  1843.        JR      NC,L058F        ; back to LD-SYNC if gap too big, that is,
  1844.                                ; a normal lead-in edge gap.
  1845.  
  1846. ; but a short gap will be the synch pulse.
  1847. ; in which case another edge should appear before B rises to $FF
  1848.  
  1849.        CALL    L05E7           ; routine LD-EDGE-1
  1850.        RET     NC              ; return with time-out.
  1851.  
  1852. ; proceed when the synch at the end of the lead-in is found.
  1853. ; We are about to load data so change the border colours.
  1854.  
  1855.        LD      A,C             ; fetch long-term mask from C
  1856.        XOR     $03             ; and make blue/yellow.
  1857.  
  1858.        LD      C,A             ; store the new long-term byte.
  1859.  
  1860.        LD      H,$00           ; set up parity byte as zero.
  1861.        LD      B,$B0           ; timing.
  1862.        JR      L05C8           ; forward to LD-MARKER
  1863.                                ; the loop mid entry point with the alternate
  1864.                                ; zero flag reset to indicate first byte
  1865.                                ; is discarded.
  1866.  
  1867. ; --------------
  1868. ; the loading loop loads each byte and is entered at the mid point.
  1869.  
  1870. ;; LD-LOOP
  1871. L05A9:  EX      AF,AF'          ; restore entry flags and type in A.
  1872.         JR      NZ,L05B3        ; forward to LD-FLAG if awaiting initial flag
  1873.                                 ; which is to be discarded.
  1874.  
  1875.         JR      NC,L05BD        ; forward to LD-VERIFY if not to be loaded.
  1876.  
  1877.         LD      (IX+$00),L      ; place loaded byte at memory location.
  1878.         JR      L05C2           ; forward to LD-NEXT
  1879.  
  1880. ; ---
  1881.  
  1882. ;; LD-FLAG
  1883. L05B3:  RL      C               ; preserve carry (verify) flag in long-term
  1884.                                 ; state byte. Bit 7 can be lost.
  1885.  
  1886.         XOR     L               ; compare type in A with first byte in L.
  1887.         RET     NZ              ; return if no match e.g. CODE vs DATA.
  1888.  
  1889. ; continue when data type matches.
  1890.  
  1891.         LD      A,C             ; fetch byte with stored carry
  1892.         RRA                     ; rotate it to carry flag again
  1893.         LD      C,A             ; restore long-term port state.
  1894.  
  1895.         INC     DE              ; increment length ??
  1896.         JR      L05C4           ; forward to LD-DEC.
  1897.                                 ; but why not to location after ?
  1898.  
  1899. ; ---
  1900. ; for verification the byte read from tape is compared with that in memory.
  1901.  
  1902. ;; LD-VERIFY
  1903. L05BD:  LD      A,(IX+$00)      ; fetch byte from memory.
  1904.         XOR     L               ; compare with that on tape
  1905.         RET     NZ              ; return if not zero.
  1906.  
  1907. ;; LD-NEXT
  1908. L05C2:  INC     IX              ; increment byte pointer.
  1909.  
  1910. ;; LD-DEC
  1911. L05C4:  DEC     DE              ; decrement length.
  1912.         EX      AF,AF'          ; store the flags.
  1913.        LD      B,$B2           ; timing.
  1914.  
  1915. ; when starting to read 8 bits the receiving byte is marked with bit at right.
  1916. ; when this is rotated out again then 8 bits have been read.
  1917.  
  1918. ;; LD-MARKER
  1919. L05C8:  LD      L,$01           ; initialize as %00000001
  1920.  
  1921. ;; LD-8-BITS
  1922. L05CA:  CALL    L05E3           ; routine LD-EDGE-2 increments B relative to
  1923.                                ; gap between 2 edges.
  1924.        RET     NC              ; return with time-out.
  1925.  
  1926.        LD      A,$CB           ; the comparison byte.
  1927.        CP      B               ; compare to incremented value of B.
  1928.                                ; if B is higher then bit on tape was set.
  1929.                                ; if <= then bit on tape is reset.
  1930.  
  1931.        RL      L               ; rotate the carry bit into L.
  1932.  
  1933.        LD      B,$B0           ; reset the B timer byte.
  1934.        JP      NC,L05CA        ; JUMP back to LD-8-BITS
  1935.  
  1936. ; when carry set then marker bit has been passed out and byte is complete.
  1937.  
  1938.        LD      A,H             ; fetch the running parity byte.
  1939.        XOR     L               ; include the new byte.
  1940.        LD      H,A             ; and store back in parity register.
  1941.  
  1942.        LD      A,D             ; check length of
  1943.        OR      E               ; expected bytes.
  1944.        JR      NZ,L05A9        ; back to LD-LOOP
  1945.                                ; while there are more.
  1946.  
  1947. ; when all bytes loaded then parity byte should be zero.
  1948.  
  1949.        LD      A,H             ; fetch parity byte.
  1950.        CP      $01             ; set carry if zero.
  1951.        RET                     ; return
  1952.                                ; in no carry then error as checksum disagrees.
  1953.  
  1954. ; -------------------------
  1955. ; Check signal being loaded
  1956. ; -------------------------
  1957. ; An edge is a transition from one mic state to another.
  1958. ; More specifically a change in bit 6 of value input from port $FE.
  1959. ; Graphically it is a change of border colour, say, blue to yellow.
  1960. ; The first entry point looks for two adjacent edges. The second entry point
  1961. ; is used to find a single edge.
  1962. ; The B register holds a count, up to 256, within which the edge (or edges)
  1963. ; must be found. The gap between two edges will be more for a '1' than a '0'
  1964. ; so the value of B denotes the state of the bit (two edges) read from tape.
  1965.  
  1966. ; ->
  1967.  
  1968. ;; LD-EDGE-2
  1969. L05E3:  CALL    L05E7           ; call routine LD-EDGE-1 below.
  1970.        RET     NC              ; return if space pressed or time-out.
  1971.                                ; else continue and look for another adjacent
  1972.                                ; edge which together represent a bit on the
  1973.                                ; tape.
  1974.  
  1975. ; ->
  1976. ; this entry point is used to find a single edge from above but also
  1977. ; when detecting a read-in signal on the tape.
  1978.  
  1979. ;; LD-EDGE-1
  1980. L05E7:  LD      A,$16           ; a delay value of twenty two.
  1981.  
  1982. ;; LD-DELAY
  1983. L05E9:  DEC     A               ; decrement counter
  1984.        JR      NZ,L05E9        ; loop back to LD-DELAY 22 times.
  1985.  
  1986.        AND      A              ; clear carry.
  1987.  
  1988. ;; LD-SAMPLE
  1989. L05ED:  INC     B               ; increment the time-out counter.
  1990.        RET     Z               ; return with failure when $FF passed.
  1991.  
  1992.        LD      A,$7F           ; prepare to read keyboard and EAR port
  1993.        IN      A,($FE)         ; row $7FFE. bit 6 is EAR, bit 0 is SPACE key.
  1994.        RRA                     ; test outer key the space. (bit 6 moves to 5)
  1995.        RET     NC              ; return if space pressed.  >>>
  1996.  
  1997.        XOR     C               ; compare with initial long-term state.
  1998.        AND     $20             ; isolate bit 5
  1999.        JR      Z,L05ED         ; back to LD-SAMPLE if no edge.
  2000.  
  2001. ; but an edge, a transition of the EAR bit, has been found so switch the
  2002. ; long-term comparison byte containing both border colour and EAR bit.
  2003.  
  2004.        LD      A,C             ; fetch comparison value.
  2005.        CPL                     ; switch the bits
  2006.        LD      C,A             ; and put back in C for long-term.
  2007.  
  2008.        AND     $07             ; isolate new colour bits.
  2009.        OR      $08             ; set bit 3 - MIC off.
  2010.        OUT     ($FE),A         ; send to port to effect change of colour.
  2011.  
  2012.        SCF                     ; set carry flag signaling edge found within
  2013.                                ; time allowed.
  2014.        RET                     ; return.
  2015.  
  2016. ; ---------------------------------
  2017. ; Entry point for all tape commands
  2018. ; ---------------------------------
  2019. ; This is the single entry point for the four tape commands.
  2020. ; The routine first determines in what context it has been called by examining
  2021. ; the low byte of the Syntax table entry which was stored in T_ADDR.
  2022. ; Subtracting $EO (the present arrangement) gives a value of
  2023. ; $00 - SAVE
  2024. ; $01 - LOAD
  2025. ; $02 - VERIFY
  2026. ; $03 - MERGE
  2027. ; As with all commands the address STMT-RET is on the stack.
  2028.  
  2029. ;; SAVE-ETC
  2030. L0605:  POP     AF              ; discard address STMT-RET.
  2031.        LD      A,($5C74)       ; fetch T_ADDR
  2032.  
  2033. ; Now reduce the low byte of the Syntax table entry to give command.
  2034. ; Note. For ZASM use SUB $E0 as next instruction.
  2035.  
  2036. L0609           SUB LOW (L1ADF)+1       ; subtract the known offset.
  2037.                                ; ( is SUB $E0 in standard ROM )
  2038.  
  2039.        LD      ($5C74),A       ; and put back in T_ADDR as 0,1,2, or 3
  2040.                                ; for future reference.
  2041.  
  2042.        CALL    L1C8C           ; routine EXPT-EXP checks that a string
  2043.                                ; expression follows and stacks the
  2044.                                ; parameters in run-time.
  2045.  
  2046.        CALL    L2530           ; routine SYNTAX-Z
  2047.        JR      Z,L0652         ; forward to SA-DATA if checking syntax.
  2048.  
  2049.        LD      BC,$0011        ; presume seventeen bytes for a header.
  2050.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2051.        AND     A               ; test for zero - SAVE.
  2052.        JR      Z,L0621         ; forward to SA-SPACE if so.
  2053.  
  2054.        LD      C,$22           ; else double length to thirty four.
  2055.  
  2056. ;; SA-SPACE
  2057. L0621:  RST     30H             ; BC-SPACES creates 17/34 bytes in workspace.
  2058.  
  2059.        PUSH    DE              ; transfer the start of new space to
  2060.        POP     IX              ; the available index register.
  2061.  
  2062. ; ten spaces are required for the default filename but it is simpler to
  2063. ; overwrite the first file-type indicator byte as well.
  2064.  
  2065.        LD      B,$0B           ; set counter to eleven.
  2066.        LD      A,$20           ; prepare a space.
  2067.  
  2068. ;; SA-BLANK
  2069. L0629:  LD      (DE),A          ; set workspace location to space.
  2070.        INC     DE              ; next location.
  2071.        DJNZ    L0629           ; loop back to SA-BLANK till all eleven done.
  2072.  
  2073.        LD      (IX+$01),$FF    ; set first byte of ten character filename
  2074.                                ; to $FF as a default to signal null string.
  2075.  
  2076.        CALL    L2BF1           ; routine STK-FETCH fetches the filename
  2077.                                ; parameters from the calculator stack.
  2078.                                ; length of string in BC.
  2079.                                ; start of string in DE.
  2080.  
  2081.        LD      HL,$FFF6        ; prepare the value minus ten.
  2082.        DEC     BC              ; decrement length.
  2083.                                ; ten becomes nine, zero becomes $FFFF.
  2084.        ADD     HL,BC           ; trial addition.
  2085.        INC     BC              ; restore true length.
  2086.        JR      NC,L064B        ; forward to SA-NAME if length is one to ten.
  2087.  
  2088. ; the filename is more than ten characters in length or the null string.
  2089.  
  2090.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2091.        AND     A               ; test for zero - SAVE.
  2092.        JR      NZ,L0644        ; forward to SA-NULL if not the SAVE command.
  2093.  
  2094. ; but no more than ten characters are allowed for SAVE.
  2095. ; The first ten characters of any other command parameter are acceptable.
  2096. ; Weird, but necessary, if saving to sectors.
  2097. ; Note. the golden rule that there are no restriction on anything is broken.
  2098.  
  2099. ;; REPORT-Fa
  2100. L0642:  RST     08H             ; ERROR-1
  2101.        DB    $0E             ; Error Report: Invalid file name
  2102.  
  2103. ; continue with LOAD, MERGE, VERIFY and also SAVE within ten character limit.
  2104.  
  2105. ;; SA-NULL
  2106. L0644:  LD      A,B             ; test length of filename
  2107.        OR      C               ; for zero.
  2108.        JR      Z,L0652         ; forward to SA-DATA if so using the 255
  2109.                                ; indicator followed by spaces.
  2110.  
  2111.        LD      BC,$000A        ; else trim length to ten.
  2112.  
  2113. ; other paths rejoin here with BC holding length in range 1 - 10.
  2114.  
  2115. ;; SA-NAME
  2116. L064B:  PUSH    IX              ; push start of file descriptor.
  2117.        POP     HL              ; and pop into HL.
  2118.  
  2119.        INC     HL              ; HL now addresses first byte of filename.
  2120.        EX      DE,HL           ; transfer destination address to DE, start
  2121.                                ; of string in command to HL.
  2122.        LDIR                    ; copy up to ten bytes
  2123.                                ; if less than ten then trailing spaces follow.
  2124.  
  2125. ; the case for the null string rejoins here.
  2126.  
  2127. ;; SA-DATA
  2128. L0652:  RST     18H             ; GET-CHAR
  2129.        CP      $E4             ; is character after filename the token 'DATA' ?
  2130.        JR      NZ,L06A0        ; forward to SA-SCR$ to consider SCREEN$ if
  2131.                                ; not.
  2132.  
  2133. ; continue to consider DATA.
  2134.  
  2135.        LD      A,($5C74)       ; fetch command from T_ADDR
  2136.        CP      $03             ; is it 'VERIFY' ?
  2137.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2138.                                ; 'Nonsense in BASIC'
  2139.                                ; VERIFY "d" DATA is not allowed.
  2140.  
  2141. ; continue with SAVE, LOAD, MERGE of DATA.
  2142.  
  2143.        RST     20H             ; NEXT-CHAR
  2144.        CALL    L28B2           ; routine LOOK-VARS searches variables area
  2145.                                ; returning with carry reset if found or
  2146.                                ; checking syntax.
  2147.        SET     7,C             ; this converts a simple string to a
  2148.                                ; string array. The test for an array or string
  2149.                                ; comes later.
  2150.        JR      NC,L0672        ; forward to SA-V-OLD if variable found.
  2151.  
  2152.        LD      HL,$0000        ; set destination to zero as not fixed.
  2153.        LD      A,($5C74)       ; fetch command from T_ADDR
  2154.        DEC     A               ; test for 1 - LOAD
  2155.        JR      Z,L0685         ; forward to SA-V-NEW with LOAD DATA.
  2156.                                ; to load a new array.
  2157.  
  2158. ; otherwise the variable was not found in run-time with SAVE/MERGE.
  2159.  
  2160. ;; REPORT-2a
  2161. L0670:  RST     08H             ; ERROR-1
  2162.        DB    $01             ; Error Report: Variable not found
  2163.  
  2164. ; continue with SAVE/LOAD  DATA
  2165.  
  2166. ;; SA-V-OLD
  2167. L0672:  JP      NZ,L1C8A        ; to REPORT-C if not an array variable.
  2168.                                ; or erroneously a simple string.
  2169.                                ; 'Nonsense in BASIC'
  2170.  
  2171.  
  2172.        CALL    L2530           ; routine SYNTAX-Z
  2173.        JR      Z,L0692         ; forward to SA-DATA-1 if checking syntax.
  2174.  
  2175.        INC     HL              ; step past single character variable name.
  2176.        LD      A,(HL)          ; fetch low byte of length.
  2177.        LD      (IX+$0B),A      ; place in descriptor.
  2178.        INC     HL              ; point to high byte.
  2179.        LD      A,(HL)          ; and transfer that
  2180.        LD      (IX+$0C),A      ; to descriptor.
  2181.        INC     HL              ; increase pointer within variable.
  2182.  
  2183. ;; SA-V-NEW
  2184. L0685:  LD      (IX+$0E),C      ; place character array name in  header.
  2185.        LD      A,$01           ; default to type numeric.
  2186.        BIT     6,C             ; test result from look-vars.
  2187.        JR      Z,L068F         ; forward to SA-V-TYPE if numeric.
  2188.  
  2189.        INC     A               ; set type to 2 - string array.
  2190.  
  2191. ;; SA-V-TYPE
  2192. L068F:  LD      (IX+$00),A      ; place type 0, 1 or 2 in descriptor.
  2193.  
  2194. ;; SA-DATA-1
  2195. L0692:  EX      DE,HL           ; save var pointer in DE
  2196.  
  2197.        RST     20H             ; NEXT-CHAR
  2198.        CP      $29             ; is character ')' ?
  2199.        JR      NZ,L0672        ; back if not to SA-V-OLD to report
  2200.                                ; 'Nonsense in BASIC'
  2201.  
  2202.        RST     20H             ; NEXT-CHAR advances character address.
  2203.        CALL    L1BEE           ; routine CHECK-END errors if not end of
  2204.                                ; the statement.
  2205.  
  2206.        EX      DE,HL           ; bring back variables data pointer.
  2207.        JP      L075A           ; jump forward to SA-ALL
  2208.  
  2209. ; ---
  2210. ; the branch was here to consider a 'SCREEN$', the display file.
  2211.  
  2212. ;; SA-SCR$
  2213. L06A0:  CP      $AA             ; is character the token 'SCREEN$' ?
  2214.        JR      NZ,L06C3        ; forward to SA-CODE if not.
  2215.  
  2216.        LD      A,($5C74)       ; fetch command from T_ADDR
  2217.        CP      $03             ; is it MERGE ?
  2218.        JP       Z,L1C8A        ; jump to REPORT-C if so.
  2219.                                ; 'Nonsense in BASIC'
  2220.  
  2221. ; continue with SAVE/LOAD/VERIFY SCREEN$.
  2222.  
  2223.        RST     20H             ; NEXT-CHAR
  2224.        CALL    L1BEE           ; routine CHECK-END errors if not at end of
  2225.                                ; statement.
  2226.  
  2227. ; continue in runtime.
  2228.  
  2229.        LD      (IX+$0B),$00    ; set descriptor length
  2230.        LD      (IX+$0C),$1B    ; to $1b00 to include bitmaps and attributes.
  2231.  
  2232.        LD      HL,$4000        ; set start to display file start.
  2233.        LD      (IX+$0D),L      ; place start in
  2234.        LD      (IX+$0E),H      ; the descriptor.
  2235.        JR      L0710           ; forward to SA-TYPE-3
  2236.  
  2237. ; ---
  2238. ; the branch was here to consider CODE.
  2239.  
  2240. ;; SA-CODE
  2241. L06C3:  CP      $AF             ; is character the token 'CODE' ?
  2242.        JR      NZ,L0716        ; forward if not to SA-LINE to consider an
  2243.                                ; auto-started BASIC program.
  2244.  
  2245.        LD      A,($5C74)       ; fetch command from T_ADDR
  2246.        CP      $03             ; is it MERGE ?
  2247.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2248.                                ; 'Nonsense in BASIC'
  2249.  
  2250.  
  2251.        RST     20H             ; NEXT-CHAR advances character address.
  2252.        CALL    L2048           ; routine PR-ST-END checks if a carriage
  2253.                                ; return or ':' follows.
  2254.        JR      NZ,L06E1        ; forward to SA-CODE-1 if there are parameters.
  2255.  
  2256.        LD      A,($5C74)       ; else fetch the command from T_ADDR.
  2257.        AND     A               ; test for zero - SAVE without a specification.
  2258.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2259.                                ; 'Nonsense in BASIC'
  2260.  
  2261. ; for LOAD/VERIFY put zero on stack to signify handle at location saved from.
  2262.  
  2263.        CALL    L1CE6           ; routine USE-ZERO
  2264.        JR      L06F0           ; forward to SA-CODE-2
  2265.  
  2266. ; ---
  2267. ; if there are more characters after CODE expect start and possibly length.
  2268.  
  2269. ;; SA-CODE-1
  2270. L06E1:  CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2271.                                ; expression and stacks it in run-time.
  2272.  
  2273.        RST     18H             ; GET-CHAR
  2274.        CP      $2C             ; does a comma follow ?
  2275.        JR      Z,L06F5         ; forward if so to SA-CODE-3
  2276.  
  2277. ; else allow saved code to be loaded to a specified address.
  2278.  
  2279.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2280.        AND     A               ; is the command SAVE which requires length ?
  2281.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2282.                                ; 'Nonsense in BASIC'
  2283.  
  2284. ; the command LOAD code may rejoin here with zero stacked as start.
  2285.  
  2286. ;; SA-CODE-2
  2287. L06F0:  CALL    L1CE6           ; routine USE-ZERO stacks zero for length.
  2288.        JR      L06F9           ; forward to SA-CODE-4
  2289.  
  2290. ; ---
  2291. ; the branch was here with SAVE CODE start,
  2292.  
  2293. ;; SA-CODE-3
  2294. L06F5:  RST     20H             ; NEXT-CHAR advances character address.
  2295.        CALL    L1C82           ; routine EXPT-1NUM checks for expression
  2296.                                ; and stacks in run-time.
  2297.  
  2298. ; paths converge here and nothing must follow.
  2299.  
  2300. ;; SA-CODE-4
  2301. L06F9:  CALL    L1BEE           ; routine CHECK-END errors with extraneous
  2302.                                ; characters and quits if checking syntax.
  2303.  
  2304. ; in run-time there are two 16-bit parameters on the calculator stack.
  2305.  
  2306.        CALL    L1E99           ; routine FIND-INT2 gets length.
  2307.        LD      (IX+$0B),C      ; place length
  2308.        LD      (IX+$0C),B      ; in descriptor.
  2309.        CALL    L1E99           ; routine FIND-INT2 gets start.
  2310.        LD      (IX+$0D),C      ; place start
  2311.        LD      (IX+$0E),B      ; in descriptor.
  2312.        LD      H,B             ; transfer the
  2313.        LD      L,C             ; start to HL also.
  2314.  
  2315. ;; SA-TYPE-3
  2316. L0710:  LD      (IX+$00),$03    ; place type 3 - code in descriptor.
  2317.        JR      L075A           ; forward to SA-ALL.
  2318.  
  2319. ; ---
  2320. ; the branch was here with BASIC to consider an optional auto-start line
  2321. ; number.
  2322.  
  2323. ;; SA-LINE
  2324. L0716:  CP      $CA             ; is character the token 'LINE' ?
  2325.        JR      Z,L0723         ; forward to SA-LINE-1 if so.
  2326.  
  2327. ; else all possibilities have been considered and nothing must follow.
  2328.  
  2329.        CALL    L1BEE           ; routine CHECK-END
  2330.  
  2331. ; continue in run-time to save BASIC without auto-start.
  2332.  
  2333.        LD      (IX+$0E),$80    ; place high line number in descriptor to
  2334.                                ; disable auto-start.
  2335.        JR      L073A           ; forward to SA-TYPE-0 to save program.
  2336.  
  2337. ; ---
  2338. ; the branch was here to consider auto-start.
  2339.  
  2340. ;; SA-LINE-1
  2341. L0723:  LD      A,($5C74)       ; fetch command from T_ADDR
  2342.        AND     A               ; test for SAVE.
  2343.        JP      NZ,L1C8A        ; jump forward to REPORT-C with anything else.
  2344.                                ; 'Nonsense in BASIC'
  2345.  
  2346. ;
  2347.  
  2348.        RST     20H             ; NEXT-CHAR
  2349.        CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2350.                                ; expression and stacks in run-time.
  2351.        CALL    L1BEE           ; routine CHECK-END quits if syntax path.
  2352.        CALL    L1E99           ; routine FIND-INT2 fetches the numeric
  2353.                                ; expression.
  2354.        LD      (IX+$0D),C      ; place the auto-start
  2355.        LD      (IX+$0E),B      ; line number in the descriptor.
  2356.  
  2357. ; Note. this isn't checked, but is subsequently handled by the system.
  2358. ; If the user typed 40000 instead of 4000 then it won't auto-start
  2359. ; at line 4000, or indeed, at all.
  2360.  
  2361. ; continue to save program and any variables.
  2362.  
  2363. ;; SA-TYPE-0
  2364. L073A:  LD      (IX+$00),$00    ; place type zero - program in descriptor.
  2365.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  2366.         LD      DE,($5C53)      ; fetch PROG to DE.
  2367.  
  2368. ;===============================
  2369.                 IF TAP_EMUL
  2370.                 CALL INI_TAP
  2371.                 ELSE
  2372.                
  2373.                 SCF             ; set carry flag to calculate from end of
  2374.                                 ; variables E_LINE -1.
  2375.                 SBC HL,DE       ; subtract to give total length.
  2376.                 ENDIF
  2377.  
  2378.         LD      (IX+$0B),L      ; place total length
  2379.         LD      (IX+$0C),H      ; in descriptor.
  2380.         LD      HL,($5C4B)      ; load HL from system variable VARS
  2381.         SBC     HL,DE           ; subtract to give program length.
  2382.         LD      (IX+$0F),L      ; place length of program
  2383.         LD      (IX+$10),H      ; in the descriptor.
  2384.         EX      DE,HL           ; start to HL, length to DE.
  2385.  
  2386. ;; SA-ALL
  2387. L075A:  LD      A,($5C74)       ; fetch command from T_ADDR
  2388.         AND     A               ; test for zero - SAVE.
  2389.         JP      Z,L0970         ; jump forward to SA-CONTRL with SAVE  ->
  2390.  
  2391. ; ---
  2392. ; continue with LOAD, MERGE and VERIFY.
  2393.  
  2394.         PUSH    HL              ; save start.
  2395.         LD      BC,$0011        ; prepare to add seventeen
  2396.         ADD     IX,BC           ; to point IX at second descriptor.
  2397.  
  2398. ;; LD-LOOK-H
  2399. L0767:  PUSH    IX              ; save IX
  2400.         LD      DE,$0011        ; seventeen bytes
  2401.         XOR     A               ; reset zero flag
  2402.         SCF                     ; set carry flag
  2403.         CALL    L0556           ; routine LD-BYTES loads a header from tape
  2404.                                 ; to second descriptor.
  2405.         POP     IX              ; restore IX.
  2406.         JR      NC,L0767        ; loop back to LD-LOOK-H until header found.
  2407.  
  2408.         LD      A,$FE           ; select system channel 'S'
  2409.         CALL    L1601           ; routine CHAN-OPEN opens it.
  2410.  
  2411.         LD      (IY+$52),$03    ; set SCR_CT to 3 lines.
  2412.  
  2413.         LD      C,$80           ; C has bit 7 set to indicate type mismatch as
  2414.                                 ; a default startpoint.
  2415.  
  2416.         LD      A,(IX+$00)      ; fetch loaded header type to A
  2417.         CP      (IX-$11)        ; compare with expected type.
  2418.         JR      NZ,L078A        ; forward to LD-TYPE with mis-match.
  2419.  
  2420.         LD      C,$F6           ; set C to minus ten - will count characters
  2421.                                 ; up to zero.
  2422.  
  2423. ;; LD-TYPE
  2424. L078A:  CP      $04             ; check if type in acceptable range 0 - 3.
  2425.         JR      NC,L0767        ; back to LD-LOOK-H with 4 and over.
  2426.  
  2427. ; else A indicates type 0-3.
  2428.  
  2429.         LD      DE,L09C0        ; address base of last 4 tape messages
  2430.         PUSH    BC              ; save BC
  2431.         CALL    L0C0A           ; routine PO-MSG outputs relevant message.
  2432.                                 ; Note. all messages have a leading newline.
  2433.         POP     BC              ; restore BC
  2434.  
  2435.         PUSH    IX              ; transfer IX,
  2436.         POP     DE              ; the 2nd descriptor, to DE.
  2437.         LD      HL,$FFF0        ; prepare minus seventeen.
  2438.         ADD     HL,DE           ; add to point HL to 1st descriptor.
  2439.         LD      B,$0A           ; the count will be ten characters for the
  2440.                                 ; filename.
  2441.  
  2442.         LD      A,(HL)          ; fetch first character and test for
  2443.         INC     A               ; value 255.
  2444.         JR      NZ,L07A6        ; forward to LD-NAME if not the wildcard.
  2445.  
  2446. ; but if it is the wildcard, then add ten to C which is minus ten for a type
  2447. ; match or -128 for a type mismatch. Although characters have to be counted
  2448. ; bit 7 of C will not alter from state set here.
  2449.  
  2450.         LD      A,C             ; transfer $F6 or $80 to A
  2451.         ADD     A,B             ; add $0A
  2452.         LD      C,A             ; place result, zero or -118, in C.
  2453.  
  2454. ; At this point we have either a type mismatch, a wildcard match or ten
  2455. ; characters to be counted. The characters must be shown on the screen.
  2456.  
  2457. ;; LD-NAME
  2458. L07A6:  INC     DE              ; address next input character
  2459.         LD      A,(DE)          ; fetch character
  2460.         CP      (HL)            ; compare to expected
  2461.         INC     HL              ; address next expected character
  2462.         JR      NZ,L07AD        ; forward to LD-CH-PR with mismatch
  2463.  
  2464.         INC     C               ; increment matched character count
  2465.  
  2466. ;; LD-CH-PR
  2467. L07AD:  RST     10H             ; PRINT-A prints character
  2468.         DJNZ    L07A6           ; loop back to LD-NAME for ten characters.
  2469.  
  2470. ; if ten characters matched and the types previously matched then C will
  2471. ; now hold zero.
  2472.  
  2473.         BIT     7,C             ; test if all matched
  2474.         JR      NZ,L0767        ; back to LD-LOOK-H if not
  2475.  
  2476. ; else print a terminal carriage return.
  2477.  
  2478.         LD      A,$0D           ; prepare carriage return.
  2479.         RST     10H             ; PRINT-A outputs it.
  2480.  
  2481. ; The various control routines for LOAD, VERIFY and MERGE are executed
  2482. ; during the one-second gap following the header on tape.
  2483.  
  2484.         POP     HL              ; restore xx
  2485.         LD      A,(IX+$00)      ; fetch incoming type
  2486.         CP      $03             ; compare with CODE
  2487.         JR      Z,L07CB         ; forward to VR-CONTROL if it is CODE.
  2488.  
  2489. ;  type is a program or an array.
  2490.  
  2491.         LD      A,($5C74)       ; fetch command from T_ADDR
  2492.         DEC     A               ; was it LOAD ?
  2493.         JP      Z,L0808         ; JUMP forward to LD-CONTRL if so to
  2494.                                 ; load BASIC or variables.
  2495.  
  2496.         CP      $02             ; was command MERGE ?
  2497.         JP      Z,L08B6         ; jump forward to ME-CONTRL if so.
  2498.  
  2499. ; else continue into VERIFY control routine to verify.
  2500.  
  2501. ; ---------------------
  2502. ; Handle VERIFY control
  2503. ; ---------------------
  2504. ; There are two branches to this routine.
  2505. ; 1) From above to verify a program or array
  2506. ; 2) from earlier with no carry to load or verify code.
  2507.  
  2508. ;; VR-CONTROL
  2509. L07CB:  PUSH    HL              ; save pointer to data.
  2510.         LD      L,(IX-$06)      ; fetch length of old data
  2511.         LD      H,(IX-$05)      ; to HL.
  2512.         LD      E,(IX+$0B)      ; fetch length of new data
  2513.         LD      D,(IX+$0C)      ; to DE.
  2514.         LD      A,H             ; check length of old
  2515.         OR      L               ; for zero.
  2516.         JR      Z,L07E9         ; forward to VR-CONT-1 if length unspecified
  2517.                                 ; e.g LOAD "x" CODE
  2518.  
  2519. ; as opposed to, say, LOAD 'x' CODE 32768,300.
  2520.  
  2521.         SBC     HL,DE           ; subtract the two lengths.
  2522.         JR      C,L0806         ; forward to REPORT-R if the length on tape is
  2523.                                 ; larger than that specified in command.
  2524.                                 ; 'Tape loading error'
  2525.  
  2526.         JR      Z,L07E9         ; forward to VR-CONT-1 if lengths match.
  2527.  
  2528. ; a length on tape shorter than expected is not allowed for CODE
  2529.  
  2530.         LD      A,(IX+$00)      ; else fetch type from tape.
  2531.         CP      $03             ; is it CODE ?
  2532.         JR      NZ,L0806        ; forward to REPORT-R if so
  2533.                                 ; 'Tape loading error'
  2534.  
  2535. ;; VR-CONT-1
  2536. L07E9:  POP     HL              ; pop pointer to data
  2537.         LD      A,H             ; test for zero
  2538.         OR      L               ; e.g. LOAD 'x' CODE
  2539.         JR      NZ,L07F4        ; forward to VR-CONT-2 if destination specified.
  2540.  
  2541.         LD      L,(IX+$0D)      ; else use the destination in the header
  2542.         LD      H,(IX+$0E)      ; and load code at address saved from.
  2543.  
  2544. ;; VR-CONT-2
  2545. L07F4:  PUSH    HL              ; push pointer to start of data block.
  2546.         POP     IX              ; transfer to IX.
  2547.         LD      A,($5C74)       ; fetch reduced command from T_ADDR
  2548.         CP      $02             ; is it VERIFY ?
  2549.         SCF                     ; prepare a set carry flag
  2550.         JR      NZ,L0800        ; skip to VR-CONT-3 if not
  2551.  
  2552.         AND     A               ; clear carry flag for VERIFY so that
  2553.                                 ; data is not loaded.
  2554.  
  2555. ;; VR-CONT-3
  2556. L0800:  LD      A,$FF           ; signal data block to be loaded
  2557.  
  2558. ; -----------------
  2559. ; Load a data block
  2560. ; -----------------
  2561. ; This routine is called from 3 places other than above to load a data block.
  2562. ; In all cases the accumulator is first set to $FF so the routine could be
  2563. ; called at the previous instruction.
  2564.  
  2565. ;; LD-BLOCK
  2566. L0802:  CALL    L0556           ; routine LD-BYTES
  2567.         RET     C               ; return if successful.
  2568.  
  2569.  
  2570. ;; REPORT-R
  2571. L0806:  RST     08H             ; ERROR-1
  2572.         DB    $1A             ; Error Report: Tape loading error
  2573.  
  2574. ; -------------------
  2575. ; Handle LOAD control
  2576. ; -------------------
  2577. ; This branch is taken when the command is LOAD with type 0, 1 or 2.
  2578.  
  2579. ;; LD-CONTRL
  2580. L0808:  LD      E,(IX+$0B)      ; fetch length of found data block
  2581.         LD      D,(IX+$0C)      ; from 2nd descriptor.
  2582.         PUSH    HL              ; save destination
  2583.         LD      A,H             ; test for zero
  2584.         OR      L               ;
  2585.         JR      NZ,L0819        ; forward if not to LD-CONT-1
  2586.  
  2587.         INC     DE              ; increase length
  2588.         INC     DE              ; for letter name
  2589.         INC     DE              ; and 16-bit length
  2590.         EX      DE,HL           ; length to HL,
  2591.         JR      L0825           ; forward to LD-CONT-2
  2592.  
  2593. ; ---
  2594.  
  2595. ;; LD-CONT-1
  2596. L0819:  LD      L,(IX-$06)      ; fetch length from
  2597.         LD      H,(IX-$05)      ; the first header.
  2598.         EX      DE,HL           ;
  2599.         SCF                     ; set carry flag
  2600.         SBC     HL,DE           ;
  2601.         JR      C,L082E         ; to LD-DATA
  2602.  
  2603. ;; LD-CONT-2
  2604. L0825:  LD      DE,$0005        ; allow overhead of five bytes.
  2605.         ADD     HL,DE           ; add in the difference in data lengths.
  2606.         LD      B,H             ; transfer to
  2607.         LD      C,L             ; the BC register pair
  2608.         CALL    L1F05           ; routine TEST-ROOM fails if not enough room.
  2609.  
  2610. ;; LD-DATA
  2611. L082E:  POP     HL              ; pop destination
  2612.         LD      A,(IX+$00)      ; fetch type 0, 1 or 2.
  2613.         AND     A               ; test for program and variables.
  2614.         JR      Z,L0873         ; forward if so to LD-PROG
  2615.  
  2616. ; the type is a numeric or string array.
  2617.  
  2618.         LD      A,H             ; test the destination for zero
  2619.         OR      L               ; indicating variable does not already exist.
  2620.         JR      Z,L084C         ; forward if so to LD-DATA-1
  2621.  
  2622. ; else the destination is the first dimension within the array structure
  2623.  
  2624.         DEC     HL              ; address high byte of total length
  2625.         LD      B,(HL)          ; transfer to B.
  2626.         DEC     HL              ; address low byte of total length.
  2627.         LD      C,(HL)          ; transfer to C.
  2628.         DEC     HL              ; point to letter of variable.
  2629.         INC     BC              ; adjust length to
  2630.         INC     BC              ; include these
  2631.         INC     BC              ; three bytes also.
  2632.         LD      ($5C5F),IX      ; save header pointer in X_PTR.
  2633.         CALL    L19E8           ; routine RECLAIM-2 reclaims the old variable
  2634.                                 ; sliding workspace including the two headers
  2635.                                 ; downwards.
  2636.         LD      IX,($5C5F)      ; reload IX from X_PTR which will have been
  2637.                                 ; adjusted down by POINTERS routine.
  2638.  
  2639. ;; LD-DATA-1
  2640. L084C:  LD      HL,($5C59)      ; address E_LINE
  2641.         DEC     HL              ; now point to the $80 variables end-marker.
  2642.         LD      C,(IX+$0B)      ; fetch new data length
  2643.         LD      B,(IX+$0C)      ; from 2nd header.
  2644.         PUSH    BC              ; * save it.
  2645.         INC     BC              ; adjust the
  2646.         INC     BC              ; length to include
  2647.         INC     BC              ; letter name and total length.
  2648.         LD      A,(IX-$03)      ; fetch letter name from old header.
  2649.         PUSH    AF              ; preserve accumulator though not corrupted.
  2650.  
  2651.         CALL    L1655           ; routine MAKE-ROOM creates space for variable
  2652.                                 ; sliding workspace up. IX no longer addresses
  2653.                                 ; anywhere meaningful.
  2654.         INC     HL              ; point to first new location.
  2655.  
  2656.         POP     AF              ; fetch back the letter name.
  2657.         LD      (HL),A          ; place in first new location.
  2658.         POP     DE              ; * pop the data length.
  2659.         INC     HL              ; address 2nd location
  2660.         LD      (HL),E          ; store low byte of length.
  2661.         INC     HL              ; address next.
  2662.         LD      (HL),D          ; store high byte.
  2663.         INC     HL              ; address start of data.
  2664.         PUSH    HL              ; transfer address
  2665.         POP     IX              ; to IX register pair.
  2666.         SCF                     ; set carry flag indicating load not verify.
  2667.         LD      A,$FF           ; signal data not header.
  2668.         JP      L0802           ; JUMP back to LD-BLOCK
  2669.  
  2670. ; -----------------
  2671. ; the branch is here when a program as opposed to an array is to be loaded.
  2672.  
  2673. ;; LD-PROG
  2674. L0873:  EX      DE,HL           ; transfer dest to DE.
  2675.         LD      HL,($5C59)      ; address E_LINE
  2676.         DEC     HL              ; now variables end-marker.
  2677.         LD      ($5C5F),IX      ; place the IX header pointer in X_PTR
  2678.         LD      C,(IX+$0B)      ; get new length
  2679.         LD      B,(IX+$0C)      ; from 2nd header
  2680.         PUSH    BC              ; and save it.
  2681.  
  2682.         CALL    L19E5           ; routine RECLAIM-1 reclaims program and vars.
  2683.                                 ; adjusting X-PTR.
  2684.  
  2685.         POP     BC              ; restore new length.
  2686.         PUSH    HL              ; * save start
  2687.         PUSH    BC              ; ** and length.
  2688.  
  2689.         CALL    L1655           ; routine MAKE-ROOM creates the space.
  2690.  
  2691.         LD      IX,($5C5F)      ; reload IX from adjusted X_PTR
  2692.         INC     HL              ; point to start of new area.
  2693.         LD      C,(IX+$0F)      ; fetch length of BASIC on tape
  2694.         LD      B,(IX+$10)      ; from 2nd descriptor
  2695.         ADD     HL,BC           ; add to address the start of variables.
  2696.         LD      ($5C4B),HL      ; set system variable VARS
  2697.  
  2698.         LD      H,(IX+$0E)      ; fetch high byte of autostart line number.
  2699.         LD      A,H             ; transfer to A
  2700.         AND     $C0             ; test if greater than $3F.
  2701.         JR      NZ,L08AD        ; forward to LD-PROG-1 if so with no autostart.
  2702.  
  2703.         LD      L,(IX+$0D)      ; else fetch the low byte.
  2704.         LD      ($5C42),HL      ; set sytem variable to line number NEWPPC
  2705.         LD      (IY+$0A),$00    ; set statement NSPPC to zero.
  2706.  
  2707. ;; LD-PROG-1
  2708. L08AD:  POP     DE              ; ** pop the length
  2709.         POP     IX              ; * and start.
  2710.         SCF                     ; set carry flag
  2711.         LD      A,$FF           ; signal data as opposed to a header.
  2712.         JP      L0802           ; jump back to LD-BLOCK
  2713.  
  2714. ; --------------------
  2715. ; Handle MERGE control
  2716. ; --------------------
  2717. ; the branch was here to merge a program and its variables or an array.
  2718. ;
  2719.  
  2720. ;; ME-CONTRL
  2721. L08B6:  LD      C,(IX+$0B)      ; fetch length
  2722.         LD      B,(IX+$0C)      ; of data block on tape.
  2723.         PUSH    BC              ; save it.
  2724.         INC     BC              ; one for the pot.
  2725.  
  2726.         RST     30H             ; BC-SPACES creates room in workspace.
  2727.                                 ; HL addresses last new location.
  2728.         LD      (HL),$80        ; place end-marker at end.
  2729.         EX      DE,HL           ; transfer first location to HL.
  2730.         POP     DE              ; restore length to DE.
  2731.         PUSH    HL              ; save start.
  2732.  
  2733.         PUSH    HL              ; and transfer it
  2734.         POP     IX              ; to IX register.
  2735.         SCF                     ; set carry flag to load data on tape.
  2736.         LD      A,$FF           ; signal data not a header.
  2737.         CALL    L0802           ; routine LD-BLOCK loads to workspace.
  2738.         POP     HL              ; restore first location in workspace to HL.
  2739. X08CE   LD      DE,($5C53)      ; set DE from system variable PROG.
  2740.  
  2741. ; now enter a loop to merge the data block in workspace with the program and
  2742. ; variables.
  2743.  
  2744. ;; ME-NEW-LP
  2745. L08D2:  LD      A,(HL)          ; fetch next byte from workspace.
  2746.         AND     $C0             ; compare with $3F.
  2747.         JR      NZ,L08F0        ; forward to ME-VAR-LP if a variable or
  2748.                                 ; end-marker.
  2749.  
  2750. ; continue when HL addresses a BASIC line number.
  2751.  
  2752. ;; ME-OLD-LP
  2753. L08D7:  LD      A,(DE)          ; fetch high byte from program area.
  2754.         INC     DE              ; bump prog address.
  2755.         CP      (HL)            ; compare with that in workspace.
  2756.         INC     HL              ; bump workspace address.
  2757.         JR      NZ,L08DF        ; forward to ME-OLD-L1 if high bytes don't match
  2758.  
  2759.         LD      A,(DE)          ; fetch the low byte of program line number.
  2760.         CP      (HL)            ; compare with that in workspace.
  2761.  
  2762. ;; ME-OLD-L1
  2763. L08DF:  DEC     DE              ; point to start of
  2764.         DEC     HL              ; respective lines again.
  2765.         JR      NC,L08EB        ; forward to ME-NEW-L2 if line number in
  2766.                                 ; workspace is less than or equal to current
  2767.                                 ; program line as has to be added to program.
  2768.  
  2769.         PUSH    HL              ; else save workspace pointer.
  2770.         EX      DE,HL           ; transfer prog pointer to HL
  2771.         CALL    L19B8           ; routine NEXT-ONE finds next line in DE.
  2772.         POP     HL              ; restore workspace pointer
  2773.         JR      L08D7           ; back to ME-OLD-LP until destination position
  2774.                                 ; in program area found.
  2775.  
  2776. ; ---
  2777. ; the branch was here with an insertion or replacement point.
  2778.  
  2779. ;; ME-NEW-L2
  2780. L08EB:  CALL    L092C           ; routine ME-ENTER enters the line
  2781.         JR      L08D2           ; loop back to ME-NEW-LP.
  2782.  
  2783. ; ---
  2784. ; the branch was here when the location in workspace held a variable.
  2785.  
  2786. ;; ME-VAR-LP
  2787. L08F0:  LD      A,(HL)          ; fetch first byte of workspace variable.
  2788.         LD      C,A             ; copy to C also.
  2789.         CP      $80             ; is it the end-marker ?
  2790.         RET     Z               ; return if so as complete.  >>>>>
  2791.  
  2792.         PUSH    HL              ; save workspace area pointer.
  2793.         LD      HL,($5C4B)      ; load HL with VARS - start of variables area.
  2794.  
  2795. ;; ME-OLD-VP
  2796. L08F9:  LD      A,(HL)          ; fetch first byte.
  2797.         CP      $80             ; is it the end-marker ?
  2798.         JR      Z,L0923         ; forward if so to ME-VAR-L2 to add
  2799.                                 ; variable at end of variables area.
  2800.  
  2801.         CP      C               ; compare with variable in workspace area.
  2802.         JR      Z,L0909         ; forward to ME-OLD-V2 if a match to replace.
  2803.  
  2804. ; else entire variables area has to be searched.
  2805.  
  2806. ;; ME-OLD-V1
  2807. L0901:  PUSH    BC              ; save character in C.
  2808.         CALL    L19B8           ; routine NEXT-ONE gets following variable
  2809.                                 ; address in DE.
  2810.         POP     BC              ; restore character in C
  2811.         EX      DE,HL           ; transfer next address to HL.
  2812.         JR      L08F9           ; loop back to ME-OLD-VP
  2813.  
  2814. ; ---
  2815. ; the branch was here when first characters of name matched.
  2816.  
  2817. ;; ME-OLD-V2
  2818. L0909:  AND     $E0             ; keep bits 11100000
  2819.         CP      $A0             ; compare   10100000 - a long-named variable.
  2820.  
  2821.         JR      NZ,L0921        ; forward to ME-VAR-L1 if just one-character.
  2822.  
  2823. ; but long-named variables have to be matched character by character.
  2824.  
  2825.         POP     DE              ; fetch workspace 1st character pointer
  2826.         PUSH    DE              ; and save it on the stack again.
  2827.         PUSH    HL              ; save variables area pointer on stack.
  2828.  
  2829. ;; ME-OLD-V3
  2830. L0912:  INC     HL              ; address next character in vars area.
  2831.         INC     DE              ; address next character in workspace area.
  2832.         LD      A,(DE)          ; fetch workspace character.
  2833.         CP      (HL)            ; compare to variables character.
  2834.         JR      NZ,L091E        ; forward to ME-OLD-V4 with a mismatch.
  2835.  
  2836.         RLA                     ; test if the terminal inverted character.
  2837.         JR      NC,L0912        ; loop back to ME-OLD-V3 if more to test.
  2838.  
  2839. ; otherwise the long name matches in its entirety.
  2840.  
  2841.         POP     HL              ; restore pointer to first character of variable
  2842.         JR      L0921           ; forward to ME-VAR-L1
  2843.  
  2844. ; ---
  2845. ; the branch is here when two characters don't match
  2846.  
  2847. ;; ME-OLD-V4
  2848. L091E:  POP     HL              ; restore the prog/vars pointer.
  2849.         JR      L0901           ; back to ME-OLD-V1 to resume search.
  2850.  
  2851. ; ---
  2852. ; branch here when variable is to replace an existing one
  2853.  
  2854. ;; ME-VAR-L1
  2855. L0921:  LD      A,$FF           ; indicate a replacement.
  2856.  
  2857. ; this entry point is when A holds $80 indicating a new variable.
  2858.  
  2859. ;; ME-VAR-L2
  2860. L0923:  POP     DE              ; pop workspace pointer.
  2861.         EX      DE,HL           ; now make HL workspace pointer, DE vars pointer
  2862.         INC     A               ; zero flag set if replacement.
  2863.         SCF                     ; set carry flag indicating a variable not a
  2864.                                 ; program line.
  2865.         CALL    L092C           ; routine ME-ENTER copies variable in.
  2866.         JR      L08F0           ; loop back to ME-VAR-LP
  2867.  
  2868. ; ------------------------
  2869. ; Merge a Line or Variable
  2870. ; ------------------------
  2871. ; A BASIC line or variable is inserted at the current point. If the line numbers
  2872. ; or variable names match (zero flag set) then a replacement takes place.
  2873.  
  2874. ;; ME-ENTER
  2875. L092C:  JR      NZ,L093E        ; forward to ME-ENT-1 for insertion only.
  2876.  
  2877. ; but the program line or variable matches so old one is reclaimed.
  2878.  
  2879.         EX      AF,AF'          ; save flag??
  2880.        LD      ($5C5F),HL      ; preserve workspace pointer in dynamic X_PTR
  2881.        EX      DE,HL           ; transfer program dest pointer to HL.
  2882.        CALL    L19B8           ; routine NEXT-ONE finds following location
  2883.                                ; in program or variables area.
  2884.        CALL    L19E8           ; routine RECLAIM-2 reclaims the space between.
  2885.        EX      DE,HL           ; transfer program dest pointer back to DE.
  2886.        LD      HL,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2887.        EX      AF,AF'          ; restore flags.
  2888.  
  2889. ; now the new line or variable is entered.
  2890.  
  2891. ;; ME-ENT-1
  2892. L093E:  EX      AF,AF'          ; save or re-save flags.
  2893.        PUSH    DE              ; save dest pointer in prog/vars area.
  2894.        CALL    L19B8           ; routine NEXT-ONE finds next in workspace.
  2895.                                ; gets next in DE, difference in BC.
  2896.                                ; prev addr in HL
  2897.        LD      ($5C5F),HL      ; store pointer in X_PTR
  2898.        LD      HL,($5C53)      ; load HL from system variable PROG
  2899.        EX      (SP),HL         ; swap with prog/vars pointer on stack.
  2900.        PUSH    BC              ; ** save length of new program line/variable.
  2901.        EX      AF,AF'          ; fetch flags back.
  2902.         JR      C,L0955         ; skip to ME-ENT-2 if variable
  2903.  
  2904.         DEC     HL              ; address location before pointer
  2905.         CALL    L1655           ; routine MAKE-ROOM creates room for BASIC line
  2906.         INC     HL              ; address next.
  2907.         JR      L0958           ; forward to ME-ENT-3
  2908.  
  2909. ; ---
  2910.  
  2911. ;; ME-ENT-2
  2912. L0955:  CALL    L1655           ; routine MAKE-ROOM creates room for variable.
  2913.  
  2914. ;; ME-ENT-3
  2915. L0958:  INC     HL              ; address next?
  2916.  
  2917.         POP     BC              ; ** pop length
  2918.         POP     DE              ; * pop value for PROG which may have been
  2919.                                 ; altered by POINTERS if first line.
  2920.         LD      ($5C53),DE      ; set PROG to original value.
  2921.         LD      DE,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2922.         PUSH    BC              ; save length
  2923.         PUSH    DE              ; and workspace pointer
  2924.         EX      DE,HL           ; make workspace pointer source, prog/vars
  2925.                                 ; pointer the destination
  2926.         LDIR                    ; copy bytes of line or variable into new area.
  2927.         POP     HL              ; restore workspace pointer.
  2928.         POP     BC              ; restore length.
  2929.         PUSH    DE              ; save new prog/vars pointer.
  2930.         CALL    L19E8           ; routine RECLAIM-2 reclaims the space used
  2931.                                 ; by the line or variable in workspace block
  2932.                                 ; as no longer required and space could be
  2933.                                 ; useful for adding more lines.
  2934.         POP     DE              ; restore the prog/vars pointer
  2935.         RET                     ; return.
  2936.  
  2937. ; -------------------
  2938. ; Handle SAVE control
  2939. ; -------------------
  2940. ; A branch from the main SAVE-ETC routine at SAVE-ALL.
  2941. ; First the header data is saved. Then after a wait of 1 second
  2942. ; the data itself is saved.
  2943. ; HL points to start of data.
  2944. ; IX points to start of descriptor.
  2945.  
  2946. ;; SA-CONTRL
  2947. L0970:  PUSH    HL              ; save start of data
  2948.  
  2949.         LD      A,$FD           ; select system channel 'S'
  2950.         CALL    L1601           ; routine CHAN-OPEN
  2951.  
  2952.         XOR     A               ; clear to address table directly
  2953.         LD      DE,L09A1        ; address: tape-msgs
  2954.         CALL    L0C0A           ; routine PO-MSG -
  2955.                                 ; 'Start tape then press any key.'
  2956.  
  2957.         SET     5,(IY+$02)      ; TV_FLAG  - Signal lower screen requires
  2958.                                 ; clearing
  2959.         CALL    L15D4           ; routine WAIT-KEY
  2960.  
  2961.         PUSH    IX              ; save pointer to descriptor.
  2962.         LD      DE,$0011        ; there are seventeen bytes.
  2963.         XOR     A               ; signal a header.
  2964.         CALL    L04C2           ; routine SA-BYTES
  2965.  
  2966.         POP     IX              ; restore descriptor pointer.
  2967.  
  2968.         LD      B,$32           ; wait for a second - 50 interrupts.
  2969.  
  2970. ;; SA-1-SEC
  2971. L0991:  HALT                    ; wait for interrupt
  2972.         DJNZ    L0991           ; back to SA-1-SEC until pause complete.
  2973.  
  2974.         LD      E,(IX+$0B)      ; fetch length of bytes from the
  2975.         LD      D,(IX+$0C)      ; descriptor.
  2976.  
  2977.         LD      A,$FF           ; signal data bytes.
  2978.  
  2979.         POP     IX              ; retrieve pointer to start
  2980.         JP      L04C2           ; jump back to SA-BYTES
  2981.  
  2982.  
  2983. ; Arrangement of two headers in workspace.
  2984. ; Originally IX addresses first location and only one header is required
  2985. ; when saving.
  2986. ;
  2987. ;   OLD     NEW         PROG   DATA  DATA  CODE
  2988. ;   HEADER  HEADER             num   chr          NOTES.
  2989. ;   ------  ------      ----   ----  ----  ----   -----------------------------
  2990. ;   IX-$11  IX+$00      0      1     2     3      Type.
  2991. ;   IX-$10  IX+$01      x      x     x     x      F  ($FF if filename is null).
  2992. ;   IX-$0F  IX+$02      x      x     x     x      i
  2993. ;   IX-$0E  IX+$03      x      x     x     x      l
  2994. ;   IX-$0D  IX+$04      x      x     x     x      e
  2995. ;   IX-$0C  IX+$05      x      x     x     x      n
  2996. ;   IX-$0B  IX+$06      x      x     x     x      a
  2997. ;   IX-$0A  IX+$07      x      x     x     x      m
  2998. ;   IX-$09  IX+$08      x      x     x     x      e
  2999. ;   IX-$08  IX+$09      x      x     x     x      .
  3000. ;   IX-$07  IX+$0A      x      x     x     x      (terminal spaces).
  3001. ;   IX-$06  IX+$0B      lo     lo    lo    lo     Total  
  3002. ;   IX-$05  IX+$0C      hi     hi    hi    hi     Length of datablock.
  3003. ;   IX-$04  IX+$0D      Auto   -     -     Start  Various
  3004. ;   IX-$03  IX+$0E      Start  a-z   a-z   addr   ($80 if no autostart).
  3005. ;   IX-$02  IX+$0F      lo     -     -     -      Length of Program
  3006. ;   IX-$01  IX+$10      hi     -     -     -      only i.e. without variables.
  3007. ;
  3008.  
  3009.  
  3010. ; ------------------------
  3011. ; Canned cassette messages
  3012. ; ------------------------
  3013. ; The last-character-inverted Cassette messages.
  3014. ; Starts with normal initial step-over byte.
  3015.  
  3016. ;; tape-msgs
  3017. L09A1           DB $80
  3018.                 DC "Start tape, then press any key."    ;DEFM    "Start tape, then press any key"
  3019. L09C0           EQU $-1                                 ;DB    '.'+$80
  3020.                 DB $0D
  3021.                 DC "Program: "                          ;DEFM    "Program:"
  3022.                                                         ;DB    ' '+$80
  3023.                 DB $0D
  3024.                 DC "Number array: "                     ;DEFM    "Number array:"
  3025.                                                         ;DB    ' '+$80
  3026.                 DB $0D
  3027.                 DC "Character array: "                  ;DEFM    "Character array:"
  3028.                                                         ;DB    ' '+$80
  3029.                 DB $0D
  3030.                 DC "Bytes: "                            ;DEFM    "Bytes:"
  3031.                                                         ;DB    ' '+$80
  3032. ;               DB    ' '+$80
  3033.  
  3034.  
  3035. ;**************************************************
  3036. ;** Part 5. SCREEN AND PRINTER HANDLING ROUTINES **
  3037. ;**************************************************
  3038.  
  3039. ; ---------------------
  3040. ; General PRINT routine
  3041. ; ---------------------
  3042. ; This is the routine most often used by the RST 10 restart although the
  3043. ; subroutine is on two occasions called directly when it is known that
  3044. ; output will definitely be to the lower screen.
  3045.  
  3046. ;; PRINT-OUT
  3047. L09F4:  CALL    L0B03           ; routine PO-FETCH fetches print position
  3048.                                 ; to HL register pair.
  3049.         CP      $20             ; is character a space or higher ?
  3050.         JP      NC,L0AD9        ; jump forward to PO-ABLE if so.
  3051.  
  3052.         CP      $06             ; is character in range 00-05 ?
  3053.         JR      C,L0A69         ; to PO-QUEST to print '?' if so.
  3054.  
  3055.         CP      $18             ; is character in range 24d - 31d ?
  3056.         JR      NC,L0A69        ; to PO-QUEST to also print '?' if so.
  3057.  
  3058.         LD      HL,L0A11 - 6    ; address 0A0B - the base address of control
  3059.                                 ; character table - where zero would be.
  3060.         LD      E,A             ; control character 06 - 23d
  3061.         LD      D,$00           ; is transferred to DE.
  3062.  
  3063.         ADD     HL,DE           ; index into table.
  3064.  
  3065.         LD      E,(HL)          ; fetch the offset to routine.
  3066.         ADD     HL,DE           ; add to make HL the address.
  3067.         PUSH    HL              ; push the address.
  3068.         JP      L0B03           ; to PO-FETCH, as the screen/printer position
  3069.                                 ; has been disturbed, and indirectly to
  3070.                                 ; routine on stack.
  3071.  
  3072. ; -----------------------
  3073. ; Control character table
  3074. ; -----------------------
  3075. ; For control characters in the range 6 - 23d the following table
  3076. ; is indexed to provide an offset to the handling routine that
  3077. ; follows the table.
  3078.  
  3079. ;; ctlchrtab
  3080. L0A11:  DB    L0A5F - $       ; 06d offset $4E to Address: PO-COMMA
  3081.         DB    L0A69 - $       ; 07d offset $57 to Address: PO-QUEST
  3082.         DB    L0A23 - $       ; 08d offset $10 to Address: PO-BACK-1
  3083.         DB    L0A3D - $       ; 09d offset $29 to Address: PO-RIGHT
  3084.         DB    L0A69 - $       ; 10d offset $54 to Address: PO-QUEST
  3085.         DB    L0A69 - $       ; 11d offset $53 to Address: PO-QUEST
  3086.         DB    L0A69 - $       ; 12d offset $52 to Address: PO-QUEST
  3087.         DB    L0A4F - $       ; 13d offset $37 to Address: PO-ENTER
  3088.         DB    L0A69 - $       ; 14d offset $50 to Address: PO-QUEST
  3089.         DB    L0A69 - $       ; 15d offset $4F to Address: PO-QUEST
  3090.         DB    L0A7A - $       ; 16d offset $5F to Address: PO-1-OPER
  3091.         DB    L0A7A - $       ; 17d offset $5E to Address: PO-1-OPER
  3092.         DB    L0A7A - $       ; 18d offset $5D to Address: PO-1-OPER
  3093.         DB    L0A7A - $       ; 19d offset $5C to Address: PO-1-OPER
  3094.         DB    L0A7A - $       ; 20d offset $5B to Address: PO-1-OPER
  3095.         DB    L0A7A - $       ; 21d offset $5A to Address: PO-1-OPER
  3096.         DB    L0A75 - $       ; 22d offset $54 to Address: PO-2-OPER
  3097.         DB    L0A75 - $       ; 23d offset $53 to Address: PO-2-OPER
  3098.  
  3099.  
  3100. ; -------------------
  3101. ; Cursor left routine
  3102. ; -------------------
  3103. ; Backspace and up a line if that action is from the left of screen.
  3104. ; For ZX printer backspace up to first column but not beyond.
  3105.  
  3106. ;; PO-BACK-1
  3107. L0A23:  INC     C               ; move left one column.
  3108.         LD      A,$22           ; value $21 is leftmost column.
  3109.         CP      C               ; have we passed ?
  3110.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3111.  
  3112.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3113.         JR      NZ,L0A38        ; to PO-BACK-2 if so, as we are unable to
  3114.                                 ; backspace from the leftmost position.
  3115.  
  3116.  
  3117.         INC     B               ; move up one screen line
  3118.         LD      C,$02           ; the rightmost column position.
  3119.         LD      A,$18           ; Note. This should be $19
  3120.                                 ; credit. Dr. Frank O'Hara, 1982
  3121.  
  3122.         CP      B               ; has position moved past top of screen ?
  3123.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3124.  
  3125.         DEC     B               ; else back to $18.
  3126.  
  3127. ;; PO-BACK-2
  3128. L0A38:  LD      C,$21           ; the leftmost column position.
  3129.  
  3130. ;; PO-BACK-3
  3131. L0A3A:  JP      L0DD9           ; to CL-SET and PO-STORE to save new
  3132.                                 ; position in system variables.
  3133.  
  3134. ; --------------------
  3135. ; Cursor right routine
  3136. ; --------------------
  3137. ; This moves the print position to the right leaving a trail in the
  3138. ; current background colour.
  3139. ; "However the programmer has failed to store the new print position
  3140. ;  so CHR$ 9 will only work if the next print position is at a newly
  3141. ;  defined place.
  3142. ;   e.g. PRINT PAPER 2; CHR$ 9; AT 4,0;
  3143. ;  does work but is not very helpful"
  3144. ; - Dr. Ian Logan, Understanding Your Spectrum, 1982.
  3145.  
  3146. ;; PO-RIGHT
  3147. L0A3D:  LD      A,($5C91)       ; fetch P_FLAG value
  3148.         PUSH    AF              ; and save it on stack.
  3149.  
  3150.         LD      (IY+$57),$01    ; temporarily set P_FLAG 'OVER 1'.
  3151.         LD      A,$20           ; prepare a space.
  3152.         CALL    L0B65           ; routine PO-CHAR to print it.
  3153.                                 ; Note. could be PO-ABLE which would update
  3154.                                 ; the column position.
  3155.  
  3156.         POP     AF              ; restore the permanent flag.
  3157.         LD      ($5C91),A       ; and restore system variable P_FLAG
  3158.  
  3159.         RET                     ; return without updating column position
  3160.  
  3161. ; -----------------------
  3162. ; Perform carriage return
  3163. ; -----------------------
  3164. ; A carriage return is 'printed' to screen or printer buffer.
  3165.  
  3166. ;; PO-ENTER
  3167. L0A4F:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3168.         JP      NZ,L0ECD        ; to COPY-BUFF if so, to flush buffer and reset
  3169.                                 ; the print position.
  3170.  
  3171.         LD      C,$21           ; the leftmost column position.
  3172.         CALL    L0C55           ; routine PO-SCR handles any scrolling required.
  3173.         DEC     B               ; to next screen line.
  3174.         JP      L0DD9           ; jump forward to CL-SET to store new position.
  3175.  
  3176. ; -----------
  3177. ; Print comma
  3178. ; -----------
  3179. ; The comma control character. The 32 column screen has two 16 character
  3180. ; tabstops.  The routine is only reached via the control character table.
  3181.  
  3182. ;; PO-COMMA
  3183. L0A5F:  CALL    L0B03           ; routine PO-FETCH - seems unnecessary.
  3184.  
  3185.         LD      A,C             ; the column position. $21-$01
  3186.         DEC     A               ; move right. $20-$00
  3187.         DEC     A               ; and again   $1F-$00 or $FF if trailing
  3188.         AND     $10             ; will be $00 or $10.
  3189.         JR      L0AC3           ; forward to PO-FILL
  3190.  
  3191. ; -------------------
  3192. ; Print question mark
  3193. ; -------------------
  3194. ; This routine prints a question mark which is commonly
  3195. ; used to print an unassigned control character in range 0-31d.
  3196. ; there are a surprising number yet to be assigned.
  3197.  
  3198. ;; PO-QUEST
  3199. L0A69:  LD      A,$3F           ; prepare the character '?'.
  3200.         JR      L0AD9           ; forward to PO-ABLE.
  3201.  
  3202. ; --------------------------------
  3203. ; Control characters with operands
  3204. ; --------------------------------
  3205. ; Certain control characters are followed by 1 or 2 operands.
  3206. ; The entry points from control character table are PO-2-OPER and PO-1-OPER.
  3207. ; The routines alter the output address of the current channel so that
  3208. ; subsequent RST $10 instructions take the appropriate action
  3209. ; before finally resetting the output address back to PRINT-OUT.
  3210.  
  3211. ;; PO-TV-2
  3212. L0A6D:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3213.         LD      ($5C0F),A       ; store first operand in TVDATA-hi
  3214.         JR      L0A80           ; forward to PO-CHANGE >>
  3215.  
  3216. ; ---
  3217.  
  3218. ; -> This initial entry point deals with two operands - AT or TAB.
  3219.  
  3220. ;; PO-2-OPER
  3221. L0A75:  LD      DE,L0A6D        ; address: PO-TV-2 will be next output routine
  3222.         JR      L0A7D           ; forward to PO-TV-1
  3223.  
  3224. ; ---
  3225.  
  3226. ; -> This initial entry point deals with one operand INK to OVER.
  3227.  
  3228. ;; PO-1-OPER
  3229. L0A7A:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3230.  
  3231. ;; PO-TV-1
  3232. L0A7D:  LD      ($5C0E),A       ; store control code in TVDATA-lo
  3233.  
  3234. ;; PO-CHANGE
  3235. L0A80:  LD      HL,($5C51)      ; use CURCHL to find current output channel.
  3236.         LD      (HL),E          ; make it
  3237.         INC     HL              ; the supplied
  3238.         LD      (HL),D          ; address from DE.
  3239.         RET                     ; return.
  3240.  
  3241. ; ---
  3242.  
  3243. ;; PO-CONT
  3244. L0A87:  LD      DE,L09F4        ; Address: PRINT-OUT
  3245.         CALL    L0A80           ; routine PO-CHANGE to restore normal channel.
  3246.         LD      HL,($5C0E)      ; TVDATA gives control code and possible
  3247.                                 ; subsequent character
  3248.         LD      D,A             ; save current character
  3249.         LD      A,L             ; the stored control code
  3250.         CP      $16             ; was it INK to OVER (1 operand) ?
  3251.         JP      C,L2211         ; to CO-TEMP-5
  3252.  
  3253.         JR      NZ,L0AC2        ; to PO-TAB if not 22d i.e. 23d TAB.
  3254.  
  3255.                                 ; else must have been 22d AT.
  3256.         LD      B,H             ; line to H   (0-23d)
  3257.         LD      C,D             ; column to C (0-31d)
  3258.         LD      A,$1F           ; the value 31d
  3259.         SUB     C               ; reverse the column number.
  3260.         JR      C,L0AAC         ; to PO-AT-ERR if C was greater than 31d.
  3261.  
  3262.         ADD     A,$02           ; transform to system range $02-$21
  3263.         LD      C,A             ; and place in column register.
  3264.  
  3265.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3266.         JR      NZ,L0ABF        ; to PO-AT-SET as line can be ignored.
  3267.  
  3268.         LD      A,$16           ; 22 decimal
  3269.         SUB     B               ; subtract line number to reverse
  3270.                                 ; 0 - 22 becomes 22 - 0.
  3271.  
  3272. ;; PO-AT-ERR
  3273. L0AAC:  JP      C,L1E9F         ; to REPORT-B if higher than 22 decimal
  3274.                                 ; Integer out of range.
  3275.  
  3276.         INC     A               ; adjust for system range $01-$17
  3277.         LD      B,A             ; place in line register
  3278.         INC     B               ; adjust to system range  $02-$18
  3279.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3280.         JP      NZ,L0C55        ; exit to PO-SCR to test for scrolling
  3281.  
  3282.         CP      (IY+$31)        ; Compare against DF_SZ
  3283.         JP      C,L0C86         ; to REPORT-5 if too low
  3284.                                 ; Out of screen.
  3285.  
  3286. ;; PO-AT-SET
  3287. L0ABF:  JP      L0DD9           ; print position is valid so exit via CL-SET
  3288.  
  3289. ; Continue here when dealing with TAB.
  3290. ; Note. In BASIC, TAB is followed by a 16-bit number and was initially
  3291. ; designed to work with any output device.
  3292.  
  3293. ;; PO-TAB
  3294. L0AC2:  LD      A,H             ; transfer parameter to A
  3295.                                 ; Losing current character -
  3296.                                 ; High byte of TAB parameter.
  3297.  
  3298.  
  3299. ;; PO-FILL
  3300. L0AC3:  CALL    L0B03           ; routine PO-FETCH, HL-addr, BC=line/column.
  3301.                                 ; column 1 (right), $21 (left)
  3302.         ADD     A,C             ; add operand to current column
  3303.         DEC     A               ; range 0 - 31+
  3304.         AND     $1F             ; make range 0 - 31d
  3305.         RET     Z               ; return if result zero
  3306.  
  3307.         LD      D,A             ; Counter to D
  3308.         SET     0,(IY+$01)      ; update FLAGS  - signal suppress leading space.
  3309.  
  3310. ;; PO-SPACE
  3311. L0AD0:  LD      A,$20           ; space character.
  3312.         CALL    L0C3B           ; routine PO-SAVE prints the character
  3313.                                 ; using alternate set (normal output routine)
  3314.         DEC     D               ; decrement counter.
  3315.         JR      NZ,L0AD0        ; to PO-SPACE until done
  3316.  
  3317.         RET                     ; return
  3318.  
  3319. ; ----------------------
  3320. ; Printable character(s)
  3321. ; ----------------------
  3322. ; This routine prints printable characters and continues into
  3323. ; the position store routine
  3324.  
  3325. ;; PO-ABLE
  3326. L0AD9:  CALL    L0B24           ; routine PO-ANY
  3327.                                 ; and continue into position store routine.
  3328.  
  3329. ; -------------------------------------
  3330. ; Store line, column, and pixel address
  3331. ; -------------------------------------
  3332. ; This routine updates the system variables associated with
  3333. ; The main screen, lower screen/input buffer or ZX printer.
  3334.  
  3335. ;; PO-STORE
  3336. L0ADC:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use ?
  3337.         JR      NZ,L0AFC        ; to PO-ST-PR if so
  3338.  
  3339.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3340.         JR      NZ,L0AF0        ; to PO-ST-E if so
  3341.  
  3342.         LD      ($5C88),BC      ; S_POSN line/column upper screen
  3343.         LD      ($5C84),HL      ; DF_CC  display file address
  3344.         RET                     ;
  3345.  
  3346. ; ---
  3347.  
  3348. ;; PO-ST-E
  3349. L0AF0:  LD      ($5C8A),BC      ; SPOSNL line/column lower screen
  3350.         LD      ($5C82),BC      ; ECHO_E line/column input buffer
  3351.         LD      ($5C86),HL      ; DFCCL  lower screen memory address
  3352.         RET                     ;
  3353.  
  3354. ; ---
  3355.  
  3356. ;; PO-ST-PR
  3357. L0AFC:  LD      (IY+$45),C      ; P_POSN column position printer
  3358.         LD      ($5C80),HL      ; PR_CC  full printer buffer memory address
  3359.         RET                     ;
  3360.  
  3361. ; -------------------------
  3362. ; Fetch position parameters
  3363. ; -------------------------
  3364. ; This routine fetches the line/column and display file address
  3365. ; of the upper and lower screen or, if the printer is in use,
  3366. ; the column position and absolute memory address.
  3367. ; Note. that PR-CC-hi (23681) is used by this routine and the one above
  3368. ; and if, in accordance with the manual (that says this is unused), the
  3369. ; location has been used for other purposes, then subsequent output
  3370. ; to the printer buffer could corrupt a 256-byte section of memory.
  3371.  
  3372. ;; PO-FETCH
  3373. L0B03:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3374.         JR      NZ,L0B1D        ; to PO-F-PR if so
  3375.  
  3376.                                 ; assume upper screen
  3377.         LD      BC,($5C88)      ; S_POSN
  3378.         LD      HL,($5C84)      ; DF_CC display file address
  3379.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3380.         RET     Z               ; return if upper screen
  3381.  
  3382.                                 ; ah well, was lower screen
  3383.         LD      BC,($5C8A)      ; SPOSNL
  3384.         LD      HL,($5C86)      ; DFCCL
  3385.         RET                     ; return
  3386.  
  3387. ; ---
  3388.  
  3389. ;; PO-F-PR
  3390. L0B1D:  LD      C,(IY+$45)      ; P_POSN column only
  3391.         LD      HL,($5C80)      ; PR_CC printer buffer address
  3392.         RET                     ; return
  3393.  
  3394. ; -------------------
  3395. ; Print any character
  3396. ; -------------------
  3397. ; This routine is used to print any character in range 32d - 255d
  3398. ; It is only called from PO-ABLE which continues into PO-STORE
  3399.  
  3400. ;; PO-ANY
  3401. L0B24:  CP      $80             ; ASCII ?
  3402.         JR      C,L0B65         ; to PO-CHAR is so.
  3403.  
  3404.         CP      $90             ; test if a block graphic character.
  3405.         JR      NC,L0B52        ; to PO-T&UDG to print tokens and udg's
  3406.  
  3407. ; The 16 2*2 mosaic characters 128-143 decimal are formed from
  3408. ; bits 0-3 of the character.
  3409.  
  3410.         LD      B,A             ; save character
  3411.         CALL    L0B38           ; routine PO-GR-1 to construct top half
  3412.                                 ; then bottom half.
  3413.         CALL    L0B03           ; routine PO-FETCH fetches print position.
  3414.         LD      DE,$5C92        ; MEM-0 is location of 8 bytes of character
  3415.         JR      L0B7F           ; to PR-ALL to print to screen or printer
  3416.  
  3417. ; ---
  3418.  
  3419. ;; PO-GR-1
  3420. L0B38:  LD      HL,$5C92        ; address MEM-0 - a temporary buffer in
  3421.                                 ; systems variables which is normally used
  3422.                                 ; by the calculator.
  3423.         CALL    L0B3E           ; routine PO-GR-2 to construct top half
  3424.                                 ; and continue into routine to construct
  3425.                                 ; bottom half.
  3426.  
  3427. ;; PO-GR-2
  3428. L0B3E:  RR      B               ; rotate bit 0/2 to carry
  3429.         SBC     A,A             ; result $00 or $FF
  3430.         AND     $0F             ; mask off right hand side
  3431.         LD      C,A             ; store part in C
  3432.         RR      B               ; rotate bit 1/3 of original chr to carry
  3433.         SBC     A,A             ; result $00 or $FF
  3434.         AND     $F0             ; mask off left hand side
  3435.         OR      C               ; combine with stored pattern
  3436.         LD      C,$04           ; four bytes for top/bottom half
  3437.  
  3438. ;; PO-GR-3
  3439. L0B4C:  LD      (HL),A          ; store bit patterns in temporary buffer
  3440.         INC     HL              ; next address
  3441.         DEC     C               ; jump back to
  3442.         JR      NZ,L0B4C        ; to PO-GR-3 until byte is stored 4 times
  3443.  
  3444.         RET                     ; return
  3445.  
  3446. ; ---
  3447.  
  3448. ; Tokens and User defined graphics are now separated.
  3449.  
  3450. ;; PO-T&UDG
  3451. L0B52           IF BAS48_ONLY
  3452.                 SUB 0XA5
  3453.                 JR NC,L0B5F
  3454.                 ELSE
  3455.                 JP L3B9F                ;Spectrum 128 patch
  3456.                 NOP
  3457.                 ENDIF
  3458.  
  3459. L0B56:  ADD     A,$15           ; add 21d to restore to 0 - 20
  3460.         PUSH    BC              ; save current print position
  3461.         LD      BC,($5C7B)      ; fetch UDG to address bit patterns
  3462.         JR      L0B6A           ; to PO-CHAR-2 - common code to lay down
  3463.                                 ; a bit patterned character
  3464.  
  3465. ; ---
  3466.  
  3467. ;; PO-T
  3468. L0B5F:  CALL    L0C10           ; routine PO-TOKENS prints tokens
  3469.         JP      L0B03           ; exit via a JUMP to PO-FETCH as this routine
  3470.                                 ; must continue into PO-STORE.
  3471.                                 ; A JR instruction could be used.
  3472.  
  3473. ; This point is used to print ASCII characters  32d - 127d.
  3474.  
  3475. ;; PO-CHAR
  3476. L0B65:  PUSH    BC              ; save print position
  3477.         LD      BC,($5C36)      ; address CHARS
  3478.  
  3479. ; This common code is used to transfer the character bytes to memory.
  3480.  
  3481. ;; PO-CHAR-2
  3482. L0B6A:  EX      DE,HL           ; transfer destination address to DE
  3483.         LD      HL,$5C3B        ; point to FLAGS
  3484.         RES     0,(HL)          ; allow for leading space
  3485.         CP      $20             ; is it a space ?
  3486.         JR      NZ,L0B76        ; to PO-CHAR-3 if not
  3487.  
  3488.         SET     0,(HL)          ; signal no leading space to FLAGS
  3489.  
  3490. ;; PO-CHAR-3
  3491. L0B76:  LD      H,$00           ; set high byte to 0
  3492.         LD      L,A             ; character to A
  3493.                                 ; 0-21 UDG or 32-127 ASCII.
  3494.         ADD     HL,HL           ; multiply
  3495.         ADD     HL,HL           ; by
  3496.         ADD     HL,HL           ; eight
  3497.         ADD     HL,BC           ; HL now points to first byte of character
  3498.         POP     BC              ; the source address CHARS or UDG
  3499.         EX      DE,HL           ; character address to DE
  3500.  
  3501. ; --------------------
  3502. ; Print all characters
  3503. ; --------------------
  3504. ; This entry point entered from above to print ASCII and UDGs
  3505. ; but also from earlier to print mosaic characters.
  3506. ; HL=destination
  3507. ; DE=character source
  3508. ; BC=line/column
  3509.  
  3510. ;; PR-ALL
  3511. L0B7F:  LD      A,C             ; column to A
  3512.         DEC     A               ; move right
  3513.         LD      A,$21           ; pre-load with leftmost position
  3514.         JR      NZ,L0B93        ; but if not zero to PR-ALL-1
  3515.  
  3516.         DEC     B               ; down one line
  3517.         LD      C,A             ; load C with $21
  3518.         BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3519.         JR      Z,L0B93         ; to PR-ALL-1 if not
  3520.  
  3521.         PUSH    DE              ; save source address
  3522.         CALL    L0ECD           ; routine COPY-BUFF outputs line to printer
  3523.         POP     DE              ; restore character source address
  3524.         LD      A,C             ; the new column number ($21) to C
  3525.  
  3526. ;; PR-ALL-1
  3527. L0B93:  CP      C               ; this test is really for screen - new line ?
  3528.         PUSH    DE              ; save source
  3529.  
  3530.         CALL    Z,L0C55         ; routine PO-SCR considers scrolling
  3531.  
  3532.         POP     DE              ; restore source
  3533.         PUSH    BC              ; save line/column
  3534.         PUSH    HL              ; and destination
  3535.         LD      A,($5C91)       ; fetch P_FLAG to accumulator
  3536.         LD      B,$FF           ; prepare OVER mask in B.
  3537.         RRA                     ; bit 0 set if OVER 1
  3538.         JR      C,L0BA4         ; to PR-ALL-2
  3539.  
  3540.         INC     B               ; set OVER mask to 0
  3541.  
  3542. ;; PR-ALL-2
  3543. L0BA4:  RRA                     ; skip bit 1 of P_FLAG
  3544.         RRA                     ; bit 2 is INVERSE
  3545.         SBC     A,A             ; will be FF for INVERSE 1 else zero
  3546.         LD      C,A             ; transfer INVERSE mask to C
  3547.         LD      A,$08           ; prepare to count 8 bytes
  3548.         AND     A               ; clear carry to signal screen
  3549.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3550.         JR      Z,L0BB6         ; to PR-ALL-3 if screen
  3551.  
  3552.         SET     1,(IY+$30)      ; update FLAGS2  - signal printer buffer has
  3553.                                 ; been used.
  3554.         SCF                     ; set carry flag to signal printer.
  3555.  
  3556. ;; PR-ALL-3
  3557. L0BB6:  EX      DE,HL           ; now HL=source, DE=destination
  3558.  
  3559. ;; PR-ALL-4
  3560. L0BB7:  EX      AF,AF'          ; save printer/screen flag
  3561.        LD      A,(DE)          ; fetch existing destination byte
  3562.        AND     B               ; consider OVER
  3563.        XOR     (HL)            ; now XOR with source
  3564.        XOR     C               ; now with INVERSE MASK
  3565.        LD      (DE),A          ; update screen/printer
  3566.        EX      AF,AF'          ; restore flag
  3567.         JR      C,L0BD3         ; to PR-ALL-6 - printer address update
  3568.  
  3569.         INC     D               ; gives next pixel line down screen
  3570.  
  3571. ;; PR-ALL-5
  3572. L0BC1:  INC     HL              ; address next character byte
  3573.         DEC     A               ; the byte count is decremented
  3574.         JR      NZ,L0BB7        ; back to PR-ALL-4 for all 8 bytes
  3575.  
  3576.         EX      DE,HL           ; destination to HL
  3577.         DEC     H               ; bring back to last updated screen position
  3578.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3579.         CALL    Z,L0BDB         ; if not, call routine PO-ATTR to update
  3580.                                 ; corresponding colour attribute.
  3581.         POP     HL              ; restore original screen/printer position
  3582.         POP     BC              ; and line column
  3583.         DEC     C               ; move column to right
  3584.         INC     HL              ; increase screen/printer position
  3585.         RET                     ; return and continue into PO-STORE
  3586.                                 ; within PO-ABLE
  3587.  
  3588. ; ---
  3589.  
  3590. ; This branch is used to update the printer position by 32 places
  3591. ; Note. The high byte of the address D remains constant (which it should).
  3592.  
  3593. ;; PR-ALL-6
  3594. L0BD3:  EX      AF,AF'          ; save the flag
  3595.        LD      A,$20           ; load A with 32 decimal
  3596.        ADD     A,E             ; add this to E
  3597.        LD      E,A             ; and store result in E
  3598.        EX      AF,AF'          ; fetch the flag
  3599.         JR      L0BC1           ; back to PR-ALL-5
  3600.  
  3601. ; -------------
  3602. ; Set attribute
  3603. ; -------------
  3604. ; This routine is entered with the HL register holding the last screen
  3605. ; address to be updated by PRINT or PLOT.
  3606. ; The Spectrum screen arrangement leads to the L register holding
  3607. ; the correct value for the attribute file and it is only necessary
  3608. ; to manipulate H to form the correct colour attribute address.
  3609.  
  3610. ;; PO-ATTR
  3611. L0BDB:  LD       A,H            ; fetch high byte $40 - $57
  3612.         RRCA                    ; shift
  3613.         RRCA                    ; bits 3 and 4
  3614.         RRCA                    ; to right.
  3615.         AND     $03             ; range is now 0 - 2
  3616.         OR      $58             ; form correct high byte for third of screen
  3617.         LD      H,A             ; HL is now correct
  3618.         LD      DE,($5C8F)      ; make D hold ATTR_T, E hold MASK-T
  3619.         LD      A,(HL)          ; fetch existing attribute
  3620.         XOR     E               ; apply masks
  3621.         AND     D               ;
  3622.         XOR     E               ;
  3623.         BIT     6,(IY+$57)      ; test P_FLAG  - is this PAPER 9 ??
  3624.         JR      Z,L0BFA         ; skip to PO-ATTR-1 if not.
  3625.  
  3626.         AND     $C7             ; set paper
  3627.         BIT     2,A             ; to contrast with ink
  3628.         JR      NZ,L0BFA        ; skip to PO-ATTR-1
  3629.  
  3630.         XOR     $38             ;
  3631.  
  3632. ;; PO-ATTR-1
  3633. L0BFA:  BIT     4,(IY+$57)      ; test P_FLAG  - Is this INK 9 ??
  3634.         JR      Z,L0C08         ; skip to PO-ATTR-2 if not
  3635.  
  3636.         AND     $F8             ; make ink
  3637.         BIT     5,A             ; contrast with paper.
  3638.         JR      NZ,L0C08        ; to PO-ATTR-2
  3639.  
  3640.         XOR     $07             ;
  3641.  
  3642. ;; PO-ATTR-2
  3643. L0C08:  LD      (HL),A          ; save the new attribute.
  3644.         RET                     ; return.
  3645.  
  3646. ; ----------------
  3647. ; Message printing
  3648. ; ----------------
  3649. ; This entry point is used to print tape, boot-up, scroll? and error messages
  3650. ; On entry the DE register points to an initial step-over byte or
  3651. ; the inverted end-marker of the previous entry in the table.
  3652. ; A contains the message number, often zero to print first message.
  3653. ; (HL has nothing important usually P_FLAG)
  3654.  
  3655. ;; PO-MSG
  3656. L0C0A:  PUSH    HL              ; put hi-byte zero on stack to suppress
  3657.         LD      H,$00           ; trailing spaces
  3658.         EX      (SP),HL         ; ld h,0; push hl would have done ?.
  3659.         JR      L0C14           ; forward to PO-TABLE.
  3660.  
  3661. ; ---
  3662.  
  3663. ; This entry point prints the BASIC keywords, '<>' etc. from alt set
  3664.  
  3665. ;; PO-TOKENS
  3666. L0C10:  LD      DE,L0095        ; address: TKN-TABLE
  3667.         PUSH    AF              ; save the token number to control
  3668.                                 ; trailing spaces - see later *
  3669.  
  3670. ;; PO-TABLE
  3671. L0C14:  CALL    L0C41           ; routine PO-SEARCH will set carry for
  3672.                                 ; all messages and function words.
  3673. L0C17:  JR      C,L0C22         ; forward to PO-EACH if not a command,
  3674.                                 ; '<>' etc.
  3675.  
  3676.         LD      A,$20           ; prepare leading space
  3677.         BIT     0,(IY+$01)      ; test FLAGS  - leading space if not set
  3678.         CALL    Z,L0C3B         ; routine PO-SAVE to print a space
  3679.                                 ; without disturbing registers
  3680.  
  3681. ;; PO-EACH
  3682. L0C22:  LD      A,(DE)          ; fetch character
  3683.         AND     $7F             ; remove any inverted bit
  3684.         CALL    L0C3B           ; routine PO-SAVE to print using alternate
  3685.                                 ; set of registers.
  3686.         LD      A,(DE)          ; re-fetch character.
  3687.         INC     DE              ; address next
  3688.         ADD     A,A             ; was character inverted ?
  3689.                                 ; (this also doubles character)
  3690.         JR      NC,L0C22        ; back to PO-EACH if not
  3691.  
  3692.         POP     DE              ; * re-fetch trailing space flag to D (was A)
  3693.         CP      $48             ; was last character '$' ($24*2)
  3694.         JR      Z,L0C35         ; forward to PO-TR-SP to consider trailing
  3695.                                 ; space if so.
  3696.  
  3697.         CP      $82             ; was it < 'A' i.e. '#','>','=' from tokens
  3698.                                 ; or ' ','.' (from tape) or '?' from scroll
  3699.         RET     C               ; no trailing space
  3700.  
  3701. ;; PO-TR-SP
  3702. L0C35:  LD      A,D             ; the trailing space flag (zero if an error msg)
  3703.         CP      $03             ; test against RND, INKEY$ and PI
  3704.                                 ; which have no parameters and
  3705.         RET     C               ; therefore no trailing space so return.
  3706.  
  3707.         LD      A,$20           ; else continue and print a trailing space.
  3708.  
  3709. ; -------------------------
  3710. ; Handle recursive printing
  3711. ; -------------------------
  3712. ; This routine which is part of PRINT-OUT allows RST $10 to be
  3713. ; used recursively to print tokens and the spaces associated with them.
  3714.  
  3715. ;; PO-SAVE
  3716. L0C3B:  PUSH    DE              ; save DE as CALL-SUB doesn't.
  3717.         EXX                     ; switch in main set
  3718.  
  3719.         RST     10H             ; PRINT-A prints using this alternate set.
  3720.  
  3721.         EXX                     ; back to this alternate set.
  3722.         POP     DE              ; restore initial DE.
  3723.         RET                     ; return.
  3724.  
  3725. ; ------------
  3726. ; Table search
  3727. ; ------------
  3728. ; This subroutine searches a message or the token table for the
  3729. ; message number held in A. DE holds the address of the table.
  3730.  
  3731. ;; PO-SEARCH
  3732. L0C41:  PUSH    AF              ; save the message/token number
  3733.         EX      DE,HL           ; transfer DE to HL
  3734.         INC     A               ; adjust for initial step-over byte
  3735.  
  3736. ;; PO-STEP
  3737. L0C44:  BIT     7,(HL)          ; is character inverted ?
  3738.         INC     HL              ; address next
  3739.         JR      Z,L0C44         ; back to PO-STEP if not inverted.
  3740.  
  3741.         DEC     A               ; decrease counter
  3742.         JR      NZ,L0C44        ; back to PO-STEP if not zero
  3743.  
  3744.         EX      DE,HL           ; transfer address to DE
  3745.         POP     AF              ; restore message/token number
  3746.         CP      $20             ; return with carry set
  3747.         RET     C               ; for all messages and function tokens
  3748.  
  3749.         LD      A,(DE)          ; test first character of token
  3750.         SUB     $41             ; and return with carry set
  3751.         RET                     ; if it is less that 'A'
  3752.                                 ; i.e. '<>', '<=', '>='
  3753.  
  3754. ; ---------------
  3755. ; Test for scroll
  3756. ; ---------------
  3757. ; This test routine is called when printing carriage return, when considering
  3758. ; PRINT AT and from the general PRINT ALL characters routine to test if
  3759. ; scrolling is required, prompting the user if necessary.
  3760. ; This is therefore using the alternate set.
  3761. ; The B register holds the current line.
  3762.  
  3763. ;; PO-SCR
  3764. L0C55:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3765.         RET     NZ              ; return immediately if so.
  3766.  
  3767.         LD      DE,L0DD9        ; set DE to address: CL-SET
  3768.         PUSH    DE              ; and push for return address.
  3769.         LD      A,B             ; transfer the line to A.
  3770.         BIT     0,(IY+$02)      ; test TV_FLAG  - Lower screen in use ?
  3771.         JP      NZ,L0D02        ; jump forward to PO-SCR-4 if so.
  3772.  
  3773.         CP      (IY+$31)        ; greater than DF_SZ display file size ?
  3774.         JR      C,L0C86         ; forward to REPORT-5 if less.
  3775.                                 ; 'Out of screen'
  3776.  
  3777.         RET     NZ              ; return (via CL-SET) if greater
  3778.  
  3779.         BIT     4,(IY+$02)      ; test TV_FLAG  - Automatic listing ?
  3780.         JR      Z,L0C88         ; forward to PO-SCR-2 if not.
  3781.  
  3782.         LD      E,(IY+$2D)      ; fetch BREG - the count of scroll lines to E.
  3783.         DEC     E               ; decrease and jump
  3784.         JR      Z,L0CD2         ; to PO-SCR-3 if zero and scrolling required.
  3785.  
  3786.         LD      A,$00           ; explicit - select channel zero.
  3787.         CALL    L1601           ; routine CHAN-OPEN opens it.
  3788.  
  3789.         LD      SP,($5C3F)      ; set stack pointer to LIST_SP
  3790.  
  3791.         RES     4,(IY+$02)      ; reset TV_FLAG  - signal auto listing finished.
  3792.         RET                     ; return ignoring pushed value, CL-SET
  3793.                                 ; to MAIN or EDITOR without updating
  3794.                                 ; print position                         ->
  3795.  
  3796. ; ---
  3797.  
  3798.  
  3799. ;; REPORT-5
  3800. L0C86:  RST     08H             ; ERROR-1
  3801.         DB    $04             ; Error Report: Out of screen
  3802.  
  3803. ; continue here if not an automatic listing.
  3804.  
  3805. ;; PO-SCR-2
  3806. L0C88:  DEC     (IY+$52)        ; decrease SCR_CT
  3807.         JR      NZ,L0CD2        ; forward to PO-SCR-3 to scroll display if
  3808.                                 ; result not zero.
  3809.  
  3810. ; now produce prompt.
  3811.  
  3812.         LD      A,$18           ; reset
  3813.         SUB     B               ; the
  3814.         LD      ($5C8C),A       ; SCR_CT scroll count
  3815.         LD      HL,($5C8F)      ; L=ATTR_T, H=MASK_T
  3816.         PUSH    HL              ; save on stack
  3817.         LD      A,($5C91)       ; P_FLAG
  3818.         PUSH    AF              ; save on stack to prevent lower screen
  3819.                                 ; attributes (BORDCR etc.) being applied.
  3820.         LD      A,$FD           ; select system channel 'K'
  3821.         CALL    L1601           ; routine CHAN-OPEN opens it
  3822.         XOR     A               ; clear to address message directly
  3823.         LD      DE,L0CF8        ; make DE address: scrl-mssg
  3824.         CALL    L0C0A           ; routine PO-MSG prints to lower screen
  3825.         SET     5,(IY+$02)      ; set TV_FLAG  - signal lower screen requires
  3826.                                 ; clearing
  3827.         LD      HL,$5C3B        ; make HL address FLAGS
  3828.         SET     3,(HL)          ; signal 'L' mode.
  3829.         RES     5,(HL)          ; signal 'no new key'.
  3830.         EXX                     ; switch to main set.
  3831.                                 ; as calling chr input from alternative set.
  3832.         CALL    L15D4           ; routine WAIT-KEY waits for new key
  3833.                                 ; Note. this is the right routine but the
  3834.                                 ; stream in use is unsatisfactory. From the
  3835.                                 ; choices available, it is however the best.
  3836.  
  3837.         EXX                     ; switch back to alternate set.
  3838.         CP      $20             ; space is considered as BREAK
  3839.         JR      Z,L0D00         ; forward to REPORT-D if so
  3840.                                 ; 'BREAK - CONT repeats'
  3841.  
  3842.         CP      $E2             ; is character 'STOP' ?
  3843.         JR      Z,L0D00         ; forward to REPORT-D if so
  3844.  
  3845.         OR      $20             ; convert to lower-case
  3846.         CP      $6E             ; is character 'n' ?
  3847.         JR      Z,L0D00         ; forward to REPORT-D if so else scroll.
  3848.  
  3849.         LD      A,$FE           ; select system channel 'S'
  3850.         CALL    L1601           ; routine CHAN-OPEN
  3851.         POP     AF              ; restore original P_FLAG
  3852.         LD      ($5C91),A       ; and save in P_FLAG.
  3853.         POP     HL              ; restore original ATTR_T, MASK_T
  3854.         LD      ($5C8F),HL      ; and reset ATTR_T, MASK-T as 'scroll?' has
  3855.                                 ; been printed.
  3856.  
  3857. ;; PO-SCR-3
  3858. L0CD2:  CALL    L0DFE           ; routine CL-SC-ALL to scroll whole display
  3859.         LD      B,(IY+$31)      ; fetch DF_SZ to B
  3860.         INC     B               ; increase to address last line of display
  3861.         LD      C,$21           ; set C to $21 (was $21 from above routine)
  3862.         PUSH    BC              ; save the line and column in BC.
  3863.  
  3864.         CALL    L0E9B           ; routine CL-ADDR finds display address.
  3865.  
  3866.         LD      A,H             ; now find the corresponding attribute byte
  3867.         RRCA                    ; (this code sequence is used twice
  3868.         RRCA                    ; elsewhere and is a candidate for
  3869.         RRCA                    ; a subroutine.)
  3870.         AND     $03             ;
  3871.         OR      $58             ;
  3872.         LD      H,A             ;
  3873.  
  3874.         LD      DE,$5AE0        ; start of last 'line' of attribute area
  3875.         LD      A,(DE)          ; get attribute for last line
  3876.         LD      C,(HL)          ; transfer to base line of upper part
  3877.         LD      B,$20           ; there are thirty two bytes
  3878.         EX      DE,HL           ; swap the pointers.
  3879.  
  3880. ;; PO-SCR-3A
  3881. L0CF0:  LD      (DE),A          ; transfer
  3882.         LD      (HL),C          ; attributes.
  3883.         INC     DE              ; address next.
  3884.         INC     HL              ; address next.
  3885.         DJNZ    L0CF0           ; loop back to PO-SCR-3A for all adjacent
  3886.                                 ; attribute lines.
  3887.  
  3888.         POP     BC              ; restore the line/column.
  3889.         RET                     ; return via CL-SET (was pushed on stack).
  3890.  
  3891. ; ---
  3892.  
  3893. ; The message 'scroll?' appears here with last byte inverted.
  3894.  
  3895. ;; scrl-mssg
  3896. L0CF8           DB $80             ; initial step-over byte.
  3897.                 DC "scroll?"    ;DEFM    "scroll"
  3898.                                 ;DB    '?'+$80
  3899.  
  3900. ;; REPORT-D
  3901. L0D00:  RST     08H             ; ERROR-1
  3902.         DB    $0C             ; Error Report: BREAK - CONT repeats
  3903.  
  3904. ; continue here if using lower display - A holds line number.
  3905.  
  3906. ;; PO-SCR-4
  3907. L0D02:  CP      $02             ; is line number less than 2 ?
  3908.         JR      C,L0C86         ; to REPORT-5 if so
  3909.                                 ; 'Out of Screen'.
  3910.  
  3911.         ADD     A,(IY+$31)      ; add DF_SZ
  3912.         SUB     $19             ;
  3913.         RET     NC              ; return if scrolling unnecessary
  3914.  
  3915.         NEG                     ; Negate to give number of scrolls required.
  3916.         PUSH    BC              ; save line/column
  3917.         LD      B,A             ; count to B
  3918.         LD      HL,($5C8F)      ; fetch current ATTR_T, MASK_T to HL.
  3919.         PUSH    HL              ; and save
  3920.         LD      HL,($5C91)      ; fetch P_FLAG
  3921.         PUSH    HL              ; and save.
  3922.                                 ; to prevent corruption by input AT
  3923.  
  3924.         CALL    L0D4D           ; routine TEMPS sets to BORDCR etc
  3925.         LD      A,B             ; transfer scroll number to A.
  3926.  
  3927. ;; PO-SCR-4A
  3928. L0D1C:  PUSH    AF              ; save scroll number.
  3929.         LD      HL,$5C6B        ; address DF_SZ
  3930.         LD      B,(HL)          ; fetch old value
  3931.         LD      A,B             ; transfer to A
  3932.         INC     A               ; and increment
  3933.         LD      (HL),A          ; then put back.
  3934.         LD      HL,$5C89        ; address S_POSN_hi - line
  3935.         CP      (HL)            ; compare
  3936.         JR      C,L0D2D         ; forward to PO-SCR-4B if scrolling required
  3937.  
  3938.         INC     (HL)            ; else increment S_POSN_hi
  3939.         LD      B,$18           ; set count to whole display ??
  3940.                                 ; Note. should be $17 and the top line
  3941.                                 ; will be scrolled into the ROM which
  3942.                                 ; is harmless on the standard set up.
  3943.  
  3944. ;; PO-SCR-4B
  3945. L0D2D:  CALL    L0E00           ; routine CL-SCROLL scrolls B lines
  3946.         POP     AF              ; restore scroll counter.
  3947.         DEC     A               ; decrease
  3948.         JR      NZ,L0D1C        ; back to to PO-SCR-4A until done
  3949.  
  3950.         POP     HL              ; restore original P_FLAG.
  3951.         LD      (IY+$57),L      ; and overwrite system variable P_FLAG.
  3952.  
  3953.         POP     HL              ; restore original ATTR_T/MASK_T.
  3954.         LD      ($5C8F),HL      ; and update system variables.
  3955.  
  3956.         LD      BC,($5C88)      ; fetch S_POSN to BC.
  3957.         RES     0,(IY+$02)      ; signal to TV_FLAG  - main screen in use.
  3958.         CALL    L0DD9           ; call routine CL-SET for upper display.
  3959.  
  3960.         SET     0,(IY+$02)      ; signal to TV_FLAG  - lower screen in use.
  3961.         POP     BC              ; restore line/column
  3962.         RET                     ; return via CL-SET for lower display.
  3963.  
  3964. ; ----------------------
  3965. ; Temporary colour items
  3966. ; ----------------------
  3967. ; This subroutine is called 11 times to copy the permanent colour items
  3968. ; to the temporary ones.
  3969.  
  3970. ;; TEMPS
  3971. L0D4D:  XOR     A               ; clear the accumulator
  3972.         LD      HL,($5C8D)      ; fetch L=ATTR_P and H=MASK_P
  3973.         BIT     0,(IY+$02)      ; test TV_FLAG  - is lower screen in use ?
  3974.         JR      Z,L0D5B         ; skip to TEMPS-1 if not
  3975.  
  3976.         LD      H,A             ; set H, MASK P, to 00000000.
  3977.         LD      L,(IY+$0E)      ; fetch BORDCR to L which is used for lower
  3978.                                 ; screen.
  3979.  
  3980. ;; TEMPS-1
  3981. L0D5B:  LD      ($5C8F),HL      ; transfer values to ATTR_T and MASK_T
  3982.  
  3983. ; for the print flag the permanent values are odd bits, temporary even bits.
  3984.  
  3985.         LD      HL,$5C91        ; address P_FLAG.
  3986.         JR      NZ,L0D65        ; skip to TEMPS-2 if lower screen using A=0.
  3987.  
  3988.         LD      A,(HL)          ; else pick up flag bits.
  3989.         RRCA                    ; rotate permanent bits to temporary bits.
  3990.  
  3991. ;; TEMPS-2
  3992. L0D65:  XOR     (HL)            ;
  3993.         AND     $55             ; BIN 01010101
  3994.         XOR     (HL)            ; permanent now as original
  3995.         LD      (HL),A          ; apply permanent bits to temporary bits.
  3996.         RET                     ; and return.
  3997.  
  3998. ; ------------------
  3999. ; Handle CLS command
  4000. ; ------------------
  4001. ; clears the display.
  4002. ; if it's difficult to write it should be difficult to read.
  4003.  
  4004. ;; CLS
  4005. L0D6B:  CALL    L0DAF           ; routine CL-ALL  clears display and
  4006.                                 ; resets attributes to permanent.
  4007.                                 ; re-attaches it to this computer.
  4008.  
  4009. ; this routine called from INPUT, **
  4010.  
  4011. ;; CLS-LOWER
  4012. L0D6E:  LD      HL,$5C3C        ; address System Variable TV_FLAG.
  4013.         RES     5,(HL)          ; TV_FLAG - signal do not clear lower screen.
  4014.         SET     0,(HL)          ; TV_FLAG - signal lower screen in use.
  4015.         CALL    L0D4D           ; routine TEMPS picks up temporary colours.
  4016.         LD      B,(IY+$31)      ; fetch lower screen DF_SZ
  4017.         CALL    L0E44           ; routine CL-LINE clears lower part
  4018.                                 ; and sets permanent attributes.
  4019.  
  4020.         LD      HL,$5AC0        ; fetch attribute address leftmost cell,
  4021.                                 ; second line up.
  4022.         LD      A,($5C8D)       ; fetch permanent attribute from ATTR_P.
  4023.         DEC     B               ; decrement lower screen display file size
  4024.         JR      L0D8E           ; forward to CLS-3 ->
  4025.  
  4026. ; ---
  4027.  
  4028. ;; CLS-1
  4029. L0D87:  LD      C,$20           ; set counter to 32 characters per line
  4030.  
  4031. ;; CLS-2
  4032. L0D89:  DEC     HL              ; decrease attribute address.
  4033.         LD      (HL),A          ; and place attributes in next line up.
  4034.         DEC     C               ; decrease 32 counter.
  4035.         JR      NZ,L0D89        ; loop back to CLS-2 until all 32 done.
  4036.  
  4037. ;; CLS-3
  4038. L0D8E:  DJNZ    L0D87           ; decrease B counter and back to CLS-1
  4039.                                 ; if not zero.
  4040.  
  4041.         LD      (IY+$31),$02    ; set DF_SZ lower screen to 2
  4042.  
  4043. ; This entry point is called from CL-ALL below to
  4044. ; reset the system channel input and output addresses to normal.
  4045.  
  4046. ;; CL-CHAN
  4047. L0D94:  LD      A,$FD           ; select system channel 'K'
  4048.         CALL    L1601           ; routine CHAN-OPEN opens it.
  4049.         LD      HL,($5C51)      ; fetch CURCHL to HL to address current channel
  4050.         LD      DE,L09F4        ; set address to PRINT-OUT for first pass.
  4051.         AND     A               ; clear carry for first pass.
  4052.  
  4053. ;; CL-CHAN-A
  4054. L0DA0:  LD      (HL),E          ; insert output address first pass.
  4055.         INC     HL              ; or input address on second pass.
  4056.         LD      (HL),D          ;
  4057.         INC     HL              ;
  4058.         LD      DE,L10A8        ; fetch address KEY-INPUT for second pass
  4059.         CCF                     ; complement carry flag - will set on pass 1.
  4060.  
  4061.         JR      C,L0DA0         ; back to CL-CHAN-A if first pass else done.
  4062.  
  4063.         LD      BC,$1721        ; line 23 for lower screen
  4064.         JR      L0DD9           ; exit via CL-SET to set column
  4065.                                 ; for lower display
  4066.  
  4067. ; ---------------------------
  4068. ; Clearing whole display area
  4069. ; ---------------------------
  4070. ; This subroutine called from CLS, AUTO-LIST and MAIN-3
  4071. ; clears 24 lines of the display and resets the relevant system variables
  4072. ; and system channels.
  4073.  
  4074. ;; CL-ALL
  4075. L0DAF:  LD      HL,$0000        ; initialize plot coordinates.
  4076.         LD      ($5C7D),HL      ; set COORDS to 0,0.
  4077.         RES     0,(IY+$30)      ; update FLAGS2  - signal main screen is clear.
  4078.  
  4079.         CALL    L0D94           ; routine CL-CHAN makes channel 'K' 'normal'.
  4080.  
  4081.         LD      A,$FE           ; select system channel 'S'
  4082.         CALL    L1601           ; routine CHAN-OPEN opens it
  4083.         CALL    L0D4D           ; routine TEMPS picks up permanent values.
  4084.         LD      B,$18           ; There are 24 lines.
  4085.         CALL    L0E44           ; routine CL-LINE clears 24 text lines
  4086.                                 ; (and sets BC to $1821)
  4087.  
  4088.         LD      HL,($5C51)      ; fetch CURCHL make HL address current
  4089.                                 ; channel 'S'
  4090.         LD      DE,L09F4        ; address: PRINT-OUT
  4091.         LD      (HL),E          ; is made
  4092.         INC     HL              ; the normal
  4093.         LD      (HL),D          ; output address.
  4094.  
  4095.         LD      (IY+$52),$01    ; set SCR_CT - scroll count is set to default.
  4096.                                 ; Note. BC already contains $1821.
  4097.         LD      BC,$1821        ; reset column and line to 0,0
  4098.                                 ; and continue into CL-SET, below, exiting
  4099.                                 ; via PO-STORE (for upper screen).
  4100.  
  4101. ; ---------------------------
  4102. ; Set line and column numbers
  4103. ; ---------------------------
  4104. ; This important subroutine is used to calculate the character output
  4105. ; address for screens or printer based on the line/column for screens
  4106. ; or the column for printer.
  4107.  
  4108. ;; CL-SET
  4109. L0DD9:  LD      HL,$5B00        ; the base address of printer buffer
  4110.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  4111.         JR      NZ,L0DF4        ; forward to CL-SET-2 if so.
  4112.  
  4113.         LD      A,B             ; transfer line to A.
  4114.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4115.         JR      Z,L0DEE         ; skip to CL-SET-1 if handling upper part
  4116.  
  4117.         ADD     A,(IY+$31)      ; add DF_SZ for lower screen
  4118.         SUB     $18             ; and adjust.
  4119.  
  4120. ;; CL-SET-1
  4121. L0DEE:  PUSH    BC              ; save the line/column.
  4122.         LD      B,A             ; transfer line to B
  4123.                                 ; (adjusted if lower screen)
  4124.  
  4125.         CALL    L0E9B           ; routine CL-ADDR calculates address at left
  4126.                                 ; of screen.
  4127.         POP     BC              ; restore the line/column.
  4128.  
  4129. ;; CL-SET-2
  4130. L0DF4:  LD      A,$21           ; the column $1-$21 is reversed
  4131.         SUB     C               ; to range $00 - $20
  4132.         LD      E,A             ; now transfer to DE
  4133.         LD      D,$00           ; prepare for addition
  4134.         ADD     HL,DE           ; and add to base address
  4135.         JP      L0ADC           ; exit via PO-STORE to update relevant
  4136.                                 ; system variables.
  4137. ; ----------------
  4138. ; Handle scrolling
  4139. ; ----------------
  4140. ; The routine CL-SC-ALL is called once from PO to scroll all the display
  4141. ; and from the routine CL-SCROLL, once, to scroll part of the display.
  4142.  
  4143. ;; CL-SC-ALL
  4144. L0DFE:  LD      B,$17           ; scroll 23 lines, after 'scroll?'.
  4145.  
  4146. ;; CL-SCROLL
  4147. L0E00:  CALL    L0E9B           ; routine CL-ADDR gets screen address in HL.
  4148.         LD      C,$08           ; there are 8 pixel lines to scroll.
  4149.  
  4150. ;; CL-SCR-1
  4151. L0E05:  PUSH    BC              ; save counters.
  4152.         PUSH    HL              ; and initial address.
  4153.         LD      A,B             ; get line count.
  4154.         AND     $07             ; will set zero if all third to be scrolled.
  4155.         LD      A,B             ; re-fetch the line count.
  4156.         JR      NZ,L0E19        ; forward to CL-SCR-3 if partial scroll.
  4157.  
  4158. ; HL points to top line of third and must be copied to bottom of previous 3rd.
  4159. ; ( so HL = $4800 or $5000 ) ( but also sometimes $4000 )
  4160.  
  4161. ;; CL-SCR-2
  4162. L0E0D:  EX      DE,HL           ; copy HL to DE.
  4163.         LD      HL,$F8E0        ; subtract $08 from H and add $E0 to L -
  4164.         ADD     HL,DE           ; to make destination bottom line of previous
  4165.                                 ; third.
  4166.         EX      DE,HL           ; restore the source and destination.
  4167.         LD      BC,$0020        ; thirty-two bytes are to be copied.
  4168.         DEC     A               ; decrement the line count.
  4169.         LDIR                    ; copy a pixel line to previous third.
  4170.  
  4171. ;; CL-SCR-3
  4172. L0E19:  EX      DE,HL           ; save source in DE.
  4173.         LD      HL,$FFE0        ; load the value -32.
  4174.         ADD     HL,DE           ; add to form destination in HL.
  4175.         EX      DE,HL           ; switch source and destination
  4176.         LD      B,A             ; save the count in B.
  4177.         AND     $07             ; mask to find count applicable to current
  4178.         RRCA                    ; third and
  4179.         RRCA                    ; multiply by
  4180.         RRCA                    ; thirty two (same as 5 RLCAs)
  4181.  
  4182.         LD      C,A             ; transfer byte count to C ($E0 at most)
  4183.         LD      A,B             ; store line count to A
  4184.         LD      B,$00           ; make B zero
  4185.         LDIR                    ; copy bytes (BC=0, H incremented, L=0)
  4186.         LD      B,$07           ; set B to 7, C is zero.
  4187.         ADD     HL,BC           ; add 7 to H to address next third.
  4188.         AND     $F8             ; has last third been done ?
  4189.         JR      NZ,L0E0D        ; back to CL-SCR-2 if not
  4190.  
  4191.         POP     HL              ; restore topmost address.
  4192.         INC     H               ; next pixel line down.
  4193.         POP     BC              ; restore counts.
  4194.         DEC     C               ; reduce pixel line count.
  4195.         JR      NZ,L0E05        ; back to CL-SCR-1 if all eight not done.
  4196.  
  4197.         CALL    L0E88           ; routine CL-ATTR gets address in attributes
  4198.                                 ; from current 'ninth line', count in BC.
  4199.         LD      HL,$FFE0        ; set HL to the 16-bit value -32.
  4200.         ADD     HL,DE           ; and add to form destination address.
  4201.         EX      DE,HL           ; swap source and destination addresses.
  4202.         LDIR                    ; copy bytes scrolling the linear attributes.
  4203.         LD      B,$01           ; continue to clear the bottom line.
  4204.  
  4205. ; ---------------------------
  4206. ; Clear text lines of display
  4207. ; ---------------------------
  4208. ; This subroutine, called from CL-ALL, CLS-LOWER and AUTO-LIST and above,
  4209. ; clears text lines at bottom of display.
  4210. ; The B register holds on entry the number of lines to be cleared 1-24.
  4211.  
  4212. ;; CL-LINE
  4213. L0E44:  PUSH    BC              ; save line count
  4214.         CALL    L0E9B           ; routine CL-ADDR gets top address
  4215.         LD      C,$08           ; there are eight screen lines to a text line.
  4216.  
  4217. ;; CL-LINE-1
  4218. L0E4A:  PUSH    BC              ; save pixel line count
  4219.         PUSH    HL              ; and save the address
  4220.         LD      A,B             ; transfer the line to A (1-24).
  4221.  
  4222. ;; CL-LINE-2
  4223. L0E4D:  AND     $07             ; mask 0-7 to consider thirds at a time
  4224.         RRCA                    ; multiply
  4225.         RRCA                    ; by 32  (same as five RLCA instructions)
  4226.         RRCA                    ; now 32 - 256(0)
  4227.         LD      C,A             ; store result in C
  4228.         LD      A,B             ; save line in A (1-24)
  4229.         LD      B,$00           ; set high byte to 0, prepare for ldir.
  4230.         DEC     C               ; decrement count 31-255.
  4231.         LD      D,H             ; copy HL
  4232.         LD      E,L             ; to DE.
  4233.         LD      (HL),$00        ; blank the first byte.
  4234.         INC     DE              ; make DE point to next byte.
  4235.         LDIR                    ; ldir will clear lines.
  4236.         LD      DE,$0701        ; now address next third adjusting
  4237.         ADD     HL,DE           ; register E to address left hand side
  4238.         DEC     A               ; decrease the line count.
  4239.         AND     $F8             ; will be 16, 8 or 0  (AND $18 will do).
  4240.         LD      B,A             ; transfer count to B.
  4241.         JR      NZ,L0E4D        ; back to CL-LINE-2 if 16 or 8 to do
  4242.                                 ; the next third.
  4243.  
  4244.         POP     HL              ; restore start address.
  4245.         INC     H               ; address next line down.
  4246.         POP     BC              ; fetch counts.
  4247.         DEC     C               ; decrement pixel line count
  4248.         JR      NZ,L0E4A        ; back to CL-LINE-1 till all done.
  4249.  
  4250.         CALL    L0E88           ; routine CL-ATTR gets attribute address
  4251.                                 ; in DE and B * 32 in BC.
  4252.         LD      H,D             ; transfer the address
  4253.         LD      L,E             ; to HL.
  4254.  
  4255.         INC     DE              ; make DE point to next location.
  4256.  
  4257.         LD      A,($5C8D)       ; fetch ATTR_P - permanent attributes
  4258.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4259.         JR      Z,L0E80         ; skip to CL-LINE-3 if not.
  4260.  
  4261.         LD      A,($5C48)       ; else lower screen uses BORDCR as attribute.
  4262.  
  4263. ;; CL-LINE-3
  4264. L0E80:  LD      (HL),A          ; put attribute in first byte.
  4265.         DEC     BC              ; decrement the counter.
  4266.         LDIR                    ; copy bytes to set all attributes.
  4267.         POP     BC              ; restore the line $01-$24.
  4268.         LD      C,$21           ; make column $21. (No use is made of this)
  4269.         RET                     ; return to the calling routine.
  4270.  
  4271. ; ------------------
  4272. ; Attribute handling
  4273. ; ------------------
  4274. ; This subroutine is called from CL-LINE or CL-SCROLL with the HL register
  4275. ; pointing to the 'ninth' line and H needs to be decremented before or after
  4276. ; the division. Had it been done first then either present code or that used
  4277. ; at the start of PO-ATTR could have been used.
  4278. ; The Spectrum screen arrangement leads to the L register holding already
  4279. ; the correct value for the attribute file and it is only necessary
  4280. ; to manipulate H to form the correct colour attribute address.
  4281.  
  4282. ;; CL-ATTR
  4283. L0E88:  LD      A,H             ; fetch H to A - $48, $50, or $58.
  4284.         RRCA                    ; divide by
  4285.         RRCA                    ; eight.
  4286.         RRCA                    ; $09, $0A or $0B.
  4287.         DEC     A               ; $08, $09 or $0A.
  4288.         OR      $50             ; $58, $59 or $5A.
  4289.         LD      H,A             ; save high byte of attributes.
  4290.  
  4291.         EX      DE,HL           ; transfer attribute address to DE
  4292.         LD      H,C             ; set H to zero - from last LDIR.
  4293.         LD      L,B             ; load L with the line from B.
  4294.         ADD     HL,HL           ; multiply
  4295.         ADD     HL,HL           ; by
  4296.         ADD     HL,HL           ; thirty two
  4297.         ADD     HL,HL           ; to give count of attribute
  4298.         ADD     HL,HL           ; cells to end of display.
  4299.  
  4300.         LD      B,H             ; transfer result
  4301.         LD      C,L             ; to register BC.
  4302.  
  4303.         RET                     ; and return.
  4304.  
  4305. ; -------------------------------
  4306. ; Handle display with line number
  4307. ; -------------------------------
  4308. ; This subroutine is called from four places to calculate the address
  4309. ; of the start of a screen character line which is supplied in B.
  4310.  
  4311. ;; CL-ADDR
  4312. L0E9B:  LD      A,$18           ; reverse the line number
  4313.         SUB     B               ; to range $00 - $17.
  4314.         LD      D,A             ; save line in D for later.
  4315.         RRCA                    ; multiply
  4316.         RRCA                    ; by
  4317.         RRCA                    ; thirty-two.
  4318.  
  4319.         AND     $E0             ; mask off low bits to make
  4320.         LD      L,A             ; L a multiple of 32.
  4321.  
  4322.         LD      A,D             ; bring back the line to A.
  4323.  
  4324.         AND     $18             ; now $00, $08 or $10.
  4325.  
  4326.         OR      $40             ; add the base address of screen.
  4327.  
  4328.         LD      H,A             ; HL now has the correct address.
  4329.         RET                     ; return.
  4330.  
  4331. ; -------------------
  4332. ; Handle COPY command
  4333. ; -------------------
  4334. ; This command copies the top 176 lines to the ZX Printer
  4335. ; It is popular to call this from machine code at point
  4336. ; L0EAF with B holding 192 (and interrupts disabled) for a full-screen
  4337. ; copy. This particularly applies to 16K Spectrums as time-critical
  4338. ; machine code routines cannot be written in the first 16K of RAM as
  4339. ; it is shared with the ULA which has precedence over the Z80 chip.
  4340.  
  4341. ;; COPY
  4342.  
  4343. L0EAC:  DI                      ; disable interrupts as this is time-critical.
  4344. ;===============================
  4345.                 IF AY_PRINTER
  4346.                 RST 0X08
  4347.                 DB AY_PRN_SCR
  4348.                 ELSE
  4349.                 LD B,$B0                ; top 176 lines.
  4350.                 ENDIF
  4351. ;===============================
  4352.  
  4353. L0EAF           LD HL,$4000             ; address start of the display file.
  4354.  
  4355. ; now enter a loop to handle each pixel line.
  4356.  
  4357. ;; COPY-1
  4358. L0EB2:  PUSH    HL              ; save the screen address.
  4359.         PUSH    BC              ; and the line counter.
  4360.  
  4361.         CALL    L0EF4           ; routine COPY-LINE outputs one line.
  4362.  
  4363.         POP     BC              ; restore the line counter.
  4364.         POP     HL              ; and display address.
  4365.         INC     H               ; next line down screen within 'thirds'.
  4366.         LD      A,H             ; high byte to A.
  4367.         AND     $07             ; result will be zero if we have left third.
  4368.         JR      NZ,L0EC9        ; forward to COPY-2 if not to continue loop.
  4369.  
  4370.         LD      A,L             ; consider low byte first.
  4371.         ADD     A,$20           ; increase by 32 - sets carry if back to zero.
  4372.         LD      L,A             ; will be next group of 8.
  4373.         CCF                     ; complement - carry set if more lines in
  4374.                                 ; the previous third.
  4375.         SBC     A,A             ; will be FF, if more, else 00.
  4376.         AND     $F8             ; will be F8 (-8) or 00.
  4377.         ADD     A,H             ; that is subtract 8, if more to do in third.
  4378.         LD      H,A             ; and reset address.
  4379.  
  4380. ;; COPY-2
  4381. L0EC9:  DJNZ    L0EB2           ; back to COPY-1 for all lines.
  4382.  
  4383.         JR      L0EDA           ; forward to COPY-END to switch off the printer
  4384.                                 ; motor and enable interrupts.
  4385.                                 ; Note. Nothing else required.
  4386.  
  4387. ; ------------------------------
  4388. ; Pass printer buffer to printer
  4389. ; ------------------------------
  4390. ; This routine is used to copy 8 text lines from the printer buffer
  4391. ; to the ZX Printer. These text lines are mapped linearly so HL does
  4392. ; not need to be adjusted at the end of each line.
  4393.  
  4394. ;; COPY-BUFF
  4395. L0ECD:  DI                      ; disable interrupts
  4396.         LD      HL,$5B00        ; the base address of the Printer Buffer.
  4397.         LD      B,$08           ; set count to 8 lines of 32 bytes.
  4398.  
  4399. ;; COPY-3
  4400. L0ED3:  PUSH    BC              ; save counter.
  4401.         CALL    L0EF4           ; routine COPY-LINE outputs 32 bytes
  4402.         POP     BC              ; restore counter.
  4403.         DJNZ    L0ED3           ; loop back to COPY-3 for all 8 lines.
  4404.                                 ; then stop motor and clear buffer.
  4405.  
  4406. ; Note. the COPY command rejoins here, essentially to execute the next
  4407. ; three instructions.
  4408.  
  4409. ;; COPY-END
  4410. L0EDA           LD A,$04                ; output value 4 to port
  4411.                 OUT ($FB),A             ; to stop the slowed printer motor.
  4412. L0EDE           EI                      ; enable interrupts.
  4413.  
  4414. ; --------------------
  4415. ; Clear Printer Buffer
  4416. ; --------------------
  4417. ; This routine clears an arbitrary 256 bytes of memory.
  4418. ; Note. The routine seems designed to clear a buffer that follows the
  4419. ; system variables.
  4420. ; The routine should check a flag or HL address and simply return if COPY
  4421. ; is in use.
  4422. ; (T-ADDR-lo would work for the system but not if COPY called externally.)
  4423. ; As a consequence of this omission the buffer will needlessly
  4424. ; be cleared when COPY is used and the screen/printer position may be set to
  4425. ; the start of the buffer and the line number to 0 (B)
  4426. ; giving an 'Out of Screen' error.
  4427. ; There seems to have been an unsuccessful attempt to circumvent the use
  4428. ; of PR_CC_hi.
  4429.  
  4430. ;; CLEAR-PRB
  4431. L0EDF:  LD      HL,$5B00        ; the location of the buffer.
  4432.         LD      (IY+$46),L      ; update PR_CC_lo - set to zero - superfluous.
  4433.         XOR     A               ; clear the accumulator.
  4434.         LD      B,A             ; set count to 256 bytes.
  4435.  
  4436. ;; PRB-BYTES
  4437. L0EE7:  LD      (HL),A          ; set addressed location to zero.
  4438.         INC     HL              ; address next byte - Note. not INC L.
  4439.         DJNZ    L0EE7           ; back to PRB-BYTES. repeat for 256 bytes.
  4440.  
  4441.         RES     1,(IY+$30)      ; set FLAGS2 - signal printer buffer is clear.
  4442.         LD      C,$21           ; set the column position .
  4443.         JP      L0DD9           ; exit via CL-SET and then PO-STORE.
  4444.  
  4445. ; -----------------
  4446. ; Copy line routine
  4447. ; -----------------
  4448. ; This routine is called from COPY and COPY-BUFF to output a line of
  4449. ; 32 bytes to the ZX Printer.
  4450. ; Output to port $FB -
  4451. ; bit 7 set - activate stylus.
  4452. ; bit 7 low - deactivate stylus.
  4453. ; bit 2 set - stops printer.
  4454. ; bit 2 reset - starts printer
  4455. ; bit 1 set - slows printer.
  4456. ; bit 1 reset - normal speed.
  4457.  
  4458. ;; COPY-LINE
  4459. ;===============================
  4460. L0EF4           IF AY_PRINTER
  4461.                 LD A,(HL)
  4462.                 RST 0X08
  4463.                 DB AY_PRN_A_
  4464.                 RET
  4465.  
  4466.                 ELSE
  4467.                 LD A,B                  ; fetch the counter 1-8 or 1-176
  4468.                 CP $03                  ; is it 01 or 02 ?.
  4469.                 SBC A,A                 ; result is $FF if so else $00.
  4470.                 ENDIF
  4471. ;===============================
  4472.                 AND $02                 ; result is 02 now else 00.
  4473.                                         ; bit 1 set slows the printer.
  4474.                 OUT ($FB),A             ; slow the printer for the
  4475.                                         ; last two lines.
  4476.         LD      D,A             ; save the mask to control the printer later.
  4477.  
  4478. ;; COPY-L-1
  4479. L0EFD:  CALL    L1F54           ; call BREAK-KEY to read keyboard immediately.
  4480.         JR      C,L0F0C         ; forward to COPY-L-2 if 'break' not pressed.
  4481.  
  4482.         LD      A,$04           ; else stop the
  4483.         OUT     ($FB),A         ; printer motor.
  4484.         EI                      ; enable interrupts.
  4485.         CALL    L0EDF           ; call routine CLEAR-PRB.
  4486.                                 ; Note. should not be cleared if COPY in use.
  4487.  
  4488. ;; REPORT-Dc
  4489. L0F0A:  RST     08H             ; ERROR-1
  4490.         DB      $0C             ; Error Report: BREAK - CONT repeats
  4491.  
  4492. ;; COPY-L-2
  4493. L0F0C:  IN      A,($FB)         ; test now to see if
  4494.         ADD     A,A             ; a printer is attached.
  4495.         RET     M               ; return if not - but continue with parent
  4496.                                 ; command.
  4497.  
  4498.         JR      NC,L0EFD        ; back to COPY-L-1 if stylus of printer not
  4499.                                 ; in position.
  4500.  
  4501.         LD      C,$20           ; set count to 32 bytes.
  4502.  
  4503. ;; COPY-L-3
  4504. L0F14:  LD      E,(HL)          ; fetch a byte from line.
  4505.         INC     HL              ; address next location. Note. not INC L.
  4506.         LD      B,$08           ; count the bits.
  4507.  
  4508. ;; COPY-L-4
  4509. L0F18:  RL      D               ; prepare mask to receive bit.
  4510.         RL      E               ; rotate leftmost print bit to carry
  4511.         RR      D               ; and back to bit 7 of D restoring bit 1
  4512.  
  4513. ;; COPY-L-5
  4514. L0F1E:  IN      A,($FB)         ; read the port.
  4515.         RRA                     ; bit 0 to carry.
  4516.         JR      NC,L0F1E        ; back to COPY-L-5 if stylus not in position.
  4517.  
  4518.         LD      A,D             ; transfer command bits to A.
  4519.         OUT     ($FB),A         ; and output to port.
  4520.         DJNZ    L0F18           ; loop back to COPY-L-4 for all 8 bits.
  4521.  
  4522.         DEC     C               ; decrease the byte count.
  4523.         JR      NZ,L0F14        ; back to COPY-L-3 until 256 bits done.
  4524.  
  4525.         RET                     ; return to calling routine COPY/COPY-BUFF.
  4526.  
  4527. ; ----------------------------------
  4528. ; Editor routine for BASIC and INPUT
  4529. ; ----------------------------------
  4530. ; The editor is called to prepare or edit a BASIC line.
  4531. ; It is also called from INPUT to input a numeric or string expression.
  4532. ; The behaviour and options are quite different in the various modes
  4533. ; and distinguished by bit 5 of FLAGX.
  4534. ;
  4535. ; This is a compact and highly versatile routine.
  4536.  
  4537. ;; EDITOR
  4538. L0F2C:  LD      HL,($5C3D)      ; fetch ERR_SP
  4539.         PUSH    HL              ; save on stack
  4540.  
  4541. ;; ED-AGAIN
  4542. L0F30:  LD      HL,L107F        ; address: ED-ERROR
  4543.         PUSH    HL              ; save address on stack and
  4544.         LD      ($5C3D),SP      ; make ERR_SP point to it.
  4545.  
  4546. ; Note. While in editing/input mode should an error occur then RST 08 will
  4547. ; update X_PTR to the location reached by CH_ADD and jump to ED-ERROR
  4548. ; where the error will be cancelled and the loop begin again from ED-AGAIN
  4549. ; above. The position of the error will be apparent when the lower screen is
  4550. ; reprinted. If no error then the re-iteration is to ED-LOOP below when
  4551. ; input is arriving from the keyboard.
  4552.  
  4553. ;; ED-LOOP
  4554. L0F38:  CALL    L15D4           ; routine WAIT-KEY gets key possibly
  4555.                                 ; changing the mode.
  4556.         PUSH    AF              ; save key.
  4557.         LD      D,$00           ; and give a short click based
  4558.         LD      E,(IY-$01)      ; on PIP value for duration.
  4559.         LD      HL,$00C8        ; and pitch.
  4560.         CALL    L03B5           ; routine BEEPER gives click - effective
  4561.                                 ; with rubber keyboard.
  4562.         POP     AF              ; get saved key value.
  4563.         LD      HL,L0F38        ; address: ED-LOOP is loaded to HL.
  4564.         PUSH    HL              ; and pushed onto stack.
  4565.  
  4566. ; At this point there is a looping return address on the stack, an error
  4567. ; handler and an input stream set up to supply characters.
  4568. ; The character that has been received can now be processed.
  4569.  
  4570.         CP      $18             ; range 24 to 255 ?
  4571.         JR      NC,L0F81        ; forward to ADD-CHAR if so.
  4572.  
  4573.         CP      $07             ; lower than 7 ?
  4574.         JR      C,L0F81         ; forward to ADD-CHAR also.
  4575.                                 ; Note. This is a 'bug' and chr$ 6, the comma
  4576.                                 ; control character, should have had an
  4577.                                 ; entry in the ED-KEYS table.
  4578.                                 ; Steven Vickers, 1984, Pitman.
  4579.  
  4580.         CP      $10             ; less than 16 ?
  4581.         JR      C,L0F92         ; forward to ED-KEYS if editing control
  4582.                                 ; range 7 to 15 dealt with by a table
  4583.  
  4584.         LD      BC,$0002        ; prepare for ink/paper etc.
  4585.         LD      D,A             ; save character in D
  4586.         CP      $16             ; is it ink/paper/bright etc. ?
  4587.         JR      C,L0F6C         ; forward to ED-CONTR if so
  4588.  
  4589.                                 ; leaves 22d AT and 23d TAB
  4590.                                 ; which can't be entered via KEY-INPUT.
  4591.                                 ; so this code is never normally executed
  4592.                                 ; when the keyboard is used for input.
  4593.  
  4594.         INC     BC              ; if it was AT/TAB - 3 locations required
  4595.         BIT     7,(IY+$37)      ; test FLAGX  - Is this INPUT LINE ?
  4596.         JP      Z,L101E         ; jump to ED-IGNORE if not, else
  4597.  
  4598.         CALL    L15D4           ; routine WAIT-KEY - input address is KEY-NEXT
  4599.                                 ; but is reset to KEY-INPUT
  4600.         LD      E,A             ; save first in E
  4601.  
  4602. ;; ED-CONTR
  4603. L0F6C:  CALL    L15D4           ; routine WAIT-KEY for control.
  4604.                                 ; input address will be key-next.
  4605.  
  4606.         PUSH    DE              ; saved code/parameters
  4607.         LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4608.         RES     0,(IY+$07)      ; set MODE to 'L'
  4609.  
  4610.         CALL    L1655           ; routine MAKE-ROOM makes 2/3 spaces at cursor
  4611.  
  4612.         POP     BC              ; restore code/parameters
  4613.         INC     HL              ; address first location
  4614.         LD      (HL),B          ; place code (ink etc.)
  4615.         INC     HL              ; address next
  4616.         LD      (HL),C          ; place possible parameter. If only one
  4617.                                 ; then DE points to this location also.
  4618.         JR      L0F8B           ; forward to ADD-CH-1
  4619.  
  4620. ; ------------------------
  4621. ; Add code to current line
  4622. ; ------------------------
  4623. ; this is the branch used to add normal non-control characters
  4624. ; with ED-LOOP as the stacked return address.
  4625. ; it is also the OUTPUT service routine for system channel 'R'.
  4626.  
  4627. ;; ADD-CHAR
  4628. L0F81:  RES     0,(IY+$07)      ; set MODE to 'L'
  4629.  
  4630. X0F85:  LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4631.         CALL    L1652           ; routine ONE-SPACE creates one space.
  4632.  
  4633. ; either a continuation of above or from ED-CONTR with ED-LOOP on stack.
  4634.  
  4635. ;; ADD-CH-1
  4636. L0F8B:  LD      (DE),A          ; load current character to last new location.
  4637.         INC     DE              ; address next
  4638.         LD      ($5C5B),DE      ; and update K_CUR system variable.
  4639.         RET                     ; return - either a simple return
  4640.                                 ; from ADD-CHAR or to ED-LOOP on stack.
  4641.  
  4642. ; ---
  4643.  
  4644. ; a branch of the editing loop to deal with control characters
  4645. ; using a look-up table.
  4646.  
  4647. ;; ED-KEYS
  4648. L0F92:  LD      E,A             ; character to E.
  4649.         LD      D,$00           ; prepare to add.
  4650.         LD      HL,L0FA0 - 7    ; base address of editing keys table. $0F99
  4651.         ADD     HL,DE           ; add E
  4652.         LD      E,(HL)          ; fetch offset to E
  4653.         ADD     HL,DE           ; add offset for address of handling routine.
  4654.         PUSH    HL              ; push the address on machine stack.
  4655.         LD      HL,($5C5B)      ; load address of cursor from K_CUR.
  4656.         RET                     ; an make an indirect jump forward to routine.
  4657.  
  4658. ; ------------------
  4659. ; Editing keys table
  4660. ; ------------------
  4661. ; For each code in the range $07 to $0F this table contains a
  4662. ; single offset byte to the routine that services that code.
  4663. ; Note. for what was intended there should also have been an
  4664. ; entry for chr$ 6 with offset to ed-symbol.
  4665.  
  4666. ;; ed-keys-t
  4667. L0FA0:  DB    L0FA9 - $  ; 07d offset $09 to Address: ED-EDIT
  4668.         DB    L1007 - $  ; 08d offset $66 to Address: ED-LEFT
  4669.         DB    L100C - $  ; 09d offset $6A to Address: ED-RIGHT
  4670.         DB    L0FF3 - $  ; 10d offset $50 to Address: ED-DOWN
  4671.         DB    L1059 - $  ; 11d offset $B5 to Address: ED-UP
  4672.         DB    L1015 - $  ; 12d offset $70 to Address: ED-DELETE
  4673.         DB    L1024 - $  ; 13d offset $7E to Address: ED-ENTER
  4674.         DB    L1076 - $  ; 14d offset $CF to Address: ED-SYMBOL
  4675.         DB    L107C - $  ; 15d offset $D4 to Address: ED-GRAPH
  4676.  
  4677. ; ---------------
  4678. ; Handle EDIT key
  4679. ; ---------------
  4680. ; The user has pressed SHIFT 1 to bring edit line down to bottom of screen.
  4681. ; Alternatively the user wishes to clear the input buffer and start again.
  4682. ; Alternatively ...
  4683.  
  4684. ;; ED-EDIT
  4685. L0FA9:  LD      HL,($5C49)      ; fetch E_PPC the last line number entered.
  4686.                                 ; Note. may not exist and may follow program.
  4687.         BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4688.         JP      NZ,L1097        ; jump forward to CLEAR-SP if not in editor.
  4689.  
  4690.         CALL    L196E           ; routine LINE-ADDR to find address of line
  4691.                                 ; or following line if it doesn't exist.
  4692.         CALL    L1695           ; routine LINE-NO will get line number from
  4693.                                 ; address or previous line if at end-marker.
  4694.         LD      A,D             ; if there is no program then DE will
  4695.         OR      E               ; contain zero so test for this.
  4696.         JP      Z,L1097         ; jump to to CLEAR-SP if so.
  4697.  
  4698. ; Note. at this point we have a validated line number, not just an
  4699. ; approximation and it would be best to update E_PPC with the true
  4700. ; cursor line value which would enable the line cursor to be suppressed
  4701. ; in all situations - see shortly.
  4702.  
  4703.         PUSH    HL              ; save address of line.
  4704.         INC     HL              ; address low byte of length.
  4705.         LD      C,(HL)          ; transfer to C
  4706.         INC     HL              ; next to high byte
  4707.         LD      B,(HL)          ; transfer to B.
  4708.         LD      HL,$000A        ; an overhead of ten bytes
  4709.         ADD     HL,BC           ; is added to length.
  4710.         LD      B,H             ; transfer adjusted value
  4711.         LD      C,L             ; to BC register.
  4712.         CALL    L1F05           ; routine TEST-ROOM checks free memory.
  4713.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  4714.         LD      HL,($5C51)      ; address CURCHL
  4715.         EX      (SP),HL         ; swap with line address on stack
  4716.         PUSH    HL              ; save line address underneath
  4717.  
  4718.         LD      A,$FF           ; select system channel 'R'
  4719.         CALL    L1601           ; routine CHAN-OPEN opens it
  4720.  
  4721.         POP     HL              ; drop line address
  4722.         DEC     HL              ; make it point to first byte of line num.
  4723.         DEC     (IY+$0F)        ; decrease E_PPC_lo to suppress line cursor.
  4724.                                 ; Note. ineffective when E_PPC is one
  4725.                                 ; greater than last line of program perhaps
  4726.                                 ; as a result of a delete.
  4727.                                 ; credit. Paul Harrison 1982.
  4728.  
  4729.         CALL    L1855           ; routine OUT-LINE outputs the BASIC line
  4730.                                 ; to the editing area.
  4731.         INC     (IY+$0F)        ; restore E_PPC_lo to the previous value.
  4732.         LD      HL,($5C59)      ; address E_LINE in editing area.
  4733.         INC     HL              ; advance
  4734.         INC     HL              ; past space
  4735.         INC     HL              ; and digit characters
  4736.         INC     HL              ; of line number.
  4737.  
  4738.         LD      ($5C5B),HL      ; update K_CUR to address start of BASIC.
  4739.         POP     HL              ; restore the address of CURCHL.
  4740.         CALL    L1615           ; routine CHAN-FLAG sets flags for it.
  4741.         RET                     ; RETURN to ED-LOOP.
  4742.  
  4743. ; -------------------
  4744. ; Cursor down editing
  4745. ; -------------------
  4746. ; The BASIC lines are displayed at the top of the screen and the user
  4747. ; wishes to move the cursor down one line in edit mode.
  4748. ; In input mode this key can be used as an alternative to entering STOP.
  4749.  
  4750. ;; ED-DOWN
  4751. L0FF3:  BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  4752.         JR      NZ,L1001        ; skip to ED-STOP if so
  4753.  
  4754.         LD      HL,$5C49        ; address E_PPC - 'current line'
  4755.         CALL    L190F           ; routine LN-FETCH fetches number of next
  4756.                                 ; line or same if at end of program.
  4757.         JR      L106E           ; forward to ED-LIST to produce an
  4758.                                 ; automatic listing.
  4759.  
  4760. ; ---
  4761.  
  4762. ;; ED-STOP
  4763. L1001:  LD      (IY+$00),$10    ; set ERR_NR to 'STOP in INPUT' code
  4764.         JR      L1024           ; forward to ED-ENTER to produce error.
  4765.  
  4766. ; -------------------
  4767. ; Cursor left editing
  4768. ; -------------------
  4769. ; This acts on the cursor in the lower section of the screen in both
  4770. ; editing and input mode.
  4771.  
  4772. ;; ED-LEFT
  4773. L1007:  CALL    L1031           ; routine ED-EDGE moves left if possible
  4774.         JR      L1011           ; forward to ED-CUR to update K-CUR
  4775.                                 ; and return to ED-LOOP.
  4776.  
  4777. ; --------------------
  4778. ; Cursor right editing
  4779. ; --------------------
  4780. ; This acts on the cursor in the lower screen in both editing and input
  4781. ; mode and moves it to the right.
  4782.  
  4783. ;; ED-RIGHT
  4784. L100C:  LD      A,(HL)          ; fetch addressed character.
  4785.         CP      $0D             ; is it carriage return ?
  4786.         RET     Z               ; return if so to ED-LOOP
  4787.  
  4788.         INC     HL              ; address next character
  4789.  
  4790. ;; ED-CUR
  4791. L1011:  LD      ($5C5B),HL      ; update K_CUR system variable
  4792.         RET                     ; return to ED-LOOP
  4793.  
  4794. ; --------------
  4795. ; DELETE editing
  4796. ; --------------
  4797. ; This acts on the lower screen and deletes the character to left of
  4798. ; cursor. If control characters are present these are deleted first
  4799. ; leaving the naked parameter (0-7) which appears as a '?' except in the
  4800. ; case of chr$ 6 which is the comma control character. It is not mandatory
  4801. ; to delete these second characters.
  4802.  
  4803. ;; ED-DELETE
  4804. L1015:  CALL    L1031           ; routine ED-EDGE moves cursor to left.
  4805.         LD      BC,$0001        ; of character to be deleted.
  4806.         JP      L19E8           ; to RECLAIM-2 reclaim the character.
  4807.  
  4808. ; ------------------------------------------
  4809. ; Ignore next 2 codes from key-input routine
  4810. ; ------------------------------------------
  4811. ; Since AT and TAB cannot be entered this point is never reached
  4812. ; from the keyboard. If inputting from a tape device or network then
  4813. ; the control and two following characters are ignored and processing
  4814. ; continues as if a carriage return had been received.
  4815. ; Here, perhaps, another Spectrum has said print #15; AT 0,0; "This is yellow"
  4816. ; and this one is interpreting input #15; a$.
  4817.  
  4818. ;; ED-IGNORE
  4819. L101E:  CALL    L15D4           ; routine WAIT-KEY to ignore keystroke.
  4820.         CALL    L15D4           ; routine WAIT-KEY to ignore next key.
  4821.  
  4822. ; -------------
  4823. ; Enter/newline
  4824. ; -------------
  4825. ; The enter key has been pressed to have BASIC line or input accepted.
  4826.  
  4827. ;; ED-ENTER
  4828. L1024:  POP     HL              ; discard address ED-LOOP
  4829.         POP     HL              ; drop address ED-ERROR
  4830.  
  4831. ;; ED-END
  4832. L1026:  POP     HL              ; the previous value of ERR_SP
  4833.         LD      ($5C3D),HL      ; is restored to ERR_SP system variable
  4834.         BIT     7,(IY+$00)      ; is ERR_NR $FF (= 'OK') ?
  4835.         RET     NZ              ; return if so
  4836.  
  4837.         LD      SP,HL           ; else put error routine on stack
  4838.         RET                     ; and make an indirect jump to it.
  4839.  
  4840. ; -----------------------------
  4841. ; Move cursor left when editing
  4842. ; -----------------------------
  4843. ; This routine moves the cursor left. The complication is that it must
  4844. ; not position the cursor between control codes and their parameters.
  4845. ; It is further complicated in that it deals with TAB and AT characters
  4846. ; which are never present from the keyboard.
  4847. ; The method is to advance from the beginning of the line each time,
  4848. ; jumping one, two, or three characters as necessary saving the original
  4849. ; position at each jump in DE. Once it arrives at the cursor then the next
  4850. ; legitimate leftmost position is in DE.
  4851.  
  4852. ;; ED-EDGE
  4853. L1031:  SCF                     ; carry flag must be set to call the nested
  4854.         CALL    L1195           ; subroutine SET-DE.
  4855.                                 ; if input   then DE=WORKSP
  4856.                                 ; if editing then DE=E_LINE
  4857.         SBC     HL,DE           ; subtract address from start of line
  4858.         ADD     HL,DE           ; and add back.
  4859.         INC     HL              ; adjust for carry.
  4860.         POP     BC              ; drop return address
  4861.         RET     C               ; return to ED-LOOP if already at left
  4862.                                 ; of line.
  4863.  
  4864.         PUSH    BC              ; resave return address - ED-LOOP.
  4865.         LD      B,H             ; transfer HL - cursor address
  4866.         LD      C,L             ; to BC register pair.
  4867.                                 ; at this point DE addresses start of line.
  4868.  
  4869. ;; ED-EDGE-1
  4870. L103E:  LD      H,D             ; transfer DE - leftmost pointer
  4871.         LD      L,E             ; to HL
  4872.         INC     HL              ; address next leftmost character to
  4873.                                 ; advance position each time.
  4874.         LD      A,(DE)          ; pick up previous in A
  4875.         AND     $F0             ; lose the low bits
  4876.         CP      $10             ; is it INK to TAB $10-$1F ?
  4877.                                 ; that is, is it followed by a parameter ?
  4878.         JR      NZ,L1051        ; to ED-EDGE-2 if not
  4879.                                 ; HL has been incremented once
  4880.  
  4881.         INC     HL              ; address next as at least one parameter.
  4882.  
  4883. ; in fact since 'tab' and 'at' cannot be entered the next section seems
  4884. ; superfluous.
  4885. ; The test will always fail and the jump to ED-EDGE-2 will be taken.
  4886.  
  4887.         LD      A,(DE)          ; reload leftmost character
  4888.         SUB     $17             ; decimal 23 ('tab')
  4889.         ADC     A,$00           ; will be 0 for 'tab' and 'at'.
  4890.         JR      NZ,L1051        ; forward to ED-EDGE-2 if not
  4891.                                 ; HL has been incremented twice
  4892.  
  4893.         INC     HL              ; increment a third time for 'at'/'tab'
  4894.  
  4895. ;; ED-EDGE-2
  4896. L1051:  AND     A               ; prepare for true subtraction
  4897.         SBC     HL,BC           ; subtract cursor address from pointer
  4898.         ADD     HL,BC           ; and add back
  4899.                                 ; Note when HL matches the cursor position BC,
  4900.                                 ; there is no carry and the previous
  4901.                                 ; position is in DE.
  4902.         EX      DE,HL           ; transfer result to DE if looping again.
  4903.                                 ; transfer DE to HL to be used as K-CUR
  4904.                                 ; if exiting loop.
  4905.         JR      C,L103E         ; back to ED-EDGE-1 if cursor not matched.
  4906.  
  4907.         RET                     ; return.
  4908.  
  4909. ; -----------------
  4910. ; Cursor up editing
  4911. ; -----------------
  4912. ; The main screen displays part of the BASIC program and the user wishes
  4913. ; to move up one line scrolling if necessary.
  4914. ; This has no alternative use in input mode.
  4915.  
  4916. ;; ED-UP
  4917. L1059:  BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4918.         RET     NZ              ; return if not in editor - to ED-LOOP.
  4919.  
  4920.         LD      HL,($5C49)      ; get current line from E_PPC
  4921.         CALL    L196E           ; routine LINE-ADDR gets address
  4922.         EX      DE,HL           ; and previous in DE
  4923.         CALL    L1695           ; routine LINE-NO gets prev line number
  4924.         LD      HL,$5C4A        ; set HL to E_PPC_hi as next routine stores
  4925.                                 ; top first.
  4926.         CALL    L191C           ; routine LN-STORE loads DE value to HL
  4927.                                 ; high byte first - E_PPC_lo takes E
  4928.  
  4929. ; this branch is also taken from ed-down.
  4930.  
  4931. ;; ED-LIST
  4932. L106E:  CALL    L1795           ; routine AUTO-LIST lists to upper screen
  4933.                                 ; including adjusted current line.
  4934.         LD      A,$00           ; select lower screen again
  4935.         JP      L1601           ; exit via CHAN-OPEN to ED-LOOP
  4936.  
  4937. ; --------------------------------
  4938. ; Use of symbol and graphics codes
  4939. ; --------------------------------
  4940. ; These will not be encountered with the keyboard but would be handled
  4941. ; otherwise as follows.
  4942. ; As noted earlier, Vickers says there should have been an entry in
  4943. ; the KEYS table for chr$ 6 which also pointed here.
  4944. ; If, for simplicity, two Spectrums were both using #15 as a bi-directional
  4945. ; channel connected to each other:-
  4946. ; then when the other Spectrum has said PRINT #15; x, y
  4947. ; input #15; i ; j  would treat the comma control as a newline and the
  4948. ; control would skip to input j.
  4949. ; You can get round the missing chr$ 6 handler by sending multiple print
  4950. ; items separated by a newline '.
  4951.  
  4952. ; chr$14 would have the same functionality.
  4953.  
  4954. ; This is chr$ 14.
  4955. ;; ED-SYMBOL
  4956. L1076:  BIT     7,(IY+$37)      ; test FLAGX - is this INPUT LINE ?
  4957.         JR      Z,L1024         ; back to ED-ENTER if not to treat as if
  4958.                                 ; enter had been pressed.
  4959.                                 ; else continue and add code to buffer.
  4960.  
  4961. ; Next is chr$ 15
  4962. ; Note that ADD-CHAR precedes the table so we can't offset to it directly.
  4963.  
  4964. ;; ED-GRAPH
  4965. L107C:  JP      L0F81           ; jump back to ADD-CHAR
  4966.  
  4967. ; --------------------
  4968. ; Editor error routine
  4969. ; --------------------
  4970. ; If an error occurs while editing, or inputting, then ERR_SP
  4971. ; points to the stack location holding address ED_ERROR.
  4972.  
  4973. ;; ED-ERROR
  4974. L107F:  BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  4975.         JR      Z,L1026         ; back to ED-END if not.
  4976.  
  4977. ; but as long as we're editing lines or inputting from the keyboard, then
  4978. ; we've run out of memory so give a short rasp.
  4979.  
  4980.         LD      (IY+$00),$FF    ; reset ERR_NR to 'OK'.
  4981.         LD      D,$00           ; prepare for beeper.
  4982.         LD      E,(IY-$02)      ; use RASP value.
  4983.         LD      HL,$1A90        ; set a duration.
  4984.         CALL    L03B5           ; routine BEEPER emits a warning rasp.
  4985.         JP      L0F30           ; to ED-AGAIN to re-stack address of
  4986.                                 ; this routine and make ERR_SP point to it.
  4987.  
  4988. ; ---------------------
  4989. ; Clear edit/work space
  4990. ; ---------------------
  4991. ; The editing area or workspace is cleared depending on context.
  4992. ; This is called from ED-EDIT to clear workspace if edit key is
  4993. ; used during input, to clear editing area if no program exists
  4994. ; and to clear editing area prior to copying the edit line to it.
  4995. ; It is also used by the error routine to clear the respective
  4996. ; area depending on FLAGX.
  4997.  
  4998. ;; CLEAR-SP
  4999. L1097:  PUSH    HL              ; preserve HL
  5000.         CALL    L1190           ; routine SET-HL
  5001.                                 ; if in edit   HL = WORKSP-1, DE = E_LINE
  5002.                                 ; if in input  HL = STKBOT,   DE = WORKSP
  5003.         DEC     HL              ; adjust
  5004.         CALL    L19E5           ; routine RECLAIM-1 reclaims space
  5005.         LD      ($5C5B),HL      ; set K_CUR to start of empty area
  5006.         LD      (IY+$07),$00    ; set MODE to 'KLC'
  5007.         POP     HL              ; restore HL.
  5008.         RET                     ; return.
  5009.  
  5010. ; ---------------------
  5011. ; Handle keyboard input
  5012. ; ---------------------
  5013. ; This is the service routine for the input stream of the keyboard
  5014. ; channel 'K'.
  5015.  
  5016. ;; KEY-INPUT
  5017. L10A8:  BIT     3,(IY+$02)      ; test TV_FLAG  - has a key been pressed in
  5018.                                 ; editor ?
  5019.         CALL    NZ,L111D        ; routine ED-COPY if so to reprint the lower
  5020.                                 ; screen at every keystroke.
  5021.         AND     A               ; clear carry - required exit condition.
  5022.         BIT     5,(IY+$01)      ; test FLAGS  - has a new key been pressed ?
  5023.         RET     Z               ; return if not.
  5024.  
  5025.         LD      A,($5C08)       ; system variable LASTK will hold last key -
  5026.                                 ; from the interrupt routine.
  5027.         RES     5,(IY+$01)      ; update FLAGS  - reset the new key flag.
  5028.         PUSH    AF              ; save the input character.
  5029.         BIT     5,(IY+$02)      ; test TV_FLAG  - clear lower screen ?
  5030.         CALL    NZ,L0D6E        ; routine CLS-LOWER if so.
  5031.  
  5032.         POP     AF              ; restore the character code.
  5033.         CP      $20             ; if space or higher then
  5034.         JR      NC,L111B        ; forward to KEY-DONE2 and return with carry
  5035.                                 ; set to signal key-found.
  5036.  
  5037.         CP      $10             ; with 16d INK and higher skip
  5038.         JR      NC,L10FA        ; forward to KEY-CONTR.
  5039.  
  5040.         CP      $06             ; for 6 - 15d
  5041.         JR      NC,L10DB        ; skip forward to KEY-M-CL to handle Modes
  5042.                                 ; and CapsLock.
  5043.  
  5044. ; that only leaves 0-5, the flash bright inverse switches.
  5045.  
  5046.         LD      B,A             ; save character in B
  5047.         AND     $01             ; isolate the embedded parameter (0/1).
  5048.         LD      C,A             ; and store in C
  5049.         LD      A,B             ; re-fetch copy (0-5)
  5050.         RRA                     ; halve it 0, 1 or 2.
  5051.         ADD     A,$12           ; add 18d gives 'flash', 'bright'
  5052.                                 ; and 'inverse'.
  5053.         JR      L1105           ; forward to KEY-DATA with the
  5054.                                 ; parameter (0/1) in C.
  5055.  
  5056. ; ---
  5057.  
  5058. ; Now separate capslock 06 from modes 7-15.
  5059.  
  5060. ;; KEY-M-CL
  5061. L10DB:  JR      NZ,L10E6        ; forward to KEY-MODE if not 06 (capslock)
  5062.  
  5063.         LD      HL,$5C6A        ; point to FLAGS2
  5064.         LD      A,$08           ; value 00000100
  5065.         XOR     (HL)            ; toggle BIT 2 of FLAGS2 the capslock bit
  5066.         LD      (HL),A          ; and store result in FLAGS2 again.
  5067.         JR      L10F4           ; forward to KEY-FLAG to signal no-key.
  5068.  
  5069. ; ---
  5070.  
  5071. ;; KEY-MODE
  5072. L10E6:  CP      $0E             ; compare with chr 14d
  5073.         RET     C               ; return with carry set "key found" for
  5074.                                 ; codes 7 - 13d leaving 14d and 15d
  5075.                                 ; which are converted to mode codes.
  5076.  
  5077.         SUB     $0D             ; subtract 13d leaving 1 and 2
  5078.                                 ; 1 is 'E' mode, 2 is 'G' mode.
  5079.         LD      HL,$5C41        ; address the MODE system variable.
  5080.         CP      (HL)            ; compare with existing value before
  5081.         LD      (HL),A          ; inserting the new value.
  5082.         JR      NZ,L10F4        ; forward to KEY-FLAG if it has changed.
  5083.  
  5084.         LD      (HL),$00        ; else make MODE zero - KLC mode
  5085.                                 ; Note. while in Extended/Graphics mode,
  5086.                                 ; the Extended Mode/Graphics key is pressed
  5087.                                 ; again to get out.
  5088.  
  5089. ;; KEY-FLAG
  5090. L10F4:  SET     3,(IY+$02)      ; update TV_FLAG  - show key state has changed
  5091.         CP      A               ; clear carry and reset zero flags -
  5092.                                 ; no actual key returned.
  5093.         RET                     ; make the return.
  5094.  
  5095. ; ---
  5096.  
  5097. ; now deal with colour controls - 16-23 ink, 24-31 paper
  5098.  
  5099. ;; KEY-CONTR
  5100. L10FA:  LD      B,A             ; make a copy of character.
  5101.         AND     $07             ; mask to leave bits 0-7
  5102.         LD      C,A             ; and store in C.
  5103.         LD      A,$10           ; initialize to 16d - INK.
  5104.         BIT     3,B             ; was it paper ?
  5105.         JR      NZ,L1105        ; forward to KEY-DATA with INK 16d and
  5106.                                 ; colour in C.
  5107.  
  5108.         INC     A               ; else change from INK to PAPER (17d) if so.
  5109.  
  5110. ;; KEY-DATA
  5111. L1105:  LD      (IY-$2D),C      ; put the colour (0-7)/state(0/1) in KDATA
  5112.         LD      DE,L110D        ; address: KEY-NEXT will be next input stream
  5113.         JR      L1113           ; forward to KEY-CHAN to change it ...
  5114.  
  5115. ; ---
  5116.  
  5117. ; ... so that INPUT_AD directs control to here at next call to WAIT-KEY
  5118.  
  5119. ;; KEY-NEXT
  5120. L110D:  LD      A,($5C0D)       ; pick up the parameter stored in KDATA.
  5121.         LD      DE,L10A8        ; address: KEY-INPUT will be next input stream
  5122.                                 ; continue to restore default channel and
  5123.                                 ; make a return with the control code.
  5124.  
  5125. ;; KEY-CHAN
  5126. L1113:  LD      HL,($5C4F)      ; address start of CHANNELS area using CHANS
  5127.                                 ; system variable.
  5128.                                 ; Note. One might have expected CURCHL to
  5129.                                 ; have been used.
  5130.         INC     HL              ; step over the
  5131.         INC     HL              ; output address
  5132.         LD      (HL),E          ; and update the input
  5133.         INC     HL              ; routine address for
  5134.         LD      (HL),D          ; the next call to WAIT-KEY.
  5135.  
  5136. ;; KEY-DONE2
  5137. L111B:  SCF                     ; set carry flag to show a key has been found
  5138.         RET                     ; and return.
  5139.  
  5140. ; --------------------
  5141. ; Lower screen copying
  5142. ; --------------------
  5143. ; This subroutine is called whenever the line in the editing area or
  5144. ; input workspace is required to be printed to the lower screen.
  5145. ; It is by calling this routine after any change that the cursor, for
  5146. ; instance, appears to move to the left.
  5147. ; Remember the edit line will contain characters and tokens
  5148. ; e.g. "1000 LET a = 1" is 12 characters.
  5149.  
  5150. ;; ED-COPY
  5151. L111D:  CALL    L0D4D           ; routine TEMPS sets temporary attributes.
  5152.         RES     3,(IY+$02)      ; update TV_FLAG  - signal no change in mode
  5153.         RES     5,(IY+$02)      ; update TV_FLAG  - signal don't clear lower
  5154.                                 ; screen.
  5155.         LD      HL,($5C8A)      ; fetch SPOSNL
  5156.         PUSH    HL              ; and save on stack.
  5157.  
  5158.         LD      HL,($5C3D)      ; fetch ERR_SP
  5159.         PUSH    HL              ; and save also
  5160.         LD      HL,L1167        ; address: ED-FULL
  5161.         PUSH    HL              ; is pushed as the error routine
  5162.         LD      ($5C3D),SP      ; and ERR_SP made to point to it.
  5163.  
  5164.         LD      HL,($5C82)      ; fetch ECHO_E
  5165.         PUSH    HL              ; and push also
  5166.  
  5167.         SCF                     ; set carry flag to control SET-DE
  5168.         CALL    L1195           ; call routine SET-DE
  5169.                                 ; if in input DE = WORKSP
  5170.                                 ; if in edit  DE = E_LINE
  5171.         EX      DE,HL           ; start address to HL
  5172.  
  5173.         CALL    L187D           ; routine OUT-LINE2 outputs entire line up to
  5174.                                 ; carriage return including initial
  5175.                                 ; characterized line number when present.
  5176.         EX      DE,HL           ; transfer new address to DE
  5177.         CALL    L18E1           ; routine OUT-CURS considers a
  5178.                                 ; terminating cursor.
  5179.  
  5180.         LD      HL,($5C8A)      ; fetch updated SPOSNL
  5181.         EX      (SP),HL         ; exchange with ECHO_E on stack
  5182.         EX      DE,HL           ; transfer ECHO_E to DE
  5183.         CALL    L0D4D           ; routine TEMPS to re-set attributes
  5184.                                 ; if altered.
  5185.  
  5186. ; the lower screen was not cleared, at the outset, so if deleting then old
  5187. ; text from a previous print may follow this line and requires blanking.
  5188.  
  5189. ;; ED-BLANK
  5190. L1150:  LD      A,($5C8B)       ; fetch SPOSNL_hi is current line
  5191.         SUB     D               ; compare with old
  5192.         JR      C,L117C         ; forward to ED-C-DONE if no blanking
  5193.  
  5194.         JR      NZ,L115E        ; forward to ED-SPACES if line has changed
  5195.  
  5196.         LD      A,E             ; old column to A
  5197.         SUB     (IY+$50)        ; subtract new in SPOSNL_lo
  5198.         JR      NC,L117C        ; forward to ED-C-DONE if no backfilling.
  5199.  
  5200. ;; ED-SPACES
  5201. L115E:  LD      A,$20           ; prepare a space.
  5202.         PUSH    DE              ; save old line/column.
  5203.         CALL    L09F4           ; routine PRINT-OUT prints a space over
  5204.                                 ; any text from previous print.
  5205.                                 ; Note. Since the blanking only occurs when
  5206.                                 ; using $09F4 to print to the lower screen,
  5207.                                 ; there is no need to vector via a RST 10
  5208.                                 ; and we can use this alternate set.
  5209.         POP     DE              ; restore the old line column.
  5210.         JR      L1150           ; back to ED-BLANK until all old text blanked.
  5211.  
  5212. ; -------
  5213. ; ED-FULL
  5214. ; -------
  5215. ; this is the error routine addressed by ERR_SP. This is not for the out of
  5216. ; memory situation as we're just printing. The pitch and duration are exactly
  5217. ; the same as used by ED-ERROR from which this has been augmented. The
  5218. ; situation is that the lower screen is full and a rasp is given to suggest
  5219. ; that this is perhaps not the best idea you've had that day.
  5220.  
  5221. ;; ED-FULL
  5222. L1167:  LD      D,$00           ; prepare to moan.
  5223.         LD      E,(IY-$02)      ; fetch RASP value.
  5224.         LD      HL,$1A90        ; set duration.
  5225.         CALL    L03B5           ; routine BEEPER.
  5226.         LD      (IY+$00),$FF    ; clear ERR_NR.
  5227.         LD      DE,($5C8A)      ; fetch SPOSNL.
  5228.         JR      L117E           ; forward to ED-C-END
  5229.  
  5230. ; -------
  5231.  
  5232. ; the exit point from line printing continues here.
  5233.  
  5234. ;; ED-C-DONE
  5235. L117C:  POP     DE              ; fetch new line/column.
  5236.         POP     HL              ; fetch the error address.
  5237.  
  5238. ; the error path rejoins here.
  5239.  
  5240. ;; ED-C-END
  5241. L117E:  POP     HL              ; restore the old value of ERR_SP.
  5242.         LD      ($5C3D),HL      ; update the system variable ERR_SP
  5243.         POP     BC              ; old value of SPOSN_L
  5244.         PUSH    DE              ; save new value
  5245.         CALL    L0DD9           ; routine CL-SET and PO-STORE
  5246.                                 ; update ECHO_E and SPOSN_L from BC
  5247.         POP     HL              ; restore new value
  5248.         LD      ($5C82),HL      ; and update ECHO_E
  5249.         LD      (IY+$26),$00    ; make error pointer X_PTR_hi out of bounds
  5250.         RET                     ; return
  5251.  
  5252. ; -----------------------------------------------
  5253. ; Point to first and last locations of work space
  5254. ; -----------------------------------------------
  5255. ; These two nested routines ensure that the appropriate pointers are
  5256. ; selected for the editing area or workspace. The routines that call
  5257. ; these routines are designed to work on either area.
  5258.  
  5259. ; this routine is called once
  5260. ;; SET-HL
  5261. L1190:  LD      HL,($5C61)      ; fetch WORKSP to HL.
  5262.         DEC     HL              ; point to last location of editing area.
  5263.         AND     A               ; clear carry to limit exit points to first
  5264.                                 ; or last.
  5265.  
  5266. ; this routine is called with carry set and exits at a conditional return.
  5267.  
  5268. ;; SET-DE
  5269. L1195:  LD      DE,($5C59)      ; fetch E_LINE to DE
  5270.         BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  5271.         RET     Z               ; return now if in editing mode
  5272.  
  5273.         LD      DE,($5C61)      ; fetch WORKSP to DE
  5274.         RET     C               ; return if carry set ( entry = set-de)
  5275.  
  5276.         LD      HL,($5C63)      ; fetch STKBOT to HL as well
  5277.         RET                     ; and return  (entry = set-hl (in input))
  5278.  
  5279. ; -------------------------------
  5280. ; Remove floating point from line
  5281. ; -------------------------------
  5282. ; When a BASIC LINE or the INPUT BUFFER is parsed any numbers will have
  5283. ; an invisible chr 14d inserted after them and the 5-byte integer or
  5284. ; floating point form inserted after that. Similar invisible value holders
  5285. ; are also created after the numeric and string variables in a DEF FN list.
  5286. ; This routine removes these 'compiled' numbers from the edit line or
  5287. ; input workspace.
  5288.  
  5289. ;; REMOVE-FP
  5290. L11A7:  LD      A,(HL)          ; fetch character
  5291.         CP      $0E             ; is it the number marker ?
  5292.         LD      BC,$0006        ; prepare for six bytes
  5293.         CALL    Z,L19E8         ; routine RECLAIM-2 reclaims space if $0E
  5294.         LD      A,(HL)          ; reload next (or same) character
  5295.         INC     HL              ; and advance address
  5296.         CP      $0D             ; end of line or input buffer ?
  5297.         JR      NZ,L11A7        ; back to REMOVE-FP until entire line done.
  5298.  
  5299.         RET                     ; return
  5300.  
  5301.  
  5302. ;*********************************
  5303. ;** Part 6. EXECUTIVE ROUTINES  **
  5304. ;*********************************
  5305.  
  5306.  
  5307. ; The memory.
  5308. ;
  5309. ; +---------+-----------+------------+--------------+-------------+--
  5310. ; | BASIC   |  Display  | Attributes | ZX Printer   |    System   |
  5311. ; |  ROM    |   File    |    File    |   Buffer     |  Variables  |
  5312. ; +---------+-----------+------------+--------------+-------------+--
  5313. ; ^         ^           ^            ^              ^             ^
  5314. ; $0000   $4000       $5800        $5B00          $5C00         $5CB6 = CHANS
  5315. ;
  5316. ;
  5317. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5318. ;    | Channel  |$80|  BASIC  | Variables |$80| Edit Line  |NL|$80|
  5319. ;    |   Info   |   | Program |   Area    |   | or Command |  |   |
  5320. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5321. ;    ^              ^         ^               ^                   ^
  5322. ;  CHANS           PROG      VARS           E_LINE              WORKSP
  5323. ;
  5324. ;
  5325. ;                             ---5-->         <---2---  <--3---
  5326. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5327. ;    | INPUT |NL| Temporary  | Calc. | Spare | Machine | GOSUB |?|$3E| UDGs |
  5328. ;    | data  |  | Work Space | Stack |       |  Stack  | Stack | |   |      |
  5329. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5330. ;    ^                       ^       ^       ^                   ^   ^      ^
  5331. ;  WORKSP                  STKBOT  STKEND   sp               RAMTOP UDG  P_RAMT
  5332. ;                                                                        
  5333.  
  5334. ; -------------------
  5335. ; Handle NEW command
  5336. ; -------------------
  5337. ; The NEW command is about to set all RAM below RAMTOP to zero and
  5338. ; then re-initialize the system. All RAM above RAMTOP should, and will be,
  5339. ; preserved.
  5340. ; There is nowhere to store values in RAM or on the stack which becomes
  5341. ; inoperable. Similarly PUSH and CALL instructions cannot be used to
  5342. ; store values or section common code. The alternate register set is the only
  5343. ; place available to store 3 persistent 16-bit system variables.
  5344.  
  5345. ;; NEW
  5346. L11B7:  DI                      ; disable interrupts - machine stack will be
  5347.                                 ; cleared.
  5348.         LD      A,$FF           ; flag coming from NEW.
  5349.         LD      DE,($5CB2)      ; fetch RAMTOP as top value.
  5350.         EXX                     ; switch in alternate set.
  5351.         LD      BC,($5CB4)      ; fetch P-RAMT differs on 16K/48K machines.
  5352.         LD      DE,($5C38)      ; fetch RASP/PIP.
  5353.         LD      HL,($5C7B)      ; fetch UDG    differs on 16K/48K machines.
  5354.         EXX                     ; switch back to main set and continue into...
  5355.  
  5356. ; ---------------------------
  5357. ; Main entry (initialization)
  5358. ; ---------------------------
  5359. ; This common code tests ram and sets it to zero re-initializing
  5360. ; all the non-zero system variables and channel information.
  5361. ; The A register tells if coming from START or NEW
  5362.  
  5363. ;; START-NEW
  5364. L11CB:  LD      B,A             ; save the flag for later branching.
  5365.  
  5366.         LD      A,$07           ; select a white border
  5367.         OUT     ($FE),A         ; and set it now.
  5368.  
  5369.         LD      A,$3F           ; load accumulator with last page in ROM.
  5370.         LD      I,A             ; set the I register - this remains constant
  5371.                                 ; and can't be in range $40 - $7F as 'snow'
  5372.                                 ; appears on the screen.
  5373.         NOP                     ; these seem unnecessary.
  5374.         NOP                     ;
  5375.         NOP                     ;
  5376.         NOP                     ;
  5377.         NOP                     ;
  5378.         NOP                     ;
  5379.  
  5380. ; ------------
  5381. ; Check RAM
  5382. ; ------------
  5383. ; Typically a Spectrum will have 16K or 48K of Ram and this code will
  5384. ; test it all till it finds an unpopulated location or, less likely, a
  5385. ; faulty location. Usually it stops when it reaches the top $FFFF or
  5386. ; in the case of NEW the supplied top value. The entire screen turns
  5387. ; black with sometimes red stripes on black paper visible.
  5388.  
  5389. ;; ram-check
  5390. L11DA:  LD      H,D             ; transfer the top value to
  5391.         LD      L,E             ; the HL register pair.
  5392.  
  5393. ;; RAM-FILL
  5394. L11DC:  LD      (HL),$02        ; load with 2 - red ink on black paper
  5395.         DEC     HL              ; next lower
  5396.         CP      H               ; have we reached ROM - $3F ?
  5397.         JR      NZ,L11DC        ; back to RAM-FILL if not.
  5398.  
  5399. ;; RAM-READ
  5400. L11E2:  AND     A               ; clear carry - prepare to subtract
  5401.         SBC     HL,DE           ; subtract and add back setting
  5402.         ADD     HL,DE           ; carry when back at start.
  5403.         INC     HL              ; and increment for next iteration.
  5404.         JR      NC,L11EF        ; forward to RAM-DONE if we've got back to
  5405.                                 ; starting point with no errors.
  5406.  
  5407.         DEC     (HL)            ; decrement to 1.
  5408.         JR      Z,L11EF         ; forward to RAM-DONE if faulty.
  5409.  
  5410.         DEC     (HL)            ; decrement to zero.
  5411.         JR      Z,L11E2         ; back to RAM-READ if zero flag was set.
  5412.  
  5413. ;; RAM-DONE
  5414. L11EF:  DEC     HL              ; step back to last valid location.
  5415.         EXX                     ; regardless of state, set up possibly
  5416.                                 ; stored system variables in case from NEW.
  5417.         LD      ($5CB4),BC      ; insert P-RAMT.
  5418.         LD      ($5C38),DE      ; insert RASP/PIP.
  5419.         LD      ($5C7B),HL      ; insert UDG.
  5420.         EXX                     ; switch in main set.
  5421.         INC     B               ; now test if we arrived here from NEW.
  5422.         JR      Z,L1219         ; forward to RAM-SET if we did.
  5423.  
  5424. ; this section applies to START only.
  5425.  
  5426.         LD      ($5CB4),HL      ; set P-RAMT to the highest working RAM
  5427.                                 ; address.
  5428.         LD      DE,$3EAF        ; address of last byte of 'U' bitmap in ROM.
  5429.         LD      BC,$00A8        ; there are 21 user defined graphics.
  5430.         EX      DE,HL           ; switch pointers and make the UDGs a
  5431.         LDDR                    ; copy of the standard characters A - U.
  5432.         EX      DE,HL           ; switch the pointer to HL.
  5433.         INC     HL              ; update to start of 'A' in RAM.
  5434.         LD      ($5C7B),HL      ; make UDG system variable address the first
  5435.                                 ; bitmap.
  5436.         DEC     HL              ; point at RAMTOP again.
  5437.  
  5438.         LD      BC,$0040        ; set the values of
  5439.         LD      ($5C38),BC      ; the PIP and RASP system variables.
  5440.  
  5441. ; the NEW command path rejoins here.
  5442.  
  5443. ;; RAM-SET
  5444. L1219:  LD      ($5CB2),HL      ; set system variable RAMTOP to HL.
  5445.  
  5446.                 LD HL,CHARS-0X100       ;$3C00        ; a strange place to set the pointer to the
  5447.         LD      ($5C36),HL      ; character set, CHARS - as no printing yet.
  5448.  
  5449.         LD      HL,($5CB2)      ; fetch RAMTOP to HL again as we've lost it.
  5450.  
  5451.         LD      (HL),$3E        ; top of user ram holds GOSUB end marker
  5452.                                 ; an impossible line number - see RETURN.
  5453.                                 ; no significance in the number $3E. It has
  5454.                                 ; been traditional since the ZX80.
  5455.  
  5456.         DEC     HL              ; followed by empty byte (not important).
  5457.         LD      SP,HL           ; set up the machine stack pointer.
  5458.         DEC     HL              ;
  5459.         DEC     HL              ;
  5460.         LD      ($5C3D),HL      ; ERR_SP is where the error pointer is
  5461.                                 ; at moment empty - will take address MAIN-4
  5462.                                 ; at the call preceding that address,
  5463.                                 ; although interrupts and calls will make use
  5464.                                 ; of this location in meantime.
  5465.  
  5466.         IM      1               ; select interrupt mode 1.
  5467.         LD      IY,$5C3A        ; set IY to ERR_NR. IY can reach all standard
  5468.                                 ; system variables but shadow ROM system
  5469.                                 ; variables will be mostly out of range.
  5470.  
  5471.         EI                      ; enable interrupts now that we have a stack.
  5472.  
  5473.         LD      HL,$5CB6        ; the address of the channels - initially
  5474.                                 ; following system variables.
  5475.         LD      ($5C4F),HL      ; set the CHANS system variable.
  5476.  
  5477.         LD      DE,L15AF        ; address: init-chan in ROM.
  5478.         LD      BC,$0015        ; there are 21 bytes of initial data in ROM.
  5479.         EX      DE,HL           ; swap the pointers.
  5480.         LDIR                    ; copy the bytes to RAM.
  5481.  
  5482.         EX      DE,HL           ; swap pointers. HL points to program area.
  5483.         DEC     HL              ; decrement address.
  5484.         LD      ($5C57),HL      ; set DATADD to location before program area.
  5485.         INC     HL              ; increment again.
  5486.  
  5487.         LD      ($5C53),HL      ; set PROG the location where BASIC starts.
  5488.         LD      ($5C4B),HL      ; set VARS to same location with a
  5489.         LD      (HL),$80        ; variables end-marker.
  5490.         INC     HL              ; advance address.
  5491.         LD      ($5C59),HL      ; set E_LINE, where the edit line
  5492.                                 ; will be created.
  5493.                                 ; Note. it is not strictly necessary to
  5494.                                 ; execute the next fifteen bytes of code
  5495.                                 ; as this will be done by the call to SET-MIN.
  5496.                                 ; --
  5497.         LD      (HL),$0D        ; initially just has a carriage return
  5498.         INC     HL              ; followed by
  5499.         LD      (HL),$80        ; an end-marker.
  5500.         INC     HL              ; address the next location.
  5501.         LD      ($5C61),HL      ; set WORKSP - empty workspace.
  5502.         LD      ($5C63),HL      ; set STKBOT - bottom of the empty stack.
  5503.         LD      ($5C65),HL      ; set STKEND to the end of the empty stack.
  5504.                                 ; --
  5505.         LD      A,$38           ; the colour system is set to white paper,
  5506.                                 ; black ink, no flash or bright.
  5507.         LD      ($5C8D),A       ; set ATTR_P permanent colour attributes.
  5508.         LD      ($5C8F),A       ; set ATTR_T temporary colour attributes.
  5509.         LD      ($5C48),A       ; set BORDCR the border colour/lower screen
  5510.                                 ; attributes.
  5511.  
  5512.         LD      HL,$0523        ; The keyboard repeat and delay values
  5513.         LD      ($5C09),HL      ; are loaded to REPDEL and REPPER.
  5514.  
  5515.         DEC     (IY-$3A)        ; set KSTATE-0 to $FF.
  5516.         DEC     (IY-$36)        ; set KSTATE-4 to $FF.
  5517.                                 ; thereby marking both available.
  5518.  
  5519.         LD      HL,L15C6        ; set source to ROM Address: init-strm
  5520.         LD      DE,$5C10        ; set destination to system variable STRMS-FD
  5521.         LD      BC,$000E        ; copy the 14 bytes of initial 7 streams data
  5522.         LDIR                    ; from ROM to RAM.
  5523.  
  5524.         SET     1,(IY+$01)      ; update FLAGS  - signal printer in use.
  5525.  
  5526. ;===============================
  5527.                 IF AY_PRINTER
  5528.                 CALL PRINTER_INITER
  5529.                 ELSE
  5530.                 CALL L0EDF                      ; call routine CLEAR-PRB to initialize system
  5531.                                                 ; variables associated with printer.
  5532.                 ENDIF
  5533. ;===============================
  5534.  
  5535.         LD      (IY+$31),$02    ; set DF_SZ the lower screen display size to
  5536.                                 ; two lines
  5537.         CALL    L0D6B           ; call routine CLS to set up system
  5538.                                 ; variables associated with screen and clear
  5539.                                 ; the screen and set attributes.
  5540.         XOR     A               ; clear accumulator so that we can address
  5541.         LD      DE,L1539 - 1    ; the message table directly.
  5542.         CALL    L0C0A           ; routine PO-MSG puts
  5543.                                 ; '(c) 1982 Sinclair Research Ltd'
  5544.                                 ; at bottom of display.
  5545.         SET     5,(IY+$02)      ; update TV_FLAG  - signal lower screen will
  5546.                                 ; require clearing.
  5547.  
  5548.         JR      L12A9           ; forward to MAIN-1
  5549.  
  5550. ; -------------------
  5551. ; Main execution loop
  5552. ; -------------------
  5553. ;
  5554. ;
  5555.  
  5556. ;; MAIN-EXEC
  5557. L12A2:  LD      (IY+$31),$02    ; set DF_SZ lower screen display file
  5558.                                 ; size to 2 lines.
  5559.         CALL    L1795           ; routine AUTO-LIST
  5560.  
  5561. ;; MAIN-1
  5562. L12A9:  CALL    L16B0           ; routine SET-MIN clears work areas.
  5563.  
  5564. ;; MAIN-2
  5565. L12AC:  LD      A,$00           ; select channel 'K' the keyboard
  5566.         CALL    L1601           ; routine CHAN-OPEN opens it
  5567.         CALL    L0F2C           ; routine EDITOR is called.
  5568.                                 ; Note the above routine is where the Spectrum
  5569.                                 ; waits for user-interaction. Perhaps the
  5570.                                 ; most common input at this stage
  5571.                                 ; is LOAD "".
  5572.         CALL    L1B17           ; routine LINE-SCAN scans the input.
  5573.         BIT     7,(IY+$00)      ; test ERR_NR - will be $FF if syntax
  5574.                                 ; is correct.
  5575.         JR      NZ,L12CF        ; forward, if correct, to MAIN-3.
  5576.  
  5577. ;
  5578.  
  5579.         BIT     4,(IY+$30)      ; test FLAGS2 - K channel in use ?
  5580.         JR      Z,L1303         ; forward to MAIN-4 if not.
  5581.  
  5582. ;
  5583.  
  5584.         LD      HL,($5C59)      ; an editing error so address E_LINE.
  5585.         CALL    L11A7           ; routine REMOVE-FP removes the hidden
  5586.                                 ; floating-point forms.
  5587.         LD      (IY+$00),$FF    ; system variable ERR_NR is reset to 'OK'.
  5588.         JR      L12AC           ; back to MAIN-2 to allow user to correct.
  5589.  
  5590. ; ---
  5591.  
  5592. ; the branch was here if syntax has passed test.
  5593.  
  5594. ;; MAIN-3
  5595. L12CF:  LD      HL,($5C59)      ; fetch the edit line address from E_LINE.
  5596.         LD      ($5C5D),HL      ; system variable CH_ADD is set to first
  5597.                                 ; character of edit line.
  5598.                                 ; Note. the above two instructions are a little
  5599.                                 ; inadequate.
  5600.                                 ; They are repeated with a subtle difference
  5601.                                 ; at the start of the next subroutine and are
  5602.                                 ; therefore not required above.
  5603.  
  5604.         CALL    L19FB           ; routine E-LINE-NO will fetch any line
  5605.                                 ; number to BC if this is a program line.
  5606.  
  5607.         LD      A,B             ; test if the number of
  5608.         OR      C               ; the line is non-zero.
  5609.         JP      NZ,L155D        ; jump forward to MAIN-ADD if so to add the
  5610.                                 ; line to the BASIC program.
  5611.  
  5612. ; Has the user just pressed the ENTER key ?
  5613.  
  5614.         RST     18H             ; GET-CHAR gets character addressed by CH_ADD.
  5615.         CP      $0D             ; is it a carriage return ?
  5616.         JR      Z,L12A2         ; back to MAIN-EXEC if so for an automatic
  5617.                                 ; listing.
  5618.  
  5619. ; this must be a direct command.
  5620.  
  5621.         BIT     0,(IY+$30)      ; test FLAGS2 - clear the main screen ?
  5622.         CALL    NZ,L0DAF        ; routine CL-ALL, if so, e.g. after listing.
  5623.         CALL    L0D6E           ; routine CLS-LOWER anyway.
  5624.         LD      A,$19           ; compute scroll count to 25 minus
  5625.         SUB     (IY+$4F)        ; value of S_POSN_hi.
  5626.         LD      ($5C8C),A       ; update SCR_CT system variable.
  5627.         SET     7,(IY+$01)      ; update FLAGS - signal running program.
  5628.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK'.
  5629.         LD      (IY+$0A),$01    ; set NSPPC to one for first statement.
  5630.         CALL    L1B8A           ; call routine LINE-RUN to run the line.
  5631.                                 ; sysvar ERR_SP therefore addresses MAIN-4
  5632.  
  5633. ; Examples of direct commands are RUN, CLS, LOAD "", PRINT USR 40000,
  5634. ; LPRINT "A"; etc..
  5635. ; If a user written machine-code program disables interrupts then it
  5636. ; must enable them to pass the next step. We also jumped to here if the
  5637. ; keyboard was not being used.
  5638.  
  5639. ;; MAIN-4
  5640. L1303:  HALT                    ; wait for interrupt.
  5641.  
  5642.         RES     5,(IY+$01)      ; update FLAGS - signal no new key.
  5643.         BIT     1,(IY+$30)      ; test FLAGS2 - is printer buffer clear ?
  5644.         CALL    NZ,L0ECD        ; call routine COPY-BUFF if not.
  5645.                                 ; Note. the programmer has neglected
  5646.                                 ; to set bit 1 of FLAGS first.
  5647.  
  5648.         LD      A,($5C3A)       ; fetch ERR_NR
  5649.         INC     A               ; increment to give true code.
  5650.  
  5651. ; Now deal with a runtime error as opposed to an editing error.
  5652. ; However if the error code is now zero then the OK message will be printed.
  5653.  
  5654. ;; MAIN-G
  5655. L1313:  PUSH    AF              ; save the error number.
  5656.  
  5657.         LD      HL,$0000        ; prepare to clear some system variables.
  5658.         LD      (IY+$37),H      ; clear all the bits of FLAGX.
  5659.         LD      (IY+$26),H      ; blank X_PTR_hi to suppress error marker.
  5660.         LD      ($5C0B),HL      ; blank DEFADD to signal that no defined
  5661.                                 ; function is currently being evaluated.
  5662.  
  5663.         LD      HL,$0001        ; explicit - inc hl would do.
  5664.         LD      ($5C16),HL      ; ensure STRMS-00 is keyboard.
  5665.  
  5666.         CALL    L16B0           ; routine SET-MIN clears workspace etc.
  5667.         RES     5,(IY+$37)      ; update FLAGX - signal in EDIT not INPUT mode.
  5668.                                 ; Note. all the bits were reset earlier.
  5669.  
  5670.         CALL    L0D6E           ; call routine CLS-LOWER.
  5671.         SET     5,(IY+$02)      ; update TV_FLAG - signal lower screen
  5672.                                 ; requires clearing.
  5673.  
  5674.         POP     AF              ; bring back the error number
  5675.         LD      B,A             ; and make a copy in B.
  5676.         CP      $0A             ; is it a print-ready digit ?
  5677.         JR      C,L133C         ; forward to MAIN-5 if so.
  5678.  
  5679.         ADD     A,$07           ; add ASCII offset to letters.
  5680.  
  5681. ;; MAIN-5
  5682. L133C:  CALL    L15EF           ; call routine OUT-CODE to print the code.
  5683.  
  5684.         LD      A,$20           ; followed by a space.
  5685.         RST     10H             ; PRINT-A
  5686.  
  5687.         LD      A,B             ; fetch stored report code.
  5688.         LD      DE,L1391        ; address: rpt-mesgs.
  5689.         CALL    L0C0A           ; call routine PO-MSG to print.
  5690.  
  5691. X1349
  5692.                 IF BAS48_ONLY
  5693.                 XOR A
  5694.                 LD DE,L1536
  5695.                 ELSE
  5696.                 CALL L3B3B              ; Spectrum 128 patch
  5697.                 NOP
  5698.                 ENDIF
  5699.  
  5700. L134D:  CALL    L0C0A           ; routine PO-MSG prints them although it would
  5701.                                 ; be more succinct to use RST $10.
  5702.  
  5703.         LD      BC,($5C45)      ; fetch PPC the current line number.
  5704.         CALL    L1A1B           ; routine OUT-NUM-1 will print that
  5705.         LD      A,$3A           ; then a ':'.
  5706.         RST     10H             ; PRINT-A
  5707.  
  5708.         LD      C,(IY+$0D)      ; then SUBPPC for statement
  5709.         LD      B,$00           ; limited to 127
  5710.         CALL    L1A1B           ; routine OUT-NUM-1
  5711.  
  5712.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  5713.                                 ; which probably contained 'RUN'.
  5714.         LD      A,($5C3A)       ; fetch ERR_NR again
  5715.         INC     A               ; test for no error originally $FF.
  5716.         JR      Z,L1386         ; forward to MAIN-9 if no error.
  5717.  
  5718.         CP      $09             ; is code Report 9 STOP ?
  5719.         JR      Z,L1373         ; forward to MAIN-6 if so
  5720.  
  5721.         CP      $15             ; is code Report L Break ?
  5722.         JR      NZ,L1376        ; forward to MAIN-7 if not
  5723.  
  5724. ; Stop or Break was encountered so consider CONTINUE.
  5725.  
  5726. ;; MAIN-6
  5727. L1373:  INC     (IY+$0D)        ; increment SUBPPC to next statement.
  5728.  
  5729. ;; MAIN-7
  5730. L1376:  LD      BC,$0003        ; prepare to copy 3 system variables to
  5731.         LD      DE,$5C70        ; address OSPPC - statement for CONTINUE.
  5732.                                 ; also updating OLDPPC line number below.
  5733.  
  5734.         LD      HL,$5C44        ; set source top to NSPPC next statement.
  5735.         BIT     7,(HL)          ; did BREAK occur before the jump ?
  5736.                                 ; e.g. between GO TO and next statement.
  5737.         JR      Z,L1384         ; skip forward to MAIN-8, if not, as setup
  5738.                                 ; is correct.
  5739.  
  5740.         ADD     HL,BC           ; set source to SUBPPC number of current
  5741.                                 ; statement/line which will be repeated.
  5742.  
  5743. ;; MAIN-8
  5744. L1384:  LDDR                    ; copy PPC to OLDPPC and SUBPPC to OSPCC
  5745.                                 ; or NSPPC to OLDPPC and NEWPPC to OSPCC
  5746.  
  5747. ;; MAIN-9
  5748. L1386:  LD      (IY+$0A),$FF    ; update NSPPC - signal 'no jump'.
  5749.         RES     3,(IY+$01)      ; update FLAGS  - signal use 'K' mode for
  5750.                                 ; the first character in the editor and
  5751.         JP      L12AC           ; jump back to MAIN-2.
  5752.  
  5753.  
  5754. ; ----------------------
  5755. ; Canned report messages
  5756. ; ----------------------
  5757. ; The Error reports with the last byte inverted. The first entry
  5758. ; is a dummy entry. The last, which begins with $7F, the Spectrum
  5759. ; character for copyright symbol, is placed here for convenience
  5760. ; as is the preceding comma and space.
  5761. ; The report line must accommodate a 4-digit line number and a 3-digit
  5762. ; statement number which limits the length of the message text to twenty
  5763. ; characters.
  5764. ; e.g.  "B Integer out of range, 1000:127"
  5765.  
  5766. ;; rpt-mesgs
  5767. L1391           DB $80
  5768.                 DC "OK"                         ;DB    'O','K'+$80              ; 0
  5769.                 DC "NEXT without FOR"           ;DEFM    "NEXT without FO"
  5770.                                                 ;DB    'R'+$80          ; 1
  5771.                 DC "Variable not found"         ;DEFM    "Variable not foun"
  5772.                                                 ;DB    'd'+$80          ; 2
  5773.                 DC "Subscript wrong"            ;DEFM    "Subscript wron"
  5774.                                                 ;DB    'g'+$80          ; 3
  5775.                 DC "Out of memory"              ;DEFM    "Out of memor"
  5776.                                                 ;DB    'y'+$80          ; 4
  5777.                 DC "Out of screen"              ;DEFM    "Out of scree"
  5778.                                                 ;DB    'n'+$80          ; 5
  5779.                 DC "Number too big"             ;DEFM    "Number too bi"
  5780.                                                 ;DB    'g'+$80          ; 6
  5781.                 DC "RETURN without GOSUB"       ;DEFM    "RETURN without GOSU"
  5782.                                                 ;DB    'B'+$80          ; 7
  5783.                 DC "End of file"                ;DEFM    "End of fil"
  5784.                                                 ;DB    'e'+$80          ; 8
  5785.                 DC "STOP statement"             ;DEFM    "STOP statemen"
  5786.                                                 ;DB    't'+$80          ; 9
  5787.                 DC "Invalid argument"           ;DEFM    "Invalid argumen"
  5788.                                                 ;DB    't'+$80          ; A
  5789.                 DC "Integer out of range"       ;DEFM    "Integer out of rang"
  5790.                                                 ;DB    'e'+$80          ; B
  5791.                 DC "Nonsense in BASIC"          ;DEFM    "Nonsense in BASI"
  5792.                                                 ;DB    'C'+$80          ; C
  5793.                 DC "BREAK - CONT repeats"       ;DEFM    "BREAK - CONT repeat"
  5794.                                                 ;DB    's'+$80          ; D
  5795.                 DC "Out of DATA"                ;DEFM    "Out of DAT"
  5796.                                                 ;DB    'A'+$80          ; E
  5797.                 DC "Invalid file name"          ;DEFM    "Invalid file nam"
  5798.                                                 ;DB    'e'+$80          ; F
  5799.                 DC "No room for line"           ;DEFM    "No room for lin"
  5800.                                                 ;DB    'e'+$80          ; G
  5801.                 DC "STOP in INPUT"              ;DEFM    "STOP in INPU"
  5802.                                                 ;DB    'T'+$80          ; H
  5803.                 DC "FOR without NEXT"           ;DEFM    "FOR without NEX"
  5804.                                                 ;DB    'T'+$80          ; I
  5805.                 DC "Invalid I/O device"         ;DEFM    "Invalid I/O devic"
  5806.                                                 ;DB    'e'+$80          ; J
  5807.                 DC "Invalid colour"             ;DEFM    "Invalid colou"
  5808.                                                 ;DB    'r'+$80          ; K
  5809.                 DC "BREAK into program"         ;DEFM    "BREAK into progra"
  5810.                                                 ;DB    'm'+$80          ; L
  5811.                 DC "RAMTOP no good"             ;DEFM    "RAMTOP no goo"
  5812.                                                 ;DB    'd'+$80          ; M
  5813.                 DC "Statement lost"             ;DEFM    "Statement los"
  5814.                                                 ;DB    't'+$80          ; N
  5815.                 DC "Invalid stream"             ;DEFM    "Invalid strea"
  5816.                                                 ;DB    'm'+$80          ; O
  5817.                 DC "FN without DEF"             ;DEFM    "FN without DE"
  5818.                                                 ;DB    'F'+$80          ; P
  5819.                 DC "Parameter error"            ;DEFM    "Parameter erro"
  5820.                                                 ;DB    'r'+$80          ; Q
  5821.                 DC "Tape loading error"         ;DEFM    "Tape loading erro"
  5822.                                                 ;DB    'r'+$80          ; R
  5823. L1536           EQU $-1
  5824. ;; comma-sp  
  5825. L1537           DC ", "                         ;DB    ',',' '+$80              ; used in report line.
  5826. ;; copyright
  5827. L1539           DB $7F                          ; copyright
  5828.                 DC " 1982 Sinclair Research Ltd"        ;DEFM    " 1982 Sinclair Research Lt"
  5829.                                                         ;DB    'd'+$80
  5830.  
  5831.  
  5832. ; -------------
  5833. ; REPORT-G
  5834. ; -------------
  5835. ; Note ERR_SP points here during line entry which allows the
  5836. ; normal 'Out of Memory' report to be augmented to the more
  5837. ; precise 'No Room for line' report.
  5838.  
  5839. ;; REPORT-G
  5840. ; No Room for line
  5841. L1555:  LD      A,$10           ; i.e. 'G' -$30 -$07
  5842.         LD      BC,$0000        ; this seems unnecessary.
  5843.         JP      L1313           ; jump back to MAIN-G
  5844.  
  5845. ; -----------------------------
  5846. ; Handle addition of BASIC line
  5847. ; -----------------------------
  5848. ; Note this is not a subroutine but a branch of the main execution loop.
  5849. ; System variable ERR_SP still points to editing error handler.
  5850. ; A new line is added to the BASIC program at the appropriate place.
  5851. ; An existing line with same number is deleted first.
  5852. ; Entering an existing line number deletes that line.
  5853. ; Entering a non-existent line allows the subsequent line to be edited next.
  5854.  
  5855. ;; MAIN-ADD
  5856. L155D:  LD      ($5C49),BC      ; set E_PPC to extracted line number.
  5857.         LD      HL,($5C5D)      ; fetch CH_ADD - points to location after the
  5858.                                 ; initial digits (set in E_LINE_NO).
  5859.         EX      DE,HL           ; save start of BASIC in DE.
  5860.  
  5861.         LD      HL,L1555        ; Address: REPORT-G
  5862.         PUSH    HL              ; is pushed on stack and addressed by ERR_SP.
  5863.                                 ; the only error that can occur is
  5864.                                 ; 'Out of memory'.
  5865.  
  5866.         LD      HL,($5C61)      ; fetch WORKSP - end of line.
  5867.         SCF                     ; prepare for true subtraction.
  5868.         SBC     HL,DE           ; find length of BASIC and
  5869.         PUSH    HL              ; save it on stack.
  5870.         LD      H,B             ; transfer line number
  5871.         LD      L,C             ; to HL register.
  5872.         CALL    L196E           ; routine LINE-ADDR will see if
  5873.                                 ; a line with the same number exists.
  5874.         JR      NZ,L157D        ; forward if no existing line to MAIN-ADD1.
  5875.  
  5876.         CALL    L19B8           ; routine NEXT-ONE finds the existing line.
  5877.         CALL    L19E8           ; routine RECLAIM-2 reclaims it.
  5878.  
  5879. ;; MAIN-ADD1
  5880. L157D:  POP     BC              ; retrieve the length of the new line.
  5881.         LD      A,C             ; and test if carriage return only
  5882.         DEC     A               ; i.e. one byte long.
  5883.         OR      B               ; result would be zero.
  5884.         JR      Z,L15AB         ; forward to MAIN-ADD2 is so.
  5885.  
  5886.         PUSH    BC              ; save the length again.
  5887.         INC     BC              ; adjust for inclusion
  5888.         INC     BC              ; of line number (two bytes)
  5889.         INC     BC              ; and line length
  5890.         INC     BC              ; (two bytes).
  5891.         DEC     HL              ; HL points to location before the destination
  5892.  
  5893.         LD      DE,($5C53)      ; fetch the address of PROG
  5894.         PUSH    DE              ; and save it on the stack
  5895.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces in
  5896.                                 ; program area and updates pointers.
  5897.         POP     HL              ; restore old program pointer.
  5898.         LD      ($5C53),HL      ; and put back in PROG as it may have been
  5899.                                 ; altered by the POINTERS routine.
  5900.  
  5901.         POP     BC              ; retrieve BASIC length
  5902.         PUSH    BC              ; and save again.
  5903.  
  5904.         INC     DE              ; points to end of new area.
  5905.         LD      HL,($5C61)      ; set HL to WORKSP - location after edit line.
  5906.         DEC     HL              ; decrement to address end marker.
  5907.         DEC     HL              ; decrement to address carriage return.
  5908.         LDDR                    ; copy the BASIC line back to initial command.
  5909.  
  5910.         LD      HL,($5C49)      ; fetch E_PPC - line number.
  5911.         EX      DE,HL           ; swap it to DE, HL points to last of
  5912.                                 ; four locations.
  5913.         POP     BC              ; retrieve length of line.
  5914.         LD      (HL),B          ; high byte last.
  5915.         DEC     HL              ;
  5916.         LD      (HL),C          ; then low byte of length.
  5917.         DEC     HL              ;
  5918.         LD      (HL),E          ; then low byte of line number.
  5919.         DEC     HL              ;
  5920.         LD      (HL),D          ; then high byte range $0 - $27 (1-9999).
  5921.  
  5922. ;; MAIN-ADD2
  5923. L15AB:  POP     AF              ; drop the address of Report G
  5924.         JP      L12A2           ; and back to MAIN-EXEC producing a listing
  5925.                                 ; and to reset ERR_SP in EDITOR.
  5926.  
  5927.  
  5928. ; ---------------------------
  5929. ; Initial channel information
  5930. ; ---------------------------
  5931. ; This initial channel information is copied from ROM to RAM,
  5932. ; during initialization. It's new location is after the system
  5933. ; variables and is addressed by the system variable CHANS
  5934. ; which means that it can slide up and down in memory.
  5935. ; The table is never searched and the last character which could be anything
  5936. ; other than a comma provides a convenient resting place for DATADD.
  5937.  
  5938. ;; init-chan
  5939. L15AF           DW L09F4                ; PRINT-OUT
  5940.                 DW L10A8                ; KEY-INPUT
  5941.                 DB "K"
  5942.                 DW L09F4                ; PRINT-OUT
  5943.                 DW L15C4                ; REPORT-J
  5944.                 DB "S"
  5945.                 DW L0F81                ; ADD-CHAR
  5946.                 DW L15C4                ; REPORT-J
  5947.                 DB "R"
  5948. ;=======================================
  5949.                 IF AY_PRINTER
  5950.                 DW PRN_TOKEN
  5951.                 ELSE
  5952.                 DW L09F4                ; PRINT-OUT
  5953.                 ENDIF
  5954. ;=======================================
  5955.                 DW L15C4                ; REPORT-J
  5956.                 DB "P"
  5957.  
  5958.         DB    $80             ; End Marker
  5959.  
  5960. ;; REPORT-J
  5961. L15C4:  RST     08H             ; ERROR-1
  5962.         DB    $12             ; Error Report: Invalid I/O device
  5963.  
  5964.  
  5965. ; -------------------
  5966. ; Initial stream data
  5967. ; -------------------
  5968. ; This is the initial stream data for the seven streams $FD - $03 that is
  5969. ; copied from ROM to the STRMS system variables area during initialization.
  5970. ; There are reserved locations there for another 12 streams.
  5971. ; Each location contains an offset to the second byte of a channel.
  5972. ; The first byte of a channel can't be used as that would result in an
  5973. ; offset of zero for some and zero is used to denote that a stream is closed.
  5974.  
  5975. ;; init-strm
  5976. L15C6:  DB    $01, $00        ; stream $FD offset to channel 'K'
  5977.         DB    $06, $00        ; stream $FE offset to channel 'S'
  5978.         DB    $0B, $00        ; stream $FF offset to channel 'R'
  5979.  
  5980.         DB    $01, $00        ; stream $00 offset to channel 'K'
  5981.         DB    $01, $00        ; stream $01 offset to channel 'K'
  5982.         DB    $06, $00        ; stream $02 offset to channel 'S'
  5983.         DB    $10, $00        ; stream $03 offset to channel 'P'
  5984.  
  5985. ; ----------------------------
  5986. ; Control for input subroutine
  5987. ; ----------------------------
  5988. ;
  5989.  
  5990. ;; WAIT-KEY
  5991. L15D4:  BIT     5,(IY+$02)      ; test TV_FLAG - clear lower screen ?
  5992.         JR      NZ,L15DE        ; forward to WAIT-KEY1 if so.
  5993.  
  5994.         SET     3,(IY+$02)      ; update TV_FLAG - signal reprint the edit
  5995.                                 ; line to the lower screen.
  5996.  
  5997. ;; WAIT-KEY1
  5998. L15DE:  CALL    L15E6           ; routine INPUT-AD is called.
  5999.         RET     C               ; return with acceptable keys.
  6000.  
  6001.         JR      Z,L15DE         ; back to WAIT-KEY1 if no key is pressed
  6002.                                 ; or it has been handled within INPUT-AD.
  6003.  
  6004. ; Note. When inputting from the keyboard all characters are returned with
  6005. ; above conditions so this path is never taken.
  6006.  
  6007. ;; REPORT-8
  6008. L15E4:  RST     08H             ; ERROR-1
  6009.         DB    $07             ; Error Report: End of file
  6010.  
  6011. ; ------------------------------
  6012. ; Make HL point to input address
  6013. ; ------------------------------
  6014. ; This routine fetches the address of the input stream from the current
  6015. ; channel area using system variable CURCHL.
  6016.  
  6017. ;; INPUT-AD
  6018. L15E6:  EXX                     ; switch in alternate set.
  6019.         PUSH    HL              ; save HL register
  6020.         LD      HL,($5C51)      ; fetch address of CURCHL - current channel.
  6021.         INC     HL              ; step over output routine
  6022.         INC     HL              ; to point to low byte of input routine.
  6023.         JR      L15F7           ; forward to CALL-SUB.
  6024.  
  6025. ; -------------------
  6026. ; Main Output Routine
  6027. ; -------------------
  6028. ; The entry point OUT-CODE is called on five occasions to print
  6029. ; the ASCII equivalent of a value 0-9.
  6030. ;
  6031. ; PRINT-A-2 is a continuation of the RST 10 to print any character.
  6032. ; Both print to the current channel and the printing of control codes
  6033. ; may alter that channel to divert subsequent RST 10 instructions
  6034. ; to temporary routines. The normal channel is $09F4.
  6035.  
  6036. ;; OUT-CODE
  6037. L15EF:  LD      E,$30           ; add 48 decimal to give ASCII
  6038.         ADD     A,E             ; character '0' to '9'.
  6039.  
  6040. ;; PRINT-A-2
  6041. L15F2:  EXX                     ; switch in alternate set
  6042.         PUSH    HL              ; save HL register
  6043.         LD      HL,($5C51)      ; fetch CURCHL the current channel.
  6044.  
  6045. ; input-ad rejoins here also.
  6046.  
  6047. ;; CALL-SUB
  6048. L15F7:  LD      E,(HL)          ; put the low byte in E.
  6049.         INC     HL              ; advance address.
  6050.         LD      D,(HL)          ; put the high byte to D.
  6051.         EX      DE,HL           ; transfer the stream to HL.
  6052.         CALL    L162C           ; use routine CALL-JUMP.
  6053.                                 ; in effect CALL (HL).
  6054.  
  6055.         POP     HL              ; restore saved HL register.
  6056.         EXX                     ; switch back to the main set and
  6057.         RET                     ; return.
  6058.  
  6059. ; ------------
  6060. ; Open channel
  6061. ; ------------
  6062. ; This subroutine is used by the ROM to open a channel 'K', 'S', 'R' or 'P'.
  6063. ; This is either for its own use or in response to a user's request, for
  6064. ; example, when '#' is encountered with output - PRINT, LIST etc.
  6065. ; or with input - INPUT, INKEY$ etc.
  6066. ; it is entered with a system stream $FD - $FF, or a user stream $00 - $0F
  6067. ; in the accumulator.
  6068.  
  6069. ;; CHAN-OPEN
  6070. L1601:  ADD     A,A             ; double the stream ($FF will become $FE etc.)
  6071.         ADD     A,$16           ; add the offset to stream 0 from $5C00
  6072.         LD      L,A             ; result to L
  6073.         LD      H,$5C           ; now form the address in STRMS area.
  6074.         LD      E,(HL)          ; fetch low byte of CHANS offset
  6075.         INC     HL              ; address next
  6076.         LD      D,(HL)          ; fetch high byte of offset
  6077.         LD      A,D             ; test that the stream is open.
  6078.         OR      E               ; zero if closed.
  6079.         JR      NZ,L1610        ; forward to CHAN-OP-1 if open.
  6080.  
  6081. ;; REPORT-Oa
  6082. L160E:  RST     08H             ; ERROR-1
  6083.         DB    $17             ; Error Report: Invalid stream
  6084.  
  6085. ; continue here if stream was open. Note that the offset is from CHANS
  6086. ; to the second byte of the channel.
  6087.  
  6088. ;; CHAN-OP-1
  6089. L1610:  DEC     DE              ; reduce offset so it points to the channel.
  6090.         LD      HL,($5C4F)      ; fetch CHANS the location of the base of
  6091.                                 ; the channel information area
  6092.         ADD     HL,DE           ; and add the offset to address the channel.
  6093.                                 ; and continue to set flags.
  6094.  
  6095. ; -----------------
  6096. ; Set channel flags
  6097. ; -----------------
  6098. ; This subroutine is used from ED-EDIT, str$ and read-in to reset the
  6099. ; current channel when it has been temporarily altered.
  6100.  
  6101. ;; CHAN-FLAG
  6102. L1615:  LD      ($5C51),HL      ; set CURCHL system variable to the
  6103.                                 ; address in HL
  6104.         RES     4,(IY+$30)      ; update FLAGS2  - signal K channel not in use.
  6105.                                 ; Note. provide a default for channel 'R'.
  6106.         INC     HL              ; advance past
  6107.         INC     HL              ; output routine.
  6108.         INC     HL              ; advance past
  6109.         INC     HL              ; input routine.
  6110.         LD      C,(HL)          ; pick up the letter.
  6111.         LD      HL,L162D        ; address: chn-cd-lu
  6112.         CALL    L16DC           ; routine INDEXER finds offset to a
  6113.                                 ; flag-setting routine.
  6114.  
  6115.         RET     NC              ; but if the letter wasn't found in the
  6116.                                 ; table just return now. - channel 'R'.
  6117.  
  6118.         LD      D,$00           ; prepare to add
  6119.         LD      E,(HL)          ; offset to E
  6120.         ADD     HL,DE           ; add offset to location of offset to form
  6121.                                 ; address of routine
  6122.  
  6123. ;; CALL-JUMP
  6124. L162C:  JP      (HL)            ; jump to the routine
  6125.  
  6126. ; Footnote. calling any location that holds JP (HL) is the equivalent to
  6127. ; a pseudo Z80 instruction CALL (HL). The ROM uses the instruction above.
  6128.  
  6129. ; --------------------------
  6130. ; Channel code look-up table
  6131. ; --------------------------
  6132. ; This table is used by the routine above to find one of the three
  6133. ; flag setting routines below it.
  6134. ; A zero end-marker is required as channel 'R' is not present.
  6135.  
  6136. ;; chn-cd-lu
  6137. L162D:  DB    'K', L1634-$-1  ; offset $06 to CHAN-K
  6138.         DB    'S', L1642-$-1  ; offset $12 to CHAN-S
  6139.         DB    'P', L164D-$-1  ; offset $1B to CHAN-P
  6140.  
  6141.         DB    $00             ; end marker.
  6142.  
  6143. ; --------------
  6144. ; Channel K flag
  6145. ; --------------
  6146. ; routine to set flags for lower screen/keyboard channel.
  6147.  
  6148. ;; CHAN-K
  6149. L1634:  SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6150.         RES     5,(IY+$01)      ; update FLAGS    - signal no new key
  6151.         SET     4,(IY+$30)      ; update FLAGS2   - signal K channel in use
  6152.         JR      L1646           ; forward to CHAN-S-1 for indirect exit
  6153.  
  6154. ; --------------
  6155. ; Channel S flag
  6156. ; --------------
  6157. ; routine to set flags for upper screen channel.
  6158.  
  6159. ;; CHAN-S
  6160. L1642:  RES     0,(IY+$02)      ; TV_FLAG  - signal main screen in use
  6161.  
  6162. ;; CHAN-S-1
  6163. L1646:  RES     1,(IY+$01)      ; update FLAGS  - signal printer not in use
  6164.         JP      L0D4D           ; jump back to TEMPS and exit via that
  6165.                                 ; routine after setting temporary attributes.
  6166. ; --------------
  6167. ; Channel P flag
  6168. ; --------------
  6169. ; This routine sets a flag so that subsequent print related commands
  6170. ; print to printer or update the relevant system variables.
  6171. ; This status remains in force until reset by the routine above.
  6172.  
  6173. ;; CHAN-P
  6174. L164D:  SET     1,(IY+$01)      ; update FLAGS  - signal printer in use
  6175.         RET                     ; return
  6176.  
  6177. ; -----------------------
  6178. ; Just one space required
  6179. ; -----------------------
  6180. ; This routine is called once only to create a single space
  6181. ; in workspace by ADD-CHAR. It is slightly quicker than using a RST $30.
  6182. ; There are several instances in the calculator where the sequence
  6183. ; ld bc, 1; rst $30 could be replaced by a call to this routine but it
  6184. ; only gives a saving of one byte each time.
  6185.  
  6186. ;; ONE-SPACE
  6187. L1652:  LD      BC,$0001        ; create space for a single character.
  6188.  
  6189. ; ---------
  6190. ; Make Room
  6191. ; ---------
  6192. ; This entry point is used to create BC spaces in various areas such as
  6193. ; program area, variables area, workspace etc..
  6194. ; The entire free RAM is available to each BASIC statement.
  6195. ; On entry, HL addresses where the first location is to be created.
  6196. ; Afterwards, HL will point to the location before this.
  6197.  
  6198. ;; MAKE-ROOM
  6199. L1655:  PUSH    HL              ; save the address pointer.
  6200.         CALL    L1F05           ; routine TEST-ROOM checks if room
  6201.                                 ; exists and generates an error if not.
  6202.         POP     HL              ; restore the address pointer.
  6203.         CALL    L1664           ; routine POINTERS updates the
  6204.                                 ; dynamic memory location pointers.
  6205.                                 ; DE now holds the old value of STKEND.
  6206.         LD      HL,($5C65)      ; fetch new STKEND the top destination.
  6207.  
  6208.         EX      DE,HL           ; HL now addresses the top of the area to
  6209.                                 ; be moved up - old STKEND.
  6210.         LDDR                    ; the program, variables, etc are moved up.
  6211.         RET                     ; return with new area ready to be populated.
  6212.                                 ; HL points to location before new area,
  6213.                                 ; and DE to last of new locations.
  6214.  
  6215. ; -----------------------------------------------
  6216. ; Adjust pointers before making or reclaiming room
  6217. ; -----------------------------------------------
  6218. ; This routine is called by MAKE-ROOM to adjust upwards and by RECLAIM to
  6219. ; adjust downwards the pointers within dynamic memory.
  6220. ; The fourteen pointers to dynamic memory, starting with VARS and ending
  6221. ; with STKEND, are updated adding BC if they are higher than the position
  6222. ; in HL.  
  6223. ; The system variables are in no particular order except that STKEND, the first
  6224. ; free location after dynamic memory must be the last encountered.
  6225.  
  6226. ;; POINTERS
  6227. L1664:  PUSH    AF              ; preserve accumulator.
  6228.         PUSH    HL              ; put pos pointer on stack.
  6229.         LD      HL,$5C4B        ; address VARS the first of the
  6230.         LD      A,$0E           ; fourteen variables to consider.
  6231.  
  6232. ;; PTR-NEXT
  6233. L166B:  LD      E,(HL)          ; fetch the low byte of the system variable.
  6234.         INC     HL              ; advance address.
  6235.         LD      D,(HL)          ; fetch high byte of the system variable.
  6236.         EX      (SP),HL         ; swap pointer on stack with the variable
  6237.                                 ; pointer.
  6238.         AND     A               ; prepare to subtract.
  6239.         SBC     HL,DE           ; subtract variable address
  6240.         ADD     HL,DE           ; and add back
  6241.         EX      (SP),HL         ; swap pos with system variable pointer
  6242.         JR      NC,L167F        ; forward to PTR-DONE if var before pos
  6243.  
  6244.         PUSH    DE              ; save system variable address.
  6245.         EX      DE,HL           ; transfer to HL
  6246.         ADD     HL,BC           ; add the offset
  6247.         EX      DE,HL           ; back to DE
  6248.         LD      (HL),D          ; load high byte
  6249.         DEC     HL              ; move back
  6250.         LD      (HL),E          ; load low byte
  6251.         INC     HL              ; advance to high byte
  6252.         POP     DE              ; restore old system variable address.
  6253.  
  6254. ;; PTR-DONE
  6255. L167F:  INC     HL              ; address next system variable.
  6256.         DEC     A               ; decrease counter.
  6257.         JR      NZ,L166B        ; back to PTR-NEXT if more.
  6258.         EX      DE,HL           ; transfer old value of STKEND to HL.
  6259.                                 ; Note. this has always been updated.
  6260.         POP     DE              ; pop the address of the position.
  6261.  
  6262.         POP     AF              ; pop preserved accumulator.
  6263.         AND     A               ; clear carry flag preparing to subtract.
  6264.  
  6265.         SBC     HL,DE           ; subtract position from old stkend
  6266.         LD      B,H             ; to give number of data bytes
  6267.         LD      C,L             ; to be moved.
  6268.         INC     BC              ; increment as we also copy byte at old STKEND.
  6269.         ADD     HL,DE           ; recompute old stkend.
  6270.         EX      DE,HL           ; transfer to DE.
  6271.         RET                     ; return.
  6272.  
  6273.  
  6274.  
  6275. ; -------------------
  6276. ; Collect line number
  6277. ; -------------------
  6278. ; This routine extracts a line number, at an address that has previously
  6279. ; been found using LINE-ADDR, and it is entered at LINE-NO. If it encounters
  6280. ; the program 'end-marker' then the previous line is used and if that
  6281. ; should also be unacceptable then zero is used as it must be a direct
  6282. ; command. The program end-marker is the variables end-marker $80, or
  6283. ; if variables exist, then the first character of any variable name.
  6284.  
  6285. ;; LINE-ZERO
  6286. L168F:  DB    $00, $00        ; dummy line number used for direct commands
  6287.  
  6288.  
  6289. ;; LINE-NO-A
  6290. L1691:  EX      DE,HL           ; fetch the previous line to HL and set
  6291.         LD      DE,$168F        ; DE to LINE-ZERO should HL also fail.
  6292.  
  6293. ; -> The Entry Point.
  6294.  
  6295. ;; LINE-NO
  6296. L1695:  LD      A,(HL)          ; fetch the high byte - max $2F
  6297.         AND     $C0             ; mask off the invalid bits.
  6298.         JR      NZ,L1691        ; to LINE-NO-A if an end-marker.
  6299.  
  6300.         LD      D,(HL)          ; reload the high byte.
  6301.         INC     HL              ; advance address.
  6302.         LD      E,(HL)          ; pick up the low byte.
  6303.         RET                     ; return from here.
  6304.  
  6305. ; -------------------
  6306. ; Handle reserve room
  6307. ; -------------------
  6308. ; This is a continuation of the restart BC-SPACES
  6309.  
  6310. ;; RESERVE
  6311. L169E:  LD      HL,($5C63)      ; STKBOT first location of calculator stack
  6312.         DEC     HL              ; make one less than new location
  6313.         CALL    L1655           ; routine MAKE-ROOM creates the room.
  6314.         INC     HL              ; address the first new location
  6315.         INC     HL              ; advance to second
  6316.         POP     BC              ; restore old WORKSP
  6317.         LD      ($5C61),BC      ; system variable WORKSP was perhaps
  6318.                                 ; changed by POINTERS routine.
  6319.         POP     BC              ; restore count for return value.
  6320.         EX      DE,HL           ; switch. DE = location after first new space
  6321.         INC     HL              ; HL now location after new space
  6322.         RET                     ; return.
  6323.  
  6324. ; ---------------------------
  6325. ; Clear various editing areas
  6326. ; ---------------------------
  6327. ; This routine sets the editing area, workspace and calculator stack
  6328. ; to their minimum configurations as at initialization and indeed this
  6329. ; routine could have been relied on to perform that task.
  6330. ; This routine uses HL only and returns with that register holding
  6331. ; WORKSP/STKBOT/STKEND though no use is made of this. The routines also
  6332. ; reset MEM to its usual place in the systems variable area should it
  6333. ; have been relocated to a FOR-NEXT variable. The main entry point
  6334. ; SET-MIN is called at the start of the MAIN-EXEC loop and prior to
  6335. ; displaying an error.
  6336.  
  6337. ;; SET-MIN
  6338. L16B0:  LD      HL,($5C59)      ; fetch E_LINE
  6339.         LD      (HL),$0D        ; insert carriage return
  6340.         LD      ($5C5B),HL      ; make K_CUR keyboard cursor point there.
  6341.         INC     HL              ; next location
  6342.         LD      (HL),$80        ; holds end-marker $80
  6343.         INC     HL              ; next location becomes
  6344.         LD      ($5C61),HL      ; start of WORKSP
  6345.  
  6346. ; This entry point is used prior to input and prior to the execution,
  6347. ; or parsing, of each statement.
  6348.  
  6349. ;; SET-WORK
  6350. L16BF:  LD      HL,($5C61)      ; fetch WORKSP value
  6351.         LD      ($5C63),HL      ; and place in STKBOT
  6352.  
  6353. ; This entry point is used to move the stack back to its normal place
  6354. ; after temporary relocation during line entry and also from ERROR-3
  6355.  
  6356. ;; SET-STK
  6357. L16C5:  LD      HL,($5C63)      ; fetch STKBOT value
  6358.         LD      ($5C65),HL      ; and place in STKEND.
  6359.  
  6360.         PUSH    HL              ; perhaps an obsolete entry point.
  6361.         LD      HL,$5C92        ; normal location of MEM-0
  6362.         LD      ($5C68),HL      ; is restored to system variable MEM.
  6363.         POP     HL              ; saved value not required.
  6364.         RET                     ; return.
  6365.  
  6366. ; ------------------
  6367. ; Reclaim edit-line?
  6368. ; ------------------
  6369. ; This seems to be legacy code from the ZX80/ZX81 as it is
  6370. ; not used in this ROM.
  6371. ; That task, in fact, is performed here by the dual-area routine CLEAR-SP.
  6372. ; This routine is designed to deal with something that is known to be in the
  6373. ; edit buffer and not workspace.
  6374. ; On entry, HL must point to the end of the something to be deleted.
  6375.  
  6376. ;; REC-EDIT
  6377. L16D4:  LD      DE,($5C59)      ; fetch start of edit line from E_LINE.
  6378.         JP      L19E5           ; jump forward to RECLAIM-1.
  6379.  
  6380. ; --------------------------
  6381. ; The Table INDEXING routine
  6382. ; --------------------------
  6383. ; This routine is used to search two-byte hash tables for a character
  6384. ; held in C, returning the address of the following offset byte.
  6385. ; if it is known that the character is in the table e.g. for priorities,
  6386. ; then the table requires no zero end-marker. If this is not known at the
  6387. ; outset then a zero end-marker is required and carry is set to signal
  6388. ; success.
  6389.  
  6390. ;; INDEXER-1
  6391. L16DB:  INC     HL              ; address the next pair of values.
  6392.  
  6393. ; -> The Entry Point.
  6394.  
  6395. ;; INDEXER
  6396. L16DC:  LD      A,(HL)          ; fetch the first byte of pair
  6397.         AND     A               ; is it the end-marker ?
  6398.         RET     Z               ; return with carry reset if so.
  6399.  
  6400.         CP      C               ; is it the required character ?
  6401.         INC     HL              ; address next location.
  6402.         JR      NZ,L16DB        ; back to INDEXER-1 if no match.
  6403.  
  6404.         SCF                     ; else set the carry flag.
  6405.         RET                     ; return with carry set
  6406.  
  6407. ; --------------------------------
  6408. ; The Channel and Streams Routines
  6409. ; --------------------------------
  6410. ; A channel is an input/output route to a hardware device
  6411. ; and is identified to the system by a single letter e.g. 'K' for
  6412. ; the keyboard. A channel can have an input and output route
  6413. ; associated with it in which case it is bi-directional like
  6414. ; the keyboard. Others like the upper screen 'S' are output
  6415. ; only and the input routine usually points to a report message.
  6416. ; Channels 'K' and 'S' are system channels and it would be inappropriate
  6417. ; to close the associated streams so a mechanism is provided to
  6418. ; re-attach them. When the re-attachment is no longer required, then
  6419. ; closing these streams resets them as at initialization.
  6420. ; The same also would have applied to channel 'R', the RS232 channel
  6421. ; as that is used by the system. It's input stream seems to have been
  6422. ; removed and it is not available to the user. However the channel could
  6423. ; not be removed entirely as its output routine was used by the system.
  6424. ; As a result of removing this channel, channel 'P', the printer is
  6425. ; erroneously treated as a system channel.
  6426. ; Ironically the tape streamer is not accessed through streams and
  6427. ; channels.
  6428. ; Early demonstrations of the Spectrum showed a single microdrive being
  6429. ; controlled by this ROM. Adverts also said that the network and RS232
  6430. ; were in this ROM. Channels 'M' and 'N' are user channels and have been
  6431. ; removed successfully if, as seems vaguely possible, they existed.
  6432.  
  6433. ; ---------------------
  6434. ; Handle CLOSE# command
  6435. ; ---------------------
  6436. ; This command allows streams to be closed after use.
  6437. ; Any temporary memory areas used by the stream would be reclaimed and
  6438. ; finally flags set or reset if necessary.
  6439.  
  6440. ;; CLOSE
  6441. L16E5:  CALL    L171E           ; routine STR-DATA fetches parameter
  6442.                                 ; from calculator stack and gets the
  6443.                                 ; existing STRMS data pointer address in HL
  6444.                                 ; and stream offset from CHANS in BC.
  6445.  
  6446.                                 ; Note. this offset could be zero if the
  6447.                                 ; stream is already closed. A check for this
  6448.                                 ; should occur now and an error should be
  6449.                                 ; generated, for example,
  6450.                                 ; Report S 'Stream already closed'.
  6451.  
  6452.         CALL    L1701           ; routine CLOSE-2 would perform any actions
  6453.                                 ; peculiar to that stream without disturbing
  6454.                                 ; data pointer to STRMS entry in HL.
  6455.  
  6456.         LD      BC,$0000        ; the stream is to be blanked.
  6457.         LD      DE,$A3E2        ; the number of bytes from stream 4, $5C1E,
  6458.                                 ; to $10000
  6459.         EX      DE,HL           ; transfer offset to HL, STRMS data pointer
  6460.                                 ; to DE.
  6461.         ADD     HL,DE           ; add the offset to the data pointer.  
  6462.         JR      C,L16FC         ; forward to CLOSE-1 if a non-system stream.
  6463.                                 ; i.e. higher than 3.
  6464.  
  6465. ; proceed with a negative result.
  6466.  
  6467.         LD      BC,L15C6 + 14   ; prepare the address of the byte after
  6468.                                 ; the initial stream data in ROM. ($15D4)
  6469.         ADD     HL,BC           ; index into the data table with negative value.
  6470.         LD      C,(HL)          ; low byte to C
  6471.         INC     HL              ; address next.
  6472.         LD      B,(HL)          ; high byte to B.
  6473.  
  6474. ; and for streams 0 - 3 just enter the initial data back into the STRMS entry
  6475. ; streams 0 - 2 can't be closed as they are shared by the operating system.
  6476. ; -> for streams 4 - 15 then blank the entry.
  6477.  
  6478. ;; CLOSE-1
  6479. L16FC:  EX      DE,HL           ; address of stream to HL.
  6480.         LD      (HL),C          ; place zero (or low byte).
  6481.         INC     HL              ; next address.
  6482.         LD      (HL),B          ; place zero (or high byte).
  6483.         RET                     ; return.
  6484.  
  6485. ; ------------------
  6486. ; CLOSE-2 Subroutine
  6487. ; ------------------
  6488. ; There is not much point in coming here.
  6489. ; The purpose was once to find the offset to a special closing routine,
  6490. ; in this ROM and within 256 bytes of the close stream look up table that
  6491. ; would reclaim any buffers associated with a stream. At least one has been
  6492. ; removed.
  6493.  
  6494. ;; CLOSE-2
  6495. L1701:  PUSH    HL              ; * save address of stream data pointer
  6496.                                 ; in STRMS on the machine stack.
  6497.         LD      HL,($5C4F)      ; fetch CHANS address to HL
  6498.         ADD     HL,BC           ; add the offset to address the second
  6499.                                 ; byte of the output routine hopefully.
  6500.         INC     HL              ; step past
  6501.         INC     HL              ; the input routine.
  6502.         INC     HL              ; to address channel's letter
  6503.         LD      C,(HL)          ; pick it up in C.
  6504.                                 ; Note. but if stream is already closed we
  6505.                                 ; get the value $10 (the byte preceding 'K').
  6506.         EX      DE,HL           ; save the pointer to the letter in DE.
  6507.         LD      HL,L1716        ; address: cl-str-lu in ROM.
  6508.         CALL    L16DC           ; routine INDEXER uses the code to get
  6509.                                 ; the 8-bit offset from the current point to
  6510.                                 ; the address of the closing routine in ROM.
  6511.                                 ; Note. it won't find $10 there!
  6512.         LD      C,(HL)          ; transfer the offset to C.
  6513.         LD      B,$00           ; prepare to add.
  6514.         ADD     HL,BC           ; add offset to point to the address of the
  6515.                                 ; routine that closes the stream.
  6516.                                 ; (and presumably removes any buffers that
  6517.                                 ; are associated with it.)
  6518.         JP      (HL)            ; jump to that routine.
  6519.  
  6520. ; --------------------------
  6521. ; CLOSE stream look-up table
  6522. ; --------------------------
  6523. ; This table contains an entry for a letter found in the CHANS area.
  6524. ; followed by an 8-bit displacement, from that byte's address in the
  6525. ; table to the routine that performs any ancillary actions associated
  6526. ; with closing the stream of that channel.
  6527. ; The table doesn't require a zero end-marker as the letter has been
  6528. ; picked up from a channel that has an open stream.
  6529.  
  6530. ;; cl-str-lu
  6531. L1716:  DB    'K', L171C-$-1  ; offset 5 to CLOSE-STR
  6532.         DB    'S', L171C-$-1  ; offset 3 to CLOSE-STR
  6533.         DB    'P', L171C-$-1  ; offset 1 to CLOSE-STR
  6534.  
  6535.  
  6536. ; ------------------------
  6537. ; Close Stream Subroutines
  6538. ; ------------------------
  6539. ; The close stream routines in fact have no ancillary actions to perform
  6540. ; which is not surprising with regard to 'K' and 'S'.
  6541.  
  6542. ;; CLOSE-STR                    
  6543. L171C:  POP     HL              ; * now just restore the stream data pointer
  6544.         RET                     ; in STRMS and return.
  6545.  
  6546. ; -----------
  6547. ; Stream data
  6548. ; -----------
  6549. ; This routine finds the data entry in the STRMS area for the specified
  6550. ; stream which is passed on the calculator stack. It returns with HL
  6551. ; pointing to this system variable and BC holding a displacement from
  6552. ; the CHANS area to the second byte of the stream's channel. If BC holds
  6553. ; zero, then that signifies that the stream is closed.
  6554.  
  6555. ;; STR-DATA
  6556. L171E:  CALL    L1E94           ; routine FIND-INT1 fetches parameter to A
  6557.         CP      $10             ; is it less than 16d ?
  6558.         JR      C,L1727         ; skip forward to STR-DATA1 if so.
  6559.  
  6560. ;; REPORT-Ob
  6561. L1725:  RST     08H             ; ERROR-1
  6562.         DB    $17             ; Error Report: Invalid stream
  6563.  
  6564. ;; STR-DATA1
  6565. L1727:  ADD     A,$03           ; add the offset for 3 system streams.
  6566.                                 ; range 00 - 15d becomes 3 - 18d.
  6567.         RLCA                    ; double as there are two bytes per
  6568.                                 ; stream - now 06 - 36d
  6569.         LD      HL,$5C10        ; address STRMS - the start of the streams
  6570.                                 ; data area in system variables.
  6571.         LD      C,A             ; transfer the low byte to A.
  6572.         LD      B,$00           ; prepare to add offset.
  6573.         ADD     HL,BC           ; add to address the data entry in STRMS.
  6574.  
  6575. ; the data entry itself contains an offset from CHANS to the address of the
  6576. ; stream
  6577.  
  6578.         LD      C,(HL)          ; low byte of displacement to C.
  6579.         INC     HL              ; address next.
  6580.         LD      B,(HL)          ; high byte of displacement to B.
  6581.         DEC     HL              ; step back to leave HL pointing to STRMS
  6582.                                 ; data entry.
  6583.         RET                     ; return with CHANS displacement in BC
  6584.                                 ; and address of stream data entry in HL.
  6585.  
  6586. ; --------------------
  6587. ; Handle OPEN# command
  6588. ; --------------------
  6589. ; Command syntax example: OPEN #5,"s"
  6590. ; On entry the channel code entry is on the calculator stack with the next
  6591. ; value containing the stream identifier. They have to swapped.
  6592.  
  6593. ;; OPEN
  6594. L1736:  RST     28H             ;; FP-CALC    ;s,c.
  6595.         DB    $01             ;;exchange    ;c,s.
  6596.         DB    $38             ;;end-calc
  6597.  
  6598.         CALL    L171E           ; routine STR-DATA fetches the stream off
  6599.                                 ; the stack and returns with the CHANS
  6600.                                 ; displacement in BC and HL addressing
  6601.                                 ; the STRMS data entry.
  6602.         LD      A,B             ; test for zero which
  6603.         OR      C               ; indicates the stream is closed.
  6604.         JR      Z,L1756         ; skip forward to OPEN-1 if so.
  6605.  
  6606. ; if it is a system channel then it can re-attached.
  6607.  
  6608.         EX      DE,HL           ; save STRMS address in DE.
  6609.         LD      HL,($5C4F)      ; fetch CHANS.
  6610.         ADD     HL,BC           ; add the offset to address the second
  6611.                                 ; byte of the channel.
  6612.         INC     HL              ; skip over the
  6613.         INC     HL              ; input routine.
  6614.         INC     HL              ; and address the letter.
  6615.         LD      A,(HL)          ; pick up the letter.
  6616.         EX      DE,HL           ; save letter pointer and bring back
  6617.                                 ; the STRMS pointer.
  6618.  
  6619.         CP      $4B             ; is it 'K' ?
  6620.         JR      Z,L1756         ; forward to OPEN-1 if so
  6621.  
  6622.         CP      $53             ; is it 'S' ?
  6623.         JR      Z,L1756         ; forward to OPEN-1 if so
  6624.  
  6625.         CP      $50             ; is it 'P' ?
  6626.         JR      NZ,L1725        ; back to REPORT-Ob if not.
  6627.                                 ; to report 'Invalid stream'.
  6628.  
  6629. ; continue if one of the upper-case letters was found.
  6630. ; and rejoin here from above if stream was closed.
  6631.  
  6632. ;; OPEN-1
  6633. L1756:  CALL    L175D           ; routine OPEN-2 opens the stream.
  6634.  
  6635. ; it now remains to update the STRMS variable.
  6636.  
  6637.         LD      (HL),E          ; insert or overwrite the low byte.
  6638.         INC     HL              ; address high byte in STRMS.
  6639.         LD      (HL),D          ; insert or overwrite the high byte.
  6640.         RET                     ; return.
  6641.  
  6642. ; -----------------
  6643. ; OPEN-2 Subroutine
  6644. ; -----------------
  6645. ; There is some point in coming here as, as well as once creating buffers,
  6646. ; this routine also sets flags.
  6647.  
  6648. ;; OPEN-2
  6649. L175D:  PUSH    HL              ; * save the STRMS data entry pointer.
  6650.         CALL    L2BF1           ; routine STK-FETCH now fetches the
  6651.                                 ; parameters of the channel string.
  6652.                                 ; start in DE, length in BC.
  6653.  
  6654.         LD      A,B             ; test that it is not
  6655.         OR      C               ; the null string.
  6656.         JR      NZ,L1767        ; skip forward to OPEN-3 with 1 character
  6657.                                 ; or more!
  6658.  
  6659. ;; REPORT-Fb
  6660. L1765:  RST     08H             ; ERROR-1
  6661.         DB    $0E             ; Error Report: Invalid file name
  6662.  
  6663. ;; OPEN-3
  6664. L1767:  PUSH    BC              ; save the length of the string.
  6665.         LD      A,(DE)          ; pick up the first character.
  6666.                                 ; Note. if the second character is used to
  6667.                                 ; distinguish between a binary or text
  6668.                                 ; channel then it will be simply a matter
  6669.                                 ; of setting bit 7 of FLAGX.
  6670.         AND     $DF             ; make it upper-case.
  6671.         LD      C,A             ; place it in C.
  6672.         LD      HL,L177A        ; address: op-str-lu is loaded.
  6673.         CALL    L16DC           ; routine INDEXER will search for letter.
  6674.         JR      NC,L1765        ; back to REPORT-F if not found
  6675.                                 ; 'Invalid filename'
  6676.  
  6677.         LD      C,(HL)          ; fetch the displacement to opening routine.
  6678.         LD      B,$00           ; prepare to add.
  6679.         ADD     HL,BC           ; now form address of opening routine.
  6680.         POP     BC              ; restore the length of string.
  6681.         JP      (HL)            ; now jump forward to the relevant routine.
  6682.  
  6683. ; -------------------------
  6684. ; OPEN stream look-up table
  6685. ; -------------------------
  6686. ; The open stream look-up table consists of matched pairs.
  6687. ; The channel letter is followed by an 8-bit displacement to the
  6688. ; associated stream-opening routine in this ROM.
  6689. ; The table requires a zero end-marker as the letter has been
  6690. ; provided by the user and not the operating system.
  6691.  
  6692. ;; op-str-lu
  6693. L177A:  DB    'K', L1781-$-1  ; $06 offset to OPEN-K
  6694.         DB    'S', L1785-$-1  ; $08 offset to OPEN-S
  6695.         DB    'P', L1789-$-1  ; $0A offset to OPEN-P
  6696.  
  6697.         DB    $00             ; end-marker.
  6698.  
  6699. ; ----------------------------
  6700. ; The Stream Opening Routines.
  6701. ; ----------------------------
  6702. ; These routines would have opened any buffers associated with the stream
  6703. ; before jumping forward to to OPEN-END with the displacement value in E
  6704. ; and perhaps a modified value in BC. The strange pathing does seem to
  6705. ; provide for flexibility in this respect.
  6706. ;
  6707. ; There is no need to open the printer buffer as it is there already
  6708. ; even if you are still saving up for a ZX Printer or have moved onto
  6709. ; something bigger. In any case it would have to be created after
  6710. ; the system variables but apart from that it is a simple task
  6711. ; and all but one of the ROM routines can handle a buffer in that position.
  6712. ; (PR-ALL-6 would require an extra 3 bytes of code).
  6713. ; However it wouldn't be wise to have two streams attached to the ZX Printer
  6714. ; as you can now, so one assumes that if PR_CC_hi was non-zero then
  6715. ; the OPEN-P routine would have refused to attach a stream if another
  6716. ; stream was attached.
  6717.  
  6718. ; Something of significance is being passed to these ghost routines in the
  6719. ; second character. Strings 'RB', 'RT' perhaps or a drive/station number.
  6720. ; The routine would have to deal with that and exit to OPEN_END with BC
  6721. ; containing $0001 or more likely there would be an exit within the routine.
  6722. ; Anyway doesn't matter, these routines are long gone.
  6723.  
  6724. ; -----------------
  6725. ; OPEN-K Subroutine
  6726. ; -----------------
  6727. ; Open Keyboard stream.
  6728.  
  6729. ;; OPEN-K
  6730. L1781:  LD      E,$01           ; 01 is offset to second byte of channel 'K'.
  6731.         JR      L178B           ; forward to OPEN-END
  6732.  
  6733. ; -----------------
  6734. ; OPEN-S Subroutine
  6735. ; -----------------
  6736. ; Open Screen stream.
  6737.  
  6738. ;; OPEN-S
  6739. L1785:  LD      E,$06           ; 06 is offset to 2nd byte of channel 'S'
  6740.         JR      L178B           ; to OPEN-END
  6741.  
  6742. ; -----------------
  6743. ; OPEN-P Subroutine
  6744. ; -----------------
  6745. ; Open Printer stream.
  6746.  
  6747. ;; OPEN-P
  6748. L1789:  LD      E,$10           ; 16d is offset to 2nd byte of channel 'P'
  6749.  
  6750. ;; OPEN-END
  6751. L178B:  DEC     BC              ; the stored length of 'K','S','P' or
  6752.                                 ; whatever is now tested. ??
  6753.         LD      A,B             ; test now if initial or residual length
  6754.         OR      C               ; is one character.
  6755.         JR      NZ,L1765        ; to REPORT-Fb 'Invalid file name' if not.
  6756.  
  6757.         LD      D,A             ; load D with zero to form the displacement
  6758.                                 ; in the DE register.
  6759.         POP     HL              ; * restore the saved STRMS pointer.
  6760.         RET                     ; return to update STRMS entry thereby
  6761.                                 ; signaling stream is open.
  6762.  
  6763. ; ----------------------------------------
  6764. ; Handle CAT, ERASE, FORMAT, MOVE commands
  6765. ; ----------------------------------------
  6766. ; These just generate an error report as the ROM is 'incomplete'.
  6767. ;
  6768. ; Luckily this provides a mechanism for extending these in a shadow ROM
  6769. ; but without the powerful mechanisms set up in this ROM.
  6770. ; An instruction fetch on $0008 may page in a peripheral ROM,
  6771. ; e.g. the Sinclair Interface 1 ROM, to handle these commands.
  6772. ; However that wasn't the plan.
  6773. ; Development of this ROM continued for another three months until the cost
  6774. ; of replacing it and the manual became unfeasible.
  6775. ; The ultimate power of channels and streams died at birth.
  6776.  
  6777. ;; CAT-ETC
  6778. L1793:  JR      L1725           ; to REPORT-Ob
  6779.  
  6780. ; -----------------
  6781. ; Perform AUTO-LIST
  6782. ; -----------------
  6783. ; This produces an automatic listing in the upper screen.
  6784.  
  6785. ;; AUTO-LIST
  6786. L1795:  LD      ($5C3F),SP      ; save stack pointer in LIST_SP
  6787.         LD      (IY+$02),$10    ; update TV_FLAG set bit 3
  6788.         CALL    L0DAF           ; routine CL-ALL.
  6789.         SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6790.  
  6791.         LD      B,(IY+$31)      ; fetch DF_SZ to B.
  6792.         CALL    L0E44           ; routine CL-LINE clears lower display
  6793.                                 ; preserving B.
  6794.         RES     0,(IY+$02)      ; update TV_FLAG  - signal main screen in use
  6795.         SET     0,(IY+$30)      ; update FLAGS2  - signal unnecessary to
  6796.                                 ; clear main screen.
  6797.         LD      HL,($5C49)      ; fetch E_PPC current edit line to HL.
  6798.         LD      DE,($5C6C)      ; fetch S_TOP to DE, the current top line
  6799.                                 ; (initially zero)
  6800.         AND     A               ; prepare for true subtraction.
  6801.         SBC     HL,DE           ; subtract and
  6802.         ADD     HL,DE           ; add back.
  6803.         JR      C,L17E1         ; to AUTO-L-2 if S_TOP higher than E_PPC
  6804.                                 ; to set S_TOP to E_PPC
  6805.  
  6806.         PUSH    DE              ; save the top line number.
  6807.         CALL    L196E           ; routine LINE-ADDR gets address of E_PPC.
  6808.         LD      DE,$02C0        ; prepare known number of characters in
  6809.                                 ; the default upper screen.
  6810.         EX      DE,HL           ; offset to HL, program address to DE.
  6811.         SBC     HL,DE           ; subtract high value from low to obtain
  6812.                                 ; negated result used in addition.
  6813.         EX      (SP),HL         ; swap result with top line number on stack.
  6814.         CALL    L196E           ; routine LINE-ADDR  gets address of that
  6815.                                 ; top line in HL and next line in DE.
  6816.         POP     BC              ; restore the result to balance stack.
  6817.  
  6818. ;; AUTO-L-1
  6819. L17CE:  PUSH    BC              ; save the result.
  6820.         CALL    L19B8           ; routine NEXT-ONE gets address in HL of
  6821.                                 ; line after auto-line (in DE).
  6822.         POP     BC              ; restore result.
  6823.         ADD     HL,BC           ; compute back.
  6824.         JR      C,L17E4         ; to AUTO-L-3 if line 'should' appear
  6825.  
  6826.         EX      DE,HL           ; address of next line to HL.
  6827.         LD      D,(HL)          ; get line
  6828.         INC     HL              ; number
  6829.         LD      E,(HL)          ; in DE.
  6830.         DEC     HL              ; adjust back to start.
  6831.         LD      ($5C6C),DE      ; update S_TOP.
  6832.         JR      L17CE           ; to AUTO-L-1 until estimate reached.
  6833.  
  6834. ; ---
  6835.  
  6836. ; the jump was to here if S_TOP was greater than E_PPC
  6837.  
  6838. ;; AUTO-L-2
  6839. L17E1:  LD      ($5C6C),HL      ; make S_TOP the same as E_PPC.
  6840.  
  6841. ; continue here with valid starting point from above or good estimate
  6842. ; from computation
  6843.  
  6844. ;; AUTO-L-3
  6845. L17E4:  LD      HL,($5C6C)      ; fetch S_TOP line number to HL.
  6846.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  6847.                                 ; address of next in DE.
  6848.         JR      Z,L17ED         ; to AUTO-L-4 if line exists.
  6849.  
  6850.         EX      DE,HL           ; else use address of next line.
  6851.  
  6852. ;; AUTO-L-4
  6853. L17ED:  CALL    L1833           ; routine LIST-ALL                >>>
  6854.  
  6855. ; The return will be to here if no scrolling occurred
  6856.  
  6857.         RES     4,(IY+$02)      ; update TV_FLAG  - signal no auto listing.
  6858.         RET                     ; return.
  6859.  
  6860. ; ------------
  6861. ; Handle LLIST
  6862. ; ------------
  6863. ; A short form of LIST #3. The listing goes to stream 3 - default printer.
  6864.  
  6865. ;; LLIST
  6866. L17F5:  LD      A,$03           ; the usual stream for ZX Printer
  6867.         JR      L17FB           ; forward to LIST-1
  6868.  
  6869. ; -----------
  6870. ; Handle LIST
  6871. ; -----------
  6872. ; List to any stream.
  6873. ; Note. While a starting line can be specified it is
  6874. ; not possible to specify an end line.
  6875. ; Just listing a line makes it the current edit line.
  6876.  
  6877. ;; LIST
  6878. L17F9:  LD      A,$02           ; default is stream 2 - the upper screen.
  6879.  
  6880. ;; LIST-1
  6881. L17FB:  LD      (IY+$02),$00    ; the TV_FLAG is initialized with bit 0 reset
  6882.                                 ; indicating upper screen in use.
  6883.         CALL    L2530           ; routine SYNTAX-Z - checking syntax ?
  6884.         CALL    NZ,L1601        ; routine CHAN-OPEN if in run-time.
  6885.  
  6886.         RST     18H             ; GET-CHAR
  6887.         CALL    L2070           ; routine STR-ALTER will alter if '#'.
  6888.         JR      C,L181F         ; forward to LIST-4 not a '#' .
  6889.  
  6890.  
  6891.         RST     18H             ; GET-CHAR
  6892.         CP      $3B             ; is it ';' ?
  6893.         JR      Z,L1814         ; skip to LIST-2 if so.
  6894.  
  6895.         CP      $2C             ; is it ',' ?
  6896.         JR      NZ,L181A        ; forward to LIST-3 if neither separator.
  6897.  
  6898. ; we have, say,  LIST #15, and a number must follow the separator.
  6899.  
  6900. ;; LIST-2
  6901. L1814:  RST     20H             ; NEXT-CHAR
  6902.         CALL    L1C82           ; routine EXPT-1NUM
  6903.         JR      L1822           ; forward to LIST-5
  6904.  
  6905. ; ---
  6906.  
  6907. ; the branch was here with just LIST #3 etc.
  6908.  
  6909. ;; LIST-3
  6910. L181A:  CALL    L1CE6           ; routine USE-ZERO
  6911.         JR      L1822           ; forward to LIST-5
  6912.  
  6913. ; ---
  6914.  
  6915. ; the branch was here with LIST
  6916.  
  6917. ;; LIST-4
  6918. L181F:  CALL    L1CDE           ; routine FETCH-NUM checks if a number
  6919.                                 ; follows else uses zero.
  6920.  
  6921. ;; LIST-5
  6922. L1822:  CALL    L1BEE           ; routine CHECK-END quits if syntax OK >>>
  6923.  
  6924.         CALL    L1E99           ; routine FIND-INT2 fetches the number
  6925.                                 ; from the calculator stack in run-time.
  6926.         LD      A,B             ; fetch high byte of line number and
  6927.         AND     $3F             ; make less than $40 so that NEXT-ONE
  6928.                                 ; (from LINE-ADDR) doesn't lose context.
  6929.                                 ; Note. this is not satisfactory and the typo
  6930.                                 ; LIST 20000 will list an entirely different
  6931.                                 ; section than LIST 2000. Such typos are not
  6932.                                 ; available for checking if they are direct
  6933.                                 ; commands.
  6934.  
  6935.         LD      H,A             ; transfer the modified
  6936.         LD      L,C             ; line number to HL.
  6937.         LD      ($5C49),HL      ; update E_PPC to new line number.
  6938.         CALL    L196E           ; routine LINE-ADDR gets the address of the
  6939.                                 ; line.
  6940.  
  6941. ; This routine is called from AUTO-LIST
  6942.  
  6943. ;; LIST-ALL
  6944. L1833:  LD      E,$01           ; signal current line not yet printed
  6945.  
  6946. ;; LIST-ALL-2
  6947. L1835:  CALL    L1855           ; routine OUT-LINE outputs a BASIC line
  6948.                                 ; using PRINT-OUT and makes an early return
  6949.                                 ; when no more lines to print. >>>
  6950.  
  6951.         RST     10H             ; PRINT-A prints the carriage return (in A)
  6952.  
  6953.         BIT     4,(IY+$02)      ; test TV_FLAG  - automatic listing ?
  6954.         JR      Z,L1835         ; back to LIST-ALL-2 if not
  6955.                                 ; (loop exit is via OUT-LINE)
  6956.  
  6957. ; continue here if an automatic listing required.
  6958.  
  6959.         LD      A,($5C6B)       ; fetch DF_SZ lower display file size.
  6960.         SUB     (IY+$4F)        ; subtract S_POSN_hi ithe current line number.
  6961.         JR      NZ,L1835        ; back to LIST-ALL-2 if upper screen not full.
  6962.  
  6963.         XOR     E               ; A contains zero, E contains one if the
  6964.                                 ; current edit line has not been printed
  6965.                                 ; or zero if it has (from OUT-LINE).
  6966.         RET     Z               ; return if the screen is full and the line
  6967.                                 ; has been printed.
  6968.  
  6969. ; continue with automatic listings if the screen is full and the current
  6970. ; edit line is missing. OUT-LINE will scroll automatically.
  6971.  
  6972.         PUSH    HL              ; save the pointer address.
  6973.         PUSH    DE              ; save the E flag.
  6974.         LD      HL,$5C6C        ; fetch S_TOP the rough estimate.
  6975.         CALL    L190F           ; routine LN-FETCH updates S_TOP with
  6976.                                 ; the number of the next line.
  6977.         POP     DE              ; restore the E flag.
  6978.         POP     HL              ; restore the address of the next line.
  6979.         JR      L1835           ; back to LIST-ALL-2.
  6980.  
  6981. ; ------------------------
  6982. ; Print a whole BASIC line
  6983. ; ------------------------
  6984. ; This routine prints a whole BASIC line and it is called
  6985. ; from LIST-ALL to output the line to current channel
  6986. ; and from ED-EDIT to 'sprint' the line to the edit buffer.
  6987.  
  6988. ;; OUT-LINE
  6989. L1855:  LD      BC,($5C49)      ; fetch E_PPC the current line which may be
  6990.                                 ; unchecked and not exist.
  6991.         CALL    L1980           ; routine CP-LINES finds match or line after.
  6992.         LD      D,$3E           ; prepare cursor '>' in D.
  6993.         JR      Z,L1865         ; to OUT-LINE1 if matched or line after.
  6994.  
  6995.         LD      DE,$0000        ; put zero in D, to suppress line cursor.
  6996.         RL      E               ; pick up carry in E if line before current
  6997.                                 ; leave E zero if same or after.
  6998.  
  6999. ;; OUT-LINE1
  7000. L1865:  LD      (IY+$2D),E      ; save flag in BREG which is spare.
  7001.         LD      A,(HL)          ; get high byte of line number.
  7002.         CP      $40             ; is it too high ($2F is maximum possible) ?
  7003.         POP     BC              ; drop the return address and
  7004.         RET     NC              ; make an early return if so >>>
  7005.  
  7006.         PUSH    BC              ; save return address
  7007.         CALL    L1A28           ; routine OUT-NUM-2 to print addressed number
  7008.                                 ; with leading space.
  7009.         INC     HL              ; skip low number byte.
  7010.         INC     HL              ; and the two
  7011.         INC     HL              ; length bytes.
  7012.         RES     0,(IY+$01)      ; update FLAGS - signal leading space required.
  7013.         LD      A,D             ; fetch the cursor.
  7014.         AND     A               ; test for zero.
  7015.         JR      Z,L1881         ; to OUT-LINE3 if zero.
  7016.  
  7017.  
  7018.         RST     10H             ; PRINT-A prints '>' the current line cursor.
  7019.  
  7020. ; this entry point is called from ED-COPY
  7021.  
  7022. ;; OUT-LINE2
  7023. L187D:  SET     0,(IY+$01)      ; update FLAGS - suppress leading space.
  7024.  
  7025. ;; OUT-LINE3
  7026. L1881:  PUSH    DE              ; save flag E for a return value.
  7027.         EX      DE,HL           ; save HL address in DE.
  7028.         RES     2,(IY+$30)      ; update FLAGS2 - signal NOT in QUOTES.
  7029.  
  7030.         LD      HL,$5C3B        ; point to FLAGS.
  7031.         RES     2,(HL)          ; signal 'K' mode. (starts before keyword)
  7032.         BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7033.         JR      Z,L1894         ; forward to OUT-LINE4 if not.
  7034.  
  7035.         SET     2,(HL)          ; signal 'L' mode. (used for input)
  7036.  
  7037. ;; OUT-LINE4
  7038. L1894:  LD      HL,($5C5F)      ; fetch X_PTR - possibly the error pointer
  7039.                                 ; address.
  7040.         AND     A               ; clear the carry flag.
  7041.         SBC     HL,DE           ; test if an error address has been reached.
  7042.         JR      NZ,L18A1        ; forward to OUT-LINE5 if not.
  7043.  
  7044.         LD      A,$3F           ; load A with '?' the error marker.
  7045.         CALL    L18C1           ; routine OUT-FLASH to print flashing marker.
  7046.  
  7047. ;; OUT-LINE5
  7048. L18A1:  CALL    L18E1           ; routine OUT-CURS will print the cursor if
  7049.                                 ; this is the right position.
  7050.         EX      DE,HL           ; restore address pointer to HL.
  7051.         LD      A,(HL)          ; fetch the addressed character.
  7052.         CALL    L18B6           ; routine NUMBER skips a hidden floating
  7053.                                 ; point number if present.
  7054.         INC     HL              ; now increment the pointer.
  7055.         CP      $0D             ; is character end-of-line ?
  7056.         JR      Z,L18B4         ; to OUT-LINE6, if so, as line is finished.
  7057.  
  7058.         EX      DE,HL           ; save the pointer in DE.
  7059.         CALL    L1937           ; routine OUT-CHAR to output character/token.
  7060.  
  7061.         JR      L1894           ; back to OUT-LINE4 until entire line is done.
  7062.  
  7063. ; ---
  7064.  
  7065. ;; OUT-LINE6
  7066. L18B4:  POP     DE              ; bring back the flag E, zero if current
  7067.                                 ; line printed else 1 if still to print.
  7068.         RET                     ; return with A holding $0D
  7069.  
  7070. ; -------------------------
  7071. ; Check for a number marker
  7072. ; -------------------------
  7073. ; this subroutine is called from two processes. while outputting BASIC lines
  7074. ; and while searching statements within a BASIC line.
  7075. ; during both, this routine will pass over an invisible number indicator
  7076. ; and the five bytes floating-point number that follows it.
  7077. ; Note that this causes floating point numbers to be stripped from
  7078. ; the BASIC line when it is fetched to the edit buffer by OUT_LINE.
  7079. ; the number marker also appears after the arguments of a DEF FN statement
  7080. ; and may mask old 5-byte string parameters.
  7081.  
  7082. ;; NUMBER
  7083. L18B6:  CP      $0E             ; character fourteen ?
  7084.         RET     NZ              ; return if not.
  7085.  
  7086.         INC     HL              ; skip the character
  7087.         INC     HL              ; and five bytes
  7088.         INC     HL              ; following.
  7089.         INC     HL              ;
  7090.         INC     HL              ;
  7091.         INC     HL              ;
  7092.         LD      A,(HL)          ; fetch the following character
  7093.         RET                     ; for return value.
  7094.  
  7095. ; --------------------------
  7096. ; Print a flashing character
  7097. ; --------------------------
  7098. ; This subroutine is called from OUT-LINE to print a flashing error
  7099. ; marker '?' or from the next routine to print a flashing cursor e.g. 'L'.
  7100. ; However, this only gets called from OUT-LINE when printing the edit line
  7101. ; or the input buffer to the lower screen so a direct call to $09F4 can
  7102. ; be used, even though out-line outputs to other streams.
  7103. ; In fact the alternate set is used for the whole routine.
  7104.  
  7105. ;; OUT-FLASH
  7106. L18C1:  EXX                     ; switch in alternate set
  7107.  
  7108.         LD      HL,($5C8F)      ; fetch L = ATTR_T, H = MASK-T
  7109.         PUSH    HL              ; save masks.
  7110.         RES     7,H             ; reset flash mask bit so active.
  7111.         SET     7,L             ; make attribute FLASH.
  7112.         LD      ($5C8F),HL      ; resave ATTR_T and MASK-T
  7113.  
  7114.         LD      HL,$5C91        ; address P_FLAG
  7115.         LD      D,(HL)          ; fetch to D
  7116.         PUSH    DE              ; and save.
  7117.         LD      (HL),$00        ; clear inverse, over, ink/paper 9
  7118.  
  7119.         CALL    L09F4           ; routine PRINT-OUT outputs character
  7120.                                 ; without the need to vector via RST 10.
  7121.  
  7122.         POP     HL              ; pop P_FLAG to H.
  7123.         LD      (IY+$57),H      ; and restore system variable P_FLAG.
  7124.         POP     HL              ; restore temporary masks
  7125.         LD      ($5C8F),HL      ; and restore system variables ATTR_T/MASK_T
  7126.  
  7127.         EXX                     ; switch back to main set
  7128.         RET                     ; return
  7129.  
  7130. ; ----------------
  7131. ; Print the cursor
  7132. ; ----------------
  7133. ; This routine is called before any character is output while outputting
  7134. ; a BASIC line or the input buffer. This includes listing to a printer
  7135. ; or screen, copying a BASIC line to the edit buffer and printing the
  7136. ; input buffer or edit buffer to the lower screen. It is only in the
  7137. ; latter two cases that it has any relevance and in the last case it
  7138. ; performs another very important function also.
  7139.  
  7140. ;; OUT-CURS
  7141. L18E1:  LD      HL,($5C5B)      ; fetch K_CUR the current cursor address
  7142.         AND     A               ; prepare for true subtraction.
  7143.         SBC     HL,DE           ; test against pointer address in DE and
  7144.         RET     NZ              ; return if not at exact position.
  7145.  
  7146. ; the value of MODE, maintained by KEY-INPUT, is tested and if non-zero
  7147. ; then this value 'E' or 'G' will take precedence.
  7148.  
  7149.         LD      A,($5C41)       ; fetch MODE  0='KLC', 1='E', 2='G'.
  7150.                 DB 0XCB
  7151.                 RLCA               ; double the value and set flags.
  7152.         JR      Z,L18F3         ; to OUT-C-1 if still zero ('KLC').
  7153.  
  7154.         ADD     A,$43           ; add 'C' - will become 'E' if originally 1
  7155.                                 ; or 'G' if originally 2.
  7156.         JR      L1909           ; forward to OUT-C-2 to print.
  7157.  
  7158. ; ---
  7159.  
  7160. ; If mode was zero then, while printing a BASIC line, bit 2 of flags has been
  7161. ; set if 'THEN' or ':' was encountered as a main character and reset otherwise.
  7162. ; This is now used to determine if the 'K' cursor is to be printed but this
  7163. ; transient state is also now transferred permanently to bit 3 of FLAGS
  7164. ; to let the interrupt routine know how to decode the next key.
  7165.  
  7166. ;; OUT-C-1
  7167. L18F3:  LD      HL,$5C3B        ; Address FLAGS
  7168.         RES     3,(HL)          ; signal 'K' mode initially.
  7169.         LD      A,$4B           ; prepare letter 'K'.
  7170.         BIT     2,(HL)          ; test FLAGS - was the
  7171.                                 ; previous main character ':' or 'THEN' ?
  7172.         JR      Z,L1909         ; forward to OUT-C-2 if so to print.
  7173.  
  7174.         SET     3,(HL)          ; signal 'L' mode to interrupt routine.
  7175.                                 ; Note. transient bit has been made permanent.
  7176.         INC     A               ; augment from 'K' to 'L'.
  7177.  
  7178.         BIT     3,(IY+$30)      ; test FLAGS2 - consider caps lock ?
  7179.                                 ; which is maintained by KEY-INPUT.
  7180.         JR      Z,L1909         ; forward to OUT-C-2 if not set to print.
  7181.  
  7182.         LD      A,$43           ; alter 'L' to 'C'.
  7183.  
  7184. ;; OUT-C-2
  7185. L1909:  PUSH    DE              ; save address pointer but OK as OUT-FLASH
  7186.                                 ; uses alternate set without RST 10.
  7187.  
  7188.         CALL    L18C1           ; routine OUT-FLASH to print.
  7189.  
  7190.         POP     DE              ; restore and
  7191.         RET                     ; return.
  7192.  
  7193. ; ----------------------------
  7194. ; Get line number of next line
  7195. ; ----------------------------
  7196. ; These two subroutines are called while editing.
  7197. ; This entry point is from ED-DOWN with HL addressing E_PPC
  7198. ; to fetch the next line number.
  7199. ; Also from AUTO-LIST with HL addressing S_TOP just to update S_TOP
  7200. ; with the value of the next line number. It gets fetched but is discarded.
  7201. ; These routines never get called while the editor is being used for input.
  7202.  
  7203. ;; LN-FETCH
  7204. L190F:  LD      E,(HL)          ; fetch low byte
  7205.         INC     HL              ; address next
  7206.         LD      D,(HL)          ; fetch high byte.
  7207.         PUSH    HL              ; save system variable hi pointer.
  7208.         EX      DE,HL           ; line number to HL,
  7209.         INC     HL              ; increment as a starting point.
  7210.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  7211.         CALL    L1695           ; routine LINE-NO gets line number in DE.
  7212.         POP     HL              ; restore system variable hi pointer.
  7213.  
  7214. ; This entry point is from the ED-UP with HL addressing E_PPC_hi
  7215.  
  7216. ;; LN-STORE
  7217. L191C:  BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7218.         RET     NZ              ; return if so.
  7219.                                 ; Note. above already checked by ED-UP/ED-DOWN.
  7220.  
  7221.         LD      (HL),D          ; save high byte of line number.
  7222.         DEC     HL              ; address lower
  7223.         LD      (HL),E          ; save low byte of line number.
  7224.         RET                     ; return.
  7225.  
  7226. ; -----------------------------------------
  7227. ; Outputting numbers at start of BASIC line
  7228. ; -----------------------------------------
  7229. ; This routine entered at OUT-SP-NO is used to compute then output the first
  7230. ; three digits of a 4-digit BASIC line printing a space if necessary.
  7231. ; The line number, or residual part, is held in HL and the BC register
  7232. ; holds a subtraction value -1000, -100 or -10.
  7233. ; Note. for example line number 200 -
  7234. ; space(out_char), 2(out_code), 0(out_char) final number always out-code.
  7235.  
  7236. ;; OUT-SP-2
  7237. L1925:  LD      A,E             ; will be space if OUT-CODE not yet called.
  7238.                                 ; or $FF if spaces are suppressed.
  7239.                                 ; else $30 ('0').
  7240.                                 ; (from the first instruction at OUT-CODE)
  7241.                                 ; this guy is just too clever.
  7242.         AND     A               ; test bit 7 of A.
  7243.         RET     M               ; return if $FF, as leading spaces not
  7244.                                 ; required. This is set when printing line
  7245.                                 ; number and statement in MAIN-5.
  7246.  
  7247.         JR      L1937           ; forward to exit via OUT-CHAR.
  7248.  
  7249. ; ---
  7250.  
  7251. ; -> the single entry point.
  7252.  
  7253. ;; OUT-SP-NO
  7254. L192A:  XOR     A               ; initialize digit to 0
  7255.  
  7256. ;; OUT-SP-1
  7257. L192B:  ADD     HL,BC           ; add negative number to HL.
  7258.         INC     A               ; increment digit
  7259.         JR      C,L192B         ; back to OUT-SP-1 until no carry from
  7260.                                 ; the addition.
  7261.  
  7262.         SBC     HL,BC           ; cancel the last addition
  7263.         DEC     A               ; and decrement the digit.
  7264.         JR      Z,L1925         ; back to OUT-SP-2 if it is zero.
  7265.  
  7266.         JP      L15EF           ; jump back to exit via OUT-CODE.    ->
  7267.  
  7268.  
  7269. ; -------------------------------------
  7270. ; Outputting characters in a BASIC line
  7271. ; -------------------------------------
  7272. ; This subroutine ...
  7273.  
  7274. ;; OUT-CHAR
  7275. L1937:  CALL    L2D1B           ; routine NUMERIC tests if it is a digit ?
  7276.         JR      NC,L196C        ; to OUT-CH-3 to print digit without
  7277.                                 ; changing mode. Will be 'K' mode if digits
  7278.                                 ; are at beginning of edit line.
  7279.  
  7280.         CP      $21             ; less than quote character ?
  7281.         JR      C,L196C         ; to OUT-CH-3 to output controls and space.
  7282.  
  7283.         RES     2,(IY+$01)      ; initialize FLAGS to 'K' mode and leave
  7284.                                 ; unchanged if this character would precede
  7285.                                 ; a keyword.
  7286.  
  7287.         CP      $CB             ; is character 'THEN' token ?
  7288.         JR      Z,L196C         ; to OUT-CH-3 to output if so.
  7289.  
  7290.         CP      $3A             ; is it ':' ?
  7291.         JR      NZ,L195A        ; to OUT-CH-1 if not statement separator
  7292.                                 ; to change mode back to 'L'.
  7293.  
  7294.         BIT     5,(IY+$37)      ; FLAGX  - Input Mode ??
  7295.         JR      NZ,L1968        ; to OUT-CH-2 if in input as no statements.
  7296.                                 ; Note. this check should seemingly be at
  7297.                                 ; the start. Commands seem inappropriate in
  7298.                                 ; INPUT mode and are rejected by the syntax
  7299.                                 ; checker anyway.
  7300.                                 ; unless INPUT LINE is being used.
  7301.  
  7302.         BIT     2,(IY+$30)      ; test FLAGS2 - is the ':' within quotes ?
  7303.         JR      Z,L196C         ; to OUT-CH-3 if ':' is outside quoted text.
  7304.  
  7305.         JR      L1968           ; to OUT-CH-2 as ':' is within quotes
  7306.  
  7307. ; ---
  7308.  
  7309. ;; OUT-CH-1
  7310. L195A:  CP      $22             ; is it quote character '"'  ?
  7311.         JR      NZ,L1968        ; to OUT-CH-2 with others to set 'L' mode.
  7312.  
  7313.         PUSH    AF              ; save character.
  7314.         LD      A,($5C6A)       ; fetch FLAGS2.
  7315.         XOR     $04             ; toggle the quotes flag.
  7316.         LD      ($5C6A),A       ; update FLAGS2
  7317.         POP     AF              ; and restore character.
  7318.  
  7319. ;; OUT-CH-2
  7320. L1968:  SET     2,(IY+$01)      ; update FLAGS - signal L mode if the cursor
  7321.                                 ; is next.
  7322.  
  7323. ;; OUT-CH-3
  7324. L196C:  RST     10H             ; PRINT-A vectors the character to
  7325.                                 ; channel 'S', 'K', 'R' or 'P'.
  7326.         RET                     ; return.
  7327.  
  7328. ; -------------------------------------------
  7329. ; Get starting address of line, or line after
  7330. ; -------------------------------------------
  7331. ; This routine is used often to get the address, in HL, of a BASIC line
  7332. ; number supplied in HL, or failing that the address of the following line
  7333. ; and the address of the previous line in DE.
  7334.  
  7335. ;; LINE-ADDR
  7336. L196E:  PUSH    HL              ; save line number in HL register
  7337.         LD      HL,($5C53)      ; fetch start of program from PROG
  7338.         LD      D,H             ; transfer address to
  7339.         LD      E,L             ; the DE register pair.
  7340.  
  7341. ;; LINE-AD-1
  7342. L1974:  POP     BC              ; restore the line number to BC
  7343.         CALL    L1980           ; routine CP-LINES compares with that
  7344.                                 ; addressed by HL
  7345.         RET     NC              ; return if line has been passed or matched.
  7346.                                 ; if NZ, address of previous is in DE
  7347.  
  7348.         PUSH    BC              ; save the current line number
  7349.         CALL    L19B8           ; routine NEXT-ONE finds address of next
  7350.                                 ; line number in DE, previous in HL.
  7351.         EX      DE,HL           ; switch so next in HL
  7352.         JR      L1974           ; back to LINE-AD-1 for another comparison
  7353.  
  7354. ; --------------------
  7355. ; Compare line numbers
  7356. ; --------------------
  7357. ; This routine compares a line number supplied in BC with an addressed
  7358. ; line number pointed to by HL.
  7359.  
  7360. ;; CP-LINES
  7361. L1980:  LD      A,(HL)          ; Load the high byte of line number and
  7362.         CP      B               ; compare with that of supplied line number.
  7363.         RET     NZ              ; return if yet to match (carry will be set).
  7364.  
  7365.         INC     HL              ; address low byte of
  7366.         LD      A,(HL)          ; number and pick up in A.
  7367.         DEC     HL              ; step back to first position.
  7368.         CP      C               ; now compare.
  7369.         RET                     ; zero set if exact match.
  7370.                                 ; carry set if yet to match.
  7371.                                 ; no carry indicates a match or
  7372.                                 ; next available BASIC line or
  7373.                                 ; program end marker.
  7374.  
  7375. ; -------------------
  7376. ; Find each statement
  7377. ; -------------------
  7378. ; The single entry point EACH-STMT is used to
  7379. ; 1) To find the D'th statement in a line.
  7380. ; 2) To find a token in held E.
  7381.  
  7382. ;; not-used
  7383. L1988:  INC     HL              ;
  7384.         INC     HL              ;
  7385.         INC     HL              ;
  7386.  
  7387. ; -> entry point.
  7388.  
  7389. ;; EACH-STMT
  7390. L198B:  LD      ($5C5D),HL      ; save HL in CH_ADD
  7391.         LD      C,$00           ; initialize quotes flag
  7392.  
  7393. ;; EACH-S-1
  7394. L1990:  DEC     D               ; decrease statement count
  7395.         RET     Z               ; return if zero
  7396.  
  7397.  
  7398.         RST     20H             ; NEXT-CHAR
  7399.         CP      E               ; is it the search token ?
  7400.         JR      NZ,L199A        ; forward to EACH-S-3 if not
  7401.  
  7402.         AND     A               ; clear carry
  7403.         RET                     ; return signalling success.
  7404.  
  7405. ; ---
  7406.  
  7407. ;; EACH-S-2
  7408. L1998:  INC     HL              ; next address
  7409.         LD      A,(HL)          ; next character
  7410.  
  7411. ;; EACH-S-3
  7412. L199A:  CALL    L18B6           ; routine NUMBER skips if number marker
  7413.         LD      ($5C5D),HL      ; save in CH_ADD
  7414.         CP      $22             ; is it quotes '"' ?
  7415.         JR      NZ,L19A5        ; to EACH-S-4 if not
  7416.  
  7417.         DEC     C               ; toggle bit 0 of C
  7418.  
  7419. ;; EACH-S-4
  7420. L19A5:  CP      $3A             ; is it ':'
  7421.         JR      Z,L19AD         ; to EACH-S-5
  7422.  
  7423.         CP      $CB             ; 'THEN'
  7424.         JR      NZ,L19B1        ; to EACH-S-6
  7425.  
  7426. ;; EACH-S-5
  7427. L19AD:  BIT     0,C             ; is it in quotes
  7428.         JR      Z,L1990         ; to EACH-S-1 if not
  7429.  
  7430. ;; EACH-S-6
  7431. L19B1:  CP      $0D             ; end of line ?
  7432.         JR      NZ,L1998        ; to EACH-S-2
  7433.  
  7434.         DEC     D               ; decrease the statement counter
  7435.                                 ; which should be zero else
  7436.                                 ; 'Statement Lost'.
  7437.         SCF                     ; set carry flag - not found
  7438.         RET                     ; return
  7439.  
  7440. ; -----------------------------------------------------------------------
  7441. ; Storage of variables. For full details - see chapter 24.
  7442. ; ZX Spectrum BASIC Programming by Steven Vickers 1982.
  7443. ; It is bits 7-5 of the first character of a variable that allow
  7444. ; the six types to be distinguished. Bits 4-0 are the reduced letter.
  7445. ; So any variable name is higher that $3F and can be distinguished
  7446. ; also from the variables area end-marker $80.
  7447. ;
  7448. ; 76543210 meaning                               brief outline of format.
  7449. ; -------- ------------------------              -----------------------
  7450. ; 010      string variable.                      2 byte length + contents.
  7451. ; 110      string array.                         2 byte length + contents.
  7452. ; 100      array of numbers.                     2 byte length + contents.
  7453. ; 011      simple numeric variable.              5 bytes.
  7454. ; 101      variable length named numeric.        5 bytes.
  7455. ; 111      for-next loop variable.               18 bytes.
  7456. ; 10000000 the variables area end-marker.
  7457. ;
  7458. ; Note. any of the above seven will serve as a program end-marker.
  7459. ;
  7460. ; -----------------------------------------------------------------------
  7461.  
  7462. ; ------------
  7463. ; Get next one
  7464. ; ------------
  7465. ; This versatile routine is used to find the address of the next line
  7466. ; in the program area or the next variable in the variables area.
  7467. ; The reason one routine is made to handle two apparently unrelated tasks
  7468. ; is that it can be called indiscriminately when merging a line or a
  7469. ; variable.
  7470.  
  7471. ;; NEXT-ONE
  7472. L19B8:  PUSH    HL              ; save the pointer address.
  7473.         LD      A,(HL)          ; get first byte.
  7474.         CP      $40             ; compare with upper limit for line numbers.
  7475.         JR      C,L19D5         ; forward to NEXT-O-3 if within BASIC area.
  7476.  
  7477. ; the continuation here is for the next variable unless the supplied
  7478. ; line number was erroneously over 16383. see RESTORE command.
  7479.  
  7480.         BIT     5,A             ; is it a string or an array variable ?
  7481.         JR      Z,L19D6         ; forward to NEXT-O-4 to compute length.
  7482.  
  7483.         ADD     A,A             ; test bit 6 for single-character variables.
  7484.         JP      M,L19C7         ; forward to NEXT-O-1 if so
  7485.  
  7486.         CCF                     ; clear the carry for long-named variables.
  7487.                                 ; it remains set for for-next loop variables.
  7488.  
  7489. ;; NEXT-O-1
  7490. L19C7:  LD      BC,$0005        ; set BC to 5 for floating point number
  7491.         JR      NC,L19CE        ; forward to NEXT-O-2 if not a for/next
  7492.                                 ; variable.
  7493.  
  7494.         LD      C,$12           ; set BC to eighteen locations.
  7495.                                 ; value, limit, step, line and statement.
  7496.  
  7497. ; now deal with long-named variables
  7498.  
  7499. ;; NEXT-O-2
  7500. L19CE:  RLA                     ; test if character inverted. carry will also
  7501.                                 ; be set for single character variables
  7502.         INC     HL              ; address next location.
  7503.         LD      A,(HL)          ; and load character.
  7504.         JR      NC,L19CE        ; back to NEXT-O-2 if not inverted bit.
  7505.                                 ; forward immediately with single character
  7506.                                 ; variable names.
  7507.  
  7508.         JR      L19DB           ; forward to NEXT-O-5 to add length of
  7509.                                 ; floating point number(s etc.).
  7510.  
  7511. ; ---
  7512.  
  7513. ; this branch is for line numbers.
  7514.  
  7515. ;; NEXT-O-3
  7516. L19D5:  INC     HL              ; increment pointer to low byte of line no.
  7517.  
  7518. ; strings and arrays rejoin here
  7519.  
  7520. ;; NEXT-O-4
  7521. L19D6:  INC     HL              ; increment to address the length low byte.
  7522.         LD      C,(HL)          ; transfer to C and
  7523.         INC     HL              ; point to high byte of length.
  7524.         LD      B,(HL)          ; transfer that to B
  7525.         INC     HL              ; point to start of BASIC/variable contents.
  7526.  
  7527. ; the three types of numeric variables rejoin here
  7528.  
  7529. ;; NEXT-O-5
  7530. L19DB:  ADD     HL,BC           ; add the length to give address of next
  7531.                                 ; line/variable in HL.
  7532.         POP     DE              ; restore previous address to DE.
  7533.  
  7534. ; ------------------
  7535. ; Difference routine
  7536. ; ------------------
  7537. ; This routine terminates the above routine and is also called from the
  7538. ; start of the next routine to calculate the length to reclaim.
  7539.  
  7540. ;; DIFFER
  7541. L19DD:  AND     A               ; prepare for true subtraction.
  7542.         SBC     HL,DE           ; subtract the two pointers.
  7543.         LD      B,H             ; transfer result
  7544.         LD      C,L             ; to BC register pair.
  7545.         ADD     HL,DE           ; add back
  7546.         EX      DE,HL           ; and switch pointers
  7547.         RET                     ; return values are the length of area in BC,
  7548.                                 ; low pointer (previous) in HL,
  7549.                                 ; high pointer (next) in DE.
  7550.  
  7551. ; -----------------------
  7552. ; Handle reclaiming space
  7553. ; -----------------------
  7554. ;
  7555.  
  7556. ;; RECLAIM-1
  7557. L19E5:  CALL    L19DD           ; routine DIFFER immediately above
  7558.  
  7559. ;; RECLAIM-2
  7560. L19E8:  PUSH    BC              ;
  7561.  
  7562.         LD      A,B             ;
  7563.         CPL                     ;
  7564.         LD      B,A             ;
  7565.         LD      A,C             ;
  7566.         CPL                     ;
  7567.         LD      C,A             ;
  7568.         INC     BC              ;
  7569.  
  7570.         CALL    L1664           ; routine POINTERS
  7571.         EX      DE,HL           ;
  7572.         POP     HL              ;
  7573.  
  7574.         ADD     HL,DE           ;
  7575.         PUSH    DE              ;
  7576.         LDIR                    ; copy bytes
  7577.  
  7578.         POP     HL              ;
  7579.         RET                     ;
  7580.  
  7581. ; ----------------------------------------
  7582. ; Read line number of line in editing area
  7583. ; ----------------------------------------
  7584. ; This routine reads a line number in the editing area returning the number
  7585. ; in the BC register or zero if no digits exist before commands.
  7586. ; It is called from LINE-SCAN to check the syntax of the digits.
  7587. ; It is called from MAIN-3 to extract the line number in preparation for
  7588. ; inclusion of the line in the BASIC program area.
  7589. ;
  7590. ; Interestingly the calculator stack is moved from its normal place at the
  7591. ; end of dynamic memory to an adequate area within the system variables area.
  7592. ; This ensures that in a low memory situation, that valid line numbers can
  7593. ; be extracted without raising an error and that memory can be reclaimed
  7594. ; by deleting lines. If the stack was in its normal place then a situation
  7595. ; arises whereby the Spectrum becomes locked with no means of reclaiming space.
  7596.  
  7597. ;; E-LINE-NO
  7598. L19FB:  LD      HL,($5C59)      ; load HL from system variable E_LINE.
  7599.  
  7600.         DEC     HL              ; decrease so that NEXT_CHAR can be used
  7601.                                 ; without skipping the first digit.
  7602.  
  7603.         LD      ($5C5D),HL      ; store in the system variable CH_ADD.
  7604.  
  7605.         RST     20H             ; NEXT-CHAR skips any noise and white-space
  7606.                                 ; to point exactly at the first digit.
  7607.  
  7608.         LD      HL,$5C92        ; use MEM-0 as a temporary calculator stack
  7609.                                 ; an overhead of three locations are needed.
  7610.         LD      ($5C65),HL      ; set new STKEND.
  7611.  
  7612.         CALL    L2D3B           ; routine INT-TO-FP will read digits till
  7613.                                 ; a non-digit found.
  7614.         CALL    L2DA2           ; routine FP-TO-BC will retrieve number
  7615.                                 ; from stack at membot.
  7616.         JR      C,L1A15         ; forward to E-L-1 if overflow i.e. > 65535.
  7617.                                 ; 'Nonsense in BASIC'
  7618.  
  7619.         LD      HL,$D8F0        ; load HL with value -9999
  7620.         ADD     HL,BC           ; add to line number in BC
  7621.  
  7622. ;; E-L-1
  7623. L1A15:  JP      C,L1C8A         ; to REPORT-C 'Nonsense in BASIC' if over.
  7624.                                 ; Note. As ERR_SP points to ED_ERROR
  7625.                                 ; the report is never produced although
  7626.                                 ; the RST $08 will update X_PTR leading to
  7627.                                 ; the error marker being displayed when
  7628.                                 ; the ED_LOOP is reiterated.
  7629.                                 ; in fact, since it is immediately
  7630.                                 ; cancelled, any report will do.
  7631.  
  7632. ; a line in the range 0 - 9999 has been entered.
  7633.  
  7634.         JP      L16C5           ; jump back to SET-STK to set the calculator
  7635.                                 ; stack back to its normal place and exit
  7636.                                 ; from there.
  7637.  
  7638. ; ---------------------------------
  7639. ; Report and line number outputting
  7640. ; ---------------------------------
  7641. ; Entry point OUT-NUM-1 is used by the Error Reporting code to print
  7642. ; the line number and later the statement number held in BC.
  7643. ; If the statement was part of a direct command then -2 is used as a
  7644. ; dummy line number so that zero will be printed in the report.
  7645. ; This routine is also used to print the exponent of E-format numbers.
  7646. ;
  7647. ; Entry point OUT-NUM-2 is used from OUT-LINE to output the line number
  7648. ; addressed by HL with leading spaces if necessary.
  7649.  
  7650. ;; OUT-NUM-1
  7651. L1A1B:  PUSH    DE              ; save the
  7652.         PUSH    HL              ; registers.
  7653.         XOR     A               ; set A to zero.
  7654.         BIT     7,B             ; is the line number minus two ?
  7655.         JR      NZ,L1A42        ; forward to OUT-NUM-4 if so to print zero
  7656.                                 ; for a direct command.
  7657.  
  7658.         LD      H,B             ; transfer the
  7659.         LD      L,C             ; number to HL.
  7660.         LD      E,$FF           ; signal 'no leading zeros'.
  7661.         JR      L1A30           ; forward to continue at OUT-NUM-3
  7662.  
  7663. ; ---
  7664.  
  7665. ; from OUT-LINE - HL addresses line number.
  7666.  
  7667. ;; OUT-NUM-2
  7668. L1A28:  PUSH    DE              ; save flags
  7669.         LD      D,(HL)          ; high byte to D
  7670.         INC     HL              ; address next
  7671.         LD      E,(HL)          ; low byte to E
  7672.         PUSH    HL              ; save pointer
  7673.         EX      DE,HL           ; transfer number to HL
  7674.         LD      E,$20           ; signal 'output leading spaces'
  7675.  
  7676. ;; OUT-NUM-3
  7677. L1A30:  LD      BC,$FC18        ; value -1000
  7678.         CALL    L192A           ; routine OUT-SP-NO outputs space or number
  7679.         LD      BC,$FF9C        ; value -100
  7680.         CALL    L192A           ; routine OUT-SP-NO
  7681.         LD      C,$F6           ; value -10 ( B is still $FF )
  7682.         CALL    L192A           ; routine OUT-SP-NO
  7683.         LD      A,L             ; remainder to A.
  7684.  
  7685. ;; OUT-NUM-4
  7686. L1A42:  CALL    L15EF           ; routine OUT-CODE for final digit.
  7687.                                 ; else report code zero wouldn't get
  7688.                                 ; printed.
  7689.         POP     HL              ; restore the
  7690.         POP     DE              ; registers and
  7691.         RET                     ; return.
  7692.  
  7693.  
  7694. ;***************************************************
  7695. ;** Part 7. BASIC LINE AND COMMAND INTERPRETATION **
  7696. ;***************************************************
  7697.  
  7698. ; ----------------
  7699. ; The offset table
  7700. ; ----------------
  7701. ; The BASIC interpreter has found a command code $CE - $FF
  7702. ; which is then reduced to range $00 - $31 and added to the base address
  7703. ; of this table to give the address of an offset which, when added to
  7704. ; the offset therein, gives the location in the following parameter table
  7705. ; where a list of class codes, separators and addresses relevant to the
  7706. ; command exists.
  7707.  
  7708. ;; offst-tbl
  7709. L1A48:  DB    L1AF9 - $       ; B1 offset to Address: P-DEF-FN
  7710.         DB    L1B14 - $       ; CB offset to Address: P-CAT
  7711.         DB    L1B06 - $       ; BC offset to Address: P-FORMAT
  7712.         DB    L1B0A - $       ; BF offset to Address: P-MOVE
  7713.         DB    L1B10 - $       ; C4 offset to Address: P-ERASE
  7714.         DB    L1AFC - $       ; AF offset to Address: P-OPEN
  7715.         DB    L1B02 - $       ; B4 offset to Address: P-CLOSE
  7716.         DB    L1AE2 - $       ; 93 offset to Address: P-MERGE
  7717.         DB    L1AE1 - $       ; 91 offset to Address: P-VERIFY
  7718.         DB    L1AE3 - $       ; 92 offset to Address: P-BEEP
  7719.         DB    L1AE7 - $       ; 95 offset to Address: P-CIRCLE
  7720.         DB    L1AEB - $       ; 98 offset to Address: P-INK
  7721.         DB    L1AEC - $       ; 98 offset to Address: P-PAPER
  7722.         DB    L1AED - $       ; 98 offset to Address: P-FLASH
  7723.         DB    L1AEE - $       ; 98 offset to Address: P-BRIGHT
  7724.         DB    L1AEF - $       ; 98 offset to Address: P-INVERSE
  7725.         DB    L1AF0 - $       ; 98 offset to Address: P-OVER
  7726.         DB    L1AF1 - $       ; 98 offset to Address: P-OUT
  7727.         DB    L1AD9 - $       ; 7F offset to Address: P-LPRINT
  7728.         DB    L1ADC - $       ; 81 offset to Address: P-LLIST
  7729.         DB    L1A8A - $       ; 2E offset to Address: P-STOP
  7730.         DB    L1AC9 - $       ; 6C offset to Address: P-READ
  7731.         DB    L1ACC - $       ; 6E offset to Address: P-DATA
  7732.         DB    L1ACF - $       ; 70 offset to Address: P-RESTORE
  7733.         DB    L1AA8 - $       ; 48 offset to Address: P-NEW
  7734.         DB    L1AF5 - $       ; 94 offset to Address: P-BORDER
  7735.         DB    L1AB8 - $       ; 56 offset to Address: P-CONT
  7736.         DB    L1AA2 - $       ; 3F offset to Address: P-DIM
  7737.         DB    L1AA5 - $       ; 41 offset to Address: P-REM
  7738.         DB    L1A90 - $       ; 2B offset to Address: P-FOR
  7739.         DB    L1A7D - $       ; 17 offset to Address: P-GO-TO
  7740.         DB    L1A86 - $       ; 1F offset to Address: P-GO-SUB
  7741.         DB    L1A9F - $       ; 37 offset to Address: P-INPUT
  7742.         DB    L1AE0 - $       ; 77 offset to Address: P-LOAD
  7743.         DB    L1AAE - $       ; 44 offset to Address: P-LIST
  7744.         DB    L1A7A - $       ; 0F offset to Address: P-LET
  7745.         DB    L1AC5 - $       ; 59 offset to Address: P-PAUSE
  7746.         DB    L1A98 - $       ; 2B offset to Address: P-NEXT
  7747.         DB    L1AB1 - $       ; 43 offset to Address: P-POKE
  7748.         DB    L1A9C - $       ; 2D offset to Address: P-PRINT
  7749.         DB    L1AC1 - $       ; 51 offset to Address: P-PLOT
  7750.         DB    L1AAB - $       ; 3A offset to Address: P-RUN
  7751.         DB    L1ADF - $       ; 6D offset to Address: P-SAVE
  7752.         DB    L1AB5 - $       ; 42 offset to Address: P-RANDOM
  7753.         DB    L1A81 - $       ; 0D offset to Address: P-IF
  7754.         DB    L1ABE - $       ; 49 offset to Address: P-CLS
  7755.         DB    L1AD2 - $       ; 5C offset to Address: P-DRAW
  7756.         DB    L1ABB - $       ; 44 offset to Address: P-CLEAR
  7757.         DB    L1A8D - $       ; 15 offset to Address: P-RETURN
  7758.         DB    L1AD6 - $       ; 5D offset to Address: P-COPY
  7759.  
  7760.  
  7761. ; -------------------------------
  7762. ; The parameter or "Syntax" table
  7763. ; -------------------------------
  7764. ; For each command there exists a variable list of parameters.
  7765. ; If the character is greater than a space it is a required separator.
  7766. ; If less, then it is a command class in the range 00 - 0B.
  7767. ; Note that classes 00, 03 and 05 will fetch the addresses from this table.
  7768. ; Some classes e.g. 07 and 0B have the same address in all invocations
  7769. ; and the command is re-computed from the low-byte of the parameter address.
  7770. ; Some e.g. 02 are only called once so a call to the command is made from
  7771. ; within the class routine rather than holding the address within the table.
  7772. ; Some class routines check syntax entirely and some leave this task for the
  7773. ; command itself.
  7774. ; Others for example CIRCLE (x,y,z) check the first part (x,y) using the
  7775. ; class routine and the final part (,z) within the command.
  7776. ; The last few commands appear to have been added in a rush but their syntax
  7777. ; is rather simple e.g. MOVE "M1","M2"
  7778.  
  7779. ;; P-LET
  7780. L1A7A:  DB    $01             ; Class-01 - A variable is required.
  7781.         DB    $3D             ; Separator:  '='
  7782.         DB    $02             ; Class-02 - An expression, numeric or string,
  7783.                                 ; must follow.
  7784.  
  7785. ;; P-GO-TO
  7786. L1A7D:  DB    $06             ; Class-06 - A numeric expression must follow.
  7787.         DB    $00             ; Class-00 - No further operands.
  7788.         DEFW    L1E67           ; Address: $1E67; Address: GO-TO
  7789.  
  7790. ;; P-IF
  7791. L1A81:  DB    $06             ; Class-06 - A numeric expression must follow.
  7792.         DB    $CB             ; Separator:  'THEN'
  7793.         DB    $05             ; Class-05 - Variable syntax checked
  7794.                                 ; by routine.
  7795.         DEFW    L1CF0           ; Address: $1CF0; Address: IF
  7796.  
  7797. ;; P-GO-SUB
  7798. L1A86:  DB    $06             ; Class-06 - A numeric expression must follow.
  7799.         DB    $00             ; Class-00 - No further operands.
  7800.         DEFW    L1EED           ; Address: $1EED; Address: GO-SUB
  7801.  
  7802. ;; P-STOP
  7803. L1A8A:  DB    $00             ; Class-00 - No further operands.
  7804.         DEFW    L1CEE           ; Address: $1CEE; Address: STOP
  7805.  
  7806. ;; P-RETURN
  7807. L1A8D:  DB    $00             ; Class-00 - No further operands.
  7808.         DEFW    L1F23           ; Address: $1F23; Address: RETURN
  7809.  
  7810. ;; P-FOR
  7811. L1A90:  DB    $04             ; Class-04 - A single character variable must
  7812.                                 ; follow.
  7813.         DB    $3D             ; Separator:  '='
  7814.         DB    $06             ; Class-06 - A numeric expression must follow.
  7815.         DB    $CC             ; Separator:  'TO'
  7816.         DB    $06             ; Class-06 - A numeric expression must follow.
  7817.         DB    $05             ; Class-05 - Variable syntax checked
  7818.                                 ; by routine.
  7819.         DEFW    L1D03           ; Address: $1D03; Address: FOR
  7820.  
  7821. ;; P-NEXT
  7822. L1A98:  DB    $04             ; Class-04 - A single character variable must
  7823.                                 ; follow.
  7824.         DB    $00             ; Class-00 - No further operands.
  7825.         DEFW    L1DAB           ; Address: $1DAB; Address: NEXT
  7826.  
  7827. ;; P-PRINT
  7828. L1A9C:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7829.                                 ; by routine.
  7830.         DEFW    L1FCD           ; Address: $1FCD; Address: PRINT
  7831.  
  7832. ;; P-INPUT
  7833. L1A9F:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7834.                                 ; by routine.
  7835.         DEFW    L2089           ; Address: $2089; Address: INPUT
  7836.  
  7837. ;; P-DIM
  7838. L1AA2:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7839.                                 ; by routine.
  7840.         DEFW    L2C02           ; Address: $2C02; Address: DIM
  7841.  
  7842. ;; P-REM
  7843. L1AA5:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7844.                                 ; by routine.
  7845.         DEFW    L1BB2           ; Address: $1BB2; Address: REM
  7846.  
  7847. ;; P-NEW
  7848. L1AA8:  DB    $00             ; Class-00 - No further operands.
  7849.         DEFW    L11B7           ; Address: $11B7; Address: NEW
  7850.  
  7851. ;; P-RUN
  7852. L1AAB:  DB    $03             ; Class-03 - A numeric expression may follow
  7853.                                 ; else default to zero.
  7854.         DEFW    L1EA1           ; Address: $1EA1; Address: RUN
  7855.  
  7856. ;; P-LIST
  7857. L1AAE:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7858.                                 ; by routine.
  7859.         DEFW    L17F9           ; Address: $17F9; Address: LIST
  7860.  
  7861. ;; P-POKE
  7862. L1AB1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7863.                                 ; expressions required.
  7864.         DB    $00             ; Class-00 - No further operands.
  7865.         DEFW    L1E80           ; Address: $1E80; Address: POKE
  7866.  
  7867. ;; P-RANDOM
  7868. L1AB5:  DB    $03             ; Class-03 - A numeric expression may follow
  7869.                                 ; else default to zero.
  7870.         DEFW    L1E4F           ; Address: $1E4F; Address: RANDOMIZE
  7871.  
  7872. ;; P-CONT
  7873. L1AB8:  DB    $00             ; Class-00 - No further operands.
  7874.         DEFW    L1E5F           ; Address: $1E5F; Address: CONTINUE
  7875.  
  7876. ;; P-CLEAR
  7877. L1ABB:  DB    $03             ; Class-03 - A numeric expression may follow
  7878.                                 ; else default to zero.
  7879.         DEFW    L1EAC           ; Address: $1EAC; Address: CLEAR
  7880.  
  7881. ;; P-CLS
  7882. L1ABE:  DB    $00             ; Class-00 - No further operands.
  7883.         DEFW    L0D6B           ; Address: $0D6B; Address: CLS
  7884.  
  7885. ;; P-PLOT
  7886. L1AC1:  DB    $09             ; Class-09 - Two comma-separated numeric
  7887.                                 ; expressions required with optional colour
  7888.                                 ; items.
  7889.         DB    $00             ; Class-00 - No further operands.
  7890.         DEFW    L22DC           ; Address: $22DC; Address: PLOT
  7891.  
  7892. ;; P-PAUSE
  7893. L1AC5:  DB    $06             ; Class-06 - A numeric expression must follow.
  7894.         DB    $00             ; Class-00 - No further operands.
  7895.         DEFW    L1F3A           ; Address: $1F3A; Address: PAUSE
  7896.  
  7897. ;; P-READ
  7898. L1AC9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7899.                                 ; by routine.
  7900.         DEFW    L1DED           ; Address: $1DED; Address: READ
  7901.  
  7902. ;; P-DATA
  7903. L1ACC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7904.                                 ; by routine.
  7905.         DEFW    L1E27           ; Address: $1E27; Address: DATA
  7906.  
  7907. ;; P-RESTORE
  7908. L1ACF:  DB    $03             ; Class-03 - A numeric expression may follow
  7909.                                 ; else default to zero.
  7910.         DEFW    L1E42           ; Address: $1E42; Address: RESTORE
  7911.  
  7912. ;; P-DRAW
  7913. L1AD2:  DB    $09             ; Class-09 - Two comma-separated numeric
  7914.                                 ; expressions required with optional colour
  7915.                                 ; items.
  7916.         DB    $05             ; Class-05 - Variable syntax checked
  7917.                                 ; by routine.
  7918.         DEFW    L2382           ; Address: $2382; Address: DRAW
  7919.  
  7920. ;; P-COPY
  7921. L1AD6:  DB    $00             ; Class-00 - No further operands.
  7922.         DEFW    L0EAC           ; Address: $0EAC; Address: COPY
  7923.  
  7924. ;; P-LPRINT
  7925. L1AD9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7926.                                 ; by routine.
  7927.         DEFW    L1FC9           ; Address: $1FC9; Address: LPRINT
  7928.  
  7929. ;; P-LLIST
  7930. L1ADC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7931.                                 ; by routine.
  7932.         DEFW    L17F5           ; Address: $17F5; Address: LLIST
  7933.  
  7934. ;; P-SAVE
  7935. L1ADF:  DB    $0B             ; Class-0B - Offset address converted to tape
  7936.                                 ; command.
  7937.  
  7938. ;; P-LOAD
  7939. L1AE0:  DB    $0B             ; Class-0B - Offset address converted to tape
  7940.                                 ; command.
  7941.  
  7942. ;; P-VERIFY
  7943. L1AE1:  DB    $0B             ; Class-0B - Offset address converted to tape
  7944.                                 ; command.
  7945.  
  7946. ;; P-MERGE
  7947. L1AE2:  DB    $0B             ; Class-0B - Offset address converted to tape
  7948.                                 ; command.
  7949.  
  7950. ;; P-BEEP
  7951. L1AE3:  DB    $08             ; Class-08 - Two comma-separated numeric
  7952.                                 ; expressions required.
  7953.         DB    $00             ; Class-00 - No further operands.
  7954.         DEFW    L03F8           ; Address: $03F8; Address: BEEP
  7955.  
  7956. ;; P-CIRCLE
  7957. L1AE7:  DB    $09             ; Class-09 - Two comma-separated numeric
  7958.                                 ; expressions required with optional colour
  7959.                                 ; items.
  7960.         DB    $05             ; Class-05 - Variable syntax checked
  7961.                                 ; by routine.
  7962.         DEFW    L2320           ; Address: $2320; Address: CIRCLE
  7963.  
  7964. ;; P-INK
  7965. L1AEB:  DB    $07             ; Class-07 - Offset address is converted to
  7966.                                 ; colour code.
  7967.  
  7968. ;; P-PAPER
  7969. L1AEC:  DB    $07             ; Class-07 - Offset address is converted to
  7970.                                 ; colour code.
  7971.  
  7972. ;; P-FLASH
  7973. L1AED:  DB    $07             ; Class-07 - Offset address is converted to
  7974.                                 ; colour code.
  7975.  
  7976. ;; P-BRIGHT
  7977. L1AEE:  DB    $07             ; Class-07 - Offset address is converted to
  7978.                                 ; colour code.
  7979.  
  7980. ;; P-INVERSE
  7981. L1AEF:  DB    $07             ; Class-07 - Offset address is converted to
  7982.                                 ; colour code.
  7983.  
  7984. ;; P-OVER
  7985. L1AF0:  DB    $07             ; Class-07 - Offset address is converted to
  7986.                                 ; colour code.
  7987.  
  7988. ;; P-OUT
  7989. L1AF1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7990.                                 ; expressions required.
  7991.         DB    $00             ; Class-00 - No further operands.
  7992.         DEFW    L1E7A           ; Address: $1E7A; Address: OUT
  7993.  
  7994. ;; P-BORDER
  7995. L1AF5:  DB    $06             ; Class-06 - A numeric expression must follow.
  7996.         DB    $00             ; Class-00 - No further operands.
  7997.         DEFW    L2294           ; Address: $2294; Address: BORDER
  7998.  
  7999. ;; P-DEF-FN
  8000. L1AF9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  8001.                                 ; by routine.
  8002.         DEFW    L1F60           ; Address: $1F60; Address: DEF-FN
  8003.  
  8004. ;; P-OPEN
  8005. L1AFC:  DB    $06             ; Class-06 - A numeric expression must follow.
  8006.         DB    $2C             ; Separator:  ','          see Footnote *
  8007.         DB    $0A             ; Class-0A - A string expression must follow.
  8008.         DB    $00             ; Class-00 - No further operands.
  8009.         DEFW    L1736           ; Address: $1736; Address: OPEN
  8010.  
  8011. ;; P-CLOSE
  8012. L1B02:  DB    $06             ; Class-06 - A numeric expression must follow.
  8013.         DB    $00             ; Class-00 - No further operands.
  8014.         DEFW    L16E5           ; Address: $16E5; Address: CLOSE
  8015.  
  8016. ;; P-FORMAT
  8017. L1B06:  DB    $0A             ; Class-0A - A string expression must follow.
  8018.         DB    $00             ; Class-00 - No further operands.
  8019.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8020.  
  8021. ;; P-MOVE
  8022. L1B0A:  DB    $0A             ; Class-0A - A string expression must follow.
  8023.         DB    $2C             ; Separator:  ','
  8024.         DB    $0A             ; Class-0A - A string expression must follow.
  8025.         DB    $00             ; Class-00 - No further operands.
  8026.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8027.  
  8028. ;; P-ERASE
  8029. L1B10:  DB    $0A             ; Class-0A - A string expression must follow.
  8030.         DB    $00             ; Class-00 - No further operands.
  8031.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8032.  
  8033. ;; P-CAT
  8034. L1B14:  DB    $00             ; Class-00 - No further operands.
  8035.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8036.  
  8037. ; * Note that a comma is required as a separator with the OPEN command
  8038. ; but the Interface 1 programmers relaxed this allowing ';' as an
  8039. ; alternative for their channels creating a confusing mixture of
  8040. ; allowable syntax as it is this ROM which opens or re-opens the
  8041. ; normal channels.
  8042.  
  8043. ; -------------------------------
  8044. ; Main parser (BASIC interpreter)
  8045. ; -------------------------------
  8046. ; This routine is called once from MAIN-2 when the BASIC line is to
  8047. ; be entered or re-entered into the Program area and the syntax
  8048. ; requires checking.
  8049.  
  8050. ;; LINE-SCAN
  8051. L1B17:  RES     7,(IY+$01)      ; update FLAGS - signal checking syntax
  8052.         CALL    L19FB           ; routine E-LINE-NO              >>
  8053.                                 ; fetches the line number if in range.
  8054.  
  8055.         XOR     A               ; clear the accumulator.
  8056.         LD      ($5C47),A       ; set statement number SUBPPC to zero.
  8057.         DEC     A               ; set accumulator to $FF.
  8058.         LD      ($5C3A),A       ; set ERR_NR to 'OK' - 1.
  8059.         JR      L1B29           ; forward to continue at STMT-L-1.
  8060.  
  8061. ; --------------
  8062. ; Statement loop
  8063. ; --------------
  8064. ;
  8065. ;
  8066.  
  8067. ;; STMT-LOOP
  8068. L1B28:  RST     20H             ; NEXT-CHAR
  8069.  
  8070. ; -> the entry point from above or LINE-RUN
  8071. ;; STMT-L-1
  8072. L1B29:  CALL    L16BF           ; routine SET-WORK clears workspace etc.
  8073.  
  8074.         INC     (IY+$0D)        ; increment statement number SUBPPC
  8075.         JP      M,L1C8A         ; to REPORT-C to raise
  8076.                                 ; 'Nonsense in BASIC' if over 127.
  8077.  
  8078.         RST     18H             ; GET-CHAR
  8079.  
  8080.         LD      B,$00           ; set B to zero for later indexing.
  8081.                                 ; early so any other reason ???
  8082.  
  8083.         CP      $0D             ; is character carriage return ?
  8084.                                 ; i.e. an empty statement.
  8085.         JR      Z,L1BB3         ; forward to LINE-END if so.
  8086.  
  8087.         CP      $3A             ; is it statement end marker ':' ?
  8088.                                 ; i.e. another type of empty statement.
  8089.         JR      Z,L1B28         ; back to STMT-LOOP if so.
  8090.  
  8091.         LD      HL,L1B76        ; address: STMT-RET
  8092.         PUSH    HL              ; is now pushed as a return address
  8093.         LD      C,A             ; transfer the current character to C.
  8094.  
  8095. ; advance CH_ADD to a position after command and test if it is a command.
  8096.  
  8097.         RST     20H             ; NEXT-CHAR to advance pointer
  8098.         LD      A,C             ; restore current character
  8099.         SUB     $CE             ; subtract 'DEF FN' - first command
  8100.         JP      C,L1C8A         ; jump to REPORT-C if less than a command
  8101.                                 ; raising
  8102.                                 ; 'Nonsense in BASIC'
  8103.  
  8104.         LD      C,A             ; put the valid command code back in C.
  8105.                                 ; register B is zero.
  8106.         LD      HL,L1A48        ; address: offst-tbl
  8107.         ADD     HL,BC           ; index into table with one of 50 commands.
  8108.         LD      C,(HL)          ; pick up displacement to syntax table entry.
  8109.         ADD     HL,BC           ; add to address the relevant entry.
  8110.         JR      L1B55           ; forward to continue at GET-PARAM
  8111.  
  8112. ; ----------------------
  8113. ; The main scanning loop
  8114. ; ----------------------
  8115. ; not documented properly
  8116. ;
  8117.  
  8118. ;; SCAN-LOOP
  8119. L1B52:  LD      HL,($5C74)      ; fetch temporary address from T_ADDR
  8120.                                 ; during subsequent loops.
  8121.  
  8122. ; -> the initial entry point with HL addressing start of syntax table entry.
  8123.  
  8124. ;; GET-PARAM
  8125. L1B55:  LD      A,(HL)          ; pick up the parameter.
  8126.         INC     HL              ; address next one.
  8127.         LD      ($5C74),HL      ; save pointer in system variable T_ADDR
  8128.  
  8129.         LD      BC,L1B52        ; address: SCAN-LOOP
  8130.         PUSH    BC              ; is now pushed on stack as looping address.
  8131.         LD      C,A             ; store parameter in C.
  8132.         CP      $20             ; is it greater than ' '  ?
  8133.         JR      NC,L1B6F        ; forward to SEPARATOR to check that correct
  8134.                                 ; separator appears in statement if so.
  8135.  
  8136.         LD      HL,L1C01        ; address: class-tbl.
  8137.         LD      B,$00           ; prepare to index into the class table.
  8138.         ADD     HL,BC           ; index to find displacement to routine.
  8139.         LD      C,(HL)          ; displacement to BC
  8140.         ADD     HL,BC           ; add to address the CLASS routine.
  8141.         PUSH    HL              ; push the address on the stack.
  8142.  
  8143.         RST     18H             ; GET-CHAR - HL points to place in statement.
  8144.  
  8145.         DEC     B               ; reset the zero flag - the initial state
  8146.                                 ; for all class routines.
  8147.  
  8148.         RET                     ; and make an indirect jump to routine
  8149.                                 ; and then SCAN-LOOP (also on stack).
  8150.  
  8151. ; Note. one of the class routines will eventually drop the return address
  8152. ; off the stack breaking out of the above seemingly endless loop.
  8153.  
  8154. ; ----------------
  8155. ; Verify separator
  8156. ; ----------------
  8157. ; This routine is called once to verify that the mandatory separator
  8158. ; present in the parameter table is also present in the correct
  8159. ; location following the command. For example, the 'THEN' token after
  8160. ; the 'IF' token and expression.
  8161.  
  8162. ;; SEPARATOR
  8163. L1B6F:  RST     18H             ; GET-CHAR
  8164.         CP      C               ; does it match the character in C ?
  8165.         JP      NZ,L1C8A        ; jump forward to REPORT-C if not
  8166.                                 ; 'Nonsense in BASIC'.
  8167.  
  8168.         RST     20H             ; NEXT-CHAR advance to next character
  8169.         RET                     ; return.
  8170.  
  8171. ; ------------------------------
  8172. ; Come here after interpretation
  8173. ; ------------------------------
  8174. ;
  8175. ;
  8176.  
  8177. ;; STMT-RET
  8178. L1B76:  CALL    L1F54           ; routine BREAK-KEY is tested after every
  8179.                                 ; statement.
  8180.         JR      C,L1B7D         ; step forward to STMT-R-1 if not pressed.
  8181.  
  8182. ;; REPORT-L
  8183. L1B7B:  RST     08H             ; ERROR-1
  8184.         DB    $14             ; Error Report: BREAK into program
  8185.  
  8186. ;; STMT-R-1
  8187. L1B7D           IF BAS48_ONLY
  8188.                 BIT 7,(IY+0X0A)
  8189.                 ELSE
  8190.                 CALL L3B4D              ; Spectrum 128 patch
  8191.                 NOP
  8192.                 ENDIF
  8193.  
  8194. L1B81:  JR      NZ,L1BF4        ; forward to STMT-NEXT if a program line.
  8195.  
  8196.         LD      HL,($5C42)      ; fetch line number from NEWPPC
  8197.         BIT     7,H             ; will be set if minus two - direct command(s)
  8198.         JR      Z,L1B9E         ; forward to LINE-NEW if a jump is to be
  8199.                                 ; made to a new program line/statement.
  8200.  
  8201. ; --------------------
  8202. ; Run a direct command
  8203. ; --------------------
  8204. ; A direct command is to be run or, if continuing from above,
  8205. ; the next statement of a direct command is to be considered.
  8206.  
  8207. ;; LINE-RUN
  8208. L1B8A:  LD      HL,$FFFE        ; The dummy value minus two
  8209.         LD      ($5C45),HL      ; is set/reset as line number in PPC.
  8210.         LD      HL,($5C61)      ; point to end of line + 1 - WORKSP.
  8211.         DEC     HL              ; now point to $80 end-marker.
  8212.         LD      DE,($5C59)      ; address the start of line E_LINE.
  8213.         DEC     DE              ; now location before - for GET-CHAR.
  8214.         LD      A,($5C44)       ; load statement to A from NSPPC.
  8215.         JR      L1BD1           ; forward to NEXT-LINE.
  8216.  
  8217. ; ------------------------------
  8218. ; Find start address of new line
  8219. ; ------------------------------
  8220. ; The branch was to here if a jump is to made to a new line number
  8221. ; and statement.
  8222. ; That is the previous statement was a GO TO, GO SUB, RUN, RETURN, NEXT etc..
  8223.  
  8224. ;; LINE-NEW
  8225. L1B9E:  CALL    L196E           ; routine LINE-ADDR gets address of line
  8226.                                 ; returning zero flag set if line found.
  8227.         LD      A,($5C44)       ; fetch new statement from NSPPC
  8228.         JR      Z,L1BBF         ; forward to LINE-USE if line matched.
  8229.  
  8230. ; continue as must be a direct command.
  8231.  
  8232.         AND     A               ; test statement which should be zero
  8233.         JR      NZ,L1BEC        ; forward to REPORT-N if not.
  8234.                                 ; 'Statement lost'
  8235.  
  8236. ;
  8237.  
  8238.         LD      B,A             ; save statement in B. ?
  8239.         LD      A,(HL)          ; fetch high byte of line number.
  8240.         AND     $C0             ; test if using direct command
  8241.                                 ; a program line is less than $3F
  8242.         LD      A,B             ; retrieve statement.
  8243.                                 ; (we can assume it is zero).
  8244.         JR      Z,L1BBF         ; forward to LINE-USE if was a program line
  8245.  
  8246. ; Alternatively a direct statement has finished correctly.
  8247.  
  8248. ;; REPORT-0
  8249. L1BB0:  RST     08H             ; ERROR-1
  8250.         DB    $FF             ; Error Report: OK
  8251.  
  8252. ; ------------------
  8253. ; Handle REM command
  8254. ; ------------------
  8255. ; The REM command routine.
  8256. ; The return address STMT-RET is dropped and the rest of line ignored.
  8257.  
  8258. ;; REM
  8259. L1BB2:  POP     BC              ; drop return address STMT-RET and
  8260.                                 ; continue ignoring rest of line.
  8261.  
  8262. ; ------------
  8263. ; End of line?
  8264. ; ------------
  8265. ;
  8266. ;
  8267.  
  8268. ;; LINE-END
  8269. L1BB3:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8270.         RET     Z               ; return if checking syntax.
  8271.  
  8272.         LD      HL,($5C55)      ; fetch NXTLIN to HL.
  8273.         LD      A,$C0           ; test against the
  8274.         AND     (HL)            ; system limit $3F.
  8275.         RET     NZ              ; return if more as must be
  8276.                                 ; end of program.
  8277.                                 ; (or direct command)
  8278.  
  8279.         XOR     A               ; set statement to zero.
  8280.  
  8281. ; and continue to set up the next following line and then consider this new one.
  8282.  
  8283. ; ---------------------
  8284. ; General line checking
  8285. ; ---------------------
  8286. ; The branch was here from LINE-NEW if BASIC is branching.
  8287. ; or a continuation from above if dealing with a new sequential line.
  8288. ; First make statement zero number one leaving others unaffected.
  8289.  
  8290. ;; LINE-USE
  8291. L1BBF:  CP      $01             ; will set carry if zero.
  8292.         ADC     A,$00           ; add in any carry.
  8293.  
  8294.         LD      D,(HL)          ; high byte of line number to D.
  8295.         INC     HL              ; advance pointer.
  8296.         LD      E,(HL)          ; low byte of line number to E.
  8297.         LD      ($5C45),DE      ; set system variable PPC.
  8298.  
  8299.         INC     HL              ; advance pointer.
  8300.         LD      E,(HL)          ; low byte of line length to E.
  8301.         INC     HL              ; advance pointer.
  8302.         LD      D,(HL)          ; high byte of line length to D.
  8303.  
  8304.         EX      DE,HL           ; swap pointer to DE before
  8305.         ADD     HL,DE           ; adding to address the end of line.
  8306.         INC     HL              ; advance to start of next line.
  8307.  
  8308. ; -----------------------------
  8309. ; Update NEXT LINE but consider
  8310. ; previous line or edit line.
  8311. ; -----------------------------
  8312. ; The pointer will be the next line if continuing from above or to
  8313. ; edit line end-marker ($80) if from LINE-RUN.
  8314.  
  8315. ;; NEXT-LINE
  8316. L1BD1:  LD      ($5C55),HL      ; store pointer in system variable NXTLIN
  8317.  
  8318.         EX      DE,HL           ; bring back pointer to previous or edit line
  8319.         LD      ($5C5D),HL      ; and update CH_ADD with character address.
  8320.  
  8321.         LD      D,A             ; store statement in D.
  8322.         LD      E,$00           ; set E to zero to suppress token searching
  8323.                                 ; if EACH-STMT is to be called.
  8324.         LD      (IY+$0A),$FF    ; set statement NSPPC to $FF signalling
  8325.                                 ; no jump to be made.
  8326.         DEC     D               ; decrement and test statement
  8327.         LD      (IY+$0D),D      ; set SUBPPC to decremented statement number.
  8328.         JP      Z,L1B28         ; to STMT-LOOP if result zero as statement is
  8329.                                 ; at start of line and address is known.
  8330.  
  8331.         INC     D               ; else restore statement.
  8332.         CALL    L198B           ; routine EACH-STMT finds the D'th statement
  8333.                                 ; address as E does not contain a token.
  8334.         JR      Z,L1BF4         ; forward to STMT-NEXT if address found.
  8335.  
  8336. ;; REPORT-N
  8337. L1BEC:  RST     08H             ; ERROR-1
  8338.         DB    $16             ; Error Report: Statement lost
  8339.  
  8340. ; -----------------
  8341. ; End of statement?
  8342. ; -----------------
  8343. ; This combination of routines is called from 20 places when
  8344. ; the end of a statement should have been reached and all preceding
  8345. ; syntax is in order.
  8346.  
  8347. ;; CHECK-END
  8348. L1BEE:  CALL    L2530           ; routine SYNTAX-Z
  8349.         RET     NZ              ; return immediately in runtime
  8350.  
  8351.         POP     BC              ; drop address of calling routine.
  8352.         POP     BC              ; drop address STMT-RET.
  8353.                                 ; and continue to find next statement.
  8354.  
  8355. ; --------------------
  8356. ; Go to next statement
  8357. ; --------------------
  8358. ; Acceptable characters at this point are carriage return and ':'.
  8359. ; If so go to next statement which in the first case will be on next line.
  8360.  
  8361. ;; STMT-NEXT
  8362. L1BF4           IF BAS48_ONLY
  8363.                 RST 0X18
  8364.                 CP 0X0D
  8365.                 ELSE
  8366.                 CALL L3B5D              ; Spectrum 128 patch
  8367.                 ENDIF
  8368.  
  8369. L1BF7:  JR      Z,L1BB3         ; back to LINE-END if so.
  8370.  
  8371.         CP      $3A             ; is it ':' ?
  8372.         JP      Z,L1B28         ; jump back to STMT-LOOP to consider
  8373.                                 ; further statements
  8374.  
  8375.         JP      L1C8A           ; jump to REPORT-C with any other character
  8376.                                 ; 'Nonsense in BASIC'.
  8377.  
  8378. ; Note. the two-byte sequence 'rst 08; DB $0b' could replace the above jp.
  8379.  
  8380. ; -------------------
  8381. ; Command class table
  8382. ; -------------------
  8383. ;
  8384.  
  8385. ;; class-tbl
  8386. L1C01:  DB    L1C10 - $       ; 0F offset to Address: CLASS-00
  8387.         DB    L1C1F - $       ; 1D offset to Address: CLASS-01
  8388.         DB    L1C4E - $       ; 4B offset to Address: CLASS-02
  8389.         DB    L1C0D - $       ; 09 offset to Address: CLASS-03
  8390.         DB    L1C6C - $       ; 67 offset to Address: CLASS-04
  8391.         DB    L1C11 - $       ; 0B offset to Address: CLASS-05
  8392.         DB    L1C82 - $       ; 7B offset to Address: CLASS-06
  8393.         DB    L1C96 - $       ; 8E offset to Address: CLASS-07
  8394.         DB    L1C7A - $       ; 71 offset to Address: CLASS-08
  8395.         DB    L1CBE - $       ; B4 offset to Address: CLASS-09
  8396.         DB    L1C8C - $       ; 81 offset to Address: CLASS-0A
  8397.         DB    L1CDB - $       ; CF offset to Address: CLASS-0B
  8398.  
  8399.  
  8400. ; --------------------------------
  8401. ; Command classes---00, 03, and 05
  8402. ; --------------------------------
  8403. ; class-03 e.g RUN or RUN 200   ;  optional operand
  8404. ; class-00 e.g CONTINUE         ;  no operand
  8405. ; class-05 e.g PRINT            ;  variable syntax checked by routine
  8406.  
  8407. ;; CLASS-03
  8408. L1C0D:  CALL    L1CDE           ; routine FETCH-NUM
  8409.  
  8410. ;; CLASS-00
  8411.  
  8412. L1C10:  CP      A               ; reset zero flag.
  8413.  
  8414. ; if entering here then all class routines are entered with zero reset.
  8415.  
  8416. ;; CLASS-05
  8417. L1C11:  POP     BC              ; drop address SCAN-LOOP.
  8418.         CALL    Z,L1BEE         ; if zero set then call routine CHECK-END >>>
  8419.                                 ; as should be no further characters.
  8420.  
  8421.         EX      DE,HL           ; save HL to DE.
  8422.         LD      HL,($5C74)      ; fetch T_ADDR
  8423.         LD      C,(HL)          ; fetch low byte of routine
  8424.         INC     HL              ; address next.
  8425.         LD      B,(HL)          ; fetch high byte of routine.
  8426.         EX      DE,HL           ; restore HL from DE
  8427.         PUSH    BC              ; push the address
  8428.         RET                     ; and make an indirect jump to the command.
  8429.  
  8430. ; --------------------------------
  8431. ; Command classes---01, 02, and 04
  8432. ; --------------------------------
  8433. ; class-01  e.g LET A = 2*3     ; a variable is reqd
  8434.  
  8435. ; This class routine is also called from INPUT and READ to find the
  8436. ; destination variable for an assignment.
  8437.  
  8438. ;; CLASS-01
  8439. L1C1F:  CALL    L28B2           ; routine LOOK-VARS returns carry set if not
  8440.                                 ; found in runtime.
  8441.  
  8442. ; ----------------------
  8443. ; Variable in assignment
  8444. ; ----------------------
  8445. ;
  8446. ;
  8447.  
  8448. ;; VAR-A-1
  8449. L1C22:  LD      (IY+$37),$00    ; set FLAGX to zero
  8450.         JR      NC,L1C30        ; forward to VAR-A-2 if found or checking
  8451.                                 ; syntax.
  8452.  
  8453.         SET     1,(IY+$37)      ; FLAGX  - Signal a new variable
  8454.         JR      NZ,L1C46        ; to VAR-A-3 if not assigning to an array
  8455.                                 ; e.g. LET a$(3,3) = "X"
  8456.  
  8457. ;; REPORT-2
  8458. L1C2E:  RST     08H             ; ERROR-1
  8459.         DB    $01             ; Error Report: Variable not found
  8460.  
  8461. ;; VAR-A-2
  8462. L1C30:  CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  8463.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8464.         JR      NZ,L1C46        ; to VAR-A-3 if numeric
  8465.  
  8466.         XOR     A               ; default to array/slice - to be retained.
  8467.         CALL    L2530           ; routine SYNTAX-Z
  8468.         CALL    NZ,L2BF1        ; routine STK-FETCH is called in runtime
  8469.                                 ; may overwrite A with 1.
  8470.         LD      HL,$5C71        ; address system variable FLAGX
  8471.         OR      (HL)            ; set bit 0 if simple variable to be reclaimed
  8472.         LD      (HL),A          ; update FLAGX
  8473.         EX      DE,HL           ; start of string/subscript to DE
  8474.  
  8475. ;; VAR-A-3
  8476. L1C46:  LD      ($5C72),BC      ; update STRLEN
  8477.         LD      ($5C4D),HL      ; and DEST of assigned string.
  8478.         RET                     ; return.
  8479.  
  8480. ; -------------------------------------------------
  8481. ; class-02 e.g. LET a = 1 + 1   ; an expression must follow
  8482.  
  8483. ;; CLASS-02
  8484. L1C4E:  POP     BC              ; drop return address SCAN-LOOP
  8485.         CALL    L1C56           ; routine VAL-FET-1 is called to check
  8486.                                 ; expression and assign result in runtime
  8487.         CALL    L1BEE           ; routine CHECK-END checks nothing else
  8488.                                 ; is present in statement.
  8489.         RET                     ; Return
  8490.  
  8491. ; -------------
  8492. ; Fetch a value
  8493. ; -------------
  8494. ;
  8495. ;
  8496.  
  8497. ;; VAL-FET-1
  8498. L1C56:  LD      A,($5C3B)       ; initial FLAGS to A
  8499.  
  8500. ;; VAL-FET-2
  8501. L1C59:  PUSH    AF              ; save A briefly
  8502.         CALL    L24FB           ; routine SCANNING evaluates expression.
  8503.         POP     AF              ; restore A
  8504.         LD      D,(IY+$01)      ; post-SCANNING FLAGS to D
  8505.         XOR     D               ; xor the two sets of flags
  8506.         AND     $40             ; pick up bit 6 of xored FLAGS should be zero
  8507.         JR      NZ,L1C8A        ; forward to REPORT-C if not zero
  8508.                                 ; 'Nonsense in BASIC' - results don't agree.
  8509.  
  8510.         BIT     7,D             ; test FLAGS - is syntax being checked ?
  8511.         JP      NZ,L2AFF        ; jump forward to LET to make the assignment
  8512.                                 ; in runtime.
  8513.  
  8514.         RET                     ; but return from here if checking syntax.
  8515.  
  8516. ; ------------------
  8517. ; Command class---04
  8518. ; ------------------
  8519. ; class-04 e.g. FOR i            ; a single character variable must follow
  8520.  
  8521. ;; CLASS-04
  8522. L1C6C:  CALL    L28B2           ; routine LOOK-VARS
  8523.         PUSH    AF              ; preserve flags.
  8524.         LD      A,C             ; fetch type - should be 011xxxxx
  8525.         OR      $9F             ; combine with 10011111.
  8526.         INC     A               ; test if now $FF by incrementing.
  8527.         JR      NZ,L1C8A        ; forward to REPORT-C if result not zero.
  8528.  
  8529.         POP     AF              ; else restore flags.
  8530.         JR      L1C22           ; back to VAR-A-1
  8531.  
  8532.  
  8533. ; --------------------------------
  8534. ; Expect numeric/string expression
  8535. ; --------------------------------
  8536. ; This routine is used to get the two coordinates of STRING$, ATTR and POINT.
  8537. ; It is also called from PRINT-ITEM to get the two numeric expressions that
  8538. ; follow the AT ( in PRINT AT, INPUT AT).
  8539.  
  8540. ;; NEXT-2NUM
  8541. L1C79:  RST     20H             ; NEXT-CHAR advance past 'AT' or '('.
  8542.  
  8543. ; --------
  8544. ; class-08 e.g POKE 65535,2     ; two numeric expressions separated by comma
  8545. ;; CLASS-08
  8546. ;; EXPT-2NUM
  8547. L1C7A:  CALL    L1C82           ; routine EXPT-1NUM is called for first
  8548.                                 ; numeric expression
  8549.         CP      $2C             ; is character ',' ?
  8550.         JR      NZ,L1C8A        ; to REPORT-C if not required separator.
  8551.                                 ; 'Nonsense in BASIC'.
  8552.  
  8553.         RST     20H             ; NEXT-CHAR
  8554.  
  8555. ; ->
  8556. ;  class-06  e.g. GOTO a*1000   ; a numeric expression must follow
  8557. ;; CLASS-06
  8558. ;; EXPT-1NUM
  8559. L1C82:  CALL    L24FB           ; routine SCANNING
  8560.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8561.         RET     NZ              ; return if result is numeric.
  8562.  
  8563. ;; REPORT-C
  8564. L1C8A:  RST     08H             ; ERROR-1
  8565.         DB    $0B             ; Error Report: Nonsense in BASIC
  8566.  
  8567. ; ---------------------------------------------------------------
  8568. ; class-0A e.g. ERASE "????"    ; a string expression must follow.
  8569. ;                               ; these only occur in unimplemented commands
  8570. ;                               ; although the routine expt-exp is called
  8571. ;                               ; from SAVE-ETC
  8572.  
  8573. ;; CLASS-0A
  8574. ;; EXPT-EXP
  8575. L1C8C:  CALL    L24FB           ; routine SCANNING
  8576.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8577.         RET     Z               ; return if string result.
  8578.  
  8579.         JR      L1C8A           ; back to REPORT-C if numeric.
  8580.  
  8581. ; ---------------------
  8582. ; Set permanent colours
  8583. ; class 07
  8584. ; ---------------------
  8585. ; class-07 e.g PAPER 6          ; a single class for a collection of
  8586. ;                               ; similar commands. Clever.
  8587. ;
  8588. ; Note. these commands should ensure that current channel is 'S'
  8589.  
  8590. ;; CLASS-07
  8591. L1C96:  BIT     7,(IY+$01)      ; test FLAGS - checking syntax only ?
  8592.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8593.         CALL    NZ,L0D4D        ; routine TEMPS is called in runtime.
  8594.         POP     AF              ; drop return address SCAN-LOOP
  8595.         LD      A,($5C74)       ; T_ADDR_lo to accumulator.
  8596.                                 ; points to '$07' entry + 1
  8597.                                 ; e.g. for INK points to $EC now
  8598.  
  8599. ; Note if you move alter the syntax table next line may have to be altered.
  8600.  
  8601. ; Note. For ZASM assembler replace following expression with SUB $13.
  8602.  
  8603. L1CA5           SUB LOW (L1AEB)-$D8     ; % 256 ; convert $EB to $D8 ('INK') etc.
  8604.                                 ; ( is SUB $13 in standard ROM )
  8605.  
  8606.         CALL    L21FC           ; routine CO-TEMP-4
  8607.         CALL    L1BEE           ; routine CHECK-END check that nothing else
  8608.                                 ; in statement.
  8609.  
  8610. ; return here in runtime.
  8611.  
  8612.         LD      HL,($5C8F)      ; pick up ATTR_T and MASK_T
  8613.         LD      ($5C8D),HL      ; and store in ATTR_P and MASK_P
  8614.         LD      HL,$5C91        ; point to P_FLAG.
  8615.         LD      A,(HL)          ; pick up in A
  8616.         RLCA                    ; rotate to left
  8617.         XOR     (HL)            ; combine with HL
  8618.         AND     $AA             ; 10101010
  8619.         XOR     (HL)            ; only permanent bits affected
  8620.         LD      (HL),A          ; reload into P_FLAG.
  8621.         RET                     ; return.
  8622.  
  8623. ; ------------------
  8624. ; Command class---09
  8625. ; ------------------
  8626. ; e.g. PLOT PAPER 0; 128,88     ; two coordinates preceded by optional
  8627. ;                               ; embedded colour items.
  8628. ;
  8629. ; Note. this command should ensure that current channel is actually 'S'.
  8630.  
  8631. ;; CLASS-09
  8632. L1CBE:  CALL    L2530           ; routine SYNTAX-Z
  8633.         JR      Z,L1CD6         ; forward to CL-09-1 if checking syntax.
  8634.  
  8635.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8636.         CALL    L0D4D           ; routine TEMPS is called.
  8637.         LD      HL,$5C90        ; point to MASK_T
  8638.         LD      A,(HL)          ; fetch mask to accumulator.
  8639.         OR      $F8             ; or with 11111000 paper/bright/flash 8
  8640.         LD      (HL),A          ; mask back to MASK_T system variable.
  8641.         RES     6,(IY+$57)      ; reset P_FLAG  - signal NOT PAPER 9 ?
  8642.  
  8643.         RST     18H             ; GET-CHAR
  8644.  
  8645. ;; CL-09-1
  8646. L1CD6:  CALL    L21E2           ; routine CO-TEMP-2 deals with any embedded
  8647.                                 ; colour items.
  8648.         JR      L1C7A           ; exit via EXPT-2NUM to check for x,y.
  8649.  
  8650. ; Note. if either of the numeric expressions contain STR$ then the flag setting
  8651. ; above will be undone when the channel flags are reset during STR$.
  8652. ; e.g.
  8653. ; 10 BORDER 3 : PLOT VAL STR$ 128, VAL STR$ 100
  8654. ; credit John Elliott.
  8655.  
  8656. ; ------------------
  8657. ; Command class---0B
  8658. ; ------------------
  8659. ; Again a single class for four commands.
  8660. ; This command just jumps back to SAVE-ETC to handle the four tape commands.
  8661. ; The routine itself works out which command has called it by examining the
  8662. ; address in T_ADDR_lo. Note therefore that the syntax table has to be
  8663. ; located where these and other sequential command addresses are not split
  8664. ; over a page boundary.
  8665.  
  8666. ;; CLASS-0B
  8667. L1CDB:  JP      L0605           ; jump way back to SAVE-ETC
  8668.  
  8669. ; --------------
  8670. ; Fetch a number
  8671. ; --------------
  8672. ; This routine is called from CLASS-03 when a command may be followed by
  8673. ; an optional numeric expression e.g. RUN. If the end of statement has
  8674. ; been reached then zero is used as the default.
  8675. ; Also called from LIST-4.
  8676.  
  8677. ;; FETCH-NUM
  8678. L1CDE:  CP      $0D             ; is character a carriage return ?
  8679.         JR      Z,L1CE6         ; forward to USE-ZERO if so
  8680.  
  8681.         CP      $3A             ; is it ':' ?
  8682.         JR      NZ,L1C82        ; forward to EXPT-1NUM if not.
  8683.                                 ; else continue and use zero.
  8684.  
  8685. ; ----------------
  8686. ; Use zero routine
  8687. ; ----------------
  8688. ; This routine is called four times to place the value zero on the
  8689. ; calculator stack as a default value in runtime.
  8690.  
  8691. ;; USE-ZERO
  8692. L1CE6:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8693.         RET     Z               ;
  8694.  
  8695.         RST     28H             ;; FP-CALC
  8696.         DB    $A0             ;;stk-zero       ;0.
  8697.         DB    $38             ;;end-calc
  8698.  
  8699.         RET                     ; return.
  8700.  
  8701. ; -------------------
  8702. ; Handle STOP command
  8703. ; -------------------
  8704. ; Command Syntax: STOP
  8705. ; One of the shortest and least used commands. As with 'OK' not an error.
  8706.  
  8707. ;; REPORT-9
  8708. ;; STOP
  8709. L1CEE:  RST     08H             ; ERROR-1
  8710.         DB    $08             ; Error Report: STOP statement
  8711.  
  8712. ; -----------------
  8713. ; Handle IF command
  8714. ; -----------------
  8715. ; e.g. IF score>100 THEN PRINT "You Win"
  8716. ; The parser has already checked the expression the result of which is on
  8717. ; the calculator stack. The presence of the 'THEN' separator has also been
  8718. ; checked and CH-ADD points to the command after THEN.
  8719. ;
  8720.  
  8721. ;; IF
  8722. L1CF0:  POP     BC              ; drop return address - STMT-RET
  8723.         CALL    L2530           ; routine SYNTAX-Z
  8724.         JR      Z,L1D00         ; forward to IF-1 if checking syntax
  8725.                                 ; to check syntax of PRINT "You Win"
  8726.  
  8727.  
  8728.         RST     28H             ;; FP-CALC    score>100 (1=TRUE 0=FALSE)
  8729.         DB    $02             ;;delete      .
  8730.         DB    $38             ;;end-calc
  8731.  
  8732.         EX      DE,HL           ; make HL point to deleted value
  8733.         CALL    L34E9           ; routine TEST-ZERO
  8734.         JP      C,L1BB3         ; jump to LINE-END if FALSE (0)
  8735.  
  8736. ;; IF-1
  8737. L1D00:  JP      L1B29           ; to STMT-L-1, if true (1) to execute command
  8738.                                 ; after 'THEN' token.
  8739.  
  8740. ; ------------------
  8741. ; Handle FOR command
  8742. ; ------------------
  8743. ; e.g. FOR i = 0 TO 1 STEP 0.1
  8744. ; Using the syntax tables, the parser has already checked for a start and
  8745. ; limit value and also for the intervening separator.
  8746. ; the two values v,l are on the calculator stack.
  8747. ; CLASS-04 has also checked the variable and the name is in STRLEN_lo.
  8748. ; The routine begins by checking for an optional STEP.
  8749.  
  8750. ;; FOR
  8751. L1D03:  CP      $CD             ; is there a 'STEP' ?
  8752.         JR      NZ,L1D10        ; to F-USE-1 if not to use 1 as default.
  8753.  
  8754.         RST     20H             ; NEXT-CHAR
  8755.         CALL    L1C82           ; routine EXPT-1NUM
  8756.         CALL    L1BEE           ; routine CHECK-END
  8757.         JR      L1D16           ; to F-REORDER
  8758.  
  8759. ; ---
  8760.  
  8761. ;; F-USE-1
  8762. L1D10:  CALL    L1BEE           ; routine CHECK-END
  8763.  
  8764.         RST     28H             ;; FP-CALC      v,l.
  8765.         DB    $A1             ;;stk-one       v,l,1=s.
  8766.         DB    $38             ;;end-calc
  8767.  
  8768.  
  8769. ;; F-REORDER
  8770. L1D16:  RST     28H             ;; FP-CALC       v,l,s.
  8771.         DB    $C0             ;;st-mem-0       v,l,s.
  8772.         DB    $02             ;;delete         v,l.
  8773.         DB    $01             ;;exchange       l,v.
  8774.         DB    $E0             ;;get-mem-0      l,v,s.
  8775.         DB    $01             ;;exchange       l,s,v.
  8776.         DB    $38             ;;end-calc
  8777.  
  8778.         CALL    L2AFF           ; routine LET assigns the initial value v to
  8779.                                 ; the variable altering type if necessary.
  8780.         LD      ($5C68),HL      ; The system variable MEM is made to point to
  8781.                                 ; the variable instead of its normal
  8782.                                 ; location MEMBOT
  8783.         DEC     HL              ; point to single-character name
  8784.         LD      A,(HL)          ; fetch name
  8785.         SET     7,(HL)          ; set bit 7 at location
  8786.         LD      BC,$0006        ; add six to HL
  8787.         ADD     HL,BC           ; to address where limit should be.
  8788.         RLCA                    ; test bit 7 of original name.
  8789.         JR      C,L1D34         ; forward to F-L-S if already a FOR/NEXT
  8790.                                 ; variable
  8791.  
  8792.         LD      C,$0D           ; otherwise an additional 13 bytes are needed.
  8793.                                 ; 5 for each value, two for line number and
  8794.                                 ; 1 byte for looping statement.
  8795.         CALL    L1655           ; routine MAKE-ROOM creates them.
  8796.         INC     HL              ; make HL address limit.
  8797.  
  8798. ;; F-L-S
  8799. L1D34:  PUSH    HL              ; save position.
  8800.  
  8801.         RST     28H             ;; FP-CALC         l,s.
  8802.         DB    $02             ;;delete           l.
  8803.         DB    $02             ;;delete           .
  8804.         DB    $38             ;;end-calc
  8805.                                 ; DE points to STKEND, l.
  8806.  
  8807.         POP     HL              ; restore variable position
  8808.         EX      DE,HL           ; swap pointers
  8809.         LD      C,$0A           ; ten bytes to move
  8810.         LDIR                    ; Copy 'deleted' values to variable.
  8811.         LD      HL,($5C45)      ; Load with current line number from PPC
  8812.         EX      DE,HL           ; exchange pointers.
  8813.         LD      (HL),E          ; save the looping line
  8814.         INC     HL              ; in the next
  8815.         LD      (HL),D          ; two locations.
  8816.         LD      D,(IY+$0D)      ; fetch statement from SUBPPC system variable.
  8817.         INC     D               ; increment statement.
  8818.         INC     HL              ; and pointer
  8819.         LD      (HL),D          ; and store the looping statement.
  8820.                                 ;
  8821.         CALL    L1DDA           ; routine NEXT-LOOP considers an initial
  8822.         RET     NC              ; iteration. Return to STMT-RET if a loop is
  8823.                                 ; possible to execute next statement.
  8824.  
  8825. ; no loop is possible so execution continues after the matching 'NEXT'
  8826.  
  8827.         LD      B,(IY+$38)      ; get single-character name from STRLEN_lo
  8828.         LD      HL,($5C45)      ; get the current line from PPC
  8829.         LD      ($5C42),HL      ; and store it in NEWPPC
  8830.         LD      A,($5C47)       ; fetch current statement from SUBPPC
  8831.         NEG                     ; Negate as counter decrements from zero
  8832.                                 ; initially and we are in the middle of a
  8833.                                 ; line.
  8834.         LD      D,A             ; Store result in D.
  8835.         LD      HL,($5C5D)      ; get current address from CH_ADD
  8836.         LD      E,$F3           ; search will be for token 'NEXT'
  8837.  
  8838. ;; F-LOOP
  8839. L1D64:  PUSH    BC              ; save variable name.
  8840.         LD      BC,($5C55)      ; fetch NXTLIN
  8841.         CALL    L1D86           ; routine LOOK-PROG searches for 'NEXT' token.
  8842.         LD      ($5C55),BC      ; update NXTLIN
  8843.         POP     BC              ; and fetch the letter
  8844.         JR      C,L1D84         ; forward to REPORT-I if the end of program
  8845.                                 ; was reached by LOOK-PROG.
  8846.                                 ; 'FOR without NEXT'
  8847.  
  8848.         RST     20H             ; NEXT-CHAR fetches character after NEXT
  8849.         OR      $20             ; ensure it is upper-case.
  8850.         CP      B               ; compare with FOR variable name
  8851.         JR      Z,L1D7C         ; forward to F-FOUND if it matches.
  8852.  
  8853. ; but if no match i.e. nested FOR/NEXT loops then continue search.
  8854.  
  8855.         RST     20H             ; NEXT-CHAR
  8856.         JR      L1D64           ; back to F-LOOP
  8857.  
  8858. ; ---
  8859.  
  8860.  
  8861. ;; F-FOUND
  8862. L1D7C:  RST     20H             ; NEXT-CHAR
  8863.         LD      A,$01           ; subtract the negated counter from 1
  8864.         SUB     D               ; to give the statement after the NEXT
  8865.         LD      ($5C44),A       ; set system variable NSPPC
  8866.         RET                     ; return to STMT-RET to branch to new
  8867.                                 ; line and statement. ->
  8868. ; ---
  8869.  
  8870. ;; REPORT-I
  8871. L1D84:  RST     08H             ; ERROR-1
  8872.         DB    $11             ; Error Report: FOR without NEXT
  8873.  
  8874. ; ---------
  8875. ; LOOK-PROG
  8876. ; ---------
  8877. ; Find DATA, DEF FN or NEXT.
  8878. ; This routine searches the program area for one of the above three keywords.
  8879. ; On entry, HL points to start of search area.
  8880. ; The token is in E, and D holds a statement count, decremented from zero.
  8881.  
  8882. ;; LOOK-PROG
  8883. L1D86:  LD      A,(HL)          ; fetch current character
  8884.         CP      $3A             ; is it ':' a statement separator ?
  8885.         JR      Z,L1DA3         ; forward to LOOK-P-2 if so.
  8886.  
  8887. ; The starting point was PROG - 1 or the end of a line.
  8888.  
  8889. ;; LOOK-P-1
  8890. L1D8B:  INC     HL              ; increment pointer to address
  8891.         LD      A,(HL)          ; the high byte of line number
  8892.         AND     $C0             ; test for program end marker $80 or a
  8893.                                 ; variable
  8894.         SCF                     ; Set Carry Flag
  8895.         RET     NZ              ; return with carry set if at end
  8896.                                 ; of program.           ->
  8897.  
  8898.         LD      B,(HL)          ; high byte of line number to B
  8899.         INC     HL              ;
  8900.         LD      C,(HL)          ; low byte to C.
  8901.         LD      ($5C42),BC      ; set system variable NEWPPC.
  8902.         INC     HL              ;
  8903.         LD      C,(HL)          ; low byte of line length to C.
  8904.         INC     HL              ;
  8905.         LD      B,(HL)          ; high byte to B.
  8906.         PUSH    HL              ; save address
  8907.         ADD     HL,BC           ; add length to position.
  8908.         LD      B,H             ; and save result
  8909.         LD      C,L             ; in BC.
  8910.         POP     HL              ; restore address.
  8911.         LD      D,$00           ; initialize statement counter to zero.
  8912.  
  8913. ;; LOOK-P-2
  8914. L1DA3:  PUSH    BC              ; save address of next line
  8915.         CALL    L198B           ; routine EACH-STMT searches current line.
  8916.         POP     BC              ; restore address.
  8917.         RET     NC              ; return if match was found. ->
  8918.  
  8919.         JR      L1D8B           ; back to LOOK-P-1 for next line.
  8920.  
  8921. ; -------------------
  8922. ; Handle NEXT command
  8923. ; -------------------
  8924. ; e.g. NEXT i
  8925. ; The parameter tables have already evaluated the presence of a variable
  8926.  
  8927. ;; NEXT
  8928. L1DAB:  BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  8929.         JP      NZ,L1C2E        ; jump back to REPORT-2 if so
  8930.                                 ; 'Variable not found'
  8931.  
  8932. ; now test if found variable is a simple variable uninitialized by a FOR.
  8933.  
  8934.         LD      HL,($5C4D)      ; load address of variable from DEST
  8935.         BIT     7,(HL)          ; is it correct type ?
  8936.         JR      Z,L1DD8         ; forward to REPORT-1 if not
  8937.                                 ; 'NEXT without FOR'
  8938.  
  8939.         INC     HL              ; step past variable name
  8940.         LD      ($5C68),HL      ; and set MEM to point to three 5-byte values
  8941.                                 ; value, limit, step.
  8942.  
  8943.         RST     28H             ;; FP-CALC     add step and re-store
  8944.         DB    $E0             ;;get-mem-0    v.
  8945.         DB    $E2             ;;get-mem-2    v,s.
  8946.         DB    $0F             ;;addition     v+s.
  8947.         DB    $C0             ;;st-mem-0     v+s.
  8948.         DB    $02             ;;delete       .
  8949.         DB    $38             ;;end-calc
  8950.  
  8951.         CALL    L1DDA           ; routine NEXT-LOOP tests against limit.
  8952.         RET     C               ; return if no more iterations possible.
  8953.  
  8954.         LD      HL,($5C68)      ; find start of variable contents from MEM.
  8955.         LD      DE,$000F        ; add 3*5 to
  8956.         ADD     HL,DE           ; address the looping line number
  8957.         LD      E,(HL)          ; low byte to E
  8958.         INC     HL              ;
  8959.         LD      D,(HL)          ; high byte to D
  8960.         INC     HL              ; address looping statement
  8961.         LD      H,(HL)          ; and store in H
  8962.         EX      DE,HL           ; swap registers
  8963.         JP      L1E73           ; exit via GO-TO-2 to execute another loop.
  8964.  
  8965. ; ---
  8966.  
  8967. ;; REPORT-1
  8968. L1DD8:  RST     08H             ; ERROR-1
  8969.         DB    $00             ; Error Report: NEXT without FOR
  8970.  
  8971.  
  8972. ; -----------------
  8973. ; Perform NEXT loop
  8974. ; -----------------
  8975. ; This routine is called from the FOR command to test for an initial
  8976. ; iteration and from the NEXT command to test for all subsequent iterations.
  8977. ; the system variable MEM addresses the variable's contents which, in the
  8978. ; latter case, have had the step, possibly negative, added to the value.
  8979.  
  8980. ;; NEXT-LOOP
  8981. L1DDA:  RST     28H             ;; FP-CALC
  8982.         DB    $E1             ;;get-mem-1        l.
  8983.         DB    $E0             ;;get-mem-0        l,v.
  8984.         DB    $E2             ;;get-mem-2        l,v,s.
  8985.         DB    $36             ;;less-0           l,v,(1/0) negative step ?
  8986.         DB    $00             ;;jump-true        l,v.(1/0)
  8987.  
  8988.         DB    $02             ;;to L1DE2, NEXT-1 if step negative
  8989.  
  8990.         DB    $01             ;;exchange         v,l.
  8991.  
  8992. ;; NEXT-1
  8993. L1DE2:  DB    $03             ;;subtract         l-v OR v-l.
  8994.         DB    $37             ;;greater-0        (1/0)
  8995.         DB    $00             ;;jump-true        .
  8996.  
  8997.         DB    $04             ;;to L1DE9, NEXT-2 if no more iterations.
  8998.  
  8999.         DB    $38             ;;end-calc         .
  9000.  
  9001.         AND     A               ; clear carry flag signalling another loop.
  9002.         RET                     ; return
  9003.  
  9004. ; ---
  9005.  
  9006. ;; NEXT-2
  9007. L1DE9:  DB    $38             ;;end-calc         .
  9008.  
  9009.         SCF                     ; set carry flag signalling looping exhausted.
  9010.         RET                     ; return
  9011.  
  9012.  
  9013. ; -------------------
  9014. ; Handle READ command
  9015. ; -------------------
  9016. ; e.g. READ a, b$, c$(1000 TO 3000)
  9017. ; A list of comma-separated variables is assigned from a list of
  9018. ; comma-separated expressions.
  9019. ; As it moves along the first list, the character address CH_ADD is stored
  9020. ; in X_PTR while CH_ADD is used to read the second list.
  9021.  
  9022. ;; READ-3
  9023. L1DEC:  RST     20H             ; NEXT-CHAR
  9024.  
  9025. ; -> Entry point.
  9026. ;; READ
  9027. L1DED:  CALL    L1C1F           ; routine CLASS-01 checks variable.
  9028.         CALL    L2530           ; routine SYNTAX-Z
  9029.         JR      Z,L1E1E         ; forward to READ-2 if checking syntax
  9030.  
  9031.  
  9032.         RST     18H             ; GET-CHAR
  9033.         LD      ($5C5F),HL      ; save character position in X_PTR.
  9034.         LD      HL,($5C57)      ; load HL with Data Address DATADD, which is
  9035.                                 ; the start of the program or the address
  9036.                                 ; after the last expression that was read or
  9037.                                 ; the address of the line number of the
  9038.                                 ; last RESTORE command.
  9039.         LD      A,(HL)          ; fetch character
  9040.         CP      $2C             ; is it a comma ?
  9041.         JR      Z,L1E0A         ; forward to READ-1 if so.
  9042.  
  9043. ; else all data in this statement has been read so look for next DATA token
  9044.  
  9045.         LD      E,$E4           ; token 'DATA'
  9046.         CALL    L1D86           ; routine LOOK-PROG
  9047.         JR      NC,L1E0A        ; forward to READ-1 if DATA found
  9048.  
  9049. ; else report the error.
  9050.  
  9051. ;; REPORT-E
  9052. L1E08:  RST     08H             ; ERROR-1
  9053.         DB    $0D             ; Error Report: Out of DATA
  9054.  
  9055. ;; READ-1
  9056. L1E0A:  CALL    L0077           ; routine TEMP-PTR1 advances updating CH_ADD
  9057.                                 ; with new DATADD position.
  9058.         CALL    L1C56           ; routine VAL-FET-1 assigns value to variable
  9059.                                 ; checking type match and adjusting CH_ADD.
  9060.  
  9061.         RST     18H             ; GET-CHAR fetches adjusted character position
  9062.         LD      ($5C57),HL      ; store back in DATADD
  9063.         LD      HL,($5C5F)      ; fetch X_PTR  the original READ CH_ADD
  9064.         LD      (IY+$26),$00    ; now nullify X_PTR_hi
  9065.         CALL    L0078           ; routine TEMP-PTR2 restores READ CH_ADD
  9066.  
  9067. ;; READ-2
  9068. L1E1E:  RST     18H             ; GET-CHAR
  9069.         CP      $2C             ; is it ',' indicating more variables to read ?
  9070.         JR      Z,L1DEC         ; back to READ-3 if so
  9071.  
  9072.         CALL    L1BEE           ; routine CHECK-END
  9073.         RET                     ; return from here in runtime to STMT-RET.
  9074.  
  9075. ; -------------------
  9076. ; Handle DATA command
  9077. ; -------------------
  9078. ; In runtime this 'command' is passed by but the syntax is checked when such
  9079. ; a statement is found while parsing a line.
  9080. ; e.g. DATA 1, 2, "text", score-1, a$(location, room, object), FN r(49),
  9081. ;         wages - tax, TRUE, The meaning of life
  9082.  
  9083. ;; DATA
  9084. L1E27:  CALL    L2530           ; routine SYNTAX-Z to check status
  9085.         JR      NZ,L1E37        ; forward to DATA-2 if in runtime
  9086.  
  9087. ;; DATA-1
  9088. L1E2C:  CALL    L24FB           ; routine SCANNING to check syntax of
  9089.                                 ; expression
  9090.         CP      $2C             ; is it a comma ?
  9091.         CALL    NZ,L1BEE        ; routine CHECK-END checks that statement
  9092.                                 ; is complete. Will make an early exit if
  9093.                                 ; so. >>>
  9094.         RST     20H             ; NEXT-CHAR
  9095.         JR      L1E2C           ; back to DATA-1
  9096.  
  9097. ; ---
  9098.  
  9099. ;; DATA-2
  9100. L1E37:  LD      A,$E4           ; set token to 'DATA' and continue into
  9101.                                 ; the the PASS-BY routine.
  9102.  
  9103.  
  9104. ; ----------------------------------
  9105. ; Check statement for DATA or DEF FN
  9106. ; ----------------------------------
  9107. ; This routine is used to backtrack to a command token and then
  9108. ; forward to the next statement in runtime.
  9109.  
  9110. ;; PASS-BY
  9111. L1E39:  LD      B,A             ; Give BC enough space to find token.
  9112.         CPDR                    ; Compare decrement and repeat. (Only use).
  9113.                                 ; Work backwards till keyword is found which
  9114.                                 ; is start of statement before any quotes.
  9115.                                 ; HL points to location before keyword.
  9116.         LD      DE,$0200        ; count 1+1 statements, dummy value in E to
  9117.                                 ; inhibit searching for a token.
  9118.         JP      L198B           ; to EACH-STMT to find next statement
  9119.  
  9120. ; -----------------------------------------------------------------------
  9121. ; A General Note on Invalid Line Numbers.
  9122. ; =======================================
  9123. ; One of the revolutionary concepts of Sinclair BASIC was that it supported
  9124. ; virtual line numbers. That is the destination of a GO TO, RESTORE etc. need
  9125. ; not exist. It could be a point before or after an actual line number.
  9126. ; Zero suffices for a before but the after should logically be infinity.
  9127. ; Since the maximum actual line limit is 9999 then the system limit, 16383
  9128. ; when variables kick in, would serve fine as a virtual end point.
  9129. ; However, ironically, only the LOAD command gets it right. It will not
  9130. ; autostart a program that has been saved with a line higher than 16383.
  9131. ; All the other commands deal with the limit unsatisfactorily.
  9132. ; LIST, RUN, GO TO, GO SUB and RESTORE have problems and the latter may
  9133. ; crash the machine when supplied with an inappropriate virtual line number.
  9134. ; This is puzzling as very careful consideration must have been given to
  9135. ; this point when the new variable types were allocated their masks and also
  9136. ; when the routine NEXT-ONE was successfully re-written to reflect this.
  9137. ; An enigma.
  9138. ; -------------------------------------------------------------------------
  9139.  
  9140. ; ----------------------
  9141. ; Handle RESTORE command
  9142. ; ----------------------
  9143. ; The restore command sets the system variable for the data address to
  9144. ; point to the location before the supplied line number or first line
  9145. ; thereafter.
  9146. ; This alters the position where subsequent READ commands look for data.
  9147. ; Note. If supplied with inappropriate high numbers the system may crash
  9148. ; in the LINE-ADDR routine as it will pass the program/variables end-marker
  9149. ; and then lose control of what it is looking for - variable or line number.
  9150. ; - observation, Steven Vickers, 1984, Pitman.
  9151.  
  9152. ;; RESTORE
  9153. L1E42:  CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9154.                                 ; Note. B should be checked against limit $3F
  9155.                                 ; and an error generated if higher.
  9156.  
  9157. ; this entry point is used from RUN command with BC holding zero
  9158.  
  9159. ;; REST-RUN
  9160. L1E45:  LD      H,B             ; transfer the line
  9161.         LD      L,C             ; number to the HL register.
  9162.         CALL    L196E           ; routine LINE-ADDR to fetch the address.
  9163.         DEC     HL              ; point to the location before the line.
  9164.         LD      ($5C57),HL      ; update system variable DATADD.
  9165.         RET                     ; return to STMT-RET (or RUN)
  9166.  
  9167. ; ------------------------
  9168. ; Handle RANDOMIZE command
  9169. ; ------------------------
  9170. ; This command sets the SEED for the RND function to a fixed value.
  9171. ; With the parameter zero, a random start point is used depending on
  9172. ; how long the computer has been switched on.
  9173.  
  9174. ;; RANDOMIZE
  9175. L1E4F:  CALL    L1E99           ; routine FIND-INT2 puts parameter in BC.
  9176.         LD      A,B             ; test this
  9177.         OR      C               ; for zero.
  9178.         JR      NZ,L1E5A        ; forward to RAND-1 if not zero.
  9179.  
  9180.         LD      BC,($5C78)      ; use the lower two bytes at FRAMES1.
  9181.  
  9182. ;; RAND-1
  9183. L1E5A:  LD      ($5C76),BC      ; place in SEED system variable.
  9184.         RET                     ; return to STMT-RET
  9185.  
  9186. ; -----------------------
  9187. ; Handle CONTINUE command
  9188. ; -----------------------
  9189. ; The CONTINUE command transfers the OLD (but incremented) values of
  9190. ; line number and statement to the equivalent "NEW VALUE" system variables
  9191. ; by using the last part of GO TO and exits indirectly to STMT-RET.
  9192.  
  9193. ;; CONTINUE
  9194. L1E5F:  LD      HL,($5C6E)      ; fetch OLDPPC line number.
  9195.         LD      D,(IY+$36)      ; fetch OSPPC statement.
  9196.         JR      L1E73           ; forward to GO-TO-2
  9197.  
  9198. ; --------------------
  9199. ; Handle GO TO command
  9200. ; --------------------
  9201. ; The GO TO command routine is also called by GO SUB and RUN routines
  9202. ; to evaluate the parameters of both commands.
  9203. ; It updates the system variables used to fetch the next line/statement.
  9204. ; It is at STMT-RET that the actual change in control takes place.
  9205. ; Unlike some BASICs the line number need not exist.
  9206. ; Note. the high byte of the line number is incorrectly compared with $F0
  9207. ; instead of $3F. This leads to commands with operands greater than 32767
  9208. ; being considered as having been run from the editing area and the
  9209. ; error report 'Statement Lost' is given instead of 'OK'.
  9210. ; - Steven Vickers, 1984.
  9211.  
  9212. ;; GO-TO
  9213. L1E67:  CALL    L1E99           ; routine FIND-INT2 puts operand in BC
  9214.         LD      H,B             ; transfer line
  9215.         LD      L,C             ; number to HL.
  9216.         LD      D,$00           ; set statement to 0 - first.
  9217.         LD      A,H             ; compare high byte only
  9218.         CP      $F0             ; to $F0 i.e. 61439 in full.
  9219.         JR      NC,L1E9F        ; forward to REPORT-B if above.
  9220.  
  9221. ; This call entry point is used to update the system variables e.g. by RETURN.
  9222.  
  9223. ;; GO-TO-2
  9224. L1E73:  LD      ($5C42),HL      ; save line number in NEWPPC
  9225.         LD      (IY+$0A),D      ; and statement in NSPPC
  9226.         RET                     ; to STMT-RET (or GO-SUB command)
  9227.  
  9228. ; ------------------
  9229. ; Handle OUT command
  9230. ; ------------------
  9231. ; Syntax has been checked and the two comma-separated values are on the
  9232. ; calculator stack.
  9233.  
  9234. ;; OUT
  9235. L1E7A:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9236.                                 ; to BC and A.
  9237.         OUT     (C),A           ; perform the operation.
  9238.         RET                     ; return to STMT-RET.
  9239.  
  9240. ; -------------------
  9241. ; Handle POKE command
  9242. ; -------------------
  9243. ; This routine alters a single byte in the 64K address space.
  9244. ; Happily no check is made as to whether ROM or RAM is addressed.
  9245. ; Sinclair BASIC requires no poking of system variables.
  9246.  
  9247. ;; POKE
  9248. L1E80:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9249.                                 ; to BC and A.
  9250.         LD      (BC),A          ; load memory location with A.
  9251.         RET                     ; return to STMT-RET.
  9252.  
  9253. ; ------------------------------------
  9254. ; Fetch two  parameters from calculator stack
  9255. ; ------------------------------------
  9256. ; This routine fetches a byte and word from the calculator stack
  9257. ; producing an error if either is out of range.
  9258.  
  9259. ;; TWO-PARAM
  9260. L1E85:  CALL    L2DD5           ; routine FP-TO-A
  9261.         JR      C,L1E9F         ; forward to REPORT-B if overflow occurred
  9262.  
  9263.         JR      Z,L1E8E         ; forward to TWO-P-1 if positive
  9264.  
  9265.         NEG                     ; negative numbers are made positive
  9266.  
  9267. ;; TWO-P-1
  9268. L1E8E:  PUSH    AF              ; save the value
  9269.         CALL    L1E99           ; routine FIND-INT2 gets integer to BC
  9270.         POP     AF              ; restore the value
  9271.         RET                     ; return
  9272.  
  9273. ; -------------
  9274. ; Find integers
  9275. ; -------------
  9276. ; The first of these routines fetches a 8-bit integer (range 0-255) from the
  9277. ; calculator stack to the accumulator and is used for colours, streams,
  9278. ; durations and coordinates.
  9279. ; The second routine fetches 16-bit integers to the BC register pair
  9280. ; and is used to fetch command and function arguments involving line numbers
  9281. ; or memory addresses and also array subscripts and tab arguments.
  9282. ; ->
  9283.  
  9284. ;; FIND-INT1
  9285. L1E94:  CALL    L2DD5           ; routine FP-TO-A
  9286.         JR      L1E9C           ; forward to FIND-I-1 for common exit routine.
  9287.  
  9288. ; ---
  9289.  
  9290. ; ->
  9291.  
  9292. ;; FIND-INT2
  9293. L1E99:  CALL    L2DA2           ; routine FP-TO-BC
  9294.  
  9295. ;; FIND-I-1
  9296. L1E9C:  JR      C,L1E9F         ; to REPORT-Bb with overflow.
  9297.  
  9298.         RET     Z               ; return if positive.
  9299.  
  9300.  
  9301. ;; REPORT-Bb
  9302. L1E9F:  RST     08H             ; ERROR-1
  9303.         DB    $0A             ; Error Report: Integer out of range
  9304.  
  9305. ; ------------------
  9306. ; Handle RUN command
  9307. ; ------------------
  9308. ; This command runs a program starting at an optional line.
  9309. ; It performs a 'RESTORE 0' then CLEAR
  9310.  
  9311. ;; RUN
  9312. L1EA1:  CALL    L1E67           ; routine GO-TO puts line number in
  9313.                                 ; system variables.
  9314.         LD      BC,$0000        ; prepare to set DATADD to first line.
  9315.         CALL    L1E45           ; routine REST-RUN does the 'restore'.
  9316.                                 ; Note BC still holds zero.
  9317.         JR      L1EAF           ; forward to CLEAR-RUN to clear variables
  9318.                                 ; without disturbing RAMTOP and
  9319.                                 ; exit indirectly to STMT-RET
  9320.  
  9321. ; --------------------
  9322. ; Handle CLEAR command
  9323. ; --------------------
  9324. ; This command reclaims the space used by the variables.
  9325. ; It also clears the screen and the GO SUB stack.
  9326. ; With an integer expression, it sets the uppermost memory
  9327. ; address within the BASIC system.
  9328. ; "Contrary to the manual, CLEAR doesn't execute a RESTORE" -
  9329. ; Steven Vickers, Pitman Pocket Guide to the Spectrum, 1984.
  9330.  
  9331. ;; CLEAR
  9332. L1EAC:  CALL    L1E99           ; routine FIND-INT2 fetches to BC.
  9333.  
  9334. ;; CLEAR-RUN
  9335. L1EAF:  LD      A,B             ; test for
  9336.         OR      C               ; zero.
  9337.         JR      NZ,L1EB7        ; skip to CLEAR-1 if not zero.
  9338.  
  9339.         LD      BC,($5CB2)      ; use the existing value of RAMTOP if zero.
  9340.  
  9341. ;; CLEAR-1
  9342. L1EB7:  PUSH    BC              ; save ramtop value.
  9343.  
  9344.         LD      DE,($5C4B)      ; fetch VARS
  9345.         LD      HL,($5C59)      ; fetch E_LINE
  9346.         DEC     HL              ; adjust to point at variables end-marker.
  9347.         CALL    L19E5           ; routine RECLAIM-1 reclaims the space used by
  9348.                                 ; the variables.
  9349.         CALL    L0D6B           ; routine CLS to clear screen.
  9350.         LD      HL,($5C65)      ; fetch STKEND the start of free memory.
  9351.         LD      DE,$0032        ; allow for another 50 bytes.
  9352.         ADD     HL,DE           ; add the overhead to HL.
  9353.  
  9354.         POP     DE              ; restore the ramtop value.
  9355.         SBC     HL,DE           ; if HL is greater than the value then jump
  9356.         JR      NC,L1EDA        ; forward to REPORT-M
  9357.                                 ; 'RAMTOP no good'
  9358.  
  9359.         LD      HL,($5CB4)      ; now P-RAMT ($7FFF on 16K RAM machine)
  9360.         AND     A               ; exact this time.
  9361.         SBC     HL,DE           ; new ramtop must be lower or the same.
  9362.         JR      NC,L1EDC        ; skip to CLEAR-2 if in actual RAM.
  9363.  
  9364. ;; REPORT-M
  9365. L1EDA:  RST     08H             ; ERROR-1
  9366.         DB    $15             ; Error Report: RAMTOP no good
  9367.  
  9368. ;; CLEAR-2
  9369. L1EDC:  EX      DE,HL           ; transfer ramtop value to HL.
  9370.         LD      ($5CB2),HL      ; update system variable RAMTOP.
  9371.         POP     DE              ; pop the return address STMT-RET.
  9372.         POP     BC              ; pop the Error Address.
  9373.         LD      (HL),$3E        ; now put the GO SUB end-marker at RAMTOP.
  9374.         DEC     HL              ; leave a location beneath it.
  9375.         LD      SP,HL           ; initialize the machine stack pointer.
  9376.         PUSH    BC              ; push the error address.
  9377.         LD      ($5C3D),SP      ; make ERR_SP point to location.
  9378.         EX      DE,HL           ; put STMT-RET in HL.
  9379.         JP      (HL)            ; and go there directly.
  9380.  
  9381. ; ---------------------
  9382. ; Handle GO SUB command
  9383. ; ---------------------
  9384. ; The GO SUB command diverts BASIC control to a new line number
  9385. ; in a very similar manner to GO TO but
  9386. ; the current line number and current statement + 1
  9387. ; are placed on the GO SUB stack as a RETURN point.
  9388.  
  9389. ;; GO-SUB
  9390. L1EED:  POP     DE              ; drop the address STMT-RET
  9391.         LD      H,(IY+$0D)      ; fetch statement from SUBPPC and
  9392.         INC     H               ; increment it
  9393.         EX      (SP),HL         ; swap - error address to HL,
  9394.                                 ; H (statement) at top of stack,
  9395.                                 ; L (unimportant) beneath.
  9396.         INC     SP              ; adjust to overwrite unimportant byte
  9397.         LD      BC,($5C45)      ; fetch the current line number from PPC
  9398.         PUSH    BC              ; and PUSH onto GO SUB stack.
  9399.                                 ; the empty machine-stack can be rebuilt
  9400.         PUSH    HL              ; push the error address.
  9401.         LD      ($5C3D),SP      ; make system variable ERR_SP point to it.
  9402.         PUSH    DE              ; push the address STMT-RET.
  9403.         CALL    L1E67           ; call routine GO-TO to update the system
  9404.                                 ; variables NEWPPC and NSPPC.
  9405.                                 ; then make an indirect exit to STMT-RET via
  9406.         LD      BC,$0014        ; a 20-byte overhead memory check.
  9407.  
  9408. ; ----------------------
  9409. ; Check available memory
  9410. ; ----------------------
  9411. ; This routine is used on many occasions when extending a dynamic area
  9412. ; upwards or the GO SUB stack downwards.
  9413.  
  9414. ;; TEST-ROOM
  9415. L1F05:  LD      HL,($5C65)      ; fetch STKEND
  9416.         ADD     HL,BC           ; add the supplied test value
  9417.         JR      C,L1F15         ; forward to REPORT-4 if over $FFFF
  9418.  
  9419.         EX      DE,HL           ; was less so transfer to DE
  9420.         LD      HL,$0050        ; test against another 80 bytes
  9421.         ADD     HL,DE           ; anyway
  9422.         JR      C,L1F15         ; forward to REPORT-4 if this passes $FFFF
  9423.  
  9424.         SBC     HL,SP           ; if less than the machine stack pointer
  9425.         RET     C               ; then return - OK.
  9426.  
  9427. ;; REPORT-4
  9428. L1F15:  LD      L,$03           ; prepare 'Out of Memory'
  9429.         JP      L0055           ; jump back to ERROR-3 at $0055
  9430.                                 ; Note. this error can't be trapped at $0008
  9431.  
  9432. ; ------------------------------
  9433. ; THE 'FREE MEMORY' USER ROUTINE
  9434. ; ------------------------------
  9435. ; This routine is not used by the ROM but allows users to evaluate
  9436. ; approximate free memory with PRINT 65536 - USR 7962.
  9437.  
  9438. ;; free-mem
  9439. L1F1A:  LD      BC,$0000        ; allow no overhead.
  9440.  
  9441.         CALL    L1F05           ; routine TEST-ROOM.
  9442.  
  9443.         LD      B,H             ; transfer the result
  9444.         LD      C,L             ; to the BC register.
  9445.         RET                     ; the USR function returns value of BC.
  9446.  
  9447. ; --------------------
  9448. ; THE 'RETURN' COMMAND
  9449. ; --------------------
  9450. ; As with any command, there are two values on the machine stack at the time
  9451. ; it is invoked.  The machine stack is below the GOSUB stack.  Both grow
  9452. ; downwards, the machine stack by two bytes, the GOSUB stack by 3 bytes.
  9453. ; The highest location is a statement byte followed by a two-byte line number.
  9454.  
  9455. ;; RETURN
  9456. L1F23:  POP     BC              ; drop the address STMT-RET.
  9457.         POP     HL              ; now the error address.
  9458.         POP     DE              ; now a possible BASIC return line.
  9459.         LD      A,D             ; the high byte $00 - $27 is
  9460.         CP      $3E             ; compared with the traditional end-marker $3E.
  9461.         JR      Z,L1F36         ; forward to REPORT-7 with a match.
  9462.                                 ; 'RETURN without GOSUB'
  9463.  
  9464. ; It was not the end-marker so a single statement byte remains at the base of
  9465. ; the calculator stack. It can't be popped off.
  9466.  
  9467.         DEC     SP              ; adjust stack pointer to create room for two
  9468.                                 ; bytes.
  9469.         EX      (SP),HL         ; statement to H, error address to base of
  9470.                                 ; new machine stack.
  9471.         EX      DE,HL           ; statement to D,  BASIC line number to HL.
  9472.         LD      ($5C3D),SP      ; adjust ERR_SP to point to new stack pointer
  9473.         PUSH    BC              ; now re-stack the address STMT-RET
  9474.         JP      L1E73           ; to GO-TO-2 to update statement and line
  9475.                                 ; system variables and exit indirectly to the
  9476.                                 ; address just pushed on stack.
  9477.  
  9478. ; ---
  9479.  
  9480. ;; REPORT-7
  9481. L1F36:  PUSH    DE              ; replace the end-marker.
  9482.         PUSH    HL              ; now restore the error address
  9483.                                 ; as will be required in a few clock cycles.
  9484.  
  9485.         RST     08H             ; ERROR-1
  9486.         DB    $06             ; Error Report: RETURN without GOSUB
  9487.  
  9488. ; --------------------
  9489. ; Handle PAUSE command
  9490. ; --------------------
  9491. ; The pause command takes as its parameter the number of interrupts
  9492. ; for which to wait. PAUSE 50 pauses for about a second.
  9493. ; PAUSE 0 pauses indefinitely.
  9494. ; Both forms can be finished by pressing a key.
  9495.  
  9496. ;; PAUSE
  9497. L1F3A:  CALL    L1E99           ; routine FIND-INT2 puts value in BC
  9498.  
  9499. ;; PAUSE-1
  9500. L1F3D:  HALT                    ; wait for interrupt.
  9501.         DEC     BC              ; decrease counter.
  9502.         LD      A,B             ; test if
  9503.         OR      C               ; result is zero.
  9504.         JR      Z,L1F4F         ; forward to PAUSE-END if so.
  9505.  
  9506.         LD      A,B             ; test if
  9507.         AND     C               ; now $FFFF
  9508.         INC     A               ; that is, initially zero.
  9509.         JR      NZ,L1F49        ; skip forward to PAUSE-2 if not.
  9510.  
  9511.         INC     BC              ; restore counter to zero.
  9512.  
  9513. ;; PAUSE-2
  9514. L1F49:  BIT     5,(IY+$01)      ; test FLAGS - has a new key been pressed ?
  9515.         JR      Z,L1F3D         ; back to PAUSE-1 if not.
  9516.  
  9517. ;; PAUSE-END
  9518. L1F4F:  RES     5,(IY+$01)      ; update FLAGS - signal no new key
  9519.         RET                     ; and return.
  9520.  
  9521. ; -------------------
  9522. ; Check for BREAK key
  9523. ; -------------------
  9524. ; This routine is called from COPY-LINE, when interrupts are disabled,
  9525. ; to test if BREAK (SHIFT - SPACE) is being pressed.
  9526. ; It is also called at STMT-RET after every statement.
  9527.  
  9528. ;; BREAK-KEY
  9529. L1F54:  LD      A,$7F           ; Input address: $7FFE
  9530.         IN      A,($FE)         ; read lower right keys
  9531.         RRA                     ; rotate bit 0 - SPACE
  9532.         RET     C               ; return if not reset
  9533.  
  9534.         LD      A,$FE           ; Input address: $FEFE
  9535.         IN      A,($FE)         ; read lower left keys
  9536.         RRA                     ; rotate bit 0 - SHIFT
  9537.         RET                     ; carry will be set if not pressed.
  9538.                                 ; return with no carry if both keys
  9539.                                 ; pressed.
  9540.  
  9541. ; ---------------------
  9542. ; Handle DEF FN command
  9543. ; ---------------------
  9544. ; e.g DEF FN r$(a$,a) = a$(a TO )
  9545. ; this 'command' is ignored in runtime but has its syntax checked
  9546. ; during line-entry.
  9547.  
  9548. ;; DEF-FN
  9549. L1F60:  CALL    L2530           ; routine SYNTAX-Z
  9550.         JR      Z,L1F6A         ; forward to DEF-FN-1 if parsing
  9551.  
  9552.         LD      A,$CE           ; else load A with 'DEF FN' and
  9553.         JP      L1E39           ; jump back to PASS-BY
  9554.  
  9555. ; ---
  9556.  
  9557. ; continue here if checking syntax.
  9558.  
  9559. ;; DEF-FN-1
  9560. L1F6A:  SET      6,(IY+$01)     ; set FLAGS  - Assume numeric result
  9561.         CALL    L2C8D           ; call routine ALPHA
  9562.         JR      NC,L1F89        ; if not then to DEF-FN-4 to jump to
  9563.                                 ; 'Nonsense in BASIC'
  9564.  
  9565.  
  9566.         RST     20H             ; NEXT-CHAR
  9567.         CP      $24             ; is it '$' ?
  9568.         JR      NZ,L1F7D        ; to DEF-FN-2 if not as numeric.
  9569.  
  9570.         RES     6,(IY+$01)      ; set FLAGS  - Signal string result
  9571.  
  9572.         RST     20H             ; get NEXT-CHAR
  9573.  
  9574. ;; DEF-FN-2
  9575. L1F7D:  CP      $28             ; is it '(' ?
  9576.         JR      NZ,L1FBD        ; to DEF-FN-7 'Nonsense in BASIC'
  9577.  
  9578.  
  9579.         RST     20H             ; NEXT-CHAR
  9580.         CP      $29             ; is it ')' ?
  9581.         JR      Z,L1FA6         ; to DEF-FN-6 if null argument
  9582.  
  9583. ;; DEF-FN-3
  9584. L1F86:  CALL    L2C8D           ; routine ALPHA checks that it is the expected
  9585.                                 ; alphabetic character.
  9586.  
  9587. ;; DEF-FN-4
  9588. L1F89:  JP      NC,L1C8A        ; to REPORT-C  if not
  9589.                                 ; 'Nonsense in BASIC'.
  9590.  
  9591.         EX      DE,HL           ; save pointer in DE
  9592.  
  9593.         RST     20H             ; NEXT-CHAR re-initializes HL from CH_ADD
  9594.                                 ; and advances.
  9595.         CP      $24             ; '$' ? is it a string argument.
  9596.         JR      NZ,L1F94        ; forward to DEF-FN-5 if not.
  9597.  
  9598.         EX      DE,HL           ; save pointer to '$' in DE
  9599.  
  9600.         RST     20H             ; NEXT-CHAR re-initializes HL and advances
  9601.  
  9602. ;; DEF-FN-5
  9603. L1F94:  EX      DE,HL           ; bring back pointer.
  9604.         LD      BC,$0006        ; the function requires six hidden bytes for
  9605.                                 ; each parameter passed.
  9606.                                 ; The first byte will be $0E
  9607.                                 ; then 5-byte numeric value
  9608.                                 ; or 5-byte string pointer.
  9609.  
  9610.         CALL    L1655           ; routine MAKE-ROOM creates space in program
  9611.                                 ; area.
  9612.  
  9613.         INC     HL              ; adjust HL (set by LDDR)
  9614.         INC     HL              ; to point to first location.
  9615.         LD      (HL),$0E        ; insert the 'hidden' marker.
  9616.  
  9617. ; Note. these invisible storage locations hold nothing meaningful for the
  9618. ; moment. They will be used every time the corresponding function is
  9619. ; evaluated in runtime.
  9620. ; Now consider the following character fetched earlier.
  9621.  
  9622.         CP      $2C             ; is it ',' ? (more than one parameter)
  9623.         JR      NZ,L1FA6        ; to DEF-FN-6 if not
  9624.  
  9625.  
  9626.         RST     20H             ; else NEXT-CHAR
  9627.         JR      L1F86           ; and back to DEF-FN-3
  9628.  
  9629. ; ---
  9630.  
  9631. ;; DEF-FN-6
  9632. L1FA6:  CP      $29             ; should close with a ')'
  9633.         JR      NZ,L1FBD        ; to DEF-FN-7 if not
  9634.                                 ; 'Nonsense in BASIC'
  9635.  
  9636.  
  9637.         RST     20H             ; get NEXT-CHAR
  9638.         CP      $3D             ; is it '=' ?
  9639.         JR      NZ,L1FBD        ; to DEF-FN-7 if not 'Nonsense...'
  9640.  
  9641.  
  9642.         RST     20H             ; address NEXT-CHAR
  9643.         LD      A,($5C3B)       ; get FLAGS which has been set above
  9644.         PUSH    AF              ; and preserve
  9645.  
  9646.         CALL    L24FB           ; routine SCANNING checks syntax of expression
  9647.                                 ; and also sets flags.
  9648.  
  9649.         POP     AF              ; restore previous flags
  9650.         XOR     (IY+$01)        ; xor with FLAGS - bit 6 should be same
  9651.                                 ; therefore will be reset.
  9652.         AND     $40             ; isolate bit 6.
  9653.  
  9654. ;; DEF-FN-7
  9655. L1FBD:  JP      NZ,L1C8A        ; jump back to REPORT-C if the expected result
  9656.                                 ; is not the same type.
  9657.                                 ; 'Nonsense in BASIC'
  9658.  
  9659.         CALL    L1BEE           ; routine CHECK-END will return early if
  9660.                                 ; at end of statement and move onto next
  9661.                                 ; else produce error report. >>>
  9662.  
  9663.                                 ; There will be no return to here.
  9664.  
  9665. ; -------------------------------
  9666. ; Returning early from subroutine
  9667. ; -------------------------------
  9668. ; All routines are capable of being run in two modes - syntax checking mode
  9669. ; and runtime mode.  This routine is called often to allow a routine to return
  9670. ; early if checking syntax.
  9671.  
  9672. ;; UNSTACK-Z
  9673. L1FC3:  CALL    L2530           ; routine SYNTAX-Z sets zero flag if syntax
  9674.                                 ; is being checked.
  9675.  
  9676.         POP     HL              ; drop the return address.
  9677.         RET      Z              ; return to previous call in chain if checking
  9678.                                 ; syntax.
  9679.  
  9680.         JP      (HL)            ; jump to return address as BASIC program is
  9681.                                 ; actually running.
  9682.  
  9683. ; ---------------------
  9684. ; Handle LPRINT command
  9685. ; ---------------------
  9686. ; A simple form of 'PRINT #3' although it can output to 16 streams.
  9687. ; Probably for compatibility with other BASICs particularly ZX81 BASIC.
  9688. ; An extra UDG might have been better.
  9689.  
  9690. ;; LPRINT
  9691. L1FC9:  LD      A,$03           ; the printer channel
  9692.         JR      L1FCF           ; forward to PRINT-1
  9693.  
  9694. ; ---------------------
  9695. ; Handle PRINT commands
  9696. ; ---------------------
  9697. ; The Spectrum's main stream output command.
  9698. ; The default stream is stream 2 which is normally the upper screen
  9699. ; of the computer. However the stream can be altered in range 0 - 15.
  9700.  
  9701. ;; PRINT
  9702. L1FCD:  LD      A,$02           ; the stream for the upper screen.
  9703.  
  9704. ; The LPRINT command joins here.
  9705.  
  9706. ;; PRINT-1
  9707. L1FCF:  CALL    L2530           ; routine SYNTAX-Z checks if program running
  9708.         CALL    NZ,L1601        ; routine CHAN-OPEN if so
  9709.         CALL    L0D4D           ; routine TEMPS sets temporary colours.
  9710.         CALL    L1FDF           ; routine PRINT-2 - the actual item
  9711.         CALL    L1BEE           ; routine CHECK-END gives error if not at end
  9712.                                 ; of statement
  9713.         RET                     ; and return >>>
  9714.  
  9715. ; ------------------------------------
  9716. ; this subroutine is called from above
  9717. ; and also from INPUT.
  9718.  
  9719. ;; PRINT-2
  9720. L1FDF:  RST     18H             ; GET-CHAR gets printable character
  9721.         CALL    L2045           ; routine PR-END-Z checks if more printing
  9722.         JR      Z,L1FF2         ; to PRINT-4 if not     e.g. just 'PRINT :'
  9723.  
  9724. ; This tight loop deals with combinations of positional controls and
  9725. ; print items. An early return can be made from within the loop
  9726. ; if the end of a print sequence is reached.
  9727.  
  9728. ;; PRINT-3
  9729. L1FE5:  CALL    L204E           ; routine PR-POSN-1 returns zero if more
  9730.                                 ; but returns early at this point if
  9731.                                 ; at end of statement!
  9732.                                 ;
  9733.         JR      Z,L1FE5         ; to PRINT-3 if consecutive positioners
  9734.  
  9735.         CALL    L1FFC           ; routine PR-ITEM-1 deals with strings etc.
  9736.         CALL    L204E           ; routine PR-POSN-1 for more position codes
  9737.         JR      Z,L1FE5         ; loop back to PRINT-3 if so
  9738.  
  9739. ;; PRINT-4
  9740. L1FF2:  CP      $29             ; return now if this is ')' from input-item.
  9741.                                 ; (see INPUT.)
  9742.         RET     Z               ; or continue and print carriage return in
  9743.                                 ; runtime
  9744.  
  9745. ; ---------------------
  9746. ; Print carriage return
  9747. ; ---------------------
  9748. ; This routine which continues from above prints a carriage return
  9749. ; in run-time. It is also called once from PRINT-POSN.
  9750.  
  9751. ;; PRINT-CR
  9752. L1FF5:  CALL    L1FC3           ; routine UNSTACK-Z
  9753.  
  9754.         LD      A,$0D           ; prepare a carriage return
  9755.  
  9756.         RST     10H             ; PRINT-A
  9757.         RET                     ; return
  9758.  
  9759.  
  9760. ; -----------
  9761. ; Print items
  9762. ; -----------
  9763. ; This routine deals with print items as in
  9764. ; PRINT AT 10,0;"The value of A is ";a
  9765. ; It returns once a single item has been dealt with as it is part
  9766. ; of a tight loop that considers sequences of positional and print items
  9767.  
  9768. ;; PR-ITEM-1
  9769. L1FFC:  RST     18H             ; GET-CHAR
  9770.         CP      $AC             ; is character 'AT' ?
  9771.         JR      NZ,L200E        ; forward to PR-ITEM-2 if not.
  9772.  
  9773.         CALL    L1C79           ; routine NEXT-2NUM  check for two comma
  9774.                                 ; separated numbers placing them on the
  9775.                                 ; calculator stack in runtime.
  9776.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9777.  
  9778.         CALL    L2307           ; routine STK-TO-BC get the numbers in B and C.
  9779.         LD      A,$16           ; prepare the 'at' control.
  9780.         JR      L201E           ; forward to PR-AT-TAB to print the sequence.
  9781.  
  9782. ; ---
  9783.  
  9784. ;; PR-ITEM-2
  9785. L200E:  CP      $AD             ; is character 'TAB' ?
  9786.         JR      NZ,L2024        ; to PR-ITEM-3 if not
  9787.  
  9788.  
  9789.         RST     20H             ; NEXT-CHAR to address next character
  9790.         CALL    L1C82           ; routine EXPT-1NUM
  9791.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9792.  
  9793.         CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9794.         LD      A,$17           ; prepare the 'tab' control.
  9795.  
  9796. ;; PR-AT-TAB
  9797. L201E:  RST     10H             ; PRINT-A outputs the control
  9798.  
  9799.         LD      A,C             ; first value to A
  9800.         RST     10H             ; PRINT-A outputs it.
  9801.  
  9802.         LD      A,B             ; second value
  9803.         RST     10H             ; PRINT-A
  9804.  
  9805.         RET                     ; return - item finished >>>
  9806.  
  9807. ; ---
  9808.  
  9809. ; Now consider paper 2; #2; a$
  9810.  
  9811. ;; PR-ITEM-3
  9812. L2024:  CALL    L21F2           ; routine CO-TEMP-3 will print any colour
  9813.         RET     NC              ; items - return if success.
  9814.  
  9815.         CALL    L2070           ; routine STR-ALTER considers new stream
  9816.         RET     NC              ; return if altered.
  9817.  
  9818.         CALL    L24FB           ; routine SCANNING now to evaluate expression
  9819.         CALL    L1FC3           ; routine UNSTACK-Z if not runtime.
  9820.  
  9821.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  9822.         CALL    Z,L2BF1         ; routine STK-FETCH if string.
  9823.                                 ; note no flags affected.
  9824.         JP      NZ,L2DE3        ; to PRINT-FP to print if numeric >>>
  9825.  
  9826. ; It was a string expression - start in DE, length in BC
  9827. ; Now enter a loop to print it
  9828.  
  9829. ;; PR-STRING
  9830. L203C:  LD      A,B             ; this tests if the
  9831.         OR      C               ; length is zero and sets flag accordingly.
  9832.         DEC     BC              ; this doesn't but decrements counter.
  9833.         RET     Z               ; return if zero.
  9834.  
  9835.         LD      A,(DE)          ; fetch character.
  9836.         INC     DE              ; address next location.
  9837.  
  9838.         RST     10H             ; PRINT-A.
  9839.  
  9840.         JR      L203C           ; loop back to PR-STRING.
  9841.  
  9842. ; ---------------
  9843. ; End of printing
  9844. ; ---------------
  9845. ; This subroutine returns zero if no further printing is required
  9846. ; in the current statement.
  9847. ; The first terminator is found in  escaped input items only,
  9848. ; the others in print_items.
  9849.  
  9850. ;; PR-END-Z
  9851. L2045:  CP      $29             ; is character a ')' ?
  9852.         RET     Z               ; return if so -        e.g. INPUT (p$); a$
  9853.  
  9854. ;; PR-ST-END
  9855. L2048:  CP      $0D             ; is it a carriage return ?
  9856.         RET     Z               ; return also -         e.g. PRINT a
  9857.  
  9858.         CP      $3A             ; is character a ':' ?
  9859.         RET                     ; return - zero flag will be set if so.
  9860.                                 ;                       e.g. PRINT a :
  9861.  
  9862. ; --------------
  9863. ; Print position
  9864. ; --------------
  9865. ; This routine considers a single positional character ';', ',', '''
  9866.  
  9867. ;; PR-POSN-1
  9868. L204E:  RST     18H             ; GET-CHAR
  9869.         CP      $3B             ; is it ';' ?            
  9870.                                 ; i.e. print from last position.
  9871.         JR      Z,L2067         ; forward to PR-POSN-3 if so.
  9872.                                 ; i.e. do nothing.
  9873.  
  9874.         CP      $2C             ; is it ',' ?
  9875.                                 ; i.e. print at next tabstop.
  9876.         JR      NZ,L2061        ; forward to PR-POSN-2 if anything else.
  9877.  
  9878.         CALL    L2530           ; routine SYNTAX-Z
  9879.         JR      Z,L2067         ; forward to PR-POSN-3 if checking syntax.
  9880.  
  9881.         LD      A,$06           ; prepare the 'comma' control character.
  9882.  
  9883.         RST     10H             ; PRINT-A  outputs to current channel in
  9884.                                 ; run-time.
  9885.  
  9886.         JR      L2067           ; skip to PR-POSN-3.
  9887.  
  9888. ; ---
  9889.  
  9890. ; check for newline.
  9891.  
  9892. ;; PR-POSN-2
  9893. L2061:  CP      $27             ; is character a "'" ? (newline)
  9894.         RET     NZ              ; return if no match              >>>
  9895.  
  9896.         CALL    L1FF5           ; routine PRINT-CR outputs a carriage return
  9897.                                 ; in runtime only.
  9898.  
  9899. ;; PR-POSN-3
  9900. L2067:  RST     20H             ; NEXT-CHAR to A.
  9901.         CALL    L2045           ; routine PR-END-Z checks if at end.
  9902.         JR      NZ,L206E        ; to PR-POSN-4 if not.
  9903.  
  9904.         POP     BC              ; drop return address if at end.
  9905.  
  9906. ;; PR-POSN-4
  9907. L206E:  CP      A               ; reset the zero flag.
  9908.         RET                     ; and return to loop or quit.
  9909.  
  9910. ; ------------
  9911. ; Alter stream
  9912. ; ------------
  9913. ; This routine is called from PRINT ITEMS above, and also LIST as in
  9914. ; LIST #15
  9915.  
  9916. ;; STR-ALTER
  9917. L2070:  CP      $23             ; is character '#' ?
  9918.         SCF                     ; set carry flag.
  9919.         RET     NZ              ; return if no match.
  9920.  
  9921.  
  9922.         RST      20H            ; NEXT-CHAR
  9923.         CALL    L1C82           ; routine EXPT-1NUM gets stream number
  9924.         AND     A               ; prepare to exit early with carry reset
  9925.         CALL    L1FC3           ; routine UNSTACK-Z exits early if parsing
  9926.         CALL    L1E94           ; routine FIND-INT1 gets number off stack
  9927.         CP      $10             ; must be range 0 - 15 decimal.
  9928.         JP      NC,L160E        ; jump back to REPORT-Oa if not
  9929.                                 ; 'Invalid stream'.
  9930.  
  9931.         CALL    L1601           ; routine CHAN-OPEN
  9932.         AND     A               ; clear carry - signal item dealt with.
  9933.         RET                     ; return
  9934.  
  9935. ; --------------------
  9936. ; Handle INPUT command
  9937. ; --------------------
  9938. ; This command
  9939. ;
  9940.  
  9941. ;; INPUT
  9942. L2089:  CALL    L2530           ; routine SYNTAX-Z to check if in runtime.
  9943.         JR      Z,L2096         ; forward to INPUT-1 if checking syntax.
  9944.  
  9945.         LD      A,$01           ; select channel 'K' the keyboard for input.
  9946.         CALL    L1601           ; routine CHAN-OPEN opens it.
  9947.         CALL    L0D6E           ; routine CLS-LOWER clears the lower screen
  9948.                                 ; and sets DF_SZ to two.
  9949.  
  9950. ;; INPUT-1
  9951. L2096:  LD      (IY+$02),$01    ; update TV_FLAG - signal lower screen in use
  9952.                                 ; ensuring that the correct set of system
  9953.                                 ; variables are updated and that the border
  9954.                                 ; colour is used.
  9955.  
  9956.         CALL    L20C1           ; routine IN-ITEM-1 to handle the input.
  9957.  
  9958.         CALL    L1BEE           ; routine CHECK-END will make an early exit
  9959.                                 ; if checking syntax. >>>
  9960.  
  9961. ; keyboard input has been made and it remains to adjust the upper
  9962. ; screen in case the lower two lines have been extended upwards.
  9963.  
  9964.         LD      BC,($5C88)      ; fetch S_POSN current line/column of
  9965.                                 ; the upper screen.
  9966.         LD      A,($5C6B)       ; fetch DF_SZ the display file size of
  9967.                                 ; the lower screen.
  9968.         CP      B               ; test that lower screen does not overlap
  9969.         JR      C,L20AD         ; forward to INPUT-2 if not.
  9970.  
  9971. ; the two screens overlap so adjust upper screen.
  9972.  
  9973.         LD      C,$21           ; set column of upper screen to leftmost.
  9974.         LD      B,A             ; and line to one above lower screen.
  9975.                                 ; continue forward to update upper screen
  9976.                                 ; print position.
  9977.  
  9978. ;; INPUT-2
  9979. L20AD:  LD      ($5C88),BC      ; set S_POSN update upper screen line/column.
  9980.         LD      A,$19           ; subtract from twenty five
  9981.         SUB     B               ; the new line number.
  9982.         LD      ($5C8C),A       ; and place result in SCR_CT - scroll count.
  9983.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use.
  9984.         CALL    L0DD9           ; routine CL-SET sets the print position
  9985.                                 ; system variables for the upper screen.
  9986.         JP      L0D6E           ; jump back to CLS-LOWER and make
  9987.                                 ; an indirect exit >>.
  9988.  
  9989. ; ---------------------
  9990. ; INPUT ITEM subroutine
  9991. ; ---------------------
  9992. ; This subroutine deals with the input items and print items.
  9993. ; from  the current input channel.
  9994. ; It is only called from the above INPUT routine but was obviously
  9995. ; once called from somewhere else in another context.
  9996.  
  9997. ;; IN-ITEM-1
  9998. L20C1:  CALL    L204E           ; routine PR-POSN-1 deals with a single
  9999.                                 ; position item at each call.
  10000.         JR      Z,L20C1         ; back to IN-ITEM-1 until no more in a
  10001.                                 ; sequence.
  10002.  
  10003.         CP      $28             ; is character '(' ?
  10004.         JR      NZ,L20D8        ; forward to IN-ITEM-2 if not.
  10005.  
  10006. ; any variables within braces will be treated as part, or all, of the prompt
  10007. ; instead of being used as destination variables.
  10008.  
  10009.         RST     20H             ; NEXT-CHAR
  10010.         CALL    L1FDF           ; routine PRINT-2 to output the dynamic
  10011.                                 ; prompt.
  10012.  
  10013.         RST     18H             ; GET-CHAR
  10014.         CP      $29             ; is character a matching ')' ?
  10015.         JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  10016.                                 ; 'Nonsense in BASIC'.
  10017.  
  10018.         RST     20H             ; NEXT-CHAR
  10019.         JP      L21B2           ; forward to IN-NEXT-2
  10020.  
  10021. ; ---
  10022.  
  10023. ;; IN-ITEM-2
  10024. L20D8:  CP      $CA             ; is the character the token 'LINE' ?
  10025.         JR      NZ,L20ED        ; forward to IN-ITEM-3 if not.
  10026.  
  10027.         RST     20H             ; NEXT-CHAR - variable must come next.
  10028.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10029.                                 ; address of variable to be assigned.
  10030.                                 ; or generates an error if no variable
  10031.                                 ; at this position.
  10032.  
  10033.         SET     7,(IY+$37)      ; update FLAGX  - signal handling INPUT LINE
  10034.         BIT     6,(IY+$01)      ; test FLAGS  - numeric or string result ?
  10035.         JP      NZ,L1C8A        ; jump back to REPORT-C if not string
  10036.                                 ; 'Nonsense in BASIC'.
  10037.  
  10038.         JR      L20FA           ; forward to IN-PROMPT to set up workspace.
  10039.  
  10040. ; ---
  10041.  
  10042. ; the jump was here for other variables.
  10043.  
  10044. ;; IN-ITEM-3
  10045. L20ED:  CALL     L2C8D          ; routine ALPHA checks if character is
  10046.                                 ; a suitable variable name.
  10047.         JP      NC,L21AF        ; forward to IN-NEXT-1 if not
  10048.  
  10049.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10050.                                 ; address of variable to be assigned.
  10051.         RES     7,(IY+$37)      ; update FLAGX  - signal not INPUT LINE.
  10052.  
  10053. ;; IN-PROMPT
  10054. L20FA:  CALL    L2530           ; routine SYNTAX-Z
  10055.         JP      Z,L21B2         ; forward to IN-NEXT-2 if checking syntax.
  10056.  
  10057.         CALL    L16BF           ; routine SET-WORK clears workspace.
  10058.         LD      HL,$5C71        ; point to system variable FLAGX
  10059.         RES     6,(HL)          ; signal string result.
  10060.         SET     5,(HL)          ; signal in Input Mode for editor.
  10061.         LD      BC,$0001        ; initialize space required to one for
  10062.                                 ; the carriage return.
  10063.         BIT     7,(HL)          ; test FLAGX - INPUT LINE in use ?
  10064.         JR      NZ,L211C        ; forward to IN-PR-2 if so as that is
  10065.                                 ; all the space that is required.
  10066.  
  10067.         LD      A,($5C3B)       ; load accumulator from FLAGS
  10068.         AND     $40             ; mask to test BIT 6 of FLAGS and clear
  10069.                                 ; the other bits in A.
  10070.                                 ; numeric result expected ?
  10071.         JR      NZ,L211A        ; forward to IN-PR-1 if so
  10072.  
  10073.         LD      C,$03           ; increase space to three bytes for the
  10074.                                 ; pair of surrounding quotes.
  10075.  
  10076. ;; IN-PR-1
  10077. L211A:  OR      (HL)            ; if numeric result, set bit 6 of FLAGX.
  10078.         LD      (HL),A          ; and update system variable
  10079.  
  10080. ;; IN-PR-2
  10081. L211C:  RST     30H             ; BC-SPACES opens 1 or 3 bytes in workspace
  10082.         LD      (HL),$0D        ; insert carriage return at last new location.
  10083.         LD      A,C             ; fetch the length, one or three.
  10084.         RRCA                    ; lose bit 0.
  10085.         RRCA                    ; test if quotes required.
  10086.         JR      NC,L2129        ; forward to IN-PR-3 if not.
  10087.  
  10088.         LD      A,$22           ; load the '"' character
  10089.         LD      (DE),A          ; place quote in first new location at DE.
  10090.         DEC     HL              ; decrease HL - from carriage return.
  10091.         LD      (HL),A          ; and place a quote in second location.
  10092.  
  10093. ;; IN-PR-3
  10094. L2129:  LD      ($5C5B),HL      ; set keyboard cursor K_CUR to HL
  10095.         BIT     7,(IY+$37)      ; test FLAGX  - is this INPUT LINE ??
  10096.         JR      NZ,L215E        ; forward to IN-VAR-3 if so as input will
  10097.                                 ; be accepted without checking its syntax.
  10098.  
  10099.         LD      HL,($5C5D)      ; fetch CH_ADD
  10100.         PUSH    HL              ; and save on stack.
  10101.         LD      HL,($5C3D)      ; fetch ERR_SP
  10102.         PUSH    HL              ; and save on stack
  10103.  
  10104. ;; IN-VAR-1
  10105. L213A:  LD      HL,L213A        ; address: IN-VAR-1 - this address
  10106.         PUSH    HL              ; is saved on stack to handle errors.
  10107.         BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  10108.         JR      Z,L2148         ; forward to IN-VAR-2 if not using the
  10109.                                 ; keyboard for input. (??)
  10110.  
  10111.         LD      ($5C3D),SP      ; set ERR_SP to point to IN-VAR-1 on stack.
  10112.  
  10113. ;; IN-VAR-2
  10114. L2148:  LD      HL,($5C61)      ; set HL to WORKSP - start of workspace.
  10115.         CALL    L11A7           ; routine REMOVE-FP removes floating point
  10116.                                 ; forms when looping in error condition.
  10117.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK' cancelling the error.
  10118.                                 ; but X_PTR causes flashing error marker
  10119.                                 ; to be displayed at each call to the editor.
  10120.         CALL    L0F2C           ; routine EDITOR allows input to be entered
  10121.                                 ; or corrected if this is second time around.
  10122.  
  10123. ; if we pass to next then there are no system errors
  10124.  
  10125.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax
  10126.         CALL    L21B9           ; routine IN-ASSIGN checks syntax using
  10127.                                 ; the VAL-FET-2 and powerful SCANNING routines.
  10128.                                 ; any syntax error and its back to IN-VAR-1.
  10129.                                 ; but with the flashing error marker showing
  10130.                                 ; where the error is.
  10131.                                 ; Note. the syntax of string input has to be
  10132.                                 ; checked as the user may have removed the
  10133.                                 ; bounding quotes or escaped them as with
  10134.                                 ; "hat" + "stand" for example.
  10135. ; proceed if syntax passed.
  10136.  
  10137.         JR      L2161           ; jump forward to IN-VAR-4
  10138.  
  10139. ; ---
  10140.  
  10141. ; the jump was to here when using INPUT LINE.
  10142.  
  10143. ;; IN-VAR-3
  10144. L215E:  CALL    L0F2C           ; routine EDITOR is called for input
  10145.  
  10146. ; when ENTER received rejoin other route but with no syntax check.
  10147.  
  10148. ; INPUT and INPUT LINE converge here.
  10149.  
  10150. ;; IN-VAR-4
  10151. L2161:  LD      (IY+$22),$00    ; set K_CUR_hi to a low value so that the cursor
  10152.                                 ; no longer appears in the input line.
  10153.  
  10154.         CALL    L21D6           ; routine IN-CHAN-K tests if the keyboard
  10155.                                 ; is being used for input.
  10156.         JR      NZ,L2174        ; forward to IN-VAR-5 if using another input
  10157.                                 ; channel.
  10158.  
  10159. ; continue here if using the keyboard.
  10160.  
  10161.         CALL    L111D           ; routine ED-COPY overprints the edit line
  10162.                                 ; to the lower screen. The only visible
  10163.                                 ; affect is that the cursor disappears.
  10164.                                 ; if you're inputting more than one item in
  10165.                                 ; a statement then that becomes apparent.
  10166.  
  10167.         LD      BC,($5C82)      ; fetch line and column from ECHO_E
  10168.         CALL    L0DD9           ; routine CL-SET sets S-POSNL to those
  10169.                                 ; values.
  10170.  
  10171. ; if using another input channel rejoin here.
  10172.  
  10173. ;; IN-VAR-5
  10174. L2174:  LD      HL,$5C71        ; point HL to FLAGX
  10175.         RES     5,(HL)          ; signal not in input mode
  10176.         BIT     7,(HL)          ; is this INPUT LINE ?
  10177.         RES     7,(HL)          ; cancel the bit anyway.
  10178.         JR      NZ,L219B        ; forward to IN-VAR-6 if INPUT LINE.
  10179.  
  10180.         POP     HL              ; drop the looping address
  10181.         POP     HL              ; drop the the address of previous
  10182.                                 ; error handler.
  10183.         LD      ($5C3D),HL      ; set ERR_SP to point to it.
  10184.         POP     HL              ; drop original CH_ADD which points to
  10185.                                 ; INPUT command in BASIC line.
  10186.         LD      ($5C5F),HL      ; save in X_PTR while input is assigned.
  10187.         SET     7,(IY+$01)      ; update FLAGS - Signal running program
  10188.         CALL    L21B9           ; routine IN-ASSIGN is called again
  10189.                                 ; this time the variable will be assigned
  10190.                                 ; the input value without error.
  10191.                                 ; Note. the previous example now
  10192.                                 ; becomes "hatstand"
  10193.  
  10194.         LD      HL,($5C5F)      ; fetch stored CH_ADD value from X_PTR.
  10195.         LD      (IY+$26),$00    ; set X_PTR_hi so that iy is no longer relevant.
  10196.         LD      ($5C5D),HL      ; put restored value back in CH_ADD
  10197.         JR      L21B2           ; forward to IN-NEXT-2 to see if anything
  10198.                                 ; more in the INPUT list.
  10199.  
  10200. ; ---
  10201.  
  10202. ; the jump was to here with INPUT LINE only
  10203.  
  10204. ;; IN-VAR-6
  10205. L219B:  LD      HL,($5C63)      ; STKBOT points to the end of the input.
  10206.         LD      DE,($5C61)      ; WORKSP points to the beginning.
  10207.         SCF                     ; prepare for true subtraction.
  10208.         SBC     HL,DE           ; subtract to get length
  10209.         LD      B,H             ; transfer it to
  10210.         LD      C,L             ; the BC register pair.
  10211.         CALL    L2AB2           ; routine STK-STO-$ stores parameters on
  10212.                                 ; the calculator stack.
  10213.         CALL    L2AFF           ; routine LET assigns it to destination.
  10214.         JR      L21B2           ; forward to IN-NEXT-2 as print items
  10215.                                 ; not allowed with INPUT LINE.
  10216.                                 ; Note. that "hat" + "stand" will, for
  10217.                                 ; example, be unchanged as also would
  10218.                                 ; 'PRINT "Iris was here"'.
  10219.  
  10220. ; ---
  10221.  
  10222. ; the jump was to here when ALPHA found more items while looking for
  10223. ; a variable name.
  10224.  
  10225. ;; IN-NEXT-1
  10226. L21AF:  CALL    L1FFC           ; routine PR-ITEM-1 considers further items.
  10227.  
  10228. ;; IN-NEXT-2
  10229. L21B2:  CALL    L204E           ; routine PR-POSN-1 handles a position item.
  10230.         JP      Z,L20C1         ; jump back to IN-ITEM-1 if the zero flag
  10231.                                 ; indicates more items are present.
  10232.  
  10233.         RET                     ; return.
  10234.  
  10235. ; ---------------------------
  10236. ; INPUT ASSIGNMENT Subroutine
  10237. ; ---------------------------
  10238. ; This subroutine is called twice from the INPUT command when normal
  10239. ; keyboard input is assigned. On the first occasion syntax is checked
  10240. ; using SCANNING. The final call with the syntax flag reset is to make
  10241. ; the assignment.
  10242.  
  10243. ;; IN-ASSIGN
  10244. L21B9:  LD      HL,($5C61)      ; fetch WORKSP start of input
  10245.         LD      ($5C5D),HL      ; set CH_ADD to first character
  10246.  
  10247.         RST     18H             ; GET-CHAR ignoring leading white-space.
  10248.         CP      $E2             ; is it 'STOP'
  10249.         JR      Z,L21D0         ; forward to IN-STOP if so.
  10250.  
  10251.         LD      A,($5C71)       ; load accumulator from FLAGX
  10252.         CALL    L1C59           ; routine VAL-FET-2 makes assignment
  10253.                                 ; or goes through the motions if checking
  10254.                                 ; syntax. SCANNING is used.
  10255.  
  10256.         RST     18H             ; GET-CHAR
  10257.         CP      $0D             ; is it carriage return ?
  10258.         RET     Z               ; return if so
  10259.                                 ; either syntax is OK
  10260.                                 ; or assignment has been made.
  10261.  
  10262. ; if another character was found then raise an error.
  10263. ; User doesn't see report but the flashing error marker
  10264. ; appears in the lower screen.
  10265.  
  10266. ;; REPORT-Cb
  10267. L21CE:  RST     08H             ; ERROR-1
  10268.         DB    $0B             ; Error Report: Nonsense in BASIC
  10269.  
  10270. ;; IN-STOP
  10271. L21D0:  CALL    L2530           ; routine SYNTAX-Z (UNSTACK-Z?)
  10272.         RET     Z               ; return if checking syntax
  10273.                                 ; as user wouldn't see error report.
  10274.                                 ; but generate visible error report
  10275.                                 ; on second invocation.
  10276.  
  10277. ;; REPORT-H
  10278. L21D4:  RST     08H             ; ERROR-1
  10279.         DB    $10             ; Error Report: STOP in INPUT
  10280.  
  10281. ; ------------------
  10282. ; Test for channel K
  10283. ; ------------------
  10284. ; This subroutine is called once from the keyboard
  10285. ; INPUT command to check if the input routine in
  10286. ; use is the one for the keyboard.
  10287.  
  10288. ;; IN-CHAN-K
  10289. L21D6:  LD      HL,($5C51)      ; fetch address of current channel CURCHL
  10290.         INC     HL              ;
  10291.         INC     HL              ; advance past
  10292.         INC     HL              ; input and
  10293.         INC     HL              ; output streams
  10294.         LD      A,(HL)          ; fetch the channel identifier.
  10295.         CP      $4B             ; test for 'K'
  10296.         RET                     ; return with zero set if keyboard is use.
  10297.  
  10298. ; --------------------
  10299. ; Colour Item Routines
  10300. ; --------------------
  10301. ;
  10302. ; These routines have 3 entry points -
  10303. ; 1) CO-TEMP-2 to handle a series of embedded Graphic colour items.
  10304. ; 2) CO-TEMP-3 to handle a single embedded print colour item.
  10305. ; 3) CO TEMP-4 to handle a colour command such as FLASH 1
  10306. ;
  10307. ; "Due to a bug, if you bring in a peripheral channel and later use a colour
  10308. ;  statement, colour controls will be sent to it by mistake." - Steven Vickers
  10309. ;  Pitman Pocket Guide, 1984.
  10310. ;
  10311. ; To be fair, this only applies if the last channel was other than 'K', 'S'
  10312. ; or 'P', which are all that are supported by this ROM, but if that last
  10313. ; channel was a microdrive file, network channel etc. then
  10314. ; PAPER 6; CLS will not turn the screen yellow and
  10315. ; CIRCLE INK 2; 128,88,50 will not draw a red circle.
  10316. ;
  10317. ; This bug does not apply to embedded PRINT items as it is quite permissible
  10318. ; to mix stream altering commands and colour items.
  10319. ; The fix therefore would be to ensure that CLASS-07 and CLASS-09 make
  10320. ; channel 'S' the current channel when not checking syntax.
  10321. ; -----------------------------------------------------------------
  10322.  
  10323. ;; CO-TEMP-1
  10324. L21E1:  RST     20H             ; NEXT-CHAR
  10325.  
  10326. ; -> Entry point from CLASS-09. Embedded Graphic colour items.
  10327. ; e.g. PLOT INK 2; PAPER 8; 128,88
  10328. ; Loops till all colour items output, finally addressing the coordinates.
  10329.  
  10330. ;; CO-TEMP-2
  10331. L21E2:  CALL    L21F2           ; routine CO-TEMP-3 to output colour control.
  10332.         RET     C               ; return if nothing more to output. ->
  10333.  
  10334.  
  10335.         RST     18H             ; GET-CHAR
  10336.         CP      $2C             ; is it ',' separator ?
  10337.         JR      Z,L21E1         ; back if so to CO-TEMP-1
  10338.  
  10339.         CP      $3B             ; is it ';' separator ?
  10340.         JR      Z,L21E1         ; back to CO-TEMP-1 for more.
  10341.  
  10342.         JP      L1C8A           ; to REPORT-C (REPORT-Cb is within range)
  10343.                                 ; 'Nonsense in BASIC'
  10344.  
  10345. ; -------------------
  10346. ; CO-TEMP-3
  10347. ; -------------------
  10348. ; -> this routine evaluates and outputs a colour control and parameter.
  10349. ; It is called from above and also from PR-ITEM-3 to handle a single embedded
  10350. ; print item e.g. PRINT PAPER 6; "Hi". In the latter case, the looping for
  10351. ; multiple items is within the PR-ITEM routine.
  10352. ; It is quite permissible to send these to any stream.
  10353.  
  10354. ;; CO-TEMP-3
  10355. L21F2:  CP      $D9             ; is it 'INK' ?
  10356.         RET     C               ; return if less.
  10357.  
  10358.         CP      $DF             ; compare with 'OUT'
  10359.         CCF                     ; Complement Carry Flag
  10360.         RET     C               ; return if greater than 'OVER', $DE.
  10361.  
  10362.         PUSH    AF              ; save the colour token.
  10363.  
  10364.         RST     20H             ; address NEXT-CHAR
  10365.         POP     AF              ; restore token and continue.
  10366.  
  10367. ; -> this entry point used by CLASS-07. e.g. the command PAPER 6.
  10368.  
  10369. ;; CO-TEMP-4
  10370. L21FC:  SUB     $C9             ; reduce to control character $10 (INK)
  10371.                                 ; thru $15 (OVER).
  10372.         PUSH    AF              ; save control.
  10373.         CALL    L1C82           ; routine EXPT-1NUM stacks addressed
  10374.                                 ; parameter on calculator stack.
  10375.         POP     AF              ; restore control.
  10376.         AND     A               ; clear carry
  10377.  
  10378.         CALL    L1FC3           ; routine UNSTACK-Z returns if checking syntax.
  10379.  
  10380.         PUSH    AF              ; save again
  10381.         CALL    L1E94           ; routine FIND-INT1 fetches parameter to A.
  10382.         LD      D,A             ; transfer now to D
  10383.         POP     AF              ; restore control.
  10384.  
  10385.         RST     10H             ; PRINT-A outputs the control to current
  10386.                                 ; channel.
  10387.         LD      A,D             ; transfer parameter to A.
  10388.  
  10389.         RST     10H             ; PRINT-A outputs parameter.
  10390.         RET                     ; return. ->
  10391.  
  10392. ; -------------------------------------------------------------------------
  10393. ;
  10394. ;         {fl}{br}{   paper   }{  ink    }    The temporary colour attributes
  10395. ;          ___ ___ ___ ___ ___ ___ ___ ___    system variable.
  10396. ; ATTR_T  |   |   |   |   |   |   |   |   |
  10397. ;         |   |   |   |   |   |   |   |   |
  10398. ; 23695   |___|___|___|___|___|___|___|___|
  10399. ;           7   6   5   4   3   2   1   0
  10400. ;
  10401. ;
  10402. ;         {fl}{br}{   paper   }{  ink    }    The temporary mask used for
  10403. ;          ___ ___ ___ ___ ___ ___ ___ ___    transparent colours. Any bit
  10404. ; MASK_T  |   |   |   |   |   |   |   |   |   that is 1 shows that the
  10405. ;         |   |   |   |   |   |   |   |   |   corresponding attribute is
  10406. ; 23696   |___|___|___|___|___|___|___|___|   taken not from ATTR-T but from
  10407. ;           7   6   5   4   3   2   1   0     what is already on the screen.
  10408. ;
  10409. ;
  10410. ;         {paper9 }{ ink9 }{ inv1 }{ over1}   The print flags. Even bits are
  10411. ;          ___ ___ ___ ___ ___ ___ ___ ___    temporary flags. The odd bits
  10412. ; P_FLAG  |   |   |   |   |   |   |   |   |   are the permanent flags.
  10413. ;         | p | t | p | t | p | t | p | t |
  10414. ; 23697   |___|___|___|___|___|___|___|___|
  10415. ;           7   6   5   4   3   2   1   0
  10416. ;
  10417. ; -----------------------------------------------------------------------
  10418.  
  10419. ; ------------------------------------
  10420. ;  The colour system variable handler.
  10421. ; ------------------------------------
  10422. ; This is an exit branch from PO-1-OPER, PO-2-OPER
  10423. ; A holds control $10 (INK) to $15 (OVER)
  10424. ; D holds parameter 0-9 for ink/paper 0,1 or 8 for bright/flash,
  10425. ; 0 or 1 for over/inverse.
  10426.  
  10427. ;; CO-TEMP-5
  10428. L2211:  SUB     $11             ; reduce range $FF-$04
  10429.         ADC     A,$00           ; add in carry if INK
  10430.         JR      Z,L2234         ; forward to CO-TEMP-7 with INK and PAPER.
  10431.  
  10432.         SUB     $02             ; reduce range $FF-$02
  10433.         ADC     A,$00           ; add carry if FLASH
  10434.         JR      Z,L2273         ; forward to CO-TEMP-C with FLASH and BRIGHT.
  10435.  
  10436.         CP      $01             ; is it 'INVERSE' ?
  10437.         LD      A,D             ; fetch parameter for INVERSE/OVER
  10438.         LD      B,$01           ; prepare OVER mask setting bit 0.
  10439.         JR      NZ,L2228        ; forward to CO-TEMP-6 if OVER
  10440.  
  10441.         RLCA                    ; shift bit 0
  10442.         RLCA                    ; to bit 2
  10443.         LD      B,$04           ; set bit 2 of mask for inverse.
  10444.  
  10445. ;; CO-TEMP-6
  10446. L2228:  LD      C,A             ; save the A
  10447.         LD      A,D             ; re-fetch parameter
  10448.         CP      $02             ; is it less than 2
  10449.         JR      NC,L2244        ; to REPORT-K if not 0 or 1.
  10450.                                 ; 'Invalid colour'.
  10451.  
  10452.         LD      A,C             ; restore A
  10453.         LD      HL,$5C91        ; address system variable P_FLAG
  10454.         JR      L226C           ; forward to exit via routine CO-CHANGE
  10455.  
  10456. ; ---
  10457.  
  10458. ; the branch was here with INK/PAPER and carry set for INK.
  10459.  
  10460. ;; CO-TEMP-7
  10461. L2234:  LD      A,D             ; fetch parameter
  10462.         LD      B,$07           ; set ink mask 00000111
  10463.         JR      C,L223E         ; forward to CO-TEMP-8 with INK
  10464.  
  10465.         RLCA                    ; shift bits 0-2
  10466.         RLCA                    ; to
  10467.         RLCA                    ; bits 3-5
  10468.         LD      B,$38           ; set paper mask 00111000
  10469.  
  10470. ; both paper and ink rejoin here
  10471.  
  10472. ;; CO-TEMP-8
  10473. L223E:  LD      C,A             ; value to C
  10474.         LD      A,D             ; fetch parameter
  10475.         CP      $0A             ; is it less than 10d ?
  10476.         JR      C,L2246         ; forward to CO-TEMP-9 if so.
  10477.  
  10478. ; ink 10 etc. is not allowed.
  10479.  
  10480. ;; REPORT-K
  10481. L2244:  RST     08H             ; ERROR-1
  10482.         DB    $13             ; Error Report: Invalid colour
  10483.  
  10484. ;; CO-TEMP-9
  10485. L2246:  LD      HL,$5C8F        ; address system variable ATTR_T initially.
  10486.         CP      $08             ; compare with 8
  10487.         JR      C,L2258         ; forward to CO-TEMP-B with 0-7.
  10488.  
  10489.         LD      A,(HL)          ; fetch temporary attribute as no change.
  10490.         JR      Z,L2257         ; forward to CO-TEMP-A with INK/PAPER 8
  10491.  
  10492. ; it is either ink 9 or paper 9 (contrasting)
  10493.  
  10494.         OR      B               ; or with mask to make white
  10495.         CPL                     ; make black and change other to dark
  10496.         AND     $24             ; 00100100
  10497.         JR      Z,L2257         ; forward to CO-TEMP-A if black and
  10498.                                 ; originally light.
  10499.  
  10500.         LD      A,B             ; else just use the mask (white)
  10501.  
  10502. ;; CO-TEMP-A
  10503. L2257:  LD      C,A             ; save A in C
  10504.  
  10505. ;; CO-TEMP-B
  10506. L2258:  LD      A,C             ; load colour to A
  10507.         CALL    L226C           ; routine CO-CHANGE addressing ATTR-T
  10508.  
  10509.         LD      A,$07           ; put 7 in accumulator
  10510.         CP      D               ; compare with parameter
  10511.         SBC     A,A             ; $00 if 0-7, $FF if 8
  10512.         CALL    L226C           ; routine CO-CHANGE addressing MASK-T
  10513.                                 ; mask returned in A.
  10514.  
  10515. ; now consider P-FLAG.
  10516.  
  10517.         RLCA                    ; 01110000 or 00001110
  10518.         RLCA                    ; 11100000 or 00011100
  10519.         AND     $50             ; 01000000 or 00010000  (AND 01010000)
  10520.         LD      B,A             ; transfer to mask
  10521.         LD      A,$08           ; load A with 8
  10522.         CP      D               ; compare with parameter
  10523.         SBC     A,A             ; $FF if was 9,  $00 if 0-8
  10524.                                 ; continue while addressing P-FLAG
  10525.                                 ; setting bit 4 if ink 9
  10526.                                 ; setting bit 6 if paper 9
  10527.  
  10528. ; -----------------------
  10529. ; Handle change of colour
  10530. ; -----------------------
  10531. ; This routine addresses a system variable ATTR_T, MASK_T or P-FLAG in HL.
  10532. ; colour value in A, mask in B.
  10533.  
  10534. ;; CO-CHANGE
  10535. L226C:  XOR     (HL)            ; impress bits specified
  10536.         AND     B               ; by mask
  10537.         XOR     (HL)            ; on system variable.
  10538.         LD      (HL),A          ; update system variable.
  10539.         INC     HL              ; address next location.
  10540.         LD      A,B             ; put current value of mask in A
  10541.         RET                     ; return.
  10542.  
  10543. ; ---
  10544.  
  10545. ; the branch was here with flash and bright
  10546.  
  10547. ;; CO-TEMP-C
  10548. L2273:  SBC     A,A             ; set zero flag for bright.
  10549.         LD      A,D             ; fetch original parameter 0,1 or 8
  10550.         RRCA                    ; rotate bit 0 to bit 7
  10551.         LD      B,$80           ; mask for flash 10000000
  10552.         JR      NZ,L227D        ; forward to CO-TEMP-D if flash
  10553.  
  10554.         RRCA                    ; rotate bit 7 to bit 6
  10555.         LD      B,$40           ; mask for bright 01000000
  10556.  
  10557. ;; CO-TEMP-D
  10558. L227D:  LD      C,A             ; store value in C
  10559.         LD      A,D             ; fetch parameter
  10560.         CP      $08             ; compare with 8
  10561.         JR      Z,L2287         ; forward to CO-TEMP-E if 8
  10562.  
  10563.         CP      $02             ; test if 0 or 1
  10564.         JR      NC,L2244        ; back to REPORT-K if not
  10565.                                 ; 'Invalid colour'
  10566.  
  10567. ;; CO-TEMP-E
  10568. L2287:  LD      A,C             ; value to A
  10569.         LD      HL,$5C8F        ; address ATTR_T
  10570.         CALL    L226C           ; routine CO-CHANGE addressing ATTR_T
  10571.         LD      A,C             ; fetch value
  10572.         RRCA                    ; for flash8/bright8 complete
  10573.         RRCA                    ; rotations to put set bit in
  10574.         RRCA                    ; bit 7 (flash) bit 6 (bright)
  10575.         JR      L226C           ; back to CO-CHANGE addressing MASK_T
  10576.                                 ; and indirect return.
  10577.  
  10578. ; ---------------------
  10579. ; Handle BORDER command
  10580. ; ---------------------
  10581. ; Command syntax example: BORDER 7
  10582. ; This command routine sets the border to one of the eight colours.
  10583. ; The colours used for the lower screen are based on this.
  10584.  
  10585. ;; BORDER
  10586. L2294:  CALL    L1E94           ; routine FIND-INT1
  10587.         CP      $08             ; must be in range 0 (black) to 7 (white)
  10588.         JR      NC,L2244        ; back to REPORT-K if not
  10589.                                 ; 'Invalid colour'.
  10590.  
  10591.         OUT     ($FE),A         ; outputting to port effects an immediate
  10592.                                 ; change.
  10593.         RLCA                    ; shift the colour to
  10594.         RLCA                    ; the paper bits setting the
  10595.         RLCA                    ; ink colour black.
  10596.         BIT     5,A             ; is the number light coloured ?
  10597.                                 ; i.e. in the range green to white.
  10598.         JR      NZ,L22A6        ; skip to BORDER-1 if so
  10599.  
  10600.         XOR     $07             ; make the ink white.
  10601.  
  10602. ;; BORDER-1
  10603. L22A6:  LD      ($5C48),A       ; update BORDCR with new paper/ink
  10604.         RET                     ; return.
  10605.  
  10606. ; -----------------
  10607. ; Get pixel address
  10608. ; -----------------
  10609. ;
  10610. ;
  10611.  
  10612. ;; PIXEL-ADD
  10613. L22AA:  LD      A,$AF           ; load with 175 decimal.
  10614.         SUB     B               ; subtract the y value.
  10615.         JP      C,L24F9         ; jump forward to REPORT-Bc if greater.
  10616.                                 ; 'Integer out of range'
  10617.  
  10618. ; the high byte is derived from Y only.
  10619. ; the first 3 bits are always 010
  10620. ; the next 2 bits denote in which third of the screen the byte is.
  10621. ; the last 3 bits denote in which of the 8 scan lines within a third
  10622. ; the byte is located. There are 24 discrete values.
  10623.  
  10624.  
  10625.         LD      B,A             ; the line number from top of screen to B.
  10626.         AND     A               ; clear carry (already clear)
  10627.         RRA                     ;                     0xxxxxxx
  10628.         SCF                     ; set carry flag
  10629.         RRA                     ;                     10xxxxxx
  10630.         AND     A               ; clear carry flag
  10631.         RRA                     ;                     010xxxxx
  10632.  
  10633.         XOR     B               ;
  10634.         AND     $F8             ; keep the top 5 bits 11111000
  10635.         XOR     B               ;                     010xxbbb
  10636.         LD      H,A             ; transfer high byte to H.
  10637.  
  10638. ; the low byte is derived from both X and Y.
  10639.  
  10640.         LD      A,C             ; the x value 0-255.
  10641.         RLCA                    ;
  10642.         RLCA                    ;
  10643.         RLCA                    ;
  10644.         XOR     B               ; the y value
  10645.         AND     $C7             ; apply mask             11000111
  10646.         XOR     B               ; restore unmasked bits  xxyyyxxx
  10647.         RLCA                    ; rotate to              xyyyxxxx
  10648.         RLCA                    ; required position.     yyyxxxxx
  10649.         LD      L,A             ; low byte to L.
  10650.  
  10651. ; finally form the pixel position in A.
  10652.  
  10653.         LD      A,C             ; x value to A
  10654.         AND     $07             ; mod 8
  10655.         RET                     ; return
  10656.  
  10657. ; ----------------
  10658. ; Point Subroutine
  10659. ; ----------------
  10660. ; The point subroutine is called from s-point via the scanning functions
  10661. ; table.
  10662.  
  10663. ;; POINT-SUB
  10664. L22CB:  CALL    L2307           ; routine STK-TO-BC
  10665.         CALL    L22AA           ; routine PIXEL-ADD finds address of pixel.
  10666.         LD      B,A             ; pixel position to B, 0-7.
  10667.         INC     B               ; increment to give rotation count 1-8.
  10668.         LD      A,(HL)          ; fetch byte from screen.
  10669.  
  10670. ;; POINT-LP
  10671. L22D4:  RLCA                    ; rotate and loop back
  10672.         DJNZ    L22D4           ; to POINT-LP until pixel at right.
  10673.  
  10674.         AND      $01            ; test to give zero or one.
  10675.         JP      L2D28           ; jump forward to STACK-A to save result.
  10676.  
  10677. ; -------------------
  10678. ; Handle PLOT command
  10679. ; -------------------
  10680. ; Command Syntax example: PLOT 128,88
  10681. ;
  10682.  
  10683. ;; PLOT
  10684. L22DC:  CALL    L2307           ; routine STK-TO-BC
  10685.         CALL    L22E5           ; routine PLOT-SUB
  10686.         JP      L0D4D           ; to TEMPS
  10687.  
  10688. ; -------------------
  10689. ; The Plot subroutine
  10690. ; -------------------
  10691. ; A screen byte holds 8 pixels so it is necessary to rotate a mask
  10692. ; into the correct position to leave the other 7 pixels unaffected.
  10693. ; However all 64 pixels in the character cell take any embedded colour
  10694. ; items.
  10695. ; A pixel can be reset (inverse 1), toggled (over 1), or set ( with inverse
  10696. ; and over switches off). With both switches on, the byte is simply put
  10697. ; back on the screen though the colours may change.
  10698.  
  10699. ;; PLOT-SUB
  10700. L22E5:  LD      ($5C7D),BC      ; store new x/y values in COORDS
  10701.         CALL    L22AA           ; routine PIXEL-ADD gets address in HL,
  10702.                                 ; count from left 0-7 in B.
  10703.         LD      B,A             ; transfer count to B.
  10704.         INC     B               ; increase 1-8.
  10705.         LD      A,$FE           ; 11111110 in A.
  10706.  
  10707. ;; PLOT-LOOP
  10708. L22F0:  RRCA                    ; rotate mask.
  10709.         DJNZ    L22F0           ; to PLOT-LOOP until B circular rotations.
  10710.  
  10711.         LD      B,A             ; load mask to B
  10712.         LD      A,(HL)          ; fetch screen byte to A
  10713.  
  10714.         LD      C,(IY+$57)      ; P_FLAG to C
  10715.         BIT     0,C             ; is it to be OVER 1 ?
  10716.         JR      NZ,L22FD        ; forward to PL-TST-IN if so.
  10717.  
  10718. ; was over 0
  10719.  
  10720.         AND     B               ; combine with mask to blank pixel.
  10721.  
  10722. ;; PL-TST-IN
  10723. L22FD:  BIT     2,C             ; is it inverse 1 ?
  10724.         JR      NZ,L2303        ; to PLOT-END if so.
  10725.  
  10726.         XOR     B               ; switch the pixel
  10727.         CPL                     ; restore other 7 bits
  10728.  
  10729. ;; PLOT-END
  10730. L2303:  LD      (HL),A          ; load byte to the screen.
  10731.         JP      L0BDB           ; exit to PO-ATTR to set colours for cell.
  10732.  
  10733. ; ------------------------------
  10734. ; Put two numbers in BC register
  10735. ; ------------------------------
  10736. ;
  10737. ;
  10738.  
  10739. ;; STK-TO-BC
  10740. L2307:  CALL    L2314           ; routine STK-TO-A
  10741.         LD      B,A             ;
  10742.         PUSH    BC              ;
  10743.         CALL    L2314           ; routine STK-TO-A
  10744.         LD      E,C             ;
  10745.         POP     BC              ;
  10746.         LD      D,C             ;
  10747.         LD      C,A             ;
  10748.         RET                     ;
  10749.  
  10750. ; -----------------------
  10751. ; Put stack in A register
  10752. ; -----------------------
  10753. ; This routine puts the last value on the calculator stack into the accumulator
  10754. ; deleting the last value.
  10755.  
  10756. ;; STK-TO-A
  10757. L2314:  CALL    L2DD5           ; routine FP-TO-A compresses last value into
  10758.                                 ; accumulator. e.g. PI would become 3.
  10759.                                 ; zero flag set if positive.
  10760.         JP      C,L24F9         ; jump forward to REPORT-Bc if >= 255.5.
  10761.  
  10762.         LD      C,$01           ; prepare a positive sign byte.
  10763.         RET     Z               ; return if FP-TO-BC indicated positive.
  10764.  
  10765.         LD      C,$FF           ; prepare negative sign byte and
  10766.         RET                     ; return.
  10767.  
  10768.  
  10769. ; ---------------------
  10770. ; Handle CIRCLE command
  10771. ; ---------------------
  10772. ;
  10773. ; syntax has been partly checked using the class for draw command.
  10774.  
  10775. ;; CIRCLE
  10776. L2320:  RST     18H             ; GET-CHAR
  10777.         CP      $2C             ; is it required comma ?
  10778.         JP      NZ,L1C8A        ; jump to REPORT-C if not
  10779.  
  10780.  
  10781.         RST     20H             ; NEXT-CHAR
  10782.         CALL    L1C82           ; routine EXPT-1NUM fetches radius
  10783.         CALL    L1BEE           ; routine CHECK-END will return here if
  10784.                                 ; nothing follows command.
  10785.  
  10786.         RST     28H             ;; FP-CALC
  10787.         DB    $2A             ;;abs           ; make radius positive
  10788.         DB    $3D             ;;re-stack      ; in full floating point form
  10789.         DB    $38             ;;end-calc
  10790.  
  10791.         LD      A,(HL)          ; fetch first floating point byte
  10792.         CP      $81             ; compare to one
  10793.         JR      NC,L233B        ; forward to C-R-GRE-1 if circle radius
  10794.                                 ; is greater than one.
  10795.  
  10796.  
  10797.         RST     28H             ;; FP-CALC
  10798.         DB    $02             ;;delete        ; delete the radius from stack.
  10799.         DB    $38             ;;end-calc
  10800.  
  10801.         JR      L22DC           ; to PLOT to just plot x,y.
  10802.  
  10803. ; ---
  10804.  
  10805.  
  10806. ;; C-R-GRE-1
  10807. L233B:  RST     28H             ;; FP-CALC      ; x, y, r
  10808.         DB    $A3             ;;stk-pi/2      ; x, y, r, pi/2.
  10809.         DB    $38             ;;end-calc
  10810.  
  10811.         LD      (HL),$83        ;               ; x, y, r, 2*PI
  10812.  
  10813.         RST     28H             ;; FP-CALC
  10814.         DB    $C5             ;;st-mem-5      ; store 2*PI in mem-5
  10815.         DB    $02             ;;delete        ; x, y, z.
  10816.         DB    $38             ;;end-calc
  10817.  
  10818.         CALL    L247D           ; routine CD-PRMS1
  10819.         PUSH    BC              ;
  10820.  
  10821.         RST     28H             ;; FP-CALC
  10822.         DB    $31             ;;duplicate
  10823.         DB    $E1             ;;get-mem-1
  10824.         DB    $04             ;;multiply
  10825.         DB    $38             ;;end-calc
  10826.  
  10827.         LD      A,(HL)          ;
  10828.         CP      $80             ;
  10829.         JR      NC,L235A        ; to C-ARC-GE1
  10830.  
  10831.  
  10832.         RST     28H             ;; FP-CALC
  10833.         DB    $02             ;;delete
  10834.         DB    $02             ;;delete
  10835.         DB    $38             ;;end-calc
  10836.  
  10837.         POP     BC              ;
  10838.         JP      L22DC           ; JUMP to PLOT
  10839.  
  10840. ; ---
  10841.  
  10842.  
  10843. ;; C-ARC-GE1
  10844. L235A:  RST     28H             ;; FP-CALC
  10845.         DB    $C2             ;;st-mem-2
  10846.         DB    $01             ;;exchange
  10847.         DB    $C0             ;;st-mem-0
  10848.         DB    $02             ;;delete
  10849.         DB    $03             ;;subtract
  10850.         DB    $01             ;;exchange
  10851.         DB    $E0             ;;get-mem-0
  10852.         DB    $0F             ;;addition
  10853.         DB    $C0             ;;st-mem-0
  10854.         DB    $01             ;;exchange
  10855.         DB    $31             ;;duplicate
  10856.         DB    $E0             ;;get-mem-0
  10857.         DB    $01             ;;exchange
  10858.         DB    $31             ;;duplicate
  10859.         DB    $E0             ;;get-mem-0
  10860.         DB    $A0             ;;stk-zero
  10861.         DB    $C1             ;;st-mem-1
  10862.         DB    $02             ;;delete
  10863.         DB    $38             ;;end-calc
  10864.  
  10865.         INC     (IY+$62)        ; MEM-2-1st
  10866.         CALL     L1E94          ; routine FIND-INT1
  10867.         LD      L,A             ;
  10868.         PUSH    HL              ;
  10869.         CALL    L1E94           ; routine FIND-INT1
  10870.         POP     HL              ;
  10871.         LD      H,A             ;
  10872.         LD      ($5C7D),HL      ; COORDS
  10873.         POP     BC              ;
  10874.         JP      L2420           ; to DRW-STEPS
  10875.  
  10876.  
  10877. ; -------------------
  10878. ; Handle DRAW command
  10879. ; -------------------
  10880. ;
  10881. ;
  10882.  
  10883. ;; DRAW
  10884. L2382:  RST     18H             ; GET-CHAR
  10885.         CP      $2C             ;
  10886.         JR      Z,L238D         ; to DR-3-PRMS
  10887.  
  10888.         CALL    L1BEE           ; routine CHECK-END
  10889.         JP      L2477           ; to LINE-DRAW
  10890.  
  10891. ; ---
  10892.  
  10893. ;; DR-3-PRMS
  10894. L238D:  RST     20H             ; NEXT-CHAR
  10895.         CALL    L1C82           ; routine EXPT-1NUM
  10896.         CALL    L1BEE           ; routine CHECK-END
  10897.  
  10898.         RST     28H             ;; FP-CALC
  10899.         DB    $C5             ;;st-mem-5
  10900.         DB    $A2             ;;stk-half
  10901.         DB    $04             ;;multiply
  10902.         DB    $1F             ;;sin
  10903.         DB    $31             ;;duplicate
  10904.         DB    $30             ;;not
  10905.         DB    $30             ;;not
  10906.         DB    $00             ;;jump-true
  10907.  
  10908.         DB    $06             ;;to L23A3, DR-SIN-NZ
  10909.  
  10910.         DB    $02             ;;delete
  10911.         DB    $38             ;;end-calc
  10912.  
  10913.         JP      L2477           ; to LINE-DRAW
  10914.  
  10915. ; ---
  10916.  
  10917. ;; DR-SIN-NZ
  10918. L23A3:  DB    $C0             ;;st-mem-0
  10919.         DB    $02             ;;delete
  10920.         DB    $C1             ;;st-mem-1
  10921.         DB    $02             ;;delete
  10922.         DB    $31             ;;duplicate
  10923.         DB    $2A             ;;abs
  10924.         DB    $E1             ;;get-mem-1
  10925.         DB    $01             ;;exchange
  10926.         DB    $E1             ;;get-mem-1
  10927.         DB    $2A             ;;abs
  10928.         DB    $0F             ;;addition
  10929.         DB    $E0             ;;get-mem-0
  10930.         DB    $05             ;;division
  10931.         DB    $2A             ;;abs
  10932.         DB    $E0             ;;get-mem-0
  10933.         DB    $01             ;;exchange
  10934.         DB    $3D             ;;re-stack
  10935.         DB    $38             ;;end-calc
  10936.  
  10937.         LD      A,(HL)          ;
  10938.         CP      $81             ;
  10939.         JR      NC,L23C1        ; to DR-PRMS
  10940.  
  10941.  
  10942.         RST     28H             ;; FP-CALC
  10943.         DB    $02             ;;delete
  10944.         DB    $02             ;;delete
  10945.         DB    $38             ;;end-calc
  10946.  
  10947.         JP      L2477           ; to LINE-DRAW
  10948.  
  10949. ; ---
  10950.  
  10951. ;; DR-PRMS
  10952. L23C1:  CALL    L247D           ; routine CD-PRMS1
  10953.         PUSH    BC              ;
  10954.  
  10955.         RST     28H             ;; FP-CALC
  10956.         DB    $02             ;;delete
  10957.         DB    $E1             ;;get-mem-1
  10958.         DB    $01             ;;exchange
  10959.         DB    $05             ;;division
  10960.         DB    $C1             ;;st-mem-1
  10961.         DB    $02             ;;delete
  10962.         DB    $01             ;;exchange
  10963.         DB    $31             ;;duplicate
  10964.         DB    $E1             ;;get-mem-1
  10965.         DB    $04             ;;multiply
  10966.         DB    $C2             ;;st-mem-2
  10967.         DB    $02             ;;delete
  10968.         DB    $01             ;;exchange
  10969.         DB    $31             ;;duplicate
  10970.         DB    $E1             ;;get-mem-1
  10971.         DB    $04             ;;multiply
  10972.         DB    $E2             ;;get-mem-2
  10973.         DB    $E5             ;;get-mem-5
  10974.         DB    $E0             ;;get-mem-0
  10975.         DB    $03             ;;subtract
  10976.         DB    $A2             ;;stk-half
  10977.         DB    $04             ;;multiply
  10978.         DB    $31             ;;duplicate
  10979.         DB    $1F             ;;sin
  10980.         DB    $C5             ;;st-mem-5
  10981.         DB    $02             ;;delete
  10982.         DB    $20             ;;cos
  10983.         DB    $C0             ;;st-mem-0
  10984.         DB    $02             ;;delete
  10985.         DB    $C2             ;;st-mem-2
  10986.         DB    $02             ;;delete
  10987.         DB    $C1             ;;st-mem-1
  10988.         DB    $E5             ;;get-mem-5
  10989.         DB    $04             ;;multiply
  10990.         DB    $E0             ;;get-mem-0
  10991.         DB    $E2             ;;get-mem-2
  10992.         DB    $04             ;;multiply
  10993.         DB    $0F             ;;addition
  10994.         DB    $E1             ;;get-mem-1
  10995.         DB    $01             ;;exchange
  10996.         DB    $C1             ;;st-mem-1
  10997.         DB    $02             ;;delete
  10998.         DB    $E0             ;;get-mem-0
  10999.         DB    $04             ;;multiply
  11000.         DB    $E2             ;;get-mem-2
  11001.         DB    $E5             ;;get-mem-5
  11002.         DB    $04             ;;multiply
  11003.         DB    $03             ;;subtract
  11004.         DB    $C2             ;;st-mem-2
  11005.         DB    $2A             ;;abs
  11006.         DB    $E1             ;;get-mem-1
  11007.         DB    $2A             ;;abs
  11008.         DB    $0F             ;;addition
  11009.         DB    $02             ;;delete
  11010.         DB    $38             ;;end-calc
  11011.  
  11012.         LD      A,(DE)          ;
  11013.         CP       $81            ;
  11014.         POP     BC              ;
  11015.         JP      C,L2477         ; to LINE-DRAW
  11016.  
  11017.         PUSH    BC              ;
  11018.  
  11019.         RST     28H             ;; FP-CALC
  11020.         DB    $01             ;;exchange
  11021.         DB    $38             ;;end-calc
  11022.  
  11023.         LD      A,($5C7D)       ; COORDS-x
  11024.         CALL    L2D28           ; routine STACK-A
  11025.  
  11026.         RST     28H             ;; FP-CALC
  11027.         DB    $C0             ;;st-mem-0
  11028.         DB    $0F             ;;addition
  11029.         DB    $01             ;;exchange
  11030.         DB    $38             ;;end-calc
  11031.  
  11032.         LD      A,($5C7E)       ; COORDS-y
  11033.         CALL    L2D28           ; routine STACK-A
  11034.  
  11035.         RST     28H             ;; FP-CALC
  11036.         DB    $C5             ;;st-mem-5
  11037.         DB    $0F             ;;addition
  11038.         DB    $E0             ;;get-mem-0
  11039.         DB    $E5             ;;get-mem-5
  11040.         DB    $38             ;;end-calc
  11041.  
  11042.         POP     BC              ;
  11043.  
  11044. ;; DRW-STEPS
  11045. L2420:  DEC     B               ;
  11046.         JR      Z,L245F         ; to ARC-END
  11047.  
  11048.         JR      L2439           ; to ARC-START
  11049.  
  11050. ; ---
  11051.  
  11052.  
  11053. ;; ARC-LOOP
  11054. L2425:  RST     28H             ;; FP-CALC
  11055.         DB    $E1             ;;get-mem-1
  11056.         DB    $31             ;;duplicate
  11057.         DB    $E3             ;;get-mem-3
  11058.         DB    $04             ;;multiply
  11059.         DB    $E2             ;;get-mem-2
  11060.         DB    $E4             ;;get-mem-4
  11061.         DB    $04             ;;multiply
  11062.         DB    $03             ;;subtract
  11063.         DB    $C1             ;;st-mem-1
  11064.         DB    $02             ;;delete
  11065.         DB    $E4             ;;get-mem-4
  11066.         DB    $04             ;;multiply
  11067.         DB    $E2             ;;get-mem-2
  11068.         DB    $E3             ;;get-mem-3
  11069.         DB    $04             ;;multiply
  11070.         DB    $0F             ;;addition
  11071.         DB    $C2             ;;st-mem-2
  11072.         DB    $02             ;;delete
  11073.         DB    $38             ;;end-calc
  11074.  
  11075. ;; ARC-START
  11076. L2439:  PUSH    BC              ;
  11077.  
  11078.         RST     28H             ;; FP-CALC
  11079.         DB    $C0             ;;st-mem-0
  11080.         DB    $02             ;;delete
  11081.         DB    $E1             ;;get-mem-1
  11082.         DB    $0F             ;;addition
  11083.         DB    $31             ;;duplicate
  11084.         DB    $38             ;;end-calc
  11085.  
  11086.         LD      A,($5C7D)       ; COORDS-x
  11087.         CALL    L2D28           ; routine STACK-A
  11088.  
  11089.         RST     28H             ;; FP-CALC
  11090.         DB    $03             ;;subtract
  11091.         DB    $E0             ;;get-mem-0
  11092.         DB    $E2             ;;get-mem-2
  11093.         DB    $0F             ;;addition
  11094.         DB    $C0             ;;st-mem-0
  11095.         DB    $01             ;;exchange
  11096.         DB    $E0             ;;get-mem-0
  11097.         DB    $38             ;;end-calc
  11098.  
  11099.         LD      A,($5C7E)       ; COORDS-y
  11100.         CALL    L2D28           ; routine STACK-A
  11101.  
  11102.         RST     28H             ;; FP-CALC
  11103.         DB    $03             ;;subtract
  11104.         DB    $38             ;;end-calc
  11105.  
  11106.         CALL    L24B7           ; routine DRAW-LINE
  11107.         POP     BC              ;
  11108.         DJNZ    L2425           ; to ARC-LOOP
  11109.  
  11110.  
  11111. ;; ARC-END
  11112. L245F:  RST     28H             ;; FP-CALC
  11113.         DB    $02             ;;delete
  11114.         DB    $02             ;;delete
  11115.         DB    $01             ;;exchange
  11116.         DB    $38             ;;end-calc
  11117.  
  11118.         LD      A,($5C7D)       ; COORDS-x
  11119.         CALL    L2D28           ; routine STACK-A
  11120.  
  11121.         RST     28H             ;; FP-CALC
  11122.         DB    $03             ;;subtract
  11123.         DB    $01             ;;exchange
  11124.         DB    $38             ;;end-calc
  11125.  
  11126.         LD      A,($5C7E)       ; COORDS-y
  11127.         CALL    L2D28           ; routine STACK-A
  11128.  
  11129.         RST     28H             ;; FP-CALC
  11130.         DB    $03             ;;subtract
  11131.         DB    $38             ;;end-calc
  11132.  
  11133. ;; LINE-DRAW
  11134. L2477:  CALL    L24B7           ; routine DRAW-LINE
  11135.         JP      L0D4D           ; to TEMPS
  11136.  
  11137.  
  11138. ; ------------------
  11139. ; Initial parameters
  11140. ; ------------------
  11141. ;
  11142. ;
  11143.  
  11144. ;; CD-PRMS1
  11145. L247D:  RST     28H             ;; FP-CALC
  11146.         DB    $31             ;;duplicate
  11147.         DB    $28             ;;sqr
  11148.         DB    $34             ;;stk-data
  11149.         DB    $32             ;;Exponent: $82, Bytes: 1
  11150.         DB    $00             ;;(+00,+00,+00)
  11151.         DB    $01             ;;exchange
  11152.         DB    $05             ;;division
  11153.         DB    $E5             ;;get-mem-5
  11154.         DB    $01             ;;exchange
  11155.         DB    $05             ;;division
  11156.         DB    $2A             ;;abs
  11157.         DB    $38             ;;end-calc
  11158.  
  11159.         CALL    L2DD5           ; routine FP-TO-A
  11160.         JR      C,L2495         ; to USE-252
  11161.  
  11162.         AND     $FC             ;
  11163.         ADD     A,$04           ;
  11164.         JR      NC,L2497        ; to DRAW-SAVE
  11165.  
  11166. ;; USE-252
  11167. L2495:  LD      A,$FC           ;
  11168.  
  11169. ;; DRAW-SAVE
  11170. L2497:  PUSH    AF              ;
  11171.         CALL    L2D28           ; routine STACK-A
  11172.  
  11173.         RST     28H             ;; FP-CALC
  11174.         DB    $E5             ;;get-mem-5
  11175.         DB    $01             ;;exchange
  11176.         DB    $05             ;;division
  11177.         DB    $31             ;;duplicate
  11178.         DB    $1F             ;;sin
  11179.         DB    $C4             ;;st-mem-4
  11180.         DB    $02             ;;delete
  11181.         DB    $31             ;;duplicate
  11182.         DB    $A2             ;;stk-half
  11183.         DB    $04             ;;multiply
  11184.         DB    $1F             ;;sin
  11185.         DB    $C1             ;;st-mem-1
  11186.         DB    $01             ;;exchange
  11187.         DB    $C0             ;;st-mem-0
  11188.         DB    $02             ;;delete
  11189.         DB    $31             ;;duplicate
  11190.         DB    $04             ;;multiply
  11191.         DB    $31             ;;duplicate
  11192.         DB    $0F             ;;addition
  11193.         DB    $A1             ;;stk-one
  11194.         DB    $03             ;;subtract
  11195.         DB    $1B             ;;negate
  11196.         DB    $C3             ;;st-mem-3
  11197.         DB    $02             ;;delete
  11198.         DB    $38             ;;end-calc
  11199.  
  11200.         POP     BC              ;
  11201.         RET                     ;
  11202.  
  11203. ; ------------
  11204. ; Line drawing
  11205. ; ------------
  11206. ;
  11207. ;
  11208.  
  11209. ;; DRAW-LINE
  11210. L24B7:  CALL    L2307           ; routine STK-TO-BC
  11211.         LD      A,C             ;
  11212.         CP      B               ;
  11213.         JR      NC,L24C4        ; to DL-X-GE-Y
  11214.  
  11215.         LD      L,C             ;
  11216.         PUSH    DE              ;
  11217.         XOR     A               ;
  11218.         LD      E,A             ;
  11219.         JR      L24CB           ; to DL-LARGER
  11220.  
  11221. ; ---
  11222.  
  11223. ;; DL-X-GE-Y
  11224. L24C4:  OR      C               ;
  11225.         RET     Z               ;
  11226.  
  11227.         LD      L,B             ;
  11228.         LD      B,C             ;
  11229.         PUSH    DE              ;
  11230.         LD      D,$00           ;
  11231.  
  11232. ;; DL-LARGER
  11233. L24CB:  LD      H,B             ;
  11234.         LD      A,B             ;
  11235.         RRA                     ;
  11236.  
  11237. ;; D-L-LOOP
  11238. L24CE:  ADD     A,L             ;
  11239.         JR      C,L24D4         ; to D-L-DIAG
  11240.  
  11241.         CP      H               ;
  11242.         JR      C,L24DB         ; to D-L-HR-VT
  11243.  
  11244. ;; D-L-DIAG
  11245. L24D4:  SUB     H               ;
  11246.         LD      C,A             ;
  11247.         EXX                     ;
  11248.         POP     BC              ;
  11249.         PUSH    BC              ;
  11250.         JR      L24DF           ; to D-L-STEP
  11251.  
  11252. ; ---
  11253.  
  11254. ;; D-L-HR-VT
  11255. L24DB:  LD      C,A             ;
  11256.         PUSH    DE              ;
  11257.         EXX                     ;
  11258.         POP     BC              ;
  11259.  
  11260. ;; D-L-STEP
  11261. L24DF:  LD      HL,($5C7D)      ; COORDS
  11262.         LD      A,B             ;
  11263.         ADD     A,H             ;
  11264.         LD      B,A             ;
  11265.         LD      A,C             ;
  11266.         INC     A               ;
  11267.         ADD     A,L             ;
  11268.         JR      C,L24F7         ; to D-L-RANGE
  11269.  
  11270.         JR      Z,L24F9         ; to REPORT-Bc
  11271.  
  11272. ;; D-L-PLOT
  11273. L24EC:  DEC     A               ;
  11274.         LD      C,A             ;
  11275.         CALL    L22E5           ; routine PLOT-SUB
  11276.         EXX                     ;
  11277.         LD      A,C             ;
  11278.         DJNZ    L24CE           ; to D-L-LOOP
  11279.  
  11280.         POP     DE              ;
  11281.         RET                     ;
  11282.  
  11283. ; ---
  11284.  
  11285. ;; D-L-RANGE
  11286. L24F7:  JR      Z,L24EC         ; to D-L-PLOT
  11287.  
  11288.  
  11289. ;; REPORT-Bc
  11290. L24F9:  RST     08H             ; ERROR-1
  11291.         DB    $0A             ; Error Report: Integer out of range
  11292.  
  11293.  
  11294.  
  11295. ;***********************************
  11296. ;** Part 8. EXPRESSION EVALUATION **
  11297. ;***********************************
  11298. ;
  11299. ; It is a this stage of the ROM that the Spectrum ceases altogether to be
  11300. ; just a colourful novelty. One remarkable feature is that in all previous
  11301. ; commands when the Spectrum is expecting a number or a string then an
  11302. ; expression of the same type can be substituted ad infinitum.
  11303. ; This is the routine that evaluates that expression.
  11304. ; This is what causes 2 + 2 to give the answer 4.
  11305. ; That is quite easy to understand. However you don't have to make it much
  11306. ; more complex to start a remarkable juggling act.
  11307. ; e.g. PRINT 2 * (VAL "2+2" + TAN 3)
  11308. ; In fact, provided there is enough free RAM, the Spectrum can evaluate
  11309. ; an expression of unlimited complexity.
  11310. ; Apart from a couple of minor glitches, which you can now correct, the
  11311. ; system is remarkably robust.
  11312.  
  11313.  
  11314. ; ---------------------------------
  11315. ; Scan expression or sub-expression
  11316. ; ---------------------------------
  11317. ;
  11318. ;
  11319.  
  11320. ;; SCANNING
  11321. L24FB:  RST     18H             ; GET-CHAR
  11322.         LD      B,$00           ; priority marker zero is pushed on stack
  11323.                                 ; to signify end of expression when it is
  11324.                                 ; popped off again.
  11325.         PUSH    BC              ; put in on stack.
  11326.                                 ; and proceed to consider the first character
  11327.                                 ; of the expression.
  11328.  
  11329. ;; S-LOOP-1
  11330. L24FF:  LD      C,A             ; store the character while a look up is done.
  11331.         LD      HL,L2596        ; Address: scan-func
  11332.         CALL    L16DC           ; routine INDEXER is called to see if it is
  11333.                                 ; part of a limited range '+', '(', 'ATTR' etc.
  11334.  
  11335.         LD      A,C             ; fetch the character back
  11336.         JP      NC,L2684        ; jump forward to S-ALPHNUM if not in primary
  11337.                                 ; operators and functions to consider in the
  11338.                                 ; first instance a digit or a variable and
  11339.                                 ; then anything else.                >>>
  11340.  
  11341.         LD      B,$00           ; but here if it was found in table so
  11342.         LD      C,(HL)          ; fetch offset from table and make B zero.
  11343.         ADD     HL,BC           ; add the offset to position found
  11344.         JP      (HL)            ; and jump to the routine e.g. S-BIN
  11345.                                 ; making an indirect exit from there.
  11346.  
  11347. ; -------------------------------------------------------------------------
  11348. ; The four service subroutines for routines in the scannings function table
  11349. ; -------------------------------------------------------------------------
  11350.  
  11351. ; PRINT """Hooray!"" he cried."
  11352.  
  11353. ;; S-QUOTE-S
  11354. L250F:  CALL    L0074           ; routine CH-ADD+1 points to next character
  11355.                                 ; and fetches that character.
  11356.         INC     BC              ; increase length counter.
  11357.         CP      $0D             ; is it carriage return ?
  11358.                                 ; inside a quote.
  11359.         JP      Z,L1C8A         ; jump back to REPORT-C if so.
  11360.                                 ; 'Nonsense in BASIC'.
  11361.  
  11362.         CP      $22             ; is it a quote '"' ?
  11363.         JR      NZ,L250F        ; back to S-QUOTE-S if not for more.
  11364.  
  11365.         CALL    L0074           ; routine CH-ADD+1
  11366.         CP      $22             ; compare with possible adjacent quote
  11367.         RET                     ; return. with zero set if two together.
  11368.  
  11369. ; ---
  11370.  
  11371. ; This subroutine is used to get two coordinate expressions for the three
  11372. ; functions SCREEN$, ATTR and POINT that have two fixed parameters and
  11373. ; therefore require surrounding braces.
  11374.  
  11375. ;; S-2-COORD
  11376. L2522:  RST     20H             ; NEXT-CHAR
  11377.         CP      $28             ; is it the opening '(' ?
  11378.         JR      NZ,L252D        ; forward to S-RPORT-C if not
  11379.                                 ; 'Nonsense in BASIC'.
  11380.  
  11381.         CALL    L1C79           ; routine NEXT-2NUM gets two comma-separated
  11382.                                 ; numeric expressions. Note. this could cause
  11383.                                 ; many more recursive calls to SCANNING but
  11384.                                 ; the parent function will be evaluated fully
  11385.                                 ; before rejoining the main juggling act.
  11386.  
  11387.         RST     18H             ; GET-CHAR
  11388.         CP      $29             ; is it the closing ')' ?
  11389.  
  11390. ;; S-RPORT-C
  11391. L252D:  JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  11392.                                 ; 'Nonsense in BASIC'.
  11393.  
  11394. ; ------------
  11395. ; Check syntax
  11396. ; ------------
  11397. ; This routine is called on a number of occasions to check if syntax is being
  11398. ; checked or if the program is being run. To test the flag inline would use
  11399. ; four bytes of code, but a call instruction only uses 3 bytes of code.
  11400.  
  11401. ;; SYNTAX-Z
  11402. L2530:  BIT     7,(IY+$01)      ; test FLAGS  - checking syntax only ?
  11403.         RET                     ; return.
  11404.  
  11405. ; ----------------
  11406. ; Scanning SCREEN$
  11407. ; ----------------
  11408. ; This function returns the code of a bit-mapped character at screen
  11409. ; position at line C, column B. It is unable to detect the mosaic characters
  11410. ; which are not bit-mapped but detects the ASCII 32 - 127 range.
  11411. ; The bit-mapped UDGs are ignored which is curious as it requires only a
  11412. ; few extra bytes of code. As usual, anything to do with CHARS is weird.
  11413. ; If no match is found a null string is returned.
  11414. ; No actual check on ranges is performed - that's up to the BASIC programmer.
  11415. ; No real harm can come from SCREEN$(255,255) although the BASIC manual
  11416. ; says that invalid values will be trapped.
  11417. ; Interestingly, in the Pitman pocket guide, 1984, Vickers says that the
  11418. ; range checking will be performed.
  11419.  
  11420. ;; S-SCRN$-S
  11421. L2535:  CALL    L2307           ; routine STK-TO-BC.
  11422.         LD      HL,($5C36)      ; fetch address of CHARS.
  11423.         LD      DE,$0100        ; fetch offset to chr$ 32
  11424.         ADD     HL,DE           ; and find start of bitmaps.
  11425.                                 ; Note. not inc h. ??
  11426.         LD      A,C             ; transfer line to A.
  11427.         RRCA                    ; multiply
  11428.         RRCA                    ; by
  11429.         RRCA                    ; thirty-two.
  11430.         AND     $E0             ; and with 11100000
  11431.         XOR     B               ; combine with column $00 - $1F
  11432.         LD      E,A             ; to give the low byte of top line
  11433.         LD      A,C             ; column to A range 00000000 to 00011111
  11434.         AND     $18             ; and with 00011000
  11435.         XOR     $40             ; xor with 01000000 (high byte screen start)
  11436.         LD      D,A             ; register DE now holds start address of cell.
  11437.         LD      B,$60           ; there are 96 characters in ASCII set.
  11438.  
  11439. ;; S-SCRN-LP
  11440. L254F:  PUSH    BC              ; save count
  11441.         PUSH    DE              ; save screen start address
  11442.         PUSH    HL              ; save bitmap start
  11443.         LD      A,(DE)          ; first byte of screen to A
  11444.         XOR     (HL)            ; xor with corresponding character byte
  11445.         JR      Z,L255A         ; forward to S-SC-MTCH if they match
  11446.                                 ; if inverse result would be $FF
  11447.                                 ; if any other then mismatch
  11448.  
  11449.         INC     A               ; set to $00 if inverse
  11450.         JR      NZ,L2573        ; forward to S-SCR-NXT if a mismatch
  11451.  
  11452.         DEC     A               ; restore $FF
  11453.  
  11454. ; a match has been found so seven more to test.
  11455.  
  11456. ;; S-SC-MTCH
  11457. L255A:  LD      C,A             ; load C with inverse mask $00 or $FF
  11458.         LD      B,$07           ; count seven more bytes
  11459.  
  11460. ;; S-SC-ROWS
  11461. L255D:  INC     D               ; increment screen address.
  11462.         INC     HL              ; increment bitmap address.
  11463.         LD      A,(DE)          ; byte to A
  11464.         XOR     (HL)            ; will give $00 or $FF (inverse)
  11465.         XOR     C               ; xor with inverse mask
  11466.         JR      NZ,L2573        ; forward to S-SCR-NXT if no match.
  11467.  
  11468.         DJNZ    L255D           ; back to S-SC-ROWS until all eight matched.
  11469.  
  11470. ; continue if a match of all eight bytes was found
  11471.  
  11472.         POP     BC              ; discard the
  11473.         POP     BC              ; saved
  11474.         POP     BC              ; pointers
  11475.         LD      A,$80           ; the endpoint of character set
  11476.         SUB     B               ; subtract the counter
  11477.                                 ; to give the code 32-127
  11478.         LD      BC,$0001        ; make one space in workspace.
  11479.  
  11480.         RST     30H             ; BC-SPACES creates the space sliding
  11481.                                 ; the calculator stack upwards.
  11482.         LD      (DE),A          ; start is addressed by DE, so insert code
  11483.         JR      L257D           ; forward to S-SCR-STO
  11484.  
  11485. ; ---
  11486.  
  11487. ; the jump was here if no match and more bitmaps to test.
  11488.  
  11489. ;; S-SCR-NXT
  11490. L2573:  POP     HL              ; restore the last bitmap start
  11491.         LD      DE,$0008        ; and prepare to add 8.
  11492.         ADD     HL,DE           ; now addresses next character bitmap.
  11493.         POP     DE              ; restore screen address
  11494.         POP     BC              ; and character counter in B
  11495.         DJNZ    L254F           ; back to S-SCRN-LP if more characters.
  11496.  
  11497.         LD      C,B             ; B is now zero, so BC now zero.
  11498.  
  11499. ;; S-SCR-STO
  11500. L257D:  JP      L2AB2           ; to STK-STO-$ to store the string in
  11501.                                 ; workspace or a string with zero length.
  11502.                                 ; (value of DE doesn't matter in last case)
  11503.  
  11504. ; Note. this exit seems correct but the general-purpose routine S-STRING
  11505. ; that calls this one will also stack any of its string results so this
  11506. ; leads to a double storing of the result in this case.
  11507. ; The instruction at L257D should just be a RET.
  11508. ; credit Stephen Kelly and others, 1982.
  11509.  
  11510. ; -------------
  11511. ; Scanning ATTR
  11512. ; -------------
  11513. ; This function subroutine returns the attributes of a screen location -
  11514. ; a numeric result.
  11515. ; Again it's up to the BASIC programmer to supply valid values of line/column.
  11516.  
  11517. ;; S-ATTR-S
  11518. L2580:  CALL    L2307           ; routine STK-TO-BC fetches line to C,
  11519.                                 ; and column to B.
  11520.         LD      A,C             ; line to A $00 - $17   (max 00010111)
  11521.         RRCA                    ; rotate
  11522.         RRCA                    ; bits
  11523.         RRCA                    ; left.
  11524.         LD      C,A             ; store in C as an intermediate value.
  11525.  
  11526.         AND     $E0             ; pick up bits 11100000 ( was 00011100 )
  11527.         XOR     B               ; combine with column $00 - $1F
  11528.         LD      L,A             ; low byte now correct.
  11529.  
  11530.         LD      A,C             ; bring back intermediate result from C
  11531.         AND     $03             ; mask to give correct third of
  11532.                                 ; screen $00 - $02
  11533.         XOR     $58             ; combine with base address.
  11534.         LD      H,A             ; high byte correct.
  11535.         LD      A,(HL)          ; pick up the colour attribute.
  11536.         JP      L2D28           ; forward to STACK-A to store result
  11537.                                 ; and make an indirect exit.
  11538.  
  11539. ; -----------------------
  11540. ; Scanning function table
  11541. ; -----------------------
  11542. ; This table is used by INDEXER routine to find the offsets to
  11543. ; four operators and eight functions. e.g. $A8 is the token 'FN'.
  11544. ; This table is used in the first instance for the first character of an
  11545. ; expression or by a recursive call to SCANNING for the first character of
  11546. ; any sub-expression. It eliminates functions that have no argument or
  11547. ; functions that can have more than one argument and therefore require
  11548. ; braces. By eliminating and dealing with these now it can later take a
  11549. ; simplistic approach to all other functions and assume that they have
  11550. ; one argument.
  11551. ; Similarly by eliminating BIN and '.' now it is later able to assume that
  11552. ; all numbers begin with a digit and that the presence of a number or
  11553. ; variable can be detected by a call to ALPHANUM.
  11554. ; By default all expressions are positive and the spurious '+' is eliminated
  11555. ; now as in print +2. This should not be confused with the operator '+'.
  11556. ; Note. this does allow a degree of nonsense to be accepted as in
  11557. ; PRINT +"3 is the greatest.".
  11558. ; An acquired programming skill is the ability to include brackets where
  11559. ; they are not necessary.
  11560. ; A bracket at the start of a sub-expression may be spurious or necessary
  11561. ; to denote that the contained expression is to be evaluated as an entity.
  11562. ; In either case this is dealt with by recursive calls to SCANNING.
  11563. ; An expression that begins with a quote requires special treatment.
  11564.  
  11565. ;; scan-func
  11566. L2596:  DB    $22, L25B3-$-1  ; $1C offset to S-QUOTE
  11567.         DB    '(', L25E8-$-1  ; $4F offset to S-BRACKET
  11568.         DB    '.', L268D-$-1  ; $F2 offset to S-DECIMAL
  11569.         DB    '+', L25AF-$-1  ; $12 offset to S-U-PLUS
  11570.  
  11571.         DB    $A8, L25F5-$-1  ; $56 offset to S-FN
  11572.         DB    $A5, L25F8-$-1  ; $57 offset to S-RND
  11573.         DB    $A7, L2627-$-1  ; $84 offset to S-PI
  11574.         DB    $A6, L2634-$-1  ; $8F offset to S-INKEY$
  11575.         DB    $C4, L268D-$-1  ; $E6 offset to S-BIN
  11576.         DB    $AA, L2668-$-1  ; $BF offset to S-SCREEN$
  11577.         DB    $AB, L2672-$-1  ; $C7 offset to S-ATTR
  11578.         DB    $A9, L267B-$-1  ; $CE offset to S-POINT
  11579.  
  11580.         DB    $00             ; zero end marker
  11581.  
  11582. ; --------------------------
  11583. ; Scanning function routines
  11584. ; --------------------------
  11585. ; These are the 11 subroutines accessed by the above table.
  11586. ; S-BIN and S-DECIMAL are the same
  11587. ; The 1-byte offset limits their location to within 255 bytes of their
  11588. ; entry in the table.
  11589.  
  11590. ; ->
  11591. ;; S-U-PLUS
  11592. L25AF:  RST     20H             ; NEXT-CHAR just ignore
  11593.         JP      L24FF           ; to S-LOOP-1
  11594.  
  11595. ; ---
  11596.  
  11597. ; ->
  11598. ;; S-QUOTE
  11599. L25B3:  RST     18H             ; GET-CHAR
  11600.         INC     HL              ; address next character (first in quotes)
  11601.         PUSH    HL              ; save start of quoted text.
  11602.         LD      BC,$0000        ; initialize length of string to zero.
  11603.         CALL    L250F           ; routine S-QUOTE-S
  11604.         JR      NZ,L25D9        ; forward to S-Q-PRMS if
  11605.  
  11606. ;; S-Q-AGAIN
  11607. L25BE:  CALL    L250F           ; routine S-QUOTE-S copies string until a
  11608.                                 ; quote is encountered
  11609.         JR      Z,L25BE         ; back to S-Q-AGAIN if two quotes WERE
  11610.                                 ; together.
  11611.  
  11612. ; but if just an isolated quote then that terminates the string.
  11613.  
  11614.         CALL    L2530           ; routine SYNTAX-Z
  11615.         JR      Z,L25D9         ; forward to S-Q-PRMS if checking syntax.
  11616.  
  11617.  
  11618.         RST     30H             ; BC-SPACES creates the space for true
  11619.                                 ; copy of string in workspace.
  11620.         POP     HL              ; re-fetch start of quoted text.
  11621.         PUSH    DE              ; save start in workspace.
  11622.  
  11623. ;; S-Q-COPY
  11624. L25CB:  LD      A,(HL)          ; fetch a character from source.
  11625.         INC     HL              ; advance source address.
  11626.         LD      (DE),A          ; place in destination.
  11627.         INC     DE              ; advance destination address.
  11628.         CP      $22             ; was it a '"' just copied ?
  11629.         JR      NZ,L25CB        ; back to S-Q-COPY to copy more if not
  11630.  
  11631.         LD      A,(HL)          ; fetch adjacent character from source.
  11632.         INC     HL              ; advance source address.
  11633.         CP      $22             ; is this '"' ? - i.e. two quotes together ?
  11634.         JR      Z,L25CB         ; to S-Q-COPY if so including just one of the
  11635.                                 ; pair of quotes.
  11636.  
  11637. ; proceed when terminating quote encountered.
  11638.  
  11639. ;; S-Q-PRMS
  11640. L25D9:  DEC     BC              ; decrease count by 1.
  11641.         POP     DE              ; restore start of string in workspace.
  11642.  
  11643. ;; S-STRING
  11644. L25DB:  LD      HL,$5C3B        ; Address FLAGS system variable.
  11645.         RES     6,(HL)          ; signal string result.
  11646.         BIT     7,(HL)          ; is syntax being checked.
  11647.         CALL    NZ,L2AB2        ; routine STK-STO-$ is called in runtime.
  11648.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11649.  
  11650. ; ---
  11651.  
  11652. ; ->
  11653. ;; S-BRACKET
  11654. L25E8:  RST     20H             ; NEXT-CHAR
  11655.         CALL    L24FB           ; routine SCANNING is called recursively.
  11656.         CP      $29             ; is it the closing ')' ?
  11657.         JP      NZ,L1C8A        ; jump back to REPORT-C if not
  11658.                                 ; 'Nonsense in BASIC'
  11659.  
  11660.         RST     20H             ; NEXT-CHAR
  11661.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11662.  
  11663. ; ---
  11664.  
  11665. ; ->
  11666. ;; S-FN
  11667. L25F5:  JP      L27BD           ; jump forward to S-FN-SBRN.
  11668.  
  11669. ; ---
  11670.  
  11671. ; ->
  11672. ;; S-RND
  11673. L25F8:  CALL    L2530           ; routine SYNTAX-Z
  11674.         JR      Z,L2625         ; forward to S-RND-END if checking syntax.
  11675.  
  11676.         LD      BC,($5C76)      ; fetch system variable SEED
  11677.         CALL    L2D2B           ; routine STACK-BC places on calculator stack
  11678.  
  11679.         RST     28H             ;; FP-CALC           ;s.
  11680.         DB    $A1             ;;stk-one            ;s,1.
  11681.         DB    $0F             ;;addition           ;s+1.
  11682.         DB    $34             ;;stk-data           ;
  11683.         DB    $37             ;;Exponent: $87,
  11684.                                 ;;Bytes: 1
  11685.         DB    $16             ;;(+00,+00,+00)      ;s+1,75.
  11686.         DB    $04             ;;multiply           ;(s+1)*75 = v
  11687.         DB    $34             ;;stk-data           ;v.
  11688.         DB    $80             ;;Bytes: 3
  11689.         DB    $41             ;;Exponent $91
  11690.         DB    $00,$00,$80     ;;(+00)              ;v,65537.
  11691.         DB    $32             ;;n-mod-m            ;remainder, result.
  11692.         DB    $02             ;;delete             ;remainder.
  11693.         DB    $A1             ;;stk-one            ;remainder, 1.
  11694.         DB    $03             ;;subtract           ;remainder - 1. = rnd
  11695.         DB    $31             ;;duplicate          ;rnd,rnd.
  11696.         DB    $38             ;;end-calc
  11697.  
  11698.         CALL    L2DA2           ; routine FP-TO-BC
  11699.         LD      ($5C76),BC      ; store in SEED for next starting point.
  11700.         LD      A,(HL)          ; fetch exponent
  11701.         AND     A               ; is it zero ?
  11702.         JR      Z,L2625         ; forward if so to S-RND-END
  11703.  
  11704.         SUB     $10             ; reduce exponent by 2^16
  11705.         LD      (HL),A          ; place back
  11706.  
  11707. ;; S-RND-END
  11708. L2625:  JR      L2630           ; forward to S-PI-END
  11709.  
  11710. ; ---
  11711.  
  11712. ; the number PI 3.14159...
  11713.  
  11714. ; ->
  11715. ;; S-PI
  11716. L2627:  CALL    L2530           ; routine SYNTAX-Z
  11717.         JR      Z,L2630         ; to S-PI-END if checking syntax.
  11718.  
  11719.         RST     28H             ;; FP-CALC
  11720.         DB    $A3             ;;stk-pi/2                          pi/2.
  11721.         DB    $38             ;;end-calc
  11722.  
  11723.         INC     (HL)            ; increment the exponent leaving pi
  11724.                                 ; on the calculator stack.
  11725.  
  11726. ;; S-PI-END
  11727. L2630:  RST     20H             ; NEXT-CHAR
  11728.         JP      L26C3           ; jump forward to S-NUMERIC
  11729.  
  11730. ; ---
  11731.  
  11732. ; ->
  11733. ;; S-INKEY$
  11734. L2634:  LD      BC,$105A        ; priority $10, operation code $1A ('read-in')
  11735.                                 ; +$40 for string result, numeric operand.
  11736.                                 ; set this up now in case we need to use the
  11737.                                 ; calculator.
  11738.         RST     20H             ; NEXT-CHAR
  11739.         CP      $23             ; '#' ?
  11740.         JP      Z,L270D         ; to S-PUSH-PO if so to use the calculator
  11741.                                 ; single operation
  11742.                                 ; to read from network/RS232 etc. .
  11743.  
  11744. ; else read a key from the keyboard.
  11745.  
  11746.         LD      HL,$5C3B        ; fetch FLAGS
  11747.         RES     6,(HL)          ; signal string result.
  11748.         BIT     7,(HL)          ; checking syntax ?
  11749.         JR      Z,L2665         ; forward to S-INK$-EN if so
  11750.  
  11751. ;===============================
  11752.                 IF BAS48_ONLY
  11753.                 CALL L028E
  11754.                 ELSE
  11755.                 JP L3B6C                ; Spectrum 128 patch
  11756.                 ENDIF
  11757. ;===============================
  11758.  
  11759. L2649:  LD      C,$00           ; the length of an empty string
  11760.         JR      NZ,L2660        ; to S-IK$-STK to store empty string if
  11761.                                 ; no key returned.
  11762.  
  11763.         CALL    L031E           ; routine K-TEST get main code in A
  11764.         JR      NC,L2660        ; to S-IK$-STK to stack null string if
  11765.                                 ; invalid
  11766.  
  11767.         DEC     D               ; D is expected to be FLAGS so set bit 3 $FF
  11768.                                 ; 'L' Mode so no keywords.
  11769.         LD      E,A             ; main key to A
  11770.                                 ; C is MODE 0 'KLC' from above still.
  11771.         CALL    L0333           ; routine K-DECODE
  11772. L2657:  PUSH    AF              ; save the code
  11773.         LD      BC,$0001        ; make room for one character
  11774.  
  11775.         RST     30H             ; BC-SPACES
  11776.         POP     AF              ; bring the code back
  11777.         LD      (DE),A          ; put the key in workspace
  11778.         LD      C,$01           ; set C length to one
  11779.  
  11780. ;; S-IK$-STK
  11781. L2660:  LD      B,$00           ; set high byte of length to zero
  11782.         CALL    L2AB2           ; routine STK-STO-$
  11783.  
  11784. ;; S-INK$-EN
  11785. L2665:  JP      L2712           ; to S-CONT-2            ===>
  11786.  
  11787. ; ---
  11788.  
  11789. ; ->
  11790. ;; S-SCREEN$
  11791. L2668:  CALL    L2522           ; routine S-2-COORD
  11792.         CALL    NZ,L2535        ; routine S-SCRN$-S
  11793.  
  11794.         RST     20H             ; NEXT-CHAR
  11795.         JP      L25DB           ; forward to S-STRING to stack result
  11796.  
  11797. ; ---
  11798.  
  11799. ; ->
  11800. ;; S-ATTR
  11801. L2672:  CALL    L2522           ; routine S-2-COORD
  11802.         CALL    NZ,L2580        ; routine S-ATTR-S
  11803.  
  11804.         RST     20H             ; NEXT-CHAR
  11805.         JR      L26C3           ; forward to S-NUMERIC
  11806.  
  11807. ; ---
  11808.  
  11809. ; ->
  11810. ;; S-POINT
  11811. L267B:  CALL    L2522           ; routine S-2-COORD
  11812.         CALL    NZ,L22CB        ; routine POINT-SUB
  11813.  
  11814.         RST     20H             ; NEXT-CHAR
  11815.         JR      L26C3           ; forward to S-NUMERIC
  11816.  
  11817. ; -----------------------------
  11818.  
  11819. ; ==> The branch was here if not in table.
  11820.  
  11821. ;; S-ALPHNUM
  11822. L2684:  CALL    L2C88           ; routine ALPHANUM checks if variable or
  11823.                                 ; a digit.
  11824.         JR      NC,L26DF        ; forward to S-NEGATE if not to consider
  11825.                                 ; a '-' character then functions.
  11826.  
  11827.         CP      $41             ; compare 'A'
  11828.         JR      NC,L26C9        ; forward to S-LETTER if alpha       ->
  11829.                                 ; else must have been numeric so continue
  11830.                                 ; into that routine.
  11831.  
  11832. ; This important routine is called during runtime and from LINE-SCAN
  11833. ; when a BASIC line is checked for syntax. It is this routine that
  11834. ; inserts, during syntax checking, the invisible floating point numbers
  11835. ; after the numeric expression. During runtime it just picks these
  11836. ; numbers up. It also handles BIN format numbers.
  11837.  
  11838. ; ->
  11839. ;; S-BIN
  11840. ;; S-DECIMAL
  11841. L268D:  CALL    L2530           ; routine SYNTAX-Z
  11842.         JR      NZ,L26B5        ; to S-STK-DEC in runtime
  11843.  
  11844. ; this route is taken when checking syntax.
  11845.  
  11846.         CALL    L2C9B           ; routine DEC-TO-FP to evaluate number
  11847.  
  11848.         RST     18H             ; GET-CHAR to fetch HL
  11849.         LD      BC,$0006        ; six locations required
  11850.         CALL    L1655           ; routine MAKE-ROOM
  11851.         INC     HL              ; to first new location
  11852.         LD      (HL),$0E        ; insert number marker
  11853.         INC     HL              ; address next
  11854.         EX      DE,HL           ; make DE destination.
  11855.         LD      HL,($5C65)      ; STKEND points to end of stack.
  11856.         LD      C,$05           ; result is five locations lower
  11857.         AND     A               ; prepare for true subtraction
  11858.         SBC     HL,BC           ; point to start of value.
  11859.         LD      ($5C65),HL      ; update STKEND as we are taking number.
  11860.         LDIR                    ; Copy five bytes to program location
  11861.         EX      DE,HL           ; transfer pointer to HL
  11862.         DEC     HL              ; adjust
  11863.         CALL    L0077           ; routine TEMP-PTR1 sets CH-ADD
  11864.         JR      L26C3           ; to S-NUMERIC to record nature of result
  11865.  
  11866. ; ---
  11867.  
  11868. ; branch here in runtime.
  11869.  
  11870. ;; S-STK-DEC
  11871. L26B5:  RST     18H             ; GET-CHAR positions HL at digit.
  11872.  
  11873. ;; S-SD-SKIP
  11874. L26B6:  INC     HL              ; advance pointer
  11875.         LD      A,(HL)          ; until we find
  11876.         CP      $0E             ; chr 14d - the number indicator
  11877.         JR      NZ,L26B6        ; to S-SD-SKIP until a match
  11878.                                 ; it has to be here.
  11879.  
  11880.         INC     HL              ; point to first byte of number
  11881.         CALL    L33B4           ; routine STACK-NUM stacks it
  11882.         LD      ($5C5D),HL      ; update system variable CH_ADD
  11883.  
  11884. ;; S-NUMERIC
  11885. L26C3:  SET     6,(IY+$01)      ; update FLAGS  - Signal numeric result
  11886.         JR      L26DD           ; forward to S-CONT-1               ===>
  11887.                                 ; actually S-CONT-2 is destination but why
  11888.                                 ; waste a byte on a jump when a JR will do.
  11889.                                 ; actually a JR L2712 can be used. Rats.
  11890.  
  11891. ; end of functions accessed from scanning functions table.
  11892.  
  11893. ; --------------------------
  11894. ; Scanning variable routines
  11895. ; --------------------------
  11896. ;
  11897. ;
  11898.  
  11899. ;; S-LETTER
  11900. L26C9:  CALL    L28B2           ; routine LOOK-VARS
  11901.         JP      C,L1C2E         ; jump back to REPORT-2 if not found
  11902.                                 ; 'Variable not found'
  11903.                                 ; but a variable is always 'found' if syntax
  11904.                                 ; is being checked.
  11905.  
  11906.         CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  11907.         LD      A,($5C3B)       ; fetch FLAGS value
  11908.         CP      $C0             ; compare 11000000
  11909.         JR      C,L26DD         ; step forward to S-CONT-1 if string  ===>
  11910.  
  11911.         INC     HL              ; advance pointer
  11912.         CALL    L33B4           ; routine STACK-NUM
  11913.  
  11914. ;; S-CONT-1
  11915. L26DD:  JR      L2712           ; forward to S-CONT-2                 ===>
  11916.  
  11917. ; ----------------------------------------
  11918. ; -> the scanning branch was here if not alphanumeric.
  11919. ; All the remaining functions will be evaluated by a single call to the
  11920. ; calculator. The correct priority for the operation has to be placed in
  11921. ; the B register and the operation code, calculator literal in the C register.
  11922. ; the operation code has bit 7 set if result is numeric and bit 6 is
  11923. ; set if operand is numeric. so
  11924. ; $C0 = numeric result, numeric operand.            e.g. 'sin'
  11925. ; $80 = numeric result, string operand.             e.g. 'code'
  11926. ; $40 = string result, numeric operand.             e.g. 'str$'
  11927. ; $00 = string result, string operand.              e.g. 'val$'
  11928.  
  11929. ;; S-NEGATE
  11930. L26DF:  LD      BC,$09DB        ; prepare priority 09, operation code $C0 +
  11931.                                 ; 'negate' ($1B) - bits 6 and 7 set for numeric
  11932.                                 ; result and numeric operand.
  11933.  
  11934.         CP      $2D             ; is it '-' ?
  11935.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11936.  
  11937.         LD      BC,$1018        ; prepare priority $10, operation code 'val$' -
  11938.                                 ; bits 6 and 7 reset for string result and
  11939.                                 ; string operand.
  11940.        
  11941.         CP      $AE             ; is it 'VAL$' ?
  11942.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11943.  
  11944.         SUB     $AF             ; subtract token 'CODE' value to reduce
  11945.                                 ; functions 'CODE' to 'NOT' although the
  11946.                                 ; upper range is, as yet, unchecked.
  11947.                                 ; valid range would be $00 - $14.
  11948.  
  11949.         JP      C,L1C8A         ; jump back to REPORT-C with anything else
  11950.                                 ; 'Nonsense in BASIC'
  11951.  
  11952.         LD      BC,$04F0        ; prepare priority $04, operation $C0 +
  11953.                                 ; 'not' ($30)
  11954.  
  11955.         CP      $14             ; is it 'NOT'
  11956.         JR      Z,L270D         ; forward to S-PUSH-PO if so
  11957.  
  11958.         JP      NC,L1C8A        ; to REPORT-C if higher
  11959.                                 ; 'Nonsense in BASIC'
  11960.  
  11961.         LD      B,$10           ; priority $10 for all the rest
  11962.         ADD     A,$DC           ; make range $DC - $EF
  11963.                                 ; $C0 + 'code'($1C) thru 'chr$' ($2F)
  11964.  
  11965.         LD      C,A             ; transfer 'function' to C
  11966.         CP      $DF             ; is it 'sin' ?
  11967.         JR      NC,L2707        ; forward to S-NO-TO-$  with 'sin' through
  11968.                                 ; 'chr$' as operand is numeric.
  11969.  
  11970. ; all the rest 'cos' through 'chr$' give a numeric result except 'str$'
  11971. ; and 'chr$'.
  11972.  
  11973.         RES     6,C             ; signal string operand for 'code', 'val' and
  11974.                                 ; 'len'.
  11975.  
  11976. ;; S-NO-TO-$
  11977. L2707:  CP      $EE             ; compare 'str$'
  11978.         JR      C,L270D         ; forward to S-PUSH-PO if lower as result
  11979.                                 ; is numeric.
  11980.  
  11981.         RES     7,C             ; reset bit 7 of op code for 'str$', 'chr$'
  11982.                                 ; as result is string.
  11983.  
  11984. ; >> This is where they were all headed for.
  11985.  
  11986. ;; S-PUSH-PO
  11987. L270D:  PUSH    BC              ; push the priority and calculator operation
  11988.                                 ; code.
  11989.  
  11990.         RST     20H             ; NEXT-CHAR
  11991.         JP      L24FF           ; jump back to S-LOOP-1 to go round the loop
  11992.                                 ; again with the next character.
  11993.  
  11994. ; --------------------------------
  11995.  
  11996. ; ===>  there were many branches forward to here
  11997.  
  11998. ;; S-CONT-2
  11999. L2712:  RST     18H             ; GET-CHAR
  12000.  
  12001. ;; S-CONT-3
  12002. L2713:  CP      $28             ; is it '(' ?
  12003.         JR      NZ,L2723        ; forward to S-OPERTR if not    >
  12004.  
  12005.         BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  12006.         JR      NZ,L2734        ; forward to S-LOOP if numeric to evaluate  >
  12007.  
  12008. ; if a string preceded '(' then slice it.
  12009.  
  12010.         CALL    L2A52           ; routine SLICING
  12011.  
  12012.         RST     20H             ; NEXT-CHAR
  12013.         JR      L2713           ; back to S-CONT-3
  12014.  
  12015. ; ---------------------------
  12016.  
  12017. ; the branch was here when possibility of an operator '(' has been excluded.
  12018.  
  12019. ;; S-OPERTR
  12020. L2723:  LD      B,$00           ; prepare to add
  12021.         LD      C,A             ; possible operator to C
  12022.         LD      HL,L2795        ; Address: $2795 - tbl-of-ops
  12023.         CALL    L16DC           ; routine INDEXER
  12024.         JR      NC,L2734        ; forward to S-LOOP if not in table
  12025.  
  12026. ; but if found in table the priority has to be looked up.
  12027.  
  12028.         LD      C,(HL)          ; operation code to C ( B is still zero )
  12029.         LD      HL,L27B0 - $C3  ; $26ED is base of table
  12030.         ADD     HL,BC           ; index into table.
  12031.         LD      B,(HL)          ; priority to B.
  12032.  
  12033. ; ------------------
  12034. ; Scanning main loop
  12035. ; ------------------
  12036. ; the juggling act
  12037.  
  12038. ;; S-LOOP
  12039. L2734:  POP     DE              ; fetch last priority and operation
  12040.         LD      A,D             ; priority to A
  12041.         CP      B               ; compare with this one
  12042.         JR      C,L2773         ; forward to S-TIGHTER to execute the
  12043.                                 ; last operation before this one as it has
  12044.                                 ; higher priority.
  12045.  
  12046. ; the last priority was greater or equal this one.
  12047.  
  12048.         AND     A               ; if it is zero then so is this
  12049.         JP      Z,L0018         ; jump to exit via get-char pointing at
  12050.                                 ; next character.
  12051.                                 ; This may be the character after the
  12052.                                 ; expression or, if exiting a recursive call,
  12053.                                 ; the next part of the expression to be
  12054.                                 ; evaluated.
  12055.  
  12056.         PUSH    BC              ; save current priority/operation
  12057.                                 ; as it has lower precedence than the one
  12058.                                 ; now in DE.
  12059.  
  12060. ; the 'USR' function is special in that it is overloaded to give two types
  12061. ; of result.
  12062.  
  12063.         LD      HL,$5C3B        ; address FLAGS
  12064.         LD      A,E             ; new operation to A register
  12065.         CP      $ED             ; is it $C0 + 'usr-no' ($2D)  ?
  12066.         JR      NZ,L274C        ; forward to S-STK-LST if not
  12067.  
  12068.         BIT     6,(HL)          ; string result expected ?
  12069.                                 ; (from the lower priority operand we've
  12070.                                 ; just pushed on stack )
  12071.         JR      NZ,L274C        ; forward to S-STK-LST if numeric
  12072.                                 ; as operand bits match.
  12073.  
  12074.         LD      E,$99           ; reset bit 6 and substitute $19 'usr-$'
  12075.                                 ; for string operand.
  12076.  
  12077. ;; S-STK-LST
  12078. L274C:  PUSH    DE              ; now stack this priority/operation
  12079.         CALL    L2530           ; routine SYNTAX-Z
  12080.         JR      Z,L275B         ; forward to S-SYNTEST if checking syntax.
  12081.  
  12082.         LD      A,E             ; fetch the operation code
  12083.         AND     $3F             ; mask off the result/operand bits to leave
  12084.                                 ; a calculator literal.
  12085.         LD      B,A             ; transfer to B register
  12086.  
  12087. ; now use the calculator to perform the single operation - operand is on
  12088. ; the calculator stack.
  12089. ; Note. although the calculator is performing a single operation most
  12090. ; functions e.g. TAN are written using other functions and literals and
  12091. ; these in turn are written using further strings of calculator literals so
  12092. ; another level of magical recursion joins the juggling act for a while
  12093. ; as the calculator too is calling itself.
  12094.  
  12095.         RST     28H             ;; FP-CALC
  12096.         DB    $3B             ;;fp-calc-2
  12097. L2758:  DB    $38             ;;end-calc
  12098.  
  12099.         JR      L2764           ; forward to S-RUNTEST
  12100.  
  12101. ; ---
  12102.  
  12103. ; the branch was here if checking syntax only.
  12104.  
  12105. ;; S-SYNTEST
  12106. L275B:  LD      A,E             ; fetch the operation code to accumulator
  12107.         XOR     (IY+$01)        ; compare with bits of FLAGS
  12108.         AND     $40             ; bit 6 will be zero now if operand
  12109.                                 ; matched expected result.
  12110.  
  12111. ;; S-RPORT-C2
  12112. L2761:  JP      NZ,L1C8A        ; to REPORT-C if mismatch
  12113.                                 ; 'Nonsense in BASIC'
  12114.                                 ; else continue to set flags for next
  12115.  
  12116. ; the branch is to here in runtime after a successful operation.
  12117.  
  12118. ;; S-RUNTEST
  12119. L2764:  POP     DE              ; fetch the last operation from stack
  12120.         LD      HL,$5C3B        ; address FLAGS
  12121.         SET     6,(HL)          ; set default to numeric result in FLAGS
  12122.         BIT     7,E             ; test the operational result
  12123.         JR      NZ,L2770        ; forward to S-LOOPEND if numeric
  12124.  
  12125.         RES     6,(HL)          ; reset bit 6 of FLAGS to show string result.
  12126.  
  12127. ;; S-LOOPEND
  12128. L2770:  POP     BC              ; fetch the previous priority/operation
  12129.         JR      L2734           ; back to S-LOOP to perform these
  12130.  
  12131. ; ---
  12132.  
  12133. ; the branch was here when a stacked priority/operator had higher priority
  12134. ; than the current one.
  12135.  
  12136. ;; S-TIGHTER
  12137. L2773:  PUSH    DE              ; save high priority op on stack again
  12138.         LD      A,C             ; fetch lower priority operation code
  12139.         BIT     6,(IY+$01)      ; test FLAGS - Numeric or string result ?
  12140.         JR      NZ,L2790        ; forward to S-NEXT if numeric result
  12141.  
  12142. ; if this is lower priority yet has string then must be a comparison.
  12143. ; Since these can only be evaluated in context and were defaulted to
  12144. ; numeric in operator look up they must be changed to string equivalents.
  12145.  
  12146.         AND     $3F             ; mask to give true calculator literal
  12147.         ADD     A,$08           ; augment numeric literals to string
  12148.                                 ; equivalents.
  12149.                                 ; 'no-&-no'  => 'str-&-no'
  12150.                                 ; 'no-l-eql' => 'str-l-eql'
  12151.                                 ; 'no-gr-eq' => 'str-gr-eq'
  12152.                                 ; 'nos-neql' => 'strs-neql'
  12153.                                 ; 'no-grtr'  => 'str-grtr'
  12154.                                 ; 'no-less'  => 'str-less'
  12155.                                 ; 'nos-eql'  => 'strs-eql'
  12156.                                 ; 'addition' => 'strs-add'
  12157.         LD      C,A             ; put modified comparison operator back
  12158.         CP      $10             ; is it now 'str-&-no' ?
  12159.         JR      NZ,L2788        ; forward to S-NOT-AND  if not.
  12160.  
  12161.         SET     6,C             ; set numeric operand bit
  12162.         JR      L2790           ; forward to S-NEXT
  12163.  
  12164. ; ---
  12165.  
  12166. ;; S-NOT-AND
  12167. L2788:  JR      C,L2761         ; back to S-RPORT-C2 if less
  12168.                                 ; 'Nonsense in BASIC'.
  12169.                                 ; e.g. a$ * b$
  12170.  
  12171.         CP      $17             ; is it 'strs-add' ?
  12172.         JR      Z,L2790         ; forward to to S-NEXT if so
  12173.                                 ; (bit 6 and 7 are reset)
  12174.  
  12175.         SET     7,C             ; set numeric (Boolean) result for all others
  12176.  
  12177. ;; S-NEXT
  12178. L2790:  PUSH    BC              ; now save this priority/operation on stack
  12179.  
  12180.         RST     20H             ; NEXT-CHAR
  12181.         JP      L24FF           ; jump back to S-LOOP-1
  12182.  
  12183. ; ------------------
  12184. ; Table of operators
  12185. ; ------------------
  12186. ; This table is used to look up the calculator literals associated with
  12187. ; the operator character. The thirteen calculator operations $03 - $0F
  12188. ; have bits 6 and 7 set to signify a numeric result.
  12189. ; Some of these codes and bits may be altered later if the context suggests
  12190. ; a string comparison or operation.
  12191. ; that is '+', '=', '>', '<', '<=', '>=' or '<>'.
  12192.  
  12193. ;; tbl-of-ops
  12194. L2795:  DB    '+', $CF        ;        $C0 + 'addition'
  12195.         DB    '-', $C3        ;        $C0 + 'subtract'
  12196.         DB    '*', $C4        ;        $C0 + 'multiply'
  12197.         DB    '/', $C5        ;        $C0 + 'division'
  12198.         DB    '^', $C6        ;        $C0 + 'to-power'
  12199.         DB    '=', $CE        ;        $C0 + 'nos-eql'
  12200.         DB    '>', $CC        ;        $C0 + 'no-grtr'
  12201.         DB    '<', $CD        ;        $C0 + 'no-less'
  12202.  
  12203.         DB    $C7, $C9        ; '<='   $C0 + 'no-l-eql'
  12204.         DB    $C8, $CA        ; '>='   $C0 + 'no-gr-eql'
  12205.         DB    $C9, $CB        ; '<>'   $C0 + 'nos-neql'
  12206.         DB    $C5, $C7        ; 'OR'   $C0 + 'or'
  12207.         DB    $C6, $C8        ; 'AND'  $C0 + 'no-&-no'
  12208.  
  12209.         DB    $00             ; zero end-marker.
  12210.  
  12211.  
  12212. ; -------------------
  12213. ; Table of priorities
  12214. ; -------------------
  12215. ; This table is indexed with the operation code obtained from the above
  12216. ; table $C3 - $CF to obtain the priority for the respective operation.
  12217.  
  12218. ;; tbl-priors
  12219. L27B0:  DB    $06             ; '-'   opcode $C3
  12220.         DB    $08             ; '*'   opcode $C4
  12221.         DB    $08             ; '/'   opcode $C5
  12222.         DB    $0A             ; '^'   opcode $C6
  12223.         DB    $02             ; 'OR'  opcode $C7
  12224.         DB    $03             ; 'AND' opcode $C8
  12225.         DB    $05             ; '<='  opcode $C9
  12226.         DB    $05             ; '>='  opcode $CA
  12227.         DB    $05             ; '<>'  opcode $CB
  12228.         DB    $05             ; '>'   opcode $CC
  12229.         DB    $05             ; '<'   opcode $CD
  12230.         DB    $05             ; '='   opcode $CE
  12231.         DB    $06             ; '+'   opcode $CF
  12232.  
  12233. ; ----------------------
  12234. ; Scanning function (FN)
  12235. ; ----------------------
  12236. ; This routine deals with user-defined functions.
  12237. ; The definition can be anywhere in the program area but these are best
  12238. ; placed near the start of the program as we shall see.
  12239. ; The evaluation process is quite complex as the Spectrum has to parse two
  12240. ; statements at the same time. Syntax of both has been checked previously
  12241. ; and hidden locations have been created immediately after each argument
  12242. ; of the DEF FN statement. Each of the arguments of the FN function is
  12243. ; evaluated by SCANNING and placed in the hidden locations. Then the
  12244. ; expression to the right of the DEF FN '=' is evaluated by SCANNING and for
  12245. ; any variables encountered, a search is made in the DEF FN variable list
  12246. ; in the program area before searching in the normal variables area.
  12247. ;
  12248. ; Recursion is not allowed: i.e. the definition of a function should not use
  12249. ; the same function, either directly or indirectly ( through another function).
  12250. ; You'll normally get error 4, ('Out of memory'), although sometimes the sytem
  12251. ; will crash. - Vickers, Pitman 1984.
  12252. ;
  12253. ; As the definition is just an expression, there would seem to be no means
  12254. ; of breaking out of such recursion.
  12255. ; However, by the clever use of string expressions and VAL, such recursion is
  12256. ; possible.
  12257. ; e.g. DEF FN a(n) = VAL "n+FN a(n-1)+0" ((n<1) * 10 + 1 TO )
  12258. ; will evaluate the full 11-character expression for all values where n is
  12259. ; greater than zero but just the 11th character, "0", when n drops to zero
  12260. ; thereby ending the recursion producing the correct result.
  12261. ; Recursive string functions are possible using VAL$ instead of VAL and the
  12262. ; null string as the final addend.
  12263. ; - from a turn of the century newsgroup discussion initiated by Mike Wynne.
  12264.  
  12265. ;; S-FN-SBRN
  12266. L27BD:  CALL    L2530           ; routine SYNTAX-Z
  12267.         JR      NZ,L27F7        ; forward to SF-RUN in runtime
  12268.  
  12269.  
  12270.         RST     20H             ; NEXT-CHAR
  12271.         CALL    L2C8D           ; routine ALPHA check for letters A-Z a-z
  12272.         JP      NC,L1C8A        ; jump back to REPORT-C if not
  12273.                                 ; 'Nonsense in BASIC'
  12274.  
  12275.  
  12276.         RST     20H             ; NEXT-CHAR
  12277.         CP      $24             ; is it '$' ?
  12278.         PUSH    AF              ; save character and flags
  12279.         JR      NZ,L27D0        ; forward to SF-BRKT-1 with numeric function
  12280.  
  12281.  
  12282.         RST     20H             ; NEXT-CHAR
  12283.  
  12284. ;; SF-BRKT-1
  12285. L27D0:  CP      $28             ; is '(' ?
  12286.         JR      NZ,L27E6        ; forward to SF-RPRT-C if not
  12287.                                 ; 'Nonsense in BASIC'
  12288.  
  12289.  
  12290.         RST     20H             ; NEXT-CHAR
  12291.         CP      $29             ; is it ')' ?
  12292.         JR      Z,L27E9         ; forward to SF-FLAG-6 if no arguments.
  12293.  
  12294. ;; SF-ARGMTS
  12295. L27D9:  CALL    L24FB           ; routine SCANNING checks each argument
  12296.                                 ; which may be an expression.
  12297.  
  12298.         RST     18H             ; GET-CHAR
  12299.         CP      $2C             ; is it a ',' ?
  12300.         JR      NZ,L27E4        ; forward if not to SF-BRKT-2 to test bracket
  12301.  
  12302.  
  12303.         RST     20H             ; NEXT-CHAR if a comma was found
  12304.         JR      L27D9           ; back to SF-ARGMTS to parse all arguments.
  12305.  
  12306. ; ---
  12307.  
  12308. ;; SF-BRKT-2
  12309. L27E4:  CP      $29             ; is character the closing ')' ?
  12310.  
  12311. ;; SF-RPRT-C
  12312. L27E6:  JP      NZ,L1C8A        ; jump to REPORT-C
  12313.                                 ; 'Nonsense in BASIC'
  12314.  
  12315. ; at this point any optional arguments have had their syntax checked.
  12316.  
  12317. ;; SF-FLAG-6
  12318. L27E9:  RST     20H             ; NEXT-CHAR
  12319.         LD      HL,$5C3B        ; address system variable FLAGS
  12320.         RES     6,(HL)          ; signal string result
  12321.         POP     AF              ; restore test against '$'.
  12322.         JR      Z,L27F4         ; forward to SF-SYN-EN if string function.
  12323.  
  12324.         SET     6,(HL)          ; signal numeric result
  12325.  
  12326. ;; SF-SYN-EN
  12327. L27F4:  JP      L2712           ; jump back to S-CONT-2 to continue scanning.
  12328.  
  12329. ; ---
  12330.  
  12331. ; the branch was here in runtime.
  12332.  
  12333. ;; SF-RUN
  12334. L27F7:  RST     20H             ; NEXT-CHAR fetches name
  12335.         AND     $DF             ; AND 11101111 - reset bit 5 - upper-case.
  12336.         LD      B,A             ; save in B
  12337.  
  12338.         RST     20H             ; NEXT-CHAR
  12339.         SUB     $24             ; subtract '$'
  12340.         LD      C,A             ; save result in C
  12341.         JR      NZ,L2802        ; forward if not '$' to SF-ARGMT1
  12342.  
  12343.         RST     20H             ; NEXT-CHAR advances to bracket
  12344.  
  12345. ;; SF-ARGMT1
  12346. L2802:  RST     20H             ; NEXT-CHAR advances to start of argument
  12347.         PUSH    HL              ; save address
  12348.         LD      HL,($5C53)      ; fetch start of program area from PROG
  12349.         DEC     HL              ; the search starting point is the previous
  12350.                                 ; location.
  12351.  
  12352. ;; SF-FND-DF
  12353. L2808:  LD      DE,$00CE        ; search is for token 'DEF FN' in E,
  12354.                                 ; statement count in D.
  12355.         PUSH    BC              ; save C the string test, and B the letter.
  12356.         CALL    L1D86           ; routine LOOK-PROG will search for token.
  12357.         POP     BC              ; restore BC.
  12358.         JR      NC,L2814        ; forward to SF-CP-DEF if a match was found.
  12359.  
  12360.  
  12361. ;; REPORT-P
  12362. L2812:  RST     08H             ; ERROR-1
  12363.         DB    $18             ; Error Report: FN without DEF
  12364.  
  12365. ;; SF-CP-DEF
  12366. L2814:  PUSH    HL              ; save address of DEF FN
  12367.         CALL    L28AB           ; routine FN-SKPOVR skips over white-space etc.
  12368.                                 ; without disturbing CH-ADD.
  12369.         AND     $DF             ; make fetched character upper-case.
  12370.         CP      B               ; compare with FN name
  12371.         JR      NZ,L2825        ; forward to SF-NOT-FD if no match.
  12372.  
  12373. ; the letters match so test the type.
  12374.  
  12375.         CALL    L28AB           ; routine FN-SKPOVR skips white-space
  12376.         SUB     $24             ; subtract '$' from fetched character
  12377.         CP      C               ; compare with saved result of same operation
  12378.                                 ; on FN name.
  12379.         JR      Z,L2831         ; forward to SF-VALUES with a match.
  12380.  
  12381. ; the letters matched but one was string and the other numeric.
  12382.  
  12383. ;; SF-NOT-FD
  12384. L2825:  POP     HL              ; restore search point.
  12385.         DEC     HL              ; make location before
  12386.         LD      DE,$0200        ; the search is to be for the end of the
  12387.                                 ; current definition - 2 statements forward.
  12388.         PUSH    BC              ; save the letter/type
  12389.         CALL    L198B           ; routine EACH-STMT steps past rejected
  12390.                                 ; definition.
  12391.         POP     BC              ; restore letter/type
  12392.         JR      L2808           ; back to SF-FND-DF to continue search
  12393.  
  12394. ; ---
  12395.  
  12396. ; Success!
  12397. ; the branch was here with matching letter and numeric/string type.
  12398.  
  12399. ;; SF-VALUES
  12400. L2831:  AND     A               ; test A ( will be zero if string '$' - '$' )
  12401.  
  12402.         CALL    Z,L28AB         ; routine FN-SKPOVR advances HL past '$'.
  12403.  
  12404.         POP     DE              ; discard pointer to 'DEF FN'.
  12405.         POP     DE              ; restore pointer to first FN argument.
  12406.         LD      ($5C5D),DE      ; save in CH_ADD
  12407.  
  12408.         CALL    L28AB           ; routine FN-SKPOVR advances HL past '('
  12409.         PUSH    HL              ; save start address in DEF FN  ***
  12410.         CP      $29             ; is character a ')' ?
  12411.         JR      Z,L2885         ; forward to SF-R-BR-2 if no arguments.
  12412.  
  12413. ;; SF-ARG-LP
  12414. L2843:  INC     HL              ; point to next character.
  12415.         LD      A,(HL)          ; fetch it.
  12416.         CP      $0E             ; is it the number marker
  12417.         LD      D,$40           ; signal numeric in D.
  12418.         JR      Z,L2852         ; forward to SF-ARG-VL if numeric.
  12419.  
  12420.         DEC     HL              ; back to letter
  12421.         CALL    L28AB           ; routine FN-SKPOVR skips any white-space
  12422.         INC     HL              ; advance past the expected '$' to
  12423.                                 ; the 'hidden' marker.
  12424.         LD      D,$00           ; signal string.
  12425.  
  12426. ;; SF-ARG-VL
  12427. L2852:  INC     HL              ; now address first of 5-byte location.
  12428.         PUSH    HL              ; save address in DEF FN statement
  12429.         PUSH    DE              ; save D - result type
  12430.  
  12431.         CALL    L24FB           ; routine SCANNING evaluates expression in
  12432.                                 ; the FN statement setting FLAGS and leaving
  12433.                                 ; result as last value on calculator stack.
  12434.  
  12435.         POP     AF              ; restore saved result type to A
  12436.  
  12437.         XOR     (IY+$01)        ; xor with FLAGS
  12438.         AND     $40             ; and with 01000000 to test bit 6
  12439.         JR      NZ,L288B        ; forward to REPORT-Q if type mismatch.
  12440.                                 ; 'Parameter error'
  12441.  
  12442.         POP     HL              ; pop the start address in DEF FN statement
  12443.         EX      DE,HL           ; transfer to DE ?? pop straight into de ?
  12444.  
  12445.         LD      HL,($5C65)      ; set HL to STKEND location after value
  12446.         LD      BC,$0005        ; five bytes to move
  12447.         SBC     HL,BC           ; decrease HL by 5 to point to start.
  12448.         LD      ($5C65),HL      ; set STKEND 'removing' value from stack.
  12449.  
  12450.         LDIR                    ; copy value into DEF FN statement
  12451.         EX      DE,HL           ; set HL to location after value in DEF FN
  12452.         DEC     HL              ; step back one
  12453.         CALL    L28AB           ; routine FN-SKPOVR gets next valid character
  12454.         CP      $29             ; is it ')' end of arguments ?
  12455.         JR      Z,L2885         ; forward to SF-R-BR-2 if so.
  12456.  
  12457. ; a comma separator has been encountered in the DEF FN argument list.
  12458.  
  12459.         PUSH    HL              ; save position in DEF FN statement
  12460.  
  12461.         RST     18H             ; GET-CHAR from FN statement
  12462.         CP      $2C             ; is it ',' ?
  12463.         JR      NZ,L288B        ; forward to REPORT-Q if not
  12464.                                 ; 'Parameter error'
  12465.  
  12466.         RST     20H             ; NEXT-CHAR in FN statement advances to next
  12467.                                 ; argument.
  12468.  
  12469.         POP     HL              ; restore DEF FN pointer
  12470.         CALL    L28AB           ; routine FN-SKPOVR advances to corresponding
  12471.                                 ; argument.
  12472.  
  12473.         JR      L2843           ; back to SF-ARG-LP looping until all
  12474.                                 ; arguments are passed into the DEF FN
  12475.                                 ; hidden locations.
  12476.  
  12477. ; ---
  12478.  
  12479. ; the branch was here when all arguments passed.
  12480.  
  12481. ;; SF-R-BR-2
  12482. L2885:  PUSH    HL              ; save location of ')' in DEF FN
  12483.  
  12484.         RST     18H             ; GET-CHAR gets next character in FN
  12485.         CP      $29             ; is it a ')' also ?
  12486.         JR      Z,L288D         ; forward to SF-VALUE if so.
  12487.  
  12488.  
  12489. ;; REPORT-Q
  12490. L288B:  RST     08H             ; ERROR-1
  12491.         DB    $19             ; Error Report: Parameter error
  12492.  
  12493. ;; SF-VALUE
  12494. L288D:  POP     DE              ; location of ')' in DEF FN to DE.
  12495.         EX      DE,HL           ; now to HL, FN ')' pointer to DE.
  12496.         LD      ($5C5D),HL      ; initialize CH_ADD to this value.
  12497.  
  12498. ; At this point the start of the DEF FN argument list is on the machine stack.
  12499. ; We also have to consider that this defined function may form part of the
  12500. ; definition of another defined function (though not itself).
  12501. ; As this defined function may be part of a hierarchy of defined functions
  12502. ; currently being evaluated by recursive calls to SCANNING, then we have to
  12503. ; preserve the original value of DEFADD and not assume that it is zero.
  12504.  
  12505.         LD      HL,($5C0B)      ; get original DEFADD address
  12506.         EX      (SP),HL         ; swap with DEF FN address on stack ***
  12507.         LD      ($5C0B),HL      ; set DEFADD to point to this argument list
  12508.                                 ; during scanning.
  12509.  
  12510.         PUSH    DE              ; save FN ')' pointer.
  12511.  
  12512.         RST     20H             ; NEXT-CHAR advances past ')' in define
  12513.  
  12514.         RST     20H             ; NEXT-CHAR advances past '=' to expression
  12515.  
  12516.         CALL    L24FB           ; routine SCANNING evaluates but searches
  12517.                                 ; initially for variables at DEFADD
  12518.  
  12519.         POP     HL              ; pop the FN ')' pointer
  12520.         LD      ($5C5D),HL      ; set CH_ADD to this
  12521.         POP     HL              ; pop the original DEFADD value
  12522.         LD      ($5C0B),HL      ; and re-insert into DEFADD system variable.
  12523.  
  12524.         RST     20H             ; NEXT-CHAR advances to character after ')'
  12525.         JP      L2712           ; to S-CONT-2 - to continue current
  12526.                                 ; invocation of scanning
  12527.  
  12528. ; --------------------
  12529. ; Used to parse DEF FN
  12530. ; --------------------
  12531. ; e.g. DEF FN     s $ ( x )     =  b     $ (  TO  x  ) : REM exaggerated
  12532. ;
  12533. ; This routine is used 10 times to advance along a DEF FN statement
  12534. ; skipping spaces and colour control codes. It is similar to NEXT-CHAR
  12535. ; which is, at the same time, used to skip along the corresponding FN function
  12536. ; except the latter has to deal with AT and TAB characters in string
  12537. ; expressions. These cannot occur in a program area so this routine is
  12538. ; simpler as both colour controls and their parameters are less than space.
  12539.  
  12540. ;; FN-SKPOVR
  12541. L28AB:  INC     HL              ; increase pointer
  12542.         LD      A,(HL)          ; fetch addressed character
  12543.         CP      $21             ; compare with space + 1
  12544.         JR      C,L28AB         ; back to FN-SKPOVR if less
  12545.  
  12546.         RET                     ; return pointing to a valid character.
  12547.  
  12548. ; ---------
  12549. ; LOOK-VARS
  12550. ; ---------
  12551. ;
  12552. ;
  12553.  
  12554. ;; LOOK-VARS
  12555. L28B2:  SET     6,(IY+$01)      ; update FLAGS - presume numeric result
  12556.  
  12557.         RST     18H             ; GET-CHAR
  12558.         CALL    L2C8D           ; routine ALPHA tests for A-Za-z
  12559.         JP      NC,L1C8A        ; jump to REPORT-C if not.
  12560.                                 ; 'Nonsense in BASIC'
  12561.  
  12562.         PUSH    HL              ; save pointer to first letter       ^1
  12563.         AND     $1F             ; mask lower bits, 1 - 26 decimal     000xxxxx
  12564.         LD      C,A             ; store in C.
  12565.  
  12566.         RST     20H             ; NEXT-CHAR
  12567.         PUSH    HL              ; save pointer to second character   ^2
  12568.         CP      $28             ; is it '(' - an array ?
  12569.         JR      Z,L28EF         ; forward to V-RUN/SYN if so.
  12570.  
  12571.         SET     6,C             ; set 6 signaling string if solitary  010
  12572.         CP      $24             ; is character a '$' ?
  12573.         JR      Z,L28DE         ; forward to V-STR-VAR
  12574.  
  12575.         SET     5,C             ; signal numeric                       011
  12576.         CALL    L2C88           ; routine ALPHANUM sets carry if second
  12577.                                 ; character is alphanumeric.
  12578.         JR      NC,L28E3        ; forward to V-TEST-FN if just one character
  12579.  
  12580. ; it is more than one character but re-test current character so that 6 reset
  12581. ; Note. this is a rare lack of elegance. Bit 6 could be reset once before
  12582. ; entering the loop. Another puzzle is that this loop renders the similar
  12583. ; loop at V-PASS redundant.
  12584.  
  12585. ;; V-CHAR
  12586. L28D4:  CALL    L2C88           ; routine ALPHANUM
  12587.         JR      NC,L28EF        ; to V-RUN/SYN when no more
  12588.  
  12589.         RES     6,C             ; make long named type                 001
  12590.  
  12591.         RST     20H             ; NEXT-CHAR
  12592.         JR      L28D4           ; loop back to V-CHAR
  12593.  
  12594. ; ---
  12595.  
  12596.  
  12597. ;; V-STR-VAR
  12598. L28DE:  RST     20H             ; NEXT-CHAR advances past '$'
  12599.         RES     6,(IY+$01)      ; update FLAGS - signal string result.
  12600.  
  12601. ;; V-TEST-FN
  12602. L28E3:  LD      A,($5C0C)       ; load A with DEFADD_hi
  12603.         AND     A               ; and test for zero.
  12604.         JR      Z,L28EF         ; forward to V-RUN/SYN if a defined function
  12605.                                 ; is not being evaluated.
  12606.  
  12607. ; Note.
  12608.  
  12609.         CALL    L2530           ; routine SYNTAX-Z
  12610.         JP      NZ,L2951        ; JUMP to STK-F-ARG in runtime and then
  12611.                                 ; back to this point if no variable found.
  12612.  
  12613. ;; V-RUN/SYN
  12614. L28EF:  LD      B,C             ; save flags in B
  12615.         CALL    L2530           ; routine SYNTAX-Z
  12616.         JR      NZ,L28FD        ; to V-RUN to look for the variable in runtime
  12617.  
  12618. ; if checking syntax the letter is not returned
  12619.  
  12620.         LD      A,C             ; copy letter/flags to A
  12621.         AND     $E0             ; and with 11100000 to get rid of the letter
  12622.         SET     7,A             ; use spare bit to signal checking syntax.
  12623.         LD      C,A             ; and transfer to C.
  12624.         JR      L2934           ; forward to V-SYNTAX
  12625.  
  12626. ; ---
  12627.  
  12628. ; but in runtime search for the variable.
  12629.  
  12630. ;; V-RUN
  12631. L28FD:  LD      HL,($5C4B)      ; set HL to start of variables from VARS
  12632.  
  12633. ;; V-EACH
  12634. L2900:  LD      A,(HL)          ; get first character
  12635.         AND     $7F             ; and with 01111111
  12636.                                 ; ignoring bit 7 which distinguishes
  12637.                                 ; arrays or for/next variables.
  12638.  
  12639.         JR      Z,L2932         ; to V-80-BYTE if zero as must be 10000000
  12640.                                 ; the variables end-marker.
  12641.  
  12642.         CP      C               ; compare with supplied value.
  12643.         JR      NZ,L292A        ; forward to V-NEXT if no match.
  12644.  
  12645.         RLA                     ; destructively test
  12646.         ADD     A,A             ; bits 5 and 6 of A
  12647.                                 ; jumping if bit 5 reset or 6 set
  12648.  
  12649.         JP      P,L293F         ; to V-FOUND-2  strings and arrays
  12650.  
  12651.         JR      C,L293F         ; to V-FOUND-2  simple and for next
  12652.  
  12653. ; leaving long name variables.
  12654.  
  12655.         POP     DE              ; pop pointer to 2nd. char
  12656.         PUSH    DE              ; save it again
  12657.         PUSH    HL              ; save variable first character pointer
  12658.  
  12659. ;; V-MATCHES
  12660. L2912:  INC     HL              ; address next character in vars area
  12661.  
  12662. ;; V-SPACES
  12663. L2913:  LD      A,(DE)          ; pick up letter from prog area
  12664.         INC     DE              ; and advance address
  12665.         CP      $20             ; is it a space
  12666.         JR      Z,L2913         ; back to V-SPACES until non-space
  12667.  
  12668.         OR      $20             ; convert to range 1 - 26.
  12669.         CP      (HL)            ; compare with addressed variables character
  12670.         JR      Z,L2912         ; loop back to V-MATCHES if a match on an
  12671.                                 ; intermediate letter.
  12672.  
  12673.         OR      $80             ; now set bit 7 as last character of long
  12674.                                 ; names are inverted.
  12675.         CP      (HL)            ; compare again
  12676.         JR      NZ,L2929        ; forward to V-GET-PTR if no match
  12677.  
  12678. ; but if they match check that this is also last letter in prog area
  12679.  
  12680.         LD      A,(DE)          ; fetch next character
  12681.         CALL    L2C88           ; routine ALPHANUM sets carry if not alphanum
  12682.         JR      NC,L293E        ; forward to V-FOUND-1 with a full match.
  12683.  
  12684. ;; V-GET-PTR
  12685. L2929:  POP     HL              ; pop saved pointer to char 1
  12686.  
  12687. ;; V-NEXT
  12688. L292A:  PUSH    BC              ; save flags
  12689.         CALL    L19B8           ; routine NEXT-ONE gets next variable in DE
  12690.         EX      DE,HL           ; transfer to HL.
  12691.         POP     BC              ; restore the flags
  12692.         JR      L2900           ; loop back to V-EACH
  12693.                                 ; to compare each variable
  12694.  
  12695. ; ---
  12696.  
  12697. ;; V-80-BYTE
  12698. L2932:  SET     7,B             ; will signal not found
  12699.  
  12700. ; the branch was here when checking syntax
  12701.  
  12702. ;; V-SYNTAX
  12703. L2934:  POP     DE              ; discard the pointer to 2nd. character  v2
  12704.                                 ; in BASIC line/workspace.
  12705.  
  12706.         RST     18H             ; GET-CHAR gets character after variable name.
  12707.         CP      $28             ; is it '(' ?
  12708.         JR      Z,L2943         ; forward to V-PASS
  12709.                                 ; Note. could go straight to V-END ?
  12710.  
  12711.         SET     5,B             ; signal not an array
  12712.         JR      L294B           ; forward to V-END
  12713.  
  12714. ; ---------------------------
  12715.  
  12716. ; the jump was here when a long name matched and HL pointing to last character
  12717. ; in variables area.
  12718.  
  12719. ;; V-FOUND-1
  12720. L293E:  POP     DE              ; discard pointer to first var letter
  12721.  
  12722. ; the jump was here with all other matches HL points to first var char.
  12723.  
  12724. ;; V-FOUND-2
  12725. L293F:  POP     DE              ; discard pointer to 2nd prog char       v2
  12726.         POP     DE              ; drop pointer to 1st prog char          v1
  12727.         PUSH    HL              ; save pointer to last char in vars
  12728.  
  12729.         RST     18H             ; GET-CHAR
  12730.  
  12731. ;; V-PASS
  12732. L2943:  CALL    L2C88           ; routine ALPHANUM
  12733.         JR      NC,L294B        ; forward to V-END if not
  12734.  
  12735. ; but it never will be as we advanced past long-named variables earlier.
  12736.  
  12737.         RST     20H             ; NEXT-CHAR
  12738.         JR      L2943           ; back to V-PASS
  12739.  
  12740. ; ---
  12741.  
  12742. ;; V-END
  12743. L294B:  POP     HL              ; pop the pointer to first character in
  12744.                                 ; BASIC line/workspace.
  12745.         RL      B               ; rotate the B register left
  12746.                                 ; bit 7 to carry
  12747.         BIT     6,B             ; test the array indicator bit.
  12748.         RET                     ; return
  12749.  
  12750. ; -----------------------
  12751. ; Stack function argument
  12752. ; -----------------------
  12753. ; This branch is taken from LOOK-VARS when a defined function is currently
  12754. ; being evaluated.
  12755. ; Scanning is evaluating the expression after the '=' and the variable
  12756. ; found could be in the argument list to the left of the '=' or in the
  12757. ; normal place after the program. Preference will be given to the former.
  12758. ; The variable name to be matched is in C.
  12759.  
  12760. ;; STK-F-ARG
  12761. L2951:  LD      HL,($5C0B)      ; set HL to DEFADD
  12762.         LD      A,(HL)          ; load the first character
  12763.         CP      $29             ; is it ')' ?
  12764.         JP      Z,L28EF         ; JUMP back to V-RUN/SYN, if so, as there are
  12765.                                 ; no arguments.
  12766.  
  12767. ; but proceed to search argument list of defined function first if not empty.
  12768.  
  12769. ;; SFA-LOOP
  12770. L295A:  LD      A,(HL)          ; fetch character again.
  12771.         OR      $60             ; or with 01100000 presume a simple variable.
  12772.         LD      B,A             ; save result in B.
  12773.         INC     HL              ; address next location.
  12774.         LD      A,(HL)          ; pick up byte.
  12775.         CP      $0E             ; is it the number marker ?
  12776.         JR      Z,L296B         ; forward to SFA-CP-VR if so.
  12777.  
  12778. ; it was a string. White-space may be present but syntax has been checked.
  12779.  
  12780.         DEC     HL              ; point back to letter.
  12781.         CALL    L28AB           ; routine FN-SKPOVR skips to the '$'
  12782.         INC     HL              ; now address the hidden marker.
  12783.         RES     5,B             ; signal a string variable.
  12784.  
  12785. ;; SFA-CP-VR
  12786. L296B:  LD      A,B             ; transfer found variable letter to A.
  12787.         CP      C               ; compare with expected.
  12788.         JR      Z,L2981         ; forward to SFA-MATCH with a match.
  12789.  
  12790.         INC     HL              ; step
  12791.         INC     HL              ; past
  12792.         INC     HL              ; the
  12793.         INC     HL              ; five
  12794.         INC     HL              ; bytes.
  12795.  
  12796.         CALL    L28AB           ; routine FN-SKPOVR skips to next character
  12797.         CP      $29             ; is it ')' ?
  12798.         JP      Z,L28EF         ; jump back if so to V-RUN/SYN to look in
  12799.                                 ; normal variables area.
  12800.  
  12801.         CALL    L28AB           ; routine FN-SKPOVR skips past the ','
  12802.                                 ; all syntax has been checked and these
  12803.                                 ; things can be taken as read.
  12804.         JR      L295A           ; back to SFA-LOOP while there are more
  12805.                                 ; arguments.
  12806.  
  12807. ; ---
  12808.  
  12809. ;; SFA-MATCH
  12810. L2981:  BIT     5,C             ; test if numeric
  12811.         JR      NZ,L2991        ; to SFA-END if so as will be stacked
  12812.                                 ; by scanning
  12813.  
  12814.         INC     HL              ; point to start of string descriptor
  12815.         LD      DE,($5C65)      ; set DE to STKEND
  12816.         CALL    L33C0           ; routine MOVE-FP puts parameters on stack.
  12817.         EX      DE,HL           ; new free location to HL.
  12818.         LD      ($5C65),HL      ; use it to set STKEND system variable.
  12819.  
  12820. ;; SFA-END
  12821. L2991:  POP     DE              ; discard
  12822.         POP     DE              ; pointers.
  12823.         XOR     A               ; clear carry flag.
  12824.         INC     A               ; and zero flag.
  12825.         RET                     ; return.
  12826.  
  12827. ; ------------------------
  12828. ; Stack variable component
  12829. ; ------------------------
  12830. ; This is called to evaluate a complex structure that has been found, in
  12831. ; runtime, by LOOK-VARS in the variables area.
  12832. ; In this case HL points to the initial letter, bits 7-5
  12833. ; of which indicate the type of variable.
  12834. ; 010 - simple string, 110 - string array, 100 - array of numbers.
  12835. ;
  12836. ; It is called from CLASS-01 when assigning to a string or array including
  12837. ; a slice.
  12838. ; It is called from SCANNING to isolate the required part of the structure.
  12839. ;
  12840. ; An important part of the runtime process is to check that the number of
  12841. ; dimensions of the variable match the number of subscripts supplied in the
  12842. ; BASIC line.
  12843. ;
  12844. ; If checking syntax,
  12845. ; the B register, which counts dimensions is set to zero (256) to allow
  12846. ; the loop to continue till all subscripts are checked. While doing this it
  12847. ; is reading dimension sizes from some arbitrary area of memory. Although
  12848. ; these are meaningless it is of no concern as the limit is never checked by
  12849. ; int-exp during syntax checking.
  12850. ;
  12851. ; The routine is also called from the syntax path of DIM command to check the
  12852. ; syntax of both string and numeric arrays definitions except that bit 6 of C
  12853. ; is reset so both are checked as numeric arrays. This ruse avoids a terminal
  12854. ; slice being accepted as part of the DIM command.
  12855. ; All that is being checked is that there are a valid set of comma-separated
  12856. ; expressions before a terminal ')', although, as above, it will still go
  12857. ; through the motions of checking dummy dimension sizes.
  12858.  
  12859. ;; STK-VAR
  12860. L2996:  XOR     A               ; clear A
  12861.         LD      B,A             ; and B, the syntax dimension counter (256)
  12862.         BIT     7,C             ; checking syntax ?
  12863.         JR      NZ,L29E7        ; forward to SV-COUNT if so.
  12864.  
  12865. ; runtime evaluation.
  12866.  
  12867.         BIT     7,(HL)          ; will be reset if a simple string.
  12868.         JR      NZ,L29AE        ; forward to SV-ARRAYS otherwise
  12869.  
  12870.         INC     A               ; set A to 1, simple string.
  12871.  
  12872. ;; SV-SIMPLE$
  12873. L29A1:  INC     HL              ; address length low
  12874.         LD      C,(HL)          ; place in C
  12875.         INC     HL              ; address length high
  12876.         LD      B,(HL)          ; place in B
  12877.         INC     HL              ; address start of string
  12878.         EX      DE,HL           ; DE = start now.
  12879.         CALL    L2AB2           ; routine STK-STO-$ stacks string parameters
  12880.                                 ; DE start in variables area,
  12881.                                 ; BC length, A=1 simple string
  12882.  
  12883. ; the only thing now is to consider if a slice is required.
  12884.  
  12885.         RST     18H             ; GET-CHAR puts character at CH_ADD in A
  12886.         JP      L2A49           ; jump forward to SV-SLICE? to test for '('
  12887.  
  12888. ; --------------------------------------------------------
  12889.  
  12890. ; the branch was here with string and numeric arrays in runtime.
  12891.  
  12892. ;; SV-ARRAYS
  12893. L29AE:  INC     HL              ; step past
  12894.         INC     HL              ; the total length
  12895.         INC     HL              ; to address Number of dimensions.
  12896.         LD      B,(HL)          ; transfer to B overwriting zero.
  12897.         BIT     6,C             ; a numeric array ?
  12898.         JR      Z,L29C0         ; forward to SV-PTR with numeric arrays
  12899.  
  12900.         DEC     B               ; ignore the final element of a string array
  12901.                                 ; the fixed string size.
  12902.  
  12903.         JR      Z,L29A1         ; back to SV-SIMPLE$ if result is zero as has
  12904.                                 ; been created with DIM a$(10) for instance
  12905.                                 ; and can be treated as a simple string.
  12906.  
  12907. ; proceed with multi-dimensioned string arrays in runtime.
  12908.  
  12909.         EX      DE,HL           ; save pointer to dimensions in DE
  12910.  
  12911.         RST     18H             ; GET-CHAR looks at the BASIC line
  12912.         CP      $28             ; is character '(' ?
  12913.         JR      NZ,L2A20        ; to REPORT-3 if not
  12914.                                 ; 'Subscript wrong'
  12915.  
  12916.         EX      DE,HL           ; dimensions pointer to HL to synchronize
  12917.                                 ; with next instruction.
  12918.  
  12919. ; runtime numeric arrays path rejoins here.
  12920.  
  12921. ;; SV-PTR
  12922. L29C0:  EX      DE,HL           ; save dimension pointer in DE
  12923.         JR      L29E7           ; forward to SV-COUNT with true no of dims
  12924.                                 ; in B. As there is no initial comma the
  12925.                                 ; loop is entered at the midpoint.
  12926.  
  12927. ; ----------------------------------------------------------
  12928. ; the dimension counting loop which is entered at mid-point.
  12929.  
  12930. ;; SV-COMMA
  12931. L29C3:  PUSH    HL              ; save counter
  12932.  
  12933.         RST     18H             ; GET-CHAR
  12934.  
  12935.         POP     HL              ; pop counter
  12936.         CP      $2C             ; is character ',' ?
  12937.         JR      Z,L29EA         ; forward to SV-LOOP if so
  12938.  
  12939. ; in runtime the variable definition indicates a comma should appear here
  12940.  
  12941.         BIT     7,C             ; checking syntax ?
  12942.         JR      Z,L2A20         ; forward to REPORT-3 if not
  12943.                                 ; 'Subscript error'
  12944.  
  12945. ; proceed if checking syntax of an array?
  12946.  
  12947.         BIT     6,C             ; array of strings
  12948.         JR      NZ,L29D8        ; forward to SV-CLOSE if so
  12949.  
  12950. ; an array of numbers.
  12951.  
  12952.         CP      $29             ; is character ')' ?
  12953.         JR      NZ,L2A12        ; forward to SV-RPT-C if not
  12954.                                 ; 'Nonsense in BASIC'
  12955.  
  12956.         RST     20H             ; NEXT-CHAR moves CH-ADD past the statement
  12957.         RET                     ; return ->
  12958.  
  12959. ; ---
  12960.  
  12961. ; the branch was here with an array of strings.
  12962.  
  12963. ;; SV-CLOSE
  12964. L29D8:  CP      $29             ; as above ')' could follow the expression
  12965.         JR      Z,L2A48         ; forward to SV-DIM if so
  12966.  
  12967.         CP      $CC             ; is it 'TO' ?
  12968.         JR      NZ,L2A12        ; to SV-RPT-C with anything else
  12969.                                 ; 'Nonsense in BASIC'
  12970.  
  12971. ; now backtrack CH_ADD to set up for slicing routine.
  12972. ; Note. in a BASIC line we can safely backtrack to a colour parameter.
  12973.  
  12974. ;; SV-CH-ADD
  12975. L29E0:  RST     18H             ; GET-CHAR
  12976.         DEC     HL              ; backtrack HL
  12977.         LD      ($5C5D),HL      ; to set CH_ADD up for slicing routine
  12978.         JR      L2A45           ; forward to SV-SLICE and make a return
  12979.                                 ; when all slicing complete.
  12980.  
  12981. ; ----------------------------------------
  12982. ; -> the mid-point entry point of the loop
  12983.  
  12984. ;; SV-COUNT
  12985. L29E7:  LD      HL,$0000        ; initialize data pointer to zero.
  12986.  
  12987. ;; SV-LOOP
  12988. L29EA:  PUSH    HL              ; save the data pointer.
  12989.  
  12990.         RST     20H             ; NEXT-CHAR in BASIC area points to an
  12991.                                 ; expression.
  12992.  
  12993.         POP     HL              ; restore the data pointer.
  12994.         LD      A,C             ; transfer name/type to A.
  12995.         CP      $C0             ; is it 11000000 ?
  12996.                                 ; Note. the letter component is absent if
  12997.                                 ; syntax checking.
  12998.         JR      NZ,L29FB        ; forward to SV-MULT if not an array of
  12999.                                 ; strings.
  13000.  
  13001. ; proceed to check string arrays during syntax.
  13002.  
  13003.         RST     18H             ; GET-CHAR
  13004.         CP      $29             ; ')'  end of subscripts ?
  13005.         JR      Z,L2A48         ; forward to SV-DIM to consider further slice
  13006.  
  13007.         CP      $CC             ; is it 'TO' ?
  13008.         JR      Z,L29E0         ; back to SV-CH-ADD to consider a slice.
  13009.                                 ; (no need to repeat get-char at L29E0)
  13010.  
  13011. ; if neither, then an expression is required so rejoin runtime loop ??
  13012. ; registers HL and DE only point to somewhere meaningful in runtime so
  13013. ; comments apply to that situation.
  13014.  
  13015. ;; SV-MULT
  13016. L29FB:  PUSH    BC              ; save dimension number.
  13017.         PUSH    HL              ; push data pointer/rubbish.
  13018.                                 ; DE points to current dimension.
  13019.         CALL    L2AEE           ; routine DE,(DE+1) gets next dimension in DE
  13020.                                 ; and HL points to it.
  13021.         EX      (SP),HL         ; dim pointer to stack, data pointer to HL (*)
  13022.         EX      DE,HL           ; data pointer to DE, dim size to HL.
  13023.  
  13024.         CALL    L2ACC           ; routine INT-EXP1 checks integer expression
  13025.                                 ; and gets result in BC in runtime.
  13026.         JR      C,L2A20         ; to REPORT-3 if > HL
  13027.                                 ; 'Subscript out of range'
  13028.  
  13029.         DEC     BC              ; adjust returned result from 1-x to 0-x
  13030.         CALL    L2AF4           ; routine GET-HL*DE multiplies data pointer by
  13031.                                 ; dimension size.
  13032.         ADD     HL,BC           ; add the integer returned by expression.
  13033.         POP     DE              ; pop the dimension pointer.                              ***
  13034.         POP     BC              ; pop dimension counter.
  13035.         DJNZ    L29C3           ; back to SV-COMMA if more dimensions
  13036.                                 ; Note. during syntax checking, unless there
  13037.                                 ; are more than 256 subscripts, the branch
  13038.                                 ; back to SV-COMMA is always taken.
  13039.  
  13040.         BIT     7,C             ; are we checking syntax ?
  13041.                                 ; then we've got a joker here.
  13042.  
  13043. ;; SV-RPT-C
  13044. L2A12:  JR      NZ,L2A7A        ; forward to SL-RPT-C if so
  13045.                                 ; 'Nonsense in BASIC'
  13046.                                 ; more than 256 subscripts in BASIC line.
  13047.  
  13048. ; but in runtime the number of subscripts are at least the same as dims
  13049.  
  13050.         PUSH    HL              ; save data pointer.
  13051.         BIT     6,C             ; is it a string array ?
  13052.         JR      NZ,L2A2C        ; forward to SV-ELEM$ if so.
  13053.  
  13054. ; a runtime numeric array subscript.
  13055.  
  13056.         LD      B,D             ; register DE has advanced past all dimensions
  13057.         LD      C,E             ; and points to start of data in variable.
  13058.                                 ; transfer it to BC.
  13059.  
  13060.         RST     18H             ; GET-CHAR checks BASIC line
  13061.         CP      $29             ; must be a ')' ?
  13062.         JR      Z,L2A22         ; skip to SV-NUMBER if so
  13063.  
  13064. ; else more subscripts in BASIC line than the variable definition.
  13065.  
  13066. ;; REPORT-3
  13067. L2A20:  RST     08H             ; ERROR-1
  13068.         DB    $02             ; Error Report: Subscript wrong
  13069.  
  13070. ; continue if subscripts matched the numeric array.
  13071.  
  13072. ;; SV-NUMBER
  13073. L2A22:  RST     20H             ; NEXT-CHAR moves CH_ADD to next statement
  13074.                                 ; - finished parsing.
  13075.  
  13076.         POP     HL              ; pop the data pointer.
  13077.         LD      DE,$0005        ; each numeric element is 5 bytes.
  13078.         CALL    L2AF4           ; routine GET-HL*DE multiplies.
  13079.         ADD     HL,BC           ; now add to start of data in the variable.
  13080.  
  13081.         RET                     ; return with HL pointing at the numeric
  13082.                                 ; array subscript.                       ->
  13083.  
  13084. ; ---------------------------------------------------------------
  13085.  
  13086. ; the branch was here for string subscripts when the number of subscripts
  13087. ; in the BASIC line was one less than in variable definition.
  13088.  
  13089. ;; SV-ELEM$
  13090. L2A2C:  CALL    L2AEE           ; routine DE,(DE+1) gets final dimension
  13091.                                 ; the length of strings in this array.
  13092.         EX      (SP),HL         ; start pointer to stack, data pointer to HL.
  13093.         CALL    L2AF4           ; routine GET-HL*DE multiplies by element
  13094.                                 ; size.
  13095.         POP     BC              ; the start of data pointer is added
  13096.         ADD     HL,BC           ; in - now points to location before.
  13097.         INC     HL              ; point to start of required string.
  13098.         LD      B,D             ; transfer the length (final dimension size)
  13099.         LD      C,E             ; from DE to BC.
  13100.         EX      DE,HL           ; put start in DE.
  13101.         CALL    L2AB1           ; routine STK-ST-0 stores the string parameters
  13102.                                 ; with A=0 - a slice or subscript.
  13103.  
  13104. ; now check that there were no more subscripts in the BASIC line.
  13105.  
  13106.         RST     18H             ; GET-CHAR
  13107.         CP      $29             ; is it ')' ?
  13108.         JR      Z,L2A48         ; forward to SV-DIM to consider a separate
  13109.                                 ; subscript or/and a slice.
  13110.  
  13111.         CP      $2C             ; a comma is allowed if the final subscript
  13112.                                 ; is to be sliced e.g a$(2,3,4 TO 6).
  13113.         JR      NZ,L2A20        ; to REPORT-3 with anything else
  13114.                                 ; 'Subscript error'
  13115.  
  13116. ;; SV-SLICE
  13117. L2A45:  CALL    L2A52           ; routine SLICING slices the string.
  13118.  
  13119. ; but a slice of a simple string can itself be sliced.
  13120.  
  13121. ;; SV-DIM
  13122. L2A48:  RST     20H             ; NEXT-CHAR
  13123.  
  13124. ;; SV-SLICE?
  13125. L2A49:  CP      $28             ; is character '(' ?
  13126.         JR      Z,L2A45         ; loop back if so to SV-SLICE
  13127.  
  13128.         RES     6,(IY+$01)      ; update FLAGS  - Signal string result
  13129.         RET                     ; and return.
  13130.  
  13131. ; ---
  13132.  
  13133. ; The above section deals with the flexible syntax allowed.
  13134. ; DIM a$(3,3,10) can be considered as two dimensional array of ten-character
  13135. ; strings or a 3-dimensional array of characters.
  13136. ; a$(1,1) will return a 10-character string as will a$(1,1,1 TO 10)
  13137. ; a$(1,1,1) will return a single character.
  13138. ; a$(1,1) (1 TO 6) is the same as a$(1,1,1 TO 6)
  13139. ; A slice can itself be sliced ad infinitum
  13140. ; b$ () () () () () () (2 TO 10) (2 TO 9) (3) is the same as b$(5)
  13141.  
  13142.  
  13143.  
  13144. ; -------------------------
  13145. ; Handle slicing of strings
  13146. ; -------------------------
  13147. ; The syntax of string slicing is very natural and it is as well to reflect
  13148. ; on the permutations possible.
  13149. ; a$() and a$( TO ) indicate the entire string although just a$ would do
  13150. ; and would avoid coming here.
  13151. ; h$(16) indicates the single character at position 16.
  13152. ; a$( TO 32) indicates the first 32 characters.
  13153. ; a$(257 TO) indicates all except the first 256 characters.
  13154. ; a$(19000 TO 19999) indicates the thousand characters at position 19000.
  13155. ; Also a$(9 TO 5) returns a null string not an error.
  13156. ; This enables a$(2 TO) to return a null string if the passed string is
  13157. ; of length zero or 1.
  13158. ; A string expression in brackets can be sliced. e.g. (STR$ PI) (3 TO )
  13159. ; We arrived here from SCANNING with CH-ADD pointing to the initial '('
  13160. ; or from above.
  13161.  
  13162. ;; SLICING
  13163. L2A52:  CALL    L2530           ; routine SYNTAX-Z
  13164.         CALL    NZ,L2BF1        ; routine STK-FETCH fetches parameters of
  13165.                                 ; string at runtime, start in DE, length
  13166.                                 ; in BC. This could be an array subscript.
  13167.  
  13168.         RST     20H             ; NEXT-CHAR
  13169.         CP      $29             ; is it ')' ?     e.g. a$()
  13170.         JR      Z,L2AAD         ; forward to SL-STORE to store entire string.
  13171.  
  13172.         PUSH    DE              ; else save start address of string
  13173.  
  13174.         XOR     A               ; clear accumulator to use as a running flag.
  13175.         PUSH    AF              ; and save on stack before any branching.
  13176.  
  13177.         PUSH    BC              ; save length of string to be sliced.
  13178.         LD      DE,$0001        ; default the start point to position 1.
  13179.  
  13180.         RST     18H             ; GET-CHAR
  13181.  
  13182.         POP     HL              ; pop length to HL as default end point
  13183.                                 ; and limit.
  13184.  
  13185.         CP      $CC             ; is it 'TO' ?    e.g. a$( TO 10000)
  13186.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter.
  13187.  
  13188.         POP     AF              ; pop the running flag.
  13189.  
  13190.         CALL    L2ACD           ; routine INT-EXP2 fetches first parameter.
  13191.  
  13192.         PUSH    AF              ; save flag (will be $FF if parameter>limit)
  13193.  
  13194.         LD      D,B             ; transfer the start
  13195.         LD      E,C             ; to DE overwriting 0001.
  13196.         PUSH    HL              ; save original length.
  13197.  
  13198.         RST     18H             ; GET-CHAR
  13199.         POP     HL              ; pop the limit length.
  13200.         CP      $CC             ; is it 'TO' after a start ?
  13201.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter
  13202.  
  13203.         CP      $29             ; is it ')' ?       e.g. a$(365)
  13204.  
  13205. ;; SL-RPT-C
  13206. L2A7A:  JP      NZ,L1C8A        ; jump to REPORT-C with anything else
  13207.                                 ; 'Nonsense in BASIC'
  13208.  
  13209.         LD      H,D             ; copy start
  13210.         LD      L,E             ; to end - just a one character slice.
  13211.         JR      L2A94           ; forward to SL-DEFINE.
  13212.  
  13213. ; ---------------------
  13214.  
  13215. ;; SL-SECOND
  13216. L2A81:  PUSH    HL              ; save limit length.
  13217.  
  13218.         RST     20H             ; NEXT-CHAR
  13219.  
  13220.         POP     HL              ; pop the length.
  13221.  
  13222.         CP      $29             ; is character ')' ?        e.g a$(7 TO )
  13223.         JR      Z,L2A94         ; to SL-DEFINE using length as end point.
  13224.  
  13225.         POP     AF              ; else restore flag.
  13226.         CALL    L2ACD           ; routine INT-EXP2 gets second expression.
  13227.  
  13228.         PUSH    AF              ; save the running flag.
  13229.  
  13230.         RST     18H             ; GET-CHAR
  13231.  
  13232.         LD      H,B             ; transfer second parameter
  13233.         LD      L,C             ; to HL.              e.g. a$(42 to 99)
  13234.         CP      $29             ; is character a ')' ?
  13235.         JR      NZ,L2A7A        ; to SL-RPT-C if not
  13236.                                 ; 'Nonsense in BASIC'
  13237.  
  13238. ; we now have start in DE and an end in HL.
  13239.  
  13240. ;; SL-DEFINE
  13241. L2A94:  POP     AF              ; pop the running flag.
  13242.         EX      (SP),HL         ; put end point on stack, start address to HL
  13243.         ADD     HL,DE           ; add address of string to the start point.
  13244.         DEC     HL              ; point to first character of slice.
  13245.         EX      (SP),HL         ; start address to stack, end point to HL (*)
  13246.         AND     A               ; prepare to subtract.
  13247.         SBC     HL,DE           ; subtract start point from end point.
  13248.         LD      BC,$0000        ; default the length result to zero.
  13249.         JR      C,L2AA8         ; forward to SL-OVER if start > end.
  13250.  
  13251.         INC     HL              ; increment the length for inclusive byte.
  13252.  
  13253.         AND     A               ; now test the running flag.
  13254.         JP      M,L2A20         ; jump back to REPORT-3 if $FF.
  13255.                                 ; 'Subscript out of range'
  13256.  
  13257.         LD      B,H             ; transfer the length
  13258.         LD      C,L             ; to BC.
  13259.  
  13260. ;; SL-OVER
  13261. L2AA8:  POP     DE              ; restore start address from machine stack ***
  13262.         RES     6,(IY+$01)      ; update FLAGS - signal string result for
  13263.                                 ; syntax.
  13264.  
  13265. ;; SL-STORE
  13266. L2AAD:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  13267.         RET     Z               ; return if checking syntax.
  13268.                                 ; but continue to store the string in runtime.
  13269.  
  13270. ; ------------------------------------
  13271. ; other than from above, this routine is called from STK-VAR to stack
  13272. ; a known string array element.
  13273. ; ------------------------------------
  13274.  
  13275. ;; STK-ST-0
  13276. L2AB1:  XOR     A               ; clear to signal a sliced string or element.
  13277.  
  13278. ; -------------------------
  13279. ; this routine is called from chr$, scrn$ etc. to store a simple string result.
  13280. ; --------------------------
  13281.  
  13282. ;; STK-STO-$
  13283. L2AB2:  RES     6,(IY+$01)      ; update FLAGS - signal string result.
  13284.                                 ; and continue to store parameters of string.
  13285.  
  13286. ; ---------------------------------------
  13287. ; Pass five registers to calculator stack
  13288. ; ---------------------------------------
  13289. ; This subroutine puts five registers on the calculator stack.
  13290.  
  13291. ;; STK-STORE
  13292. L2AB6:  PUSH    BC              ; save two registers
  13293.         CALL    L33A9           ; routine TEST-5-SP checks room and puts 5
  13294.                                 ; in BC.
  13295.         POP     BC              ; fetch the saved registers.
  13296.         LD      HL,($5C65)      ; make HL point to first empty location STKEND
  13297.         LD      (HL),A          ; place the 5 registers.
  13298.         INC     HL              ;
  13299.         LD      (HL),E          ;
  13300.         INC     HL              ;
  13301.         LD      (HL),D          ;
  13302.         INC     HL              ;
  13303.         LD      (HL),C          ;
  13304.         INC     HL              ;
  13305.         LD      (HL),B          ;
  13306.         INC     HL              ;
  13307.         LD      ($5C65),HL      ; update system variable STKEND.
  13308.         RET                     ; and return.
  13309.  
  13310. ; -------------------------------------------
  13311. ; Return result of evaluating next expression
  13312. ; -------------------------------------------
  13313. ; This clever routine is used to check and evaluate an integer expression
  13314. ; which is returned in BC, setting A to $FF, if greater than a limit supplied
  13315. ; in HL. It is used to check array subscripts, parameters of a string slice
  13316. ; and the arguments of the DIM command. In the latter case, the limit check
  13317. ; is not required and H is set to $FF. When checking optional string slice
  13318. ; parameters, it is entered at the second entry point so as not to disturb
  13319. ; the running flag A, which may be $00 or $FF from a previous invocation.
  13320.  
  13321. ;; INT-EXP1
  13322. L2ACC:  XOR     A               ; set result flag to zero.
  13323.  
  13324. ; -> The entry point is here if A is used as a running flag.
  13325.  
  13326. ;; INT-EXP2
  13327. L2ACD:  PUSH    DE              ; preserve DE register throughout.
  13328.         PUSH    HL              ; save the supplied limit.
  13329.         PUSH    AF              ; save the flag.
  13330.  
  13331.         CALL    L1C82           ; routine EXPT-1NUM evaluates expression
  13332.                                 ; at CH_ADD returning if numeric result,
  13333.                                 ; with value on calculator stack.
  13334.  
  13335.         POP     AF              ; pop the flag.
  13336.         CALL    L2530           ; routine SYNTAX-Z
  13337.         JR      Z,L2AEB         ; forward to I-RESTORE if checking syntax so
  13338.                                 ; avoiding a comparison with supplied limit.
  13339.  
  13340.         PUSH    AF              ; save the flag.
  13341.  
  13342.         CALL    L1E99           ; routine FIND-INT2 fetches value from
  13343.                                 ; calculator stack to BC producing an error
  13344.                                 ; if too high.
  13345.  
  13346.         POP     DE              ; pop the flag to D.
  13347.         LD      A,B             ; test value for zero and reject
  13348.         OR      C               ; as arrays and strings begin at 1.
  13349.         SCF                     ; set carry flag.
  13350.         JR      Z,L2AE8         ; forward to I-CARRY if zero.
  13351.  
  13352.         POP     HL              ; restore the limit.
  13353.         PUSH    HL              ; and save.
  13354.         AND     A               ; prepare to subtract.
  13355.         SBC     HL,BC           ; subtract value from limit.
  13356.  
  13357. ;; I-CARRY
  13358. L2AE8:  LD      A,D             ; move flag to accumulator $00 or $FF.
  13359.         SBC     A,$00           ; will set to $FF if carry set.
  13360.  
  13361. ;; I-RESTORE
  13362. L2AEB:  POP     HL              ; restore the limit.
  13363.         POP     DE              ; and DE register.
  13364.         RET                     ; return.
  13365.  
  13366.  
  13367. ; -----------------------
  13368. ; LD DE,(DE+1) Subroutine
  13369. ; -----------------------
  13370. ; This routine just loads the DE register with the contents of the two
  13371. ; locations following the location addressed by DE.
  13372. ; It is used to step along the 16-bit dimension sizes in array definitions.
  13373. ; Note. Such code is made into subroutines to make programs easier to
  13374. ; write and it would use less space to include the five instructions in-line.
  13375. ; However, there are so many exchanges going on at the places this is invoked
  13376. ; that to implement it in-line would make the code hard to follow.
  13377. ; It probably had a zippier label though as the intention is to simplify the
  13378. ; program.
  13379.  
  13380. ;; DE,(DE+1)
  13381. L2AEE:  EX      DE,HL           ;
  13382.         INC     HL              ;
  13383.         LD      E,(HL)          ;
  13384.         INC     HL              ;
  13385.         LD      D,(HL)          ;
  13386.         RET                     ;
  13387.  
  13388. ; -------------------
  13389. ; HL=HL*DE Subroutine
  13390. ; -------------------
  13391. ; This routine calls the mathematical routine to multiply HL by DE in runtime.
  13392. ; It is called from STK-VAR and from DIM. In the latter case syntax is not
  13393. ; being checked so the entry point could have been at the second CALL
  13394. ; instruction to save a few clock-cycles.
  13395.  
  13396. ;; GET-HL*DE
  13397. L2AF4:  CALL    L2530           ; routine SYNTAX-Z.
  13398.         RET     Z               ; return if checking syntax.
  13399.  
  13400.         CALL    L30A9           ; routine HL-HL*DE.
  13401.         JP      C,L1F15         ; jump back to REPORT-4 if over 65535.
  13402.  
  13403.         RET                     ; else return with 16-bit result in HL.
  13404.  
  13405. ; -----------------
  13406. ; THE 'LET' COMMAND
  13407. ; -----------------
  13408. ; Sinclair BASIC adheres to the ANSI-78 standard and a LET is required in
  13409. ; assignments e.g. LET a = 1  :   LET h$ = "hat".
  13410. ;
  13411. ; Long names may contain spaces but not colour controls (when assigned).
  13412. ; a substring can appear to the left of the equals sign.
  13413.  
  13414. ; An earlier mathematician Lewis Carroll may have been pleased that
  13415. ; 10 LET Babies cannot manage crocodiles = Babies are illogical AND
  13416. ;    Nobody is despised who can manage a crocodile AND Illogical persons
  13417. ;    are despised
  13418. ; does not give the 'Nonsense..' error if the three variables exist.
  13419. ; I digress.
  13420.  
  13421. ;; LET
  13422. L2AFF:  LD      HL,($5C4D)      ; fetch system variable DEST to HL.
  13423.         BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  13424.         JR      Z,L2B66         ; forward to L-EXISTS if not.
  13425.  
  13426. ; continue for a new variable. DEST points to start in BASIC line.
  13427. ; from the CLASS routines.
  13428.  
  13429.         LD      BC,$0005        ; assume numeric and assign an initial 5 bytes
  13430.  
  13431. ;; L-EACH-CH
  13432. L2B0B:  INC     BC              ; increase byte count for each relevant
  13433.                                 ; character
  13434.  
  13435. ;; L-NO-SP
  13436. L2B0C:  INC     HL              ; increase pointer.
  13437.         LD      A,(HL)          ; fetch character.
  13438.         CP      $20             ; is it a space ?
  13439.         JR      Z,L2B0C         ; back to L-NO-SP is so.
  13440.  
  13441.         JR      NC,L2B1F        ; forward to L-TEST-CH if higher.
  13442.  
  13443.         CP      $10             ; is it $00 - $0F ?
  13444.         JR      C,L2B29         ; forward to L-SPACES if so.
  13445.  
  13446.         CP      $16             ; is it $16 - $1F ?
  13447.         JR      NC,L2B29        ; forward to L-SPACES if so.
  13448.  
  13449. ; it was $10 - $15  so step over a colour code.
  13450.  
  13451.         INC     HL              ; increase pointer.
  13452.         JR      L2B0C           ; loop back to L-NO-SP.
  13453.  
  13454. ; ---
  13455.  
  13456. ; the branch was to here if higher than space.
  13457.  
  13458. ;; L-TEST-CH
  13459. L2B1F:  CALL    L2C88           ; routine ALPHANUM sets carry if alphanumeric
  13460.         JR      C,L2B0B         ; loop back to L-EACH-CH for more if so.
  13461.  
  13462.         CP      $24             ; is it '$' ?
  13463.         JP      Z,L2BC0         ; jump forward if so, to L-NEW$
  13464.                                 ; with a new string.
  13465.  
  13466. ;; L-SPACES
  13467. L2B29:  LD      A,C             ; save length lo in A.
  13468.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  13469.         DEC     HL              ; point to location before, the variables
  13470.                                 ; end-marker.
  13471.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces
  13472.                                 ; for name and numeric value.
  13473.         INC     HL              ; advance to first new location.
  13474.         INC     HL              ; then to second.
  13475.         EX      DE,HL           ; set DE to second location.
  13476.         PUSH    DE              ; save this pointer.
  13477.         LD      HL,($5C4D)      ; reload HL with DEST.
  13478.         DEC     DE              ; point to first.
  13479.         SUB     $06             ; subtract six from length_lo.
  13480.         LD      B,A             ; save count in B.
  13481.         JR      Z,L2B4F         ; forward to L-SINGLE if it was just
  13482.                                 ; one character.
  13483.  
  13484. ; HL points to start of variable name after 'LET' in BASIC line.
  13485.  
  13486. ;; L-CHAR
  13487. L2B3E:  INC     HL              ; increase pointer.
  13488.         LD      A,(HL)          ; pick up character.
  13489.         CP      $21             ; is it space or higher ?
  13490.         JR      C,L2B3E         ; back to L-CHAR with space and less.
  13491.  
  13492.         OR      $20             ; make variable lower-case.
  13493.         INC     DE              ; increase destination pointer.
  13494.         LD      (DE),A          ; and load to edit line.
  13495.         DJNZ    L2B3E           ; loop back to L-CHAR until B is zero.
  13496.  
  13497.         OR      $80             ; invert the last character.
  13498.         LD      (DE),A          ; and overwrite that in edit line.
  13499.  
  13500. ; now consider first character which has bit 6 set
  13501.  
  13502.         LD      A,$C0           ; set A 11000000 is xor mask for a long name.
  13503.                                 ; %101      is xor/or  result
  13504.  
  13505. ; single character numerics rejoin here with %00000000 in mask.
  13506. ;                                            %011      will be xor/or result
  13507.  
  13508. ;; L-SINGLE
  13509. L2B4F:  LD      HL,($5C4D)      ; fetch DEST - HL addresses first character.
  13510.         XOR     (HL)            ; apply variable type indicator mask (above).
  13511.         OR      $20             ; make lowercase - set bit 5.
  13512.         POP     HL              ; restore pointer to 2nd character.
  13513.         CALL    L2BEA           ; routine L-FIRST puts A in first character.
  13514.                                 ; and returns with HL holding
  13515.                                 ; new E_LINE-1  the $80 vars end-marker.
  13516.  
  13517. ;; L-NUMERIC
  13518. L2B59:  PUSH    HL              ; save the pointer.
  13519.  
  13520. ; the value of variable is deleted but remains after calculator stack.
  13521.  
  13522.         RST     28H             ;; FP-CALC
  13523.         DB    $02             ;;delete      ; delete variable value
  13524.         DB    $38             ;;end-calc
  13525.  
  13526. ; DE (STKEND) points to start of value.
  13527.  
  13528.         POP     HL              ; restore the pointer.
  13529.         LD      BC,$0005        ; start of number is five bytes before.
  13530.         AND     A               ; prepare for true subtraction.
  13531.         SBC     HL,BC           ; HL points to start of value.
  13532.         JR      L2BA6           ; forward to L-ENTER  ==>
  13533.  
  13534. ; ---
  13535.  
  13536.  
  13537. ; the jump was to here if the variable already existed.
  13538.  
  13539. ;; L-EXISTS
  13540. L2B66:  BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  13541.         JR      Z,L2B72         ; skip forward to L-DELETE$   -*->
  13542.                                 ; if string result.
  13543.  
  13544. ; A numeric variable could be simple or an array element.
  13545. ; They are treated the same and the old value is overwritten.
  13546.  
  13547.         LD      DE,$0006        ; six bytes forward points to loc past value.
  13548.         ADD     HL,DE           ; add to start of number.
  13549.         JR      L2B59           ; back to L-NUMERIC to overwrite value.
  13550.  
  13551. ; ---
  13552.  
  13553. ; -*-> the branch was here if a string existed.
  13554.  
  13555. ;; L-DELETE$
  13556. L2B72:  LD      HL,($5C4D)      ; fetch DEST to HL.
  13557.                                 ; (still set from first instruction)
  13558.         LD      BC,($5C72)      ; fetch STRLEN to BC.
  13559.         BIT     0,(IY+$37)      ; test FLAGX - handling a complete simple
  13560.                                 ; string ?
  13561.         JR      NZ,L2BAF        ; forward to L-ADD$ if so.
  13562.  
  13563. ; must be a string array or a slice in workspace.
  13564. ; Note. LET a$(3 TO 6) = h$   will assign "hat " if h$ = "hat"
  13565. ;                                  and    "hats" if h$ = "hatstand".
  13566. ;
  13567. ; This is known as Procrustian lengthening and shortening after a
  13568. ; character Procrustes in Greek legend who made travellers sleep in his bed,
  13569. ; cutting off their feet or stretching them so they fitted the bed perfectly.
  13570. ; The bloke was hatstand and slain by Theseus.
  13571.  
  13572.         LD      A,B             ; test if length
  13573.         OR      C               ; is zero and
  13574.         RET     Z               ; return if so.
  13575.  
  13576.         PUSH    HL              ; save pointer to start.
  13577.  
  13578.         RST     30H             ; BC-SPACES creates room.
  13579.         PUSH    DE              ; save pointer to first new location.
  13580.         PUSH    BC              ; and length            (*)
  13581.         LD      D,H             ; set DE to point to last location.
  13582.         LD      E,L             ;
  13583.         INC     HL              ; set HL to next location.
  13584.         LD      (HL),$20        ; place a space there.
  13585.         LDDR                    ; copy bytes filling with spaces.
  13586.  
  13587.         PUSH    HL              ; save pointer to start.
  13588.         CALL    L2BF1           ; routine STK-FETCH start to DE,
  13589.                                 ; length to BC.
  13590.         POP     HL              ; restore the pointer.
  13591.         EX      (SP),HL         ; (*) length to HL, pointer to stack.
  13592.         AND     A               ; prepare for true subtraction.
  13593.         SBC     HL,BC           ; subtract old length from new.
  13594.         ADD     HL,BC           ; and add back.
  13595.         JR      NC,L2B9B        ; forward if it fits to L-LENGTH.
  13596.  
  13597.         LD      B,H             ; otherwise set
  13598.         LD      C,L             ; length to old length.
  13599.                                 ; "hatstand" becomes "hats"
  13600.  
  13601. ;; L-LENGTH
  13602. L2B9B:  EX      (SP),HL         ; (*) length to stack, pointer to HL.
  13603.         EX      DE,HL           ; pointer to DE, start of string to HL.
  13604.         LD      A,B             ; is the length zero ?
  13605.         OR      C               ;
  13606.         JR      Z,L2BA3         ; forward to L-IN-W/S if so
  13607.                                 ; leaving prepared spaces.
  13608.  
  13609.         LDIR                    ; else copy bytes overwriting some spaces.
  13610.  
  13611. ;; L-IN-W/S
  13612. L2BA3:  POP     BC              ; pop the new length.  (*)
  13613.         POP     DE              ; pop pointer to new area.
  13614.         POP     HL              ; pop pointer to variable in assignment.
  13615.                                 ; and continue copying from workspace
  13616.                                 ; to variables area.
  13617.  
  13618. ; ==> branch here from  L-NUMERIC
  13619.  
  13620. ;; L-ENTER
  13621. L2BA6:  EX      DE,HL           ; exchange pointers HL=STKEND DE=end of vars.
  13622.         LD      A,B             ; test the length
  13623.         OR      C               ; and make a
  13624.         RET     Z               ; return if zero (strings only).
  13625.  
  13626.         PUSH    DE              ; save start of destination.
  13627.         LDIR                    ; copy bytes.
  13628.         POP     HL              ; address the start.
  13629.         RET                     ; and return.
  13630.  
  13631. ; ---
  13632.  
  13633. ; the branch was here from L-DELETE$ if an existing simple string.
  13634. ; register HL addresses start of string in variables area.
  13635.  
  13636. ;; L-ADD$
  13637. L2BAF:  DEC     HL              ; point to high byte of length.
  13638.         DEC     HL              ; to low byte.
  13639.         DEC     HL              ; to letter.
  13640.         LD      A,(HL)          ; fetch masked letter to A.
  13641.         PUSH    HL              ; save the pointer on stack.
  13642.         PUSH    BC              ; save new length.
  13643.         CALL    L2BC6           ; routine L-STRING adds new string at end
  13644.                                 ; of variables area.
  13645.                                 ; if no room we still have old one.
  13646.         POP     BC              ; restore length.
  13647.         POP     HL              ; restore start.
  13648.         INC     BC              ; increase
  13649.         INC     BC              ; length by three
  13650.         INC     BC              ; to include character and length bytes.
  13651.         JP      L19E8           ; jump to indirect exit via RECLAIM-2
  13652.                                 ; deleting old version and adjusting pointers.
  13653.  
  13654. ; ---
  13655.  
  13656. ; the jump was here with a new string variable.
  13657.  
  13658. ;; L-NEW$
  13659. L2BC0:  LD      A,$DF           ; indicator mask %11011111 for
  13660.                                 ;                %010xxxxx will be result
  13661.         LD      HL,($5C4D)      ; address DEST first character.
  13662.         AND     (HL)            ; combine mask with character.
  13663.  
  13664. ;; L-STRING
  13665. L2BC6:  PUSH    AF              ; save first character and mask.
  13666.         CALL    L2BF1           ; routine STK-FETCH fetches parameters of
  13667.                                 ; the string.
  13668.         EX      DE,HL           ; transfer start to HL.
  13669.         ADD     HL,BC           ; add to length.
  13670.         PUSH    BC              ; save the length.
  13671.         DEC     HL              ; point to end of string.
  13672.         LD      ($5C4D),HL      ; save pointer in DEST.
  13673.                                 ; (updated by POINTERS if in workspace)
  13674.         INC     BC              ; extra byte for letter.
  13675.         INC     BC              ; two bytes
  13676.         INC     BC              ; for the length of string.
  13677.         LD      HL,($5C59)      ; address E_LINE.
  13678.         DEC     HL              ; now end of VARS area.
  13679.         CALL    L1655           ; routine MAKE-ROOM makes room for string.
  13680.                                 ; updating pointers including DEST.
  13681.         LD      HL,($5C4D)      ; pick up pointer to end of string from DEST.
  13682.         POP     BC              ; restore length from stack.
  13683.         PUSH    BC              ; and save again on stack.
  13684.         INC     BC              ; add a byte.
  13685.         LDDR                    ; copy bytes from end to start.
  13686.         EX      DE,HL           ; HL addresses length low
  13687.         INC     HL              ; increase to address high byte
  13688.         POP     BC              ; restore length to BC
  13689.         LD      (HL),B          ; insert high byte
  13690.         DEC     HL              ; address low byte location
  13691.         LD      (HL),C          ; insert that byte
  13692.         POP     AF              ; restore character and mask
  13693.  
  13694. ;; L-FIRST
  13695. L2BEA:  DEC     HL              ; address variable name
  13696.         LD      (HL),A          ; and insert character.
  13697.         LD      HL,($5C59)      ; load HL with E_LINE.
  13698.         DEC     HL              ; now end of VARS area.
  13699.         RET                     ; return
  13700.  
  13701. ; ------------------------------------
  13702. ; Get last value from calculator stack
  13703. ; ------------------------------------
  13704. ;
  13705. ;
  13706.  
  13707. ;; STK-FETCH
  13708. L2BF1:  LD      HL,($5C65)      ; STKEND
  13709.         DEC     HL              ;
  13710.         LD      B,(HL)          ;
  13711.         DEC     HL              ;
  13712.         LD      C,(HL)          ;
  13713.         DEC     HL              ;
  13714.         LD      D,(HL)          ;
  13715.         DEC     HL              ;
  13716.         LD      E,(HL)          ;
  13717.         DEC     HL              ;
  13718.         LD      A,(HL)          ;
  13719.         LD      ($5C65),HL      ; STKEND
  13720.         RET                     ;
  13721.  
  13722. ; ------------------
  13723. ; Handle DIM command
  13724. ; ------------------
  13725. ; e.g. DIM a(2,3,4,7): DIM a$(32) : DIM b$(300,2,768) : DIM c$(20000)
  13726. ; the only limit to dimensions is memory so, for example,
  13727. ; DIM a(2,2,2,2,2,2,2,2,2,2,2,2,2) is possible and creates a multi-
  13728. ; dimensional array of zeros. String arrays are initialized to spaces.
  13729. ; It is not possible to erase an array, but it can be re-dimensioned to
  13730. ; a minimal size of 1, after use, to free up memory.
  13731.  
  13732. ;; DIM
  13733. L2C02:  CALL    L28B2           ; routine LOOK-VARS
  13734.  
  13735. ;; D-RPORT-C
  13736. L2C05:  JP      NZ,L1C8A        ; jump to REPORT-C if a long-name variable.
  13737.                                 ; DIM lottery numbers(49) doesn't work.
  13738.  
  13739.         CALL    L2530           ; routine SYNTAX-Z
  13740.         JR      NZ,L2C15        ; forward to D-RUN in runtime.
  13741.  
  13742.         RES     6,C             ; signal 'numeric' array even if string as
  13743.                                 ; this simplifies the syntax checking.
  13744.  
  13745.         CALL    L2996           ; routine STK-VAR checks syntax.
  13746.         CALL    L1BEE           ; routine CHECK-END performs early exit ->
  13747.  
  13748. ; the branch was here in runtime.
  13749.  
  13750. ;; D-RUN
  13751. L2C15:  JR      C,L2C1F         ; skip to D-LETTER if variable did not exist.
  13752.                                 ; else reclaim the old one.
  13753.  
  13754.         PUSH    BC              ; save type in C.
  13755.         CALL    L19B8           ; routine NEXT-ONE find following variable
  13756.                                 ; or position of $80 end-marker.
  13757.         CALL    L19E8           ; routine RECLAIM-2 reclaims the
  13758.                                 ; space between.
  13759.         POP     BC              ; pop the type.
  13760.  
  13761. ;; D-LETTER
  13762. L2C1F:  SET     7,C             ; signal array.
  13763.         LD      B,$00           ; initialize dimensions to zero and
  13764.         PUSH    BC              ; save with the type.
  13765.         LD      HL,$0001        ; make elements one character presuming string
  13766.         BIT     6,C             ; is it a string ?
  13767.         JR      NZ,L2C2D        ; forward to D-SIZE if so.
  13768.  
  13769.         LD      L,$05           ; make elements 5 bytes as is numeric.
  13770.  
  13771. ;; D-SIZE
  13772. L2C2D:  EX      DE,HL           ; save the element size in DE.
  13773.  
  13774. ; now enter a loop to parse each of the integers in the list.
  13775.  
  13776. ;; D-NO-LOOP
  13777. L2C2E:  RST     20H             ; NEXT-CHAR
  13778.         LD      H,$FF           ; disable limit check by setting HL high
  13779.         CALL    L2ACC           ; routine INT-EXP1
  13780.         JP      C,L2A20         ; to REPORT-3 if > 65280 and then some
  13781.                                 ; 'Subscript out of range'
  13782.  
  13783.         POP     HL              ; pop dimension counter, array type
  13784.         PUSH    BC              ; save dimension size                     ***
  13785.         INC     H               ; increment the dimension counter
  13786.         PUSH    HL              ; save the dimension counter
  13787.         LD      H,B             ; transfer size
  13788.         LD      L,C             ; to HL
  13789.         CALL    L2AF4           ; routine GET-HL*DE multiplies dimension by
  13790.                                 ; running total of size required initially
  13791.                                 ; 1 or 5.
  13792.         EX      DE,HL           ; save running total in DE
  13793.  
  13794.         RST     18H             ; GET-CHAR
  13795.         CP      $2C             ; is it ',' ?
  13796.         JR      Z,L2C2E         ; loop back to D-NO-LOOP until all dimensions
  13797.                                 ; have been considered
  13798.  
  13799. ; when loop complete continue.
  13800.  
  13801.         CP      $29             ; is it ')' ?
  13802.         JR      NZ,L2C05        ; to D-RPORT-C with anything else
  13803.                                 ; 'Nonsense in BASIC'
  13804.  
  13805.  
  13806.         RST     20H             ; NEXT-CHAR advances to next statement/CR
  13807.  
  13808.         POP     BC              ; pop dimension counter/type
  13809.         LD      A,C             ; type to A
  13810.  
  13811. ; now calculate space required for array variable
  13812.  
  13813.         LD      L,B             ; dimensions to L since these require 16 bits
  13814.                                 ; then this value will be doubled
  13815.         LD      H,$00           ; set high byte to zero
  13816.  
  13817. ; another four bytes are required for letter(1), total length(2), number of
  13818. ; dimensions(1) but since we have yet to double allow for two
  13819.  
  13820.         INC     HL              ; increment
  13821.         INC     HL              ; increment
  13822.  
  13823.         ADD     HL,HL           ; now double giving 4 + dimensions * 2
  13824.  
  13825.         ADD     HL,DE           ; add to space required for array contents
  13826.  
  13827.         JP      C,L1F15         ; to REPORT-4 if > 65535
  13828.                                 ; 'Out of memory'
  13829.  
  13830.         PUSH    DE              ; save data space
  13831.         PUSH    BC              ; save dimensions/type
  13832.         PUSH    HL              ; save total space
  13833.         LD      B,H             ; total space
  13834.         LD      C,L             ; to BC
  13835.         LD      HL,($5C59)      ; address E_LINE - first location after
  13836.                                 ; variables area
  13837.         DEC     HL              ; point to location before - the $80 end-marker
  13838.         CALL    L1655           ; routine MAKE-ROOM creates the space if
  13839.                                 ; memory is available.
  13840.  
  13841.         INC     HL              ; point to first new location and
  13842.         LD      (HL),A          ; store letter/type
  13843.  
  13844.         POP     BC              ; pop total space
  13845.         DEC     BC              ; exclude name
  13846.         DEC     BC              ; exclude the 16-bit
  13847.         DEC     BC              ; counter itself
  13848.         INC     HL              ; point to next location the 16-bit counter
  13849.         LD      (HL),C          ; insert low byte
  13850.         INC     HL              ; address next
  13851.         LD      (HL),B          ; insert high byte
  13852.  
  13853.         POP     BC              ; pop the number of dimensions.
  13854.         LD      A,B             ; dimensions to A
  13855.         INC     HL              ; address next
  13856.         LD      (HL),A          ; and insert "No. of dims"
  13857.  
  13858.         LD      H,D             ; transfer DE space + 1 from make-room
  13859.         LD      L,E             ; to HL
  13860.         DEC     DE              ; set DE to next location down.
  13861.         LD      (HL),$00        ; presume numeric and insert a zero
  13862.         BIT     6,C             ; test bit 6 of C. numeric or string ?
  13863.         JR      Z,L2C7C         ; skip to DIM-CLEAR if numeric
  13864.  
  13865.         LD      (HL),$20        ; place a space character in HL
  13866.  
  13867. ;; DIM-CLEAR
  13868. L2C7C:  POP     BC              ; pop the data length
  13869.  
  13870.         LDDR                    ; LDDR sets to zeros or spaces
  13871.  
  13872. ; The number of dimensions is still in A.
  13873. ; A loop is now entered to insert the size of each dimension that was pushed
  13874. ; during the D-NO-LOOP working downwards from position before start of data.
  13875.  
  13876. ;; DIM-SIZES
  13877. L2C7F:  POP     BC              ; pop a dimension size                    ***
  13878.         LD      (HL),B          ; insert high byte at position
  13879.         DEC     HL              ; next location down
  13880.         LD      (HL),C          ; insert low byte
  13881.         DEC     HL              ; next location down
  13882.         DEC     A               ; decrement dimension counter
  13883.         JR      NZ,L2C7F        ; back to DIM-SIZES until all done.
  13884.  
  13885.         RET                     ; return.
  13886.  
  13887. ; -----------------------------
  13888. ; Check whether digit or letter
  13889. ; -----------------------------
  13890. ; This routine checks that the character in A is alphanumeric
  13891. ; returning with carry set if so.
  13892.  
  13893. ;; ALPHANUM
  13894. L2C88:  CALL    L2D1B           ; routine NUMERIC will reset carry if so.
  13895.         CCF                     ; Complement Carry Flag
  13896.         RET     C               ; Return if numeric else continue into
  13897.                                 ; next routine.
  13898.  
  13899. ; This routine checks that the character in A is alphabetic
  13900.  
  13901. ;; ALPHA
  13902. L2C8D:  CP      $41             ; less than 'A' ?
  13903.         CCF                     ; Complement Carry Flag
  13904.         RET     NC              ; return if so
  13905.  
  13906.         CP      $5B             ; less than 'Z'+1 ?
  13907.         RET     C               ; is within first range
  13908.  
  13909.         CP      $61             ; less than 'a' ?
  13910.         CCF                     ; Complement Carry Flag
  13911.         RET     NC              ; return if so.
  13912.  
  13913.         CP      $7B             ; less than 'z'+1 ?
  13914.         RET                     ; carry set if within a-z.
  13915.  
  13916. ; -------------------------
  13917. ; Decimal to floating point
  13918. ; -------------------------
  13919. ; This routine finds the floating point number represented by an expression
  13920. ; beginning with BIN, '.' or a digit.
  13921. ; Note that BIN need not have any '0's or '1's after it.
  13922. ; BIN is really just a notational symbol and not a function.
  13923.  
  13924. ;; DEC-TO-FP
  13925. L2C9B:  CP      $C4             ; 'BIN' token ?
  13926.         JR      NZ,L2CB8        ; to NOT-BIN if not
  13927.  
  13928.         LD      DE,$0000        ; initialize 16 bit buffer register.
  13929.  
  13930. ;; BIN-DIGIT
  13931. L2CA2:  RST     20H             ; NEXT-CHAR
  13932.         SUB     $31             ; '1'
  13933.         ADC     A,$00           ; will be zero if '1' or '0'
  13934.                                 ; carry will be set if was '0'
  13935.         JR      NZ,L2CB3        ; forward to BIN-END if result not zero
  13936.  
  13937.         EX      DE,HL           ; buffer to HL
  13938.         CCF                     ; Carry now set if originally '1'
  13939.         ADC     HL,HL           ; shift the carry into HL
  13940.         JP      C,L31AD         ; to REPORT-6 if overflow - too many digits
  13941.                                 ; after first '1'. There can be an unlimited
  13942.                                 ; number of leading zeros.
  13943.                                 ; 'Number too big' - raise an error
  13944.  
  13945.         EX      DE,HL           ; save the buffer
  13946.         JR      L2CA2           ; back to BIN-DIGIT for more digits
  13947.  
  13948. ; ---
  13949.  
  13950. ;; BIN-END
  13951. L2CB3:  LD      B,D             ; transfer 16 bit buffer
  13952.         LD      C,E             ; to BC register pair.
  13953.         JP      L2D2B           ; JUMP to STACK-BC to put on calculator stack
  13954.  
  13955. ; ---
  13956.  
  13957. ; continue here with .1,  42, 3.14, 5., 2.3 E -4
  13958.  
  13959. ;; NOT-BIN
  13960. L2CB8:  CP      $2E             ; '.' - leading decimal point ?
  13961.         JR      Z,L2CCB         ; skip to DECIMAL if so.
  13962.  
  13963.         CALL    L2D3B           ; routine INT-TO-FP to evaluate all digits
  13964.                                 ; This number 'x' is placed on stack.
  13965.         CP      $2E             ; '.' - mid decimal point ?
  13966.  
  13967.         JR      NZ,L2CEB        ; to E-FORMAT if not to consider that format
  13968.  
  13969.         RST     20H             ; NEXT-CHAR
  13970.         CALL    L2D1B           ; routine NUMERIC returns carry reset if 0-9
  13971.  
  13972.         JR      C,L2CEB         ; to E-FORMAT if not a digit e.g. '1.'
  13973.  
  13974.         JR      L2CD5           ; to DEC-STO-1 to add the decimal part to 'x'
  13975.  
  13976. ; ---
  13977.  
  13978. ; a leading decimal point has been found in a number.
  13979.  
  13980. ;; DECIMAL
  13981. L2CCB:  RST     20H             ; NEXT-CHAR
  13982.         CALL    L2D1B           ; routine NUMERIC will reset carry if digit
  13983.  
  13984. ;; DEC-RPT-C
  13985. L2CCF:  JP      C,L1C8A         ; to REPORT-C if just a '.'
  13986.                                 ; raise 'Nonsense in BASIC'
  13987.  
  13988. ; since there is no leading zero put one on the calculator stack.
  13989.  
  13990.         RST     28H             ;; FP-CALC
  13991.         DB    $A0             ;;stk-zero  ; 0.
  13992.         DB    $38             ;;end-calc
  13993.  
  13994. ; If rejoining from earlier there will be a value 'x' on stack.
  13995. ; If continuing from above the value zero.
  13996. ; Now store 1 in mem-0.
  13997. ; Note. At each pass of the digit loop this will be divided by ten.
  13998.  
  13999. ;; DEC-STO-1
  14000. L2CD5:  RST     28H             ;; FP-CALC
  14001.         DB    $A1             ;;stk-one   ;x or 0,1.
  14002.         DB    $C0             ;;st-mem-0  ;x or 0,1.
  14003.         DB    $02             ;;delete    ;x or 0.
  14004.         DB    $38             ;;end-calc
  14005.  
  14006.  
  14007. ;; NXT-DGT-1
  14008. L2CDA:  RST     18H             ; GET-CHAR
  14009.         CALL    L2D22           ; routine STK-DIGIT stacks single digit 'd'
  14010.         JR      C,L2CEB         ; exit to E-FORMAT when digits exhausted  >
  14011.  
  14012.  
  14013.         RST     28H             ;; FP-CALC   ;x or 0,d.           first pass.
  14014.         DB    $E0             ;;get-mem-0  ;x or 0,d,1.
  14015.         DB    $A4             ;;stk-ten    ;x or 0,d,1,10.
  14016.         DB    $05             ;;division   ;x or 0,d,1/10.
  14017.         DB    $C0             ;;st-mem-0   ;x or 0,d,1/10.
  14018.         DB    $04             ;;multiply   ;x or 0,d/10.
  14019.         DB    $0F             ;;addition   ;x or 0 + d/10.
  14020.         DB    $38             ;;end-calc   last value.
  14021.  
  14022.         RST     20H             ; NEXT-CHAR  moves to next character
  14023.         JR      L2CDA           ; back to NXT-DGT-1
  14024.  
  14025. ; ---
  14026.  
  14027. ; although only the first pass is shown it can be seen that at each pass
  14028. ; the new less significant digit is multiplied by an increasingly smaller
  14029. ; factor (1/100, 1/1000, 1/10000 ... ) before being added to the previous
  14030. ; last value to form a new last value.
  14031.  
  14032. ; Finally see if an exponent has been input.
  14033.  
  14034. ;; E-FORMAT
  14035. L2CEB:  CP      $45             ; is character 'E' ?
  14036.         JR      Z,L2CF2         ; to SIGN-FLAG if so
  14037.  
  14038.         CP      $65             ; 'e' is acceptable as well.
  14039.         RET     NZ              ; return as no exponent.
  14040.  
  14041. ;; SIGN-FLAG
  14042. L2CF2:  LD      B,$FF           ; initialize temporary sign byte to $FF
  14043.  
  14044.         RST     20H             ; NEXT-CHAR
  14045.         CP      $2B             ; is character '+' ?
  14046.         JR      Z,L2CFE         ; to SIGN-DONE
  14047.  
  14048.         CP      $2D             ; is character '-' ?
  14049.         JR      NZ,L2CFF        ; to ST-E-PART as no sign
  14050.  
  14051.         INC     B               ; set sign to zero
  14052.  
  14053. ; now consider digits of exponent.
  14054. ; Note. incidentally this is the only occasion in Spectrum BASIC when an
  14055. ; expression may not be used when a number is expected.
  14056.  
  14057. ;; SIGN-DONE
  14058. L2CFE:  RST     20H             ; NEXT-CHAR
  14059.  
  14060. ;; ST-E-PART
  14061. L2CFF:  CALL    L2D1B           ; routine NUMERIC
  14062.         JR      C,L2CCF         ; to DEC-RPT-C if not
  14063.                                 ; raise 'Nonsense in BASIC'.
  14064.  
  14065.         PUSH    BC              ; save sign (in B)
  14066.         CALL    L2D3B           ; routine INT-TO-FP places exponent on stack
  14067.         CALL    L2DD5           ; routine FP-TO-A  transfers it to A
  14068.         POP     BC              ; restore sign
  14069.         JP      C,L31AD         ; to REPORT-6 if overflow (over 255)
  14070.                                 ; raise 'Number too big'.
  14071.  
  14072.         AND     A               ; set flags
  14073.         JP      M,L31AD         ; to REPORT-6 if over '127'.
  14074.                                 ; raise 'Number too big'.
  14075.                                 ; 127 is still way too high and it is
  14076.                                 ; impossible to enter an exponent greater
  14077.                                 ; than 39 from the keyboard. The error gets
  14078.                                 ; raised later in E-TO-FP so two different
  14079.                                 ; error messages depending how high A is.
  14080.  
  14081.         INC     B               ; $FF to $00 or $00 to $01 - expendable now.
  14082.         JR      Z,L2D18         ; forward to E-FP-JUMP if exponent positive
  14083.  
  14084.         NEG                     ; Negate the exponent.
  14085.  
  14086. ;; E-FP-JUMP
  14087. L2D18:  JP      L2D4F           ; JUMP forward to E-TO-FP to assign to
  14088.                                 ; last value x on stack x * 10 to power A
  14089.                                 ; a relative jump would have done.
  14090.  
  14091. ; ---------------------
  14092. ; Check for valid digit
  14093. ; ---------------------
  14094. ; This routine checks that the ASCII character in A is numeric
  14095. ; returning with carry reset if so.
  14096.  
  14097. ;; NUMERIC
  14098. L2D1B:  CP      $30             ; '0'
  14099.         RET     C               ; return if less than zero character.
  14100.  
  14101.         CP      $3A             ; The upper test is '9'
  14102.         CCF                     ; Complement Carry Flag
  14103.         RET                     ; Return - carry clear if character '0' - '9'
  14104.  
  14105. ; -----------
  14106. ; Stack Digit
  14107. ; -----------
  14108. ; This subroutine is called from INT-TO-FP and DEC-TO-FP to stack a digit
  14109. ; on the calculator stack.
  14110.  
  14111. ;; STK-DIGIT
  14112. L2D22:  CALL    L2D1B           ; routine NUMERIC
  14113.         RET     C               ; return if not numeric character
  14114.  
  14115.         SUB     $30             ; convert from ASCII to digit
  14116.  
  14117. ; -----------------
  14118. ; Stack accumulator
  14119. ; -----------------
  14120. ;
  14121. ;
  14122.  
  14123. ;; STACK-A
  14124. L2D28:  LD      C,A             ; transfer to C
  14125.         LD      B,$00           ; and make B zero
  14126.  
  14127. ; ----------------------
  14128. ; Stack BC register pair
  14129. ; ----------------------
  14130. ;
  14131.  
  14132. ;; STACK-BC
  14133. L2D2B:  LD      IY,$5C3A        ; re-initialize ERR_NR
  14134.  
  14135.         XOR     A               ; clear to signal small integer
  14136.         LD      E,A             ; place in E for sign
  14137.         LD      D,C             ; LSB to D
  14138.         LD      C,B             ; MSB to C
  14139.         LD      B,A             ; last byte not used
  14140.         CALL    L2AB6           ; routine STK-STORE
  14141.  
  14142.         RST     28H             ;; FP-CALC
  14143.         DB    $38             ;;end-calc  make HL = STKEND-5
  14144.  
  14145.         AND     A               ; clear carry
  14146.         RET                     ; before returning
  14147.  
  14148. ; -------------------------
  14149. ; Integer to floating point
  14150. ; -------------------------
  14151. ; This routine places one or more digits found in a BASIC line
  14152. ; on the calculator stack multiplying the previous value by ten each time
  14153. ; before adding in the new digit to form a last value on calculator stack.
  14154.  
  14155. ;; INT-TO-FP
  14156. L2D3B:  PUSH    AF              ; save first character
  14157.  
  14158.         RST     28H             ;; FP-CALC
  14159.         DB    $A0             ;;stk-zero    ; v=0. initial value
  14160.         DB    $38             ;;end-calc
  14161.  
  14162.         POP     AF              ; fetch first character back.
  14163.  
  14164. ;; NXT-DGT-2
  14165. L2D40:  CALL    L2D22           ; routine STK-DIGIT puts 0-9 on stack
  14166.         RET     C               ; will return when character is not numeric >
  14167.  
  14168.         RST     28H             ;; FP-CALC    ; v, d.
  14169.         DB    $01             ;;exchange    ; d, v.
  14170.         DB    $A4             ;;stk-ten     ; d, v, 10.
  14171.         DB    $04             ;;multiply    ; d, v*10.
  14172.         DB    $0F             ;;addition    ; d + v*10 = newvalue
  14173.         DB    $38             ;;end-calc    ; v.
  14174.  
  14175.         CALL    L0074           ; routine CH-ADD+1 get next character
  14176.         JR      L2D40           ; back to NXT-DGT-2 to process as a digit
  14177.  
  14178.  
  14179. ;*********************************
  14180. ;** Part 9. ARITHMETIC ROUTINES **
  14181. ;*********************************
  14182.  
  14183. ; --------------------------
  14184. ; E-format to floating point
  14185. ; --------------------------
  14186. ; This subroutine is used by the PRINT-FP routine and the decimal to FP
  14187. ; routines to stack a number expressed in exponent format.
  14188. ; Note. Though not used by the ROM as such, it has also been set up as
  14189. ; a unary calculator literal but this will not work as the accumulator
  14190. ; is not available from within the calculator.
  14191.  
  14192. ; on entry there is a value x on the calculator stack and an exponent of ten
  14193. ; in A.    The required value is x + 10 ^ A
  14194.  
  14195. ;; e-to-fp
  14196. ;; E-TO-FP
  14197. L2D4F:  RLCA                    ; this will set the          x.
  14198.         RRCA                    ; carry if bit 7 is set
  14199.  
  14200.         JR      NC,L2D55        ; to E-SAVE  if positive.
  14201.  
  14202.         CPL                     ; make negative positive
  14203.         INC     A               ; without altering carry.
  14204.  
  14205. ;; E-SAVE
  14206. L2D55:  PUSH    AF              ; save positive exp and sign in carry
  14207.  
  14208.         LD      HL,$5C92        ; address MEM-0
  14209.  
  14210.         CALL    L350B           ; routine FP-0/1
  14211.                                 ; places an integer zero, if no carry,
  14212.                                 ; else a one in mem-0 as a sign flag
  14213.  
  14214.         RST     28H             ;; FP-CALC
  14215.         DB    $A4             ;;stk-ten                    x, 10.
  14216.         DB    $38             ;;end-calc
  14217.  
  14218.         POP     AF              ; pop the exponent.
  14219.  
  14220. ; now enter a loop
  14221.  
  14222. ;; E-LOOP
  14223. L2D60:  SRL     A               ; 0>76543210>C
  14224.  
  14225.         JR      NC,L2D71        ; forward to E-TST-END if no bit
  14226.  
  14227.         PUSH    AF              ; save shifted exponent.
  14228.  
  14229.         RST     28H             ;; FP-CALC
  14230.         DB    $C1             ;;st-mem-1                   x, 10.
  14231.         DB    $E0             ;;get-mem-0                  x, 10, (0/1).
  14232.         DB    $00             ;;jump-true
  14233.  
  14234.         DB    $04             ;;to L2D6D, E-DIVSN
  14235.  
  14236.         DB    $04             ;;multiply                   x*10.
  14237.         DB    $33             ;;jump
  14238.  
  14239.         DB    $02             ;;to L2D6E, E-FETCH
  14240.  
  14241. ;; E-DIVSN
  14242. L2D6D:  DB    $05             ;;division                   x/10.
  14243.  
  14244. ;; E-FETCH
  14245. L2D6E:  DB    $E1             ;;get-mem-1                  x/10 or x*10, 10.
  14246.         DB    $38             ;;end-calc                   new x, 10.
  14247.  
  14248.         POP     AF              ; restore shifted exponent
  14249.  
  14250. ; the loop branched to here with no carry
  14251.  
  14252. ;; E-TST-END
  14253. L2D71:  JR      Z,L2D7B         ; forward to E-END  if A emptied of bits
  14254.  
  14255.         PUSH    AF              ; re-save shifted exponent
  14256.  
  14257.         RST     28H             ;; FP-CALC
  14258.         DB    $31             ;;duplicate                  new x, 10, 10.
  14259.         DB    $04             ;;multiply                   new x, 100.
  14260.         DB    $38             ;;end-calc
  14261.  
  14262.         POP     AF              ; restore shifted exponent
  14263.         JR      L2D60           ; back to E-LOOP  until all bits done.
  14264.  
  14265. ; ---
  14266.  
  14267. ; although only the first pass is shown it can be seen that for each set bit
  14268. ; representing a power of two, x is multiplied or divided by the
  14269. ; corresponding power of ten.
  14270.  
  14271. ;; E-END
  14272. L2D7B:  RST     28H             ;; FP-CALC                   final x, factor.
  14273.         DB    $02             ;;delete                     final x.
  14274.         DB    $38             ;;end-calc                   x.
  14275.  
  14276.         RET                     ; return
  14277.  
  14278.  
  14279.  
  14280.  
  14281. ; -------------
  14282. ; Fetch integer
  14283. ; -------------
  14284. ; This routine is called by the mathematical routines - FP-TO-BC, PRINT-FP,
  14285. ; mult, re-stack and negate to fetch an integer from address HL.
  14286. ; HL points to the stack or a location in MEM and no deletion occurs.
  14287. ; If the number is negative then a similar process to that used in INT-STORE
  14288. ; is used to restore the twos complement number to normal in DE and a sign
  14289. ; in C.
  14290.  
  14291. ;; INT-FETCH
  14292. L2D7F:  INC     HL              ; skip zero indicator.
  14293.         LD      C,(HL)          ; fetch sign to C
  14294.         INC     HL              ; address low byte
  14295.         LD      A,(HL)          ; fetch to A
  14296.         XOR     C               ; two's complement
  14297.         SUB     C               ;
  14298.         LD      E,A             ; place in E
  14299.         INC     HL              ; address high byte
  14300.         LD      A,(HL)          ; fetch to A
  14301.         ADC     A,C             ; two's complement
  14302.         XOR     C               ;
  14303.         LD      D,A             ; place in D
  14304.         RET                     ; return
  14305.  
  14306. ; ------------------------
  14307. ; Store a positive integer
  14308. ; ------------------------
  14309. ; This entry point is not used in this ROM but would
  14310. ; store any integer as positive.
  14311.  
  14312. ;; p-int-sto
  14313. L2D8C:  LD      C,$00           ; make sign byte positive and continue
  14314.  
  14315. ; -------------
  14316. ; Store integer
  14317. ; -------------
  14318. ; this routine stores an integer in DE at address HL.
  14319. ; It is called from mult, truncate, negate and sgn.
  14320. ; The sign byte $00 +ve or $FF -ve is in C.
  14321. ; If negative, the number is stored in 2's complement form so that it is
  14322. ; ready to be added.
  14323.  
  14324. ;; INT-STORE
  14325. L2D8E:  PUSH    HL              ; preserve HL
  14326.  
  14327.         LD      (HL),$00        ; first byte zero shows integer not exponent
  14328.         INC     HL              ;
  14329.         LD      (HL),C          ; then store the sign byte
  14330.         INC     HL              ;
  14331.                                 ; e.g.             +1             -1
  14332.         LD      A,E             ; fetch low byte   00000001       00000001
  14333.         XOR     C               ; xor sign         00000000   or  11111111
  14334.                                 ; gives            00000001   or  11111110
  14335.         SUB     C               ; sub sign         00000000   or  11111111
  14336.                                 ; gives            00000001>0 or  11111111>C
  14337.         LD      (HL),A          ; store 2's complement.
  14338.         INC     HL              ;
  14339.         LD      A,D             ; high byte        00000000       00000000
  14340.         ADC     A,C             ; sign             00000000<0     11111111<C
  14341.                                 ; gives            00000000   or  00000000
  14342.         XOR     C               ; xor sign         00000000       11111111
  14343.         LD      (HL),A          ; store 2's complement.
  14344.         INC     HL              ;
  14345.         LD      (HL),$00        ; last byte always zero for integers.
  14346.                                 ; is not used and need not be looked at when
  14347.                                 ; testing for zero but comes into play should
  14348.                                 ; an integer be converted to fp.
  14349.         POP     HL              ; restore HL
  14350.         RET                     ; return.
  14351.  
  14352.  
  14353. ; -----------------------------
  14354. ; Floating point to BC register
  14355. ; -----------------------------
  14356. ; This routine gets a floating point number e.g. 127.4 from the calculator
  14357. ; stack to the BC register.
  14358.  
  14359. ;; FP-TO-BC
  14360. L2DA2:  RST     28H             ;; FP-CALC            set HL to
  14361.         DB    $38             ;;end-calc            point to last value.
  14362.  
  14363.         LD      A,(HL)          ; get first of 5 bytes
  14364.         AND     A               ; and test
  14365.         JR      Z,L2DAD         ; forward to FP-DELETE if an integer
  14366.  
  14367. ; The value is first rounded up and then converted to integer.
  14368.  
  14369.         RST     28H             ;; FP-CALC           x.
  14370.         DB    $A2             ;;stk-half           x. 1/2.
  14371.         DB    $0F             ;;addition           x + 1/2.
  14372.         DB    $27             ;;int                int(x + .5)
  14373.         DB    $38             ;;end-calc
  14374.  
  14375. ; now delete but leave HL pointing at integer
  14376.  
  14377. ;; FP-DELETE
  14378. L2DAD:  RST     28H             ;; FP-CALC
  14379.         DB    $02             ;;delete
  14380.         DB    $38             ;;end-calc
  14381.  
  14382.         PUSH    HL              ; save pointer.
  14383.         PUSH    DE              ; and STKEND.
  14384.         EX      DE,HL           ; make HL point to exponent/zero indicator
  14385.         LD      B,(HL)          ; indicator to B
  14386.         CALL    L2D7F           ; routine INT-FETCH
  14387.                                 ; gets int in DE sign byte to C
  14388.                                 ; but meaningless values if a large integer
  14389.  
  14390.         XOR     A               ; clear A
  14391.         SUB     B               ; subtract indicator byte setting carry
  14392.                                 ; if not a small integer.
  14393.  
  14394.         BIT     7,C             ; test a bit of the sign byte setting zero
  14395.                                 ; if positive.
  14396.  
  14397.         LD      B,D             ; transfer int
  14398.         LD      C,E             ; to BC
  14399.         LD      A,E             ; low byte to A as a useful return value.
  14400.  
  14401.         POP     DE              ; pop STKEND
  14402.         POP     HL              ; and pointer to last value
  14403.         RET                     ; return
  14404.                                 ; if carry is set then the number was too big.
  14405.  
  14406. ; ------------
  14407. ; LOG(2^A)
  14408. ; ------------
  14409. ; This routine is used when printing floating point numbers to calculate
  14410. ; the number of digits before the decimal point.
  14411.  
  14412. ; first convert a one-byte signed integer to its five byte form.
  14413.  
  14414. ;; LOG(2^A)
  14415. L2DC1:  LD      D,A             ; store a copy of A in D.
  14416.         RLA                     ; test sign bit of A.
  14417.         SBC     A,A             ; now $FF if negative or $00
  14418.         LD      E,A             ; sign byte to E.
  14419.         LD      C,A             ; and to C
  14420.         XOR     A               ; clear A
  14421.         LD      B,A             ; and B.
  14422.         CALL    L2AB6           ; routine STK-STORE stacks number AEDCB
  14423.  
  14424. ;  so 00 00 XX 00 00 (positive) or 00 FF XX FF 00 (negative).
  14425. ;  i.e. integer indicator, sign byte, low, high, unused.
  14426.  
  14427. ; now multiply exponent by log to the base 10 of two.
  14428.  
  14429.         RST      28H            ;; FP-CALC
  14430.  
  14431.         DB    $34             ;;stk-data                      .30103 (log 2)
  14432.         DB    $EF             ;;Exponent: $7F, Bytes: 4
  14433.         DB    $1A,$20,$9A,$85 ;;
  14434.         DB    $04             ;;multiply
  14435.  
  14436.         DB    $27             ;;int
  14437.  
  14438.         DB    $38             ;;end-calc
  14439.  
  14440. ; -------------------
  14441. ; Floating point to A
  14442. ; -------------------
  14443. ; this routine collects a floating point number from the stack into the
  14444. ; accumulator returning carry set if not in range 0 - 255.
  14445. ; Not all the calling routines raise an error with overflow so no attempt
  14446. ; is made to produce an error report here.
  14447.  
  14448. ;; FP-TO-A
  14449. L2DD5:  CALL    L2DA2           ; routine FP-TO-BC returns with C in A also.
  14450.         RET     C               ; return with carry set if > 65535, overflow
  14451.  
  14452.         PUSH    AF              ; save the value and flags
  14453.         DEC     B               ; and test that
  14454.         INC     B               ; the high byte is zero.
  14455.         JR      Z,L2DE1         ; forward  FP-A-END if zero
  14456.  
  14457. ; else there has been 8-bit overflow
  14458.  
  14459.         POP     AF              ; retrieve the value
  14460.         SCF                     ; set carry flag to show overflow
  14461.         RET                     ; and return.
  14462.  
  14463. ; ---
  14464.  
  14465. ;; FP-A-END
  14466. L2DE1:  POP     AF              ; restore value and success flag and
  14467.         RET                     ; return.
  14468.  
  14469.  
  14470. ; -----------------------------
  14471. ; Print a floating point number
  14472. ; -----------------------------
  14473. ; Not a trivial task.
  14474. ; Begin by considering whether to print a leading sign for negative numbers.
  14475.  
  14476. ;; PRINT-FP
  14477. L2DE3:  RST     28H             ;; FP-CALC
  14478.         DB    $31             ;;duplicate
  14479.         DB    $36             ;;less-0
  14480.         DB    $00             ;;jump-true
  14481.  
  14482.         DB    $0B             ;;to L2DF2, PF-NEGTVE
  14483.  
  14484.         DB    $31             ;;duplicate
  14485.         DB    $37             ;;greater-0
  14486.         DB    $00             ;;jump-true
  14487.  
  14488.         DB    $0D             ;;to L2DF8, PF-POSTVE
  14489.  
  14490. ; must be zero itself
  14491.  
  14492.         DB    $02             ;;delete
  14493.         DB    $38             ;;end-calc
  14494.  
  14495.         LD      A,$30           ; prepare the character '0'
  14496.  
  14497.         RST     10H             ; PRINT-A
  14498.         RET                     ; return.                 ->
  14499. ; ---
  14500.  
  14501. ;; PF-NEGTVE
  14502. L2DF2:  DB    $2A             ;;abs
  14503.         DB    $38             ;;end-calc
  14504.  
  14505.         LD      A,$2D           ; the character '-'
  14506.  
  14507.         RST     10H             ; PRINT-A
  14508.  
  14509. ; and continue to print the now positive number.
  14510.  
  14511.         RST     28H             ;; FP-CALC
  14512.  
  14513. ;; PF-POSTVE
  14514. L2DF8:  DB    $A0             ;;stk-zero     x,0.     begin by
  14515.         DB    $C3             ;;st-mem-3     x,0.     clearing a temporary
  14516.         DB    $C4             ;;st-mem-4     x,0.     output buffer to
  14517.         DB    $C5             ;;st-mem-5     x,0.     fifteen zeros.
  14518.         DB    $02             ;;delete       x.
  14519.         DB    $38             ;;end-calc     x.
  14520.  
  14521.         EXX                     ; in case called from 'str$' then save the
  14522.         PUSH    HL              ; pointer to whatever comes after
  14523.         EXX                     ; str$ as H'L' will be used.
  14524.  
  14525. ; now enter a loop?
  14526.  
  14527. ;; PF-LOOP
  14528. L2E01:  RST     28H             ;; FP-CALC
  14529.         DB    $31             ;;duplicate    x,x.
  14530.         DB    $27             ;;int          x,int x.
  14531.         DB    $C2             ;;st-mem-2     x,int x.
  14532.         DB    $03             ;;subtract     x-int x.     fractional part.
  14533.         DB    $E2             ;;get-mem-2    x-int x, int x.
  14534.         DB    $01             ;;exchange     int x, x-int x.
  14535.         DB    $C2             ;;st-mem-2     int x, x-int x.
  14536.         DB    $02             ;;delete       int x.
  14537.         DB    $38             ;;end-calc     int x.
  14538.                                 ;
  14539.                                 ; mem-2 holds the fractional part.
  14540.  
  14541. ; HL points to last value int x
  14542.  
  14543.         LD      A,(HL)          ; fetch exponent of int x.
  14544.         AND     A               ; test
  14545.         JR      NZ,L2E56        ; forward to PF-LARGE if a large integer
  14546.                                 ; > 65535
  14547.  
  14548. ; continue with small positive integer components in range 0 - 65535
  14549. ; if original number was say .999 then this integer component is zero.
  14550.  
  14551.         CALL    L2D7F           ; routine INT-FETCH gets x in DE
  14552.                                 ; (but x is not deleted)
  14553.  
  14554.         LD      B,$10           ; set B, bit counter, to 16d
  14555.  
  14556.         LD      A,D             ; test if
  14557.         AND     A               ; high byte is zero
  14558.         JR      NZ,L2E1E        ; forward to PF-SAVE if 16-bit integer.
  14559.  
  14560. ; and continue with integer in range 0 - 255.
  14561.  
  14562.         OR      E               ; test the low byte for zero
  14563.                                 ; i.e. originally just point something or other.
  14564.         JR      Z,L2E24         ; forward if so to PF-SMALL
  14565.  
  14566. ;
  14567.  
  14568.         LD      D,E             ; transfer E to D
  14569.         LD      B,$08           ; and reduce the bit counter to 8.
  14570.  
  14571. ;; PF-SAVE
  14572. L2E1E:  PUSH    DE              ; save the part before decimal point.
  14573.         EXX                     ;
  14574.         POP     DE              ; and pop in into D'E'
  14575.         EXX                     ;
  14576.         JR      L2E7B           ; forward to PF-BITS
  14577.  
  14578. ; ---------------------
  14579.  
  14580. ; the branch was here when 'int x' was found to be zero as in say 0.5.
  14581. ; The zero has been fetched from the calculator stack but not deleted and
  14582. ; this should occur now. This omission leaves the stack unbalanced and while
  14583. ; that causes no problems with a simple PRINT statement, it will if str$ is
  14584. ; being used in an expression e.g. "2" + STR$ 0.5 gives the result "0.5"
  14585. ; instead of the expected result "20.5".
  14586. ; credit Tony Stratton, 1982.
  14587. ; A DB 02 delete is required immediately on using the calculator.
  14588.  
  14589. ;; PF-SMALL
  14590. L2E24:  RST     28H             ;; FP-CALC       int x = 0.
  14591. L2E25:  DB    $E2             ;;get-mem-2      int x = 0, x-int x.
  14592.         DB    $38             ;;end-calc
  14593.  
  14594.         LD      A,(HL)          ; fetch exponent of positive fractional number
  14595.         SUB     $7E             ; subtract
  14596.  
  14597.         CALL    L2DC1           ; routine LOG(2^A) calculates leading digits.
  14598.  
  14599.         LD      D,A             ; transfer count to D
  14600.         LD      A,($5CAC)       ; fetch total MEM-5-1
  14601.         SUB     D               ;
  14602.         LD      ($5CAC),A       ; MEM-5-1
  14603.         LD      A,D             ;
  14604.         CALL    L2D4F           ; routine E-TO-FP
  14605.  
  14606.         RST     28H             ;; FP-CALC
  14607.         DB    $31             ;;duplicate
  14608.         DB    $27             ;;int
  14609.         DB    $C1             ;;st-mem-1
  14610.         DB    $03             ;;subtract
  14611.         DB    $E1             ;;get-mem-1
  14612.         DB    $38             ;;end-calc
  14613.  
  14614.         CALL    L2DD5           ; routine FP-TO-A
  14615.  
  14616.         PUSH    HL              ; save HL
  14617.         LD      ($5CA1),A       ; MEM-3-1
  14618.         DEC     A               ;
  14619.         RLA                     ;
  14620.         SBC     A,A             ;
  14621.         INC     A               ;
  14622.  
  14623.         LD      HL,$5CAB        ; address MEM-5-1 leading digit counter
  14624.         LD      (HL),A          ; store counter
  14625.         INC     HL              ; address MEM-5-2 total digits
  14626.         ADD     A,(HL)          ; add counter to contents
  14627.         LD      (HL),A          ; and store updated value
  14628.         POP     HL              ; restore HL
  14629.  
  14630.         JP      L2ECF           ; JUMP forward to PF-FRACTN
  14631.  
  14632. ; ---
  14633.  
  14634. ; Note. while it would be pedantic to comment on every occasion a JP
  14635. ; instruction could be replaced with a JR instruction, this applies to the
  14636. ; above, which is useful if you wish to correct the unbalanced stack error
  14637. ; by inserting a 'DB 02 delete' at L2E25, and maintain main addresses.
  14638.  
  14639. ; the branch was here with a large positive integer > 65535 e.g. 123456789
  14640. ; the accumulator holds the exponent.
  14641.  
  14642. ;; PF-LARGE
  14643. L2E56:  SUB     $80             ; make exponent positive
  14644.         CP      $1C             ; compare to 28
  14645.         JR      C,L2E6F         ; to PF-MEDIUM if integer <= 2^27
  14646.  
  14647.         CALL    L2DC1           ; routine LOG(2^A)
  14648.         SUB     $07             ;
  14649.         LD      B,A             ;
  14650.         LD      HL,$5CAC        ; address MEM-5-1 the leading digits counter.
  14651.         ADD     A,(HL)          ; add A to contents
  14652.         LD      (HL),A          ; store updated value.
  14653.         LD      A,B             ;
  14654.         NEG                     ; negate
  14655.         CALL    L2D4F           ; routine E-TO-FP
  14656.         JR      L2E01           ; back to PF-LOOP
  14657.  
  14658. ; ----------------------------
  14659.  
  14660. ;; PF-MEDIUM
  14661. L2E6F:  EX      DE,HL           ;
  14662.         CALL    L2FBA           ; routine FETCH-TWO
  14663.         EXX                     ;
  14664.         SET     7,D             ;
  14665.         LD      A,L             ;
  14666.         EXX                     ;
  14667.         SUB     $80             ;
  14668.         LD      B,A             ;
  14669.  
  14670. ; the branch was here to handle bits in DE with 8 or 16 in B  if small int
  14671. ; and integer in D'E', 6 nibbles will accommodate 065535 but routine does
  14672. ; 32-bit numbers as well from above
  14673.  
  14674. ;; PF-BITS
  14675. L2E7B:  SLA     E               ;  C<xxxxxxxx<0
  14676.         RL      D               ;  C<xxxxxxxx<C
  14677.         EXX                     ;
  14678.         RL      E               ;  C<xxxxxxxx<C
  14679.         RL      D               ;  C<xxxxxxxx<C
  14680.         EXX                     ;
  14681.  
  14682.         LD      HL,$5CAA        ; set HL to mem-4-5th last byte of buffer
  14683.         LD      C,$05           ; set byte count to 5 -  10 nibbles
  14684.  
  14685. ;; PF-BYTES
  14686. L2E8A:  LD      A,(HL)          ; fetch 0 or prev value
  14687.         ADC     A,A             ; shift left add in carry    C<xxxxxxxx<C
  14688.  
  14689.         DAA                     ; Decimal Adjust Accumulator.
  14690.                                 ; if greater than 9 then the left hand
  14691.                                 ; nibble is incremented. If greater than
  14692.                                 ; 99 then adjusted and carry set.
  14693.                                 ; so if we'd built up 7 and a carry came in
  14694.                                 ;      0000 0111 < C
  14695.                                 ;      0000 1111
  14696.                                 ; daa     1 0101  which is 15 in BCD
  14697.  
  14698.         LD      (HL),A          ; put back
  14699.         DEC     HL              ; work down thru mem 4
  14700.         DEC     C               ; decrease the 5 counter.
  14701.         JR      NZ,L2E8A        ; back to PF-BYTES until the ten nibbles rolled
  14702.  
  14703.         DJNZ    L2E7B           ; back to PF-BITS until 8 or 16 (or 32) done
  14704.  
  14705. ; at most 9 digits for 32-bit number will have been loaded with digits
  14706. ; each of the 9 nibbles in mem 4 is placed into ten bytes in mem-3 and mem 4
  14707. ; unless the nibble is zero as the buffer is already zero.
  14708. ; ( or in the case of mem-5 will become zero as a result of RLD instruction )
  14709.  
  14710.         XOR     A               ; clear to accept
  14711.         LD      HL,$5CA6        ; address MEM-4-0 byte destination.
  14712.         LD      DE,$5CA1        ; address MEM-3-0 nibble source.
  14713.         LD      B,$09           ; the count is 9 (not ten) as the first
  14714.                                 ; nibble is known to be blank.
  14715.  
  14716.         RLD                     ; shift RH nibble to left in (HL)
  14717.                                 ;    A           (HL)
  14718.                                 ; 0000 0000 < 0000 3210
  14719.                                 ; 0000 0000   3210 0000
  14720.                                 ; A picks up the blank nibble
  14721.  
  14722.  
  14723.         LD      C,$FF           ; set a flag to indicate when a significant
  14724.                                 ; digit has been encountered.
  14725.  
  14726. ;; PF-DIGITS
  14727. L2EA1:  RLD                     ; pick up leftmost nibble from (HL)
  14728.                                 ;    A           (HL)
  14729.                                 ; 0000 0000 < 7654 3210
  14730.                                 ; 0000 7654   3210 0000
  14731.  
  14732.  
  14733.         JR      NZ,L2EA9        ; to PF-INSERT if non-zero value picked up.
  14734.  
  14735.         DEC     C               ; test
  14736.         INC     C               ; flag
  14737.         JR      NZ,L2EB3        ; skip forward to PF-TEST-2 if flag still $FF
  14738.                                 ; indicating this is a leading zero.
  14739.  
  14740. ; but if the zero is a significant digit e.g. 10 then include in digit totals.
  14741. ; the path for non-zero digits rejoins here.
  14742.  
  14743. ;; PF-INSERT
  14744. L2EA9:  LD      (DE),A          ; insert digit at destination
  14745.         INC     DE              ; increase the destination pointer
  14746.         INC     (IY+$71)        ; increment MEM-5-1st  digit counter
  14747.         INC     (IY+$72)        ; increment MEM-5-2nd  leading digit counter
  14748.         LD      C,$00           ; set flag to zero indicating that any
  14749.                                 ; subsequent zeros are significant and not
  14750.                                 ; leading.
  14751.  
  14752. ;; PF-TEST-2
  14753. L2EB3:  BIT     0,B             ; test if the nibble count is even
  14754.         JR      Z,L2EB8         ; skip to PF-ALL-9 if so to deal with the
  14755.                                 ; other nibble in the same byte
  14756.  
  14757.         INC     HL              ; point to next source byte if not
  14758.  
  14759. ;; PF-ALL-9
  14760. L2EB8:  DJNZ    L2EA1           ; decrement the nibble count, back to PF-DIGITS
  14761.                                 ; if all nine not done.
  14762.  
  14763. ; For 8-bit integers there will be at most 3 digits.
  14764. ; For 16-bit integers there will be at most 5 digits.
  14765. ; but for larger integers there could be nine leading digits.
  14766. ; if nine digits complete then the last one is rounded up as the number will
  14767. ; be printed using E-format notation
  14768.  
  14769.         LD      A,($5CAB)       ; fetch digit count from MEM-5-1st
  14770.         SUB     $09             ; subtract 9 - max possible
  14771.         JR      C,L2ECB         ; forward if less to PF-MORE
  14772.  
  14773.         DEC     (IY+$71)        ; decrement digit counter MEM-5-1st to 8
  14774.         LD      A,$04           ; load A with the value 4.
  14775.         CP      (IY+$6F)        ; compare with MEM-4-4th - the ninth digit
  14776.         JR      L2F0C           ; forward to PF-ROUND
  14777.                                 ; to consider rounding.
  14778.  
  14779. ; ---------------------------------------
  14780.  
  14781. ; now delete int x from calculator stack and fetch fractional part.
  14782.  
  14783. ;; PF-MORE
  14784. L2ECB:  RST     28H             ;; FP-CALC        int x.
  14785.         DB    $02             ;;delete          .
  14786.         DB    $E2             ;;get-mem-2       x - int x = f.
  14787.         DB    $38             ;;end-calca       f.
  14788.  
  14789. ;; PF-FRACTN
  14790. L2ECF:  EX      DE,HL           ;
  14791.         CALL    L2FBA           ; routine FETCH-TWO
  14792.         EXX                     ;
  14793.         LD      A,$80           ;
  14794.         SUB     L               ;
  14795.         LD      L,$00           ;
  14796.         SET     7,D             ;
  14797.         EXX                     ;
  14798.         CALL    L2FDD           ; routine SHIFT-FP
  14799.  
  14800. ;; PF-FRN-LP
  14801. L2EDF:  LD      A,(IY+$71)      ; MEM-5-1st
  14802.         CP      $08             ;
  14803.         JR      C,L2EEC         ; to PF-FR-DGT
  14804.  
  14805.         EXX                     ;
  14806.         RL      D               ;
  14807.         EXX                     ;
  14808.         JR      L2F0C           ; to PF-ROUND
  14809.  
  14810. ; ---
  14811.  
  14812. ;; PF-FR-DGT
  14813. L2EEC:  LD      BC,$0200        ;
  14814.  
  14815. ;; PF-FR-EXX
  14816. L2EEF:  LD      A,E             ;
  14817.         CALL    L2F8B           ; routine CA-10*A+C
  14818.         LD      E,A             ;
  14819.         LD      A,D             ;
  14820.         CALL    L2F8B           ; routine CA-10*A+C
  14821.         LD      D,A             ;
  14822.         PUSH    BC              ;
  14823.         EXX                     ;
  14824.         POP     BC              ;
  14825.         DJNZ    L2EEF           ; to PF-FR-EXX
  14826.  
  14827.         LD      HL,$5CA1        ; MEM-3
  14828.         LD      A,C             ;
  14829.         LD      C,(IY+$71)      ; MEM-5-1st
  14830.         ADD     HL,BC           ;
  14831.         LD      (HL),A          ;
  14832.         INC     (IY+$71)        ; MEM-5-1st
  14833.         JR      L2EDF           ; to PF-FRN-LP
  14834.  
  14835. ; ----------------
  14836.  
  14837. ; 1) with 9 digits but 8 in mem-5-1 and A holding 4, carry set if rounding up.
  14838. ; e.g.
  14839. ;      999999999 is printed as 1E+9
  14840. ;      100000001 is printed as 1E+8
  14841. ;      100000009 is printed as 1.0000001E+8
  14842.  
  14843. ;; PF-ROUND
  14844. L2F0C:  PUSH    AF              ; save A and flags
  14845.         LD      HL,$5CA1        ; address MEM-3 start of digits
  14846.         LD      C,(IY+$71)      ; MEM-5-1st No. of digits to C
  14847.         LD      B,$00           ; prepare to add
  14848.         ADD     HL,BC           ; address last digit + 1
  14849.         LD      B,C             ; No. of digits to B counter
  14850.         POP     AF              ; restore A and carry flag from comparison.
  14851.  
  14852. ;; PF-RND-LP
  14853. L2F18:  DEC     HL              ; address digit at rounding position.
  14854.         LD      A,(HL)          ; fetch it
  14855.         ADC     A,$00           ; add carry from the comparison
  14856.         LD      (HL),A          ; put back result even if $0A.
  14857.         AND     A               ; test A
  14858.         JR      Z,L2F25         ; skip to PF-R-BACK if ZERO?
  14859.  
  14860.         CP      $0A             ; compare to 'ten' - overflow
  14861.         CCF                     ; complement carry flag so that set if ten.
  14862.         JR      NC,L2F2D        ; forward to PF-COUNT with 1 - 9.
  14863.  
  14864. ;; PF-R-BACK
  14865. L2F25:  DJNZ    L2F18           ; loop back to PF-RND-LP
  14866.  
  14867. ; if B counts down to zero then we've rounded right back as in 999999995.
  14868. ; and the first 8 locations all hold $0A.
  14869.  
  14870.  
  14871.         LD      (HL),$01        ; load first location with digit 1.
  14872.         INC     B               ; make B hold 1 also.
  14873.                                 ; could save an instruction byte here.
  14874.         INC     (IY+$72)        ; make MEM-5-2nd hold 1.
  14875.                                 ; and proceed to initialize total digits to 1.
  14876.  
  14877. ;; PF-COUNT
  14878. L2F2D:  LD      (IY+$71),B      ; MEM-5-1st
  14879.  
  14880. ; now balance the calculator stack by deleting  it
  14881.  
  14882.         RST     28H             ;; FP-CALC
  14883.         DB    $02             ;;delete
  14884.         DB    $38             ;;end-calc
  14885.  
  14886. ; note if used from str$ then other values may be on the calculator stack.
  14887. ; we can also restore the next literal pointer from its position on the
  14888. ; machine stack.
  14889.  
  14890.         EXX                     ;
  14891.         POP     HL              ; restore next literal pointer.
  14892.         EXX                     ;
  14893.  
  14894.         LD      BC,($5CAB)      ; set C to MEM-5-1st digit counter.
  14895.                                 ; set B to MEM-5-2nd leading digit counter.
  14896.         LD      HL,$5CA1        ; set HL to start of digits at MEM-3-1
  14897.         LD      A,B             ;
  14898.         CP      $09             ;
  14899.         JR      C,L2F46         ; to PF-NOT-E
  14900.  
  14901.         CP      $FC             ;
  14902.         JR      C,L2F6C         ; to PF-E-FRMT
  14903.  
  14904. ;; PF-NOT-E
  14905. L2F46:  AND     A               ; test for zero leading digits as in .123
  14906.  
  14907.         CALL    Z,L15EF         ; routine OUT-CODE prints a zero e.g. 0.123
  14908.  
  14909. ;; PF-E-SBRN
  14910. L2F4A:  XOR     A               ;
  14911.         SUB     B               ;
  14912.         JP      M,L2F52         ; skip forward to PF-OUT-LP if originally +ve
  14913.  
  14914.         LD      B,A             ; else negative count now +ve
  14915.         JR      L2F5E           ; forward to PF-DC-OUT       ->
  14916.  
  14917. ; ---
  14918.  
  14919. ;; PF-OUT-LP
  14920. L2F52:  LD      A,C             ; fetch total digit count
  14921.         AND     A               ; test for zero
  14922.         JR      Z,L2F59         ; forward to PF-OUT-DT if so
  14923.  
  14924.         LD      A,(HL)          ; fetch digit
  14925.         INC     HL              ; address next digit
  14926.         DEC     C               ; decrease total digit counter
  14927.  
  14928. ;; PF-OUT-DT
  14929. L2F59:  CALL    L15EF           ; routine OUT-CODE outputs it.
  14930.         DJNZ    L2F52           ; loop back to PF-OUT-LP until B leading
  14931.                                 ; digits output.
  14932.  
  14933. ;; PF-DC-OUT
  14934. L2F5E:  LD      A,C             ; fetch total digits and
  14935.         AND     A               ; test if also zero
  14936.         RET     Z               ; return if so              -->
  14937.  
  14938. ;
  14939.  
  14940.         INC     B               ; increment B
  14941.         LD      A,$2E           ; prepare the character '.'
  14942.  
  14943. ;; PF-DEC-0$
  14944. L2F64:  RST     10H             ; PRINT-A outputs the character '.' or '0'
  14945.  
  14946.         LD      A,$30           ; prepare the character '0'
  14947.                                 ; (for cases like .000012345678)
  14948.         DJNZ    L2F64           ; loop back to PF-DEC-0$ for B times.
  14949.  
  14950.         LD      B,C             ; load B with now trailing digit counter.
  14951.         JR      L2F52           ; back to PF-OUT-LP
  14952.  
  14953. ; ---------------------------------
  14954.  
  14955. ; the branch was here for E-format printing e.g 123456789 => 1.2345679e+8
  14956.  
  14957. ;; PF-E-FRMT
  14958. L2F6C:  LD      D,B             ; counter to D
  14959.         DEC     D               ; decrement
  14960.         LD      B,$01           ; load B with 1.
  14961.  
  14962.         CALL    L2F4A           ; routine PF-E-SBRN above
  14963.  
  14964.         LD      A,$45           ; prepare character 'e'
  14965.         RST     10H             ; PRINT-A
  14966.  
  14967.         LD      C,D             ; exponent to C
  14968.         LD      A,C             ; and to A
  14969.         AND     A               ; test exponent
  14970.         JP      P,L2F83         ; to PF-E-POS if positive
  14971.  
  14972.         NEG                     ; negate
  14973.         LD      C,A             ; positive exponent to C
  14974.         LD      A,$2D           ; prepare character '-'
  14975.         JR      L2F85           ; skip to PF-E-SIGN
  14976.  
  14977. ; ---
  14978.  
  14979. ;; PF-E-POS
  14980. L2F83:  LD      A,$2B           ; prepare character '+'
  14981.  
  14982. ;; PF-E-SIGN
  14983. L2F85:  RST     10H             ; PRINT-A outputs the sign
  14984.  
  14985.         LD      B,$00           ; make the high byte zero.
  14986.         JP      L1A1B           ; exit via OUT-NUM-1 to print exponent in BC
  14987.  
  14988. ; ------------------------------
  14989. ; Handle printing floating point
  14990. ; ------------------------------
  14991. ; This subroutine is called twice from above when printing floating-point
  14992. ; numbers. It returns 10*A +C in registers C and A
  14993.  
  14994. ;; CA-10*A+C
  14995. L2F8B:  PUSH    DE              ; preserve DE.
  14996.         LD      L,A             ; transfer A to L
  14997.         LD      H,$00           ; zero high byte.
  14998.         LD      E,L             ; copy HL
  14999.         LD      D,H             ; to DE.
  15000.         ADD     HL,HL           ; double (*2)
  15001.         ADD     HL,HL           ; double (*4)
  15002.         ADD     HL,DE           ; add DE (*5)
  15003.         ADD     HL,HL           ; double (*10)
  15004.         LD      E,C             ; copy C to E    (D is 0)
  15005.         ADD     HL,DE           ; and add to give required result.
  15006.         LD      C,H             ; transfer to
  15007.         LD      A,L             ; destination registers.
  15008.         POP     DE              ; restore DE
  15009.         RET                     ; return with result.
  15010.  
  15011. ; --------------
  15012. ; Prepare to add
  15013. ; --------------
  15014. ; This routine is called twice by addition to prepare the two numbers. The
  15015. ; exponent is picked up in A and the location made zero. Then the sign bit
  15016. ; is tested before being set to the implied state. Negative numbers are twos
  15017. ; complemented.
  15018.  
  15019. ;; PREP-ADD
  15020. L2F9B:  LD      A,(HL)          ; pick up exponent
  15021.         LD      (HL),$00        ; make location zero
  15022.         AND     A               ; test if number is zero
  15023.         RET     Z               ; return if so
  15024.  
  15025.         INC     HL              ; address mantissa
  15026.         BIT     7,(HL)          ; test the sign bit
  15027.         SET     7,(HL)          ; set it to implied state
  15028.         DEC     HL              ; point to exponent
  15029.         RET     Z               ; return if positive number.
  15030.  
  15031.         PUSH    BC              ; preserve BC
  15032.         LD      BC,$0005        ; length of number
  15033.         ADD     HL,BC           ; point HL past end
  15034.         LD      B,C             ; set B to 5 counter
  15035.         LD      C,A             ; store exponent in C
  15036.         SCF                     ; set carry flag
  15037.  
  15038. ;; NEG-BYTE
  15039. L2FAF:  DEC     HL              ; work from LSB to MSB
  15040.         LD      A,(HL)          ; fetch byte
  15041.         CPL                     ; complement
  15042.         ADC     A,$00           ; add in initial carry or from prev operation
  15043.         LD      (HL),A          ; put back
  15044.         DJNZ    L2FAF           ; loop to NEG-BYTE till all 5 done
  15045.  
  15046.         LD      A,C             ; stored exponent to A
  15047.         POP     BC              ; restore original BC
  15048.         RET                     ; return
  15049.  
  15050. ; -----------------
  15051. ; Fetch two numbers
  15052. ; -----------------
  15053. ; This routine is called twice when printing floating point numbers and also
  15054. ; to fetch two numbers by the addition, multiply and division routines.
  15055. ; HL addresses the first number, DE addresses the second number.
  15056. ; For arithmetic only, A holds the sign of the result which is stored in
  15057. ; the second location.
  15058.  
  15059. ;; FETCH-TWO
  15060. L2FBA:  PUSH    HL              ; save pointer to first number, result if math.
  15061.         PUSH    AF              ; save result sign.
  15062.  
  15063.         LD      C,(HL)          ;
  15064.         INC     HL              ;
  15065.  
  15066.         LD      B,(HL)          ;
  15067.         LD      (HL),A          ; store the sign at correct location in
  15068.                                 ; destination 5 bytes for arithmetic only.
  15069.         INC     HL              ;
  15070.  
  15071.         LD      A,C             ;
  15072.         LD      C,(HL)          ;
  15073.         PUSH    BC              ;
  15074.         INC     HL              ;
  15075.         LD      C,(HL)          ;
  15076.         INC     HL              ;
  15077.         LD      B,(HL)          ;
  15078.         EX      DE,HL           ;
  15079.         LD      D,A             ;
  15080.         LD      E,(HL)          ;
  15081.         PUSH    DE              ;
  15082.         INC     HL              ;
  15083.         LD      D,(HL)          ;
  15084.         INC     HL              ;
  15085.         LD      E,(HL)          ;
  15086.         PUSH    DE              ;
  15087.         EXX                     ;
  15088.         POP     DE              ;
  15089.         POP     HL              ;
  15090.         POP     BC              ;
  15091.         EXX                     ;
  15092.         INC     HL              ;
  15093.         LD      D,(HL)          ;
  15094.         INC     HL              ;
  15095.         LD      E,(HL)          ;
  15096.  
  15097.         POP     AF              ; restore possible result sign.
  15098.         POP     HL              ; and pointer to possible result.
  15099.         RET                     ; return.
  15100.  
  15101. ; ---------------------------------
  15102. ; Shift floating point number right
  15103. ; ---------------------------------
  15104. ;
  15105. ;
  15106.  
  15107. ;; SHIFT-FP
  15108. L2FDD:  AND     A               ;
  15109.         RET     Z               ;
  15110.  
  15111.         CP      $21             ;
  15112.         JR      NC,L2FF9        ; to ADDEND-0
  15113.  
  15114.         PUSH    BC              ;
  15115.         LD      B,A             ;
  15116.  
  15117. ;; ONE-SHIFT
  15118. L2FE5:  EXX                     ;
  15119.         SRA     L               ;
  15120.         RR      D               ;
  15121.         RR      E               ;
  15122.         EXX                     ;
  15123.         RR      D               ;
  15124.         RR      E               ;
  15125.         DJNZ    L2FE5           ; to ONE-SHIFT
  15126.  
  15127.         POP     BC              ;
  15128.         RET     NC              ;
  15129.  
  15130.         CALL    L3004           ; routine ADD-BACK
  15131.         RET     NZ              ;
  15132.  
  15133. ;; ADDEND-0
  15134. L2FF9:  EXX                     ;
  15135.         XOR     A               ;
  15136.  
  15137. ;; ZEROS-4/5
  15138. L2FFB:  LD      L,$00           ;
  15139.         LD      D,A             ;
  15140.         LD      E,L             ;
  15141.         EXX                     ;
  15142.         LD      DE,$0000        ;
  15143.         RET                     ;
  15144.  
  15145. ; ------------------
  15146. ; Add back any carry
  15147. ; ------------------
  15148. ;
  15149. ;
  15150.  
  15151. ;; ADD-BACK
  15152. L3004:  INC     E               ;
  15153.         RET     NZ              ;
  15154.  
  15155.         INC      D              ;
  15156.         RET     NZ              ;
  15157.  
  15158.         EXX                     ;
  15159.         INC     E               ;
  15160.         JR      NZ,L300D        ; to ALL-ADDED
  15161.  
  15162.         INC     D               ;
  15163.  
  15164. ;; ALL-ADDED
  15165. L300D:  EXX                     ;
  15166.         RET                     ;
  15167.  
  15168. ; -----------------------
  15169. ; Handle subtraction (03)
  15170. ; -----------------------
  15171. ; Subtraction is done by switching the sign byte/bit of the second number
  15172. ; which may be integer of floating point and continuing into addition.
  15173.  
  15174. ;; subtract
  15175. L300F:  EX      DE,HL           ; address second number with HL
  15176.  
  15177.         CALL    L346E           ; routine NEGATE switches sign
  15178.  
  15179.         EX      DE,HL           ; address first number again
  15180.                                 ; and continue.
  15181.  
  15182. ; --------------------
  15183. ; Handle addition (0F)
  15184. ; --------------------
  15185. ; HL points to first number, DE to second.
  15186. ; If they are both integers, then go for the easy route.
  15187.  
  15188. ;; addition
  15189. L3014:  LD      A,(DE)          ; fetch first byte of second
  15190.         OR      (HL)            ; combine with first byte of first
  15191.         JR      NZ,L303E        ; forward to FULL-ADDN if at least one was
  15192.                                 ; in floating point form.
  15193.  
  15194. ; continue if both were small integers.
  15195.  
  15196.         PUSH    DE              ; save pointer to lowest number for result.
  15197.  
  15198.         INC     HL              ; address sign byte and
  15199.         PUSH    HL              ; push the pointer.
  15200.  
  15201.         INC     HL              ; address low byte
  15202.         LD      E,(HL)          ; to E
  15203.         INC     HL              ; address high byte
  15204.         LD      D,(HL)          ; to D
  15205.         INC     HL              ; address unused byte
  15206.  
  15207.         INC     HL              ; address known zero indicator of 1st number
  15208.         INC     HL              ; address sign byte
  15209.  
  15210.         LD      A,(HL)          ; sign to A, $00 or $FF
  15211.  
  15212.         INC     HL              ; address low byte
  15213.         LD      C,(HL)          ; to C
  15214.         INC     HL              ; address high byte
  15215.         LD      B,(HL)          ; to B
  15216.  
  15217.         POP     HL              ; pop result sign pointer
  15218.         EX      DE,HL           ; integer to HL
  15219.  
  15220.         ADD     HL,BC           ; add to the other one in BC
  15221.                                 ; setting carry if overflow.
  15222.  
  15223.         EX      DE,HL           ; save result in DE bringing back sign pointer
  15224.  
  15225.         ADC     A,(HL)          ; if pos/pos A=01 with overflow else 00
  15226.                                 ; if neg/neg A=FF with overflow else FE
  15227.                                 ; if mixture A=00 with overflow else FF
  15228.  
  15229.         RRCA                    ; bit 0 to (C)
  15230.  
  15231.         ADC     A,$00           ; both acceptable signs now zero
  15232.  
  15233.         JR      NZ,L303C        ; forward to ADDN-OFLW if not
  15234.  
  15235.         SBC     A,A             ; restore a negative result sign
  15236.  
  15237.         LD      (HL),A          ;
  15238.         INC     HL              ;
  15239.         LD      (HL),E          ;
  15240.         INC     HL              ;
  15241.         LD      (HL),D          ;
  15242.         DEC     HL              ;
  15243.         DEC     HL              ;
  15244.         DEC     HL              ;
  15245.  
  15246.         POP     DE              ; STKEND
  15247.         RET                     ;
  15248.  
  15249. ; ---
  15250.  
  15251. ;; ADDN-OFLW
  15252. L303C:  DEC     HL              ;
  15253.         POP     DE              ;
  15254.  
  15255. ;; FULL-ADDN
  15256. L303E:  CALL    L3293           ; routine RE-ST-TWO
  15257.         EXX                     ;
  15258.         PUSH    HL              ;
  15259.         EXX                     ;
  15260.         PUSH    DE              ;
  15261.         PUSH    HL              ;
  15262.         CALL    L2F9B           ; routine PREP-ADD
  15263.         LD      B,A             ;
  15264.         EX      DE,HL           ;
  15265.         CALL    L2F9B           ; routine PREP-ADD
  15266.         LD       C,A            ;
  15267.         CP      B               ;
  15268.         JR      NC,L3055        ; to SHIFT-LEN
  15269.  
  15270.         LD      A,B             ;
  15271.         LD      B,C             ;
  15272.         EX      DE,HL           ;
  15273.  
  15274. ;; SHIFT-LEN
  15275. L3055:  PUSH    AF              ;
  15276.         SUB     B               ;
  15277.         CALL    L2FBA           ; routine FETCH-TWO
  15278.         CALL    L2FDD           ; routine SHIFT-FP
  15279.         POP     AF              ;
  15280.         POP     HL              ;
  15281.         LD      (HL),A          ;
  15282.         PUSH    HL              ;
  15283.         LD      L,B             ;
  15284.         LD      H,C             ;
  15285.         ADD     HL,DE           ;
  15286.         EXX                     ;
  15287.         EX      DE,HL           ;
  15288.         ADC     HL,BC           ;
  15289.         EX      DE,HL           ;
  15290.         LD      A,H             ;
  15291.         ADC     A,L             ;
  15292.         LD      L,A             ;
  15293.         RRA                     ;
  15294.         XOR     L               ;
  15295.         EXX                     ;
  15296.         EX      DE,HL           ;
  15297.         POP     HL              ;
  15298.         RRA                     ;
  15299.         JR      NC,L307C        ; to TEST-NEG
  15300.  
  15301.         LD      A,$01           ;
  15302.         CALL    L2FDD           ; routine SHIFT-FP
  15303.         INC     (HL)            ;
  15304.         JR      Z,L309F         ; to ADD-REP-6
  15305.  
  15306. ;; TEST-NEG
  15307. L307C:  EXX                     ;
  15308.         LD      A,L             ;
  15309.         AND     $80             ;
  15310.         EXX                     ;
  15311.         INC     HL              ;
  15312.         LD      (HL),A          ;
  15313.         DEC     HL              ;
  15314.         JR      Z,L30A5         ; to GO-NC-MLT
  15315.  
  15316.         LD      A,E             ;
  15317.         NEG                     ; Negate
  15318.         CCF                     ; Complement Carry Flag
  15319.         LD      E,A             ;
  15320.         LD      A,D             ;
  15321.         CPL                     ;
  15322.         ADC     A,$00           ;
  15323.         LD      D,A             ;
  15324.         EXX                     ;
  15325.         LD      A,E             ;
  15326.         CPL                     ;
  15327.         ADC     A,$00           ;
  15328.         LD      E,A             ;
  15329.         LD      A,D             ;
  15330.         CPL                     ;
  15331.         ADC     A,$00           ;
  15332.         JR      NC,L30A3        ; to END-COMPL
  15333.  
  15334.         RRA                     ;
  15335.         EXX                     ;
  15336.         INC     (HL)            ;
  15337.  
  15338. ;; ADD-REP-6
  15339. L309F:  JP      Z,L31AD         ; to REPORT-6
  15340.  
  15341.         EXX                     ;
  15342.  
  15343. ;; END-COMPL
  15344. L30A3:  LD      D,A             ;
  15345.         EXX                     ;
  15346.  
  15347. ;; GO-NC-MLT
  15348. L30A5:  XOR     A               ;
  15349.         JP      L3155           ; to TEST-NORM
  15350.  
  15351. ; -----------------------------
  15352. ; Used in 16 bit multiplication
  15353. ; -----------------------------
  15354. ; This routine is used, in the first instance, by the multiply calculator
  15355. ; literal to perform an integer multiplication in preference to
  15356. ; 32-bit multiplication to which it will resort if this overflows.
  15357. ;
  15358. ; It is also used by STK-VAR to calculate array subscripts and by DIM to
  15359. ; calculate the space required for multi-dimensional arrays.
  15360.  
  15361. ;; HL-HL*DE
  15362. L30A9:  PUSH    BC              ; preserve BC throughout
  15363.         LD      B,$10           ; set B to 16
  15364.         LD      A,H             ; save H in A high byte
  15365.         LD      C,L             ; save L in C low byte
  15366.         LD      HL,$0000        ; initialize result to zero
  15367.  
  15368. ; now enter a loop.
  15369.  
  15370. ;; HL-LOOP
  15371. L30B1:  ADD     HL,HL           ; double result
  15372.         JR      C,L30BE         ; to HL-END if overflow
  15373.  
  15374.         RL      C               ; shift AC left into carry
  15375.         RLA                     ;
  15376.         JR      NC,L30BC        ; to HL-AGAIN to skip addition if no carry
  15377.  
  15378.         ADD     HL,DE           ; add in DE
  15379.         JR      C,L30BE         ; to HL-END if overflow
  15380.  
  15381. ;; HL-AGAIN
  15382. L30BC:  DJNZ    L30B1           ; back to HL-LOOP for all 16 bits
  15383.  
  15384. ;; HL-END
  15385. L30BE:  POP     BC              ; restore preserved BC
  15386.         RET                     ; return with carry reset if successful
  15387.                                 ; and result in HL.
  15388.  
  15389. ; -----------------------------
  15390. ; Prepare to multiply or divide
  15391. ; -----------------------------
  15392. ; This routine is called in succession from multiply and divide to prepare
  15393. ; two mantissas by setting the leftmost bit that is used for the sign.
  15394. ; On the first call A holds zero and picks up the sign bit. On the second
  15395. ; call the two bits are XORed to form the result sign - minus * minus giving
  15396. ; plus etc. If either number is zero then this is flagged.
  15397. ; HL addresses the exponent.
  15398.  
  15399. ;; PREP-M/D
  15400. L30C0:  CALL    L34E9           ; routine TEST-ZERO  preserves accumulator.
  15401.         RET     C               ; return carry set if zero
  15402.  
  15403.         INC     HL              ; address first byte of mantissa
  15404.         XOR     (HL)            ; pick up the first or xor with first.
  15405.         SET     7,(HL)          ; now set to give true 32-bit mantissa
  15406.         DEC     HL              ; point to exponent
  15407.         RET                     ; return with carry reset
  15408.  
  15409. ; --------------------------
  15410. ; Handle multiplication (04)
  15411. ; --------------------------
  15412. ;
  15413. ;
  15414.  
  15415. ;; multiply
  15416. L30CA:  LD      A,(DE)          ;
  15417.         OR      (HL)            ;
  15418.         JR      NZ,L30F0        ; to MULT-LONG
  15419.  
  15420.         PUSH    DE              ;
  15421.         PUSH    HL              ;
  15422.         PUSH    DE              ;
  15423.         CALL    L2D7F           ; routine INT-FETCH
  15424.         EX      DE,HL           ;
  15425.         EX      (SP),HL         ;
  15426.         LD      B,C             ;
  15427.         CALL    L2D7F           ; routine INT-FETCH
  15428.         LD      A,B             ;
  15429.         XOR     C               ;
  15430.         LD      C,A             ;
  15431.         POP     HL              ;
  15432.         CALL    L30A9           ; routine HL-HL*DE
  15433.         EX      DE,HL           ;
  15434.         POP     HL              ;
  15435.         JR      C,L30EF         ; to MULT-OFLW
  15436.  
  15437.         LD      A,D             ;
  15438.         OR      E               ;
  15439.         JR      NZ,L30EA        ; to MULT-RSLT
  15440.  
  15441.         LD      C,A             ;
  15442.  
  15443. ;; MULT-RSLT
  15444. L30EA:  CALL    L2D8E           ; routine INT-STORE
  15445.         POP      DE             ;
  15446.         RET                     ;
  15447.  
  15448. ; ---
  15449.  
  15450. ;; MULT-OFLW
  15451. L30EF:  POP     DE              ;
  15452.  
  15453. ;; MULT-LONG
  15454. L30F0:  CALL    L3293           ; routine RE-ST-TWO
  15455.         XOR     A               ;
  15456.         CALL    L30C0           ; routine PREP-M/D
  15457.         RET     C               ;
  15458.  
  15459.         EXX                     ;
  15460.         PUSH    HL              ;
  15461.         EXX                     ;
  15462.         PUSH    DE              ;
  15463.         EX      DE,HL           ;
  15464.         CALL    L30C0           ; routine PREP-M/D
  15465.         EX      DE,HL           ;
  15466.         JR      C,L315D         ; to ZERO-RSLT
  15467.  
  15468.         PUSH    HL              ;
  15469.         CALL    L2FBA           ; routine FETCH-TWO
  15470.         LD      A,B             ;
  15471.         AND     A               ;
  15472.         SBC     HL,HL           ;
  15473.         EXX                     ;
  15474.         PUSH    HL              ;
  15475.         SBC     HL,HL           ;
  15476.         EXX                     ;
  15477.         LD      B,$21           ;
  15478.         JR      L3125           ; to STRT-MLT
  15479.  
  15480. ; ---
  15481.  
  15482. ;; MLT-LOOP
  15483. L3114:  JR      NC,L311B        ; to NO-ADD
  15484.  
  15485.         ADD     HL,DE           ;
  15486.         EXX                     ;
  15487.         ADC     HL,DE           ;
  15488.         EXX                     ;
  15489.  
  15490. ;; NO-ADD
  15491. L311B:  EXX                     ;
  15492.         RR      H               ;
  15493.         RR      L               ;
  15494.         EXX                     ;
  15495.         RR      H               ;
  15496.         RR      L               ;
  15497.  
  15498. ;; STRT-MLT
  15499. L3125:  EXX                     ;
  15500.         RR      B               ;
  15501.         RR      C               ;
  15502.         EXX                     ;
  15503.         RR      C               ;
  15504.         RRA                     ;
  15505.         DJNZ    L3114           ; to MLT-LOOP
  15506.  
  15507.         EX      DE,HL           ;
  15508.         EXX                     ;
  15509.         EX      DE,HL           ;
  15510.         EXX                     ;
  15511.         POP     BC              ;
  15512.         POP     HL              ;
  15513.         LD      A,B             ;
  15514.         ADD     A,C             ;
  15515.         JR      NZ,L313B        ; to MAKE-EXPT
  15516.  
  15517.         AND     A               ;
  15518.  
  15519. ;; MAKE-EXPT
  15520. L313B:  DEC     A               ;
  15521.         CCF                     ; Complement Carry Flag
  15522.  
  15523. ;; DIVN-EXPT
  15524. L313D:  RLA                     ;
  15525.         CCF                     ; Complement Carry Flag
  15526.         RRA                     ;
  15527.         JP      P,L3146         ; to OFLW1-CLR
  15528.  
  15529.         JR      NC,L31AD        ; to REPORT-6
  15530.  
  15531.         AND     A               ;
  15532.  
  15533. ;; OFLW1-CLR
  15534. L3146:  INC     A               ;
  15535.         JR      NZ,L3151        ; to OFLW2-CLR
  15536.  
  15537.         JR      C,L3151         ; to OFLW2-CLR
  15538.  
  15539.         EXX                     ;
  15540.         BIT     7,D             ;
  15541.         EXX                     ;
  15542.         JR      NZ,L31AD        ; to REPORT-6
  15543.  
  15544. ;; OFLW2-CLR
  15545. L3151:  LD      (HL),A          ;
  15546.         EXX                     ;
  15547.         LD      A,B             ;
  15548.         EXX                     ;
  15549.  
  15550. ;; TEST-NORM
  15551. L3155:  JR      NC,L316C        ; to NORMALISE
  15552.  
  15553.         LD      A,(HL)          ;
  15554.         AND     A               ;
  15555.  
  15556. ;; NEAR-ZERO
  15557. L3159:  LD      A,$80           ;
  15558.         JR      Z,L315E         ; to SKIP-ZERO
  15559.  
  15560. ;; ZERO-RSLT
  15561. L315D:  XOR     A               ;
  15562.  
  15563. ;; SKIP-ZERO
  15564. L315E:  EXX                     ;
  15565.         AND     D               ;
  15566.         CALL    L2FFB           ; routine ZEROS-4/5
  15567.         RLCA                    ;
  15568.         LD      (HL),A          ;
  15569.         JR      C,L3195         ; to OFLOW-CLR
  15570.  
  15571.         INC     HL              ;
  15572.         LD      (HL),A          ;
  15573.         DEC     HL              ;
  15574.         JR      L3195           ; to OFLOW-CLR
  15575.  
  15576. ; ---
  15577.  
  15578. ;; NORMALISE
  15579. L316C:  LD      B,$20           ;
  15580.  
  15581. ;; SHIFT-ONE
  15582. L316E:  EXX                     ;
  15583.         BIT     7,D             ;
  15584.         EXX                     ;
  15585.         JR      NZ,L3186        ; to NORML-NOW
  15586.  
  15587.         RLCA                    ;
  15588.         RL      E               ;
  15589.         RL      D               ;
  15590.         EXX                     ;
  15591.         RL      E               ;
  15592.         RL      D               ;
  15593.         EXX                     ;
  15594.         DEC     (HL)            ;
  15595.         JR      Z,L3159         ; to NEAR-ZERO
  15596.  
  15597.         DJNZ    L316E           ; to SHIFT-ONE
  15598.  
  15599.         JR      L315D           ; to ZERO-RSLT
  15600.  
  15601. ; ---
  15602.  
  15603. ;; NORML-NOW
  15604. L3186:  RLA                     ;
  15605.         JR      NC,L3195        ; to OFLOW-CLR
  15606.  
  15607.         CALL    L3004           ; routine ADD-BACK
  15608.         JR      NZ,L3195        ; to OFLOW-CLR
  15609.  
  15610.         EXX                     ;
  15611.         LD       D,$80          ;
  15612.         EXX                     ;
  15613.         INC     (HL)            ;
  15614.         JR      Z,L31AD         ; to REPORT-6
  15615.  
  15616. ;; OFLOW-CLR
  15617. L3195:  PUSH    HL              ;
  15618.         INC     HL              ;
  15619.         EXX                     ;
  15620.         PUSH    DE              ;
  15621.         EXX                     ;
  15622.         POP     BC              ;
  15623.         LD      A,B             ;
  15624.         RLA                     ;
  15625.         RL      (HL)            ;
  15626.         RRA                     ;
  15627.         LD      (HL),A          ;
  15628.         INC     HL              ;
  15629.         LD      (HL),C          ;
  15630.         INC     HL              ;
  15631.         LD      (HL),D          ;
  15632.         INC     HL              ;
  15633.         LD      (HL),E          ;
  15634.         POP     HL              ;
  15635.         POP     DE              ;
  15636.         EXX                     ;
  15637.         POP     HL              ;
  15638.         EXX                     ;
  15639.         RET                     ;
  15640.  
  15641. ; ---
  15642.  
  15643. ;; REPORT-6
  15644. L31AD:  RST     08H             ; ERROR-1
  15645.         DB    $05             ; Error Report: Number too big
  15646.  
  15647. ; --------------------
  15648. ; Handle division (05)
  15649. ; --------------------
  15650. ;
  15651. ;
  15652.  
  15653. ;; division
  15654. L31AF:  CALL    L3293           ; routine RE-ST-TWO
  15655.         EX      DE,HL           ;
  15656.         XOR     A               ;
  15657.         CALL    L30C0           ; routine PREP-M/D
  15658.         JR      C,L31AD         ; to REPORT-6
  15659.  
  15660.         EX      DE,HL           ;
  15661.         CALL    L30C0           ; routine PREP-M/D
  15662.         RET     C               ;
  15663.  
  15664.         EXX                     ;
  15665.         PUSH    HL              ;
  15666.         EXX                     ;
  15667.         PUSH    DE              ;
  15668.         PUSH    HL              ;
  15669.         CALL    L2FBA           ; routine FETCH-TWO
  15670.         EXX                     ;
  15671.         PUSH    HL              ;
  15672.         LD      H,B             ;
  15673.         LD      L,C             ;
  15674.         EXX                     ;
  15675.         LD      H,C             ;
  15676.         LD      L,B             ;
  15677.         XOR     A               ;
  15678.         LD      B,$DF           ;
  15679.         JR      L31E2           ; to DIV-START
  15680.  
  15681. ; ---
  15682.  
  15683. ;; DIV-LOOP
  15684. L31D2:  RLA                     ;
  15685.         RL      C               ;
  15686.         EXX                     ;
  15687.         RL      C               ;
  15688.         RL      B               ;
  15689.         EXX                     ;
  15690.  
  15691. ;; div-34th
  15692. L31DB:  ADD     HL,HL           ;
  15693.         EXX                     ;
  15694.         ADC     HL,HL           ;
  15695.         EXX                     ;
  15696.         JR      C,L31F2         ; to SUBN-ONLY
  15697.  
  15698. ;; DIV-START
  15699. L31E2:  SBC     HL,DE           ;
  15700.         EXX                     ;
  15701.         SBC     HL,DE           ;
  15702.         EXX                     ;
  15703.         JR      NC,L31F9        ; to NO-RSTORE
  15704.  
  15705.         ADD     HL,DE           ;
  15706.         EXX                     ;
  15707.         ADC     HL,DE           ;
  15708.         EXX                     ;
  15709.         AND     A               ;
  15710.         JR      L31FA           ; to COUNT-ONE
  15711.  
  15712. ; ---
  15713.  
  15714. ;; SUBN-ONLY
  15715. L31F2:  AND     A               ;
  15716.         SBC     HL,DE           ;
  15717.         EXX                     ;
  15718.         SBC     HL,DE           ;
  15719.         EXX                     ;
  15720.  
  15721. ;; NO-RSTORE
  15722. L31F9:  SCF                     ; Set Carry Flag
  15723.  
  15724. ;; COUNT-ONE
  15725. L31FA:  INC     B               ;
  15726.         JP      M,L31D2         ; to DIV-LOOP
  15727.  
  15728.         PUSH    AF              ;
  15729.         JR      Z,L31E2         ; to DIV-START
  15730.  
  15731. ;
  15732. ;
  15733. ;
  15734. ;
  15735.  
  15736.         LD      E,A             ;
  15737.         LD      D,C             ;
  15738.         EXX                     ;
  15739.         LD      E,C             ;
  15740.         LD      D,B             ;
  15741.         POP     AF              ;
  15742.         RR      B               ;
  15743.         POP     AF              ;
  15744.         RR      B               ;
  15745.         EXX                     ;
  15746.         POP     BC              ;
  15747.         POP     HL              ;
  15748.         LD      A,B             ;
  15749.         SUB     C               ;
  15750.         JP      L313D           ; jump back to DIVN-EXPT
  15751.  
  15752. ; ------------------------------------
  15753. ; Integer truncation towards zero ($3A)
  15754. ; ------------------------------------
  15755. ;
  15756. ;
  15757.  
  15758. ;; truncate
  15759. L3214:  LD      A,(HL)          ;
  15760.         AND     A               ;
  15761.         RET     Z               ;
  15762.  
  15763.         CP      $81             ;
  15764.         JR      NC,L3221        ; to T-GR-ZERO
  15765.  
  15766.         LD      (HL),$00        ;
  15767.         LD      A,$20           ;
  15768.         JR      L3272           ; to NIL-BYTES
  15769.  
  15770. ; ---
  15771.  
  15772. ;; T-GR-ZERO
  15773. L3221:  CP      $91             ;
  15774.         JR      NZ,L323F        ; to T-SMALL
  15775.  
  15776.         INC     HL              ;
  15777.         INC     HL              ;
  15778.         INC     HL              ;
  15779.         LD      A,$80           ;
  15780.         AND     (HL)            ;
  15781.         DEC      HL             ;
  15782.         OR      (HL)            ;
  15783.         DEC     HL              ;
  15784.         JR      NZ,L3233        ; to T-FIRST
  15785.  
  15786.         LD      A,$80           ;
  15787.         XOR     (HL)            ;
  15788.  
  15789. ;; T-FIRST
  15790. L3233:  DEC     HL              ;
  15791.         JR      NZ,L326C        ; to T-EXPNENT
  15792.  
  15793.         LD      (HL),A          ;
  15794.         INC     HL              ;
  15795.         LD      (HL),$FF        ;
  15796.         DEC     HL              ;
  15797.         LD      A,$18           ;
  15798.         JR      L3272           ; to NIL-BYTES
  15799.  
  15800. ; ---
  15801.  
  15802. ;; T-SMALL
  15803. L323F:  JR      NC,L326D        ; to X-LARGE
  15804.  
  15805.         PUSH    DE              ;
  15806.         CPL                     ;
  15807.         ADD     A,$91           ;
  15808.         INC     HL              ;
  15809.         LD      D,(HL)          ;
  15810.         INC     HL              ;
  15811.         LD      E,(HL)          ;
  15812.         DEC     HL              ;
  15813.         DEC     HL              ;
  15814.         LD      C,$00           ;
  15815.         BIT     7,D             ;
  15816.         JR      Z,L3252         ; to T-NUMERIC
  15817.  
  15818.         DEC     C               ;
  15819.  
  15820. ;; T-NUMERIC
  15821. L3252:  SET     7,D             ;
  15822.         LD      B,$08           ;
  15823.         SUB     B               ;
  15824.         ADD     A,B             ;
  15825.         JR      C,L325E         ; to T-TEST
  15826.  
  15827.         LD      E,D             ;
  15828.         LD      D,$00           ;
  15829.         SUB     B               ;
  15830.  
  15831. ;; T-TEST
  15832. L325E:  JR      Z,L3267         ; to T-STORE
  15833.  
  15834.         LD      B,A             ;
  15835.  
  15836. ;; T-SHIFT
  15837. L3261:  SRL     D               ;
  15838.         RR      E               ;
  15839.         DJNZ    L3261           ; to T-SHIFT
  15840.  
  15841. ;; T-STORE
  15842. L3267:  CALL    L2D8E           ; routine INT-STORE
  15843.         POP     DE              ;
  15844.         RET                     ;
  15845.  
  15846. ; ---
  15847.  
  15848. ;; T-EXPNENT
  15849. L326C:  LD      A,(HL)          ;
  15850.  
  15851. ;; X-LARGE
  15852. L326D:  SUB     $A0             ;
  15853.         RET     P               ;
  15854.  
  15855.         NEG                     ; Negate
  15856.  
  15857. ;; NIL-BYTES
  15858. L3272:  PUSH    DE              ;
  15859.         EX      DE,HL           ;
  15860.         DEC     HL              ;
  15861.         LD      B,A             ;
  15862.         SRL     B               ;
  15863.         SRL     B               ;
  15864.         SRL     B               ;
  15865.         JR      Z,L3283         ; to BITS-ZERO
  15866.  
  15867. ;; BYTE-ZERO
  15868. L327E:  LD      (HL),$00        ;
  15869.         DEC     HL              ;
  15870.         DJNZ    L327E           ; to BYTE-ZERO
  15871.  
  15872. ;; BITS-ZERO
  15873. L3283:  AND     $07             ;
  15874.         JR      Z,L3290         ; to IX-END
  15875.  
  15876.         LD      B,A             ;
  15877.         LD      A,$FF           ;
  15878.  
  15879. ;; LESS-MASK
  15880. L328A:  SLA     A               ;
  15881.         DJNZ    L328A           ; to LESS-MASK
  15882.  
  15883.         AND     (HL)            ;
  15884.         LD      (HL),A          ;
  15885.  
  15886. ;; IX-END
  15887. L3290:  EX      DE,HL           ;
  15888.         POP     DE              ;
  15889.         RET                     ;
  15890.  
  15891. ; ----------------------------------
  15892. ; Storage of numbers in 5 byte form.
  15893. ; ==================================
  15894. ; Both integers and floating-point numbers can be stored in five bytes.
  15895. ; Zero is a special case stored as 5 zeros.
  15896. ; For integers the form is
  15897. ; Byte 1 - zero,
  15898. ; Byte 2 - sign byte, $00 +ve, $FF -ve.
  15899. ; Byte 3 - Low byte of integer.
  15900. ; Byte 4 - High byte
  15901. ; Byte 5 - unused but always zero.
  15902. ;
  15903. ; it seems unusual to store the low byte first but it is just as easy either
  15904. ; way. Statistically it just increases the chances of trailing zeros which
  15905. ; is an advantage elsewhere in saving ROM code.
  15906. ;
  15907. ;             zero     sign     low      high    unused
  15908. ; So +1 is  00000000 00000000 00000001 00000000 00000000
  15909. ;
  15910. ; and -1 is 00000000 11111111 11111111 11111111 00000000
  15911. ;
  15912. ; much of the arithmetic found in BASIC lines can be done using numbers
  15913. ; in this form using the Z80's 16 bit register operation ADD.
  15914. ; (multiplication is done by a sequence of additions).
  15915. ;
  15916. ; Storing -ve integers in two's complement form, means that they are ready for
  15917. ; addition and you might like to add the numbers above to prove that the
  15918. ; answer is zero. If, as in this case, the carry is set then that denotes that
  15919. ; the result is positive. This only applies when the signs don't match.
  15920. ; With positive numbers a carry denotes the result is out of integer range.
  15921. ; With negative numbers a carry denotes the result is within range.
  15922. ; The exception to the last rule is when the result is -65536
  15923. ;
  15924. ; Floating point form is an alternative method of storing numbers which can
  15925. ; be used for integers and larger (or fractional) numbers.
  15926. ;
  15927. ; In this form 1 is stored as
  15928. ;           10000001 00000000 00000000 00000000 00000000
  15929. ;
  15930. ; When a small integer is converted to a floating point number the last two
  15931. ; bytes are always blank so they are omitted in the following steps
  15932. ;
  15933. ; first make exponent +1 +16d  (bit 7 of the exponent is set if positive)
  15934.  
  15935. ; 10010001 00000000 00000001
  15936. ; 10010000 00000000 00000010 <-  now shift left and decrement exponent
  15937. ; ...
  15938. ; 10000010 01000000 00000000 <-  until a 1 abuts the imaginary point
  15939. ; 10000001 10000000 00000000     to the left of the mantissa.
  15940. ;
  15941. ; however since the leftmost bit of the mantissa is always set then it can
  15942. ; be used to denote the sign of the mantissa and put back when needed by the
  15943. ; PREP routines which gives
  15944. ;
  15945. ; 10000001 00000000 00000000
  15946.  
  15947. ; -----------------------------
  15948. ; Re-stack two `small' integers
  15949. ; -----------------------------
  15950. ; This routine is called to re-stack two numbers in full floating point form
  15951. ; e.g. from mult when integer multiplication has overflowed.
  15952.  
  15953. ;; RE-ST-TWO
  15954. L3293:  CALL    L3296           ; routine RESTK-SUB  below and continue
  15955.                                 ; into the routine to do the other one.
  15956.  
  15957. ;; RESTK-SUB
  15958. L3296:  EX      DE,HL           ; swap pointers
  15959.  
  15960. ; --------------------------------
  15961. ; Re-stack one number in full form
  15962. ; --------------------------------
  15963. ; This routine re-stacks an integer usually on the calculator stack
  15964. ; in full floating point form.
  15965. ; HL points to first byte.
  15966.  
  15967. ;; re-stack
  15968. L3297:  LD      A,(HL)          ; Fetch Exponent byte to A
  15969.         AND     A               ; test it
  15970.         RET     NZ              ; return if not zero as already in full
  15971.                                 ; floating-point form.
  15972.  
  15973.         PUSH    DE              ; preserve DE.
  15974.         CALL    L2D7F           ; routine INT-FETCH
  15975.                                 ; integer to DE, sign to C.
  15976.  
  15977. ; HL points to 4th byte.
  15978.  
  15979.         XOR     A               ; clear accumulator.
  15980.         INC     HL              ; point to 5th.
  15981.         LD      (HL),A          ; and blank.
  15982.         DEC     HL              ; point to 4th.
  15983.         LD      (HL),A          ; and blank.
  15984.  
  15985.         LD      B,$91           ; set exponent byte +ve $81
  15986.                                 ; and imaginary dec point 16 bits to right
  15987.                                 ; of first bit.
  15988.  
  15989. ; we could skip to normalize now but it's quicker to avoid
  15990. ; normalizing through an empty D.
  15991.  
  15992.         LD      A,D             ; fetch the high byte D
  15993.         AND     A               ; is it zero ?
  15994.         JR      NZ,L32B1        ; skip to RS-NRMLSE if not.
  15995.  
  15996.         OR      E               ; low byte E to A and test for zero
  15997.         LD      B,D             ; set B exponent to 0
  15998.         JR      Z,L32BD         ; forward to RS-STORE if value is zero.
  15999.  
  16000.         LD      D,E             ; transfer E to D
  16001.         LD      E,B             ; set E to 0
  16002.         LD      B,$89           ; reduce the initial exponent by eight.
  16003.  
  16004.  
  16005. ;; RS-NRMLSE
  16006. L32B1:  EX      DE,HL           ; integer to HL, addr of 4th byte to DE.
  16007.  
  16008. ;; RSTK-LOOP
  16009. L32B2:  DEC     B               ; decrease exponent
  16010.         ADD     HL,HL           ; shift DE left
  16011.         JR      NC,L32B2        ; loop back to RSTK-LOOP
  16012.                                 ; until a set bit pops into carry
  16013.  
  16014.         RRC     C               ; now rotate the sign byte $00 or $FF
  16015.                                 ; into carry to give a sign bit
  16016.  
  16017.         RR      H               ; rotate the sign bit to left of H
  16018.         RR      L               ; rotate any carry into L
  16019.  
  16020.         EX      DE,HL           ; address 4th byte, normalized int to DE
  16021.  
  16022. ;; RS-STORE
  16023. L32BD:  DEC     HL              ; address 3rd byte
  16024.         LD      (HL),E          ; place E
  16025.         DEC     HL              ; address 2nd byte
  16026.         LD      (HL),D          ; place D
  16027.         DEC     HL              ; address 1st byte
  16028.         LD      (HL),B          ; store the exponent
  16029.  
  16030.         POP     DE              ; restore initial DE.
  16031.         RET                     ; return.
  16032.  
  16033. ;****************************************
  16034. ;** Part 10. FLOATING-POINT CALCULATOR **
  16035. ;****************************************
  16036.  
  16037. ; As a general rule the calculator avoids using the IY register.
  16038. ; exceptions are val, val$ and str$.
  16039. ; So an assembly language programmer who has disabled interrupts to use
  16040. ; IY for other purposes can still use the calculator for mathematical
  16041. ; purposes.
  16042.  
  16043.  
  16044. ; ------------------
  16045. ; Table of constants
  16046. ; ------------------
  16047. ;
  16048. ;
  16049.  
  16050. ; used 11 times
  16051. ;; stk-zero                                                 00 00 00 00 00
  16052. L32C5:  DB    $00             ;;Bytes: 1
  16053.         DB    $B0             ;;Exponent $00
  16054.         DB    $00             ;;(+00,+00,+00)
  16055.  
  16056. ; used 19 times
  16057. ;; stk-one                                                  00 00 01 00 00
  16058. L32C8:  DB    $40             ;;Bytes: 2
  16059.         DB    $B0             ;;Exponent $00
  16060.         DB    $00,$01         ;;(+00,+00)
  16061.  
  16062. ; used 9 times
  16063. ;; stk-half                                                 80 00 00 00 00
  16064. L32CC:  DB    $30             ;;Exponent: $80, Bytes: 1
  16065.         DB    $00             ;;(+00,+00,+00)
  16066.  
  16067. ; used 4 times.
  16068. ;; stk-pi/2                                                 81 49 0F DA A2
  16069. L32CE:  DB    $F1             ;;Exponent: $81, Bytes: 4
  16070.         DB    $49,$0F,$DA,$A2 ;;
  16071.  
  16072. ; used 3 times.
  16073. ;; stk-ten                                                  00 00 0A 00 00
  16074. L32D3:  DB    $40             ;;Bytes: 2
  16075.         DB    $B0             ;;Exponent $00
  16076.         DB    $00,$0A         ;;(+00,+00)
  16077.  
  16078.  
  16079. ; ------------------
  16080. ; Table of addresses
  16081. ; ------------------
  16082. ;
  16083. ; starts with binary operations which have two operands and one result.
  16084. ; three pseudo binary operations first.
  16085.  
  16086. ;; tbl-addrs
  16087. L32D7:  DEFW    L368F           ; $00 Address: $368F - jump-true
  16088.         DEFW    L343C           ; $01 Address: $343C - exchange
  16089.         DEFW    L33A1           ; $02 Address: $33A1 - delete
  16090.  
  16091. ; true binary operations.
  16092.  
  16093.         DEFW    L300F           ; $03 Address: $300F - subtract
  16094.         DEFW    L30CA           ; $04 Address: $30CA - multiply
  16095.         DEFW    L31AF           ; $05 Address: $31AF - division
  16096.         DEFW    L3851           ; $06 Address: $3851 - to-power
  16097.         DEFW    L351B           ; $07 Address: $351B - or
  16098.  
  16099.         DEFW    L3524           ; $08 Address: $3524 - no-&-no
  16100.         DEFW    L353B           ; $09 Address: $353B - no-l-eql
  16101.         DEFW    L353B           ; $0A Address: $353B - no-gr-eql
  16102.         DEFW    L353B           ; $0B Address: $353B - nos-neql
  16103.         DEFW    L353B           ; $0C Address: $353B - no-grtr
  16104.         DEFW    L353B           ; $0D Address: $353B - no-less
  16105.         DEFW    L353B           ; $0E Address: $353B - nos-eql
  16106.         DEFW    L3014           ; $0F Address: $3014 - addition
  16107.  
  16108.         DEFW    L352D           ; $10 Address: $352D - str-&-no
  16109.         DEFW    L353B           ; $11 Address: $353B - str-l-eql
  16110.         DEFW    L353B           ; $12 Address: $353B - str-gr-eql
  16111.         DEFW    L353B           ; $13 Address: $353B - strs-neql
  16112.         DEFW    L353B           ; $14 Address: $353B - str-grtr
  16113.         DEFW    L353B           ; $15 Address: $353B - str-less
  16114.         DEFW    L353B           ; $16 Address: $353B - strs-eql
  16115.         DEFW    L359C           ; $17 Address: $359C - strs-add
  16116.  
  16117. ; unary follow
  16118.  
  16119.         DEFW    L35DE           ; $18 Address: $35DE - val$
  16120.         DEFW    L34BC           ; $19 Address: $34BC - usr-$
  16121.         DEFW    L3645           ; $1A Address: $3645 - read-in
  16122.         DEFW    L346E           ; $1B Address: $346E - negate
  16123.  
  16124.         DEFW    L3669           ; $1C Address: $3669 - code
  16125.         DEFW    L35DE           ; $1D Address: $35DE - val
  16126.         DEFW    L3674           ; $1E Address: $3674 - len
  16127.         DEFW    L37B5           ; $1F Address: $37B5 - sin
  16128.         DEFW    L37AA           ; $20 Address: $37AA - cos
  16129.         DEFW    L37DA           ; $21 Address: $37DA - tan
  16130.         DEFW    L3833           ; $22 Address: $3833 - asn
  16131.         DEFW    L3843           ; $23 Address: $3843 - acs
  16132.         DEFW    L37E2           ; $24 Address: $37E2 - atn
  16133.         DEFW    L3713           ; $25 Address: $3713 - ln
  16134.         DEFW    L36C4           ; $26 Address: $36C4 - exp
  16135.         DEFW    L36AF           ; $27 Address: $36AF - int
  16136.         DEFW    L384A           ; $28 Address: $384A - sqr
  16137.         DEFW    L3492           ; $29 Address: $3492 - sgn
  16138.         DEFW    L346A           ; $2A Address: $346A - abs
  16139.         DEFW    L34AC           ; $2B Address: $34AC - peek
  16140.         DEFW    L34A5           ; $2C Address: $34A5 - in
  16141.         DEFW    L34B3           ; $2D Address: $34B3 - usr-no
  16142.         DEFW    L361F           ; $2E Address: $361F - str$
  16143.         DEFW    L35C9           ; $2F Address: $35C9 - chrs
  16144.         DEFW    L3501           ; $30 Address: $3501 - not
  16145.  
  16146. ; end of true unary
  16147.  
  16148.         DEFW    L33C0           ; $31 Address: $33C0 - duplicate
  16149.         DEFW    L36A0           ; $32 Address: $36A0 - n-mod-m
  16150.         DEFW    L3686           ; $33 Address: $3686 - jump
  16151.         DEFW    L33C6           ; $34 Address: $33C6 - stk-data
  16152.         DEFW    L367A           ; $35 Address: $367A - dec-jr-nz
  16153.         DEFW    L3506           ; $36 Address: $3506 - less-0
  16154.         DEFW    L34F9           ; $37 Address: $34F9 - greater-0
  16155.         DEFW    L369B           ; $38 Address: $369B - end-calc
  16156.         DEFW    L3783           ; $39 Address: $3783 - get-argt
  16157.         DEFW    L3214           ; $3A Address: $3214 - truncate
  16158.         DEFW    L33A2           ; $3B Address: $33A2 - fp-calc-2
  16159.         DEFW    L2D4F           ; $3C Address: $2D4F - e-to-fp
  16160.         DEFW    L3297           ; $3D Address: $3297 - re-stack
  16161.  
  16162. ; the following are just the next available slots for the 128 compound literals
  16163. ; which are in range $80 - $FF.
  16164.  
  16165.         DEFW    L3449           ; $3E Address: $3449 - series-xx    $80 - $9F.
  16166.         DEFW    L341B           ; $3F Address: $341B - stk-const-xx $A0 - $BF.
  16167.         DEFW    L342D           ; $40 Address: $342D - st-mem-xx    $C0 - $DF.
  16168.         DEFW    L340F           ; $41 Address: $340F - get-mem-xx   $E0 - $FF.
  16169.  
  16170. ; Aside: 3E - 7F are therefore unused calculator literals.
  16171. ;        3E - 7B would be available for expansion.
  16172.  
  16173. ; --------------
  16174. ; The Calculator
  16175. ; --------------
  16176. ;
  16177. ;
  16178.  
  16179. ;; CALCULATE
  16180. L335B:  CALL    L35BF           ; routine STK-PNTRS is called to set up the
  16181.                                 ; calculator stack pointers for a default
  16182.                                 ; unary operation. HL = last value on stack.
  16183.                                 ; DE = STKEND first location after stack.
  16184.  
  16185. ; the calculate routine is called at this point by the series generator...
  16186.  
  16187. ;; GEN-ENT-1
  16188. L335E:  LD      A,B             ; fetch the Z80 B register to A
  16189.         LD      ($5C67),A       ; and store value in system variable BREG.
  16190.                                 ; this will be the counter for dec-jr-nz
  16191.                                 ; or if used from fp-calc2 the calculator
  16192.                                 ; instruction.
  16193.  
  16194. ; ... and again later at this point
  16195.  
  16196. ;; GEN-ENT-2
  16197. L3362:  EXX                     ; switch sets
  16198.         EX      (SP),HL         ; and store the address of next instruction,
  16199.                                 ; the return address, in H'L'.
  16200.                                 ; If this is a recursive call the the H'L'
  16201.                                 ; of the previous invocation goes on stack.
  16202.                                 ; c.f. end-calc.
  16203.         EXX                     ; switch back to main set
  16204.  
  16205. ; this is the re-entry looping point when handling a string of literals.
  16206.  
  16207. ;; RE-ENTRY
  16208. L3365:  LD      ($5C65),DE      ; save end of stack in system variable STKEND
  16209.         EXX                     ; switch to alt
  16210.         LD      A,(HL)          ; get next literal
  16211.         INC     HL              ; increase pointer'
  16212.  
  16213. ; single operation jumps back to here
  16214.  
  16215. ;; SCAN-ENT
  16216. L336C:  PUSH    HL              ; save pointer on stack
  16217.         AND     A               ; now test the literal
  16218.         JP      P,L3380         ; forward to FIRST-3D if in range $00 - $3D
  16219.                                 ; anything with bit 7 set will be one of
  16220.                                 ; 128 compound literals.
  16221.  
  16222. ; compound literals have the following format.
  16223. ; bit 7 set indicates compound.
  16224. ; bits 6-5 the subgroup 0-3.
  16225. ; bits 4-0 the embedded parameter $00 - $1F.
  16226. ; The subgroup 0-3 needs to be manipulated to form the next available four
  16227. ; address places after the simple literals in the address table.
  16228.  
  16229.         LD      D,A             ; save literal in D
  16230.         AND     $60             ; and with 01100000 to isolate subgroup
  16231.         RRCA                    ; rotate bits
  16232.         RRCA                    ; 4 places to right
  16233.         RRCA                    ; not five as we need offset * 2
  16234.         RRCA                    ; 00000xx0
  16235.         ADD     A,$7C           ; add ($3E * 2) to give correct offset.
  16236.                                 ; alter above if you add more literals.
  16237.         LD      L,A             ; store in L for later indexing.
  16238.         LD      A,D             ; bring back compound literal
  16239.         AND     $1F             ; use mask to isolate parameter bits
  16240.         JR      L338E           ; forward to ENT-TABLE
  16241.  
  16242. ; ---
  16243.  
  16244. ; the branch was here with simple literals.
  16245.  
  16246. ;; FIRST-3D
  16247. L3380:  CP      $18             ; compare with first unary operations.
  16248.         JR      NC,L338C        ; to DOUBLE-A with unary operations
  16249.  
  16250. ; it is binary so adjust pointers.
  16251.  
  16252.         EXX                     ;
  16253.         LD      BC,$FFFB        ; the value -5
  16254.         LD      D,H             ; transfer HL, the last value, to DE.
  16255.         LD      E,L             ;
  16256.         ADD     HL,BC           ; subtract 5 making HL point to second
  16257.                                 ; value.
  16258.         EXX                     ;
  16259.  
  16260. ;; DOUBLE-A
  16261. L338C:  RLCA                    ; double the literal
  16262.         LD      L,A             ; and store in L for indexing
  16263.  
  16264. ;; ENT-TABLE
  16265. L338E:  LD      DE,L32D7        ; Address: tbl-addrs
  16266.         LD      H,$00           ; prepare to index
  16267.         ADD     HL,DE           ; add to get address of routine
  16268.         LD      E,(HL)          ; low byte to E
  16269.         INC     HL              ;
  16270.         LD      D,(HL)          ; high byte to D
  16271.         LD      HL,L3365        ; Address: RE-ENTRY
  16272.         EX      (SP),HL         ; goes to stack
  16273.         PUSH    DE              ; now address of routine
  16274.         EXX                     ; main set
  16275.                                 ; avoid using IY register.
  16276.         LD      BC,($5C66)      ; STKEND_hi
  16277.                                 ; nothing much goes to C but BREG to B
  16278.                                 ; and continue into next ret instruction
  16279.                                 ; which has a dual identity
  16280.  
  16281.  
  16282. ; ------------------
  16283. ; Handle delete (02)
  16284. ; ------------------
  16285. ; A simple return but when used as a calculator literal this
  16286. ; deletes the last value from the calculator stack.
  16287. ; On entry, as always with binary operations,
  16288. ; HL=first number, DE=second number
  16289. ; On exit, HL=result, DE=stkend.
  16290. ; So nothing to do
  16291.  
  16292. ;; delete
  16293. L33A1:  RET                     ; return - indirect jump if from above.
  16294.  
  16295. ; ---------------------
  16296. ; Single operation (3B)
  16297. ; ---------------------
  16298. ; this single operation is used, in the first instance, to evaluate most
  16299. ; of the mathematical and string functions found in BASIC expressions.
  16300.  
  16301. ;; fp-calc-2
  16302. L33A2:  POP     AF              ; drop return address.
  16303.         LD      A,($5C67)       ; load accumulator from system variable BREG
  16304.                                 ; value will be literal eg. 'tan'
  16305.         EXX                     ; switch to alt
  16306.         JR      L336C           ; back to SCAN-ENT
  16307.                                 ; next literal will be end-calc at L2758
  16308.  
  16309. ; ----------------
  16310. ; Test five-spaces
  16311. ; ----------------
  16312. ; This routine is called from MOVE-FP, STK-CONST and STK-STORE to
  16313. ; test that there is enough space between the calculator stack and the
  16314. ; machine stack for another five-byte value. It returns with BC holding
  16315. ; the value 5 ready for any subsequent LDIR.
  16316.  
  16317. ;; TEST-5-SP
  16318. L33A9:  PUSH    DE              ; save
  16319.         PUSH    HL              ; registers
  16320.         LD      BC,$0005        ; an overhead of five bytes
  16321.         CALL    L1F05           ; routine TEST-ROOM tests free RAM raising
  16322.                                 ; an error if not.
  16323.         POP     HL              ; else restore
  16324.         POP     DE              ; registers.
  16325.         RET                     ; return with BC set at 5.
  16326.  
  16327. ; ------------
  16328. ; Stack number
  16329. ; ------------
  16330. ; This routine is called to stack a hidden floating point number found in
  16331. ; a BASIC line. It is also called to stack a numeric variable value, and
  16332. ; from BEEP, to stack an entry in the semi-tone table. It is not part of the
  16333. ; calculator suite of routines.
  16334. ; On entry HL points to the number to be stacked.
  16335.  
  16336. ;; STACK-NUM
  16337. L33B4:  LD      DE,($5C65)      ; load destination from STKEND system variable.
  16338.         CALL    L33C0           ; routine MOVE-FP puts on calculator stack
  16339.                                 ; with a memory check.
  16340.         LD      ($5C65),DE      ; set STKEND to next free location.
  16341.         RET                     ; return.
  16342.  
  16343. ; ---------------------------------
  16344. ; Move a floating point number (31)
  16345. ; ---------------------------------
  16346. ; This simple routine is a 5-byte LDIR instruction
  16347. ; that incorporates a memory check.
  16348. ; When used as a calculator literal it duplicates the last value on the
  16349. ; calculator stack.
  16350. ; Unary so on entry HL points to last value, DE to stkend
  16351.  
  16352. ;; duplicate
  16353. ;; MOVE-FP
  16354. L33C0:  CALL    L33A9           ; routine TEST-5-SP test free memory
  16355.                                 ; and sets BC to 5.
  16356.         LDIR                    ; copy the five bytes.
  16357.         RET                     ; return with DE addressing new STKEND
  16358.                                 ; and HL addressing new last value.
  16359.  
  16360. ; -------------------
  16361. ; Stack literals ($34)
  16362. ; -------------------
  16363. ; When a calculator subroutine needs to put a value on the calculator
  16364. ; stack that is not a regular constant this routine is called with a
  16365. ; variable number of following data bytes that convey to the routine
  16366. ; the integer or floating point form as succinctly as is possible.
  16367.  
  16368. ;; stk-data
  16369. L33C6:  LD      H,D             ; transfer STKEND
  16370.         LD      L,E             ; to HL for result.
  16371.  
  16372. ;; STK-CONST
  16373. L33C8:  CALL    L33A9           ; routine TEST-5-SP tests that room exists
  16374.                                 ; and sets BC to $05.
  16375.  
  16376.         EXX                     ; switch to alternate set
  16377.         PUSH    HL              ; save the pointer to next literal on stack
  16378.         EXX                     ; switch back to main set
  16379.  
  16380.         EX      (SP),HL         ; pointer to HL, destination to stack.
  16381.  
  16382.         PUSH    BC              ; save BC - value 5 from test room ??.
  16383.  
  16384.         LD      A,(HL)          ; fetch the byte following 'stk-data'
  16385.         AND     $C0             ; isolate bits 7 and 6
  16386.         RLCA                    ; rotate
  16387.         RLCA                    ; to bits 1 and 0  range $00 - $03.
  16388.         LD      C,A             ; transfer to C
  16389.         INC     C               ; and increment to give number of bytes
  16390.                                 ; to read. $01 - $04
  16391.         LD      A,(HL)          ; reload the first byte
  16392.         AND     $3F             ; mask off to give possible exponent.
  16393.         JR      NZ,L33DE        ; forward to FORM-EXP if it was possible to
  16394.                                 ; include the exponent.
  16395.  
  16396. ; else byte is just a byte count and exponent comes next.
  16397.  
  16398.         INC     HL              ; address next byte and
  16399.         LD      A,(HL)          ; pick up the exponent ( - $50).
  16400.  
  16401. ;; FORM-EXP
  16402. L33DE:  ADD     A,$50           ; now add $50 to form actual exponent
  16403.         LD      (DE),A          ; and load into first destination byte.
  16404.         LD      A,$05           ; load accumulator with $05 and
  16405.         SUB     C               ; subtract C to give count of trailing
  16406.                                 ; zeros plus one.
  16407.         INC     HL              ; increment source
  16408.         INC     DE              ; increment destination
  16409.         LD      B,$00           ; prepare to copy
  16410.         LDIR                    ; copy C bytes
  16411.  
  16412.         POP     BC              ; restore 5 counter to BC ??.
  16413.  
  16414.         EX      (SP),HL         ; put HL on stack as next literal pointer
  16415.                                 ; and the stack value - result pointer -
  16416.                                 ; to HL.
  16417.  
  16418.         EXX                     ; switch to alternate set.
  16419.         POP     HL              ; restore next literal pointer from stack
  16420.                                 ; to H'L'.
  16421.         EXX                     ; switch back to main set.
  16422.  
  16423.         LD      B,A             ; zero count to B
  16424.         XOR     A               ; clear accumulator
  16425.  
  16426. ;; STK-ZEROS
  16427. L33F1:  DEC     B               ; decrement B counter
  16428.         RET     Z               ; return if zero.          >>
  16429.                                 ; DE points to new STKEND
  16430.                                 ; HL to new number.
  16431.  
  16432.         LD      (DE),A          ; else load zero to destination
  16433.         INC     DE              ; increase destination
  16434.         JR      L33F1           ; loop back to STK-ZEROS until done.
  16435.  
  16436. ; -------------------------------
  16437. ; THE 'SKIP CONSTANTS' SUBROUTINE
  16438. ; -------------------------------
  16439. ; This routine traverses variable-length entries in the table of constants,
  16440. ; stacking intermediate, unwanted constants onto a dummy calculator stack,
  16441. ; in the first five bytes of ROM. The destination DE normally points to the
  16442. ; end of the calculator stack which might be in the normal place or in the
  16443. ; system variables area during E-LINE-NO; INT-TO-FP; stk-ten. In any case,
  16444. ; it would be simpler all round if the routine just shoved unwanted values
  16445. ; where it is going to stick the wanted value.
  16446. ; The instruction LD DE, $0000 can be removed.
  16447.  
  16448. ;; SKIP-CONS
  16449. L33F7:  AND     A               ; test if initially zero.
  16450.  
  16451. ;; SKIP-NEXT
  16452. L33F8:  RET     Z               ; return if zero.          >>
  16453.  
  16454.         PUSH    AF              ; save count.
  16455.         PUSH    DE              ; and normal STKEND
  16456.  
  16457.         LD      DE,$0000        ; dummy value for STKEND at start of ROM
  16458.                                 ; Note. not a fault but this has to be
  16459.                                 ; moved elsewhere when running in RAM.
  16460.                                 ; e.g. with Expandor Systems 'Soft ROM'.
  16461.                                 ; Better still, write to the normal place.
  16462.         CALL    L33C8           ; routine STK-CONST works through variable
  16463.                                 ; length records.
  16464.  
  16465.         POP     DE              ; restore real STKEND
  16466.         POP     AF              ; restore count
  16467.         DEC     A               ; decrease
  16468.         JR      L33F8           ; loop back to SKIP-NEXT
  16469.  
  16470. ; ---------------
  16471. ; Memory location
  16472. ; ---------------
  16473. ; This routine, when supplied with a base address in HL and an index in A
  16474. ; will calculate the address of the A'th entry, where each entry occupies
  16475. ; five bytes. It is used for reading the semi-tone table and addressing
  16476. ; floating-point numbers in the calculator's memory area.
  16477.  
  16478. ;; LOC-MEM
  16479. L3406:  LD      C,A             ; store the original number $00-$1F.
  16480.         RLCA                    ; double.
  16481.         RLCA                    ; quadruple.
  16482.         ADD     A,C             ; now add original to multiply by five.
  16483.  
  16484.         LD      C,A             ; place the result in C.
  16485.         LD      B,$00           ; set B to 0.
  16486.         ADD     HL,BC           ; add to form address of start of number in HL.
  16487.         RET                     ; return.
  16488.  
  16489. ; ------------------------------
  16490. ; Get from memory area ($E0 etc.)
  16491. ; ------------------------------
  16492. ; Literals $E0 to $FF
  16493. ; A holds $00-$1F offset.
  16494. ; The calculator stack increases by 5 bytes.
  16495.  
  16496. ;; get-mem-xx
  16497. L340F:  PUSH    DE              ; save STKEND
  16498.         LD      HL,($5C68)      ; MEM is base address of the memory cells.
  16499.         CALL    L3406           ; routine LOC-MEM so that HL = first byte
  16500.         CALL    L33C0           ; routine MOVE-FP moves 5 bytes with memory
  16501.                                 ; check.
  16502.                                 ; DE now points to new STKEND.
  16503.         POP     HL              ; original STKEND is now RESULT pointer.
  16504.         RET                     ; return.
  16505.  
  16506. ; --------------------------
  16507. ; Stack a constant (A0 etc.)
  16508. ; --------------------------
  16509. ; This routine allows a one-byte instruction to stack up to 32 constants
  16510. ; held in short form in a table of constants. In fact only 5 constants are
  16511. ; required. On entry the A register holds the literal ANDed with 1F.
  16512. ; It isn't very efficient and it would have been better to hold the
  16513. ; numbers in full, five byte form and stack them in a similar manner
  16514. ; to that used for semi-tone table values.
  16515.  
  16516. ;; stk-const-xx
  16517. L341B:  LD      H,D             ; save STKEND - required for result
  16518.         LD      L,E             ;
  16519.         EXX                     ; swap
  16520.         PUSH    HL              ; save pointer to next literal
  16521.         LD      HL,L32C5        ; Address: stk-zero - start of table of
  16522.                                 ; constants
  16523.         EXX                     ;
  16524.         CALL    L33F7           ; routine SKIP-CONS
  16525.         CALL    L33C8           ; routine STK-CONST
  16526.         EXX                     ;
  16527.         POP     HL              ; restore pointer to next literal.
  16528.         EXX                     ;
  16529.         RET                     ; return.
  16530.  
  16531. ; --------------------------------
  16532. ; Store in a memory area ($C0 etc.)
  16533. ; --------------------------------
  16534. ; Offsets $C0 to $DF
  16535. ; Although 32 memory storage locations can be addressed, only six
  16536. ; $C0 to $C5 are required by the ROM and only the thirty bytes (6*5)
  16537. ; required for these are allocated. Spectrum programmers who wish to
  16538. ; use the floating point routines from assembly language may wish to
  16539. ; alter the system variable MEM to point to 160 bytes of RAM to have
  16540. ; use the full range available.
  16541. ; A holds the derived offset $00-$1F.
  16542. ; This is a unary operation, so on entry HL points to the last value and DE
  16543. ; points to STKEND.
  16544.  
  16545. ;; st-mem-xx
  16546. L342D:  PUSH    HL              ; save the result pointer.
  16547.         EX      DE,HL           ; transfer to DE.
  16548.         LD      HL,($5C68)      ; fetch MEM the base of memory area.
  16549.         CALL    L3406           ; routine LOC-MEM sets HL to the destination.
  16550.         EX      DE,HL           ; swap - HL is start, DE is destination.
  16551.         CALL    L33C0           ; routine MOVE-FP.
  16552.                                 ; note. a short ld bc,5; ldir
  16553.                                 ; the embedded memory check is not required
  16554.                                 ; so these instructions would be faster.
  16555.         EX      DE,HL           ; DE = STKEND
  16556.         POP     HL              ; restore original result pointer
  16557.         RET                     ; return.
  16558.  
  16559. ; ------------------------------------
  16560. ; Swap first number with second number
  16561. ; ------------------------------------
  16562. ; This routine exchanges the last two values on the calculator stack
  16563. ; On entry, as always with binary operations,
  16564. ; HL=first number, DE=second number
  16565. ; On exit, HL=result, DE=stkend.
  16566.  
  16567. ;; exchange
  16568. L343C:  LD      B,$05           ; there are five bytes to be swapped
  16569.  
  16570. ; start of loop.
  16571.  
  16572. ;; SWAP-BYTE
  16573. L343E:  LD      A,(DE)          ; each byte of second
  16574.         LD      C,(HL)          ; each byte of first
  16575.         EX      DE,HL           ; swap pointers
  16576.         LD      (DE),A          ; store each byte of first
  16577.         LD      (HL),C          ; store each byte of second
  16578.         INC     HL              ; advance both
  16579.         INC     DE              ; pointers.
  16580.         DJNZ    L343E           ; loop back to SWAP-BYTE until all 5 done.
  16581.  
  16582.         EX      DE,HL           ; even up the exchanges
  16583.                                 ; so that DE addresses STKEND.
  16584.         RET                     ; return.
  16585.  
  16586. ; --------------------------
  16587. ; Series generator (86 etc.)
  16588. ; --------------------------
  16589. ; The Spectrum uses Chebyshev polynomials to generate approximations for
  16590. ; SIN, ATN, LN and EXP. These are named after the Russian mathematician
  16591. ; Pafnuty Chebyshev, born in 1821, who did much pioneering work on numerical
  16592. ; series. As far as calculators are concerned, Chebyshev polynomials have an
  16593. ; advantage over other series, for example the Taylor series, as they can
  16594. ; reach an approximation in just six iterations for SIN, eight for EXP and
  16595. ; twelve for LN and ATN. The mechanics of the routine are interesting but
  16596. ; for full treatment of how these are generated with demonstrations in
  16597. ; Sinclair BASIC see "The Complete Spectrum ROM Disassembly" by Dr Ian Logan
  16598. ; and Dr Frank O'Hara, published 1983 by Melbourne House.
  16599.  
  16600. ;; series-xx
  16601. L3449:  LD      B,A             ; parameter $00 - $1F to B counter
  16602.         CALL    L335E           ; routine GEN-ENT-1 is called.
  16603.                                 ; A recursive call to a special entry point
  16604.                                 ; in the calculator that puts the B register
  16605.                                 ; in the system variable BREG. The return
  16606.                                 ; address is the next location and where
  16607.                                 ; the calculator will expect its first
  16608.                                 ; instruction - now pointed to by HL'.
  16609.                                 ; The previous pointer to the series of
  16610.                                 ; five-byte numbers goes on the machine stack.
  16611.  
  16612. ; The initialization phase.
  16613.  
  16614.         DB    $31             ;;duplicate       x,x
  16615.         DB    $0F             ;;addition        x+x
  16616.         DB    $C0             ;;st-mem-0        x+x
  16617.         DB    $02             ;;delete          .
  16618.         DB    $A0             ;;stk-zero        0
  16619.         DB    $C2             ;;st-mem-2        0
  16620.  
  16621. ; a loop is now entered to perform the algebraic calculation for each of
  16622. ; the numbers in the series
  16623.  
  16624. ;; G-LOOP
  16625. L3453:  DB    $31             ;;duplicate       v,v.
  16626.         DB    $E0             ;;get-mem-0       v,v,x+2
  16627.         DB    $04             ;;multiply        v,v*x+2
  16628.         DB    $E2             ;;get-mem-2       v,v*x+2,v
  16629.         DB    $C1             ;;st-mem-1
  16630.         DB    $03             ;;subtract
  16631.         DB    $38             ;;end-calc
  16632.  
  16633. ; the previous pointer is fetched from the machine stack to H'L' where it
  16634. ; addresses one of the numbers of the series following the series literal.
  16635.  
  16636.         CALL    L33C6           ; routine STK-DATA is called directly to
  16637.                                 ; push a value and advance H'L'.
  16638.         CALL    L3362           ; routine GEN-ENT-2 recursively re-enters
  16639.                                 ; the calculator without disturbing
  16640.                                 ; system variable BREG
  16641.                                 ; H'L' value goes on the machine stack and is
  16642.                                 ; then loaded as usual with the next address.
  16643.  
  16644.         DB    $0F             ;;addition
  16645.         DB    $01             ;;exchange
  16646.         DB    $C2             ;;st-mem-2
  16647.         DB    $02             ;;delete
  16648.  
  16649.         DB    $35             ;;dec-jr-nz
  16650.         DB    $EE             ;;back to L3453, G-LOOP
  16651.  
  16652. ; when the counted loop is complete the final subtraction yields the result
  16653. ; for example SIN X.
  16654.  
  16655.         DB    $E1             ;;get-mem-1
  16656.         DB    $03             ;;subtract
  16657.         DB    $38             ;;end-calc
  16658.  
  16659.         RET                     ; return with H'L' pointing to location
  16660.                                 ; after last number in series.
  16661.  
  16662. ; -----------------------
  16663. ; Absolute magnitude (2A)
  16664. ; -----------------------
  16665. ; This calculator literal finds the absolute value of the last value,
  16666. ; integer or floating point, on calculator stack.
  16667.  
  16668. ;; abs
  16669. L346A:  LD      B,$FF           ; signal abs
  16670.         JR      L3474           ; forward to NEG-TEST
  16671.  
  16672. ; -----------------------
  16673. ; Handle unary minus (1B)
  16674. ; -----------------------
  16675. ; Unary so on entry HL points to last value, DE to STKEND.
  16676.  
  16677. ;; NEGATE
  16678. ;; negate
  16679. L346E:  CALL    L34E9           ; call routine TEST-ZERO and
  16680.         RET     C               ; return if so leaving zero unchanged.
  16681.  
  16682.         LD      B,$00           ; signal negate required before joining
  16683.                                 ; common code.
  16684.  
  16685. ;; NEG-TEST
  16686. L3474:  LD      A,(HL)          ; load first byte and
  16687.         AND     A               ; test for zero
  16688.         JR      Z,L3483         ; forward to INT-CASE if a small integer
  16689.  
  16690. ; for floating point numbers a single bit denotes the sign.
  16691.  
  16692.         INC     HL              ; address the first byte of mantissa.
  16693.         LD      A,B             ; action flag $FF=abs, $00=neg.
  16694.         AND     $80             ; now         $80      $00
  16695.         OR      (HL)            ; sets bit 7 for abs
  16696.         RLA                     ; sets carry for abs and if number negative
  16697.         CCF                     ; complement carry flag
  16698.         RRA                     ; and rotate back in altering sign
  16699.         LD      (HL),A          ; put the altered adjusted number back
  16700.         DEC     HL              ; HL points to result
  16701.         RET                     ; return with DE unchanged
  16702.  
  16703. ; ---
  16704.  
  16705. ; for integer numbers an entire byte denotes the sign.
  16706.  
  16707. ;; INT-CASE
  16708. L3483:  PUSH    DE              ; save STKEND.
  16709.  
  16710.         PUSH    HL              ; save pointer to the last value/result.
  16711.  
  16712.         CALL    L2D7F           ; routine INT-FETCH puts integer in DE
  16713.                                 ; and the sign in C.
  16714.  
  16715.         POP     HL              ; restore the result pointer.
  16716.  
  16717.         LD      A,B             ; $FF=abs, $00=neg
  16718.         OR      C               ; $FF for abs, no change neg
  16719.         CPL                     ; $00 for abs, switched for neg
  16720.         LD      C,A             ; transfer result to sign byte.
  16721.  
  16722.         CALL    L2D8E           ; routine INT-STORE to re-write the integer.
  16723.  
  16724.         POP     DE              ; restore STKEND.
  16725.         RET                     ; return.
  16726.  
  16727. ; -----------
  16728. ; Signum (29)
  16729. ; -----------
  16730. ; This routine replaces the last value on the calculator stack,
  16731. ; which may be in floating point or integer form, with the integer values
  16732. ; zero if zero, with one if positive and  with -minus one if negative.
  16733.  
  16734. ;; sgn
  16735. L3492:  CALL    L34E9           ; call routine TEST-ZERO and
  16736.         RET     C               ; exit if so as no change is required.
  16737.  
  16738.         PUSH    DE              ; save pointer to STKEND.
  16739.  
  16740.         LD      DE,$0001        ; the result will be 1.
  16741.         INC     HL              ; skip over the exponent.
  16742.         RL      (HL)            ; rotate the sign bit into the carry flag.
  16743.         DEC     HL              ; step back to point to the result.
  16744.         SBC     A,A             ; byte will be $FF if negative, $00 if positive.
  16745.         LD      C,A             ; store the sign byte in the C register.
  16746.         CALL    L2D8E           ; routine INT-STORE to overwrite the last
  16747.                                 ; value with 0001 and sign.
  16748.  
  16749.         POP     DE              ; restore STKEND.
  16750.         RET                     ; return.
  16751.  
  16752. ; -----------------------
  16753. ; Handle IN function (2C)
  16754. ; -----------------------
  16755. ; This function reads a byte from an input port.
  16756.  
  16757. ;; in
  16758. L34A5:  CALL    L1E99           ; routine FIND-INT2 puts port address in BC.
  16759.                                 ; all 16 bits are put on the address line.
  16760.         IN      A,(C)           ; read the port.
  16761.  
  16762.         JR      L34B0           ; exit to STACK-A (via IN-PK-STK to save a byte
  16763.                                 ; of instruction code).
  16764.  
  16765. ; -------------------------
  16766. ; Handle PEEK function (2B)
  16767. ; -------------------------
  16768. ; This function returns the contents of a memory address.
  16769. ; The entire address space can be peeked including the ROM.
  16770.  
  16771. ;; peek
  16772. L34AC:  CALL    L1E99           ; routine FIND-INT2 puts address in BC.
  16773.         LD      A,(BC)          ; load contents into A register.
  16774.  
  16775. ;; IN-PK-STK
  16776. L34B0:  JP      L2D28           ; exit via STACK-A to put value on the
  16777.                                 ; calculator stack.
  16778.  
  16779. ; ---------------
  16780. ; USR number (2D)
  16781. ; ---------------
  16782. ; The USR function followed by a number 0-65535 is the method by which
  16783. ; the Spectrum invokes machine code programs. This function returns the
  16784. ; contents of the BC register pair.
  16785. ; Note. that STACK-BC re-initializes the IY register if a user-written
  16786. ; program has altered it.
  16787.  
  16788. ;; usr-no
  16789. L34B3:  CALL    L1E99           ; routine FIND-INT2 to fetch the
  16790.                                 ; supplied address into BC.
  16791.  
  16792.         LD      HL,L2D2B        ; address: STACK-BC is
  16793.         PUSH    HL              ; pushed onto the machine stack.
  16794.         PUSH    BC              ; then the address of the machine code
  16795.                                 ; routine.
  16796.  
  16797.         RET                     ; make an indirect jump to the routine
  16798.                                 ; and, hopefully, to STACK-BC also.
  16799.  
  16800. ; ---------------
  16801. ; USR string (19)
  16802. ; ---------------
  16803. ; The user function with a one-character string argument, calculates the
  16804. ; address of the User Defined Graphic character that is in the string.
  16805. ; As an alternative, the ASCII equivalent, upper or lower case,
  16806. ; may be supplied. This provides a user-friendly method of redefining
  16807. ; the 21 User Definable Graphics e.g.
  16808. ; POKE USR "a", BIN 10000000 will put a dot in the top left corner of the
  16809. ; character 144.
  16810. ; Note. the curious double check on the range. With 26 UDGs the first check
  16811. ; only is necessary. With anything less the second check only is required.
  16812. ; It is highly likely that the first check was written by Steven Vickers.
  16813.  
  16814. ;; usr-$
  16815. L34BC:  CALL    L2BF1           ; routine STK-FETCH fetches the string
  16816.                                 ; parameters.
  16817.         DEC     BC              ; decrease BC by
  16818.         LD      A,B             ; one to test
  16819.         OR      C               ; the length.
  16820.         JR      NZ,L34E7        ; to REPORT-A if not a single character.
  16821.  
  16822.         LD      A,(DE)          ; fetch the character
  16823.         CALL    L2C8D           ; routine ALPHA sets carry if 'A-Z' or 'a-z'.
  16824.         JR      C,L34D3         ; forward to USR-RANGE if ASCII.
  16825.  
  16826.         SUB     $90             ; make udgs range 0-20d
  16827.         JR      C,L34E7         ; to REPORT-A if too low. e.g. usr " ".
  16828.  
  16829.         CP      $15             ; Note. this test is not necessary.
  16830.         JR      NC,L34E7        ; to REPORT-A if higher than 20.
  16831.  
  16832.         INC     A               ; make range 1-21d to match LSBs of ASCII
  16833.  
  16834. ;; USR-RANGE
  16835. L34D3:  DEC     A               ; make range of bits 0-4 start at zero
  16836.         ADD     A,A             ; multiply by eight
  16837.         ADD     A,A             ; and lose any set bits
  16838.         ADD     A,A             ; range now 0 - 25*8
  16839.         CP      $A8             ; compare to 21*8
  16840.         JR      NC,L34E7        ; to REPORT-A if originally higher
  16841.                                 ; than 'U','u' or graphics U.
  16842.  
  16843.         LD      BC,($5C7B)      ; fetch the UDG system variable value.
  16844.         ADD     A,C             ; add the offset to character
  16845.         LD      C,A             ; and store back in register C.
  16846.         JR      NC,L34E4        ; forward to USR-STACK if no overflow.
  16847.  
  16848.         INC     B               ; increment high byte.
  16849.  
  16850. ;; USR-STACK
  16851. L34E4:  JP      L2D2B           ; jump back and exit via STACK-BC to store
  16852.  
  16853. ; ---
  16854.  
  16855. ;; REPORT-A
  16856. L34E7:  RST     08H             ; ERROR-1
  16857.         DB    $09             ; Error Report: Invalid argument
  16858.  
  16859. ; -------------
  16860. ; Test for zero
  16861. ; -------------
  16862. ; Test if top value on calculator stack is zero.
  16863. ; The carry flag is set if the last value is zero but no registers are altered.
  16864. ; All five bytes will be zero but first four only need be tested.
  16865. ; On entry HL points to the exponent the first byte of the value.
  16866.  
  16867. ;; TEST-ZERO
  16868. L34E9:  PUSH    HL              ; preserve HL which is used to address.
  16869.         PUSH    BC              ; preserve BC which is used as a store.
  16870.         LD      B,A             ; preserve A in B.
  16871.  
  16872.         LD      A,(HL)          ; load first byte to accumulator
  16873.         INC     HL              ; advance.
  16874.         OR      (HL)            ; OR with second byte and clear carry.
  16875.         INC     HL              ; advance.
  16876.         OR      (HL)            ; OR with third byte.
  16877.         INC     HL              ; advance.
  16878.         OR      (HL)            ; OR with fourth byte.
  16879.  
  16880.         LD      A,B             ; restore A without affecting flags.
  16881.         POP     BC              ; restore the saved
  16882.         POP     HL              ; registers.
  16883.  
  16884.         RET     NZ              ; return if not zero and with carry reset.
  16885.  
  16886.         SCF                     ; set the carry flag.
  16887.         RET                     ; return with carry set if zero.
  16888.  
  16889. ; -----------------------
  16890. ; Greater than zero ($37)
  16891. ; -----------------------
  16892. ; Test if the last value on the calculator stack is greater than zero.
  16893. ; This routine is also called directly from the end-tests of the comparison
  16894. ; routine.
  16895.  
  16896. ;; GREATER-0
  16897. ;; greater-0
  16898. L34F9:  CALL    L34E9           ; routine TEST-ZERO
  16899.         RET     C               ; return if was zero as this
  16900.                                 ; is also the Boolean 'false' value.
  16901.  
  16902.         LD      A,$FF           ; prepare XOR mask for sign bit
  16903.         JR      L3507           ; forward to SIGN-TO-C
  16904.                                 ; to put sign in carry
  16905.                                 ; (carry will become set if sign is positive)
  16906.                                 ; and then overwrite location with 1 or 0
  16907.                                 ; as appropriate.
  16908.  
  16909. ; ------------------------
  16910. ; Handle NOT operator ($30)
  16911. ; ------------------------
  16912. ; This overwrites the last value with 1 if it was zero else with zero
  16913. ; if it was any other value.
  16914. ;
  16915. ; e.g NOT 0 returns 1, NOT 1 returns 0, NOT -3 returns 0.
  16916. ;
  16917. ; The subroutine is also called directly from the end-tests of the comparison
  16918. ; operator.
  16919.  
  16920. ;; NOT
  16921. ;; not
  16922. L3501:  CALL    L34E9           ; routine TEST-ZERO sets carry if zero
  16923.  
  16924.         JR      L350B           ; to FP-0/1 to overwrite operand with
  16925.                                 ; 1 if carry is set else to overwrite with zero.
  16926.  
  16927. ; -------------------
  16928. ; Less than zero (36)
  16929. ; -------------------
  16930. ; Destructively test if last value on calculator stack is less than zero.
  16931. ; Bit 7 of second byte will be set if so.
  16932.  
  16933. ;; less-0
  16934. L3506:  XOR     A               ; set xor mask to zero
  16935.                                 ; (carry will become set if sign is negative).
  16936.  
  16937. ; transfer sign of mantissa to Carry Flag.
  16938.  
  16939. ;; SIGN-TO-C
  16940. L3507:  INC     HL              ; address 2nd byte.
  16941.         XOR     (HL)            ; bit 7 of HL will be set if number is negative.
  16942.         DEC     HL              ; address 1st byte again.
  16943.         RLCA                    ; rotate bit 7 of A to carry.
  16944.  
  16945. ; -----------
  16946. ; Zero or one
  16947. ; -----------
  16948. ; This routine places an integer value of zero or one at the addressed location
  16949. ; of the calculator stack or MEM area.  The value one is written if carry is
  16950. ; set on entry else zero.
  16951.  
  16952. ;; FP-0/1
  16953. L350B:  PUSH    HL              ; save pointer to the first byte
  16954.         LD      A,$00           ; load accumulator with zero - without
  16955.                                 ; disturbing flags.
  16956.         LD      (HL),A          ; zero to first byte
  16957.         INC     HL              ; address next
  16958.         LD      (HL),A          ; zero to 2nd byte
  16959.         INC     HL              ; address low byte of integer
  16960.         RLA                     ; carry to bit 0 of A
  16961.         LD      (HL),A          ; load one or zero to low byte.
  16962.         RRA                     ; restore zero to accumulator.
  16963.         INC     HL              ; address high byte of integer.
  16964.         LD      (HL),A          ; put a zero there.
  16965.         INC     HL              ; address fifth byte.
  16966.         LD      (HL),A          ; put a zero there.
  16967.         POP     HL              ; restore pointer to the first byte.
  16968.         RET                     ; return.
  16969.  
  16970. ; -----------------------
  16971. ; Handle OR operator (07)
  16972. ; -----------------------
  16973. ; The Boolean OR operator. eg. X OR Y
  16974. ; The result is zero if both values are zero else a non-zero value.
  16975. ;
  16976. ; e.g.    0 OR 0  returns 0.
  16977. ;        -3 OR 0  returns -3.
  16978. ;         0 OR -3 returns 1.
  16979. ;        -3 OR 2  returns 1.
  16980. ;
  16981. ; A binary operation.
  16982. ; On entry HL points to first operand (X) and DE to second operand (Y).
  16983.  
  16984. ;; or
  16985. L351B:  EX      DE,HL           ; make HL point to second number
  16986.         CALL    L34E9           ; routine TEST-ZERO
  16987.         EX      DE,HL           ; restore pointers
  16988.         RET     C               ; return if result was zero - first operand,
  16989.                                 ; now the last value, is the result.
  16990.  
  16991.         SCF                     ; set carry flag
  16992.         JR      L350B           ; back to FP-0/1 to overwrite the first operand
  16993.                                 ; with the value 1.
  16994.  
  16995.  
  16996. ; -----------------------------
  16997. ; Handle number AND number (08)
  16998. ; -----------------------------
  16999. ; The Boolean AND operator.
  17000. ;
  17001. ; e.g.    -3 AND 2  returns -3.
  17002. ;         -3 AND 0  returns 0.
  17003. ;          0 and -2 returns 0.
  17004. ;          0 and 0  returns 0.
  17005. ;
  17006. ; Compare with OR routine above.
  17007.  
  17008. ;; no-&-no
  17009. L3524:  EX      DE,HL           ; make HL address second operand.
  17010.  
  17011.         CALL    L34E9           ; routine TEST-ZERO sets carry if zero.
  17012.  
  17013.         EX      DE,HL           ; restore pointers.
  17014.         RET     NC              ; return if second non-zero, first is result.
  17015.  
  17016. ;
  17017.  
  17018.         AND     A               ; else clear carry.
  17019.         JR      L350B           ; back to FP-0/1 to overwrite first operand
  17020.                                 ; with zero for return value.
  17021.  
  17022. ; -----------------------------
  17023. ; Handle string AND number (10)
  17024. ; -----------------------------
  17025. ; e.g. "You Win" AND score>99 will return the string if condition is true
  17026. ; or the null string if false.
  17027.  
  17028. ;; str-&-no
  17029. L352D:  EX      DE,HL           ; make HL point to the number.
  17030.         CALL    L34E9           ; routine TEST-ZERO.
  17031.         EX      DE,HL           ; restore pointers.
  17032.         RET     NC              ; return if number was not zero - the string
  17033.                                 ; is the result.
  17034.  
  17035. ; if the number was zero (false) then the null string must be returned by
  17036. ; altering the length of the string on the calculator stack to zero.
  17037.  
  17038.         PUSH    DE              ; save pointer to the now obsolete number
  17039.                                 ; (which will become the new STKEND)
  17040.  
  17041.         DEC     DE              ; point to the 5th byte of string descriptor.
  17042.         XOR     A               ; clear the accumulator.
  17043.         LD      (DE),A          ; place zero in high byte of length.
  17044.         DEC     DE              ; address low byte of length.
  17045.         LD      (DE),A          ; place zero there - now the null string.
  17046.  
  17047.         POP     DE              ; restore pointer - new STKEND.
  17048.         RET                     ; return.
  17049.  
  17050. ; -----------------------------------
  17051. ; Perform comparison ($09-$0E, $11-$16)
  17052. ; -----------------------------------
  17053. ; True binary operations.
  17054. ;
  17055. ; A single entry point is used to evaluate six numeric and six string
  17056. ; comparisons. On entry, the calculator literal is in the B register and
  17057. ; the two numeric values, or the two string parameters, are on the
  17058. ; calculator stack.
  17059. ; The individual bits of the literal are manipulated to group similar
  17060. ; operations although the SUB 8 instruction does nothing useful and merely
  17061. ; alters the string test bit.
  17062. ; Numbers are compared by subtracting one from the other, strings are
  17063. ; compared by comparing every character until a mismatch, or the end of one
  17064. ; or both, is reached.
  17065. ;
  17066. ; Numeric Comparisons.
  17067. ; --------------------
  17068. ; The 'x>y' example is the easiest as it employs straight-thru logic.
  17069. ; Number y is subtracted from x and the result tested for greater-0 yielding
  17070. ; a final value 1 (true) or 0 (false).
  17071. ; For 'x<y' the same logic is used but the two values are first swapped on the
  17072. ; calculator stack.
  17073. ; For 'x=y' NOT is applied to the subtraction result yielding true if the
  17074. ; difference was zero and false with anything else.
  17075. ; The first three numeric comparisons are just the opposite of the last three
  17076. ; so the same processing steps are used and then a final NOT is applied.
  17077. ;
  17078. ; literal    Test   No  sub 8       ExOrNot  1st RRCA  exch sub  ?   End-Tests
  17079. ; =========  ====   == ======== === ======== ========  ==== ===  =  === === ===
  17080. ; no-l-eql   x<=y   09 00000001 dec 00000000 00000000  ---- x-y  ?  --- >0? NOT
  17081. ; no-gr-eql  x>=y   0A 00000010 dec 00000001 10000000c swap y-x  ?  --- >0? NOT
  17082. ; nos-neql   x<>y   0B 00000011 dec 00000010 00000001  ---- x-y  ?  NOT --- NOT
  17083. ; no-grtr    x>y    0C 00000100  -  00000100 00000010  ---- x-y  ?  --- >0? ---
  17084. ; no-less    x<y    0D 00000101  -  00000101 10000010c swap y-x  ?  --- >0? ---
  17085. ; nos-eql    x=y    0E 00000110  -  00000110 00000011  ---- x-y  ?  NOT --- ---
  17086. ;
  17087. ;                                                           comp -> C/F
  17088. ;                                                           ====    ===
  17089. ; str-l-eql  x$<=y$ 11 00001001 dec 00001000 00000100  ---- x$y$ 0  !or >0? NOT
  17090. ; str-gr-eql x$>=y$ 12 00001010 dec 00001001 10000100c swap y$x$ 0  !or >0? NOT
  17091. ; strs-neql  x$<>y$ 13 00001011 dec 00001010 00000101  ---- x$y$ 0  !or >0? NOT
  17092. ; str-grtr   x$>y$  14 00001100  -  00001100 00000110  ---- x$y$ 0  !or >0? ---
  17093. ; str-less   x$<y$  15 00001101  -  00001101 10000110c swap y$x$ 0  !or >0? ---
  17094. ; strs-eql   x$=y$  16 00001110  -  00001110 00000111  ---- x$y$ 0  !or >0? ---
  17095. ;
  17096. ; String comparisons are a little different in that the eql/neql carry flag
  17097. ; from the 2nd RRCA is, as before, fed into the first of the end tests but
  17098. ; along the way it gets modified by the comparison process. The result on the
  17099. ; stack always starts off as zero and the carry fed in determines if NOT is
  17100. ; applied to it. So the only time the greater-0 test is applied is if the
  17101. ; stack holds zero which is not very efficient as the test will always yield
  17102. ; zero. The most likely explanation is that there were once separate end tests
  17103. ; for numbers and strings.
  17104.  
  17105. ;; no-l-eql, etc.
  17106. L353B:  LD      A,B             ; transfer literal to accumulator.
  17107.         SUB     $08             ; subtract eight - which is not useful.
  17108.  
  17109.         BIT     2,A             ; isolate '>', '<', '='.
  17110.  
  17111.         JR      NZ,L3543        ; skip to EX-OR-NOT with these.
  17112.  
  17113.         DEC     A               ; else make $00-$02, $08-$0A to match bits 0-2.
  17114.  
  17115. ;; EX-OR-NOT
  17116. L3543:  RRCA                    ; the first RRCA sets carry for a swap.
  17117.         JR      NC,L354E        ; forward to NU-OR-STR with other 8 cases
  17118.  
  17119. ; for the other 4 cases the two values on the calculator stack are exchanged.
  17120.  
  17121.         PUSH    AF              ; save A and carry.
  17122.         PUSH    HL              ; save HL - pointer to first operand.
  17123.                                 ; (DE points to second operand).
  17124.  
  17125.         CALL    L343C           ; routine exchange swaps the two values.
  17126.                                 ; (HL = second operand, DE = STKEND)
  17127.  
  17128.         POP     DE              ; DE = first operand
  17129.         EX      DE,HL           ; as we were.
  17130.         POP     AF              ; restore A and carry.
  17131.  
  17132. ; Note. it would be better if the 2nd RRCA preceded the string test.
  17133. ; It would save two duplicate bytes and if we also got rid of that sub 8
  17134. ; at the beginning we wouldn't have to alter which bit we test.
  17135.  
  17136. ;; NU-OR-STR
  17137. L354E:  BIT     2,A             ; test if a string comparison.
  17138.         JR      NZ,L3559        ; forward to STRINGS if so.
  17139.  
  17140. ; continue with numeric comparisons.
  17141.  
  17142.         RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17143.         PUSH    AF              ; save A and carry
  17144.  
  17145.         CALL    L300F           ; routine subtract leaves result on stack.
  17146.         JR      L358C           ; forward to END-TESTS
  17147.  
  17148. ; ---
  17149.  
  17150. ;; STRINGS
  17151. L3559:  RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17152.         PUSH    AF              ; save A and carry.
  17153.  
  17154.         CALL    L2BF1           ; routine STK-FETCH gets 2nd string params
  17155.         PUSH    DE              ; save start2 *.
  17156.         PUSH    BC              ; and the length.
  17157.  
  17158.         CALL    L2BF1           ; routine STK-FETCH gets 1st string
  17159.                                 ; parameters - start in DE, length in BC.
  17160.         POP     HL              ; restore length of second to HL.
  17161.  
  17162. ; A loop is now entered to compare, by subtraction, each corresponding character
  17163. ; of the strings. For each successful match, the pointers are incremented and
  17164. ; the lengths decreased and the branch taken back to here. If both string
  17165. ; remainders become null at the same time, then an exact match exists.
  17166.  
  17167. ;; BYTE-COMP
  17168. L3564:  LD      A,H             ; test if the second string
  17169.         OR      L               ; is the null string and hold flags.
  17170.  
  17171.         EX      (SP),HL         ; put length2 on stack, bring start2 to HL *.
  17172.         LD      A,B             ; hi byte of length1 to A
  17173.  
  17174.         JR      NZ,L3575        ; forward to SEC-PLUS if second not null.
  17175.  
  17176.         OR      C               ; test length of first string.
  17177.  
  17178. ;; SECND-LOW
  17179. L356B:  POP     BC              ; pop the second length off stack.
  17180.         JR      Z,L3572         ; forward to BOTH-NULL if first string is also
  17181.                                 ; of zero length.
  17182.  
  17183. ; the true condition - first is longer than second (SECND-LESS)
  17184.  
  17185.         POP     AF              ; restore carry (set if eql/neql)
  17186.         CCF                     ; complement carry flag.
  17187.                                 ; Note. equality becomes false.
  17188.                                 ; Inequality is true. By swapping or applying
  17189.                                 ; a terminal 'not', all comparisons have been
  17190.                                 ; manipulated so that this is success path.
  17191.         JR      L3588           ; forward to leave via STR-TEST
  17192.  
  17193. ; ---
  17194. ; the branch was here with a match
  17195.  
  17196. ;; BOTH-NULL
  17197. L3572:  POP     AF              ; restore carry - set for eql/neql
  17198.         JR      L3588           ; forward to STR-TEST
  17199.  
  17200. ; ---  
  17201. ; the branch was here when 2nd string not null and low byte of first is yet
  17202. ; to be tested.
  17203.  
  17204.  
  17205. ;; SEC-PLUS
  17206. L3575:  OR      C               ; test the length of first string.
  17207.         JR      Z,L3585         ; forward to FRST-LESS if length is zero.
  17208.  
  17209. ; both strings have at least one character left.
  17210.  
  17211.         LD      A,(DE)          ; fetch character of first string.
  17212.         SUB     (HL)            ; subtract with that of 2nd string.
  17213.         JR      C,L3585         ; forward to FRST-LESS if carry set
  17214.  
  17215.         JR      NZ,L356B        ; back to SECND-LOW and then STR-TEST
  17216.                                 ; if not exact match.
  17217.  
  17218.         DEC     BC              ; decrease length of 1st string.
  17219.         INC     DE              ; increment 1st string pointer.
  17220.  
  17221.         INC     HL              ; increment 2nd string pointer.
  17222.         EX      (SP),HL         ; swap with length on stack
  17223.         DEC     HL              ; decrement 2nd string length
  17224.         JR      L3564           ; back to BYTE-COMP
  17225.  
  17226. ; ---
  17227. ; the false condition.
  17228.  
  17229. ;; FRST-LESS
  17230. L3585:  POP     BC              ; discard length
  17231.         POP     AF              ; pop A
  17232.         AND     A               ; clear the carry for false result.
  17233.  
  17234. ; ---
  17235. ; exact match and x$>y$ rejoin here
  17236.  
  17237. ;; STR-TEST
  17238. L3588:  PUSH    AF              ; save A and carry
  17239.  
  17240.         RST     28H             ;; FP-CALC
  17241.         DB    $A0             ;;stk-zero      an initial false value.
  17242.         DB    $38             ;;end-calc
  17243.  
  17244. ; both numeric and string paths converge here.
  17245.  
  17246. ;; END-TESTS
  17247. L358C:  POP     AF              ; pop carry  - will be set if eql/neql
  17248.         PUSH    AF              ; save it again.
  17249.  
  17250.         CALL    C,L3501         ; routine NOT sets true(1) if equal(0)
  17251.                                 ; or, for strings, applies true result.
  17252.  
  17253.         POP     AF              ; pop carry and
  17254.         PUSH    AF              ; save A
  17255.  
  17256.         CALL    NC,L34F9        ; routine GREATER-0 tests numeric subtraction
  17257.                                 ; result but also needlessly tests the string
  17258.                                 ; value for zero - it must be.
  17259.  
  17260.         POP     AF              ; pop A
  17261.         RRCA                    ; the third RRCA - test for '<=', '>=' or '<>'.
  17262.         CALL    NC,L3501        ; apply a terminal NOT if so.
  17263.         RET                     ; return.
  17264.  
  17265. ; -------------------------
  17266. ; String concatenation ($17)
  17267. ; -------------------------
  17268. ; This literal combines two strings into one e.g. LET a$ = b$ + c$
  17269. ; The two parameters of the two strings to be combined are on the stack.
  17270.  
  17271. ;; strs-add
  17272. L359C:  CALL    L2BF1           ; routine STK-FETCH fetches string parameters
  17273.                                 ; and deletes calculator stack entry.
  17274.         PUSH    DE              ; save start address.
  17275.         PUSH    BC              ; and length.
  17276.  
  17277.         CALL    L2BF1           ; routine STK-FETCH for first string
  17278.         POP     HL              ; re-fetch first length
  17279.         PUSH    HL              ; and save again
  17280.         PUSH    DE              ; save start of second string
  17281.         PUSH    BC              ; and its length.
  17282.  
  17283.         ADD     HL,BC           ; add the two lengths.
  17284.         LD      B,H             ; transfer to BC
  17285.         LD      C,L             ; and create
  17286.         RST     30H             ; BC-SPACES in workspace.
  17287.                                 ; DE points to start of space.
  17288.  
  17289.         CALL    L2AB2           ; routine STK-STO-$ stores parameters
  17290.                                 ; of new string updating STKEND.
  17291.  
  17292.         POP     BC              ; length of first
  17293.         POP     HL              ; address of start
  17294.         LD      A,B             ; test for
  17295.         OR      C               ; zero length.
  17296.         JR      Z,L35B7         ; to OTHER-STR if null string
  17297.  
  17298.         LDIR                    ; copy string to workspace.
  17299.  
  17300. ;; OTHER-STR
  17301. L35B7:  POP     BC              ; now second length
  17302.         POP     HL              ; and start of string
  17303.         LD      A,B             ; test this one
  17304.         OR      C               ; for zero length
  17305.         JR      Z,L35BF         ; skip forward to STK-PNTRS if so as complete.
  17306.  
  17307.         LDIR                    ; else copy the bytes.
  17308.                                 ; and continue into next routine which
  17309.                                 ; sets the calculator stack pointers.
  17310.  
  17311. ; --------------------
  17312. ; Check stack pointers
  17313. ; --------------------
  17314. ; Register DE is set to STKEND and HL, the result pointer, is set to five
  17315. ; locations below this.
  17316. ; This routine is used when it is inconvenient to save these values at the
  17317. ; time the calculator stack is manipulated due to other activity on the
  17318. ; machine stack.
  17319. ; This routine is also used to terminate the VAL and READ-IN  routines for
  17320. ; the same reason and to initialize the calculator stack at the start of
  17321. ; the CALCULATE routine.
  17322.  
  17323. ;; STK-PNTRS
  17324. L35BF:  LD      HL,($5C65)      ; fetch STKEND value from system variable.
  17325.         LD      DE,$FFFB        ; the value -5
  17326.         PUSH    HL              ; push STKEND value.
  17327.  
  17328.         ADD     HL,DE           ; subtract 5 from HL.
  17329.  
  17330.         POP     DE              ; pop STKEND to DE.
  17331.         RET                     ; return.
  17332.  
  17333. ; ----------------
  17334. ; Handle CHR$ (2F)
  17335. ; ----------------
  17336. ; This function returns a single character string that is a result of
  17337. ; converting a number in the range 0-255 to a string e.g. CHR$ 65 = "A".
  17338.  
  17339. ;; chrs
  17340. L35C9:  CALL    L2DD5           ; routine FP-TO-A puts the number in A.
  17341.  
  17342.         JR      C,L35DC         ; forward to REPORT-Bd if overflow
  17343.         JR      NZ,L35DC        ; forward to REPORT-Bd if negative
  17344.  
  17345.         PUSH    AF              ; save the argument.
  17346.  
  17347.         LD      BC,$0001        ; one space required.
  17348.         RST     30H             ; BC-SPACES makes DE point to start
  17349.  
  17350.         POP     AF              ; restore the number.
  17351.  
  17352.         LD      (DE),A          ; and store in workspace
  17353.  
  17354.         CALL    L2AB2           ; routine STK-STO-$ stacks descriptor.
  17355.  
  17356.         EX      DE,HL           ; make HL point to result and DE to STKEND.
  17357.         RET                     ; return.
  17358.  
  17359. ; ---
  17360.  
  17361. ;; REPORT-Bd
  17362. L35DC:  RST     08H             ; ERROR-1
  17363.         DB    $0A             ; Error Report: Integer out of range
  17364.  
  17365. ; ----------------------------
  17366. ; Handle VAL and VAL$ ($1D, $18)
  17367. ; ----------------------------
  17368. ; VAL treats the characters in a string as a numeric expression.
  17369. ;     e.g. VAL "2.3" = 2.3, VAL "2+4" = 6, VAL ("2" + "4") = 24.
  17370. ; VAL$ treats the characters in a string as a string expression.
  17371. ;     e.g. VAL$ (z$+"(2)") = a$(2) if z$ happens to be "a$".
  17372.  
  17373. ;; val
  17374. ;; val$
  17375. L35DE:  LD      HL,($5C5D)      ; fetch value of system variable CH_ADD
  17376.         PUSH    HL              ; and save on the machine stack.
  17377.         LD      A,B             ; fetch the literal (either $1D or $18).
  17378.         ADD     A,$E3           ; add $E3 to form $00 (setting carry) or $FB.
  17379.         SBC     A,A             ; now form $FF bit 6 = numeric result
  17380.                                 ; or $00 bit 6 = string result.
  17381.         PUSH    AF              ; save this mask on the stack
  17382.  
  17383.         CALL    L2BF1           ; routine STK-FETCH fetches the string operand
  17384.                                 ; from calculator stack.
  17385.  
  17386.         PUSH    DE              ; save the address of the start of the string.
  17387.         INC     BC              ; increment the length for a carriage return.
  17388.  
  17389.         RST     30H             ; BC-SPACES creates the space in workspace.
  17390.         POP     HL              ; restore start of string to HL.
  17391.         LD      ($5C5D),DE      ; load CH_ADD with start DE in workspace.
  17392.  
  17393.         PUSH    DE              ; save the start in workspace
  17394.         LDIR                    ; copy string from program or variables or
  17395.                                 ; workspace to the workspace area.
  17396.         EX      DE,HL           ; end of string + 1 to HL
  17397.         DEC     HL              ; decrement HL to point to end of new area.
  17398.         LD      (HL),$0D        ; insert a carriage return at end.
  17399.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax.
  17400.         CALL    L24FB           ; routine SCANNING evaluates string
  17401.                                 ; expression and result.
  17402.  
  17403.         RST     18H             ; GET-CHAR fetches next character.
  17404.         CP      $0D             ; is it the expected carriage return ?
  17405.         JR      NZ,L360C        ; forward to V-RPORT-C if not
  17406.                                 ; 'Nonsense in BASIC'.
  17407.  
  17408.         POP     HL              ; restore start of string in workspace.
  17409.         POP     AF              ; restore expected result flag (bit 6).
  17410.         XOR     (IY+$01)        ; xor with FLAGS now updated by SCANNING.
  17411.         AND     $40             ; test bit 6 - should be zero if result types
  17412.                                 ; match.
  17413.  
  17414. ;; V-RPORT-C
  17415. L360C:  JP      NZ,L1C8A        ; jump back to REPORT-C with a result mismatch.
  17416.  
  17417.         LD      ($5C5D),HL      ; set CH_ADD to the start of the string again.
  17418.         SET     7,(IY+$01)      ; update FLAGS  - signal running program.
  17419.         CALL    L24FB           ; routine SCANNING evaluates the string
  17420.                                 ; in full leaving result on calculator stack.
  17421.  
  17422.         POP     HL              ; restore saved character address in program.
  17423.         LD      ($5C5D),HL      ; and reset the system variable CH_ADD.
  17424.  
  17425.         JR      L35BF           ; back to exit via STK-PNTRS.
  17426.                                 ; resetting the calculator stack pointers
  17427.                                 ; HL and DE from STKEND as it wasn't possible
  17428.                                 ; to preserve them during this routine.
  17429.  
  17430. ; ----------------
  17431. ; Handle STR$ (2E)
  17432. ; ----------------
  17433. ;
  17434. ;
  17435.  
  17436. ;; str$
  17437. L361F:  LD      BC,$0001        ; create an initial byte in workspace
  17438.         RST     30H             ; using BC-SPACES restart.
  17439.  
  17440.         LD      ($5C5B),HL      ; set system variable K_CUR to new location.
  17441.         PUSH    HL              ; and save start on machine stack also.
  17442.  
  17443.         LD      HL,($5C51)      ; fetch value of system variable CURCHL
  17444.         PUSH    HL              ; and save that too.
  17445.  
  17446.         LD      A,$FF           ; select system channel 'R'.
  17447.         CALL    L1601           ; routine CHAN-OPEN opens it.
  17448.         CALL    L2DE3           ; routine PRINT-FP outputs the number to
  17449.                                 ; workspace updating K-CUR.
  17450.  
  17451.         POP     HL              ; restore current channel.
  17452.         CALL    L1615           ; routine CHAN-FLAG resets flags.
  17453.  
  17454.         POP     DE              ; fetch saved start of string to DE.
  17455.         LD      HL,($5C5B)      ; load HL with end of string from K_CUR.
  17456.  
  17457.         AND     A               ; prepare for true subtraction.
  17458.         SBC     HL,DE           ; subtract start from end to give length.
  17459.         LD      B,H             ; transfer the length to
  17460.         LD      C,L             ; the BC register pair.
  17461.  
  17462.         CALL    L2AB2           ; routine STK-STO-$ stores string parameters
  17463.                                 ; on the calculator stack.
  17464.  
  17465.         EX      DE,HL           ; HL = last value, DE = STKEND.
  17466.         RET                     ; return.
  17467.  
  17468. ; ------------
  17469. ; Read-in (1A)
  17470. ; ------------
  17471. ; This is the calculator literal used by the INKEY$ function when a '#'
  17472. ; is encountered after the keyword.
  17473. ; INKEY$ # does not interact correctly with the keyboard, #0 or #1, and
  17474. ; its uses are for other channels.
  17475.  
  17476. ;; read-in
  17477. L3645:  CALL    L1E94           ; routine FIND-INT1 fetches stream to A
  17478.         CP      $10             ; compare with 16 decimal.
  17479.         JP      NC,L1E9F        ; jump to REPORT-Bb if not in range 0 - 15.
  17480.                                 ; 'Integer out of range'
  17481.                                 ; (REPORT-Bd is within range)
  17482.  
  17483.         LD      HL,($5C51)      ; fetch current channel CURCHL
  17484.         PUSH    HL              ; save it
  17485.         CALL    L1601           ; routine CHAN-OPEN opens channel
  17486.  
  17487.         CALL    L15E6           ; routine INPUT-AD - the channel must have an
  17488.                                 ; input stream or else error here from stream
  17489.                                 ; stub.
  17490.         LD      BC,$0000        ; initialize length of string to zero
  17491.         JR      NC,L365F        ; forward to R-I-STORE if no key detected.
  17492.  
  17493.         INC     C               ; increase length to one.
  17494.  
  17495.         RST     30H             ; BC-SPACES creates space for one character
  17496.                                 ; in workspace.
  17497.         LD      (DE),A          ; the character is inserted.
  17498.  
  17499. ;; R-I-STORE
  17500. L365F:  CALL    L2AB2           ; routine STK-STO-$ stacks the string
  17501.                                 ; parameters.
  17502.         POP     HL              ; restore current channel address
  17503.         CALL    L1615           ; routine CHAN-FLAG resets current channel
  17504.                                 ; system variable and flags.
  17505.         JP      L35BF           ; jump back to STK-PNTRS
  17506.  
  17507. ; ----------------
  17508. ; Handle CODE (1C)
  17509. ; ----------------
  17510. ; Returns the ASCII code of a character or first character of a string
  17511. ; e.g. CODE "Aardvark" = 65, CODE "" = 0.
  17512.  
  17513. ;; code
  17514. L3669:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17515.                                 ; string parameters.
  17516.                                 ; DE points to the start, BC holds the length.
  17517.         LD      A,B             ; test length
  17518.         OR      C               ; of the string.
  17519.         JR      Z,L3671         ; skip to STK-CODE with zero if the null string.
  17520.  
  17521.         LD      A,(DE)          ; else fetch the first character.
  17522.  
  17523. ;; STK-CODE
  17524. L3671:  JP      L2D28           ; jump back to STACK-A (with memory check)
  17525.  
  17526. ; ---------------
  17527. ; Handle LEN (1E)
  17528. ; ---------------
  17529. ; Returns the length of a string.
  17530. ; In Sinclair BASIC strings can be more than twenty thousand characters long
  17531. ; so a sixteen-bit register is required to store the length
  17532.  
  17533. ;; len
  17534. L3674:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17535.                                 ; string parameters from the calculator stack.
  17536.                                 ; register BC now holds the length of string.
  17537.  
  17538.         JP      L2D2B           ; jump back to STACK-BC to save result on the
  17539.                                 ; calculator stack (with memory check).
  17540.  
  17541. ; -------------------------
  17542. ; Decrease the counter (35)
  17543. ; -------------------------
  17544. ; The calculator has an instruction that decrements a single-byte
  17545. ; pseudo-register and makes consequential relative jumps just like
  17546. ; the Z80's DJNZ instruction.
  17547.  
  17548. ;; dec-jr-nz
  17549. L367A:  EXX                     ; switch in set that addresses code
  17550.  
  17551.         PUSH    HL              ; save pointer to offset byte
  17552.         LD      HL,$5C67        ; address BREG in system variables
  17553.         DEC     (HL)            ; decrement it
  17554.         POP     HL              ; restore pointer
  17555.  
  17556.         JR      NZ,L3687        ; to JUMP-2 if not zero
  17557.  
  17558.         INC     HL              ; step past the jump length.
  17559.         EXX                     ; switch in the main set.
  17560.         RET                     ; return.
  17561.  
  17562. ; Note. as a general rule the calculator avoids using the IY register
  17563. ; otherwise the cumbersome 4 instructions in the middle could be replaced by
  17564. ; dec (iy+$2d) - three bytes instead of six.
  17565.  
  17566.  
  17567. ; ---------
  17568. ; Jump (33)
  17569. ; ---------
  17570. ; This enables the calculator to perform relative jumps just like
  17571. ; the Z80 chip's JR instruction
  17572.  
  17573. ;; jump
  17574. ;; JUMP
  17575. L3686:  EXX                     ;switch in pointer set
  17576.  
  17577. ;; JUMP-2
  17578. L3687:  LD      E,(HL)          ; the jump byte 0-127 forward, 128-255 back.
  17579.         LD      A,E             ; transfer to accumulator.
  17580.         RLA                     ; if backward jump, carry is set.
  17581.         SBC     A,A             ; will be $FF if backward or $00 if forward.
  17582.         LD      D,A             ; transfer to high byte.
  17583.         ADD     HL,DE           ; advance calculator pointer forward or back.
  17584.         EXX                     ; switch back.
  17585.         RET                     ; return.
  17586.  
  17587. ; -----------------
  17588. ; Jump on true (00)
  17589. ; -----------------
  17590. ; This enables the calculator to perform conditional relative jumps
  17591. ; dependent on whether the last test gave a true result
  17592.  
  17593. ;; jump-true
  17594. L368F:  INC     DE              ; collect the
  17595.         INC     DE              ; third byte
  17596.         LD      A,(DE)          ; of the test
  17597.         DEC     DE              ; result and
  17598.         DEC     DE              ; backtrack.
  17599.  
  17600.         AND     A               ; is result 0 or 1 ?
  17601.         JR      NZ,L3686        ; back to JUMP if true (1).
  17602.  
  17603.         EXX                     ; else switch in the pointer set.
  17604.         INC     HL              ; step past the jump length.
  17605.         EXX                     ; switch in the main set.
  17606.         RET                     ; return.
  17607.  
  17608. ; -----------------------
  17609. ; End of calculation (38)
  17610. ; -----------------------
  17611. ; The end-calc literal terminates a mini-program written in the Spectrum's
  17612. ; internal language.
  17613.  
  17614. ;; end-calc
  17615. L369B:  POP     AF              ; drop the calculator return address RE-ENTRY
  17616.         EXX                     ; switch to the other set.
  17617.  
  17618.         EX      (SP),HL         ; transfer H'L' to machine stack for the
  17619.                                 ; return address.
  17620.                                 ; when exiting recursion then the previous
  17621.                                 ; pointer is transferred to H'L'.
  17622.  
  17623.         EXX                     ; back to main set.
  17624.         RET                     ; return.
  17625.  
  17626.  
  17627. ; ------------------------
  17628. ; THE 'MODULUS' SUBROUTINE
  17629. ; ------------------------
  17630. ; (offset: $32 'n-mod-m')
  17631. ;
  17632. ;
  17633.  
  17634. ;; n-mod-m
  17635. L36A0:  RST     28H             ;; FP-CALC          17, 3.
  17636.         DB    $C0             ;;st-mem-0          17, 3.
  17637.         DB    $02             ;;delete            17.
  17638.         DB    $31             ;;duplicate         17, 17.
  17639.         DB    $E0             ;;get-mem-0         17, 17, 3.
  17640.         DB    $05             ;;division          17, 17/3.
  17641.         DB    $27             ;;int               17, 5.
  17642.         DB    $E0             ;;get-mem-0         17, 5, 3.
  17643.         DB    $01             ;;exchange          17, 3, 5.
  17644.         DB    $C0             ;;st-mem-0          17, 3, 5.
  17645.         DB    $04             ;;multiply          17, 15.
  17646.         DB    $03             ;;subtract          2.
  17647.         DB    $E0             ;;get-mem-0         2, 5.
  17648.         DB    $38             ;;end-calc          2, 5.
  17649.  
  17650.         RET                     ; return.
  17651.  
  17652.  
  17653. ; ------------------
  17654. ; THE 'INT' FUNCTION
  17655. ; ------------------
  17656. ; (offset $27: 'int' )
  17657. ;
  17658. ; This function returns the integer of x, which is just the same as truncate
  17659. ; for positive numbers. The truncate literal truncates negative numbers
  17660. ; upwards so that -3.4 gives -3 whereas the BASIC INT function has to
  17661. ; truncate negative numbers down so that INT -3.4 is -4.
  17662. ; It is best to work through using, say, +-3.4 as examples.
  17663.  
  17664. ;; int
  17665. L36AF:  RST     28H             ;; FP-CALC              x.    (= 3.4 or -3.4).
  17666.         DB    $31             ;;duplicate             x, x.
  17667.         DB    $36             ;;less-0                x, (1/0)
  17668.         DB    $00             ;;jump-true             x, (1/0)
  17669.         DB    $04             ;;to L36B7, X-NEG
  17670.  
  17671.         DB    $3A             ;;truncate              trunc 3.4 = 3.
  17672.         DB    $38             ;;end-calc              3.
  17673.  
  17674.         RET                     ; return with + int x on stack.
  17675.  
  17676. ; ---
  17677.  
  17678.  
  17679. ;; X-NEG
  17680. L36B7:  DB    $31             ;;duplicate             -3.4, -3.4.
  17681.         DB    $3A             ;;truncate              -3.4, -3.
  17682.         DB    $C0             ;;st-mem-0              -3.4, -3.
  17683.         DB    $03             ;;subtract              -.4
  17684.         DB    $E0             ;;get-mem-0             -.4, -3.
  17685.         DB    $01             ;;exchange              -3, -.4.
  17686.         DB    $30             ;;not                   -3, (0).
  17687.         DB    $00             ;;jump-true             -3.
  17688.         DB    $03             ;;to L36C2, EXIT        -3.
  17689.  
  17690.         DB    $A1             ;;stk-one               -3, 1.
  17691.         DB    $03             ;;subtract              -4.
  17692.  
  17693. ;; EXIT
  17694. L36C2:  DB    $38             ;;end-calc              -4.
  17695.  
  17696.         RET                     ; return.
  17697.  
  17698.  
  17699. ; ----------------
  17700. ; Exponential (26)
  17701. ; ----------------
  17702. ;
  17703. ;
  17704.  
  17705. ;; EXP
  17706. ;; exp
  17707. L36C4:  RST     28H             ;; FP-CALC
  17708.         DB    $3D             ;;re-stack
  17709.         DB    $34             ;;stk-data
  17710.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17711.         DB    $38,$AA,$3B,$29 ;;
  17712.         DB    $04             ;;multiply
  17713.         DB    $31             ;;duplicate
  17714.         DB    $27             ;;int
  17715.         DB    $C3             ;;st-mem-3
  17716.         DB    $03             ;;subtract
  17717.         DB    $31             ;;duplicate
  17718.         DB    $0F             ;;addition
  17719.         DB    $A1             ;;stk-one
  17720.         DB    $03             ;;subtract
  17721.         DB    $88             ;;series-08
  17722.         DB    $13             ;;Exponent: $63, Bytes: 1
  17723.         DB    $36             ;;(+00,+00,+00)
  17724.         DB    $58             ;;Exponent: $68, Bytes: 2
  17725.         DB    $65,$66         ;;(+00,+00)
  17726.         DB    $9D             ;;Exponent: $6D, Bytes: 3
  17727.         DB    $78,$65,$40     ;;(+00)
  17728.         DB    $A2             ;;Exponent: $72, Bytes: 3
  17729.         DB    $60,$32,$C9     ;;(+00)
  17730.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17731.         DB    $21,$F7,$AF,$24 ;;
  17732.         DB    $EB             ;;Exponent: $7B, Bytes: 4
  17733.         DB    $2F,$B0,$B0,$14 ;;
  17734.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  17735.         DB    $7E,$BB,$94,$58 ;;
  17736.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17737.         DB    $3A,$7E,$F8,$CF ;;
  17738.         DB    $E3             ;;get-mem-3
  17739.         DB    $38             ;;end-calc
  17740.  
  17741.         CALL    L2DD5           ; routine FP-TO-A
  17742.         JR      NZ,L3705        ; to N-NEGTV
  17743.  
  17744.         JR      C,L3703         ; to REPORT-6b
  17745.  
  17746.         ADD     A,(HL)          ;
  17747.         JR      NC,L370C        ; to RESULT-OK
  17748.  
  17749.  
  17750. ;; REPORT-6b
  17751. L3703:  RST     08H             ; ERROR-1
  17752.         DB    $05             ; Error Report: Number too big
  17753.  
  17754. ;; N-NEGTV
  17755. L3705:  JR      C,L370E         ; to RSLT-ZERO
  17756.  
  17757.         SUB     (HL)            ;
  17758.         JR      NC,L370E        ; to RSLT-ZERO
  17759.  
  17760.         NEG                     ; Negate
  17761.  
  17762. ;; RESULT-OK
  17763. L370C:  LD      (HL),A          ;
  17764.         RET                     ; return.
  17765.  
  17766. ; ---
  17767.  
  17768.  
  17769. ;; RSLT-ZERO
  17770. L370E:  RST     28H             ;; FP-CALC
  17771.         DB    $02             ;;delete
  17772.         DB    $A0             ;;stk-zero
  17773.         DB    $38             ;;end-calc
  17774.  
  17775.         RET                     ; return.
  17776.  
  17777.  
  17778. ; ----------------------
  17779. ; Natural logarithm (25)
  17780. ; ----------------------
  17781. ;
  17782. ;
  17783.  
  17784. ;; ln
  17785. L3713:  RST     28H             ;; FP-CALC
  17786.         DB    $3D             ;;re-stack
  17787.         DB    $31             ;;duplicate
  17788.         DB    $37             ;;greater-0
  17789.         DB    $00             ;;jump-true
  17790.         DB    $04             ;;to L371C, VALID
  17791.  
  17792.         DB    $38             ;;end-calc
  17793.  
  17794.  
  17795. ;; REPORT-Ab
  17796. L371A:  RST     08H             ; ERROR-1
  17797.         DB    $09             ; Error Report: Invalid argument
  17798.  
  17799. ;; VALID
  17800. L371C:  DB    $A0             ;;stk-zero
  17801.         DB    $02             ;;delete
  17802.         DB    $38             ;;end-calc
  17803.         LD      A,(HL)          ;
  17804.  
  17805.         LD      (HL),$80        ;
  17806.         CALL    L2D28           ; routine STACK-A
  17807.  
  17808.         RST     28H             ;; FP-CALC
  17809.         DB    $34             ;;stk-data
  17810.         DB    $38             ;;Exponent: $88, Bytes: 1
  17811.         DB    $00             ;;(+00,+00,+00)
  17812.         DB    $03             ;;subtract
  17813.         DB    $01             ;;exchange
  17814.         DB    $31             ;;duplicate
  17815.         DB    $34             ;;stk-data
  17816.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17817.         DB    $4C,$CC,$CC,$CD ;;
  17818.         DB    $03             ;;subtract
  17819.         DB    $37             ;;greater-0
  17820.         DB    $00             ;;jump-true
  17821.         DB    $08             ;;to L373D, GRE.8
  17822.  
  17823.         DB    $01             ;;exchange
  17824.         DB    $A1             ;;stk-one
  17825.         DB    $03             ;;subtract
  17826.         DB    $01             ;;exchange
  17827.         DB    $38             ;;end-calc
  17828.  
  17829.         INC     (HL)            ;
  17830.  
  17831.         RST     28H             ;; FP-CALC
  17832.  
  17833. ;; GRE.8
  17834. L373D:  DB    $01             ;;exchange
  17835.         DB    $34             ;;stk-data
  17836.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17837.         DB    $31,$72,$17,$F8 ;;
  17838.         DB    $04             ;;multiply
  17839.         DB    $01             ;;exchange
  17840.         DB    $A2             ;;stk-half
  17841.         DB    $03             ;;subtract
  17842.         DB    $A2             ;;stk-half
  17843.         DB    $03             ;;subtract
  17844.         DB    $31             ;;duplicate
  17845.         DB    $34             ;;stk-data
  17846.         DB    $32             ;;Exponent: $82, Bytes: 1
  17847.         DB    $20             ;;(+00,+00,+00)
  17848.         DB    $04             ;;multiply
  17849.         DB    $A2             ;;stk-half
  17850.         DB    $03             ;;subtract
  17851.         DB    $8C             ;;series-0C
  17852.         DB    $11             ;;Exponent: $61, Bytes: 1
  17853.         DB    $AC             ;;(+00,+00,+00)
  17854.         DB    $14             ;;Exponent: $64, Bytes: 1
  17855.         DB    $09             ;;(+00,+00,+00)
  17856.         DB    $56             ;;Exponent: $66, Bytes: 2
  17857.         DB    $DA,$A5         ;;(+00,+00)
  17858.         DB    $59             ;;Exponent: $69, Bytes: 2
  17859.         DB    $30,$C5         ;;(+00,+00)
  17860.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  17861.         DB    $90,$AA         ;;(+00,+00)
  17862.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  17863.         DB    $70,$6F,$61     ;;(+00)
  17864.         DB    $A1             ;;Exponent: $71, Bytes: 3
  17865.         DB    $CB,$DA,$96     ;;(+00)
  17866.         DB    $A4             ;;Exponent: $74, Bytes: 3
  17867.         DB    $31,$9F,$B4     ;;(+00)
  17868.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17869.         DB    $A0,$FE,$5C,$FC ;;
  17870.         DB    $EA             ;;Exponent: $7A, Bytes: 4
  17871.         DB    $1B,$43,$CA,$36 ;;
  17872.         DB    $ED             ;;Exponent: $7D, Bytes: 4
  17873.         DB    $A7,$9C,$7E,$5E ;;
  17874.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17875.         DB    $6E,$23,$80,$93 ;;
  17876.         DB    $04             ;;multiply
  17877.         DB    $0F             ;;addition
  17878.         DB    $38             ;;end-calc
  17879.  
  17880.         RET                     ; return.
  17881.  
  17882.  
  17883. ; -----------------------------
  17884. ; THE 'TRIGONOMETRIC' FUNCTIONS
  17885. ; -----------------------------
  17886. ; Trigonometry is rocket science. It is also used by carpenters and pyramid
  17887. ; builders.
  17888. ; Some uses can be quite abstract but the principles can be seen in simple
  17889. ; right-angled triangles. Triangles have some special properties -
  17890. ;
  17891. ; 1) The sum of the three angles is always PI radians (180 degrees).
  17892. ;    Very helpful if you know two angles and wish to find the third.
  17893. ; 2) In any right-angled triangle the sum of the squares of the two shorter
  17894. ;    sides is equal to the square of the longest side opposite the right-angle.
  17895. ;    Very useful if you know the length of two sides and wish to know the
  17896. ;    length of the third side.
  17897. ; 3) Functions sine, cosine and tangent enable one to calculate the length
  17898. ;    of an unknown side when the length of one other side and an angle is
  17899. ;    known.
  17900. ; 4) Functions arcsin, arccosine and arctan enable one to calculate an unknown
  17901. ;    angle when the length of two of the sides is known.
  17902.  
  17903. ;---------------------------------
  17904. ; THE 'REDUCE ARGUMENT' SUBROUTINE
  17905. ;---------------------------------
  17906. ; (offset $39: 'get-argt')
  17907. ;
  17908. ; This routine performs two functions on the angle, in radians, that forms
  17909. ; the argument to the sine and cosine functions.
  17910. ; First it ensures that the angle 'wraps round'. That if a ship turns through
  17911. ; an angle of, say, 3*PI radians (540 degrees) then the net effect is to turn
  17912. ; through an angle of PI radians (180 degrees).
  17913. ; Secondly it converts the angle in radians to a fraction of a right angle,
  17914. ; depending within which quadrant the angle lies, with the periodicity
  17915. ; resembling that of the desired sine value.
  17916. ; The result lies in the range -1 to +1.              
  17917. ;
  17918. ;                     90 deg.
  17919. ;
  17920. ;                     (pi/2)
  17921. ;              II       +1        I
  17922. ;                       |
  17923. ;        sin+      |\   |   /|    sin+
  17924. ;        cos-      | \  |  / |    cos+
  17925. ;        tan-      |  \ | /  |    tan+
  17926. ;                  |   \|/)  |          
  17927. ; 180 deg. (pi) 0 -|----+----|-- 0  (0)   0 degrees
  17928. ;                  |   /|\   |
  17929. ;        sin-      |  / | \  |    sin-
  17930. ;        cos-      | /  |  \ |    cos+
  17931. ;        tan+      |/   |   \|    tan-
  17932. ;                       |
  17933. ;              III      -1       IV
  17934. ;                     (3pi/2)
  17935. ;
  17936. ;                     270 deg.
  17937. ;
  17938.  
  17939. ;; get-argt
  17940. L3783:  RST     28H             ;; FP-CALC      X.
  17941.         DB    $3D             ;;re-stack
  17942.         DB    $34             ;;stk-data
  17943.         DB    $EE             ;;Exponent: $7E,
  17944.                                 ;;Bytes: 4
  17945.         DB    $22,$F9,$83,$6E ;;              X, 1/(2*PI)
  17946.         DB    $04             ;;multiply      X/(2*PI) = fraction
  17947.         DB    $31             ;;duplicate
  17948.         DB    $A2             ;;stk-half
  17949.         DB    $0F             ;;addition
  17950.         DB    $27             ;;int
  17951.  
  17952.         DB    $03             ;;subtract      now range -.5 to .5
  17953.  
  17954.         DB    $31             ;;duplicate
  17955.         DB    $0F             ;;addition      now range -1 to 1.
  17956.         DB    $31             ;;duplicate
  17957.         DB    $0F             ;;addition      now range -2 to +2.
  17958.  
  17959. ; quadrant I (0 to +1) and quadrant IV (-1 to 0) are now correct.
  17960. ; quadrant II ranges +1 to +2.
  17961. ; quadrant III ranges -2 to -1.
  17962.  
  17963.         DB    $31             ;;duplicate     Y, Y.
  17964.         DB    $2A             ;;abs           Y, abs(Y).    range 1 to 2
  17965.         DB    $A1             ;;stk-one       Y, abs(Y), 1.
  17966.         DB    $03             ;;subtract      Y, abs(Y)-1.  range 0 to 1
  17967.         DB    $31             ;;duplicate     Y, Z, Z.
  17968.         DB    $37             ;;greater-0     Y, Z, (1/0).
  17969.  
  17970.         DB    $C0             ;;st-mem-0         store as possible sign
  17971.                                 ;;                 for cosine function.
  17972.  
  17973.         DB    $00             ;;jump-true
  17974.         DB    $04             ;;to L37A1, ZPLUS  with quadrants II and III.
  17975.  
  17976. ; else the angle lies in quadrant I or IV and value Y is already correct.
  17977.  
  17978.         DB    $02             ;;delete        Y.   delete the test value.
  17979.         DB    $38             ;;end-calc      Y.
  17980.  
  17981.         RET                     ; return.       with Q1 and Q4           >>>
  17982.  
  17983. ; ---
  17984.  
  17985. ; the branch was here with quadrants II (0 to 1) and III (1 to 0).
  17986. ; Y will hold -2 to -1 if this is quadrant III.
  17987.  
  17988. ;; ZPLUS
  17989. L37A1:  DB    $A1             ;;stk-one         Y, Z, 1.
  17990.         DB    $03             ;;subtract        Y, Z-1.       Q3 = 0 to -1
  17991.         DB    $01             ;;exchange        Z-1, Y.
  17992.         DB    $36             ;;less-0          Z-1, (1/0).
  17993.         DB    $00             ;;jump-true       Z-1.
  17994.         DB    $02             ;;to L37A8, YNEG
  17995.                                 ;;if angle in quadrant III
  17996.  
  17997. ; else angle is within quadrant II (-1 to 0)
  17998.  
  17999.         DB    $1B             ;;negate          range +1 to 0.
  18000.  
  18001. ;; YNEG
  18002. L37A8:  DB    $38             ;;end-calc        quadrants II and III correct.
  18003.  
  18004.         RET                     ; return.
  18005.  
  18006.  
  18007. ;----------------------
  18008. ; THE 'COSINE' FUNCTION
  18009. ;----------------------
  18010. ; (offset $20: 'cos')
  18011. ; Cosines are calculated as the sine of the opposite angle rectifying the
  18012. ; sign depending on the quadrant rules.
  18013. ;
  18014. ;
  18015. ;           /|
  18016. ;        h /y|
  18017. ;         /  |o
  18018. ;        /x  |
  18019. ;       /----|    
  18020. ;         a
  18021. ;
  18022. ; The cosine of angle x is the adjacent side (a) divided by the hypotenuse 1.
  18023. ; However if we examine angle y then a/h is the sine of that angle.
  18024. ; Since angle x plus angle y equals a right-angle, we can find angle y by
  18025. ; subtracting angle x from pi/2.
  18026. ; However it's just as easy to reduce the argument first and subtract the
  18027. ; reduced argument from the value 1 (a reduced right-angle).
  18028. ; It's even easier to subtract 1 from the angle and rectify the sign.
  18029. ; In fact, after reducing the argument, the absolute value of the argument
  18030. ; is used and rectified using the test result stored in mem-0 by 'get-argt'
  18031. ; for that purpose.
  18032. ;
  18033.  
  18034. ;; cos
  18035. L37AA:  RST     28H             ;; FP-CALC              angle in radians.
  18036.         DB    $39             ;;get-argt              X     reduce -1 to +1
  18037.  
  18038.         DB    $2A             ;;abs                   ABS X.   0 to 1
  18039.         DB    $A1             ;;stk-one               ABS X, 1.
  18040.         DB    $03             ;;subtract              now opposite angle
  18041.                                 ;;                      although sign is -ve.
  18042.  
  18043.         DB    $E0             ;;get-mem-0             fetch the sign indicator
  18044.         DB    $00             ;;jump-true
  18045.         DB    $06             ;;fwd to L37B7, C-ENT
  18046.                                 ;;forward to common code if in QII or QIII.
  18047.  
  18048.         DB    $1B             ;;negate                else make sign +ve.
  18049.         DB    $33             ;;jump
  18050.         DB    $03             ;;fwd to L37B7, C-ENT
  18051.                                 ;; with quadrants I and IV.
  18052.  
  18053. ;--------------------
  18054. ; THE 'SINE' FUNCTION
  18055. ;--------------------
  18056. ; (offset $1F: 'sin')
  18057. ; This is a fundamental transcendental function from which others such as cos
  18058. ; and tan are directly, or indirectly, derived.
  18059. ; It uses the series generator to produce Chebyshev polynomials.
  18060. ;
  18061. ;
  18062. ;           /|
  18063. ;        1 / |
  18064. ;         /  |x
  18065. ;        /a  |
  18066. ;       /----|    
  18067. ;         y
  18068. ;
  18069. ; The 'get-argt' function is designed to modify the angle and its sign
  18070. ; in line with the desired sine value and afterwards it can launch straight
  18071. ; into common code.
  18072.  
  18073. ;; sin
  18074. L37B5:  RST     28H             ;; FP-CALC      angle in radians
  18075.         DB    $39             ;;get-argt      reduce - sign now correct.
  18076.  
  18077. ;; C-ENT
  18078. L37B7:  DB    $31             ;;duplicate
  18079.         DB    $31             ;;duplicate
  18080.         DB    $04             ;;multiply
  18081.         DB    $31             ;;duplicate
  18082.         DB    $0F             ;;addition
  18083.         DB    $A1             ;;stk-one
  18084.         DB    $03             ;;subtract
  18085.  
  18086.         DB    $86             ;;series-06
  18087.         DB    $14             ;;Exponent: $64, Bytes: 1
  18088.         DB    $E6             ;;(+00,+00,+00)
  18089.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  18090.         DB    $1F,$0B         ;;(+00,+00)
  18091.         DB    $A3             ;;Exponent: $73, Bytes: 3
  18092.         DB    $8F,$38,$EE     ;;(+00)
  18093.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18094.         DB    $15,$63,$BB,$23 ;;
  18095.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  18096.         DB    $92,$0D,$CD,$ED ;;
  18097.         DB    $F1             ;;Exponent: $81, Bytes: 4
  18098.         DB    $23,$5D,$1B,$EA ;;
  18099.         DB    $04             ;;multiply
  18100.         DB    $38             ;;end-calc
  18101.  
  18102.         RET                     ; return.
  18103.  
  18104. ;-----------------------
  18105. ; THE 'TANGENT' FUNCTION
  18106. ;-----------------------
  18107. ; (offset $21: 'tan')
  18108. ;
  18109. ; Evaluates tangent x as    sin(x) / cos(x).
  18110. ;
  18111. ;
  18112. ;           /|
  18113. ;        h / |
  18114. ;         /  |o
  18115. ;        /x  |
  18116. ;       /----|    
  18117. ;         a
  18118. ;
  18119. ; the tangent of angle x is the ratio of the length of the opposite side
  18120. ; divided by the length of the adjacent side. As the opposite length can
  18121. ; be calculates using sin(x) and the adjacent length using cos(x) then
  18122. ; the tangent can be defined in terms of the previous two functions.
  18123.  
  18124. ; Error 6 if the argument, in radians, is too close to one like pi/2
  18125. ; which has an infinite tangent. e.g. PRINT TAN (PI/2)  evaluates as 1/0.
  18126. ; Similarly PRINT TAN (3*PI/2), TAN (5*PI/2) etc.
  18127.  
  18128. ;; tan
  18129. L37DA:  RST     28H             ;; FP-CALC          x.
  18130.         DB    $31             ;;duplicate         x, x.
  18131.         DB    $1F             ;;sin               x, sin x.
  18132.         DB    $01             ;;exchange          sin x, x.
  18133.         DB    $20             ;;cos               sin x, cos x.
  18134.         DB    $05             ;;division          sin x/cos x (= tan x).
  18135.         DB    $38             ;;end-calc          tan x.
  18136.  
  18137.         RET                     ; return.
  18138.  
  18139. ;----------------------
  18140. ; THE 'ARCTAN' FUNCTION
  18141. ;----------------------
  18142. ; (Offset $24: 'atn')
  18143. ; the inverse tangent function with the result in radians.
  18144. ; This is a fundamental transcendental function from which others such as asn
  18145. ; and acs are directly, or indirectly, derived.
  18146. ; It uses the series generator to produce Chebyshev polynomials.
  18147.  
  18148. ;; atn
  18149. L37E2:  CALL    L3297           ; routine re-stack
  18150.         LD      A,(HL)          ; fetch exponent byte.
  18151.         CP      $81             ; compare to that for 'one'
  18152.         JR      C,L37F8         ; forward, if less, to SMALL
  18153.  
  18154.         RST     28H             ;; FP-CALC
  18155.         DB    $A1             ;;stk-one
  18156.         DB    $1B             ;;negate
  18157.         DB    $01             ;;exchange
  18158.         DB    $05             ;;division
  18159.         DB    $31             ;;duplicate
  18160.         DB    $36             ;;less-0
  18161.         DB    $A3             ;;stk-pi/2
  18162.         DB    $01             ;;exchange
  18163.         DB    $00             ;;jump-true
  18164.         DB    $06             ;;to L37FA, CASES
  18165.  
  18166.         DB    $1B             ;;negate
  18167.         DB    $33             ;;jump
  18168.         DB    $03             ;;to L37FA, CASES
  18169.  
  18170. ;; SMALL
  18171. L37F8:  RST     28H             ;; FP-CALC
  18172.         DB    $A0             ;;stk-zero
  18173.  
  18174. ;; CASES
  18175. L37FA:  DB    $01             ;;exchange
  18176.         DB    $31             ;;duplicate
  18177.         DB    $31             ;;duplicate
  18178.         DB    $04             ;;multiply
  18179.         DB    $31             ;;duplicate
  18180.         DB    $0F             ;;addition
  18181.         DB    $A1             ;;stk-one
  18182.         DB    $03             ;;subtract
  18183.         DB    $8C             ;;series-0C
  18184.         DB    $10             ;;Exponent: $60, Bytes: 1
  18185.         DB    $B2             ;;(+00,+00,+00)
  18186.         DB    $13             ;;Exponent: $63, Bytes: 1
  18187.         DB    $0E             ;;(+00,+00,+00)
  18188.         DB    $55             ;;Exponent: $65, Bytes: 2
  18189.         DB    $E4,$8D         ;;(+00,+00)
  18190.         DB    $58             ;;Exponent: $68, Bytes: 2
  18191.         DB    $39,$BC         ;;(+00,+00)
  18192.         DB    $5B             ;;Exponent: $6B, Bytes: 2
  18193.         DB    $98,$FD         ;;(+00,+00)
  18194.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  18195.         DB    $00,$36,$75     ;;(+00)
  18196.         DB    $A0             ;;Exponent: $70, Bytes: 3
  18197.         DB    $DB,$E8,$B4     ;;(+00)
  18198.         DB    $63             ;;Exponent: $73, Bytes: 2
  18199.         DB    $42,$C4         ;;(+00,+00)
  18200.         DB    $E6             ;;Exponent: $76, Bytes: 4
  18201.         DB    $B5,$09,$36,$BE ;;
  18202.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18203.         DB    $36,$73,$1B,$5D ;;
  18204.         DB    $EC             ;;Exponent: $7C, Bytes: 4
  18205.         DB    $D8,$DE,$63,$BE ;;
  18206.         DB    $F0             ;;Exponent: $80, Bytes: 4
  18207.         DB    $61,$A1,$B3,$0C ;;
  18208.         DB    $04             ;;multiply
  18209.         DB    $0F             ;;addition
  18210.         DB    $38             ;;end-calc
  18211.  
  18212.         RET                     ; return.
  18213.  
  18214.  
  18215. ;----------------------
  18216. ; THE 'ARCSIN' FUNCTION
  18217. ;----------------------
  18218. ; (Offset $22: 'asn')
  18219. ; the inverse sine function with result in radians.
  18220. ; derived from arctan function above.
  18221. ; Error A unless the argument is between -1 and +1 inclusive.
  18222. ; uses an adaptation of the formula asn(x) = atn(x/sqr(1-x*x))
  18223. ;
  18224. ;
  18225. ;           /|
  18226. ;        1 / |
  18227. ;         /  |x
  18228. ;        /a  |
  18229. ;       /----|    
  18230. ;         y
  18231. ;
  18232. ; e.g. we know the opposite side (x) and hypotenuse (1)
  18233. ; and we wish to find angle a in radians.
  18234. ; we can derive length y by Pythagorus and then use ATN instead.
  18235. ; since y*y + x*x = 1*1 (Pythagorus Theorem) then
  18236. ; y=sqr(1-x*x)                         - no need to multiply 1 by itself.
  18237. ; so, asn(a) = atn(x/y)
  18238. ; or more fully,
  18239. ; asn(a) = atn(x/sqr(1-x*x))
  18240.  
  18241. ; Close but no cigar.
  18242.  
  18243. ; While PRINT ATN (x/SQR (1-x*x)) gives the same results as PRINT ASN x,
  18244. ; it leads to division by zero when x is 1 or -1.
  18245. ; To overcome this, 1 is added to y giving half the required angle and the
  18246. ; result is then doubled.
  18247. ; That is PRINT ATN (x/(SQR (1-x*x) +1)) *2
  18248. ; A value higher than 1 gives the required error as attempting to find  the
  18249. ; square root of a negative number generates an error in Sinclair BASIC.
  18250.  
  18251. ;; asn
  18252. L3833:  RST     28H             ;; FP-CALC      x.
  18253.         DB    $31             ;;duplicate     x, x.
  18254.         DB    $31             ;;duplicate     x, x, x.
  18255.         DB    $04             ;;multiply      x, x*x.
  18256.         DB    $A1             ;;stk-one       x, x*x, 1.
  18257.         DB    $03             ;;subtract      x, x*x-1.
  18258.         DB    $1B             ;;negate        x, 1-x*x.
  18259.         DB    $28             ;;sqr           x, sqr(1-x*x) = y
  18260.         DB    $A1             ;;stk-one       x, y, 1.
  18261.         DB    $0F             ;;addition      x, y+1.
  18262.         DB    $05             ;;division      x/y+1.
  18263.         DB    $24             ;;atn           a/2       (half the angle)
  18264.         DB    $31             ;;duplicate     a/2, a/2.
  18265.         DB    $0F             ;;addition      a.
  18266.         DB    $38             ;;end-calc      a.
  18267.  
  18268.         RET                     ; return.
  18269.  
  18270.  
  18271. ;-------------------------
  18272. ; THE 'ARCCOS' FUNCTION
  18273. ;-------------------------
  18274. ; (Offset $23: 'acs')
  18275. ; the inverse cosine function with the result in radians.
  18276. ; Error A unless the argument is between -1 and +1.
  18277. ; Result in range 0 to pi.
  18278. ; Derived from asn above which is in turn derived from the preceding atn.
  18279. ; It could have been derived directly from atn using acs(x) = atn(sqr(1-x*x)/x).
  18280. ; However, as sine and cosine are horizontal translations of each other,
  18281. ; uses acs(x) = pi/2 - asn(x)
  18282.  
  18283. ; e.g. the arccosine of a known x value will give the required angle b in
  18284. ; radians.
  18285. ; We know, from above, how to calculate the angle a using asn(x).
  18286. ; Since the three angles of any triangle add up to 180 degrees, or pi radians,
  18287. ; and the largest angle in this case is a right-angle (pi/2 radians), then
  18288. ; we can calculate angle b as pi/2 (both angles) minus asn(x) (angle a).
  18289. ;
  18290. ;
  18291. ;           /|
  18292. ;        1 /b|
  18293. ;         /  |x
  18294. ;        /a  |
  18295. ;       /----|    
  18296. ;         y
  18297. ;
  18298.  
  18299. ;; acs
  18300. L3843:  RST     28H             ;; FP-CALC      x.
  18301.         DB    $22             ;;asn           asn(x).
  18302.         DB    $A3             ;;stk-pi/2      asn(x), pi/2.
  18303.         DB    $03             ;;subtract      asn(x) - pi/2.
  18304.         DB    $1B             ;;negate        pi/2 -asn(x)  =  acs(x).
  18305.         DB    $38             ;;end-calc      acs(x).
  18306.  
  18307.         RET                     ; return.
  18308.  
  18309.  
  18310. ; --------------------------
  18311. ; THE 'SQUARE ROOT' FUNCTION
  18312. ; --------------------------
  18313. ; (Offset $28: 'sqr')
  18314. ; This routine is remarkable only in its brevity - 7 bytes.
  18315. ; It wasn't written here but in the ZX81 where the programmers had to squeeze
  18316. ; a bulky operating sytem into an 8K ROM. It simply calculates
  18317. ; the square root by stacking the value .5 and continuing into the 'to-power'
  18318. ; routine. With more space available the much faster Newton-Raphson method
  18319. ; should have been used as on the Jupiter Ace.
  18320.  
  18321. ;; sqr
  18322. L384A:  RST     28H             ;; FP-CALC
  18323.         DB    $31             ;;duplicate
  18324.         DB    $30             ;;not
  18325.         DB    $00             ;;jump-true
  18326.         DB    $1E             ;;to L386C, LAST
  18327.  
  18328.         DB    $A2             ;;stk-half
  18329.         DB    $38             ;;end-calc
  18330.  
  18331.  
  18332. ; ------------------------------
  18333. ; THE 'EXPONENTIATION' OPERATION
  18334. ; ------------------------------
  18335. ; (Offset $06: 'to-power')
  18336. ; This raises the first number X to the power of the second number Y.
  18337. ; As with the ZX80,
  18338. ; 0 ^ 0 = 1.
  18339. ; 0 ^ +n = 0.
  18340. ; 0 ^ -n = arithmetic overflow.
  18341. ;
  18342.  
  18343. ;; to-power
  18344. L3851:  RST     28H             ;; FP-CALC              X, Y.
  18345.         DB    $01             ;;exchange              Y, X.
  18346.         DB    $31             ;;duplicate             Y, X, X.
  18347.         DB    $30             ;;not                   Y, X, (1/0).
  18348.         DB    $00             ;;jump-true
  18349.         DB    $07             ;;to L385D, XISO   if X is zero.
  18350.  
  18351. ; else X is non-zero. Function 'ln' will catch a negative value of X.
  18352.  
  18353.         DB    $25             ;;ln                    Y, LN X.
  18354.         DB    $04             ;;multiply              Y * LN X.
  18355.         DB    $38             ;;end-calc
  18356.  
  18357.         JP      L36C4           ; jump back to EXP routine   ->
  18358.  
  18359. ; ---
  18360.  
  18361. ; these routines form the three simple results when the number is zero.
  18362. ; begin by deleting the known zero to leave Y the power factor.
  18363.  
  18364. ;; XISO
  18365. L385D:  DB    $02             ;;delete                Y.
  18366.         DB    $31             ;;duplicate             Y, Y.
  18367.         DB    $30             ;;not                   Y, (1/0).
  18368.         DB    $00             ;;jump-true
  18369.         DB    $09             ;;to L386A, ONE         if Y is zero.
  18370.  
  18371.         DB    $A0             ;;stk-zero              Y, 0.
  18372.         DB    $01             ;;exchange              0, Y.
  18373.         DB    $37             ;;greater-0             0, (1/0).
  18374.         DB    $00             ;;jump-true             0.
  18375.         DB    $06             ;;to L386C, LAST        if Y was any positive
  18376.                                 ;;                      number.
  18377.  
  18378. ; else force division by zero thereby raising an Arithmetic overflow error.
  18379. ; There are some one and two-byte alternatives but perhaps the most formal
  18380. ; might have been to use end-calc; rst 08; DB 05.
  18381.  
  18382.         DB    $A1             ;;stk-one               0, 1.
  18383.         DB    $01             ;;exchange              1, 0.
  18384.         DB    $05             ;;division              1/0        ouch!
  18385.  
  18386. ; ---
  18387.  
  18388. ;; ONE
  18389. L386A:  DB    $02             ;;delete                .
  18390.         DB    $A1             ;;stk-one               1.
  18391.  
  18392. ;; LAST
  18393. L386C:  DB    $38             ;;end-calc              last value is 1 or 0.
  18394.  
  18395.         RET                     ; return.               Whew!
  18396.  
  18397. ;*********************************
  18398. ;** Spectrum 128 Patch Routines **
  18399. ;*********************************
  18400.  
  18401. ; The new code added to the standard 48K Spectrum ROM is mainly devoted to the scanning and decoding of the keypad.
  18402. ; These routines occupy addresses 386E through to 3B3A. Addresses 3B3B through to 3C96 contain a variety of routines for the following purposes: displaying the new tokens 'PLAY' and 'SPECTRUM',
  18403. ; dealing with the keypad when using INKEY$, handling new 128 BASIC error messages, and producing the TV tuner display. Addresses 3BE1 to 3BFE and addresses 3C97 to 3CFF are unused and all contain 00.
  18404. ; Documented by Paul Farrow.
  18405.  
  18406. ; --------------------------------
  18407. ; SCAN THE KEYPAD AND THE KEYBOARD
  18408. ; --------------------------------
  18409. ; This patch will attempt to scan the keypad if in 128K mode and will then scan the keyboard.
  18410.  
  18411. ;; KEYS
  18412. L386E:  PUSH    IX
  18413.         BIT     4,(IY+$01)      ; [FLAGS] Test if in 128K mode
  18414.         JR      Z,L3879         ; Z=in 48K mode
  18415.  
  18416.         CALL    L3A42           ; Attempt to scan the keypad
  18417.  
  18418. ;; KEYS_CONT
  18419. L3879:  CALL    L02BF           ; Scan the keyboard
  18420.         POP     IX
  18421.         RET
  18422.  
  18423. ; ----------------------------------
  18424. ; READ THE STATE OF THE OUTPUT LINES
  18425. ; ----------------------------------
  18426. ; This routine returns the state of the four output lines (bits 0-3) in the lower four bits of L. The LSB of L corresponds to the output communication line to the keypad.
  18427. ; In this way the state of the other three outputs are maintained when the state of the LSB of L is changed and sent out to register 14 of the AY-3-8912.
  18428.  
  18429. ;; READ_OUTPUTS
  18430. L387F:  LD      C,$FD           ; FFFD = Address of the
  18431.         LD      D,$FF           ; command register (register 7)
  18432.         LD      E,$BF           ; BFFD = Address of the
  18433.         LD      B,D             ; data register (register 14)
  18434.         LD      A,$07
  18435.         OUT     (C),A           ; Select command register
  18436.         IN      H,(C)           ; Read its status
  18437.         LD      A,$0E
  18438.         OUT     (C),A           ; Select data register
  18439.         IN      A,(C)           ; Read its status
  18440.         OR      $F0             ; Mask off the input lines
  18441.         LD      L,A             ; L=state of output lines at the
  18442.         RET                     ; keypad socket
  18443.  
  18444. ; --------------------------
  18445. ; SET THE OUTPUT LINE, BIT 0
  18446. ; --------------------------
  18447. ; The output line to the keypad is set via the LSB of L.
  18448.  
  18449. ;; SET_REG14
  18450. L3896:  LD      B,D
  18451.         LD      A,$0E
  18452.         OUT     (C),A           ; Select the data register
  18453.         LD      B,E
  18454.         OUT     (C),L           ; Send L out to the data register
  18455.         RET                     ; Set the output line
  18456.  
  18457. ; ----------------------------------------
  18458. ; FETCH THE STATE OF THE INPUT LINE, BIT 5
  18459. ; ----------------------------------------
  18460. ; Return the state of the input line from the keypad in bit 5 of A.
  18461.  
  18462. ;; GET_REG14
  18463. L389F:  LD      B,D
  18464.         LD      A,$0E
  18465.         OUT     (C),A           ; Select the data register
  18466.         IN      A,(C)           ; Read the input line
  18467.         RET
  18468.  
  18469. ; ------------------------------
  18470. ; SET THE OUTPUT LINE LOW, BIT 0
  18471. ; ------------------------------
  18472.  
  18473. ;; RESET_LINE
  18474. L38A7:  LD      A,L
  18475.         AND     $FE             ; Reset bit 0 of L
  18476.         LD      L,A
  18477.         JR      L3896           ; Send out L to the data register
  18478.  
  18479. ; -------------------------------
  18480. ; SET THE OUTPUT LINE HIGH, BIT 0
  18481. ; -------------------------------
  18482.  
  18483. ;; SET_LINE
  18484. L38AD:  LD      A,L
  18485.         OR      $01             ; Set bit 0 of L
  18486.         LD      L,A
  18487.         JR      L3896           ; Send out L to the data register
  18488.  
  18489. ; -------------------
  18490. ; MINOR DELAY ROUTINE
  18491. ; -------------------
  18492. ; Delay for (B*13)+5 T-States.
  18493.  
  18494. ;; DELAY
  18495. L38B3:  DJNZ    L38B3
  18496.         RET
  18497.  
  18498. ; -------------------
  18499. ; MAJOR DELAY ROUTINE
  18500. ; -------------------
  18501. ; Delay for (B*271)+5 T-states.
  18502.  
  18503. ;; DELAY2
  18504. L38B6:  PUSH    BC
  18505.         LD      B,$10
  18506.         CALL    L38B3           ; Inner delay of 135 T-States
  18507.         POP     BC
  18508.         DJNZ    L38B6
  18509.         RET
  18510.  
  18511. ; ------------------------------------
  18512. ; MONITOR FOR THE INPUT LINE TO GO LOW
  18513. ; ------------------------------------
  18514. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18515.  
  18516. ;; MON_B5_LO
  18517. L38C0:  PUSH    BC
  18518.         CALL    L389F           ; Read the state of the input line
  18519.         POP     BC
  18520.         AND     $20             ; Test bit 5, the input line
  18521.         JR      Z,L38CB         ; Exit if input line found low
  18522.         DJNZ    L38C0           ; Repeat until timeout expires
  18523.  
  18524. ;; EXT_MON_LO
  18525. L38CB:  RET
  18526.  
  18527. ; -------------------------------------
  18528. ; MONITOR FOR THE INPUT LINE TO GO HIGH
  18529. ; -------------------------------------
  18530. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18531.  
  18532. ;; MON_B5_HI
  18533. L38CC:  PUSH    BC
  18534.         CALL    L389F           ; Read the state of the input line
  18535.         POP     BC
  18536.         AND     $20             ; Test bit 5, the input line
  18537.         JR      NZ,L38D7        ; Exit if input line found low
  18538.         DJNZ    L38CC           ; Repeat until timeout expires
  18539.  
  18540. ;; EXT_MON_HI
  18541. L38D7:  RET
  18542.  
  18543. ; -------------------------
  18544. ; READ KEY PRESS STATUS BIT
  18545. ; -------------------------
  18546. ; This entry point is used to read in the status bit for a keypad row. If a key is being pressed in the current row then the bit read in will be a 1.
  18547.  
  18548. ;; READ_STATUS
  18549. L38D8:  CALL    L387F           ; Read the output lines
  18550.         LD      B,$01           ; Read in one bit
  18551.         JR      L38E4
  18552.  
  18553. ; ----------------
  18554. ; READ IN A NIBBLE
  18555. ; ----------------
  18556. ; This entry point is used to read in a nibble of data from the keypad. It is used for two functions. The first is to read in the poll nibble and the second is to read in a row of key press data.
  18557. ; For a nibble of key press data, a bit read in as 1 indicates that the corresponding key was pressed.
  18558.  
  18559. ;; READ_NIBBLE
  18560. L38DF:  CALL    L387F           ; Read the state of the output lines
  18561.         LD      B,$04           ; Read in four bits
  18562.  
  18563. ;; READ_BIT
  18564. L38E4:  PUSH    BC
  18565.         CALL    L389F           ; Read the input line from the keypad
  18566.         POP     BC
  18567.         AND     $20             ; This line should initially be high
  18568.         JR      Z,L392D         ; Z=read in a 0, there must be an error
  18569.  
  18570.         XOR     A               ; The bits read in will be stored in register A
  18571.  
  18572. ;; BIT_LOOP
  18573. L38EE:  PUSH    BC              ; Preserve the loop count and any bits
  18574.         PUSH    AF              ; read in so far
  18575.         CALL    L38AD           ; Set the output line high
  18576.  
  18577.         LD      B,$A3           ; Monitor for 17609 T-states for the
  18578.         CALL    L38C0           ; input line to go low
  18579.         JR      NZ,L392B        ; NZ=the line did not go low
  18580.  
  18581.         CALL    L38A7           ; Set the output line low
  18582.         JR      L3901           ; Insert a delay of 12 T-states
  18583.  
  18584. L38FF:  DB    $FF, $FF
  18585.  
  18586. ;; BL_CONTINUE
  18587. L3901:  LD      B,$2B           ; Delay for 564 T-states
  18588.         CALL    L38B3
  18589.         CALL    L389F           ; Read in the bit value
  18590.         BIT     5,A
  18591.         JR      Z,L3911         ; Z=read in a 0
  18592.  
  18593.         POP     AF              ; Retrieve read in bits
  18594.         SCF                     ; Set carry bit
  18595.         JR      L3914
  18596.  
  18597. ;; BL_READ_0
  18598. L3911:  POP     AF              ; Retrieve read in bits
  18599.         SCF
  18600.         CCF                     ; Clear carry bit
  18601.  
  18602. ;; BL_STORE
  18603. L3914:  RRA                     ; Shift the carry bit into bit 0 of A
  18604.         PUSH    AF              ; Save bits read in
  18605.         CALL    L38AD           ; Set the output line high
  18606.  
  18607.         LD      B,$26           ; Delay for 499 T-states
  18608.         CALL    L38B3
  18609.  
  18610.         CALL    L38A7           ; Set the output line low
  18611.  
  18612.         LD      B,$23           ; Delay for 460 T-states
  18613.         CALL    L38B3
  18614.  
  18615.         POP     AF              ; Retrieve read in bits
  18616.         POP     BC              ; Retrieve loop counter and repeat
  18617.         DJNZ    L38EE           ; for all bits to read in
  18618.         RET
  18619.  
  18620. ; ----------
  18621. ; LINE ERROR
  18622. ; ----------
  18623. ; The input line was found at the wrong level. The output line is now set high which will eventually cause the keypad to abandon its transmissions.
  18624. ; The upper nibble of system variable FLAGS/ROW3 will be cleared to indicate that communications to the keypad is no longer in progress.
  18625.  
  18626. ;; LINE_ERROR
  18627. L392B:  POP     AF
  18628.         POP     BC              ; Clear the stack
  18629.  
  18630. ;; LINE_ERROR2
  18631. L392D:  CALL    L38AD           ; Set the output line high
  18632.  
  18633.         XOR     A               ; Clear FLAGS nibble
  18634.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18635.  
  18636.         INC     A               ; Return zero flag reset
  18637.         SCF
  18638.         CCF                     ; Return carry flag reset
  18639.         RET
  18640.  
  18641. ; ---------------
  18642. ; POLL THE KEYPAD
  18643. ; ---------------
  18644. ; The Spectrum 128 polls the keypad by changing the state of the output line and monitoring for responses from the keypad on the input line.
  18645. ; Before a poll occurs, the poll counter must be decremented until it reaches zero. This counter causes a delay of three seconds before a communications attempt to the keypad is made.
  18646. ; The routine can exit at five different places and it is the state of the A register, the zero flag and the carry flag which indicates the cause of the exit. This is summarised below:
  18647. ;
  18648. ; A Register    Zero Flag       Carry Flag    Cause
  18649. ; 0             set             set           Communications already established
  18650. ; 0             set             reset         Nibble read in OK
  18651. ; 1             reset           reset         Nibble read in with an error or i/p line initially low
  18652. ; 1             reset           set           Poll counter has not yet reached zero
  18653. ;
  18654. ; The third bit of the nibble read in must be set for the poll to be subsequently accepted.
  18655.  
  18656. ;; ATTEMPT_POLL
  18657. L3938:  CALL    L387F           ; Read the output line states
  18658.  
  18659.         LD      A,($5B88)       ; [FLAGS/ROW3] Has communications already been
  18660.         AND     $80             ; established with the keypad?
  18661.         JR      NZ,L3999        ; NZ=yes, so skip the poll
  18662.  
  18663.         CALL    L389F           ; Read the input line
  18664.         AND     $20             ; It should be high initially
  18665.         JR      Z,L392D         ; Z=error, input line found low
  18666.  
  18667.         LD      A,($5B88)       ; [FLAGS/ROW3] Test if poll counter already zero thus
  18668.         AND     A               ; indicating a previous comms error
  18669.         JR      NZ,L395A        ; NZ=ready to poll the keypad
  18670.  
  18671.         INC     A               ; Indicate comms not established
  18672.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18673.         LD      A,$4C           ; Reset the poll counter
  18674.         LD      ($5B89),A       ; [ROW2/ROW1]
  18675.         JR      L399C           ; Exit the routine
  18676.  
  18677. ;;POLL_KEYPAD
  18678. L395A:  LD      A,($5B89)       ; [ROW2/ROW1] Decrement the poll counter
  18679.         DEC     A
  18680.         LD      ($5B89),A       ; [ROW2/ROW1]
  18681.         JR      NZ,L399C        ; Exit the routine if it is not yet zero
  18682.  
  18683. ; The poll counter has reached zero so a poll of the keypad can now occur.
  18684.  
  18685.         XOR     A
  18686.         LD      ($5B88),A       ; [FLAGS/ROW3] Indicate that a poll can occur
  18687.         LD      ($5B89),A       ; [ROW2/ROW1]
  18688.         LD      ($5B8A),A       ; [ROW4/ROW5] Clear all the row nibble stores
  18689.  
  18690.         CALL    L38A7           ; Set the output line low
  18691.  
  18692.         LD      B,$21           ; Wait up to 3569 T-States for the
  18693.         CALL    L38C0           ; input line to go low
  18694.         JR      NZ,L392D        ; NZ=line did not go low
  18695.  
  18696.         CALL    L38AD           ; Set the output line high
  18697.  
  18698.         LD      B,$24           ; Wait up to 3893 T-States for the
  18699.         CALL    L38CC           ; input line to go high
  18700.         JR      Z,L392D         ; NZ=line did not go high
  18701.  
  18702.         CALL    L38A7           ; Set the output line low
  18703.  
  18704.         LD      B,$0F
  18705.         CALL    L38B6           ; Delay for 4070 T-States
  18706.         CALL    L38DF           ; Read in a nibble of data
  18707.         JR      NZ,L392D        ; NZ=error occurred when reading in nibble
  18708.  
  18709.         SET     7,A             ; Set bit 7
  18710.         AND     $F0             ; Keep only the upper four bits
  18711.                                 ; (Bit 6 will be set if poll successful)
  18712.         LD      ($5B88),A       ; [FLAGS/ROW3] Store the flags nibble
  18713.         XOR     A
  18714.         SRL     A               ; Exit: Zero flag set, Carry flag reset
  18715.         RET
  18716.  
  18717. ;; AP_SKIP_POLL
  18718. L3999:  XOR     A               ; Communications already established
  18719.         SCF                     ; Exit: Zero flag set, Carry flag set
  18720.         RET
  18721.  
  18722. ;; PK_EXIT
  18723. L399C:  XOR     A               ; Poll counter not zero
  18724.         INC     A
  18725.         SCF                     ; Exit: Zero flag reset, Carry flag set
  18726.         RET
  18727.  
  18728. ; -----------------------
  18729. ; SCAN THE KEYPAD ROUTINE
  18730. ; -----------------------
  18731. ; If a successful poll of the keypad occurs then the five rows of keys are read in and a unique key code generated.
  18732.  
  18733. ;; KEYPAD_SCAN
  18734. L39A0:  CALL    L3938           ; Try to poll the keypad
  18735.  
  18736.         LD      A,($5B88)       ; [FLAGS/ROW3] Test the flags nibble
  18737.         CPL
  18738.         AND     $C0             ; Bits 6 and 7 must be set in FLAGS
  18739.         RET     NZ              ; NZ=poll was not successful
  18740.  
  18741. ; The poll was successful so now read in data for the five keypad rows.
  18742.  
  18743.         LD      IX,$5B8A        ; [ROW4/ROW5]
  18744.         LD      B,$05           ; The five rows
  18745.  
  18746. ;; KS_LOOP
  18747. L39B0:  PUSH    BC              ; Save counter
  18748.  
  18749.         CALL    L38D8           ; Read the key press status bit
  18750.         JP      NZ,L3A3A        ; NZ=error occurred
  18751.  
  18752.         BIT     7,A             ; Test the bit read in
  18753.         JR      Z,L39DC         ; Z=no key pressed in this row
  18754.  
  18755.         CALL    L38DF           ; Read in the row's nibble of data
  18756.         JR      NZ,L3A3A        ; NZ=error occurred
  18757.  
  18758.         POP     BC              ; Fetch the nibble loop counter
  18759.         PUSH    BC
  18760.         LD      C,A             ; Move the nibble read in to C
  18761.         LD      A,(IX+$00)      ; Fetch the nibble store
  18762.         BIT     0,B             ; Test if an upper or lower nibble
  18763.         JR      Z,L39D6         ; Z=upper nibble
  18764.  
  18765.         SRL     C               ; Shift the nibble to the lower position
  18766.         SRL     C
  18767.         SRL     C
  18768.         SRL     C
  18769.         AND     $F0             ; Mask off the lower nibble of the
  18770.         JR      L39D8           ; nibble store
  18771.  
  18772. ;; KS_UPPER
  18773. L39D6:  AND     $0F             ; Mask off the upper nibble of the nibble store
  18774.  
  18775. ;; KS_STORE
  18776. L39D8:  OR      C               ; Combine the existing and new
  18777.         LD      (IX+$00),A      ; nibbles and store them
  18778.  
  18779. ;; KS_NEXT
  18780. L39DC:  POP     BC              ; Retrieve the row counter
  18781.         BIT     0,B             ; Test if next nibble store is required
  18782.         JR      NZ,L39E3        ; NZ=use same nibble store
  18783.  
  18784.         DEC     IX              ; Point to the next nibble store
  18785.  
  18786. ;; KS_NEW
  18787. L39E3:  DJNZ    L39B0           ; Repeat for the next keypad row
  18788.  
  18789. ; All five rows have now been read so compose a unique code for the key pressed.
  18790.  
  18791.         LD      E,$80           ; Signal no key press found yet
  18792.         LD      IX,$5B88        ; [FLAGS/ROW3]
  18793.         LD      HL,$3A3F        ; Point to the key mask data
  18794.         LD      B,$03           ; Scan three nibbles
  18795.  
  18796. ;; GEN_LOOP
  18797. L39F0:  LD      A,(IX+$00)      ; Fetch a pair of nibbles
  18798.         AND     (HL)            ; This will mask off the FLAGS nibble and the SHIFT/0 key
  18799.  
  18800.         JR      Z,L3A17         ; Z=no key pressed in these nibbles
  18801.  
  18802.         BIT     7,E             ; Test if a key has already been found
  18803.         JR      Z,L3A3C         ; Z=multiple keys pressed
  18804.  
  18805.         PUSH    BC              ; Save the loop counter
  18806.         PUSH    AF              ; Save the byte of key bit data
  18807.         LD      A,B             ; Move loop counter to A
  18808.         JR      L3A01           ; A delay of 12 T-States
  18809.  
  18810. L39FF:  DB    $FF, $FF        ; Unused locations
  18811.  
  18812. ;; GEN_CONT
  18813. L3A01:  DEC     A               ; These lines of code generate base
  18814.         SLA     A               ; values of 7, 15 and 23 for the three
  18815.         SLA     A               ; nibble stores 5B88, 5B89 & 5B8A.
  18816.         SLA     A
  18817.         OR      $07
  18818.         LD      B,A             ; B=(loop counter-1)*8+7
  18819.         POP     AF              ; Fetch the byte of key press data
  18820.  
  18821. ;; GEN_BIT
  18822. L3A0C:  SLA     A               ; Shift until a set key bit drops into the
  18823.         JP      C,L3A13         ; carry flag
  18824.  
  18825.         DJNZ    L3A0C           ; Decrement B for each 'unsuccessful' shift of the A register
  18826.  
  18827. ;; GEN_FOUND
  18828. L3A13:  LD      E,B             ; E=a unique number for the key pressed, between 1 - 19 except 2 & 3
  18829.  
  18830.         POP     BC              ; As a result shifting the set key bit
  18831.                                 ; into the carry flag, the A register will
  18832.                                 ; hold 00 if only one key was pressed
  18833.         JR      NZ,L3A3C        ; NZ=multiple keys pressed
  18834.  
  18835. ;; GEN_NEXT
  18836. L3A17:  INC     IX              ; Point to the next nibble store
  18837.         INC     HL              ; Point to the corresponding mask data
  18838.         DJNZ    L39F0           ; Repeat for all three 'nibble' bytes
  18839.  
  18840.         BIT     7,E             ; Test if any keys were pressed
  18841.         JR      NZ,L3A27        ; NZ=no keys were pressed
  18842.  
  18843.         LD      A,E             ; Copy the key code
  18844.         AND     $FC             ; Test for the '.' key (E=1)
  18845.         JR      Z,L3A27         ; Z='.' key pressed
  18846.  
  18847.         DEC     E
  18848.         DEC     E               ; Key code in range 2 - 17
  18849.  
  18850. ; The E register now holds a unique key code value between 1 and 17.
  18851.  
  18852. ;; GEN_POINT
  18853. L3A27:  LD      A,($5B8A)       ; [ROW4/ROW5] Test if the SHIFT key was pressed
  18854.         AND     $08
  18855.         JR      Z,L3A34         ; Z=the SHIFT key was not pressed
  18856.  
  18857. ; The SHIFT key was pressed or no key was pressed.
  18858.  
  18859.         LD      A,E             ; Fetch the key code
  18860.         AND     $7F             ; Mask off 'no key pressed' bit
  18861.         ADD     A,$12           ; Add on a shift offset of 12
  18862.         LD      E,A
  18863.  
  18864. ; Add a base offset of 5A to all key codes. Note that no key press will result in a key code of DA. This is the only code with bit 7 set and so will be detected later.
  18865.  
  18866. ;; GEN_NOSHIFT
  18867. L3A34:  LD      A,E
  18868.         ADD     A,$5A           ; Add a base offset of 5A
  18869.         LD      E,A             ; Return key codes in range 5B - 7D
  18870.         XOR     A
  18871.         RET                     ; Exit: Zero flag set, key found OK
  18872.  
  18873. ; These two lines belong with the loop above to read in the five keypad rows and are jumped to when an error occurs during reading in a nibble of data.
  18874.  
  18875. ;; KS_ERROR
  18876. L3A3A:  POP     BC              ; Clear the stack and exit
  18877.         RET                     ; Exit: Zero flag reset
  18878.  
  18879. ;; GEN_INVALID
  18880. L3A3C:  XOR     A               ; Exit: Zero flag reset indicating an
  18881.         INC     A               ; invalid key press
  18882.         RET
  18883.  
  18884. ; ----------------
  18885. ; KEYPAD MASK DATA
  18886. ; ----------------
  18887.  
  18888. ;; KEY_MASKS
  18889. L3A3F:  DB    $0F, $FF, $F2   ; Key mask data
  18890.  
  18891. ; ---------------
  18892. ; READ THE KEYPAD
  18893. ; ---------------
  18894. ; This routine reads the keypad and handles key repeat and decoding. The bulk of the key repeat code is very similar to that used in the equivalent keyboard routine and works are follows.
  18895. ; A double system of KSTATE system variables (KSTATE0 - KSTATE3 and KSTATE4 - KSTATE7) is used to allow the detection of one key while in the repeat period of the previous key.
  18896. ; In this way, a 'spike' from another key will not stop the previous key from repeating. For a new key to be acknowledged, it must be held down for at least 1/5th of a second, i.e. ten calls to KEYPAD.
  18897. ; The KSTATE system variables store the following data:
  18898. ;
  18899. ;       KSTATE0/4       Un-decoded Key Value (00-27 for keyboard, 5B-7D for keypad, FF for no key)
  18900. ;       KSTATE1/5       10 Call Counter
  18901. ;       KSTATE2/6       Repeat Delay
  18902. ;       KSTATE3/7       Decoded Key Value
  18903. ;
  18904. ; The code returned is then stored in system variable LAST_K (5C08) and a new key signalled by setting bit 5 of FLAGS (5C3B).
  18905. ;
  18906. ; If the Spectrum 128 were to operate identically to the standard 48K Spectrum when in 48K mode, it would have to spend zero time in reading the keypad.
  18907. ; As this is not possible, the loading on the CPU is reduced by scanning the keypad upon every other interrupt. A '10 Call Counter' is then used to ensure that a key is held down for at least 1/5th of a second
  18908. ; before it is registered. Note that this is twice as long as for keyboard key presses and so the keypad key repeat delay is halved.
  18909. ;
  18910. ; At every other interrupt the keypad scanning routine is skipped. The net result of the routine is simply to decrement both '10 Call Counters', if appropriate. By loading the E register with 80 ensures that
  18911. ; the call to KP_TEST will reject the key code and cause a return. A test for keyboard key codes prevents the Call Counter decrements affecting a keyboard key press. It would have been more efficient to execute
  18912. ; a return upon every other call to KEYPAD and then to have used a '5 Call Counter' just as the keyboard routine does.
  18913. ;
  18914. ; A side effect of both the keyboard and keypad using the same KSTATE system variables is that if a key is held down on the keypad and then a key is held down on the keyboard, both keys will be monitored and
  18915. ; repeated alternatively, but with a reduced repeat delay. This delay is between the keypad key repeat delay and the keyboard key repeat delay. This occurs because both the keypad and keyboard routines will
  18916. ; decrement the KSTATE system variable Call Counters. The keypad routine 'knows' of the existence of keyboard key codes but the reverse is not true.
  18917.  
  18918. ;; KEYPAD
  18919. L3A42:  LD      E,$80           ; Signal no key pressed
  18920.         LD      A,($5C78)       ; [FRAMES]
  18921.         AND     $01             ; Scan the keypad every other
  18922.         JR      NZ,L3A4F        ; interrupt
  18923.  
  18924.         CALL    L39A0
  18925.         RET     NZ              ; NZ=no valid key pressed
  18926.  
  18927. ;; KP_CHECK
  18928. L3A4F:  LD HL,$5C00             ; [KSTATE0] Test the first KSTATE variable
  18929.  
  18930. ;; KP_LOOP
  18931. L3A52:  BIT     7,(HL)          ; Is the set free?
  18932.         JR      NZ,L3A62        ; NZ=yes
  18933.  
  18934.         LD      A,(HL)          ; Fetch the un-decoded key value
  18935.         CP      $5B             ; Is it a keyboard code?
  18936.         JR      C,L3A62         ; C=yes, so do not decrement counter
  18937.  
  18938.         INC     HL
  18939.         DEC     (HL)            ; Decrement the 10 Call Counter
  18940.         DEC     HL
  18941.         JR      NZ,L3A62        ; If the counter reaches zero, then
  18942.                                 ; signal the set is free
  18943.         LD      (HL),$FF
  18944.  
  18945. ;; KP_CH_SET
  18946. L3A62:  LD      A,L             ; Jump back and test the second set if
  18947.         LD      HL,$5C04        ; [KSTATE4] not yet considered
  18948.         CP      L
  18949.         JR      NZ,L3A52
  18950.  
  18951.         CALL    L3AAE           ; Test for valid key combinations and
  18952.         RET     NZ              ; return if invalid
  18953.  
  18954.         LD      A,E             ; Test if the key in the first set is being
  18955.         LD      HL,$5C00        ; [KSTATE0] repeated
  18956.         CP      (HL)
  18957.         JR      Z,L3A9E         ; Jump if being repeated
  18958.  
  18959.         EX      DE,HL           ; Save the address of KSTATE0
  18960.         LD      HL,$5C04        ; [KSTATE4] Test if the key in the second set is
  18961.         CP      (HL)            ; being repeated
  18962.         JR      Z,L3A9E         ; Jump if being repeated
  18963.  
  18964. ; A new key will not be accepted unless one of the KSTATE sets is free.
  18965.  
  18966.         BIT     7,(HL)          ; Test if the second set is free
  18967.         JR      NZ,L3A83        ; Jump if set is free
  18968.  
  18969.         EX      DE,HL
  18970.         BIT     7,(HL)          ; Test if the first set is free
  18971.         RET     Z               ; Return if no set is free
  18972.  
  18973. ;; KP_NEW
  18974. L3A83:  LD      E,A             ; Pass the key code to the E register
  18975.         LD      (HL),A          ; and to KSTATE0/4
  18976.         INC     HL
  18977.         LD      (HL),$0A        ; Set the '10 Call Counter' to 10
  18978.         INC     HL
  18979.  
  18980.         LD      A,($5C09)       ; [REPDEL] Fetch the initial repeat delay
  18981.         SRL     A               ; Divide delay by two
  18982.         LD      (HL),A          ; Store the repeat delay
  18983.         INC     HL
  18984.  
  18985.         CALL    L3AD7           ; Decode the keypad key code
  18986.         LD      (HL),E          ; and store it in KSTATE3/7
  18987.  
  18988. ; This section is common for both new keys and repeated keys.
  18989.  
  18990. ;; KP_END
  18991. L3A94:  LD      A,E
  18992.         LD      ($5C08),A       ; [LAST_K] Store the key value in LAST_K
  18993.         LD      HL,$5C3B        ; FLAGS
  18994.         SET     5,(HL)          ; Signal a new key pressed
  18995.         RET
  18996.  
  18997. ; -------------------------
  18998. ; THE KEY REPEAT SUBROUTINE
  18999. ; -------------------------
  19000.  
  19001. ;; KP_REPEAT
  19002. L3A9E:  INC     HL
  19003.         LD      (HL),$0A        ; Reset the '10 Call Counter' to 10
  19004.         INC     HL
  19005.         DEC     (HL)            ; Decrement the repeat delay
  19006.         RET     NZ              ; Return if not zero
  19007.  
  19008.         LD      A,($5C0A)       ; [REPPER] The subsequent repeat delay is
  19009.         SRL     A               ; divided by two and stored
  19010.         LD      (HL),A
  19011.         INC     HL
  19012.         LD      E,(HL)          ; The key repeating is fetched
  19013.         JR      L3A94           ; and then returned in LAST_K
  19014.  
  19015. ; ----------------------------------------
  19016. ; THE TEST FOR A VALID KEY CODE SUBROUTINE
  19017. ; ----------------------------------------
  19018. ; The zero flag is returned set if the key code is valid. No key press, SHIFT only or invalid shifted key presses return the zero flag reset.
  19019.  
  19020. ;; KP_TEST
  19021. L3AAE:  LD      A,E
  19022.         LD      HL,$5B66        ; FLAGS3 Test if in BASIC or EDIT mode
  19023.         BIT     0,(HL)
  19024.         JR      Z,L3ABC         ; Z=EDIT mode
  19025.  
  19026. ; Test key codes when in BASIC/CALCULATOR mode
  19027.  
  19028.         CP      $6D             ; Test for shifted keys
  19029.         JR      NC,L3AD4        ; and signal an error if found
  19030.  
  19031. ;; KPT_OK
  19032. L3ABA:  XOR     A               ; Signal valid key code
  19033.         RET                     ; Exit: Zero flag set
  19034.  
  19035. ; Test key codes when in EDIT/MENU mode.
  19036.  
  19037. ;; KPT_EDIT
  19038. L3ABC:  CP      $80             ; Test for no key press
  19039.         JR      NC,L3AD4        ; NC=no key press
  19040.  
  19041.         CP      $6C             ; Test for SHIFT on its own
  19042.         JR      NZ,L3ABA        ; NZ=valid key code
  19043.  
  19044. L3AC4:  DB    $00, $00, $00   ; Delay for 64 T-States
  19045.         DB    $00, $00, $00
  19046.         DB    $00, $00, $00
  19047.         DB    $00, $00, $00
  19048.         DB    $00, $00, $00
  19049.         DB    $00
  19050.  
  19051. ;; KPT_INVALID
  19052. L3AD4:  XOR     A               ; Signal invalid key code
  19053.         INC     A
  19054.         RET                     ; Exit: Zero flag reset
  19055.  
  19056. ; ---------------------------
  19057. ; THE KEY DECODING SUBROUTINE
  19058. ; ---------------------------
  19059.  
  19060. ;; KP_DECODE
  19061. L3AD7:  PUSH    HL              ; Save the KSTATE pointer
  19062.         LD      A,E
  19063.         SUB     $5B             ; Reduce the key code range to
  19064.         LD      D,$00           ; 00 - 22 and transfer to DE
  19065.         LD      E,A
  19066.  
  19067.         LD      HL,$5B66        ; FLAGS3 Test if in EDIT or BASIC mode
  19068.         BIT     0,(HL)
  19069.         JR      Z,L3AEA         ; Z=EDIT/MENU mode
  19070.  
  19071. ; Use Table 1 when in CALCULATOR/BASIC mode.
  19072.  
  19073.         LD      HL,L3B13
  19074.         JR      L3B0F           ; Look up the key value
  19075.  
  19076. ; Deal with EDIT/MENU mode.
  19077.  
  19078. ;; KPD_EDIT
  19079. L3AEA:  LD      HL,L3B25        ; Use Table 4 for unshifted key
  19080.         CP      $11             ; presses
  19081.         JR      C,L3B0F
  19082.  
  19083. ; Deal with shifted keys in EDIT/MENU mode.
  19084.  
  19085. ; Use Table 3 with SHIFT 1 (delete to beginning of line), SHIFT 2 (delete to end of line), SHIFT 3 (SHIFT TOGGLE). Note that although SHIFT TOGGLE produces a unique valid code,
  19086. ; it actually performs no function when editing a BASIC program.
  19087.  
  19088.         LD      HL,L3B21
  19089.         CP      $15             ; Test for SHIFT 1
  19090.         JR      Z,L3B0F
  19091.  
  19092.         CP      $16             ; Test for SHIFT 2
  19093.         JR      Z,L3B0F
  19094.  
  19095.         JR      L3B01           ; Delay for 12 T-States
  19096.  
  19097. L3AFE:  DB    $00, $FF, $FF   ; Unused locations
  19098.  
  19099. ;; KPD_CONT
  19100. L3B01:  CP      $17             ; Test for SHIFT 3
  19101.         JR      Z,L3B0F
  19102.  
  19103. ; Use Table 2 with SHIFT 4 (delete to beginning of word) and SHIFT 5 (delete to end of word).
  19104.  
  19105.         LD      HL,L3B18
  19106.         CP      $21             ; Test for SHIFT 4 and above
  19107.         JR      NC,L3B0F
  19108.  
  19109. ;Use Table 1 for all other shifted key presses.
  19110.  
  19111.         LD      HL,L3B13
  19112.  
  19113. ;; KPD_EXIT
  19114. L3B0F:  ADD     HL,DE           ; Look up the key value
  19115.         LD      E,(HL)
  19116.         POP     HL              ; Retrieve the KSTATE address
  19117.         RET
  19118.  
  19119. ; --------------------------------
  19120. ; THE KEYPAD DECODE LOOK-UP TABLES
  19121. ; --------------------------------
  19122.  
  19123. ;; KPD_TABLE1
  19124. L3B13:  DB    $2E, $0D, $33   ; '.', ENTER, 3
  19125.         DB    $32, $31        ; 2, 1
  19126.  
  19127. ;; KPD_TABLE2
  19128. L3B18:  DB    $29, $28, $2A   ; ), (, *
  19129.         DB    $2F, $2D, $39   ; /, - , 9
  19130.         DB    $38, $37, $2B   ; 8, 7, +
  19131.  
  19132. ;; KPD_TABLE3
  19133. L3B21:  DB    $36, $35, $34   ; 6, 5, 4
  19134.         DB    $30             ; 0
  19135.  
  19136. ;; KPD_TABLE4
  19137. L3B25:  DB    $A5, $0D, $A6   ; Bottom, ENTER, Top
  19138.         DB    $A7, $A8, $A9   ; End of line, Start of line, TOGGLE
  19139.         DB    $AA, $0B, $0C   ; DEL right, Up, DEL
  19140.         DB    $07, $09, $0A   ; CMND, Right, Down
  19141.         DB    $08, $AC, $AD   ; Left, Down ten, Up ten
  19142.         DB    $AE, $AF        ; End word, Beginning of word
  19143.         DB    $B0, $B1, $B2   ; DEL to end of line, DEL to start of line, SHIFT TOGGLE
  19144.         DB    $B3, $B4        ; DEL to end of word, DEL to beginning of word
  19145.  
  19146. ; -----------------------------
  19147. ; PRINT NEW ERROR MESSAGE PATCH
  19148. ; -----------------------------
  19149.  
  19150. L3B3B:  BIT     4,(IY+$01)      ; FLAGS 3 - In 128K mode?
  19151.         JR      NZ,L3B46        ; NZ=128K mode
  19152.  
  19153. ; In 48K mode
  19154.  
  19155.         XOR     A               ; Replicate code from standard ROM that the patch over-wrote
  19156.         LD      DE,$1536
  19157.         RET    
  19158.  
  19159. ; In 128K mode
  19160.  
  19161. L3B46:  LD      HL,$010F        ; Vector table entry in Editor ROM -> JP $03A2
  19162.  
  19163. ; Return to Editor ROM at address in HL
  19164.  
  19165. L3B49:  EX      (SP),HL         ; Change the return address
  19166.         JP      $5B00           ; Page Editor ROM and return to the address on the stack
  19167.  
  19168. ; -------------------------------------
  19169. ; STATEMENT INTERPRETATION RETURN PATCH
  19170. ; -------------------------------------
  19171.  
  19172. L3B4D:  BIT     4,(IY+$01)      ; In 128K mode?
  19173.         JR      NZ,L3B58        ; NZ=128K mode
  19174.  
  19175. ; In 48K mode
  19176.  
  19177.         BIT     7,(IY+$0A)      ; replicate code from standard ROM that the patch over-wrote
  19178.         RET    
  19179.  
  19180. ; In 128K mode
  19181.  
  19182. L3B58:  LD      HL,$0112        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #182A
  19183.         JR      L3B49
  19184.  
  19185. ; --------------------------
  19186. ; GO TO NEXT STATEMENT PATCH
  19187. ; --------------------------
  19188.  
  19189. L3B5D:  BIT     4,(IY+$01)      ; In 128K mode?
  19190.         JR      NZ,L3B67        ; NZ=128K mode
  19191.  
  19192. ; In 48K mode
  19193.  
  19194.         RST     18H             ; replicate code from standard ROM that the patch over-wrote
  19195.         CP      $0D
  19196.         RET    
  19197.  
  19198. ; In 128K mode
  19199.  
  19200. L3B67:  LD      HL,$0115        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #18A8
  19201.         JR      L3B49
  19202.  
  19203. ; --------------------------------------
  19204. ; INKEY$ ROUTINE TO DEAL WITH THE KEYPAD
  19205. ; --------------------------------------
  19206.  
  19207. ;; KEYSCAN2
  19208. L3B6C:  CALL    L028E           ; KEYSCAN Scan the keyboard
  19209.         LD      C,$00
  19210.         JR      NZ,L3B80        ; NZ=multiple keys
  19211.  
  19212.         CALL    L031E           ; K_TEST
  19213.         JR      NC,L3B80        ; NC=shift only or no key
  19214.  
  19215.         DEC     D
  19216.         LD      E,A
  19217.         CALL    L0333           ; K_DECODE
  19218.         JP      L2657           ; S_CONT Get string and continue scanning
  19219.  
  19220. ;; KPI_SCAN
  19221. L3B80:  BIT     4,(IY+$01)      ; 128K mode?
  19222.         JP      Z,L2660         ; S_IK$_STK Z=no, stack keyboard code
  19223.  
  19224.         DI                      ; Disable interrupts whilst scanning
  19225.         CALL    L39A0           ; the keypad
  19226.         EI
  19227.         JR      NZ,L3B9A        ; NZ=multiple keys
  19228.  
  19229.         CALL    L3AAE           ; Test the keypad
  19230.         JR      NZ,L3B9A        ; NZ=no key, shift only or invalid combination
  19231.  
  19232.         CALL    L3AD7           ; Form the key code
  19233.         LD      A,E
  19234.         JP      L2657           ; S_CONT Get string and continue scanning
  19235.  
  19236. ;; KPI_INVALID
  19237. L3B9A:  LD      C,$00           ; Signal no key, i.e. length=0
  19238.         JP      L2660           ; S_IK$_STK
  19239.  
  19240. ; ---------------------
  19241. ; PRINT TOKEN/UDG PATCH
  19242. ; ---------------------
  19243.  
  19244. L3B9F:  CP      $A3             ; SPECTRUM (T)
  19245.         JR      Z,L3BAF
  19246.  
  19247.         CP      $A4             ; PLAY (U)
  19248.         JR      Z,L3BAF
  19249.  
  19250. ; In 48K mode here
  19251.  
  19252. L3BA7:  SUB     $A5             ; Check as per original ROM
  19253.         JP      NC,$0B5F
  19254.  
  19255.         JP      $0B56           ; Rejoin original ROM routine
  19256.  
  19257. L3BAF:  BIT     4,(IY+$01)      ; FLAGS3 - Bit 4=1 if in 128K mode
  19258.         JR      Z,L3BA7         ; Rejoin code for when in 48K mode
  19259.  
  19260. ; In 128K mode here
  19261.  
  19262.         LD      DE,L3BC9
  19263.         PUSH    DE              ; Stack return address
  19264.  
  19265.         SUB     $A3             ; Check whether the SPECTRUM token
  19266.  
  19267.         LD      DE,L3BD2        ; SPECTRUM token
  19268.         JR      Z,L3BC3
  19269.  
  19270.         LD      DE,L3BDA        ; PLAY token
  19271.  
  19272. L3BC3:  LD      A,$04           ; Signal not RND, INKEY$ or PI so that a trailing space is printed
  19273.         PUSH    AF
  19274.         JP      L0C17           ; Rejoin printing routine PO-TABLE+3
  19275.  
  19276. ; Return address from above
  19277.  
  19278. L3BC9:  SCF                     ; Return as if no trailing space
  19279.  
  19280.         BIT     1,(IY+$01)      ; Test if printer is in use
  19281.         RET     NZ              ; NZ=printer in use
  19282.  
  19283.         JP      $0B03           ; PO-FETCH - Return via Position Fetch routine
  19284.  
  19285. L3BD2           DC "SPECTRUM"   ;DEFM    "SPECTRU"       ; SPECTRUM token
  19286.                                 ;DB    'M'+$80
  19287.  
  19288. L3BDA           DC "PLAY"       ;DEFM    "PLA"           ; PLAY token
  19289.                                 ;DB    'Y'+$80
  19290.  
  19291. ;; KP_SCAN2
  19292. L3BDE:  JP      L3C01           ; This is not called from either ROM. It can be used to scan the keypad.
  19293.  
  19294. ;===============================
  19295.                 IF TAP_EMUL
  19296. INI_TAP         RST 0X08
  19297.                 DB TAPE_INIT
  19298.                 SCF
  19299.                 SBC HL,DE
  19300.                 RET
  19301.                 ENDIF
  19302.  
  19303.                 IF AY_PRINTER
  19304. PRINTER_INITER  RST 0X08
  19305.                 DB AY_PRN_INIT
  19306.                 JP L0EDF
  19307.                 ENDIF
  19308.  
  19309. PRN_TOKEN       RST 0X08
  19310.                 DB AY_PRN_TOKEN
  19311.                 RET
  19312.  
  19313.                 DUPL 0X3BFF-$,0
  19314.                 DW 0XFFFF
  19315. ;===============================
  19316.  
  19317. ;; KP_SCAN
  19318. L3C01:  JP      L39A0           ; This was to be called via the vector table in the EDITOR ROM but due to a programming error it never gets called.
  19319.  
  19320. ; -----------------------
  19321. ; TV TUNER VECTOR ENTRIES
  19322. ; -----------------------
  19323.  
  19324. L3C04:  JP      L3C10
  19325. L3C07:  JP      L3C10
  19326. L3C0A:  JP      L3C10
  19327. L3C0D:  JP      L3C10
  19328.  
  19329. ; ----------------
  19330. ; TV TUNER ROUTINE
  19331. ; ----------------
  19332. ; This routine generates a display showing all possible colours and emitting a continuous cycle of a 440Hz tone for 1 second followed by silence for 1 second.
  19333. ; Its purpose is to ease the tuning in of TV sets to the Spectrum 128's RF signal. The display consists of vertical stripes of width four character squares showing each of the eight colours
  19334. ; available at both their normal and bright intensities. The display begins with white on the left progressing up to black on the right. With in each colour stripe in the first eight rows is
  19335. ; shown the year '1986' in varying ink colours. This leads to a display that shows all possible ink colours on all possible paper colours.
  19336.  
  19337. ;; TV_TUNER
  19338. L3C10:  LD      A,$7F           ; Test for the BREAK key
  19339.         IN      A,($FE)
  19340.         RRA    
  19341.         RET     C               ; C=SPACE not pressed
  19342.  
  19343.         LD      A,$FE
  19344.         IN      A,($FE)
  19345.         RRA    
  19346.         RET     C               ; C=SPACE not pressed
  19347.  
  19348.         LD      A,$07
  19349.         OUT     ($FE),A         ; Set the border to white
  19350.  
  19351.         LD      A,$02           ; Open channel 2 (main screen)
  19352.         CALL    $1601
  19353.  
  19354.         XOR     A
  19355.         LD      ($5C3C),A       ; [TV_FLAG] Signal using main screen
  19356.  
  19357.         LD      A,$16           ; Print character 'AT'
  19358.         RST     10H
  19359.  
  19360.         XOR     A               ; Print character '0'
  19361.         RST     10H
  19362.  
  19363.         XOR     A               ; Print character '0'
  19364.         RST     10H
  19365.  
  19366.         LD      E,$08           ; Number of characters per colour
  19367.         LD      B,E             ; Paper counter + 1
  19368.         LD      D,B             ; Ink counter + 1
  19369.  
  19370. ;; TVT_ROW
  19371. L3C34:  LD      A,B             ; Calculate the paper colour
  19372.         DEC     A               ; Bits 3-5 of each screen attribute
  19373.                 DB 0XCB
  19374.                 RLA               ; holds the paper colour; bits 0-2
  19375.                 DB 0XCB
  19376.                 RLA               ; the ink colour
  19377.                 DB 0XCB
  19378.                 RLA
  19379.         ADD     A,D             ; Add the ink colour
  19380.         DEC     A
  19381.         LD      ($5C8F),A       ; [ATTR_T] Store as temporary attribute value
  19382.  
  19383.         LD      HL,L3C8F        ; TVT_DATA Point to the 'year' data
  19384.         LD      C,E             ; Get number of characters to print
  19385.  
  19386. ;; TVT_YEAR
  19387. L3C45:  LD      A,(HL)          ; Fetch a character from the data
  19388.         RST     10H             ; Print it
  19389.         INC     HL
  19390.         DEC     C
  19391.         JR      NZ,L3C45        ; Repeat for the 8 characters
  19392.  
  19393.         DJNZ    L3C34           ; Repeat for all colours in this row
  19394.  
  19395.         LD      B,E             ; Reset paper colour
  19396.         DEC     D               ; Next ink colour
  19397.         JR      NZ,L3C34        ; Produce next row with new ink colour
  19398.  
  19399.         LD      HL,$4800        ; Point to 2nd third of display file
  19400.         LD      D,H
  19401.         LD      E,L
  19402.         INC     DE              ; Point to the next display cell
  19403.         XOR     A
  19404.         LD      (HL),A          ; Clear first display cell
  19405.         LD      BC,$0FFF
  19406.         LDIR                    ; Clear lower 2 thirds of display file
  19407.  
  19408.         EX      DE,HL           ; HL points to start of attributes file
  19409.         LD      DE,$5900        ; Point to 2nd third of attributes file
  19410.         LD      BC,$0200
  19411.         LDIR                    ; Copy screen attributes
  19412.  
  19413. ; Now that the display has been constructed, produce a continuous cycle of a 440Hz tone for 1 second followed by a period of silence for 1 second (actually 962ms).
  19414.  
  19415.         DI                      ; Disable interrupts so that a pure tone can be generated
  19416.  
  19417. ;; TVT_TONE
  19418. L3C68:  LD      DE,$0370        ; DE=twice the tone frequency in Hz
  19419.         LD      L,$07           ; Border colour of white
  19420.  
  19421. ;; TVT_DURATION
  19422. L3C6D:  LD      BC,$0099        ; Delay for 950.4us
  19423.  
  19424. ;; TVT_PERIOD
  19425. L3C70:  DEC     BC
  19426.         LD      A,B
  19427.         OR      C
  19428.         JR      NZ,L3C70
  19429.  
  19430.         LD      A,L
  19431.         XOR     $10              ; Toggle the speaker output whilst
  19432.         LD      L,A              ; preserving the border colour
  19433.         OUT     ($FE),A
  19434.  
  19435.         DEC     DE               ; Generate the tone for 1 second
  19436.         LD      A,D
  19437.         OR      E
  19438.         JR      NZ,L3C6D
  19439.  
  19440. ; At this point the speaker is turned off, so delay for 1 second.
  19441.  
  19442.         LD      BC,$0000         ; Delay for 480.4us
  19443.  
  19444. ;; TVT_DELAY1
  19445. L3C83:  DEC     BC
  19446.         LD      A,B
  19447.         OR      C
  19448.         JR      NZ,L3C83
  19449.  
  19450. ;; TVT_DELAY2
  19451. L3C88:  DEC     BC               ; Delay for 480.4us
  19452.         LD      A,B
  19453.         OR      C
  19454.         JR      NZ,L3C88
  19455.  
  19456.         JR      L3C68            ; Repeat the tone cycle
  19457.  
  19458. ;; TVT_DATA
  19459. L3C8F:  DB    $13, $00         ; Bright, off
  19460.         DB    $31, $39         ; '1', '9'
  19461.         DB    $13, $01         ; Bright, on
  19462.         DB    $38, $36         ; '8', '6'
  19463.  
  19464.                 include new_proc.a80
  19465.  
  19466. ; ------
  19467. ; UNUSED
  19468. ; ------
  19469.  
  19470.                 DUPL 0X3D00-$,0
  19471.  
  19472. ; -------------------------------
  19473. ; THE 'ZX SPECTRUM CHARACTER SET'
  19474. ; -------------------------------
  19475.  
  19476. ;; char-set
  19477.  
  19478. ; $20 - Character: ' '          CHR$(32)
  19479.  
  19480. CHARS           binclude shr_3d00.bin
  19481.