Subversion Repositories pentevo

Rev

Rev 385 | Rev 516 | 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.                 RET
  104.  
  105.                 DUPL 0X0018-$,0XFF
  106.  
  107. ;        DB    $FF             ; this byte is used by the SPECTRUM command in
  108.                                 ; ROM 0 to generate an error report "0 OK".
  109. ;        DB    $FF, $FF        ; four unused locations.
  110. ;        DB    $FF, $FF        ;
  111.  
  112. ; -------------------------------
  113. ; THE 'COLLECT CHARACTER' RESTART
  114. ; -------------------------------
  115. ; The contents of the location currently addressed by CH_ADD are fetched.
  116. ; A return is made if the value represents a character that has
  117. ; relevance to the BASIC parser. Otherwise CH_ADD is incremented and the
  118. ; tests repeated. CH_ADD will be addressing somewhere -
  119. ; 1) in the BASIC program area during line execution.
  120. ; 2) in workspace if evaluating, for example, a string expression.
  121. ; 3) in the edit buffer if parsing a direct command or a new BASIC line.
  122. ; 4) in workspace if accepting input but not that from INPUT LINE.
  123.  
  124. ;; GET-CHAR
  125. L0018:  LD      HL,($5C5D)      ; fetch the address from CH_ADD.
  126.         LD      A,(HL)          ; use it to pick up current character.
  127.  
  128. ;; TEST-CHAR
  129. L001C:  CALL    L007D           ; routine SKIP-OVER tests if the character
  130.         RET     NC              ; is relevant. Return if it is so.
  131.  
  132. ; ------------------------------------
  133. ; THE 'COLLECT NEXT CHARACTER' RESTART
  134. ; ------------------------------------
  135. ; As the BASIC commands and expressions are interpreted, this routine is
  136. ; called repeatedly to step along the line. It is used 83 times.
  137.  
  138. ;; NEXT-CHAR
  139. L0020:  CALL    L0074           ; routine CH-ADD+1 fetches the next immediate
  140.                                 ; character.
  141.         JR      L001C           ; jump back to TEST-CHAR until a valid
  142.                                 ; character is found.
  143.  
  144. ; ---
  145.  
  146.         DB    $FF, $FF, $FF   ; unused
  147.  
  148. ; -----------------------
  149. ; THE 'CALCULATE' RESTART
  150. ; -----------------------
  151. ; This restart enters the Spectrum's internal, floating-point,
  152. ; stack-based, FORTH-like language.
  153. ; It is further used recursively from within the calculator.
  154. ; It is used on 77 occasions.
  155.  
  156. ;; FP-CALC
  157. L0028:  JP      L335B           ; jump forward to the CALCULATE routine.
  158.  
  159. ; ---
  160.  
  161.         DB    $FF, $FF, $FF   ; spare - note that on the ZX81, space being a
  162.         DB    $FF, $FF        ; little cramped, these same locations were
  163.                                 ; used for the five-byte end-calc literal.
  164.  
  165. ; ------------------------------
  166. ; THE 'CREATE BC SPACES' RESTART
  167. ; ------------------------------
  168. ; This restart is used on only 12 occasions to create BC spaces
  169. ; between workspace and the calculator stack.
  170.  
  171. ;; BC-SPACES
  172. L0030:  PUSH    BC              ; save number of spaces.
  173.         LD      HL,($5C61)      ; fetch WORKSP.
  174.         PUSH    HL              ; save address of workspace.
  175.         JP      L169E           ; jump forward to continuation code RESERVE.
  176.  
  177. ; --------------------------------
  178. ; THE 'MASKABLE INTERRUPT' ROUTINE
  179. ; --------------------------------
  180. ; This routine increments the Spectrum's three-byte FRAMES counter
  181. ; fifty times a second (sixty times a second in the USA ).
  182. ; Both this routine and the called KEYBOARD subroutine use
  183. ; the IY register to access system variables and flags so a user-written
  184. ; program must disable interrupts to make use of the IY register.
  185.  
  186. ;; MASK-INT
  187. L0038:  PUSH    AF              ; save the registers.
  188.         PUSH    HL              ; but not IY unfortunately.
  189.         LD      HL,($5C78)      ; fetch two bytes at FRAMES1.
  190.         INC     HL              ; increment lowest two bytes of counter.
  191.         LD      ($5C78),HL      ; place back in FRAMES1.
  192.         LD      A,H             ; test if the result
  193.         OR      L               ; was zero.
  194.         JR      NZ,L0048        ; forward to KEY-INT if not.
  195.  
  196.         INC     (IY+$40)        ; otherwise increment FRAMES3 the third byte.
  197.  
  198. ; now save the rest of the main registers and read and decode the keyboard.
  199.  
  200. ;; KEY-INT
  201. L0048:  PUSH    BC              ; save the other
  202.         PUSH    DE              ; main registers.
  203.  
  204.                 IF BAS48_ONLY
  205.                 CALL L02BF
  206.                 ELSE
  207.                 CALL L386E              ; Spectrum 128 patch: read the keypad and keyboard
  208.                                         ; in the process of reading a key-press.
  209.                 ENDIF
  210.  
  211. L004D:  POP     DE              ;
  212.         POP     BC              ; restore registers.
  213.  
  214.         POP     HL              ;
  215.         POP     AF              ;
  216.         EI                      ; enable interrupts.
  217.         RET                     ; return.
  218.  
  219. ; ---------------------
  220. ; THE 'ERROR-2' ROUTINE
  221. ; ---------------------
  222. ; A continuation of the code at 0008.
  223. ; The error code is stored and after clearing down stacks,
  224. ; an indirect jump is made to MAIN-4, etc. to handle the error.
  225.  
  226. ;; ERROR-2
  227. L0053:  POP     HL              ; drop the return address - the location
  228.                                 ; after the RST 08H instruction.
  229.         LD      L,(HL)          ; fetch the error code that follows.
  230.                                 ; (nice to see this instruction used.)
  231.  
  232. ; Note. this entry point is used when out of memory at REPORT-4.
  233. ; The L register has been loaded with the report code but X-PTR is not
  234. ; updated.
  235.  
  236. ;; ERROR-3
  237. L0055:  LD      (IY+$00),L      ; store it in the system variable ERR_NR.
  238.         LD      SP,($5C3D)      ; ERR_SP points to an error handler on the
  239.                                 ; machine stack. There may be a hierarchy
  240.                                 ; of routines.
  241.                                 ; to MAIN-4 initially at base.
  242.                                 ; or REPORT-G on line entry.
  243.                                 ; or  ED-ERROR when editing.
  244.                                 ; or   ED-FULL during ed-enter.
  245.                                 ; or  IN-VAR-1 during runtime input etc.
  246.  
  247.         JP      L16C5           ; jump to SET-STK to clear the calculator
  248.                                 ; stack and reset MEM to usual place in the
  249.                                 ; systems variables area.
  250.                                 ; and then indirectly to MAIN-4, etc.
  251.  
  252. ; ---
  253.  
  254.         DB    $FF, $FF, $FF   ; unused locations
  255.         DB    $FF, $FF, $FF   ; before the fixed-position
  256.         DB    $FF             ; NMI routine.
  257.  
  258. ; ------------------------------------
  259. ; THE 'NON-MASKABLE INTERRUPT' ROUTINE
  260. ; ------------------------------------
  261. ; There is no NMI switch on the standard Spectrum.
  262. ; When activated, a location in the system variables is tested
  263. ; and if the contents are zero a jump made to that location else
  264. ; a return is made. Perhaps a disabled development feature but
  265. ; if the logic was reversed, no program would be safe from
  266. ; copy-protection and the Spectrum would have had no software base.
  267. ; The location NMIADD was later used by Interface 1 for other purposes.
  268. ; On later Spectrums, and the Brazilian Spectrum, the logic of this
  269. ; routine was reversed.
  270.  
  271. ;===============================
  272.                 IF PRESS_MAGIC
  273.                 RST 0X08
  274.                 DB EXEC_MAGIC
  275.                 RETN
  276.                 NOP
  277.                 ELSE
  278. ;; RESET
  279. L0066           PUSH AF         ; save the
  280.                 PUSH HL         ; registers.
  281.                 LD HL,($5CB0)   ; fetch the system variable NMIADD.
  282.                 ENDIF
  283. ;===============================
  284.  
  285.                 LD A,H          ; test address
  286.                 OR L            ; for zero.
  287. ;               JR NZ,L0070     ; skip to NO-RESET if NOT ZERO
  288.                 JR Z,L0070
  289.                 JP (HL)         ; jump to routine ( i.e. L0000 )
  290.  
  291. ;; NO-RESET
  292. L0070           POP HL          ; restore the
  293.                 POP AF          ; registers.
  294.                 RETN            ; return to previous interrupt state.
  295.  
  296. ; ---------------------------
  297. ; THE 'CH ADD + 1' SUBROUTINE
  298. ; ---------------------------
  299. ; This subroutine is called from RST 20, and three times from elsewhere
  300. ; to fetch the next immediate character following the current valid character
  301. ; address and update the associated system variable.
  302. ; The entry point TEMP-PTR1 is used from the SCANNING routine.
  303. ; Both TEMP-PTR1 and TEMP-PTR2 are used by the READ command routine.
  304.  
  305. ;; CH-ADD+1
  306. L0074:  LD      HL,($5C5D)      ; fetch address from CH_ADD.
  307.  
  308. ;; TEMP-PTR1
  309. L0077:  INC     HL              ; increase the character address by one.
  310.  
  311. ;; TEMP-PTR2
  312. L0078:  LD      ($5C5D),HL      ; update CH_ADD with character address.
  313.  
  314. X007B:  LD      A,(HL)          ; load character to A from HL.
  315.         RET                     ; and return.
  316.  
  317. ; --------------------------
  318. ; THE 'SKIP OVER' SUBROUTINE
  319. ; --------------------------
  320. ; This subroutine is called once from RST 18 to skip over white-space and
  321. ; other characters irrelevant to the parsing of a BASIC line etc. .
  322. ; Initially the A register holds the character to be considered
  323. ; and HL holds its address which will not be within quoted text
  324. ; when a BASIC line is parsed.
  325. ; Although the 'tab' and 'at' characters will not appear in a BASIC line,
  326. ; they could be present in a string expression, and in other situations.
  327. ; Note. although white-space is usually placed in a program to indent loops
  328. ; and make it more readable, it can also be used for the opposite effect and
  329. ; spaces may appear in variable names although the parser never sees them.
  330. ; It is this routine that helps make the variables 'Anum bEr5 3BUS' and
  331. ; 'a number 53 bus' appear the same to the parser.
  332.  
  333. ;; SKIP-OVER
  334. L007D:  CP      $21             ; test if higher than space.
  335.         RET     NC              ; return with carry clear if so.
  336.  
  337.         CP      $0D             ; carriage return ?
  338.         RET     Z               ; return also with carry clear if so.
  339.  
  340.                                 ; all other characters have no relevance
  341.                                 ; to the parser and must be returned with
  342.                                 ; carry set.
  343.  
  344.         CP      $10             ; test if 0-15d
  345.         RET     C               ; return, if so, with carry set.
  346.  
  347.         CP      $18             ; test if 24-32d
  348.         CCF                     ; complement carry flag.
  349.         RET     C               ; return with carry set if so.
  350.  
  351.                                 ; now leaves 16d-23d
  352.  
  353.         INC     HL              ; all above have at least one extra character
  354.                                 ; to be stepped over.
  355.  
  356.         CP      $16             ; controls 22d ('at') and 23d ('tab') have two.
  357.         JR      C,L0090         ; forward to SKIPS with ink, paper, flash,
  358.                                 ; bright, inverse or over controls.
  359.                                 ; Note. the high byte of tab is for RS232 only.
  360.                                 ; it has no relevance on this machine.
  361.  
  362.         INC     HL              ; step over the second character of 'at'/'tab'.
  363.  
  364. ;; SKIPS
  365. L0090:  SCF                     ; set the carry flag
  366.         LD      ($5C5D),HL      ; update the CH_ADD system variable.
  367.         RET                     ; return with carry set.
  368.  
  369.  
  370. ; ------------------
  371. ; THE 'TOKEN TABLES'
  372. ; ------------------
  373. ; The tokenized characters 134d (RND) to 255d (COPY) are expanded using
  374. ; this table. The last byte of a token is inverted to denote the end of
  375. ; the word. The first is an inverted step-over byte.
  376.  
  377. ;; TKN-TABLE
  378. L0095           DC "?"          ;DB    '?'+$80
  379.                 DC "RND"        ;DEFM    "RN"
  380.                                 ;DB    'D'+$80
  381.                 DC "INKEY$"     ;DEFM    "INKEY"
  382.                                 ;DB    '$'+$80
  383.                 DC "PI"         ;DB    'P','I'+$80
  384.                 DC "FN"         ;DB    'F','N'+$80
  385.                 DC "POINT"      ;DEFM    "POIN"
  386.                                 ;DB    'T'+$80
  387.                 DC "SCREEN$"    ;DEFM    "SCREEN"
  388.                                 ;DB    '$'+$80
  389.                 DC "ATTR"       ;DEFM    "ATT"
  390.                                 ;DB    'R'+$80
  391.                 DC "AT"         ;DB    'A','T'+$80
  392.                 DC "TAB"        ;DEFM    "TA"
  393.                                 ;DB    'B'+$80
  394.                 DC "VAL$"       ;DEFM    "VAL"
  395.                                 ;DB    '$'+$80
  396.                 DC "CODE"       ;DEFM    "COD"
  397.                                 ;DB    'E'+$80
  398.                 DC "VAL"        ;DEFM    "VA"
  399.                                 ;DB    'L'+$80
  400.                 DC "LEN"        ;DEFM    "LE"
  401.                                 ;DB    'N'+$80
  402.                 DC "SIN"        ;DEFM    "SI"
  403.                                 ;DB    'N'+$80
  404.                 DC "COS"        ;DEFM    "CO"
  405.                                 ;DB    'S'+$80
  406.                 DC "TAN"        ;DEFM    "TA"
  407.                                 ;DB    'N'+$80
  408.                 DC "ASN"        ;DEFM    "AS"
  409.                                 ;DB    'N'+$80
  410.                 DC "ACS"        ;DEFM    "AC"
  411.                                 ;DB    'S'+$80
  412.                 DC "ATN"        ;DEFM    "AT"
  413.                                 ;DB    'N'+$80
  414.                 DC "LN"         ;DB    'L','N'+$80
  415.                 DC "EXP"        ;DEFM    "EX"
  416.                                 ;DB    'P'+$80
  417.                 DC "INT"        ;DEFM    "IN"
  418.                                 ;DB    'T'+$80
  419.                 DC "SQR"        ;DEFM    "SQ"
  420.                                 ;DB    'R'+$80
  421.                 DC "SGN"        ;DEFM    "SG"
  422.                                 ;DB    'N'+$80
  423.                 DC "ABS"        ;DEFM    "AB"
  424.                                 ;DB    'S'+$80
  425.                 DC "PEEK"       ;DEFM    "PEE"
  426.                                 ;DB    'K'+$80
  427.                 DC "IN"         ;DB    'I','N'+$80
  428.                 DC "USR"        ;DEFM    "US"
  429.                                 ;DB    'R'+$80
  430.                 DC "STR$"       ;DEFM    "STR"
  431.                                 ;DB    '$'+$80
  432.                 DC "CHR$"       ;DEFM    "CHR"
  433.                                 ;DB    '$'+$80
  434.                 DC "NOT"        ;DEFM    "NO"
  435.                                 ;DB    'T'+$80
  436.                 DC "BIN"        ;DEFM    "BI"
  437.                                 ;DB    'N'+$80
  438.  
  439. ;   The previous 32 function-type words are printed without a leading space
  440. ;   The following have a leading space if they begin with a letter
  441.  
  442.                 DC "OR"         ;DB    'O','R'+$80
  443.                 DC "AND"        ;DEFM    "AN"
  444.                                 ;DB    'D'+$80
  445.                 DC "<="         ;DB    $3C,'='+$80             ; <=
  446.                 DC ">="         ;DB    $3E,'='+$80             ; >=
  447.                 DC "<>"         ;DB    $3C,$3E+$80             ; <>
  448.                 DC "LINE"       ;DEFM    "LIN"
  449.                                 ;DB    'E'+$80
  450.                 DC "THEN"       ;DEFM    "THE"
  451.                                 ;DB    'N'+$80
  452.                 DC "TO"         ;DB    'T','O'+$80
  453.                 DC "STEP"       ;DEFM    "STE"
  454.                                 ;DB    'P'+$80
  455.                 DC "DEF FN"     ;DEFM    "DEF F"
  456.                                 ;DB    'N'+$80
  457.                 DC "CAT"        ;DEFM    "CA"
  458.                                 ;DB    'T'+$80
  459.                 DC "FORMAT"     ;DEFM    "FORMA"
  460.                                 ;DB    'T'+$80
  461.                 DC "MOVE"       ;DEFM    "MOV"
  462.                                 ;DB    'E'+$80
  463.                 DC "ERASE"      ;DEFM    "ERAS"
  464.                                 ;DB    'E'+$80
  465.                 DC "OPEN #"     ;DEFM    "OPEN "
  466.                                 ;DB    '#'+$80
  467.                 DC "CLOSE #"    ;DEFM    "CLOSE "
  468.                                 ;DB    '#'+$80
  469.                 DC "MERGE"      ;DEFM    "MERG"
  470.                                 ;DB    'E'+$80
  471.                 DC "VERIFY"     ;DEFM    "VERIF"
  472.                                 ;DB    'Y'+$80
  473.                 DC "BEEP"       ;DEFM    "BEE"
  474.                                 ;DB    'P'+$80
  475.                 DC "CIRCLE"     ;DEFM    "CIRCL"
  476.                                 ;DB    'E'+$80
  477.                 DC "INK"        ;DEFM    "IN"
  478.                                 ;DB    'K'+$80
  479.                 DC "PAPER"      ;DEFM    "PAPE"
  480.                                 ;DB    'R'+$80
  481.                 DC "FLASH"      ;DEFM    "FLAS"
  482.                                 ;DB    'H'+$80
  483.                 DC "BRIGHT"     ;DEFM    "BRIGH"
  484.                                 ;DB    'T'+$80
  485.                 DC "INVERSE"    ;DEFM    "INVERS"
  486.                                 ;DB    'E'+$80
  487.                 DC "OVER"       ;DEFM    "OVE"
  488.                                 ;DB    'R'+$80
  489.                 DC "OUT"        ;DEFM    "OU"
  490.                                 ;DB    'T'+$80
  491.                 DC "LPRINT"     ;DEFM    "LPRIN"
  492.                                 ;DB    'T'+$80
  493.                 DC "LLIST"      ;DEFM    "LLIS"
  494.                                 ;DB    'T'+$80
  495.                 DC "STOP"       ;DEFM    "STO"
  496.                                 ;DB    'P'+$80
  497.                 DC "READ"       ;DEFM    "REA"
  498.                                 ;DB    'D'+$80
  499.                 DC "DATA"       ;DEFM    "DAT"
  500.                                 ;DB    'A'+$80
  501.                 DC "RESTORE"    ;DEFM    "RESTOR"
  502.                                 ;DB    'E'+$80
  503.                 DC "NEW"        ;DEFM    "NE"
  504.                                 ;DB    'W'+$80
  505.                 DC "BORDER"     ;DEFM    "BORDE"
  506.                                 ;DB    'R'+$80
  507.                 DC "CONTINUE"   ;DEFM    "CONTINU"
  508.                                 ;DB    'E'+$80
  509.                 DC "DIM"        ;DEFM    "DI"
  510.                                 ;DB    'M'+$80
  511.                 DC "REM"        ;DEFM    "RE"
  512.                                 ;DB    'M'+$80
  513.                 DC "FOR"        ;DEFM    "FO"
  514.                                 ;DB    'R'+$80
  515.                 DC "GO TO"      ;DEFM    "GO T"
  516.                                 ;DB    'O'+$80
  517.                 DC "GO SUB"     ;DEFM    "GO SU"
  518.                                 ;DB    'B'+$80
  519.                 DC "INPUT"      ;DEFM    "INPU"
  520.                                 ;DB    'T'+$80
  521.                 DC "LOAD"       ;DEFM    "LOA"
  522.                                 ;DB    'D'+$80
  523.                 DC "LIST"       ;DEFM    "LIS"
  524.                                 ;DB    'T'+$80
  525.                 DC "LET"        ;DEFM    "LE"
  526.                                 ;DB    'T'+$80
  527.                 DC "PAUSE"      ;DEFM    "PAUS"
  528.                                 ;DB    'E'+$80
  529.                 DC "NEXT"       ;DEFM    "NEX"
  530.                                 ;DB    'T'+$80
  531.                 DC "POKE"       ;DEFM    "POK"
  532.                                 ;DB    'E'+$80
  533.                 DC "PRINT"      ;DEFM    "PRIN"
  534.                                 ;DB    'T'+$80
  535.                 DC "PLOT"       ;DEFM    "PLO"
  536.                                 ;DB    'T'+$80
  537.                 DC "RUN"        ;DEFM    "RU"
  538.                                 ;DB    'N'+$80
  539.                 DC "SAVE"       ;DEFM    "SAV"
  540.                                 ;DB    'E'+$80
  541.                 DC "RANDOMIZE"  ;DEFM    "RANDOMIZ"
  542.                                 ;DB    'E'+$80
  543.                 DC "IF"         ;DB    'I','F'+$80
  544.                 DC "CLS"        ;DEFM    "CL"
  545.                                 ;DB    'S'+$80
  546.                 DC "DRAW"       ;DEFM    "DRA"
  547.                                 ;DB    'W'+$80
  548.                 DC "CLEAR"      ;DEFM    "CLEA"
  549.                                 ;DB    'R'+$80
  550.                 DC "RETURN"     ;DEFM    "RETUR"
  551.                                 ;DB    'N'+$80
  552.                 DC "COPY"       ;DEFM    "COP"
  553.                                 ;DB    'Y'+$80
  554.  
  555. ; ----------------
  556. ; THE 'KEY' TABLES
  557. ; ----------------
  558. ; These six look-up tables are used by the keyboard reading routine
  559. ; to decode the key values.
  560.  
  561. ; The first table contains the maps for the 39 keys of the standard
  562. ; 40-key Spectrum keyboard. The remaining key [SHIFT $27] is read directly.
  563. ; The keys consist of the 26 upper-case alphabetic characters, the 10 digit
  564. ; keys and the space, ENTER and symbol shift key.
  565. ; Unshifted alphabetic keys have $20 added to the value.
  566. ; The keywords for the main alphabetic keys are obtained by adding $A5 to
  567. ; the values obtained from this table.
  568.  
  569. ;; MAIN-KEYS
  570. L0205:  DB    $42             ; B
  571.         DB    $48             ; H
  572.         DB    $59             ; Y
  573.         DB    $36             ; 6
  574.         DB    $35             ; 5
  575.         DB    $54             ; T
  576.         DB    $47             ; G
  577.         DB    $56             ; V
  578.         DB    $4E             ; N
  579.         DB    $4A             ; J
  580.         DB    $55             ; U
  581.         DB    $37             ; 7
  582.         DB    $34             ; 4
  583.         DB    $52             ; R
  584.         DB    $46             ; F
  585.         DB    $43             ; C
  586.         DB    $4D             ; M
  587.         DB    $4B             ; K
  588.         DB    $49             ; I
  589.         DB    $38             ; 8
  590.         DB    $33             ; 3
  591.         DB    $45             ; E
  592.         DB    $44             ; D
  593.         DB    $58             ; X
  594.         DB    $0E             ; SYMBOL SHIFT
  595.         DB    $4C             ; L
  596.         DB    $4F             ; O
  597.         DB    $39             ; 9
  598.         DB    $32             ; 2
  599.         DB    $57             ; W
  600.         DB    $53             ; S
  601.         DB    $5A             ; Z
  602.         DB    $20             ; SPACE
  603.         DB    $0D             ; ENTER
  604.         DB    $50             ; P
  605.         DB    $30             ; 0
  606.         DB    $31             ; 1
  607.         DB    $51             ; Q
  608.         DB    $41             ; A
  609.  
  610.  
  611. ;; E-UNSHIFT
  612. ;  The 26 unshifted extended mode keys for the alphabetic characters.
  613. ;  The green keywords on the original keyboard.
  614. L022C:  DB    $E3             ; READ
  615.         DB    $C4             ; BIN
  616.         DB    $E0             ; LPRINT
  617.         DB    $E4             ; DATA
  618.         DB    $B4             ; TAN
  619.         DB    $BC             ; SGN
  620.         DB    $BD             ; ABS
  621.         DB    $BB             ; SQR
  622.         DB    $AF             ; CODE
  623.         DB    $B0             ; VAL
  624.         DB    $B1             ; LEN
  625.         DB    $C0             ; USR
  626.         DB    $A7             ; PI
  627.         DB    $A6             ; INKEY$
  628.         DB    $BE             ; PEEK
  629.         DB    $AD             ; TAB
  630.         DB    $B2             ; SIN
  631.         DB    $BA             ; INT
  632.         DB    $E5             ; RESTORE
  633.         DB    $A5             ; RND
  634.         DB    $C2             ; CHR$
  635.         DB    $E1             ; LLIST
  636.         DB    $B3             ; COS
  637.         DB    $B9             ; EXP
  638.         DB    $C1             ; STR$
  639.         DB    $B8             ; LN
  640.  
  641.  
  642. ;; EXT-SHIFT
  643. ;  The 26 shifted extended mode keys for the alphabetic characters.
  644. ;  The red keywords below keys on the original keyboard.
  645. L0246:  DB    $7E             ; ~
  646.         DB    $DC             ; BRIGHT
  647.         DB    $DA             ; PAPER
  648.         DB    $5C             ;
  649.         DB    $B7             ; ATN
  650.         DB    $7B             ; {
  651.         DB    $7D             ; }
  652.         DB    $D8             ; CIRCLE
  653.         DB    $BF             ; IN
  654.         DB    $AE             ; VAL$
  655.         DB    $AA             ; SCREEN$
  656.         DB    $AB             ; ATTR
  657.         DB    $DD             ; INVERSE
  658.         DB    $DE             ; OVER
  659.         DB    $DF             ; OUT
  660.         DB    $7F             ; (Copyright character)
  661.         DB    $B5             ; ASN
  662.         DB    $D6             ; VERIFY
  663.         DB    $7C             ; |
  664.         DB    $D5             ; MERGE
  665.         DB    $5D             ; ]
  666.         DB    $DB             ; FLASH
  667.         DB    $B6             ; ACS
  668.         DB    $D9             ; INK
  669.         DB    $5B             ; [
  670.         DB    $D7             ; BEEP
  671.  
  672.  
  673. ;; CTL-CODES
  674. ;  The ten control codes assigned to the top line of digits when the shift
  675. ;  key is pressed.
  676. L0260:  DB    $0C             ; DELETE
  677.         DB    $07             ; EDIT
  678.         DB    $06             ; CAPS LOCK
  679.         DB    $04             ; TRUE VIDEO
  680.         DB    $05             ; INVERSE VIDEO
  681.         DB    $08             ; CURSOR LEFT
  682.         DB    $0A             ; CURSOR DOWN
  683.         DB    $0B             ; CURSOR UP
  684.         DB    $09             ; CURSOR RIGHT
  685.         DB    $0F             ; GRAPHICS
  686.  
  687.  
  688. ;; SYM-CODES
  689. ;  The 26 red symbols assigned to the alphabetic characters of the keyboard.
  690. ;  The ten single-character digit symbols are converted without the aid of
  691. ;  a table using subtraction and minor manipulation.
  692. L026A:  DB    $E2             ; STOP
  693.         DB    $2A             ; *
  694.         DB    $3F             ; ?
  695.         DB    $CD             ; STEP
  696.         DB    $C8             ; >=
  697.         DB    $CC             ; TO
  698.         DB    $CB             ; THEN
  699.         DB    $5E             ; ^
  700.         DB    $AC             ; AT
  701.         DB    $2D             ; -
  702.         DB    $2B             ; +
  703.         DB    $3D             ; =
  704.         DB    $2E             ; .
  705.         DB    $2C             ; ,
  706.         DB    $3B             ; ;
  707.         DB    $22             ; "
  708.         DB    $C7             ; <=
  709.         DB    $3C             ; <
  710.         DB    $C3             ; NOT
  711.         DB    $3E             ; >
  712.         DB    $C5             ; OR
  713.         DB    $2F             ; /
  714.         DB    $C9             ; <>
  715.         DB    $60             ; pound
  716.         DB    $C6             ; AND
  717.         DB    $3A             ; :
  718.  
  719. ;; E-DIGITS
  720. ;  The ten keywords assigned to the digits in extended mode.
  721. ;  The remaining red keywords below the keys.
  722. L0284:  DB    $D0             ; FORMAT
  723.         DB    $CE             ; DEF FN
  724.         DB    $A8             ; FN
  725.         DB    $CA             ; LINE
  726.         DB    $D3             ; OPEN#
  727.         DB    $D4             ; CLOSE#
  728.         DB    $D1             ; MOVE
  729.         DB    $D2             ; ERASE
  730.         DB    $A9             ; POINT
  731.         DB    $CF             ; CAT
  732.  
  733.  
  734. ;*******************************
  735. ;** Part 2. KEYBOARD ROUTINES **
  736. ;*******************************
  737.  
  738. ; Using shift keys and a combination of modes the Spectrum 40-key keyboard
  739. ; can be mapped to 256 input characters
  740.  
  741. ; ---------------------------------------------------------------------------
  742. ;
  743. ;         0     1     2     3     4 -Bits-  4     3     2     1     0
  744. ; PORT                                                                    PORT
  745. ;
  746. ; F7FE  [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]  |  [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 0 ]   EFFE
  747. ;  ^                                   |                                   v
  748. ; FBFE  [ Q ] [ W ] [ E ] [ R ] [ T ]  |  [ Y ] [ U ] [ I ] [ O ] [ P ]   DFFE
  749. ;  ^                                   |                                   v
  750. ; FDFE  [ A ] [ S ] [ D ] [ F ] [ G ]  |  [ H ] [ J ] [ K ] [ L ] [ ENT ] BFFE
  751. ;  ^                                   |                                   v
  752. ; FEFE  [SHI] [ Z ] [ X ] [ C ] [ V ]  |  [ B ] [ N ] [ M ] [sym] [ SPC ] 7FFE
  753. ;  ^     $27                                                 $18           v
  754. ; Start                                                                   End
  755. ;        00100111                                            00011000
  756. ;
  757. ; ---------------------------------------------------------------------------
  758. ; The above map may help in reading.
  759. ; The neat arrangement of ports means that the B register need only be
  760. ; rotated left to work up the left hand side and then down the right
  761. ; hand side of the keyboard. When the reset bit drops into the carry
  762. ; then all 8 half-rows have been read. Shift is the first key to be
  763. ; read. The lower six bits of the shifts are unambiguous.
  764.  
  765. ; -------------------------------
  766. ; THE 'KEYBOARD SCANNING' ROUTINE
  767. ; -------------------------------
  768. ; from keyboard and s-inkey$
  769. ; returns 1 or 2 keys in DE, most significant shift first if any
  770. ; key values 0-39 else 255
  771.  
  772. ;; KEY-SCAN
  773. L028E:  LD      L,$2F           ; initial key value
  774.                                 ; valid values are obtained by subtracting
  775.                                 ; eight five times.
  776.         LD      DE,$FFFF        ; a buffer to receive 2 keys.
  777.  
  778.         LD      BC,$FEFE        ; the commencing port address
  779.                                 ; B holds 11111110 initially and is also
  780.                                 ; used to count the 8 half-rows
  781. ;; KEY-LINE
  782. L0296:  IN      A,(C)           ; read the port to A - bits will be reset
  783.                                 ; if a key is pressed else set.
  784.         CPL                     ; complement - pressed key-bits are now set
  785.         AND     $1F             ; apply 00011111 mask to pick up the
  786.                                 ; relevant set bits.
  787.  
  788.         JR      Z,L02AB         ; forward to KEY-DONE if zero and therefore
  789.                                 ; no keys pressed in row at all.
  790.  
  791.         LD      H,A             ; transfer row bits to H
  792.         LD      A,L             ; load the initial key value to A
  793.  
  794. ;; KEY-3KEYS
  795. L029F:  INC     D               ; now test the key buffer
  796.         RET     NZ              ; if we have collected 2 keys already
  797.                                 ; then too many so quit.
  798.  
  799. ;; KEY-BITS
  800. L02A1:  SUB     $08             ; subtract 8 from the key value
  801.                                 ; cycling through key values (top = $27)
  802.                                 ; e.g. 2F>   27>1F>17>0F>07
  803.                                 ;      2E>   26>1E>16>0E>06
  804.         SRL     H               ; shift key bits right into carry.
  805.         JR      NC,L02A1        ; back to KEY-BITS if not pressed
  806.                                 ; but if pressed we have a value (0-39d)
  807.  
  808.         LD      D,E             ; transfer a possible previous key to D
  809.         LD      E,A             ; transfer the new key to E
  810.         JR      NZ,L029F        ; back to KEY-3KEYS if there were more
  811.                                 ; set bits - H was not yet zero.
  812.  
  813. ;; KEY-DONE
  814. L02AB:  DEC     L               ; cycles 2F>2E>2D>2C>2B>2A>29>28 for
  815.                                 ; each half-row.
  816.         RLC     B               ; form next port address e.g. FEFE > FDFE
  817.         JR      C,L0296         ; back to KEY-LINE if still more rows to do.
  818.  
  819.         LD      A,D             ; now test if D is still FF ?
  820.         INC     A               ; if it is zero we have at most 1 key
  821.                                 ; range now $01-$28  (1-40d)
  822.         RET     Z               ; return if one key or no key.
  823.  
  824.         CP      $28             ; is it capsshift (was $27) ?
  825.         RET     Z               ; return if so.
  826.  
  827.         CP      $19             ; is it symbol shift (was $18) ?
  828.         RET     Z               ; return also
  829.  
  830.         LD      A,E             ; now test E
  831.         LD      E,D             ; but first switch
  832.         LD      D,A             ; the two keys.
  833.         CP      $18             ; is it symbol shift ?
  834.         RET                     ; return (with zero set if it was).
  835.                                 ; but with symbol shift now in D
  836.  
  837. ; ------------------------------
  838. ; Scan keyboard and decode value
  839. ; ------------------------------
  840. ; from interrupt 50 times a second
  841. ;
  842.  
  843. ;; KEYBOARD
  844. L02BF:  CALL    L028E           ; routine KEY-SCAN
  845.         RET     NZ              ; return if invalid combinations
  846.  
  847. ; then decrease the counters within the two key-state maps
  848. ; as this could cause one to become free.
  849. ; if the keyboard has not been pressed during the last five interrupts
  850. ; then both sets will be free.
  851.  
  852.  
  853.         LD      HL,$5C00        ; point to KSTATE-0
  854.  
  855. ;; K-ST-LOOP
  856. L02C6:  BIT     7,(HL)          ; is it free ?  ($FF)
  857.         JR      NZ,L02D1        ; forward to K-CH-SET if so
  858.  
  859.         INC     HL              ; address 5-counter
  860.         DEC     (HL)            ; decrease counter
  861.         DEC     HL              ; step back
  862.         JR      NZ,L02D1        ; forward to K-CH-SET if not at end of count
  863.  
  864.         LD      (HL),$FF        ; else mark it free.
  865.  
  866. ;; K-CH-SET
  867. L02D1:  LD      A,L             ; store low address byte.
  868.         LD      HL,$5C04        ; point to KSTATE-4
  869.                                 ; (ld l, $04)
  870.         CP      L               ; have 2 been done ?
  871.         JR      NZ,L02C6        ; back to K-ST-LOOP to consider this 2nd set
  872.  
  873. ; now the raw key (0-38) is converted to a main key (uppercase).
  874.  
  875.         CALL    L031E           ; routine K-TEST to get main key in A
  876.         RET     NC              ; return if single shift
  877.  
  878.         LD      HL,$5C00        ; point to KSTATE-0
  879.         CP      (HL)            ; does it match ?
  880.         JR      Z,L0310         ; forward to K-REPEAT if so
  881.  
  882. ; if not consider the second key map.
  883.  
  884.         EX      DE,HL           ; save kstate-0 in de
  885.         LD      HL,$5C04        ; point to KSTATE-4
  886.         CP      (HL)            ; does it match ?
  887.         JR      Z,L0310         ; forward to K-REPEAT if so
  888.  
  889. ; having excluded a repeating key we can now consider a new key.
  890. ; the second set is always examined before the first.
  891.  
  892.         BIT     7,(HL)          ; is it free ?
  893.         JR      NZ,L02F1        ; forward to K-NEW if so.
  894.  
  895.         EX      DE,HL           ; bring back kstate-0
  896.         BIT     7,(HL)          ; is it free ?
  897.         RET     Z               ; return if not.
  898.                                 ; as we have a key but nowhere to put it yet.
  899.  
  900. ; continue or jump to here if one of the buffers was free.
  901.  
  902. ;; K-NEW
  903. L02F1:  LD      E,A             ; store key in E
  904.         LD      (HL),A          ; place in free location
  905.         INC     HL              ; advance to interrupt counter
  906.         LD      (HL),$05        ; and initialize to 5
  907.         INC     HL              ; advance to delay
  908.         LD      A,($5C09)       ; pick up system variable REPDEL
  909.         LD      (HL),A          ; and insert that for first repeat delay.
  910.         INC     HL              ; advance to last location of state map.
  911.  
  912.         LD      C,(IY+$07)      ; pick up MODE  (3 bytes)
  913.         LD      D,(IY+$01)      ; pick up FLAGS (3 bytes)
  914.         PUSH    HL              ; save state map location
  915.                                 ; Note. could now have used.
  916.                                 ; ld l,$41; ld c,(hl); ld l,$3B; ld d,(hl).
  917.                                 ; six and two threes of course.
  918.         CALL    L0333           ; routine K-DECODE
  919.         POP     HL              ; restore map pointer
  920.         LD      (HL),A          ; put decoded key in last location of map.
  921.  
  922. ;; K-END
  923. L0308:  LD      ($5C08),A       ; update LASTK system variable.
  924.         SET     5,(IY+$01)      ; update FLAGS  - signal new key.
  925.         RET                     ; done
  926.  
  927. ; ---------------------------
  928. ; THE 'REPEAT KEY' SUBROUTINE
  929. ; ---------------------------
  930. ; A possible repeat has been identified. HL addresses the raw (main) key.
  931. ; The last location holds the decoded key (from the first context).
  932.  
  933. ;; K-REPEAT
  934. L0310:  INC     HL              ; advance
  935.         LD      (HL),$05        ; maintain interrupt counter at 5
  936.         INC     HL              ; advance
  937.         DEC     (HL)            ; decrease REPDEL value.
  938.         RET     NZ              ; return if not yet zero.
  939.  
  940.         LD      A,($5C0A)       ; REPPER
  941.         LD      (HL),A          ; but for subsequent repeats REPPER will be used.
  942.         INC     HL              ; advance
  943.                                 ;
  944.         LD      A,(HL)          ; pick up the key decoded possibly in another
  945.                                 ; context.
  946.         JR      L0308           ; back to K-END
  947.  
  948. ; --------------
  949. ; Test key value
  950. ; --------------
  951. ; also called from s-inkey$
  952. ; begin by testing for a shift with no other.
  953.  
  954. ;; K-TEST
  955. L031E:  LD      B,D             ; load most significant key to B
  956.                                 ; will be $FF if not shift.
  957.         LD      D,$00           ; and reset D to index into main table
  958.         LD      A,E             ; load least significant key from E
  959.         CP      $27             ; is it higher than 39d   i.e. FF
  960.         RET     NC              ; return with just a shift (in B now)
  961.  
  962.         CP      $18             ; is it symbol shift ?
  963.         JR      NZ,L032C        ; forward to K-MAIN if not
  964.  
  965. ; but we could have just symbol shift and no other
  966.  
  967.         BIT     7,B             ; is other key $FF (ie not shift)
  968.         RET     NZ              ; return with solitary symbol shift
  969.  
  970.  
  971. ;; K-MAIN
  972. L032C:  LD      HL,L0205        ; address: MAIN-KEYS
  973.         ADD     HL,DE           ; add offset 0-38
  974.         LD      A,(HL)          ; pick up main key value
  975.         SCF                     ; set carry flag
  976.         RET                     ; return    (B has other key still)
  977.  
  978. ; -----------------
  979. ; Keyboard decoding
  980. ; -----------------
  981. ; also called from s-inkey$
  982.  
  983. ;; K-DECODE
  984. L0333:  LD      A,E             ; pick up the stored main key
  985.         CP      $3A             ; an arbitrary point between digits and letters
  986.         JR      C,L0367         ; forward to K-DIGIT with digits, space, enter.
  987.  
  988.         DEC     C               ; decrease MODE ( 0='KLC', 1='E', 2='G')
  989.  
  990.         JP      M,L034F         ; to K-KLC-LET if was zero
  991.  
  992.         JR      Z,L0341         ; to K-E-LET if was 1 for extended letters.
  993.  
  994. ; proceed with graphic codes.
  995. ; Note. should selectively drop return address if code > 'U' ($55).
  996. ; i.e. abort the KEYBOARD call.
  997. ; e.g. cp 'V'; jr c addit; pop af; ;;addit etc. (5 bytes of instruction).
  998. ; (s-inkey$ never gets into graphics mode.)
  999.  
  1000. ;; addit
  1001.         ADD     A,$4F           ; add offset to augment 'A' to graphics A say.
  1002.         RET                     ; return.
  1003.                                 ; Note. ( but [GRAPH] V gives RND, etc ).
  1004.  
  1005. ; ---
  1006.  
  1007. ; the jump was to here with extended mode with uppercase A-Z.
  1008.  
  1009. ;; K-E-LET
  1010. L0341:  LD      HL,L022C-$41    ; base address of E-UNSHIFT L022c
  1011.                                 ; ( $01EB in standard ROM )
  1012.         INC     B               ; test B is it empty i.e. not a shift
  1013.         JR      Z,L034A         ; forward to K-LOOK-UP if neither shift
  1014.  
  1015.         LD      HL,L0246-$41    ; Address: $0205 L0246-$41 EXT-SHIFT base
  1016.  
  1017. ;; K-LOOK-UP
  1018. L034A:  LD      D,$00           ; prepare to index
  1019.         ADD     HL,DE           ; add the main key value
  1020.         LD      A,(HL)          ; pick up other mode value
  1021.         RET                     ; return
  1022.  
  1023. ; ---
  1024.  
  1025. ; the jump was here with mode = 0
  1026.  
  1027. ;; K-KLC-LET
  1028. L034F:  LD      HL,L026A-$41    ; prepare base of sym-codes
  1029.         BIT     0,B             ; shift=$27 sym-shift=$18
  1030.         JR      Z,L034A         ; back to K-LOOK-UP with symbol-shift
  1031.  
  1032.         BIT     3,D             ; test FLAGS is it 'K' mode (from OUT-CURS)
  1033.         JR      Z,L0364         ; skip to K-TOKENS if so
  1034.  
  1035.         BIT     3,(IY+$30)      ; test FLAGS2 - consider CAPS LOCK ?
  1036.         RET     NZ              ; return if so with main code.
  1037.  
  1038.         INC     B               ; is shift being pressed ?
  1039.                                 ; result zero if not
  1040.         RET     NZ              ; return if shift pressed.
  1041.  
  1042.         ADD     A,$20           ; else convert the code to lower case.
  1043.         RET                     ; return.
  1044.  
  1045. ; ---
  1046.  
  1047. ; the jump was here for tokens
  1048.  
  1049. ;; K-TOKENS
  1050. L0364:  ADD     A,$A5           ; add offset to main code so that 'A'
  1051.                                 ; becomes 'NEW' etc.
  1052.         RET                     ; return
  1053.  
  1054. ; ---
  1055.  
  1056. ; the jump was here with digits, space, enter and symbol shift (< $xx)
  1057.  
  1058. ;; K-DIGIT
  1059. L0367:  CP      $30             ; is it '0' or higher ?
  1060.         RET     C               ; return with space, enter and symbol-shift
  1061.  
  1062.         DEC     C               ; test MODE (was 0='KLC', 1='E', 2='G')
  1063.         JP      M,L039D         ; jump to K-KLC-DGT if was 0.
  1064.  
  1065.         JR      NZ,L0389        ; forward to K-GRA-DGT if mode was 2.
  1066.  
  1067. ; continue with extended digits 0-9.
  1068.  
  1069.         LD      HL,L0284-$30    ; $0254 - base of E-DIGITS
  1070.         BIT     5,B             ; test - shift=$27 sym-shift=$18
  1071.         JR      Z,L034A         ; to K-LOOK-UP if sym-shift
  1072.  
  1073.         CP      $38             ; is character '8' ?
  1074.         JR      NC,L0382        ; to K-8-&-9 if greater than '7'
  1075.  
  1076.         SUB     $20             ; reduce to ink range $10-$17
  1077.         INC     B               ; shift ?
  1078.         RET     Z               ; return if not.
  1079.  
  1080.         ADD     A,$08           ; add 8 to give paper range $18 - $1F
  1081.         RET                     ; return
  1082.  
  1083. ; ---
  1084.  
  1085. ; 89
  1086.  
  1087. ;; K-8-&-9
  1088. L0382:  SUB     $36             ; reduce to 02 and 03  bright codes
  1089.         INC     B               ; test if shift pressed.
  1090.         RET     Z               ; return if not.
  1091.  
  1092.         ADD     A,$FE           ; subtract 2 setting carry
  1093.         RET                     ; to give 0 and 1    flash codes.
  1094.  
  1095. ; ---
  1096.  
  1097. ;  graphics mode with digits
  1098.  
  1099. ;; K-GRA-DGT
  1100. L0389:  LD      HL,L0260-$30    ; $0230 base address of CTL-CODES
  1101.  
  1102.         CP      $39             ; is key '9' ?
  1103.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0F, GRAPHICS.
  1104.  
  1105.         CP      $30             ; is key '0' ?
  1106.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0C, delete.
  1107.  
  1108. ; for keys '0' - '7' we assign a mosaic character depending on shift.
  1109.  
  1110.         AND     $07             ; convert character to number. 0 - 7.
  1111.         ADD     A,$80           ; add offset - they start at $80
  1112.  
  1113.         INC     B               ; destructively test for shift
  1114.         RET     Z               ; and return if not pressed.
  1115.  
  1116.         XOR     $0F             ; toggle bits becomes range $88-$8F
  1117.         RET                     ; return.
  1118.  
  1119. ; ---
  1120.  
  1121. ; now digits in 'KLC' mode
  1122.  
  1123. ;; K-KLC-DGT
  1124. L039D:  INC     B               ; return with digit codes if neither
  1125.         RET     Z               ; shift key pressed.
  1126.  
  1127.         BIT     5,B             ; test for caps shift.
  1128.  
  1129.         LD      HL,L0260-$30    ; prepare base of table CTL-CODES.
  1130.         JR      NZ,L034A        ; back to K-LOOK-UP if shift pressed.
  1131.  
  1132. ; must have been symbol shift
  1133.  
  1134.         SUB     $10             ; for ASCII most will now be correct
  1135.                                 ; on a standard typewriter.
  1136.         CP      $22             ; but '@' is not - see below.
  1137.         JR      Z,L03B2         ; forward to to K-@-CHAR if so
  1138.  
  1139.         CP      $20             ; '_' is the other one that fails
  1140.         RET     NZ              ; return if not.
  1141.  
  1142.         LD      A,$5F           ; substitute ASCII '_'
  1143.         RET                     ; return.
  1144.  
  1145. ; ---
  1146.  
  1147. ;; K-@-CHAR
  1148. L03B2:  LD      A,$40           ; substitute ASCII '@'
  1149.         RET                     ; return.
  1150.  
  1151.  
  1152. ; ------------------------------------------------------------------------
  1153. ; The Spectrum Input character keys. One or two are abbreviated.
  1154. ; From $00 Flash 0 to $FF COPY. The routine above has decoded all these.
  1155.  
  1156. ;  | 00 Fl0| 01 Fl1| 02 Br0| 03 Br1| 04 In0| 05 In1| 06 CAP| 07 EDT|
  1157. ;  | 08 LFT| 09 RIG| 0A DWN| 0B UP | 0C DEL| 0D ENT| 0E SYM| 0F GRA|
  1158. ;  | 10 Ik0| 11 Ik1| 12 Ik2| 13 Ik3| 14 Ik4| 15 Ik5| 16 Ik6| 17 Ik7|
  1159. ;  | 18 Pa0| 19 Pa1| 1A Pa2| 1B Pa3| 1C Pa4| 1D Pa5| 1E Pa6| 1F Pa7|
  1160. ;  | 20 SP | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  ' |
  1161. ;  | 28  ( | 29  ) | 2A  * | 2B  + | 2C  , | 2D  - | 2E  . | 2F  / |
  1162. ;  | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |
  1163. ;  | 38  8 | 39  9 | 3A  : | 3B  ; | 3C  < | 3D  = | 3E  > | 3F  ? |
  1164. ;  | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |
  1165. ;  | 48  H | 49  I | 4A  J | 4B  K | 4C  L | 4D  M | 4E  N | 4F  O |
  1166. ;  | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |
  1167. ;  | 58  X | 59  Y | 5A  Z | 5B  [ | 5C  \ | 5D  ] | 5E  ^ | 5F  _ |
  1168. ;  | 60 ukp| 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |
  1169. ;  | 68  h | 69  i | 6A  j | 6B  k | 6C  l | 6D  m | 6E  n | 6F  o |
  1170. ;  | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |
  1171. ;  | 78  x | 79  y | 7A  z | 7B  { | 7C  | | 7D  } | 7E  ~ | 7F (c)|
  1172. ;  | 80 128| 81 129| 82 130| 83 131| 84 132| 85 133| 86 134| 87 135|
  1173. ;  | 88 136| 89 137| 8A 138| 8B 139| 8C 140| 8D 141| 8E 142| 8F 143|
  1174. ;  | 90 [A]| 91 [B]| 92 [C]| 93 [D]| 94 [E]| 95 [F]| 96 [G]| 97 [H]|
  1175. ;  | 98 [I]| 99 [J]| 9A [K]| 9B [L]| 9C [M]| 9D [N]| 9E [O]| 9F [P]|
  1176. ;  | A0 [Q]| A1 [R]| A2 [S]| A3 [T]| A4 [U]| A5 RND| A6 IK$| A7 PI |
  1177. ;  | A8 FN | A9 PNT| AA SC$| AB ATT| AC AT | AD TAB| AE VL$| AF COD|
  1178. ;  | B0 VAL| B1 LEN| B2 SIN| B3 COS| B4 TAN| B5 ASN| B6 ACS| B7 ATN|
  1179. ;  | B8 LN | B9 EXP| BA INT| BB SQR| BC SGN| BD ABS| BE PEK| BF IN |
  1180. ;  | C0 USR| C1 ST$| C2 CH$| C3 NOT| C4 BIN| C5 OR | C6 AND| C7 <= |
  1181. ;  | C8 >= | C9 <> | CA LIN| CB THN| CC TO | CD STP| CE DEF| CF CAT|
  1182. ;  | D0 FMT| D1 MOV| D2 ERS| D3 OPN| D4 CLO| D5 MRG| D6 VFY| D7 BEP|
  1183. ;  | D8 CIR| D9 INK| DA PAP| DB FLA| DC BRI| DD INV| DE OVR| DF OUT|
  1184. ;  | E0 LPR| E1 LLI| E2 STP| E3 REA| E4 DAT| E5 RES| E6 NEW| E7 BDR|
  1185. ;  | E8 CON| E9 DIM| EA REM| EB FOR| EC GTO| ED GSB| EE INP| EF LOA|
  1186. ;  | F0 LIS| F1 LET| F2 PAU| F3 NXT| F4 POK| F5 PRI| F6 PLO| F7 RUN|
  1187. ;  | F8 SAV| F9 RAN| FA IF | FB CLS| FC DRW| FD CLR| FE RET| FF CPY|
  1188.  
  1189. ; Note that for simplicity, Sinclair have located all the control codes
  1190. ; below the space character.
  1191. ; ASCII DEL, $7F, has been made a copyright symbol.
  1192. ; Also $60, '`', not used in BASIC but used in other languages, has been
  1193. ; allocated the local currency symbol for the relevant country -
  1194. ; ukp in most Spectrums.
  1195.  
  1196. ; ------------------------------------------------------------------------
  1197.  
  1198. ;**********************************
  1199. ;** Part 3. LOUDSPEAKER ROUTINES **
  1200. ;**********************************
  1201.  
  1202.  
  1203. ; Documented by Alvin Albrecht.
  1204.  
  1205.  
  1206. ; ------------------------------
  1207. ; Routine to control loudspeaker
  1208. ; ------------------------------
  1209. ; Outputs a square wave of given duration and frequency
  1210. ; to the loudspeaker.
  1211. ;   Enter with: DE = #cycles - 1
  1212. ;               HL = tone period as described next
  1213. ;
  1214. ; The tone period is measured in T states and consists of
  1215. ; three parts: a coarse part (H register), a medium part
  1216. ; (bits 7..2 of L) and a fine part (bits 1..0 of L) which
  1217. ; contribute to the waveform timing as follows:
  1218. ;
  1219. ;                          coarse    medium       fine
  1220. ; duration of low  = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1221. ; duration of hi   = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1222. ; Tp = tone period = 236 + 2048*H + 32*(L>>2) + 8*(L&0x3)
  1223. ;                  = 236 + 2048*H + 8*L = 236 + 8*HL
  1224. ;
  1225. ; As an example, to output five seconds of middle C (261.624 Hz):
  1226. ;   (a) Tone period = 1/261.624 = 3.822ms
  1227. ;   (b) Tone period in T-States = 3.822ms*fCPU = 13378
  1228. ;         where fCPU = clock frequency of the CPU = 3.5MHz
  1229. ;   (c) Find H and L for desired tone period:
  1230. ;         HL = (Tp - 236) / 8 = (13378 - 236) / 8 = 1643 = 0x066B
  1231. ;   (d) Tone duration in cycles = 5s/3.822ms = 1308 cycles
  1232. ;         DE = 1308 - 1 = 0x051B
  1233. ;
  1234. ; The resulting waveform has a duty ratio of exactly 50%.
  1235. ;
  1236. ;
  1237. ;; BEEPER
  1238. L03B5:  DI                      ; Disable Interrupts so they don't disturb timing
  1239.         LD      A,L             ;
  1240.         SRL     L               ;
  1241.         SRL     L               ; L = medium part of tone period
  1242.         CPL                     ;
  1243.         AND     $03             ; A = 3 - fine part of tone period
  1244.         LD      C,A             ;
  1245.         LD      B,$00           ;
  1246.         LD      IX,L03D1        ; Address: BE-IX+3
  1247.         ADD     IX,BC           ;   IX holds address of entry into the loop
  1248.                                 ;   the loop will contain 0-3 NOPs, implementing
  1249.                                 ;   the fine part of the tone period.
  1250.         LD      A,($5C48)       ; BORDCR
  1251.         AND     $38             ; bits 5..3 contain border colour
  1252.         RRCA                    ; border colour bits moved to 2..0
  1253.         RRCA                    ;   to match border bits on port #FE
  1254.         RRCA                    ;
  1255.         OR       $08            ; bit 3 set (tape output bit on port #FE)
  1256.                                 ;   for loud sound output
  1257. ;; BE-IX+3
  1258. L03D1:  NOP              ;(4)   ; optionally executed NOPs for small
  1259.                                 ;   adjustments to tone period
  1260. ;; BE-IX+2
  1261. L03D2:  NOP              ;(4)   ;
  1262.  
  1263. ;; BE-IX+1
  1264. L03D3:  NOP              ;(4)   ;
  1265.  
  1266. ;; BE-IX+0
  1267. L03D4:  INC     B        ;(4)   ;
  1268.         INC     C        ;(4)   ;
  1269.  
  1270. ;; BE-H&L-LP
  1271. L03D6:  DEC     C        ;(4)   ; timing loop for duration of
  1272.         JR      NZ,L03D6 ;(12/7);   high or low pulse of waveform
  1273.  
  1274.         LD      C,$3F    ;(7)   ;
  1275.         DEC     B        ;(4)   ;
  1276.         JP      NZ,L03D6 ;(10)  ; to BE-H&L-LP
  1277.  
  1278.         XOR     $10      ;(7)   ; toggle output beep bit
  1279.         OUT     ($FE),A  ;(11)  ; output pulse
  1280.         LD      B,H      ;(4)   ; B = coarse part of tone period
  1281.         LD      C,A      ;(4)   ; save port #FE output byte
  1282.         BIT     4,A      ;(8)   ; if new output bit is high, go
  1283.         JR      NZ,L03F2 ;(12/7);   to BE-AGAIN
  1284.  
  1285.         LD      A,D      ;(4)   ; one cycle of waveform has completed
  1286.         OR      E        ;(4)   ;   (low->low). if cycle countdown = 0
  1287.         JR      Z,L03F6  ;(12/7);   go to BE-END
  1288.  
  1289.         LD      A,C      ;(4)   ; restore output byte for port #FE
  1290.         LD      C,L      ;(4)   ; C = medium part of tone period
  1291.         DEC     DE       ;(6)   ; decrement cycle count
  1292.         JP      (IX)     ;(8)   ; do another cycle
  1293.  
  1294. ;; BE-AGAIN                     ; halfway through cycle
  1295. L03F2:  LD      C,L      ;(4)   ; C = medium part of tone period
  1296.         INC     C        ;(4)   ; adds 16 cycles to make duration of high = duration of low
  1297.         JP      (IX)     ;(8)   ; do high pulse of tone
  1298.  
  1299. ;; BE-END
  1300. L03F6:  EI                      ; Enable Interrupts
  1301.         RET                     ;
  1302.  
  1303.  
  1304. ; -------------------
  1305. ; Handle BEEP command
  1306. ; -------------------
  1307. ; BASIC interface to BEEPER subroutine.
  1308. ; Invoked in BASIC with:
  1309. ;   BEEP dur, pitch
  1310. ;   where dur   = duration in seconds
  1311. ;         pitch = # of semitones above/below middle C
  1312. ;
  1313. ; Enter with: pitch on top of calculator stack
  1314. ;             duration next on calculator stack
  1315. ;
  1316. ;; beep
  1317. L03F8:  RST     28H             ;; FP-CALC
  1318.         DB    $31             ;;duplicate                  ; duplicate pitch
  1319.         DB    $27             ;;int                        ; convert to integer
  1320.         DB    $C0             ;;st-mem-0                   ; store integer pitch to memory 0
  1321.         DB    $03             ;;subtract                   ; calculate fractional part of pitch = fp_pitch - int_pitch
  1322.         DB    $34             ;;stk-data                   ; push constant
  1323.         DB    $EC             ;;Exponent: $7C, Bytes: 4    ; constant = 0.05762265
  1324.         DB    $6C,$98,$1F,$F5 ;;($6C,$98,$1F,$F5)
  1325.         DB    $04             ;;multiply                   ; compute:
  1326.         DB    $A1             ;;stk-one                    ; 1 + 0.05762265 * fraction_part(pitch)
  1327.         DB    $0F             ;;addition
  1328.         DB    $38             ;;end-calc                   ; leave on calc stack
  1329.  
  1330.         LD      HL,$5C92        ; MEM-0: number stored here is in 16 bit integer format (pitch)
  1331.                                 ;   0, 0/FF (pos/neg), LSB, MSB, 0
  1332.                                 ;   LSB/MSB is stored in two's complement
  1333.                                 ; In the following, the pitch is checked if it is in the range -128<=p<=127
  1334.         LD      A,(HL)          ; First byte must be zero, otherwise
  1335.         AND     A               ;   error in integer conversion
  1336.         JR      NZ,L046C        ; to REPORT-B
  1337.  
  1338.         INC     HL              ;
  1339.         LD      C,(HL)          ; C = pos/neg flag = 0/FF
  1340.         INC     HL              ;
  1341.         LD      B,(HL)          ; B = LSB, two's complement
  1342.         LD      A,B             ;
  1343.         RLA                     ;
  1344.         SBC     A,A             ; A = 0/FF if B is pos/neg
  1345.         CP      C               ; must be the same as C if the pitch is -128<=p<=127
  1346.         JR      NZ,L046C        ; if no, error REPORT-B
  1347.  
  1348.         INC     HL              ; if -128<=p<=127, MSB will be 0/FF if B is pos/neg
  1349.         CP      (HL)            ; verify this
  1350.         JR      NZ,L046C        ; if no, error REPORT-B
  1351.                                 ; now we know -128<=p<=127
  1352.         LD      A,B             ; A = pitch + 60
  1353.         ADD     A,$3C           ; if -60<=pitch<=67,
  1354.         JP      P,L0425         ;   goto BE-i-OK
  1355.  
  1356.         JP      PO,L046C        ; if pitch <= 67 goto REPORT-B
  1357.                                 ;   lower bound of pitch set at -60
  1358.  
  1359. ;; BE-I-OK                      ; here, -60<=pitch<=127
  1360.                                 ; and A=pitch+60 -> 0<=A<=187
  1361.  
  1362. L0425:  LD      B,$FA           ; 6 octaves below middle C
  1363.  
  1364. ;; BE-OCTAVE                    ; A=# semitones above 5 octaves below middle C
  1365. L0427:  INC     B               ; increment octave
  1366.         SUB     $0C             ; 12 semitones = one octave
  1367.         JR      NC,L0427        ; to BE-OCTAVE
  1368.  
  1369.         ADD     A,$0C           ; A = # semitones above C (0-11)
  1370.         PUSH    BC              ; B = octave displacement from middle C, 2's complement: -5<=B<=10
  1371.         LD      HL,L046E        ; Address: semi-tone
  1372.         CALL    L3406           ; routine LOC-MEM
  1373.                                 ;   HL = 5*A + $046E
  1374.         CALL    L33B4           ; routine STACK-NUM
  1375.                                 ;   read FP value (freq) from semitone table (HL) and push onto calc stack
  1376.  
  1377.         RST     28H             ;; FP-CALC
  1378.         DB    $04             ;;multiply   mult freq by 1 + 0.0576 * fraction_part(pitch) stacked earlier
  1379.                                 ;;             thus taking into account fractional part of pitch.
  1380.                                 ;;           the number 0.0576*frequency is the distance in Hz to the next
  1381.                                 ;;             note (verify with the frequencies recorded in the semitone
  1382.                                 ;;             table below) so that the fraction_part of the pitch does
  1383.                                 ;;             indeed represent a fractional distance to the next note.
  1384.         DB    $38             ;;end-calc   HL points to first byte of fp num on stack = middle frequency to generate
  1385.  
  1386.         POP     AF              ; A = octave displacement from middle C, 2's complement: -5<=A<=10
  1387.         ADD     A,(HL)          ; increase exponent by A (equivalent to multiplying by 2^A)
  1388.         LD      (HL),A          ;
  1389.  
  1390.         RST     28H             ;; FP-CALC
  1391.         DB    $C0             ;;st-mem-0          ; store frequency in memory 0
  1392.         DB    $02             ;;delete            ; remove from calc stack
  1393.         DB    $31             ;;duplicate         ; duplicate duration (seconds)
  1394.         DB    $38             ;;end-calc
  1395.  
  1396.         CALL    L1E94           ; routine FIND-INT1 ; FP duration to A
  1397.         CP      $0B             ; if dur > 10 seconds,
  1398.         JR      NC,L046C        ;   goto REPORT-B
  1399.  
  1400.         ;;; The following calculation finds the tone period for HL and the cycle count
  1401.         ;;; for DE expected in the BEEPER subroutine.  From the example in the BEEPER comments,
  1402.         ;;;
  1403.         ;;; HL = ((fCPU / f) - 236) / 8 = fCPU/8/f - 236/8 = 437500/f -29.5
  1404.         ;;; DE = duration * frequency - 1
  1405.         ;;;
  1406.         ;;; Note the different constant (30.125) used in the calculation of HL
  1407.         ;;; below.  This is probably an error.
  1408.  
  1409.         RST     28H             ;; FP-CALC
  1410.         DB    $E0             ;;get-mem-0                 ; push frequency
  1411.         DB    $04             ;;multiply                  ; result1: #cycles = duration * frequency
  1412.         DB    $E0             ;;get-mem-0                 ; push frequency
  1413.         DB    $34             ;;stk-data                  ; push constant
  1414.         DB    $80             ;;Exponent $93, Bytes: 3    ; constant = 437500
  1415.         DB    $43,$55,$9F,$80 ;;($55,$9F,$80,$00)
  1416.         DB    $01             ;;exchange                  ; frequency on top
  1417.         DB    $05             ;;division                  ; 437500 / frequency
  1418.         DB    $34             ;;stk-data                  ; push constant
  1419.         DB    $35             ;;Exponent: $85, Bytes: 1   ; constant = 30.125
  1420.         DB    $71             ;;($71,$00,$00,$00)
  1421.         DB    $03             ;;subtract                  ; result2: tone_period(HL) = 437500 / freq - 30.125
  1422.         DB    $38             ;;end-calc
  1423.  
  1424.         CALL    L1E99           ; routine FIND-INT2
  1425.         PUSH    BC              ;   BC = tone_period(HL)
  1426.         CALL    L1E99           ; routine FIND-INT2, BC = #cycles to generate
  1427.         POP     HL              ; HL = tone period
  1428.         LD      D,B             ;
  1429.         LD      E,C             ; DE = #cycles
  1430.         LD      A,D             ;
  1431.         OR      E               ;
  1432.         RET     Z               ; if duration = 0, skip BEEP and avoid 65536 cycle
  1433.                                 ;   boondoggle that would occur next
  1434.         DEC     DE              ; DE = #cycles - 1
  1435.         JP      L03B5           ; to BEEPER
  1436.  
  1437. ; ---
  1438.  
  1439.  
  1440. ;; REPORT-B
  1441. L046C:  RST     08H             ; ERROR-1
  1442.         DB    $0A             ; Error Report: Integer out of range
  1443.  
  1444.  
  1445.  
  1446. ; ---------------
  1447. ; Semi-tone table
  1448. ; ---------------
  1449. ;
  1450. ; Holds frequencies corresponding to semitones in middle octave.
  1451. ; To move n octaves higher or lower, frequencies are multiplied by 2^n.
  1452.  
  1453. ;; semi-tone         five byte fp         decimal freq     note (middle)
  1454. L046E:  DB    $89, $02, $D0, $12, $86;  261.625565290         C
  1455.         DB    $89, $0A, $97, $60, $75;  277.182631135         C#
  1456.         DB    $89, $12, $D5, $17, $1F;  293.664768100         D
  1457.         DB    $89, $1B, $90, $41, $02;  311.126983881         D#
  1458.         DB    $89, $24, $D0, $53, $CA;  329.627557039         E
  1459.         DB    $89, $2E, $9D, $36, $B1;  349.228231549         F
  1460.         DB    $89, $38, $FF, $49, $3E;  369.994422674         F#
  1461.         DB    $89, $43, $FF, $6A, $73;  391.995436072         G
  1462.         DB    $89, $4F, $A7, $00, $54;  415.304697513         G#
  1463.         DB    $89, $5C, $00, $00, $00;  440.000000000         A
  1464.         DB    $89, $69, $14, $F6, $24;  466.163761616         A#
  1465.         DB    $89, $76, $F1, $10, $05;  493.883301378         B
  1466.  
  1467.  
  1468. ;****************************************
  1469. ;** Part 4. CASSETTE HANDLING ROUTINES **
  1470. ;****************************************
  1471.  
  1472. ; These routines begin with the service routines followed by a single
  1473. ; command entry point.
  1474. ; The first of these service routines is a curiosity.
  1475.  
  1476. ; -----------------------
  1477. ; THE 'ZX81 NAME' ROUTINE
  1478. ; -----------------------
  1479. ;   This routine fetches a filename in ZX81 format and is not used by the
  1480. ;   cassette handling routines in this ROM.
  1481.  
  1482. ;; zx81-name
  1483. L04AA:  CALL    L24FB           ; routine SCANNING to evaluate expression.
  1484.         LD      A,($5C3B)       ; fetch system variable FLAGS.
  1485.         ADD     A,A             ; test bit 7 - syntax, bit 6 - result type.
  1486.         JP      M,L1C8A         ; to REPORT-C if not string result
  1487.                                 ; 'Nonsense in BASIC'.
  1488.  
  1489.         POP     HL              ; drop return address.
  1490.         RET     NC              ; return early if checking syntax.
  1491.  
  1492.         PUSH    HL              ; re-save return address.
  1493.         CALL    L2BF1           ; routine STK-FETCH fetches string parameters.
  1494.         LD      H,D             ; transfer start of filename
  1495.         LD      L,E             ; to the HL register.
  1496.         DEC     C               ; adjust to point to last character and
  1497.         RET     M               ; return if the null string.
  1498.                                 ; or multiple of 256!
  1499.  
  1500.         ADD     HL,BC           ; find last character of the filename.
  1501.                                 ; and also clear carry.
  1502.         SET     7,(HL)          ; invert it.
  1503.         RET                     ; return.
  1504.  
  1505. ; =========================================
  1506. ;
  1507. ; PORT 254 ($FE)
  1508. ;
  1509. ;                      spk mic { border  }  
  1510. ;          ___ ___ ___ ___ ___ ___ ___ ___
  1511. ; PORT    |   |   |   |   |   |   |   |   |
  1512. ; 254     |   |   |   |   |   |   |   |   |
  1513. ; $FE     |___|___|___|___|___|___|___|___|
  1514. ;           7   6   5   4   3   2   1   0
  1515. ;
  1516.  
  1517. ; ----------------------------------
  1518. ; Save header and program/data bytes
  1519. ; ----------------------------------
  1520. ; This routine saves a section of data. It is called from SA-CTRL to save the
  1521. ; seventeen bytes of header data. It is also the exit route from that routine
  1522. ; when it is set up to save the actual data.
  1523. ; On entry -
  1524. ; HL points to start of data.
  1525. ; IX points to descriptor.
  1526. ; The accumulator is set to  $00 for a header, $FF for data.
  1527.  
  1528. ;; SA-BYTES
  1529. L04C2:  LD      HL,L053F        ; address: SA/LD-RET
  1530.         PUSH    HL              ; is pushed as common exit route.
  1531.                                 ; however there is only one non-terminal exit
  1532.                                 ; point.
  1533.  
  1534.         LD      HL,$1F80        ; a timing constant H=$1F, L=$80
  1535.                                 ; inner and outer loop counters
  1536.                                 ; a five second lead-in is used for a header.
  1537.  
  1538.         BIT     7,A             ; test one bit of accumulator.
  1539.                                 ; (AND A ?)
  1540.         JR      Z,L04D0         ; skip to SA-FLAG if a header is being saved.
  1541.  
  1542. ; else is data bytes and a shorter lead-in is used.
  1543.  
  1544.         LD      HL,$0C98        ; another timing value H=$0C, L=$98.
  1545.                                 ; a two second lead-in is used for the data.
  1546.  
  1547.  
  1548. ;; SA-FLAG
  1549. L04D0:  EX      AF,AF'          ; save flag
  1550.        INC     DE              ; increase length by one.
  1551.        DEC     IX              ; decrease start.
  1552.  
  1553.        DI                      ; disable interrupts
  1554.  
  1555.        LD      A,$02           ; select red for border, microphone bit on.
  1556.        LD      B,A             ; also does as an initial slight counter value.
  1557.  
  1558. ;; SA-LEADER
  1559. L04D8:  DJNZ    L04D8           ; self loop to SA-LEADER for delay.
  1560.                                ; after initial loop, count is $A4 (or $A3)
  1561.  
  1562.        OUT     ($FE),A         ; output byte $02/$0D to tape port.
  1563.  
  1564.        XOR     $0F             ; switch from RED (mic on) to CYAN (mic off).
  1565.  
  1566.        LD      B,$A4           ; hold count. also timed instruction.
  1567.  
  1568.        DEC     L               ; originally $80 or $98.
  1569.                                ; but subsequently cycles 256 times.
  1570.        JR      NZ,L04D8        ; back to SA-LEADER until L is zero.
  1571.  
  1572. ; the outer loop is counted by H
  1573.  
  1574.        DEC     B               ; decrement count
  1575.        DEC     H               ; originally  twelve or thirty-one.
  1576.        JP      P,L04D8         ; back to SA-LEADER until H becomes $FF
  1577.  
  1578. ; now send a synch pulse. At this stage mic is off and A holds value
  1579. ; for mic on.
  1580. ; A synch pulse is much shorter than the steady pulses of the lead-in.
  1581.  
  1582.        LD      B,$2F           ; another short timed delay.
  1583.  
  1584. ;; SA-SYNC-1
  1585. L04EA:  DJNZ    L04EA           ; self loop to SA-SYNC-1
  1586.  
  1587.        OUT     ($FE),A         ; switch to mic on and red.
  1588.        LD      A,$0D           ; prepare mic off - cyan
  1589.        LD      B,$37           ; another short timed delay.
  1590.  
  1591. ;; SA-SYNC-2
  1592. L04F2:  DJNZ    L04F2           ; self loop to SA-SYNC-2
  1593.  
  1594.        OUT     ($FE),A         ; output mic off, cyan border.
  1595.        LD      BC,$3B0E        ; B=$3B time(*), C=$0E, YELLOW, MIC OFF.
  1596.  
  1597. ;
  1598.  
  1599.        EX      AF,AF'          ; restore saved flag
  1600.                                 ; which is 1st byte to be saved.
  1601.  
  1602.         LD      L,A             ; and transfer to L.
  1603.                                 ; the initial parity is A, $FF or $00.
  1604.         JP      L0507           ; JUMP forward to SA-START     ->
  1605.                                 ; the mid entry point of loop.
  1606.  
  1607. ; -------------------------
  1608. ; During the save loop a parity byte is maintained in H.
  1609. ; the save loop begins by testing if reduced length is zero and if so
  1610. ; the final parity byte is saved reducing count to $FFFF.
  1611.  
  1612. ;; SA-LOOP
  1613. L04FE:  LD      A,D             ; fetch high byte
  1614.         OR      E               ; test against low byte.
  1615.         JR      Z,L050E         ; forward to SA-PARITY if zero.
  1616.  
  1617.         LD      L,(IX+$00)      ; load currently addressed byte to L.
  1618.  
  1619. ;; SA-LOOP-P
  1620. L0505:  LD      A,H             ; fetch parity byte.
  1621.         XOR     L               ; exclusive or with new byte.
  1622.  
  1623. ; -> the mid entry point of loop.
  1624.  
  1625. ;; SA-START
  1626. L0507:  LD      H,A             ; put parity byte in H.
  1627.         LD      A,$01           ; prepare blue, mic=on.
  1628.         SCF                     ; set carry flag ready to rotate in.
  1629.         JP      L0525           ; JUMP forward to SA-8-BITS            -8->
  1630.  
  1631. ; ---
  1632.  
  1633. ;; SA-PARITY
  1634. L050E:  LD      L,H             ; transfer the running parity byte to L and
  1635.         JR      L0505           ; back to SA-LOOP-P
  1636.                                 ; to output that byte before quitting normally.
  1637.  
  1638. ; ---
  1639.  
  1640. ; entry point to save yellow part of bit.
  1641. ; a bit consists of a period with mic on and blue border followed by
  1642. ; a period of mic off with yellow border.
  1643. ; Note. since the DJNZ instruction does not affect flags, the zero flag is used
  1644. ; to indicate which of the two passes is in effect and the carry maintains the
  1645. ; state of the bit to be saved.
  1646.  
  1647. ;; SA-BIT-2
  1648. L0511:  LD      A,C             ; fetch 'mic on and yellow' which is
  1649.                                 ; held permanently in C.
  1650.         BIT     7,B             ; set the zero flag. B holds $3E.
  1651.  
  1652. ; entry point to save 1 entire bit. For first bit B holds $3B(*).
  1653. ; Carry is set if saved bit is 1. zero is reset NZ on entry.
  1654.  
  1655. ;; SA-BIT-1
  1656. L0514:  DJNZ    L0514           ; self loop for delay to SA-BIT-1
  1657.  
  1658.         JR      NC,L051C        ; forward to SA-OUT if bit is 0.
  1659.  
  1660. ; but if bit is 1 then the mic state is held for longer.
  1661.  
  1662.         LD      B,$42           ; set timed delay. (66 decimal)
  1663.  
  1664. ;; SA-SET
  1665. L051A:  DJNZ    L051A           ; self loop to SA-SET
  1666.                                 ; (roughly an extra 66*13 clock cycles)
  1667.  
  1668. ;; SA-OUT
  1669. L051C:  OUT     ($FE),A         ; blue and mic on OR  yellow and mic off.
  1670.  
  1671.         LD      B,$3E           ; set up delay
  1672.         JR      NZ,L0511        ; back to SA-BIT-2 if zero reset NZ (first pass)
  1673.  
  1674. ; proceed when the blue and yellow bands have been output.
  1675.  
  1676.         DEC     B               ; change value $3E to $3D.
  1677.         XOR     A               ; clear carry flag (ready to rotate in).
  1678.         INC     A               ; reset zero flag ie. NZ.
  1679.  
  1680. ; -8->
  1681.  
  1682. ;; SA-8-BITS
  1683. L0525:  RL      L               ; rotate left through carry
  1684.                                 ; C<76543210<C  
  1685.         JP      NZ,L0514        ; JUMP back to SA-BIT-1
  1686.                                 ; until all 8 bits done.
  1687.  
  1688. ; when the initial set carry is passed out again then a byte is complete.
  1689.  
  1690.         DEC     DE              ; decrease length
  1691.         INC     IX              ; increase byte pointer
  1692.         LD      B,$31           ; set up timing.
  1693.  
  1694.         LD      A,$7F           ; test the space key and
  1695.         IN      A,($FE)         ; return to common exit (to restore border)
  1696.         RRA                     ; if a space is pressed
  1697.         RET     NC              ; return to SA/LD-RET.   - - >
  1698.  
  1699. ; now test if byte counter has reached $FFFF.
  1700.  
  1701.         LD      A,D             ; fetch high byte
  1702.         INC     A               ; increment.
  1703.         JP      NZ,L04FE        ; JUMP to SA-LOOP if more bytes.
  1704.  
  1705.         LD      B,$3B           ; a final delay.
  1706.  
  1707. ;; SA-DELAY
  1708. L053C:  DJNZ    L053C           ; self loop to SA-DELAY
  1709.  
  1710.         RET                     ; return - - >
  1711.  
  1712. ; --------------------------------------------------
  1713. ; Reset border and check BREAK key for LOAD and SAVE
  1714. ; --------------------------------------------------
  1715. ; the address of this routine is pushed on the stack prior to any load/save
  1716. ; operation and it handles normal completion with the restoration of the
  1717. ; border and also abnormal termination when the break key, or to be more
  1718. ; precise the space key is pressed during a tape operation.
  1719. ; - - >
  1720.  
  1721. ;; SA/LD-RET
  1722. L053F:  PUSH    AF              ; preserve accumulator throughout.
  1723.         LD      A,($5C48)       ; fetch border colour from BORDCR.
  1724.         AND     $38             ; mask off paper bits.
  1725.         RRCA                    ; rotate
  1726.         RRCA                    ; to the
  1727.         RRCA                    ; range 0-7.
  1728.  
  1729. ;===============================
  1730. ;               IF TAP_EMUL
  1731. ;               DB 0,0
  1732. ;               ELSE
  1733.                 OUT ($FE),A     ; change the border colour.
  1734. ;               ENDIF
  1735. ;===============================
  1736.  
  1737.         LD      A,$7F           ; read from port address $7FFE the
  1738.         IN      A,($FE)         ; row with the space key at outside.
  1739.  
  1740.         RRA                     ; test for space key pressed.
  1741.         EI                      ; enable interrupts
  1742.         JR      C,L0554         ; forward to SA/LD-END if not
  1743.  
  1744.  
  1745. ;; REPORT-Da
  1746. L0552:  RST     08H             ; ERROR-1
  1747.         DB    $0C             ; Error Report: BREAK - CONT repeats
  1748.  
  1749. ; ---
  1750.  
  1751. ;; SA/LD-END
  1752. L0554:  POP     AF              ; restore the accumulator.
  1753.         RET                     ; return.
  1754.  
  1755. ; ------------------------------------
  1756. ; Load header or block of information
  1757. ; ------------------------------------
  1758. ; This routine is used to load bytes and on entry A is set to $00 for a
  1759. ; header or to $FF for data.  IX points to the start of receiving location
  1760. ; and DE holds the length of bytes to be loaded. If, on entry the carry flag
  1761. ; is set then data is loaded, if reset then it is verified.
  1762.  
  1763. ;; LD-BYTES
  1764. L0556:  INC     D               ; reset the zero flag without disturbing carry.
  1765.         EX      AF,AF'          ; preserve entry flags.
  1766.        DEC     D               ; restore high byte of length.
  1767.  
  1768.        DI                      ; disable interrupts
  1769.  
  1770.        LD      A,$0F           ; make the border white and mic off.
  1771.  
  1772. ;===============================
  1773. ;               IF TAP_EMUL
  1774. ;               DB 0,0
  1775. ;               ELSE
  1776.                 OUT ($FE),A     ; output to port.
  1777. ;               ENDIF
  1778. ;===============================
  1779.  
  1780.        LD      HL,L053F        ; Address: SA/LD-RET
  1781.        PUSH    HL              ; is saved on stack as terminating routine.
  1782.  
  1783. ; the reading of the EAR bit (D6) will always be preceded by a test of the
  1784. ; space key (D0), so store the initial post-test state.
  1785.  
  1786.        IN      A,($FE)         ; read the ear state - bit 6.
  1787.        RRA                     ; rotate to bit 5.
  1788.        AND     $20             ; isolate this bit.
  1789.        OR      $02             ; combine with red border colour.
  1790.  
  1791. ;===============================
  1792.                 IF TAP_EMUL
  1793.                 RST 0X08
  1794.                 DB TAPE_EMUL
  1795.                 ELSE
  1796.                 LD C,A          ; and store initial state long-term in C.
  1797.                 CP A            ; set the zero flag.
  1798.                 ENDIF
  1799. ;===============================
  1800.  
  1801. ;; LD-BREAK
  1802. L056B:  RET     NZ              ; return if at any time space is pressed.
  1803.  
  1804. ;; LD-START
  1805. L056C:  CALL    L05E7           ; routine LD-EDGE-1
  1806.        JR      NC,L056B        ; back to LD-BREAK with time out and no
  1807.                                ; edge present on tape.
  1808.  
  1809. ; but continue when a transition is found on tape.
  1810.  
  1811.        LD      HL,$0415        ; set up 16-bit outer loop counter for
  1812.                                ; approx 1 second delay.
  1813.  
  1814. ;; LD-WAIT
  1815. L0574:  DJNZ    L0574           ; self loop to LD-WAIT (for 256 times)
  1816.  
  1817.        DEC     HL              ; decrease outer loop counter.
  1818.        LD      A,H             ; test for
  1819.        OR      L               ; zero.
  1820.        JR      NZ,L0574        ; back to LD-WAIT, if not zero, with zero in B.
  1821.  
  1822. ; continue after delay with H holding zero and B also.
  1823. ; sample 256 edges to check that we are in the middle of a lead-in section.
  1824.  
  1825.        CALL    L05E3           ; routine LD-EDGE-2
  1826.        JR      NC,L056B        ; back to LD-BREAK
  1827.                                ; if no edges at all.
  1828.  
  1829. ;; LD-LEADER
  1830. L0580:  LD      B,$9C           ; set timing value.
  1831.        CALL    L05E3           ; routine LD-EDGE-2
  1832.        JR      NC,L056B        ; back to LD-BREAK if time-out
  1833.  
  1834.        LD      A,$C6           ; two edges must be spaced apart.
  1835.        CP      B               ; compare
  1836.        JR      NC,L056C        ; back to LD-START if too close together for a
  1837.                                ; lead-in.
  1838.  
  1839.        INC     H               ; proceed to test 256 edged sample.
  1840.        JR      NZ,L0580        ; back to LD-LEADER while more to do.
  1841.  
  1842. ; sample indicates we are in the middle of a two or five second lead-in.
  1843. ; Now test every edge looking for the terminal synch signal.
  1844.  
  1845. ;; LD-SYNC
  1846. L058F:  LD      B,$C9           ; initial timing value in B.
  1847.        CALL    L05E7           ; routine LD-EDGE-1
  1848.        JR      NC,L056B        ; back to LD-BREAK with time-out.
  1849.  
  1850.        LD      A,B             ; fetch augmented timing value from B.
  1851.        CP      $D4             ; compare
  1852.        JR      NC,L058F        ; back to LD-SYNC if gap too big, that is,
  1853.                                ; a normal lead-in edge gap.
  1854.  
  1855. ; but a short gap will be the synch pulse.
  1856. ; in which case another edge should appear before B rises to $FF
  1857.  
  1858.        CALL    L05E7           ; routine LD-EDGE-1
  1859.        RET     NC              ; return with time-out.
  1860.  
  1861. ; proceed when the synch at the end of the lead-in is found.
  1862. ; We are about to load data so change the border colours.
  1863.  
  1864.        LD      A,C             ; fetch long-term mask from C
  1865.        XOR     $03             ; and make blue/yellow.
  1866.  
  1867.        LD      C,A             ; store the new long-term byte.
  1868.  
  1869.        LD      H,$00           ; set up parity byte as zero.
  1870.        LD      B,$B0           ; timing.
  1871.        JR      L05C8           ; forward to LD-MARKER
  1872.                                ; the loop mid entry point with the alternate
  1873.                                ; zero flag reset to indicate first byte
  1874.                                ; is discarded.
  1875.  
  1876. ; --------------
  1877. ; the loading loop loads each byte and is entered at the mid point.
  1878.  
  1879. ;; LD-LOOP
  1880. L05A9:  EX      AF,AF'          ; restore entry flags and type in A.
  1881.         JR      NZ,L05B3        ; forward to LD-FLAG if awaiting initial flag
  1882.                                 ; which is to be discarded.
  1883.  
  1884.         JR      NC,L05BD        ; forward to LD-VERIFY if not to be loaded.
  1885.  
  1886.         LD      (IX+$00),L      ; place loaded byte at memory location.
  1887.         JR      L05C2           ; forward to LD-NEXT
  1888.  
  1889. ; ---
  1890.  
  1891. ;; LD-FLAG
  1892. L05B3:  RL      C               ; preserve carry (verify) flag in long-term
  1893.                                 ; state byte. Bit 7 can be lost.
  1894.  
  1895.         XOR     L               ; compare type in A with first byte in L.
  1896.         RET     NZ              ; return if no match e.g. CODE vs DATA.
  1897.  
  1898. ; continue when data type matches.
  1899.  
  1900.         LD      A,C             ; fetch byte with stored carry
  1901.         RRA                     ; rotate it to carry flag again
  1902.         LD      C,A             ; restore long-term port state.
  1903.  
  1904.         INC     DE              ; increment length ??
  1905.         JR      L05C4           ; forward to LD-DEC.
  1906.                                 ; but why not to location after ?
  1907.  
  1908. ; ---
  1909. ; for verification the byte read from tape is compared with that in memory.
  1910.  
  1911. ;; LD-VERIFY
  1912. L05BD:  LD      A,(IX+$00)      ; fetch byte from memory.
  1913.         XOR     L               ; compare with that on tape
  1914.         RET     NZ              ; return if not zero.
  1915.  
  1916. ;; LD-NEXT
  1917. L05C2:  INC     IX              ; increment byte pointer.
  1918.  
  1919. ;; LD-DEC
  1920. L05C4:  DEC     DE              ; decrement length.
  1921.         EX      AF,AF'          ; store the flags.
  1922.        LD      B,$B2           ; timing.
  1923.  
  1924. ; when starting to read 8 bits the receiving byte is marked with bit at right.
  1925. ; when this is rotated out again then 8 bits have been read.
  1926.  
  1927. ;; LD-MARKER
  1928. L05C8:  LD      L,$01           ; initialize as %00000001
  1929.  
  1930. ;; LD-8-BITS
  1931. L05CA:  CALL    L05E3           ; routine LD-EDGE-2 increments B relative to
  1932.                                ; gap between 2 edges.
  1933.        RET     NC              ; return with time-out.
  1934.  
  1935.        LD      A,$CB           ; the comparison byte.
  1936.        CP      B               ; compare to incremented value of B.
  1937.                                ; if B is higher then bit on tape was set.
  1938.                                ; if <= then bit on tape is reset.
  1939.  
  1940.        RL      L               ; rotate the carry bit into L.
  1941.  
  1942.        LD      B,$B0           ; reset the B timer byte.
  1943.        JP      NC,L05CA        ; JUMP back to LD-8-BITS
  1944.  
  1945. ; when carry set then marker bit has been passed out and byte is complete.
  1946.  
  1947.        LD      A,H             ; fetch the running parity byte.
  1948.        XOR     L               ; include the new byte.
  1949.        LD      H,A             ; and store back in parity register.
  1950.  
  1951.        LD      A,D             ; check length of
  1952.        OR      E               ; expected bytes.
  1953.        JR      NZ,L05A9        ; back to LD-LOOP
  1954.                                ; while there are more.
  1955.  
  1956. ; when all bytes loaded then parity byte should be zero.
  1957.  
  1958.        LD      A,H             ; fetch parity byte.
  1959.        CP      $01             ; set carry if zero.
  1960.        RET                     ; return
  1961.                                ; in no carry then error as checksum disagrees.
  1962.  
  1963. ; -------------------------
  1964. ; Check signal being loaded
  1965. ; -------------------------
  1966. ; An edge is a transition from one mic state to another.
  1967. ; More specifically a change in bit 6 of value input from port $FE.
  1968. ; Graphically it is a change of border colour, say, blue to yellow.
  1969. ; The first entry point looks for two adjacent edges. The second entry point
  1970. ; is used to find a single edge.
  1971. ; The B register holds a count, up to 256, within which the edge (or edges)
  1972. ; must be found. The gap between two edges will be more for a '1' than a '0'
  1973. ; so the value of B denotes the state of the bit (two edges) read from tape.
  1974.  
  1975. ; ->
  1976.  
  1977. ;; LD-EDGE-2
  1978. L05E3:  CALL    L05E7           ; call routine LD-EDGE-1 below.
  1979.        RET     NC              ; return if space pressed or time-out.
  1980.                                ; else continue and look for another adjacent
  1981.                                ; edge which together represent a bit on the
  1982.                                ; tape.
  1983.  
  1984. ; ->
  1985. ; this entry point is used to find a single edge from above but also
  1986. ; when detecting a read-in signal on the tape.
  1987.  
  1988. ;; LD-EDGE-1
  1989. L05E7:  LD      A,$16           ; a delay value of twenty two.
  1990.  
  1991. ;; LD-DELAY
  1992. L05E9:  DEC     A               ; decrement counter
  1993.        JR      NZ,L05E9        ; loop back to LD-DELAY 22 times.
  1994.  
  1995.        AND      A              ; clear carry.
  1996.  
  1997. ;; LD-SAMPLE
  1998. L05ED:  INC     B               ; increment the time-out counter.
  1999.        RET     Z               ; return with failure when $FF passed.
  2000.  
  2001.        LD      A,$7F           ; prepare to read keyboard and EAR port
  2002.        IN      A,($FE)         ; row $7FFE. bit 6 is EAR, bit 0 is SPACE key.
  2003.        RRA                     ; test outer key the space. (bit 6 moves to 5)
  2004.        RET     NC              ; return if space pressed.  >>>
  2005.  
  2006.        XOR     C               ; compare with initial long-term state.
  2007.        AND     $20             ; isolate bit 5
  2008.        JR      Z,L05ED         ; back to LD-SAMPLE if no edge.
  2009.  
  2010. ; but an edge, a transition of the EAR bit, has been found so switch the
  2011. ; long-term comparison byte containing both border colour and EAR bit.
  2012.  
  2013.        LD      A,C             ; fetch comparison value.
  2014.        CPL                     ; switch the bits
  2015.        LD      C,A             ; and put back in C for long-term.
  2016.  
  2017.        AND     $07             ; isolate new colour bits.
  2018.        OR      $08             ; set bit 3 - MIC off.
  2019.        OUT     ($FE),A         ; send to port to effect change of colour.
  2020.  
  2021.        SCF                     ; set carry flag signaling edge found within
  2022.                                ; time allowed.
  2023.        RET                     ; return.
  2024.  
  2025. ; ---------------------------------
  2026. ; Entry point for all tape commands
  2027. ; ---------------------------------
  2028. ; This is the single entry point for the four tape commands.
  2029. ; The routine first determines in what context it has been called by examining
  2030. ; the low byte of the Syntax table entry which was stored in T_ADDR.
  2031. ; Subtracting $EO (the present arrangement) gives a value of
  2032. ; $00 - SAVE
  2033. ; $01 - LOAD
  2034. ; $02 - VERIFY
  2035. ; $03 - MERGE
  2036. ; As with all commands the address STMT-RET is on the stack.
  2037.  
  2038. ;; SAVE-ETC
  2039. L0605:  POP     AF              ; discard address STMT-RET.
  2040.        LD      A,($5C74)       ; fetch T_ADDR
  2041.  
  2042. ; Now reduce the low byte of the Syntax table entry to give command.
  2043. ; Note. For ZASM use SUB $E0 as next instruction.
  2044.  
  2045. L0609           SUB LOW (L1ADF)+1       ; subtract the known offset.
  2046.                                ; ( is SUB $E0 in standard ROM )
  2047.  
  2048.        LD      ($5C74),A       ; and put back in T_ADDR as 0,1,2, or 3
  2049.                                ; for future reference.
  2050.  
  2051.        CALL    L1C8C           ; routine EXPT-EXP checks that a string
  2052.                                ; expression follows and stacks the
  2053.                                ; parameters in run-time.
  2054.  
  2055.        CALL    L2530           ; routine SYNTAX-Z
  2056.        JR      Z,L0652         ; forward to SA-DATA if checking syntax.
  2057.  
  2058.        LD      BC,$0011        ; presume seventeen bytes for a header.
  2059.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2060.        AND     A               ; test for zero - SAVE.
  2061.        JR      Z,L0621         ; forward to SA-SPACE if so.
  2062.  
  2063.        LD      C,$22           ; else double length to thirty four.
  2064.  
  2065. ;; SA-SPACE
  2066. L0621:  RST     30H             ; BC-SPACES creates 17/34 bytes in workspace.
  2067.  
  2068.        PUSH    DE              ; transfer the start of new space to
  2069.        POP     IX              ; the available index register.
  2070.  
  2071. ; ten spaces are required for the default filename but it is simpler to
  2072. ; overwrite the first file-type indicator byte as well.
  2073.  
  2074.        LD      B,$0B           ; set counter to eleven.
  2075.        LD      A,$20           ; prepare a space.
  2076.  
  2077. ;; SA-BLANK
  2078. L0629:  LD      (DE),A          ; set workspace location to space.
  2079.        INC     DE              ; next location.
  2080.        DJNZ    L0629           ; loop back to SA-BLANK till all eleven done.
  2081.  
  2082.        LD      (IX+$01),$FF    ; set first byte of ten character filename
  2083.                                ; to $FF as a default to signal null string.
  2084.  
  2085.        CALL    L2BF1           ; routine STK-FETCH fetches the filename
  2086.                                ; parameters from the calculator stack.
  2087.                                ; length of string in BC.
  2088.                                ; start of string in DE.
  2089.  
  2090.        LD      HL,$FFF6        ; prepare the value minus ten.
  2091.        DEC     BC              ; decrement length.
  2092.                                ; ten becomes nine, zero becomes $FFFF.
  2093.        ADD     HL,BC           ; trial addition.
  2094.        INC     BC              ; restore true length.
  2095.        JR      NC,L064B        ; forward to SA-NAME if length is one to ten.
  2096.  
  2097. ; the filename is more than ten characters in length or the null string.
  2098.  
  2099.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2100.        AND     A               ; test for zero - SAVE.
  2101.        JR      NZ,L0644        ; forward to SA-NULL if not the SAVE command.
  2102.  
  2103. ; but no more than ten characters are allowed for SAVE.
  2104. ; The first ten characters of any other command parameter are acceptable.
  2105. ; Weird, but necessary, if saving to sectors.
  2106. ; Note. the golden rule that there are no restriction on anything is broken.
  2107.  
  2108. ;; REPORT-Fa
  2109. L0642:  RST     08H             ; ERROR-1
  2110.        DB    $0E             ; Error Report: Invalid file name
  2111.  
  2112. ; continue with LOAD, MERGE, VERIFY and also SAVE within ten character limit.
  2113.  
  2114. ;; SA-NULL
  2115. L0644:  LD      A,B             ; test length of filename
  2116.        OR      C               ; for zero.
  2117.        JR      Z,L0652         ; forward to SA-DATA if so using the 255
  2118.                                ; indicator followed by spaces.
  2119.  
  2120.        LD      BC,$000A        ; else trim length to ten.
  2121.  
  2122. ; other paths rejoin here with BC holding length in range 1 - 10.
  2123.  
  2124. ;; SA-NAME
  2125. L064B:  PUSH    IX              ; push start of file descriptor.
  2126.        POP     HL              ; and pop into HL.
  2127.  
  2128.        INC     HL              ; HL now addresses first byte of filename.
  2129.        EX      DE,HL           ; transfer destination address to DE, start
  2130.                                ; of string in command to HL.
  2131.        LDIR                    ; copy up to ten bytes
  2132.                                ; if less than ten then trailing spaces follow.
  2133.  
  2134. ; the case for the null string rejoins here.
  2135.  
  2136. ;; SA-DATA
  2137. L0652:  RST     18H             ; GET-CHAR
  2138.        CP      $E4             ; is character after filename the token 'DATA' ?
  2139.        JR      NZ,L06A0        ; forward to SA-SCR$ to consider SCREEN$ if
  2140.                                ; not.
  2141.  
  2142. ; continue to consider DATA.
  2143.  
  2144.        LD      A,($5C74)       ; fetch command from T_ADDR
  2145.        CP      $03             ; is it 'VERIFY' ?
  2146.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2147.                                ; 'Nonsense in BASIC'
  2148.                                ; VERIFY "d" DATA is not allowed.
  2149.  
  2150. ; continue with SAVE, LOAD, MERGE of DATA.
  2151.  
  2152.        RST     20H             ; NEXT-CHAR
  2153.        CALL    L28B2           ; routine LOOK-VARS searches variables area
  2154.                                ; returning with carry reset if found or
  2155.                                ; checking syntax.
  2156.        SET     7,C             ; this converts a simple string to a
  2157.                                ; string array. The test for an array or string
  2158.                                ; comes later.
  2159.        JR      NC,L0672        ; forward to SA-V-OLD if variable found.
  2160.  
  2161.        LD      HL,$0000        ; set destination to zero as not fixed.
  2162.        LD      A,($5C74)       ; fetch command from T_ADDR
  2163.        DEC     A               ; test for 1 - LOAD
  2164.        JR      Z,L0685         ; forward to SA-V-NEW with LOAD DATA.
  2165.                                ; to load a new array.
  2166.  
  2167. ; otherwise the variable was not found in run-time with SAVE/MERGE.
  2168.  
  2169. ;; REPORT-2a
  2170. L0670:  RST     08H             ; ERROR-1
  2171.        DB    $01             ; Error Report: Variable not found
  2172.  
  2173. ; continue with SAVE/LOAD  DATA
  2174.  
  2175. ;; SA-V-OLD
  2176. L0672:  JP      NZ,L1C8A        ; to REPORT-C if not an array variable.
  2177.                                ; or erroneously a simple string.
  2178.                                ; 'Nonsense in BASIC'
  2179.  
  2180.  
  2181.        CALL    L2530           ; routine SYNTAX-Z
  2182.        JR      Z,L0692         ; forward to SA-DATA-1 if checking syntax.
  2183.  
  2184.        INC     HL              ; step past single character variable name.
  2185.        LD      A,(HL)          ; fetch low byte of length.
  2186.        LD      (IX+$0B),A      ; place in descriptor.
  2187.        INC     HL              ; point to high byte.
  2188.        LD      A,(HL)          ; and transfer that
  2189.        LD      (IX+$0C),A      ; to descriptor.
  2190.        INC     HL              ; increase pointer within variable.
  2191.  
  2192. ;; SA-V-NEW
  2193. L0685:  LD      (IX+$0E),C      ; place character array name in  header.
  2194.        LD      A,$01           ; default to type numeric.
  2195.        BIT     6,C             ; test result from look-vars.
  2196.        JR      Z,L068F         ; forward to SA-V-TYPE if numeric.
  2197.  
  2198.        INC     A               ; set type to 2 - string array.
  2199.  
  2200. ;; SA-V-TYPE
  2201. L068F:  LD      (IX+$00),A      ; place type 0, 1 or 2 in descriptor.
  2202.  
  2203. ;; SA-DATA-1
  2204. L0692:  EX      DE,HL           ; save var pointer in DE
  2205.  
  2206.        RST     20H             ; NEXT-CHAR
  2207.        CP      $29             ; is character ')' ?
  2208.        JR      NZ,L0672        ; back if not to SA-V-OLD to report
  2209.                                ; 'Nonsense in BASIC'
  2210.  
  2211.        RST     20H             ; NEXT-CHAR advances character address.
  2212.        CALL    L1BEE           ; routine CHECK-END errors if not end of
  2213.                                ; the statement.
  2214.  
  2215.        EX      DE,HL           ; bring back variables data pointer.
  2216.        JP      L075A           ; jump forward to SA-ALL
  2217.  
  2218. ; ---
  2219. ; the branch was here to consider a 'SCREEN$', the display file.
  2220.  
  2221. ;; SA-SCR$
  2222. L06A0:  CP      $AA             ; is character the token 'SCREEN$' ?
  2223.        JR      NZ,L06C3        ; forward to SA-CODE if not.
  2224.  
  2225.        LD      A,($5C74)       ; fetch command from T_ADDR
  2226.        CP      $03             ; is it MERGE ?
  2227.        JP       Z,L1C8A        ; jump to REPORT-C if so.
  2228.                                ; 'Nonsense in BASIC'
  2229.  
  2230. ; continue with SAVE/LOAD/VERIFY SCREEN$.
  2231.  
  2232.        RST     20H             ; NEXT-CHAR
  2233.        CALL    L1BEE           ; routine CHECK-END errors if not at end of
  2234.                                ; statement.
  2235.  
  2236. ; continue in runtime.
  2237.  
  2238.        LD      (IX+$0B),$00    ; set descriptor length
  2239.        LD      (IX+$0C),$1B    ; to $1b00 to include bitmaps and attributes.
  2240.  
  2241.        LD      HL,$4000        ; set start to display file start.
  2242.        LD      (IX+$0D),L      ; place start in
  2243.        LD      (IX+$0E),H      ; the descriptor.
  2244.        JR      L0710           ; forward to SA-TYPE-3
  2245.  
  2246. ; ---
  2247. ; the branch was here to consider CODE.
  2248.  
  2249. ;; SA-CODE
  2250. L06C3:  CP      $AF             ; is character the token 'CODE' ?
  2251.        JR      NZ,L0716        ; forward if not to SA-LINE to consider an
  2252.                                ; auto-started BASIC program.
  2253.  
  2254.        LD      A,($5C74)       ; fetch command from T_ADDR
  2255.        CP      $03             ; is it MERGE ?
  2256.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2257.                                ; 'Nonsense in BASIC'
  2258.  
  2259.  
  2260.        RST     20H             ; NEXT-CHAR advances character address.
  2261.        CALL    L2048           ; routine PR-ST-END checks if a carriage
  2262.                                ; return or ':' follows.
  2263.        JR      NZ,L06E1        ; forward to SA-CODE-1 if there are parameters.
  2264.  
  2265.        LD      A,($5C74)       ; else fetch the command from T_ADDR.
  2266.        AND     A               ; test for zero - SAVE without a specification.
  2267.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2268.                                ; 'Nonsense in BASIC'
  2269.  
  2270. ; for LOAD/VERIFY put zero on stack to signify handle at location saved from.
  2271.  
  2272.        CALL    L1CE6           ; routine USE-ZERO
  2273.        JR      L06F0           ; forward to SA-CODE-2
  2274.  
  2275. ; ---
  2276. ; if there are more characters after CODE expect start and possibly length.
  2277.  
  2278. ;; SA-CODE-1
  2279. L06E1:  CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2280.                                ; expression and stacks it in run-time.
  2281.  
  2282.        RST     18H             ; GET-CHAR
  2283.        CP      $2C             ; does a comma follow ?
  2284.        JR      Z,L06F5         ; forward if so to SA-CODE-3
  2285.  
  2286. ; else allow saved code to be loaded to a specified address.
  2287.  
  2288.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2289.        AND     A               ; is the command SAVE which requires length ?
  2290.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2291.                                ; 'Nonsense in BASIC'
  2292.  
  2293. ; the command LOAD code may rejoin here with zero stacked as start.
  2294.  
  2295. ;; SA-CODE-2
  2296. L06F0:  CALL    L1CE6           ; routine USE-ZERO stacks zero for length.
  2297.        JR      L06F9           ; forward to SA-CODE-4
  2298.  
  2299. ; ---
  2300. ; the branch was here with SAVE CODE start,
  2301.  
  2302. ;; SA-CODE-3
  2303. L06F5:  RST     20H             ; NEXT-CHAR advances character address.
  2304.        CALL    L1C82           ; routine EXPT-1NUM checks for expression
  2305.                                ; and stacks in run-time.
  2306.  
  2307. ; paths converge here and nothing must follow.
  2308.  
  2309. ;; SA-CODE-4
  2310. L06F9:  CALL    L1BEE           ; routine CHECK-END errors with extraneous
  2311.                                ; characters and quits if checking syntax.
  2312.  
  2313. ; in run-time there are two 16-bit parameters on the calculator stack.
  2314.  
  2315.        CALL    L1E99           ; routine FIND-INT2 gets length.
  2316.        LD      (IX+$0B),C      ; place length
  2317.        LD      (IX+$0C),B      ; in descriptor.
  2318.        CALL    L1E99           ; routine FIND-INT2 gets start.
  2319.        LD      (IX+$0D),C      ; place start
  2320.        LD      (IX+$0E),B      ; in descriptor.
  2321.        LD      H,B             ; transfer the
  2322.        LD      L,C             ; start to HL also.
  2323.  
  2324. ;; SA-TYPE-3
  2325. L0710:  LD      (IX+$00),$03    ; place type 3 - code in descriptor.
  2326.        JR      L075A           ; forward to SA-ALL.
  2327.  
  2328. ; ---
  2329. ; the branch was here with BASIC to consider an optional auto-start line
  2330. ; number.
  2331.  
  2332. ;; SA-LINE
  2333. L0716:  CP      $CA             ; is character the token 'LINE' ?
  2334.        JR      Z,L0723         ; forward to SA-LINE-1 if so.
  2335.  
  2336. ; else all possibilities have been considered and nothing must follow.
  2337.  
  2338.        CALL    L1BEE           ; routine CHECK-END
  2339.  
  2340. ; continue in run-time to save BASIC without auto-start.
  2341.  
  2342.        LD      (IX+$0E),$80    ; place high line number in descriptor to
  2343.                                ; disable auto-start.
  2344.        JR      L073A           ; forward to SA-TYPE-0 to save program.
  2345.  
  2346. ; ---
  2347. ; the branch was here to consider auto-start.
  2348.  
  2349. ;; SA-LINE-1
  2350. L0723:  LD      A,($5C74)       ; fetch command from T_ADDR
  2351.        AND     A               ; test for SAVE.
  2352.        JP      NZ,L1C8A        ; jump forward to REPORT-C with anything else.
  2353.                                ; 'Nonsense in BASIC'
  2354.  
  2355. ;
  2356.  
  2357.        RST     20H             ; NEXT-CHAR
  2358.        CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2359.                                ; expression and stacks in run-time.
  2360.        CALL    L1BEE           ; routine CHECK-END quits if syntax path.
  2361.        CALL    L1E99           ; routine FIND-INT2 fetches the numeric
  2362.                                ; expression.
  2363.        LD      (IX+$0D),C      ; place the auto-start
  2364.        LD      (IX+$0E),B      ; line number in the descriptor.
  2365.  
  2366. ; Note. this isn't checked, but is subsequently handled by the system.
  2367. ; If the user typed 40000 instead of 4000 then it won't auto-start
  2368. ; at line 4000, or indeed, at all.
  2369.  
  2370. ; continue to save program and any variables.
  2371.  
  2372. ;; SA-TYPE-0
  2373. L073A:  LD      (IX+$00),$00    ; place type zero - program in descriptor.
  2374.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  2375.         LD      DE,($5C53)      ; fetch PROG to DE.
  2376.  
  2377. ;===============================
  2378.                 IF TAP_EMUL
  2379.                 CALL INI_TAP
  2380.                 ELSE
  2381.                
  2382.                 SCF             ; set carry flag to calculate from end of
  2383.                                 ; variables E_LINE -1.
  2384.                 SBC HL,DE       ; subtract to give total length.
  2385.                 ENDIF
  2386.  
  2387.         LD      (IX+$0B),L      ; place total length
  2388.         LD      (IX+$0C),H      ; in descriptor.
  2389.         LD      HL,($5C4B)      ; load HL from system variable VARS
  2390.         SBC     HL,DE           ; subtract to give program length.
  2391.         LD      (IX+$0F),L      ; place length of program
  2392.         LD      (IX+$10),H      ; in the descriptor.
  2393.         EX      DE,HL           ; start to HL, length to DE.
  2394.  
  2395. ;; SA-ALL
  2396. L075A:  LD      A,($5C74)       ; fetch command from T_ADDR
  2397.         AND     A               ; test for zero - SAVE.
  2398.         JP      Z,L0970         ; jump forward to SA-CONTRL with SAVE  ->
  2399.  
  2400. ; ---
  2401. ; continue with LOAD, MERGE and VERIFY.
  2402.  
  2403.         PUSH    HL              ; save start.
  2404.         LD      BC,$0011        ; prepare to add seventeen
  2405.         ADD     IX,BC           ; to point IX at second descriptor.
  2406.  
  2407. ;; LD-LOOK-H
  2408. L0767:  PUSH    IX              ; save IX
  2409.         LD      DE,$0011        ; seventeen bytes
  2410.         XOR     A               ; reset zero flag
  2411.         SCF                     ; set carry flag
  2412.         CALL    L0556           ; routine LD-BYTES loads a header from tape
  2413.                                 ; to second descriptor.
  2414.         POP     IX              ; restore IX.
  2415.         JR      NC,L0767        ; loop back to LD-LOOK-H until header found.
  2416.  
  2417.         LD      A,$FE           ; select system channel 'S'
  2418.         CALL    L1601           ; routine CHAN-OPEN opens it.
  2419.  
  2420.         LD      (IY+$52),$03    ; set SCR_CT to 3 lines.
  2421.  
  2422.         LD      C,$80           ; C has bit 7 set to indicate type mismatch as
  2423.                                 ; a default startpoint.
  2424.  
  2425.         LD      A,(IX+$00)      ; fetch loaded header type to A
  2426.         CP      (IX-$11)        ; compare with expected type.
  2427.         JR      NZ,L078A        ; forward to LD-TYPE with mis-match.
  2428.  
  2429.         LD      C,$F6           ; set C to minus ten - will count characters
  2430.                                 ; up to zero.
  2431.  
  2432. ;; LD-TYPE
  2433. L078A:  CP      $04             ; check if type in acceptable range 0 - 3.
  2434.         JR      NC,L0767        ; back to LD-LOOK-H with 4 and over.
  2435.  
  2436. ; else A indicates type 0-3.
  2437.  
  2438.         LD      DE,L09C0        ; address base of last 4 tape messages
  2439.         PUSH    BC              ; save BC
  2440.         CALL    L0C0A           ; routine PO-MSG outputs relevant message.
  2441.                                 ; Note. all messages have a leading newline.
  2442.         POP     BC              ; restore BC
  2443.  
  2444.         PUSH    IX              ; transfer IX,
  2445.         POP     DE              ; the 2nd descriptor, to DE.
  2446.         LD      HL,$FFF0        ; prepare minus seventeen.
  2447.         ADD     HL,DE           ; add to point HL to 1st descriptor.
  2448.         LD      B,$0A           ; the count will be ten characters for the
  2449.                                 ; filename.
  2450.  
  2451.         LD      A,(HL)          ; fetch first character and test for
  2452.         INC     A               ; value 255.
  2453.         JR      NZ,L07A6        ; forward to LD-NAME if not the wildcard.
  2454.  
  2455. ; but if it is the wildcard, then add ten to C which is minus ten for a type
  2456. ; match or -128 for a type mismatch. Although characters have to be counted
  2457. ; bit 7 of C will not alter from state set here.
  2458.  
  2459.         LD      A,C             ; transfer $F6 or $80 to A
  2460.         ADD     A,B             ; add $0A
  2461.         LD      C,A             ; place result, zero or -118, in C.
  2462.  
  2463. ; At this point we have either a type mismatch, a wildcard match or ten
  2464. ; characters to be counted. The characters must be shown on the screen.
  2465.  
  2466. ;; LD-NAME
  2467. L07A6:  INC     DE              ; address next input character
  2468.         LD      A,(DE)          ; fetch character
  2469.         CP      (HL)            ; compare to expected
  2470.         INC     HL              ; address next expected character
  2471.         JR      NZ,L07AD        ; forward to LD-CH-PR with mismatch
  2472.  
  2473.         INC     C               ; increment matched character count
  2474.  
  2475. ;; LD-CH-PR
  2476. L07AD:  RST     10H             ; PRINT-A prints character
  2477.         DJNZ    L07A6           ; loop back to LD-NAME for ten characters.
  2478.  
  2479. ; if ten characters matched and the types previously matched then C will
  2480. ; now hold zero.
  2481.  
  2482.         BIT     7,C             ; test if all matched
  2483.         JR      NZ,L0767        ; back to LD-LOOK-H if not
  2484.  
  2485. ; else print a terminal carriage return.
  2486.  
  2487.         LD      A,$0D           ; prepare carriage return.
  2488.         RST     10H             ; PRINT-A outputs it.
  2489.  
  2490. ; The various control routines for LOAD, VERIFY and MERGE are executed
  2491. ; during the one-second gap following the header on tape.
  2492.  
  2493.         POP     HL              ; restore xx
  2494.         LD      A,(IX+$00)      ; fetch incoming type
  2495.         CP      $03             ; compare with CODE
  2496.         JR      Z,L07CB         ; forward to VR-CONTROL if it is CODE.
  2497.  
  2498. ;  type is a program or an array.
  2499.  
  2500.         LD      A,($5C74)       ; fetch command from T_ADDR
  2501.         DEC     A               ; was it LOAD ?
  2502.         JP      Z,L0808         ; JUMP forward to LD-CONTRL if so to
  2503.                                 ; load BASIC or variables.
  2504.  
  2505.         CP      $02             ; was command MERGE ?
  2506.         JP      Z,L08B6         ; jump forward to ME-CONTRL if so.
  2507.  
  2508. ; else continue into VERIFY control routine to verify.
  2509.  
  2510. ; ---------------------
  2511. ; Handle VERIFY control
  2512. ; ---------------------
  2513. ; There are two branches to this routine.
  2514. ; 1) From above to verify a program or array
  2515. ; 2) from earlier with no carry to load or verify code.
  2516.  
  2517. ;; VR-CONTROL
  2518. L07CB:  PUSH    HL              ; save pointer to data.
  2519.         LD      L,(IX-$06)      ; fetch length of old data
  2520.         LD      H,(IX-$05)      ; to HL.
  2521.         LD      E,(IX+$0B)      ; fetch length of new data
  2522.         LD      D,(IX+$0C)      ; to DE.
  2523.         LD      A,H             ; check length of old
  2524.         OR      L               ; for zero.
  2525.         JR      Z,L07E9         ; forward to VR-CONT-1 if length unspecified
  2526.                                 ; e.g LOAD "x" CODE
  2527.  
  2528. ; as opposed to, say, LOAD 'x' CODE 32768,300.
  2529.  
  2530.         SBC     HL,DE           ; subtract the two lengths.
  2531.         JR      C,L0806         ; forward to REPORT-R if the length on tape is
  2532.                                 ; larger than that specified in command.
  2533.                                 ; 'Tape loading error'
  2534.  
  2535.         JR      Z,L07E9         ; forward to VR-CONT-1 if lengths match.
  2536.  
  2537. ; a length on tape shorter than expected is not allowed for CODE
  2538.  
  2539.         LD      A,(IX+$00)      ; else fetch type from tape.
  2540.         CP      $03             ; is it CODE ?
  2541.         JR      NZ,L0806        ; forward to REPORT-R if so
  2542.                                 ; 'Tape loading error'
  2543.  
  2544. ;; VR-CONT-1
  2545. L07E9:  POP     HL              ; pop pointer to data
  2546.         LD      A,H             ; test for zero
  2547.         OR      L               ; e.g. LOAD 'x' CODE
  2548.         JR      NZ,L07F4        ; forward to VR-CONT-2 if destination specified.
  2549.  
  2550.         LD      L,(IX+$0D)      ; else use the destination in the header
  2551.         LD      H,(IX+$0E)      ; and load code at address saved from.
  2552.  
  2553. ;; VR-CONT-2
  2554. L07F4:  PUSH    HL              ; push pointer to start of data block.
  2555.         POP     IX              ; transfer to IX.
  2556.         LD      A,($5C74)       ; fetch reduced command from T_ADDR
  2557.         CP      $02             ; is it VERIFY ?
  2558.         SCF                     ; prepare a set carry flag
  2559.         JR      NZ,L0800        ; skip to VR-CONT-3 if not
  2560.  
  2561.         AND     A               ; clear carry flag for VERIFY so that
  2562.                                 ; data is not loaded.
  2563.  
  2564. ;; VR-CONT-3
  2565. L0800:  LD      A,$FF           ; signal data block to be loaded
  2566.  
  2567. ; -----------------
  2568. ; Load a data block
  2569. ; -----------------
  2570. ; This routine is called from 3 places other than above to load a data block.
  2571. ; In all cases the accumulator is first set to $FF so the routine could be
  2572. ; called at the previous instruction.
  2573.  
  2574. ;; LD-BLOCK
  2575. L0802:  CALL    L0556           ; routine LD-BYTES
  2576.         RET     C               ; return if successful.
  2577.  
  2578.  
  2579. ;; REPORT-R
  2580. L0806:  RST     08H             ; ERROR-1
  2581.         DB    $1A             ; Error Report: Tape loading error
  2582.  
  2583. ; -------------------
  2584. ; Handle LOAD control
  2585. ; -------------------
  2586. ; This branch is taken when the command is LOAD with type 0, 1 or 2.
  2587.  
  2588. ;; LD-CONTRL
  2589. L0808:  LD      E,(IX+$0B)      ; fetch length of found data block
  2590.         LD      D,(IX+$0C)      ; from 2nd descriptor.
  2591.         PUSH    HL              ; save destination
  2592.         LD      A,H             ; test for zero
  2593.         OR      L               ;
  2594.         JR      NZ,L0819        ; forward if not to LD-CONT-1
  2595.  
  2596.         INC     DE              ; increase length
  2597.         INC     DE              ; for letter name
  2598.         INC     DE              ; and 16-bit length
  2599.         EX      DE,HL           ; length to HL,
  2600.         JR      L0825           ; forward to LD-CONT-2
  2601.  
  2602. ; ---
  2603.  
  2604. ;; LD-CONT-1
  2605. L0819:  LD      L,(IX-$06)      ; fetch length from
  2606.         LD      H,(IX-$05)      ; the first header.
  2607.         EX      DE,HL           ;
  2608.         SCF                     ; set carry flag
  2609.         SBC     HL,DE           ;
  2610.         JR      C,L082E         ; to LD-DATA
  2611.  
  2612. ;; LD-CONT-2
  2613. L0825:  LD      DE,$0005        ; allow overhead of five bytes.
  2614.         ADD     HL,DE           ; add in the difference in data lengths.
  2615.         LD      B,H             ; transfer to
  2616.         LD      C,L             ; the BC register pair
  2617.         CALL    L1F05           ; routine TEST-ROOM fails if not enough room.
  2618.  
  2619. ;; LD-DATA
  2620. L082E:  POP     HL              ; pop destination
  2621.         LD      A,(IX+$00)      ; fetch type 0, 1 or 2.
  2622.         AND     A               ; test for program and variables.
  2623.         JR      Z,L0873         ; forward if so to LD-PROG
  2624.  
  2625. ; the type is a numeric or string array.
  2626.  
  2627.         LD      A,H             ; test the destination for zero
  2628.         OR      L               ; indicating variable does not already exist.
  2629.         JR      Z,L084C         ; forward if so to LD-DATA-1
  2630.  
  2631. ; else the destination is the first dimension within the array structure
  2632.  
  2633.         DEC     HL              ; address high byte of total length
  2634.         LD      B,(HL)          ; transfer to B.
  2635.         DEC     HL              ; address low byte of total length.
  2636.         LD      C,(HL)          ; transfer to C.
  2637.         DEC     HL              ; point to letter of variable.
  2638.         INC     BC              ; adjust length to
  2639.         INC     BC              ; include these
  2640.         INC     BC              ; three bytes also.
  2641.         LD      ($5C5F),IX      ; save header pointer in X_PTR.
  2642.         CALL    L19E8           ; routine RECLAIM-2 reclaims the old variable
  2643.                                 ; sliding workspace including the two headers
  2644.                                 ; downwards.
  2645.         LD      IX,($5C5F)      ; reload IX from X_PTR which will have been
  2646.                                 ; adjusted down by POINTERS routine.
  2647.  
  2648. ;; LD-DATA-1
  2649. L084C:  LD      HL,($5C59)      ; address E_LINE
  2650.         DEC     HL              ; now point to the $80 variables end-marker.
  2651.         LD      C,(IX+$0B)      ; fetch new data length
  2652.         LD      B,(IX+$0C)      ; from 2nd header.
  2653.         PUSH    BC              ; * save it.
  2654.         INC     BC              ; adjust the
  2655.         INC     BC              ; length to include
  2656.         INC     BC              ; letter name and total length.
  2657.         LD      A,(IX-$03)      ; fetch letter name from old header.
  2658.         PUSH    AF              ; preserve accumulator though not corrupted.
  2659.  
  2660.         CALL    L1655           ; routine MAKE-ROOM creates space for variable
  2661.                                 ; sliding workspace up. IX no longer addresses
  2662.                                 ; anywhere meaningful.
  2663.         INC     HL              ; point to first new location.
  2664.  
  2665.         POP     AF              ; fetch back the letter name.
  2666.         LD      (HL),A          ; place in first new location.
  2667.         POP     DE              ; * pop the data length.
  2668.         INC     HL              ; address 2nd location
  2669.         LD      (HL),E          ; store low byte of length.
  2670.         INC     HL              ; address next.
  2671.         LD      (HL),D          ; store high byte.
  2672.         INC     HL              ; address start of data.
  2673.         PUSH    HL              ; transfer address
  2674.         POP     IX              ; to IX register pair.
  2675.         SCF                     ; set carry flag indicating load not verify.
  2676.         LD      A,$FF           ; signal data not header.
  2677.         JP      L0802           ; JUMP back to LD-BLOCK
  2678.  
  2679. ; -----------------
  2680. ; the branch is here when a program as opposed to an array is to be loaded.
  2681.  
  2682. ;; LD-PROG
  2683. L0873:  EX      DE,HL           ; transfer dest to DE.
  2684.         LD      HL,($5C59)      ; address E_LINE
  2685.         DEC     HL              ; now variables end-marker.
  2686.         LD      ($5C5F),IX      ; place the IX header pointer in X_PTR
  2687.         LD      C,(IX+$0B)      ; get new length
  2688.         LD      B,(IX+$0C)      ; from 2nd header
  2689.         PUSH    BC              ; and save it.
  2690.  
  2691.         CALL    L19E5           ; routine RECLAIM-1 reclaims program and vars.
  2692.                                 ; adjusting X-PTR.
  2693.  
  2694.         POP     BC              ; restore new length.
  2695.         PUSH    HL              ; * save start
  2696.         PUSH    BC              ; ** and length.
  2697.  
  2698.         CALL    L1655           ; routine MAKE-ROOM creates the space.
  2699.  
  2700.         LD      IX,($5C5F)      ; reload IX from adjusted X_PTR
  2701.         INC     HL              ; point to start of new area.
  2702.         LD      C,(IX+$0F)      ; fetch length of BASIC on tape
  2703.         LD      B,(IX+$10)      ; from 2nd descriptor
  2704.         ADD     HL,BC           ; add to address the start of variables.
  2705.         LD      ($5C4B),HL      ; set system variable VARS
  2706.  
  2707.         LD      H,(IX+$0E)      ; fetch high byte of autostart line number.
  2708.         LD      A,H             ; transfer to A
  2709.         AND     $C0             ; test if greater than $3F.
  2710.         JR      NZ,L08AD        ; forward to LD-PROG-1 if so with no autostart.
  2711.  
  2712.         LD      L,(IX+$0D)      ; else fetch the low byte.
  2713.         LD      ($5C42),HL      ; set sytem variable to line number NEWPPC
  2714.         LD      (IY+$0A),$00    ; set statement NSPPC to zero.
  2715.  
  2716. ;; LD-PROG-1
  2717. L08AD:  POP     DE              ; ** pop the length
  2718.         POP     IX              ; * and start.
  2719.         SCF                     ; set carry flag
  2720.         LD      A,$FF           ; signal data as opposed to a header.
  2721.         JP      L0802           ; jump back to LD-BLOCK
  2722.  
  2723. ; --------------------
  2724. ; Handle MERGE control
  2725. ; --------------------
  2726. ; the branch was here to merge a program and its variables or an array.
  2727. ;
  2728.  
  2729. ;; ME-CONTRL
  2730. L08B6:  LD      C,(IX+$0B)      ; fetch length
  2731.         LD      B,(IX+$0C)      ; of data block on tape.
  2732.         PUSH    BC              ; save it.
  2733.         INC     BC              ; one for the pot.
  2734.  
  2735.         RST     30H             ; BC-SPACES creates room in workspace.
  2736.                                 ; HL addresses last new location.
  2737.         LD      (HL),$80        ; place end-marker at end.
  2738.         EX      DE,HL           ; transfer first location to HL.
  2739.         POP     DE              ; restore length to DE.
  2740.         PUSH    HL              ; save start.
  2741.  
  2742.         PUSH    HL              ; and transfer it
  2743.         POP     IX              ; to IX register.
  2744.         SCF                     ; set carry flag to load data on tape.
  2745.         LD      A,$FF           ; signal data not a header.
  2746.         CALL    L0802           ; routine LD-BLOCK loads to workspace.
  2747.         POP     HL              ; restore first location in workspace to HL.
  2748. X08CE   LD      DE,($5C53)      ; set DE from system variable PROG.
  2749.  
  2750. ; now enter a loop to merge the data block in workspace with the program and
  2751. ; variables.
  2752.  
  2753. ;; ME-NEW-LP
  2754. L08D2:  LD      A,(HL)          ; fetch next byte from workspace.
  2755.         AND     $C0             ; compare with $3F.
  2756.         JR      NZ,L08F0        ; forward to ME-VAR-LP if a variable or
  2757.                                 ; end-marker.
  2758.  
  2759. ; continue when HL addresses a BASIC line number.
  2760.  
  2761. ;; ME-OLD-LP
  2762. L08D7:  LD      A,(DE)          ; fetch high byte from program area.
  2763.         INC     DE              ; bump prog address.
  2764.         CP      (HL)            ; compare with that in workspace.
  2765.         INC     HL              ; bump workspace address.
  2766.         JR      NZ,L08DF        ; forward to ME-OLD-L1 if high bytes don't match
  2767.  
  2768.         LD      A,(DE)          ; fetch the low byte of program line number.
  2769.         CP      (HL)            ; compare with that in workspace.
  2770.  
  2771. ;; ME-OLD-L1
  2772. L08DF:  DEC     DE              ; point to start of
  2773.         DEC     HL              ; respective lines again.
  2774.         JR      NC,L08EB        ; forward to ME-NEW-L2 if line number in
  2775.                                 ; workspace is less than or equal to current
  2776.                                 ; program line as has to be added to program.
  2777.  
  2778.         PUSH    HL              ; else save workspace pointer.
  2779.         EX      DE,HL           ; transfer prog pointer to HL
  2780.         CALL    L19B8           ; routine NEXT-ONE finds next line in DE.
  2781.         POP     HL              ; restore workspace pointer
  2782.         JR      L08D7           ; back to ME-OLD-LP until destination position
  2783.                                 ; in program area found.
  2784.  
  2785. ; ---
  2786. ; the branch was here with an insertion or replacement point.
  2787.  
  2788. ;; ME-NEW-L2
  2789. L08EB:  CALL    L092C           ; routine ME-ENTER enters the line
  2790.         JR      L08D2           ; loop back to ME-NEW-LP.
  2791.  
  2792. ; ---
  2793. ; the branch was here when the location in workspace held a variable.
  2794.  
  2795. ;; ME-VAR-LP
  2796. L08F0:  LD      A,(HL)          ; fetch first byte of workspace variable.
  2797.         LD      C,A             ; copy to C also.
  2798.         CP      $80             ; is it the end-marker ?
  2799.         RET     Z               ; return if so as complete.  >>>>>
  2800.  
  2801.         PUSH    HL              ; save workspace area pointer.
  2802.         LD      HL,($5C4B)      ; load HL with VARS - start of variables area.
  2803.  
  2804. ;; ME-OLD-VP
  2805. L08F9:  LD      A,(HL)          ; fetch first byte.
  2806.         CP      $80             ; is it the end-marker ?
  2807.         JR      Z,L0923         ; forward if so to ME-VAR-L2 to add
  2808.                                 ; variable at end of variables area.
  2809.  
  2810.         CP      C               ; compare with variable in workspace area.
  2811.         JR      Z,L0909         ; forward to ME-OLD-V2 if a match to replace.
  2812.  
  2813. ; else entire variables area has to be searched.
  2814.  
  2815. ;; ME-OLD-V1
  2816. L0901:  PUSH    BC              ; save character in C.
  2817.         CALL    L19B8           ; routine NEXT-ONE gets following variable
  2818.                                 ; address in DE.
  2819.         POP     BC              ; restore character in C
  2820.         EX      DE,HL           ; transfer next address to HL.
  2821.         JR      L08F9           ; loop back to ME-OLD-VP
  2822.  
  2823. ; ---
  2824. ; the branch was here when first characters of name matched.
  2825.  
  2826. ;; ME-OLD-V2
  2827. L0909:  AND     $E0             ; keep bits 11100000
  2828.         CP      $A0             ; compare   10100000 - a long-named variable.
  2829.  
  2830.         JR      NZ,L0921        ; forward to ME-VAR-L1 if just one-character.
  2831.  
  2832. ; but long-named variables have to be matched character by character.
  2833.  
  2834.         POP     DE              ; fetch workspace 1st character pointer
  2835.         PUSH    DE              ; and save it on the stack again.
  2836.         PUSH    HL              ; save variables area pointer on stack.
  2837.  
  2838. ;; ME-OLD-V3
  2839. L0912:  INC     HL              ; address next character in vars area.
  2840.         INC     DE              ; address next character in workspace area.
  2841.         LD      A,(DE)          ; fetch workspace character.
  2842.         CP      (HL)            ; compare to variables character.
  2843.         JR      NZ,L091E        ; forward to ME-OLD-V4 with a mismatch.
  2844.  
  2845.         RLA                     ; test if the terminal inverted character.
  2846.         JR      NC,L0912        ; loop back to ME-OLD-V3 if more to test.
  2847.  
  2848. ; otherwise the long name matches in its entirety.
  2849.  
  2850.         POP     HL              ; restore pointer to first character of variable
  2851.         JR      L0921           ; forward to ME-VAR-L1
  2852.  
  2853. ; ---
  2854. ; the branch is here when two characters don't match
  2855.  
  2856. ;; ME-OLD-V4
  2857. L091E:  POP     HL              ; restore the prog/vars pointer.
  2858.         JR      L0901           ; back to ME-OLD-V1 to resume search.
  2859.  
  2860. ; ---
  2861. ; branch here when variable is to replace an existing one
  2862.  
  2863. ;; ME-VAR-L1
  2864. L0921:  LD      A,$FF           ; indicate a replacement.
  2865.  
  2866. ; this entry point is when A holds $80 indicating a new variable.
  2867.  
  2868. ;; ME-VAR-L2
  2869. L0923:  POP     DE              ; pop workspace pointer.
  2870.         EX      DE,HL           ; now make HL workspace pointer, DE vars pointer
  2871.         INC     A               ; zero flag set if replacement.
  2872.         SCF                     ; set carry flag indicating a variable not a
  2873.                                 ; program line.
  2874.         CALL    L092C           ; routine ME-ENTER copies variable in.
  2875.         JR      L08F0           ; loop back to ME-VAR-LP
  2876.  
  2877. ; ------------------------
  2878. ; Merge a Line or Variable
  2879. ; ------------------------
  2880. ; A BASIC line or variable is inserted at the current point. If the line numbers
  2881. ; or variable names match (zero flag set) then a replacement takes place.
  2882.  
  2883. ;; ME-ENTER
  2884. L092C:  JR      NZ,L093E        ; forward to ME-ENT-1 for insertion only.
  2885.  
  2886. ; but the program line or variable matches so old one is reclaimed.
  2887.  
  2888.         EX      AF,AF'          ; save flag??
  2889.        LD      ($5C5F),HL      ; preserve workspace pointer in dynamic X_PTR
  2890.        EX      DE,HL           ; transfer program dest pointer to HL.
  2891.        CALL    L19B8           ; routine NEXT-ONE finds following location
  2892.                                ; in program or variables area.
  2893.        CALL    L19E8           ; routine RECLAIM-2 reclaims the space between.
  2894.        EX      DE,HL           ; transfer program dest pointer back to DE.
  2895.        LD      HL,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2896.        EX      AF,AF'          ; restore flags.
  2897.  
  2898. ; now the new line or variable is entered.
  2899.  
  2900. ;; ME-ENT-1
  2901. L093E:  EX      AF,AF'          ; save or re-save flags.
  2902.        PUSH    DE              ; save dest pointer in prog/vars area.
  2903.        CALL    L19B8           ; routine NEXT-ONE finds next in workspace.
  2904.                                ; gets next in DE, difference in BC.
  2905.                                ; prev addr in HL
  2906.        LD      ($5C5F),HL      ; store pointer in X_PTR
  2907.        LD      HL,($5C53)      ; load HL from system variable PROG
  2908.        EX      (SP),HL         ; swap with prog/vars pointer on stack.
  2909.        PUSH    BC              ; ** save length of new program line/variable.
  2910.        EX      AF,AF'          ; fetch flags back.
  2911.         JR      C,L0955         ; skip to ME-ENT-2 if variable
  2912.  
  2913.         DEC     HL              ; address location before pointer
  2914.         CALL    L1655           ; routine MAKE-ROOM creates room for BASIC line
  2915.         INC     HL              ; address next.
  2916.         JR      L0958           ; forward to ME-ENT-3
  2917.  
  2918. ; ---
  2919.  
  2920. ;; ME-ENT-2
  2921. L0955:  CALL    L1655           ; routine MAKE-ROOM creates room for variable.
  2922.  
  2923. ;; ME-ENT-3
  2924. L0958:  INC     HL              ; address next?
  2925.  
  2926.         POP     BC              ; ** pop length
  2927.         POP     DE              ; * pop value for PROG which may have been
  2928.                                 ; altered by POINTERS if first line.
  2929.         LD      ($5C53),DE      ; set PROG to original value.
  2930.         LD      DE,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2931.         PUSH    BC              ; save length
  2932.         PUSH    DE              ; and workspace pointer
  2933.         EX      DE,HL           ; make workspace pointer source, prog/vars
  2934.                                 ; pointer the destination
  2935.         LDIR                    ; copy bytes of line or variable into new area.
  2936.         POP     HL              ; restore workspace pointer.
  2937.         POP     BC              ; restore length.
  2938.         PUSH    DE              ; save new prog/vars pointer.
  2939.         CALL    L19E8           ; routine RECLAIM-2 reclaims the space used
  2940.                                 ; by the line or variable in workspace block
  2941.                                 ; as no longer required and space could be
  2942.                                 ; useful for adding more lines.
  2943.         POP     DE              ; restore the prog/vars pointer
  2944.         RET                     ; return.
  2945.  
  2946. ; -------------------
  2947. ; Handle SAVE control
  2948. ; -------------------
  2949. ; A branch from the main SAVE-ETC routine at SAVE-ALL.
  2950. ; First the header data is saved. Then after a wait of 1 second
  2951. ; the data itself is saved.
  2952. ; HL points to start of data.
  2953. ; IX points to start of descriptor.
  2954.  
  2955. ;; SA-CONTRL
  2956. L0970:  PUSH    HL              ; save start of data
  2957.  
  2958.         LD      A,$FD           ; select system channel 'S'
  2959.         CALL    L1601           ; routine CHAN-OPEN
  2960.  
  2961.         XOR     A               ; clear to address table directly
  2962.         LD      DE,L09A1        ; address: tape-msgs
  2963.         CALL    L0C0A           ; routine PO-MSG -
  2964.                                 ; 'Start tape then press any key.'
  2965.  
  2966.         SET     5,(IY+$02)      ; TV_FLAG  - Signal lower screen requires
  2967.                                 ; clearing
  2968.         CALL    L15D4           ; routine WAIT-KEY
  2969.  
  2970.         PUSH    IX              ; save pointer to descriptor.
  2971.         LD      DE,$0011        ; there are seventeen bytes.
  2972.         XOR     A               ; signal a header.
  2973.         CALL    L04C2           ; routine SA-BYTES
  2974.  
  2975.         POP     IX              ; restore descriptor pointer.
  2976.  
  2977.         LD      B,$32           ; wait for a second - 50 interrupts.
  2978.  
  2979. ;; SA-1-SEC
  2980. L0991:  HALT                    ; wait for interrupt
  2981.         DJNZ    L0991           ; back to SA-1-SEC until pause complete.
  2982.  
  2983.         LD      E,(IX+$0B)      ; fetch length of bytes from the
  2984.         LD      D,(IX+$0C)      ; descriptor.
  2985.  
  2986.         LD      A,$FF           ; signal data bytes.
  2987.  
  2988.         POP     IX              ; retrieve pointer to start
  2989.         JP      L04C2           ; jump back to SA-BYTES
  2990.  
  2991.  
  2992. ; Arrangement of two headers in workspace.
  2993. ; Originally IX addresses first location and only one header is required
  2994. ; when saving.
  2995. ;
  2996. ;   OLD     NEW         PROG   DATA  DATA  CODE
  2997. ;   HEADER  HEADER             num   chr          NOTES.
  2998. ;   ------  ------      ----   ----  ----  ----   -----------------------------
  2999. ;   IX-$11  IX+$00      0      1     2     3      Type.
  3000. ;   IX-$10  IX+$01      x      x     x     x      F  ($FF if filename is null).
  3001. ;   IX-$0F  IX+$02      x      x     x     x      i
  3002. ;   IX-$0E  IX+$03      x      x     x     x      l
  3003. ;   IX-$0D  IX+$04      x      x     x     x      e
  3004. ;   IX-$0C  IX+$05      x      x     x     x      n
  3005. ;   IX-$0B  IX+$06      x      x     x     x      a
  3006. ;   IX-$0A  IX+$07      x      x     x     x      m
  3007. ;   IX-$09  IX+$08      x      x     x     x      e
  3008. ;   IX-$08  IX+$09      x      x     x     x      .
  3009. ;   IX-$07  IX+$0A      x      x     x     x      (terminal spaces).
  3010. ;   IX-$06  IX+$0B      lo     lo    lo    lo     Total  
  3011. ;   IX-$05  IX+$0C      hi     hi    hi    hi     Length of datablock.
  3012. ;   IX-$04  IX+$0D      Auto   -     -     Start  Various
  3013. ;   IX-$03  IX+$0E      Start  a-z   a-z   addr   ($80 if no autostart).
  3014. ;   IX-$02  IX+$0F      lo     -     -     -      Length of Program
  3015. ;   IX-$01  IX+$10      hi     -     -     -      only i.e. without variables.
  3016. ;
  3017.  
  3018.  
  3019. ; ------------------------
  3020. ; Canned cassette messages
  3021. ; ------------------------
  3022. ; The last-character-inverted Cassette messages.
  3023. ; Starts with normal initial step-over byte.
  3024.  
  3025. ;; tape-msgs
  3026. L09A1           DB $80
  3027.                 DC "Start tape, then press any key."    ;DEFM    "Start tape, then press any key"
  3028. L09C0           EQU $-1                                 ;DB    '.'+$80
  3029.                 DB $0D
  3030.                 DC "Program: "                          ;DEFM    "Program:"
  3031.                                                         ;DB    ' '+$80
  3032.                 DB $0D
  3033.                 DC "Number array: "                     ;DEFM    "Number array:"
  3034.                                                         ;DB    ' '+$80
  3035.                 DB $0D
  3036.                 DC "Character array: "                  ;DEFM    "Character array:"
  3037.                                                         ;DB    ' '+$80
  3038.                 DB $0D
  3039.                 DC "Bytes: "                            ;DEFM    "Bytes:"
  3040.                                                         ;DB    ' '+$80
  3041. ;               DB    ' '+$80
  3042.  
  3043.  
  3044. ;**************************************************
  3045. ;** Part 5. SCREEN AND PRINTER HANDLING ROUTINES **
  3046. ;**************************************************
  3047.  
  3048. ; ---------------------
  3049. ; General PRINT routine
  3050. ; ---------------------
  3051. ; This is the routine most often used by the RST 10 restart although the
  3052. ; subroutine is on two occasions called directly when it is known that
  3053. ; output will definitely be to the lower screen.
  3054.  
  3055. ;; PRINT-OUT
  3056. L09F4:  CALL    L0B03           ; routine PO-FETCH fetches print position
  3057.                                 ; to HL register pair.
  3058.         CP      $20             ; is character a space or higher ?
  3059.         JP      NC,L0AD9        ; jump forward to PO-ABLE if so.
  3060.  
  3061.         CP      $06             ; is character in range 00-05 ?
  3062.         JR      C,L0A69         ; to PO-QUEST to print '?' if so.
  3063.  
  3064.         CP      $18             ; is character in range 24d - 31d ?
  3065.         JR      NC,L0A69        ; to PO-QUEST to also print '?' if so.
  3066.  
  3067.         LD      HL,L0A11 - 6    ; address 0A0B - the base address of control
  3068.                                 ; character table - where zero would be.
  3069.         LD      E,A             ; control character 06 - 23d
  3070.         LD      D,$00           ; is transferred to DE.
  3071.  
  3072.         ADD     HL,DE           ; index into table.
  3073.  
  3074.         LD      E,(HL)          ; fetch the offset to routine.
  3075.         ADD     HL,DE           ; add to make HL the address.
  3076.         PUSH    HL              ; push the address.
  3077.         JP      L0B03           ; to PO-FETCH, as the screen/printer position
  3078.                                 ; has been disturbed, and indirectly to
  3079.                                 ; routine on stack.
  3080.  
  3081. ; -----------------------
  3082. ; Control character table
  3083. ; -----------------------
  3084. ; For control characters in the range 6 - 23d the following table
  3085. ; is indexed to provide an offset to the handling routine that
  3086. ; follows the table.
  3087.  
  3088. ;; ctlchrtab
  3089. L0A11:  DB    L0A5F - $       ; 06d offset $4E to Address: PO-COMMA
  3090.         DB    L0A69 - $       ; 07d offset $57 to Address: PO-QUEST
  3091.         DB    L0A23 - $       ; 08d offset $10 to Address: PO-BACK-1
  3092.         DB    L0A3D - $       ; 09d offset $29 to Address: PO-RIGHT
  3093.         DB    L0A69 - $       ; 10d offset $54 to Address: PO-QUEST
  3094.         DB    L0A69 - $       ; 11d offset $53 to Address: PO-QUEST
  3095.         DB    L0A69 - $       ; 12d offset $52 to Address: PO-QUEST
  3096.         DB    L0A4F - $       ; 13d offset $37 to Address: PO-ENTER
  3097.         DB    L0A69 - $       ; 14d offset $50 to Address: PO-QUEST
  3098.         DB    L0A69 - $       ; 15d offset $4F to Address: PO-QUEST
  3099.         DB    L0A7A - $       ; 16d offset $5F to Address: PO-1-OPER
  3100.         DB    L0A7A - $       ; 17d offset $5E to Address: PO-1-OPER
  3101.         DB    L0A7A - $       ; 18d offset $5D to Address: PO-1-OPER
  3102.         DB    L0A7A - $       ; 19d offset $5C to Address: PO-1-OPER
  3103.         DB    L0A7A - $       ; 20d offset $5B to Address: PO-1-OPER
  3104.         DB    L0A7A - $       ; 21d offset $5A to Address: PO-1-OPER
  3105.         DB    L0A75 - $       ; 22d offset $54 to Address: PO-2-OPER
  3106.         DB    L0A75 - $       ; 23d offset $53 to Address: PO-2-OPER
  3107.  
  3108.  
  3109. ; -------------------
  3110. ; Cursor left routine
  3111. ; -------------------
  3112. ; Backspace and up a line if that action is from the left of screen.
  3113. ; For ZX printer backspace up to first column but not beyond.
  3114.  
  3115. ;; PO-BACK-1
  3116. L0A23:  INC     C               ; move left one column.
  3117.         LD      A,$22           ; value $21 is leftmost column.
  3118.         CP      C               ; have we passed ?
  3119.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3120.  
  3121.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3122.         JR      NZ,L0A38        ; to PO-BACK-2 if so, as we are unable to
  3123.                                 ; backspace from the leftmost position.
  3124.  
  3125.  
  3126.         INC     B               ; move up one screen line
  3127.         LD      C,$02           ; the rightmost column position.
  3128.         LD      A,$18           ; Note. This should be $19
  3129.                                 ; credit. Dr. Frank O'Hara, 1982
  3130.  
  3131.         CP      B               ; has position moved past top of screen ?
  3132.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3133.  
  3134.         DEC     B               ; else back to $18.
  3135.  
  3136. ;; PO-BACK-2
  3137. L0A38:  LD      C,$21           ; the leftmost column position.
  3138.  
  3139. ;; PO-BACK-3
  3140. L0A3A:  JP      L0DD9           ; to CL-SET and PO-STORE to save new
  3141.                                 ; position in system variables.
  3142.  
  3143. ; --------------------
  3144. ; Cursor right routine
  3145. ; --------------------
  3146. ; This moves the print position to the right leaving a trail in the
  3147. ; current background colour.
  3148. ; "However the programmer has failed to store the new print position
  3149. ;  so CHR$ 9 will only work if the next print position is at a newly
  3150. ;  defined place.
  3151. ;   e.g. PRINT PAPER 2; CHR$ 9; AT 4,0;
  3152. ;  does work but is not very helpful"
  3153. ; - Dr. Ian Logan, Understanding Your Spectrum, 1982.
  3154.  
  3155. ;; PO-RIGHT
  3156. L0A3D:  LD      A,($5C91)       ; fetch P_FLAG value
  3157.         PUSH    AF              ; and save it on stack.
  3158.  
  3159.         LD      (IY+$57),$01    ; temporarily set P_FLAG 'OVER 1'.
  3160.         LD      A,$20           ; prepare a space.
  3161.         CALL    L0B65           ; routine PO-CHAR to print it.
  3162.                                 ; Note. could be PO-ABLE which would update
  3163.                                 ; the column position.
  3164.  
  3165.         POP     AF              ; restore the permanent flag.
  3166.         LD      ($5C91),A       ; and restore system variable P_FLAG
  3167.  
  3168.         RET                     ; return without updating column position
  3169.  
  3170. ; -----------------------
  3171. ; Perform carriage return
  3172. ; -----------------------
  3173. ; A carriage return is 'printed' to screen or printer buffer.
  3174.  
  3175. ;; PO-ENTER
  3176. L0A4F:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3177.         JP      NZ,L0ECD        ; to COPY-BUFF if so, to flush buffer and reset
  3178.                                 ; the print position.
  3179.  
  3180.         LD      C,$21           ; the leftmost column position.
  3181.         CALL    L0C55           ; routine PO-SCR handles any scrolling required.
  3182.         DEC     B               ; to next screen line.
  3183.         JP      L0DD9           ; jump forward to CL-SET to store new position.
  3184.  
  3185. ; -----------
  3186. ; Print comma
  3187. ; -----------
  3188. ; The comma control character. The 32 column screen has two 16 character
  3189. ; tabstops.  The routine is only reached via the control character table.
  3190.  
  3191. ;; PO-COMMA
  3192. L0A5F:  CALL    L0B03           ; routine PO-FETCH - seems unnecessary.
  3193.  
  3194.         LD      A,C             ; the column position. $21-$01
  3195.         DEC     A               ; move right. $20-$00
  3196.         DEC     A               ; and again   $1F-$00 or $FF if trailing
  3197.         AND     $10             ; will be $00 or $10.
  3198.         JR      L0AC3           ; forward to PO-FILL
  3199.  
  3200. ; -------------------
  3201. ; Print question mark
  3202. ; -------------------
  3203. ; This routine prints a question mark which is commonly
  3204. ; used to print an unassigned control character in range 0-31d.
  3205. ; there are a surprising number yet to be assigned.
  3206.  
  3207. ;; PO-QUEST
  3208. L0A69:  LD      A,$3F           ; prepare the character '?'.
  3209.         JR      L0AD9           ; forward to PO-ABLE.
  3210.  
  3211. ; --------------------------------
  3212. ; Control characters with operands
  3213. ; --------------------------------
  3214. ; Certain control characters are followed by 1 or 2 operands.
  3215. ; The entry points from control character table are PO-2-OPER and PO-1-OPER.
  3216. ; The routines alter the output address of the current channel so that
  3217. ; subsequent RST $10 instructions take the appropriate action
  3218. ; before finally resetting the output address back to PRINT-OUT.
  3219.  
  3220. ;; PO-TV-2
  3221. L0A6D:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3222.         LD      ($5C0F),A       ; store first operand in TVDATA-hi
  3223.         JR      L0A80           ; forward to PO-CHANGE >>
  3224.  
  3225. ; ---
  3226.  
  3227. ; -> This initial entry point deals with two operands - AT or TAB.
  3228.  
  3229. ;; PO-2-OPER
  3230. L0A75:  LD      DE,L0A6D        ; address: PO-TV-2 will be next output routine
  3231.         JR      L0A7D           ; forward to PO-TV-1
  3232.  
  3233. ; ---
  3234.  
  3235. ; -> This initial entry point deals with one operand INK to OVER.
  3236.  
  3237. ;; PO-1-OPER
  3238. L0A7A:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3239.  
  3240. ;; PO-TV-1
  3241. L0A7D:  LD      ($5C0E),A       ; store control code in TVDATA-lo
  3242.  
  3243. ;; PO-CHANGE
  3244. L0A80:  LD      HL,($5C51)      ; use CURCHL to find current output channel.
  3245.         LD      (HL),E          ; make it
  3246.         INC     HL              ; the supplied
  3247.         LD      (HL),D          ; address from DE.
  3248.         RET                     ; return.
  3249.  
  3250. ; ---
  3251.  
  3252. ;; PO-CONT
  3253. L0A87:  LD      DE,L09F4        ; Address: PRINT-OUT
  3254.         CALL    L0A80           ; routine PO-CHANGE to restore normal channel.
  3255.         LD      HL,($5C0E)      ; TVDATA gives control code and possible
  3256.                                 ; subsequent character
  3257.         LD      D,A             ; save current character
  3258.         LD      A,L             ; the stored control code
  3259.         CP      $16             ; was it INK to OVER (1 operand) ?
  3260.         JP      C,L2211         ; to CO-TEMP-5
  3261.  
  3262.         JR      NZ,L0AC2        ; to PO-TAB if not 22d i.e. 23d TAB.
  3263.  
  3264.                                 ; else must have been 22d AT.
  3265.         LD      B,H             ; line to H   (0-23d)
  3266.         LD      C,D             ; column to C (0-31d)
  3267.         LD      A,$1F           ; the value 31d
  3268.         SUB     C               ; reverse the column number.
  3269.         JR      C,L0AAC         ; to PO-AT-ERR if C was greater than 31d.
  3270.  
  3271.         ADD     A,$02           ; transform to system range $02-$21
  3272.         LD      C,A             ; and place in column register.
  3273.  
  3274.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3275.         JR      NZ,L0ABF        ; to PO-AT-SET as line can be ignored.
  3276.  
  3277.         LD      A,$16           ; 22 decimal
  3278.         SUB     B               ; subtract line number to reverse
  3279.                                 ; 0 - 22 becomes 22 - 0.
  3280.  
  3281. ;; PO-AT-ERR
  3282. L0AAC:  JP      C,L1E9F         ; to REPORT-B if higher than 22 decimal
  3283.                                 ; Integer out of range.
  3284.  
  3285.         INC     A               ; adjust for system range $01-$17
  3286.         LD      B,A             ; place in line register
  3287.         INC     B               ; adjust to system range  $02-$18
  3288.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3289.         JP      NZ,L0C55        ; exit to PO-SCR to test for scrolling
  3290.  
  3291.         CP      (IY+$31)        ; Compare against DF_SZ
  3292.         JP      C,L0C86         ; to REPORT-5 if too low
  3293.                                 ; Out of screen.
  3294.  
  3295. ;; PO-AT-SET
  3296. L0ABF:  JP      L0DD9           ; print position is valid so exit via CL-SET
  3297.  
  3298. ; Continue here when dealing with TAB.
  3299. ; Note. In BASIC, TAB is followed by a 16-bit number and was initially
  3300. ; designed to work with any output device.
  3301.  
  3302. ;; PO-TAB
  3303. L0AC2:  LD      A,H             ; transfer parameter to A
  3304.                                 ; Losing current character -
  3305.                                 ; High byte of TAB parameter.
  3306.  
  3307.  
  3308. ;; PO-FILL
  3309. L0AC3:  CALL    L0B03           ; routine PO-FETCH, HL-addr, BC=line/column.
  3310.                                 ; column 1 (right), $21 (left)
  3311.         ADD     A,C             ; add operand to current column
  3312.         DEC     A               ; range 0 - 31+
  3313.         AND     $1F             ; make range 0 - 31d
  3314.         RET     Z               ; return if result zero
  3315.  
  3316.         LD      D,A             ; Counter to D
  3317.         SET     0,(IY+$01)      ; update FLAGS  - signal suppress leading space.
  3318.  
  3319. ;; PO-SPACE
  3320. L0AD0:  LD      A,$20           ; space character.
  3321.         CALL    L0C3B           ; routine PO-SAVE prints the character
  3322.                                 ; using alternate set (normal output routine)
  3323.         DEC     D               ; decrement counter.
  3324.         JR      NZ,L0AD0        ; to PO-SPACE until done
  3325.  
  3326.         RET                     ; return
  3327.  
  3328. ; ----------------------
  3329. ; Printable character(s)
  3330. ; ----------------------
  3331. ; This routine prints printable characters and continues into
  3332. ; the position store routine
  3333.  
  3334. ;; PO-ABLE
  3335. L0AD9:  CALL    L0B24           ; routine PO-ANY
  3336.                                 ; and continue into position store routine.
  3337.  
  3338. ; -------------------------------------
  3339. ; Store line, column, and pixel address
  3340. ; -------------------------------------
  3341. ; This routine updates the system variables associated with
  3342. ; The main screen, lower screen/input buffer or ZX printer.
  3343.  
  3344. ;; PO-STORE
  3345. L0ADC:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use ?
  3346.         JR      NZ,L0AFC        ; to PO-ST-PR if so
  3347.  
  3348.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3349.         JR      NZ,L0AF0        ; to PO-ST-E if so
  3350.  
  3351.         LD      ($5C88),BC      ; S_POSN line/column upper screen
  3352.         LD      ($5C84),HL      ; DF_CC  display file address
  3353.         RET                     ;
  3354.  
  3355. ; ---
  3356.  
  3357. ;; PO-ST-E
  3358. L0AF0:  LD      ($5C8A),BC      ; SPOSNL line/column lower screen
  3359.         LD      ($5C82),BC      ; ECHO_E line/column input buffer
  3360.         LD      ($5C86),HL      ; DFCCL  lower screen memory address
  3361.         RET                     ;
  3362.  
  3363. ; ---
  3364.  
  3365. ;; PO-ST-PR
  3366. L0AFC:  LD      (IY+$45),C      ; P_POSN column position printer
  3367.         LD      ($5C80),HL      ; PR_CC  full printer buffer memory address
  3368.         RET                     ;
  3369.  
  3370. ; -------------------------
  3371. ; Fetch position parameters
  3372. ; -------------------------
  3373. ; This routine fetches the line/column and display file address
  3374. ; of the upper and lower screen or, if the printer is in use,
  3375. ; the column position and absolute memory address.
  3376. ; Note. that PR-CC-hi (23681) is used by this routine and the one above
  3377. ; and if, in accordance with the manual (that says this is unused), the
  3378. ; location has been used for other purposes, then subsequent output
  3379. ; to the printer buffer could corrupt a 256-byte section of memory.
  3380.  
  3381. ;; PO-FETCH
  3382. L0B03:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3383.         JR      NZ,L0B1D        ; to PO-F-PR if so
  3384.  
  3385.                                 ; assume upper screen
  3386.         LD      BC,($5C88)      ; S_POSN
  3387.         LD      HL,($5C84)      ; DF_CC display file address
  3388.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3389.         RET     Z               ; return if upper screen
  3390.  
  3391.                                 ; ah well, was lower screen
  3392.         LD      BC,($5C8A)      ; SPOSNL
  3393.         LD      HL,($5C86)      ; DFCCL
  3394.         RET                     ; return
  3395.  
  3396. ; ---
  3397.  
  3398. ;; PO-F-PR
  3399. L0B1D:  LD      C,(IY+$45)      ; P_POSN column only
  3400.         LD      HL,($5C80)      ; PR_CC printer buffer address
  3401.         RET                     ; return
  3402.  
  3403. ; -------------------
  3404. ; Print any character
  3405. ; -------------------
  3406. ; This routine is used to print any character in range 32d - 255d
  3407. ; It is only called from PO-ABLE which continues into PO-STORE
  3408.  
  3409. ;; PO-ANY
  3410. L0B24:  CP      $80             ; ASCII ?
  3411.         JR      C,L0B65         ; to PO-CHAR is so.
  3412.  
  3413.         CP      $90             ; test if a block graphic character.
  3414.         JR      NC,L0B52        ; to PO-T&UDG to print tokens and udg's
  3415.  
  3416. ; The 16 2*2 mosaic characters 128-143 decimal are formed from
  3417. ; bits 0-3 of the character.
  3418.  
  3419.         LD      B,A             ; save character
  3420.         CALL    L0B38           ; routine PO-GR-1 to construct top half
  3421.                                 ; then bottom half.
  3422.         CALL    L0B03           ; routine PO-FETCH fetches print position.
  3423.         LD      DE,$5C92        ; MEM-0 is location of 8 bytes of character
  3424.         JR      L0B7F           ; to PR-ALL to print to screen or printer
  3425.  
  3426. ; ---
  3427.  
  3428. ;; PO-GR-1
  3429. L0B38:  LD      HL,$5C92        ; address MEM-0 - a temporary buffer in
  3430.                                 ; systems variables which is normally used
  3431.                                 ; by the calculator.
  3432.         CALL    L0B3E           ; routine PO-GR-2 to construct top half
  3433.                                 ; and continue into routine to construct
  3434.                                 ; bottom half.
  3435.  
  3436. ;; PO-GR-2
  3437. L0B3E:  RR      B               ; rotate bit 0/2 to carry
  3438.         SBC     A,A             ; result $00 or $FF
  3439.         AND     $0F             ; mask off right hand side
  3440.         LD      C,A             ; store part in C
  3441.         RR      B               ; rotate bit 1/3 of original chr to carry
  3442.         SBC     A,A             ; result $00 or $FF
  3443.         AND     $F0             ; mask off left hand side
  3444.         OR      C               ; combine with stored pattern
  3445.         LD      C,$04           ; four bytes for top/bottom half
  3446.  
  3447. ;; PO-GR-3
  3448. L0B4C:  LD      (HL),A          ; store bit patterns in temporary buffer
  3449.         INC     HL              ; next address
  3450.         DEC     C               ; jump back to
  3451.         JR      NZ,L0B4C        ; to PO-GR-3 until byte is stored 4 times
  3452.  
  3453.         RET                     ; return
  3454.  
  3455. ; ---
  3456.  
  3457. ; Tokens and User defined graphics are now separated.
  3458.  
  3459. ;; PO-T&UDG
  3460. L0B52           IF BAS48_ONLY
  3461.                 SUB 0XA5
  3462.                 JR NC,L0B5F
  3463.                 ELSE
  3464.                 JP L3B9F                ;Spectrum 128 patch
  3465.                 NOP
  3466.                 ENDIF
  3467.  
  3468. L0B56:  ADD     A,$15           ; add 21d to restore to 0 - 20
  3469.         PUSH    BC              ; save current print position
  3470.         LD      BC,($5C7B)      ; fetch UDG to address bit patterns
  3471.         JR      L0B6A           ; to PO-CHAR-2 - common code to lay down
  3472.                                 ; a bit patterned character
  3473.  
  3474. ; ---
  3475.  
  3476. ;; PO-T
  3477. L0B5F:  CALL    L0C10           ; routine PO-TOKENS prints tokens
  3478.         JP      L0B03           ; exit via a JUMP to PO-FETCH as this routine
  3479.                                 ; must continue into PO-STORE.
  3480.                                 ; A JR instruction could be used.
  3481.  
  3482. ; This point is used to print ASCII characters  32d - 127d.
  3483.  
  3484. ;; PO-CHAR
  3485. L0B65:  PUSH    BC              ; save print position
  3486.         LD      BC,($5C36)      ; address CHARS
  3487.  
  3488. ; This common code is used to transfer the character bytes to memory.
  3489.  
  3490. ;; PO-CHAR-2
  3491. L0B6A:  EX      DE,HL           ; transfer destination address to DE
  3492.         LD      HL,$5C3B        ; point to FLAGS
  3493.         RES     0,(HL)          ; allow for leading space
  3494.         CP      $20             ; is it a space ?
  3495.         JR      NZ,L0B76        ; to PO-CHAR-3 if not
  3496.  
  3497.         SET     0,(HL)          ; signal no leading space to FLAGS
  3498.  
  3499. ;; PO-CHAR-3
  3500. L0B76:  LD      H,$00           ; set high byte to 0
  3501.         LD      L,A             ; character to A
  3502.                                 ; 0-21 UDG or 32-127 ASCII.
  3503.         ADD     HL,HL           ; multiply
  3504.         ADD     HL,HL           ; by
  3505.         ADD     HL,HL           ; eight
  3506.         ADD     HL,BC           ; HL now points to first byte of character
  3507.         POP     BC              ; the source address CHARS or UDG
  3508.         EX      DE,HL           ; character address to DE
  3509.  
  3510. ; --------------------
  3511. ; Print all characters
  3512. ; --------------------
  3513. ; This entry point entered from above to print ASCII and UDGs
  3514. ; but also from earlier to print mosaic characters.
  3515. ; HL=destination
  3516. ; DE=character source
  3517. ; BC=line/column
  3518.  
  3519. ;; PR-ALL
  3520. L0B7F:  LD      A,C             ; column to A
  3521.         DEC     A               ; move right
  3522.         LD      A,$21           ; pre-load with leftmost position
  3523.         JR      NZ,L0B93        ; but if not zero to PR-ALL-1
  3524.  
  3525.         DEC     B               ; down one line
  3526.         LD      C,A             ; load C with $21
  3527.         BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3528.         JR      Z,L0B93         ; to PR-ALL-1 if not
  3529.  
  3530.         PUSH    DE              ; save source address
  3531.         CALL    L0ECD           ; routine COPY-BUFF outputs line to printer
  3532.         POP     DE              ; restore character source address
  3533.         LD      A,C             ; the new column number ($21) to C
  3534.  
  3535. ;; PR-ALL-1
  3536. L0B93:  CP      C               ; this test is really for screen - new line ?
  3537.         PUSH    DE              ; save source
  3538.  
  3539.         CALL    Z,L0C55         ; routine PO-SCR considers scrolling
  3540.  
  3541.         POP     DE              ; restore source
  3542.         PUSH    BC              ; save line/column
  3543.         PUSH    HL              ; and destination
  3544.         LD      A,($5C91)       ; fetch P_FLAG to accumulator
  3545.         LD      B,$FF           ; prepare OVER mask in B.
  3546.         RRA                     ; bit 0 set if OVER 1
  3547.         JR      C,L0BA4         ; to PR-ALL-2
  3548.  
  3549.         INC     B               ; set OVER mask to 0
  3550.  
  3551. ;; PR-ALL-2
  3552. L0BA4:  RRA                     ; skip bit 1 of P_FLAG
  3553.         RRA                     ; bit 2 is INVERSE
  3554.         SBC     A,A             ; will be FF for INVERSE 1 else zero
  3555.         LD      C,A             ; transfer INVERSE mask to C
  3556.         LD      A,$08           ; prepare to count 8 bytes
  3557.         AND     A               ; clear carry to signal screen
  3558.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3559.         JR      Z,L0BB6         ; to PR-ALL-3 if screen
  3560.  
  3561.         SET     1,(IY+$30)      ; update FLAGS2  - signal printer buffer has
  3562.                                 ; been used.
  3563.         SCF                     ; set carry flag to signal printer.
  3564.  
  3565. ;; PR-ALL-3
  3566. L0BB6:  EX      DE,HL           ; now HL=source, DE=destination
  3567.  
  3568. ;; PR-ALL-4
  3569. L0BB7:  EX      AF,AF'          ; save printer/screen flag
  3570.        LD      A,(DE)          ; fetch existing destination byte
  3571.        AND     B               ; consider OVER
  3572.        XOR     (HL)            ; now XOR with source
  3573.        XOR     C               ; now with INVERSE MASK
  3574.        LD      (DE),A          ; update screen/printer
  3575.        EX      AF,AF'          ; restore flag
  3576.         JR      C,L0BD3         ; to PR-ALL-6 - printer address update
  3577.  
  3578.         INC     D               ; gives next pixel line down screen
  3579.  
  3580. ;; PR-ALL-5
  3581. L0BC1:  INC     HL              ; address next character byte
  3582.         DEC     A               ; the byte count is decremented
  3583.         JR      NZ,L0BB7        ; back to PR-ALL-4 for all 8 bytes
  3584.  
  3585.         EX      DE,HL           ; destination to HL
  3586.         DEC     H               ; bring back to last updated screen position
  3587.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3588.         CALL    Z,L0BDB         ; if not, call routine PO-ATTR to update
  3589.                                 ; corresponding colour attribute.
  3590.         POP     HL              ; restore original screen/printer position
  3591.         POP     BC              ; and line column
  3592.         DEC     C               ; move column to right
  3593.         INC     HL              ; increase screen/printer position
  3594.         RET                     ; return and continue into PO-STORE
  3595.                                 ; within PO-ABLE
  3596.  
  3597. ; ---
  3598.  
  3599. ; This branch is used to update the printer position by 32 places
  3600. ; Note. The high byte of the address D remains constant (which it should).
  3601.  
  3602. ;; PR-ALL-6
  3603. L0BD3:  EX      AF,AF'          ; save the flag
  3604.        LD      A,$20           ; load A with 32 decimal
  3605.        ADD     A,E             ; add this to E
  3606.        LD      E,A             ; and store result in E
  3607.        EX      AF,AF'          ; fetch the flag
  3608.         JR      L0BC1           ; back to PR-ALL-5
  3609.  
  3610. ; -------------
  3611. ; Set attribute
  3612. ; -------------
  3613. ; This routine is entered with the HL register holding the last screen
  3614. ; address to be updated by PRINT or PLOT.
  3615. ; The Spectrum screen arrangement leads to the L register holding
  3616. ; the correct value for the attribute file and it is only necessary
  3617. ; to manipulate H to form the correct colour attribute address.
  3618.  
  3619. ;; PO-ATTR
  3620. L0BDB:  LD       A,H            ; fetch high byte $40 - $57
  3621.         RRCA                    ; shift
  3622.         RRCA                    ; bits 3 and 4
  3623.         RRCA                    ; to right.
  3624.         AND     $03             ; range is now 0 - 2
  3625.         OR      $58             ; form correct high byte for third of screen
  3626.         LD      H,A             ; HL is now correct
  3627.         LD      DE,($5C8F)      ; make D hold ATTR_T, E hold MASK-T
  3628.         LD      A,(HL)          ; fetch existing attribute
  3629.         XOR     E               ; apply masks
  3630.         AND     D               ;
  3631.         XOR     E               ;
  3632.         BIT     6,(IY+$57)      ; test P_FLAG  - is this PAPER 9 ??
  3633.         JR      Z,L0BFA         ; skip to PO-ATTR-1 if not.
  3634.  
  3635.         AND     $C7             ; set paper
  3636.         BIT     2,A             ; to contrast with ink
  3637.         JR      NZ,L0BFA        ; skip to PO-ATTR-1
  3638.  
  3639.         XOR     $38             ;
  3640.  
  3641. ;; PO-ATTR-1
  3642. L0BFA:  BIT     4,(IY+$57)      ; test P_FLAG  - Is this INK 9 ??
  3643.         JR      Z,L0C08         ; skip to PO-ATTR-2 if not
  3644.  
  3645.         AND     $F8             ; make ink
  3646.         BIT     5,A             ; contrast with paper.
  3647.         JR      NZ,L0C08        ; to PO-ATTR-2
  3648.  
  3649.         XOR     $07             ;
  3650.  
  3651. ;; PO-ATTR-2
  3652. L0C08:  LD      (HL),A          ; save the new attribute.
  3653.         RET                     ; return.
  3654.  
  3655. ; ----------------
  3656. ; Message printing
  3657. ; ----------------
  3658. ; This entry point is used to print tape, boot-up, scroll? and error messages
  3659. ; On entry the DE register points to an initial step-over byte or
  3660. ; the inverted end-marker of the previous entry in the table.
  3661. ; A contains the message number, often zero to print first message.
  3662. ; (HL has nothing important usually P_FLAG)
  3663.  
  3664. ;; PO-MSG
  3665. L0C0A:  PUSH    HL              ; put hi-byte zero on stack to suppress
  3666.         LD      H,$00           ; trailing spaces
  3667.         EX      (SP),HL         ; ld h,0; push hl would have done ?.
  3668.         JR      L0C14           ; forward to PO-TABLE.
  3669.  
  3670. ; ---
  3671.  
  3672. ; This entry point prints the BASIC keywords, '<>' etc. from alt set
  3673.  
  3674. ;; PO-TOKENS
  3675. L0C10:  LD      DE,L0095        ; address: TKN-TABLE
  3676.         PUSH    AF              ; save the token number to control
  3677.                                 ; trailing spaces - see later *
  3678.  
  3679. ;; PO-TABLE
  3680. L0C14:  CALL    L0C41           ; routine PO-SEARCH will set carry for
  3681.                                 ; all messages and function words.
  3682. L0C17:  JR      C,L0C22         ; forward to PO-EACH if not a command,
  3683.                                 ; '<>' etc.
  3684.  
  3685.         LD      A,$20           ; prepare leading space
  3686.         BIT     0,(IY+$01)      ; test FLAGS  - leading space if not set
  3687.         CALL    Z,L0C3B         ; routine PO-SAVE to print a space
  3688.                                 ; without disturbing registers
  3689.  
  3690. ;; PO-EACH
  3691. L0C22:  LD      A,(DE)          ; fetch character
  3692.         AND     $7F             ; remove any inverted bit
  3693.         CALL    L0C3B           ; routine PO-SAVE to print using alternate
  3694.                                 ; set of registers.
  3695.         LD      A,(DE)          ; re-fetch character.
  3696.         INC     DE              ; address next
  3697.         ADD     A,A             ; was character inverted ?
  3698.                                 ; (this also doubles character)
  3699.         JR      NC,L0C22        ; back to PO-EACH if not
  3700.  
  3701.         POP     DE              ; * re-fetch trailing space flag to D (was A)
  3702.         CP      $48             ; was last character '$' ($24*2)
  3703.         JR      Z,L0C35         ; forward to PO-TR-SP to consider trailing
  3704.                                 ; space if so.
  3705.  
  3706.         CP      $82             ; was it < 'A' i.e. '#','>','=' from tokens
  3707.                                 ; or ' ','.' (from tape) or '?' from scroll
  3708.         RET     C               ; no trailing space
  3709.  
  3710. ;; PO-TR-SP
  3711. L0C35:  LD      A,D             ; the trailing space flag (zero if an error msg)
  3712.         CP      $03             ; test against RND, INKEY$ and PI
  3713.                                 ; which have no parameters and
  3714.         RET     C               ; therefore no trailing space so return.
  3715.  
  3716.         LD      A,$20           ; else continue and print a trailing space.
  3717.  
  3718. ; -------------------------
  3719. ; Handle recursive printing
  3720. ; -------------------------
  3721. ; This routine which is part of PRINT-OUT allows RST $10 to be
  3722. ; used recursively to print tokens and the spaces associated with them.
  3723.  
  3724. ;; PO-SAVE
  3725. L0C3B:  PUSH    DE              ; save DE as CALL-SUB doesn't.
  3726.         EXX                     ; switch in main set
  3727.  
  3728.         RST     10H             ; PRINT-A prints using this alternate set.
  3729.  
  3730.         EXX                     ; back to this alternate set.
  3731.         POP     DE              ; restore initial DE.
  3732.         RET                     ; return.
  3733.  
  3734. ; ------------
  3735. ; Table search
  3736. ; ------------
  3737. ; This subroutine searches a message or the token table for the
  3738. ; message number held in A. DE holds the address of the table.
  3739.  
  3740. ;; PO-SEARCH
  3741. L0C41:  PUSH    AF              ; save the message/token number
  3742.         EX      DE,HL           ; transfer DE to HL
  3743.         INC     A               ; adjust for initial step-over byte
  3744.  
  3745. ;; PO-STEP
  3746. L0C44:  BIT     7,(HL)          ; is character inverted ?
  3747.         INC     HL              ; address next
  3748.         JR      Z,L0C44         ; back to PO-STEP if not inverted.
  3749.  
  3750.         DEC     A               ; decrease counter
  3751.         JR      NZ,L0C44        ; back to PO-STEP if not zero
  3752.  
  3753.         EX      DE,HL           ; transfer address to DE
  3754.         POP     AF              ; restore message/token number
  3755.         CP      $20             ; return with carry set
  3756.         RET     C               ; for all messages and function tokens
  3757.  
  3758.         LD      A,(DE)          ; test first character of token
  3759.         SUB     $41             ; and return with carry set
  3760.         RET                     ; if it is less that 'A'
  3761.                                 ; i.e. '<>', '<=', '>='
  3762.  
  3763. ; ---------------
  3764. ; Test for scroll
  3765. ; ---------------
  3766. ; This test routine is called when printing carriage return, when considering
  3767. ; PRINT AT and from the general PRINT ALL characters routine to test if
  3768. ; scrolling is required, prompting the user if necessary.
  3769. ; This is therefore using the alternate set.
  3770. ; The B register holds the current line.
  3771.  
  3772. ;; PO-SCR
  3773. L0C55:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3774.         RET     NZ              ; return immediately if so.
  3775.  
  3776.         LD      DE,L0DD9        ; set DE to address: CL-SET
  3777.         PUSH    DE              ; and push for return address.
  3778.         LD      A,B             ; transfer the line to A.
  3779.         BIT     0,(IY+$02)      ; test TV_FLAG  - Lower screen in use ?
  3780.         JP      NZ,L0D02        ; jump forward to PO-SCR-4 if so.
  3781.  
  3782.         CP      (IY+$31)        ; greater than DF_SZ display file size ?
  3783.         JR      C,L0C86         ; forward to REPORT-5 if less.
  3784.                                 ; 'Out of screen'
  3785.  
  3786.         RET     NZ              ; return (via CL-SET) if greater
  3787.  
  3788.         BIT     4,(IY+$02)      ; test TV_FLAG  - Automatic listing ?
  3789.         JR      Z,L0C88         ; forward to PO-SCR-2 if not.
  3790.  
  3791.         LD      E,(IY+$2D)      ; fetch BREG - the count of scroll lines to E.
  3792.         DEC     E               ; decrease and jump
  3793.         JR      Z,L0CD2         ; to PO-SCR-3 if zero and scrolling required.
  3794.  
  3795.         LD      A,$00           ; explicit - select channel zero.
  3796.         CALL    L1601           ; routine CHAN-OPEN opens it.
  3797.  
  3798.         LD      SP,($5C3F)      ; set stack pointer to LIST_SP
  3799.  
  3800.         RES     4,(IY+$02)      ; reset TV_FLAG  - signal auto listing finished.
  3801.         RET                     ; return ignoring pushed value, CL-SET
  3802.                                 ; to MAIN or EDITOR without updating
  3803.                                 ; print position                         ->
  3804.  
  3805. ; ---
  3806.  
  3807.  
  3808. ;; REPORT-5
  3809. L0C86:  RST     08H             ; ERROR-1
  3810.         DB    $04             ; Error Report: Out of screen
  3811.  
  3812. ; continue here if not an automatic listing.
  3813.  
  3814. ;; PO-SCR-2
  3815. L0C88:  DEC     (IY+$52)        ; decrease SCR_CT
  3816.         JR      NZ,L0CD2        ; forward to PO-SCR-3 to scroll display if
  3817.                                 ; result not zero.
  3818.  
  3819. ; now produce prompt.
  3820.  
  3821.         LD      A,$18           ; reset
  3822.         SUB     B               ; the
  3823.         LD      ($5C8C),A       ; SCR_CT scroll count
  3824.         LD      HL,($5C8F)      ; L=ATTR_T, H=MASK_T
  3825.         PUSH    HL              ; save on stack
  3826.         LD      A,($5C91)       ; P_FLAG
  3827.         PUSH    AF              ; save on stack to prevent lower screen
  3828.                                 ; attributes (BORDCR etc.) being applied.
  3829.         LD      A,$FD           ; select system channel 'K'
  3830.         CALL    L1601           ; routine CHAN-OPEN opens it
  3831.         XOR     A               ; clear to address message directly
  3832.         LD      DE,L0CF8        ; make DE address: scrl-mssg
  3833.         CALL    L0C0A           ; routine PO-MSG prints to lower screen
  3834.         SET     5,(IY+$02)      ; set TV_FLAG  - signal lower screen requires
  3835.                                 ; clearing
  3836.         LD      HL,$5C3B        ; make HL address FLAGS
  3837.         SET     3,(HL)          ; signal 'L' mode.
  3838.         RES     5,(HL)          ; signal 'no new key'.
  3839.         EXX                     ; switch to main set.
  3840.                                 ; as calling chr input from alternative set.
  3841.         CALL    L15D4           ; routine WAIT-KEY waits for new key
  3842.                                 ; Note. this is the right routine but the
  3843.                                 ; stream in use is unsatisfactory. From the
  3844.                                 ; choices available, it is however the best.
  3845.  
  3846.         EXX                     ; switch back to alternate set.
  3847.         CP      $20             ; space is considered as BREAK
  3848.         JR      Z,L0D00         ; forward to REPORT-D if so
  3849.                                 ; 'BREAK - CONT repeats'
  3850.  
  3851.         CP      $E2             ; is character 'STOP' ?
  3852.         JR      Z,L0D00         ; forward to REPORT-D if so
  3853.  
  3854.         OR      $20             ; convert to lower-case
  3855.         CP      $6E             ; is character 'n' ?
  3856.         JR      Z,L0D00         ; forward to REPORT-D if so else scroll.
  3857.  
  3858.         LD      A,$FE           ; select system channel 'S'
  3859.         CALL    L1601           ; routine CHAN-OPEN
  3860.         POP     AF              ; restore original P_FLAG
  3861.         LD      ($5C91),A       ; and save in P_FLAG.
  3862.         POP     HL              ; restore original ATTR_T, MASK_T
  3863.         LD      ($5C8F),HL      ; and reset ATTR_T, MASK-T as 'scroll?' has
  3864.                                 ; been printed.
  3865.  
  3866. ;; PO-SCR-3
  3867. L0CD2:  CALL    L0DFE           ; routine CL-SC-ALL to scroll whole display
  3868.         LD      B,(IY+$31)      ; fetch DF_SZ to B
  3869.         INC     B               ; increase to address last line of display
  3870.         LD      C,$21           ; set C to $21 (was $21 from above routine)
  3871.         PUSH    BC              ; save the line and column in BC.
  3872.  
  3873.         CALL    L0E9B           ; routine CL-ADDR finds display address.
  3874.  
  3875.         LD      A,H             ; now find the corresponding attribute byte
  3876.         RRCA                    ; (this code sequence is used twice
  3877.         RRCA                    ; elsewhere and is a candidate for
  3878.         RRCA                    ; a subroutine.)
  3879.         AND     $03             ;
  3880.         OR      $58             ;
  3881.         LD      H,A             ;
  3882.  
  3883.         LD      DE,$5AE0        ; start of last 'line' of attribute area
  3884.         LD      A,(DE)          ; get attribute for last line
  3885.         LD      C,(HL)          ; transfer to base line of upper part
  3886.         LD      B,$20           ; there are thirty two bytes
  3887.         EX      DE,HL           ; swap the pointers.
  3888.  
  3889. ;; PO-SCR-3A
  3890. L0CF0:  LD      (DE),A          ; transfer
  3891.         LD      (HL),C          ; attributes.
  3892.         INC     DE              ; address next.
  3893.         INC     HL              ; address next.
  3894.         DJNZ    L0CF0           ; loop back to PO-SCR-3A for all adjacent
  3895.                                 ; attribute lines.
  3896.  
  3897.         POP     BC              ; restore the line/column.
  3898.         RET                     ; return via CL-SET (was pushed on stack).
  3899.  
  3900. ; ---
  3901.  
  3902. ; The message 'scroll?' appears here with last byte inverted.
  3903.  
  3904. ;; scrl-mssg
  3905. L0CF8           DB $80             ; initial step-over byte.
  3906.                 DC "scroll?"    ;DEFM    "scroll"
  3907.                                 ;DB    '?'+$80
  3908.  
  3909. ;; REPORT-D
  3910. L0D00:  RST     08H             ; ERROR-1
  3911.         DB    $0C             ; Error Report: BREAK - CONT repeats
  3912.  
  3913. ; continue here if using lower display - A holds line number.
  3914.  
  3915. ;; PO-SCR-4
  3916. L0D02:  CP      $02             ; is line number less than 2 ?
  3917.         JR      C,L0C86         ; to REPORT-5 if so
  3918.                                 ; 'Out of Screen'.
  3919.  
  3920.         ADD     A,(IY+$31)      ; add DF_SZ
  3921.         SUB     $19             ;
  3922.         RET     NC              ; return if scrolling unnecessary
  3923.  
  3924.         NEG                     ; Negate to give number of scrolls required.
  3925.         PUSH    BC              ; save line/column
  3926.         LD      B,A             ; count to B
  3927.         LD      HL,($5C8F)      ; fetch current ATTR_T, MASK_T to HL.
  3928.         PUSH    HL              ; and save
  3929.         LD      HL,($5C91)      ; fetch P_FLAG
  3930.         PUSH    HL              ; and save.
  3931.                                 ; to prevent corruption by input AT
  3932.  
  3933.         CALL    L0D4D           ; routine TEMPS sets to BORDCR etc
  3934.         LD      A,B             ; transfer scroll number to A.
  3935.  
  3936. ;; PO-SCR-4A
  3937. L0D1C:  PUSH    AF              ; save scroll number.
  3938.         LD      HL,$5C6B        ; address DF_SZ
  3939.         LD      B,(HL)          ; fetch old value
  3940.         LD      A,B             ; transfer to A
  3941.         INC     A               ; and increment
  3942.         LD      (HL),A          ; then put back.
  3943.         LD      HL,$5C89        ; address S_POSN_hi - line
  3944.         CP      (HL)            ; compare
  3945.         JR      C,L0D2D         ; forward to PO-SCR-4B if scrolling required
  3946.  
  3947.         INC     (HL)            ; else increment S_POSN_hi
  3948.         LD      B,$18           ; set count to whole display ??
  3949.                                 ; Note. should be $17 and the top line
  3950.                                 ; will be scrolled into the ROM which
  3951.                                 ; is harmless on the standard set up.
  3952.  
  3953. ;; PO-SCR-4B
  3954. L0D2D:  CALL    L0E00           ; routine CL-SCROLL scrolls B lines
  3955.         POP     AF              ; restore scroll counter.
  3956.         DEC     A               ; decrease
  3957.         JR      NZ,L0D1C        ; back to to PO-SCR-4A until done
  3958.  
  3959.         POP     HL              ; restore original P_FLAG.
  3960.         LD      (IY+$57),L      ; and overwrite system variable P_FLAG.
  3961.  
  3962.         POP     HL              ; restore original ATTR_T/MASK_T.
  3963.         LD      ($5C8F),HL      ; and update system variables.
  3964.  
  3965.         LD      BC,($5C88)      ; fetch S_POSN to BC.
  3966.         RES     0,(IY+$02)      ; signal to TV_FLAG  - main screen in use.
  3967.         CALL    L0DD9           ; call routine CL-SET for upper display.
  3968.  
  3969.         SET     0,(IY+$02)      ; signal to TV_FLAG  - lower screen in use.
  3970.         POP     BC              ; restore line/column
  3971.         RET                     ; return via CL-SET for lower display.
  3972.  
  3973. ; ----------------------
  3974. ; Temporary colour items
  3975. ; ----------------------
  3976. ; This subroutine is called 11 times to copy the permanent colour items
  3977. ; to the temporary ones.
  3978.  
  3979. ;; TEMPS
  3980. L0D4D:  XOR     A               ; clear the accumulator
  3981.         LD      HL,($5C8D)      ; fetch L=ATTR_P and H=MASK_P
  3982.         BIT     0,(IY+$02)      ; test TV_FLAG  - is lower screen in use ?
  3983.         JR      Z,L0D5B         ; skip to TEMPS-1 if not
  3984.  
  3985.         LD      H,A             ; set H, MASK P, to 00000000.
  3986.         LD      L,(IY+$0E)      ; fetch BORDCR to L which is used for lower
  3987.                                 ; screen.
  3988.  
  3989. ;; TEMPS-1
  3990. L0D5B:  LD      ($5C8F),HL      ; transfer values to ATTR_T and MASK_T
  3991.  
  3992. ; for the print flag the permanent values are odd bits, temporary even bits.
  3993.  
  3994.         LD      HL,$5C91        ; address P_FLAG.
  3995.         JR      NZ,L0D65        ; skip to TEMPS-2 if lower screen using A=0.
  3996.  
  3997.         LD      A,(HL)          ; else pick up flag bits.
  3998.         RRCA                    ; rotate permanent bits to temporary bits.
  3999.  
  4000. ;; TEMPS-2
  4001. L0D65:  XOR     (HL)            ;
  4002.         AND     $55             ; BIN 01010101
  4003.         XOR     (HL)            ; permanent now as original
  4004.         LD      (HL),A          ; apply permanent bits to temporary bits.
  4005.         RET                     ; and return.
  4006.  
  4007. ; ------------------
  4008. ; Handle CLS command
  4009. ; ------------------
  4010. ; clears the display.
  4011. ; if it's difficult to write it should be difficult to read.
  4012.  
  4013. ;; CLS
  4014. L0D6B:  CALL    L0DAF           ; routine CL-ALL  clears display and
  4015.                                 ; resets attributes to permanent.
  4016.                                 ; re-attaches it to this computer.
  4017.  
  4018. ; this routine called from INPUT, **
  4019.  
  4020. ;; CLS-LOWER
  4021. L0D6E:  LD      HL,$5C3C        ; address System Variable TV_FLAG.
  4022.         RES     5,(HL)          ; TV_FLAG - signal do not clear lower screen.
  4023.         SET     0,(HL)          ; TV_FLAG - signal lower screen in use.
  4024.         CALL    L0D4D           ; routine TEMPS picks up temporary colours.
  4025.         LD      B,(IY+$31)      ; fetch lower screen DF_SZ
  4026.         CALL    L0E44           ; routine CL-LINE clears lower part
  4027.                                 ; and sets permanent attributes.
  4028.  
  4029.         LD      HL,$5AC0        ; fetch attribute address leftmost cell,
  4030.                                 ; second line up.
  4031.         LD      A,($5C8D)       ; fetch permanent attribute from ATTR_P.
  4032.         DEC     B               ; decrement lower screen display file size
  4033.         JR      L0D8E           ; forward to CLS-3 ->
  4034.  
  4035. ; ---
  4036.  
  4037. ;; CLS-1
  4038. L0D87:  LD      C,$20           ; set counter to 32 characters per line
  4039.  
  4040. ;; CLS-2
  4041. L0D89:  DEC     HL              ; decrease attribute address.
  4042.         LD      (HL),A          ; and place attributes in next line up.
  4043.         DEC     C               ; decrease 32 counter.
  4044.         JR      NZ,L0D89        ; loop back to CLS-2 until all 32 done.
  4045.  
  4046. ;; CLS-3
  4047. L0D8E:  DJNZ    L0D87           ; decrease B counter and back to CLS-1
  4048.                                 ; if not zero.
  4049.  
  4050.         LD      (IY+$31),$02    ; set DF_SZ lower screen to 2
  4051.  
  4052. ; This entry point is called from CL-ALL below to
  4053. ; reset the system channel input and output addresses to normal.
  4054.  
  4055. ;; CL-CHAN
  4056. L0D94:  LD      A,$FD           ; select system channel 'K'
  4057.         CALL    L1601           ; routine CHAN-OPEN opens it.
  4058.         LD      HL,($5C51)      ; fetch CURCHL to HL to address current channel
  4059.         LD      DE,L09F4        ; set address to PRINT-OUT for first pass.
  4060.         AND     A               ; clear carry for first pass.
  4061.  
  4062. ;; CL-CHAN-A
  4063. L0DA0:  LD      (HL),E          ; insert output address first pass.
  4064.         INC     HL              ; or input address on second pass.
  4065.         LD      (HL),D          ;
  4066.         INC     HL              ;
  4067.         LD      DE,L10A8        ; fetch address KEY-INPUT for second pass
  4068.         CCF                     ; complement carry flag - will set on pass 1.
  4069.  
  4070.         JR      C,L0DA0         ; back to CL-CHAN-A if first pass else done.
  4071.  
  4072.         LD      BC,$1721        ; line 23 for lower screen
  4073.         JR      L0DD9           ; exit via CL-SET to set column
  4074.                                 ; for lower display
  4075.  
  4076. ; ---------------------------
  4077. ; Clearing whole display area
  4078. ; ---------------------------
  4079. ; This subroutine called from CLS, AUTO-LIST and MAIN-3
  4080. ; clears 24 lines of the display and resets the relevant system variables
  4081. ; and system channels.
  4082.  
  4083. ;; CL-ALL
  4084. L0DAF:  LD      HL,$0000        ; initialize plot coordinates.
  4085.         LD      ($5C7D),HL      ; set COORDS to 0,0.
  4086.         RES     0,(IY+$30)      ; update FLAGS2  - signal main screen is clear.
  4087.  
  4088.         CALL    L0D94           ; routine CL-CHAN makes channel 'K' 'normal'.
  4089.  
  4090.         LD      A,$FE           ; select system channel 'S'
  4091.         CALL    L1601           ; routine CHAN-OPEN opens it
  4092.         CALL    L0D4D           ; routine TEMPS picks up permanent values.
  4093.         LD      B,$18           ; There are 24 lines.
  4094.         CALL    L0E44           ; routine CL-LINE clears 24 text lines
  4095.                                 ; (and sets BC to $1821)
  4096.  
  4097.         LD      HL,($5C51)      ; fetch CURCHL make HL address current
  4098.                                 ; channel 'S'
  4099.         LD      DE,L09F4        ; address: PRINT-OUT
  4100.         LD      (HL),E          ; is made
  4101.         INC     HL              ; the normal
  4102.         LD      (HL),D          ; output address.
  4103.  
  4104.         LD      (IY+$52),$01    ; set SCR_CT - scroll count is set to default.
  4105.                                 ; Note. BC already contains $1821.
  4106.         LD      BC,$1821        ; reset column and line to 0,0
  4107.                                 ; and continue into CL-SET, below, exiting
  4108.                                 ; via PO-STORE (for upper screen).
  4109.  
  4110. ; ---------------------------
  4111. ; Set line and column numbers
  4112. ; ---------------------------
  4113. ; This important subroutine is used to calculate the character output
  4114. ; address for screens or printer based on the line/column for screens
  4115. ; or the column for printer.
  4116.  
  4117. ;; CL-SET
  4118. L0DD9:  LD      HL,$5B00        ; the base address of printer buffer
  4119.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  4120.         JR      NZ,L0DF4        ; forward to CL-SET-2 if so.
  4121.  
  4122.         LD      A,B             ; transfer line to A.
  4123.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4124.         JR      Z,L0DEE         ; skip to CL-SET-1 if handling upper part
  4125.  
  4126.         ADD     A,(IY+$31)      ; add DF_SZ for lower screen
  4127.         SUB     $18             ; and adjust.
  4128.  
  4129. ;; CL-SET-1
  4130. L0DEE:  PUSH    BC              ; save the line/column.
  4131.         LD      B,A             ; transfer line to B
  4132.                                 ; (adjusted if lower screen)
  4133.  
  4134.         CALL    L0E9B           ; routine CL-ADDR calculates address at left
  4135.                                 ; of screen.
  4136.         POP     BC              ; restore the line/column.
  4137.  
  4138. ;; CL-SET-2
  4139. L0DF4:  LD      A,$21           ; the column $1-$21 is reversed
  4140.         SUB     C               ; to range $00 - $20
  4141.         LD      E,A             ; now transfer to DE
  4142.         LD      D,$00           ; prepare for addition
  4143.         ADD     HL,DE           ; and add to base address
  4144.         JP      L0ADC           ; exit via PO-STORE to update relevant
  4145.                                 ; system variables.
  4146. ; ----------------
  4147. ; Handle scrolling
  4148. ; ----------------
  4149. ; The routine CL-SC-ALL is called once from PO to scroll all the display
  4150. ; and from the routine CL-SCROLL, once, to scroll part of the display.
  4151.  
  4152. ;; CL-SC-ALL
  4153. L0DFE:  LD      B,$17           ; scroll 23 lines, after 'scroll?'.
  4154.  
  4155. ;; CL-SCROLL
  4156. L0E00:  CALL    L0E9B           ; routine CL-ADDR gets screen address in HL.
  4157.         LD      C,$08           ; there are 8 pixel lines to scroll.
  4158.  
  4159. ;; CL-SCR-1
  4160. L0E05:  PUSH    BC              ; save counters.
  4161.         PUSH    HL              ; and initial address.
  4162.         LD      A,B             ; get line count.
  4163.         AND     $07             ; will set zero if all third to be scrolled.
  4164.         LD      A,B             ; re-fetch the line count.
  4165.         JR      NZ,L0E19        ; forward to CL-SCR-3 if partial scroll.
  4166.  
  4167. ; HL points to top line of third and must be copied to bottom of previous 3rd.
  4168. ; ( so HL = $4800 or $5000 ) ( but also sometimes $4000 )
  4169.  
  4170. ;; CL-SCR-2
  4171. L0E0D:  EX      DE,HL           ; copy HL to DE.
  4172.         LD      HL,$F8E0        ; subtract $08 from H and add $E0 to L -
  4173.         ADD     HL,DE           ; to make destination bottom line of previous
  4174.                                 ; third.
  4175.         EX      DE,HL           ; restore the source and destination.
  4176.         LD      BC,$0020        ; thirty-two bytes are to be copied.
  4177.         DEC     A               ; decrement the line count.
  4178.         LDIR                    ; copy a pixel line to previous third.
  4179.  
  4180. ;; CL-SCR-3
  4181. L0E19:  EX      DE,HL           ; save source in DE.
  4182.         LD      HL,$FFE0        ; load the value -32.
  4183.         ADD     HL,DE           ; add to form destination in HL.
  4184.         EX      DE,HL           ; switch source and destination
  4185.         LD      B,A             ; save the count in B.
  4186.         AND     $07             ; mask to find count applicable to current
  4187.         RRCA                    ; third and
  4188.         RRCA                    ; multiply by
  4189.         RRCA                    ; thirty two (same as 5 RLCAs)
  4190.  
  4191.         LD      C,A             ; transfer byte count to C ($E0 at most)
  4192.         LD      A,B             ; store line count to A
  4193.         LD      B,$00           ; make B zero
  4194.         LDIR                    ; copy bytes (BC=0, H incremented, L=0)
  4195.         LD      B,$07           ; set B to 7, C is zero.
  4196.         ADD     HL,BC           ; add 7 to H to address next third.
  4197.         AND     $F8             ; has last third been done ?
  4198.         JR      NZ,L0E0D        ; back to CL-SCR-2 if not
  4199.  
  4200.         POP     HL              ; restore topmost address.
  4201.         INC     H               ; next pixel line down.
  4202.         POP     BC              ; restore counts.
  4203.         DEC     C               ; reduce pixel line count.
  4204.         JR      NZ,L0E05        ; back to CL-SCR-1 if all eight not done.
  4205.  
  4206.         CALL    L0E88           ; routine CL-ATTR gets address in attributes
  4207.                                 ; from current 'ninth line', count in BC.
  4208.         LD      HL,$FFE0        ; set HL to the 16-bit value -32.
  4209.         ADD     HL,DE           ; and add to form destination address.
  4210.         EX      DE,HL           ; swap source and destination addresses.
  4211.         LDIR                    ; copy bytes scrolling the linear attributes.
  4212.         LD      B,$01           ; continue to clear the bottom line.
  4213.  
  4214. ; ---------------------------
  4215. ; Clear text lines of display
  4216. ; ---------------------------
  4217. ; This subroutine, called from CL-ALL, CLS-LOWER and AUTO-LIST and above,
  4218. ; clears text lines at bottom of display.
  4219. ; The B register holds on entry the number of lines to be cleared 1-24.
  4220.  
  4221. ;; CL-LINE
  4222. L0E44:  PUSH    BC              ; save line count
  4223.         CALL    L0E9B           ; routine CL-ADDR gets top address
  4224.         LD      C,$08           ; there are eight screen lines to a text line.
  4225.  
  4226. ;; CL-LINE-1
  4227. L0E4A:  PUSH    BC              ; save pixel line count
  4228.         PUSH    HL              ; and save the address
  4229.         LD      A,B             ; transfer the line to A (1-24).
  4230.  
  4231. ;; CL-LINE-2
  4232. L0E4D:  AND     $07             ; mask 0-7 to consider thirds at a time
  4233.         RRCA                    ; multiply
  4234.         RRCA                    ; by 32  (same as five RLCA instructions)
  4235.         RRCA                    ; now 32 - 256(0)
  4236.         LD      C,A             ; store result in C
  4237.         LD      A,B             ; save line in A (1-24)
  4238.         LD      B,$00           ; set high byte to 0, prepare for ldir.
  4239.         DEC     C               ; decrement count 31-255.
  4240.         LD      D,H             ; copy HL
  4241.         LD      E,L             ; to DE.
  4242.         LD      (HL),$00        ; blank the first byte.
  4243.         INC     DE              ; make DE point to next byte.
  4244.         LDIR                    ; ldir will clear lines.
  4245.         LD      DE,$0701        ; now address next third adjusting
  4246.         ADD     HL,DE           ; register E to address left hand side
  4247.         DEC     A               ; decrease the line count.
  4248.         AND     $F8             ; will be 16, 8 or 0  (AND $18 will do).
  4249.         LD      B,A             ; transfer count to B.
  4250.         JR      NZ,L0E4D        ; back to CL-LINE-2 if 16 or 8 to do
  4251.                                 ; the next third.
  4252.  
  4253.         POP     HL              ; restore start address.
  4254.         INC     H               ; address next line down.
  4255.         POP     BC              ; fetch counts.
  4256.         DEC     C               ; decrement pixel line count
  4257.         JR      NZ,L0E4A        ; back to CL-LINE-1 till all done.
  4258.  
  4259.         CALL    L0E88           ; routine CL-ATTR gets attribute address
  4260.                                 ; in DE and B * 32 in BC.
  4261.         LD      H,D             ; transfer the address
  4262.         LD      L,E             ; to HL.
  4263.  
  4264.         INC     DE              ; make DE point to next location.
  4265.  
  4266.         LD      A,($5C8D)       ; fetch ATTR_P - permanent attributes
  4267.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4268.         JR      Z,L0E80         ; skip to CL-LINE-3 if not.
  4269.  
  4270.         LD      A,($5C48)       ; else lower screen uses BORDCR as attribute.
  4271.  
  4272. ;; CL-LINE-3
  4273. L0E80:  LD      (HL),A          ; put attribute in first byte.
  4274.         DEC     BC              ; decrement the counter.
  4275.         LDIR                    ; copy bytes to set all attributes.
  4276.         POP     BC              ; restore the line $01-$24.
  4277.         LD      C,$21           ; make column $21. (No use is made of this)
  4278.         RET                     ; return to the calling routine.
  4279.  
  4280. ; ------------------
  4281. ; Attribute handling
  4282. ; ------------------
  4283. ; This subroutine is called from CL-LINE or CL-SCROLL with the HL register
  4284. ; pointing to the 'ninth' line and H needs to be decremented before or after
  4285. ; the division. Had it been done first then either present code or that used
  4286. ; at the start of PO-ATTR could have been used.
  4287. ; The Spectrum screen arrangement leads to the L register holding already
  4288. ; the correct value for the attribute file and it is only necessary
  4289. ; to manipulate H to form the correct colour attribute address.
  4290.  
  4291. ;; CL-ATTR
  4292. L0E88:  LD      A,H             ; fetch H to A - $48, $50, or $58.
  4293.         RRCA                    ; divide by
  4294.         RRCA                    ; eight.
  4295.         RRCA                    ; $09, $0A or $0B.
  4296.         DEC     A               ; $08, $09 or $0A.
  4297.         OR      $50             ; $58, $59 or $5A.
  4298.         LD      H,A             ; save high byte of attributes.
  4299.  
  4300.         EX      DE,HL           ; transfer attribute address to DE
  4301.         LD      H,C             ; set H to zero - from last LDIR.
  4302.         LD      L,B             ; load L with the line from B.
  4303.         ADD     HL,HL           ; multiply
  4304.         ADD     HL,HL           ; by
  4305.         ADD     HL,HL           ; thirty two
  4306.         ADD     HL,HL           ; to give count of attribute
  4307.         ADD     HL,HL           ; cells to end of display.
  4308.  
  4309.         LD      B,H             ; transfer result
  4310.         LD      C,L             ; to register BC.
  4311.  
  4312.         RET                     ; and return.
  4313.  
  4314. ; -------------------------------
  4315. ; Handle display with line number
  4316. ; -------------------------------
  4317. ; This subroutine is called from four places to calculate the address
  4318. ; of the start of a screen character line which is supplied in B.
  4319.  
  4320. ;; CL-ADDR
  4321. L0E9B:  LD      A,$18           ; reverse the line number
  4322.         SUB     B               ; to range $00 - $17.
  4323.         LD      D,A             ; save line in D for later.
  4324.         RRCA                    ; multiply
  4325.         RRCA                    ; by
  4326.         RRCA                    ; thirty-two.
  4327.  
  4328.         AND     $E0             ; mask off low bits to make
  4329.         LD      L,A             ; L a multiple of 32.
  4330.  
  4331.         LD      A,D             ; bring back the line to A.
  4332.  
  4333.         AND     $18             ; now $00, $08 or $10.
  4334.  
  4335.         OR      $40             ; add the base address of screen.
  4336.  
  4337.         LD      H,A             ; HL now has the correct address.
  4338.         RET                     ; return.
  4339.  
  4340. ; -------------------
  4341. ; Handle COPY command
  4342. ; -------------------
  4343. ; This command copies the top 176 lines to the ZX Printer
  4344. ; It is popular to call this from machine code at point
  4345. ; L0EAF with B holding 192 (and interrupts disabled) for a full-screen
  4346. ; copy. This particularly applies to 16K Spectrums as time-critical
  4347. ; machine code routines cannot be written in the first 16K of RAM as
  4348. ; it is shared with the ULA which has precedence over the Z80 chip.
  4349.  
  4350. ;; COPY
  4351.  
  4352. L0EAC:  DI                      ; disable interrupts as this is time-critical.
  4353. ;===============================
  4354.                 IF AY_PRINTER
  4355.                 RST 0X08
  4356.                 DB AY_PRN_SCR
  4357.                 ELSE
  4358.                 LD B,$B0                ; top 176 lines.
  4359.                 ENDIF
  4360. ;===============================
  4361.  
  4362. L0EAF           LD HL,$4000             ; address start of the display file.
  4363.  
  4364. ; now enter a loop to handle each pixel line.
  4365.  
  4366. ;; COPY-1
  4367. L0EB2:  PUSH    HL              ; save the screen address.
  4368.         PUSH    BC              ; and the line counter.
  4369.  
  4370.         CALL    L0EF4           ; routine COPY-LINE outputs one line.
  4371.  
  4372.         POP     BC              ; restore the line counter.
  4373.         POP     HL              ; and display address.
  4374.         INC     H               ; next line down screen within 'thirds'.
  4375.         LD      A,H             ; high byte to A.
  4376.         AND     $07             ; result will be zero if we have left third.
  4377.         JR      NZ,L0EC9        ; forward to COPY-2 if not to continue loop.
  4378.  
  4379.         LD      A,L             ; consider low byte first.
  4380.         ADD     A,$20           ; increase by 32 - sets carry if back to zero.
  4381.         LD      L,A             ; will be next group of 8.
  4382.         CCF                     ; complement - carry set if more lines in
  4383.                                 ; the previous third.
  4384.         SBC     A,A             ; will be FF, if more, else 00.
  4385.         AND     $F8             ; will be F8 (-8) or 00.
  4386.         ADD     A,H             ; that is subtract 8, if more to do in third.
  4387.         LD      H,A             ; and reset address.
  4388.  
  4389. ;; COPY-2
  4390. L0EC9:  DJNZ    L0EB2           ; back to COPY-1 for all lines.
  4391.  
  4392.         JR      L0EDA           ; forward to COPY-END to switch off the printer
  4393.                                 ; motor and enable interrupts.
  4394.                                 ; Note. Nothing else required.
  4395.  
  4396. ; ------------------------------
  4397. ; Pass printer buffer to printer
  4398. ; ------------------------------
  4399. ; This routine is used to copy 8 text lines from the printer buffer
  4400. ; to the ZX Printer. These text lines are mapped linearly so HL does
  4401. ; not need to be adjusted at the end of each line.
  4402.  
  4403. ;; COPY-BUFF
  4404. L0ECD:  DI                      ; disable interrupts
  4405.         LD      HL,$5B00        ; the base address of the Printer Buffer.
  4406.         LD      B,$08           ; set count to 8 lines of 32 bytes.
  4407.  
  4408. ;; COPY-3
  4409. L0ED3:  PUSH    BC              ; save counter.
  4410.         CALL    L0EF4           ; routine COPY-LINE outputs 32 bytes
  4411.         POP     BC              ; restore counter.
  4412.         DJNZ    L0ED3           ; loop back to COPY-3 for all 8 lines.
  4413.                                 ; then stop motor and clear buffer.
  4414.  
  4415. ; Note. the COPY command rejoins here, essentially to execute the next
  4416. ; three instructions.
  4417.  
  4418. ;; COPY-END
  4419. L0EDA           LD A,$04                ; output value 4 to port
  4420.                 OUT ($FB),A             ; to stop the slowed printer motor.
  4421. L0EDE           EI                      ; enable interrupts.
  4422.  
  4423. ; --------------------
  4424. ; Clear Printer Buffer
  4425. ; --------------------
  4426. ; This routine clears an arbitrary 256 bytes of memory.
  4427. ; Note. The routine seems designed to clear a buffer that follows the
  4428. ; system variables.
  4429. ; The routine should check a flag or HL address and simply return if COPY
  4430. ; is in use.
  4431. ; (T-ADDR-lo would work for the system but not if COPY called externally.)
  4432. ; As a consequence of this omission the buffer will needlessly
  4433. ; be cleared when COPY is used and the screen/printer position may be set to
  4434. ; the start of the buffer and the line number to 0 (B)
  4435. ; giving an 'Out of Screen' error.
  4436. ; There seems to have been an unsuccessful attempt to circumvent the use
  4437. ; of PR_CC_hi.
  4438.  
  4439. ;; CLEAR-PRB
  4440. L0EDF:  LD      HL,$5B00        ; the location of the buffer.
  4441.         LD      (IY+$46),L      ; update PR_CC_lo - set to zero - superfluous.
  4442.         XOR     A               ; clear the accumulator.
  4443.         LD      B,A             ; set count to 256 bytes.
  4444.  
  4445. ;; PRB-BYTES
  4446. L0EE7:  LD      (HL),A          ; set addressed location to zero.
  4447.         INC     HL              ; address next byte - Note. not INC L.
  4448.         DJNZ    L0EE7           ; back to PRB-BYTES. repeat for 256 bytes.
  4449.  
  4450.         RES     1,(IY+$30)      ; set FLAGS2 - signal printer buffer is clear.
  4451.         LD      C,$21           ; set the column position .
  4452.         JP      L0DD9           ; exit via CL-SET and then PO-STORE.
  4453.  
  4454. ; -----------------
  4455. ; Copy line routine
  4456. ; -----------------
  4457. ; This routine is called from COPY and COPY-BUFF to output a line of
  4458. ; 32 bytes to the ZX Printer.
  4459. ; Output to port $FB -
  4460. ; bit 7 set - activate stylus.
  4461. ; bit 7 low - deactivate stylus.
  4462. ; bit 2 set - stops printer.
  4463. ; bit 2 reset - starts printer
  4464. ; bit 1 set - slows printer.
  4465. ; bit 1 reset - normal speed.
  4466.  
  4467. ;; COPY-LINE
  4468. ;===============================
  4469. L0EF4           IF AY_PRINTER
  4470.                 LD A,(HL)
  4471.                 RST 0X08
  4472.                 DB AY_PRN_A_
  4473.                 RET
  4474.  
  4475.                 ELSE
  4476.                 LD A,B                  ; fetch the counter 1-8 or 1-176
  4477.                 CP $03                  ; is it 01 or 02 ?.
  4478.                 SBC A,A                 ; result is $FF if so else $00.
  4479.                 ENDIF
  4480. ;===============================
  4481.                 AND $02                 ; result is 02 now else 00.
  4482.                                         ; bit 1 set slows the printer.
  4483.                 OUT ($FB),A             ; slow the printer for the
  4484.                                         ; last two lines.
  4485.         LD      D,A             ; save the mask to control the printer later.
  4486.  
  4487. ;; COPY-L-1
  4488. L0EFD:  CALL    L1F54           ; call BREAK-KEY to read keyboard immediately.
  4489.         JR      C,L0F0C         ; forward to COPY-L-2 if 'break' not pressed.
  4490.  
  4491.         LD      A,$04           ; else stop the
  4492.         OUT     ($FB),A         ; printer motor.
  4493.         EI                      ; enable interrupts.
  4494.         CALL    L0EDF           ; call routine CLEAR-PRB.
  4495.                                 ; Note. should not be cleared if COPY in use.
  4496.  
  4497. ;; REPORT-Dc
  4498. L0F0A:  RST     08H             ; ERROR-1
  4499.         DB      $0C             ; Error Report: BREAK - CONT repeats
  4500.  
  4501. ;; COPY-L-2
  4502. L0F0C:  IN      A,($FB)         ; test now to see if
  4503.         ADD     A,A             ; a printer is attached.
  4504.         RET     M               ; return if not - but continue with parent
  4505.                                 ; command.
  4506.  
  4507.         JR      NC,L0EFD        ; back to COPY-L-1 if stylus of printer not
  4508.                                 ; in position.
  4509.  
  4510.         LD      C,$20           ; set count to 32 bytes.
  4511.  
  4512. ;; COPY-L-3
  4513. L0F14:  LD      E,(HL)          ; fetch a byte from line.
  4514.         INC     HL              ; address next location. Note. not INC L.
  4515.         LD      B,$08           ; count the bits.
  4516.  
  4517. ;; COPY-L-4
  4518. L0F18:  RL      D               ; prepare mask to receive bit.
  4519.         RL      E               ; rotate leftmost print bit to carry
  4520.         RR      D               ; and back to bit 7 of D restoring bit 1
  4521.  
  4522. ;; COPY-L-5
  4523. L0F1E:  IN      A,($FB)         ; read the port.
  4524.         RRA                     ; bit 0 to carry.
  4525.         JR      NC,L0F1E        ; back to COPY-L-5 if stylus not in position.
  4526.  
  4527.         LD      A,D             ; transfer command bits to A.
  4528.         OUT     ($FB),A         ; and output to port.
  4529.         DJNZ    L0F18           ; loop back to COPY-L-4 for all 8 bits.
  4530.  
  4531.         DEC     C               ; decrease the byte count.
  4532.         JR      NZ,L0F14        ; back to COPY-L-3 until 256 bits done.
  4533.  
  4534.         RET                     ; return to calling routine COPY/COPY-BUFF.
  4535.  
  4536. ; ----------------------------------
  4537. ; Editor routine for BASIC and INPUT
  4538. ; ----------------------------------
  4539. ; The editor is called to prepare or edit a BASIC line.
  4540. ; It is also called from INPUT to input a numeric or string expression.
  4541. ; The behaviour and options are quite different in the various modes
  4542. ; and distinguished by bit 5 of FLAGX.
  4543. ;
  4544. ; This is a compact and highly versatile routine.
  4545.  
  4546. ;; EDITOR
  4547. L0F2C:  LD      HL,($5C3D)      ; fetch ERR_SP
  4548.         PUSH    HL              ; save on stack
  4549.  
  4550. ;; ED-AGAIN
  4551. L0F30:  LD      HL,L107F        ; address: ED-ERROR
  4552.         PUSH    HL              ; save address on stack and
  4553.         LD      ($5C3D),SP      ; make ERR_SP point to it.
  4554.  
  4555. ; Note. While in editing/input mode should an error occur then RST 08 will
  4556. ; update X_PTR to the location reached by CH_ADD and jump to ED-ERROR
  4557. ; where the error will be cancelled and the loop begin again from ED-AGAIN
  4558. ; above. The position of the error will be apparent when the lower screen is
  4559. ; reprinted. If no error then the re-iteration is to ED-LOOP below when
  4560. ; input is arriving from the keyboard.
  4561.  
  4562. ;; ED-LOOP
  4563. L0F38:  CALL    L15D4           ; routine WAIT-KEY gets key possibly
  4564.                                 ; changing the mode.
  4565.         PUSH    AF              ; save key.
  4566.         LD      D,$00           ; and give a short click based
  4567.         LD      E,(IY-$01)      ; on PIP value for duration.
  4568.         LD      HL,$00C8        ; and pitch.
  4569.         CALL    L03B5           ; routine BEEPER gives click - effective
  4570.                                 ; with rubber keyboard.
  4571.         POP     AF              ; get saved key value.
  4572.         LD      HL,L0F38        ; address: ED-LOOP is loaded to HL.
  4573.         PUSH    HL              ; and pushed onto stack.
  4574.  
  4575. ; At this point there is a looping return address on the stack, an error
  4576. ; handler and an input stream set up to supply characters.
  4577. ; The character that has been received can now be processed.
  4578.  
  4579.         CP      $18             ; range 24 to 255 ?
  4580.         JR      NC,L0F81        ; forward to ADD-CHAR if so.
  4581.  
  4582.         CP      $07             ; lower than 7 ?
  4583.         JR      C,L0F81         ; forward to ADD-CHAR also.
  4584.                                 ; Note. This is a 'bug' and chr$ 6, the comma
  4585.                                 ; control character, should have had an
  4586.                                 ; entry in the ED-KEYS table.
  4587.                                 ; Steven Vickers, 1984, Pitman.
  4588.  
  4589.         CP      $10             ; less than 16 ?
  4590.         JR      C,L0F92         ; forward to ED-KEYS if editing control
  4591.                                 ; range 7 to 15 dealt with by a table
  4592.  
  4593.         LD      BC,$0002        ; prepare for ink/paper etc.
  4594.         LD      D,A             ; save character in D
  4595.         CP      $16             ; is it ink/paper/bright etc. ?
  4596.         JR      C,L0F6C         ; forward to ED-CONTR if so
  4597.  
  4598.                                 ; leaves 22d AT and 23d TAB
  4599.                                 ; which can't be entered via KEY-INPUT.
  4600.                                 ; so this code is never normally executed
  4601.                                 ; when the keyboard is used for input.
  4602.  
  4603.         INC     BC              ; if it was AT/TAB - 3 locations required
  4604.         BIT     7,(IY+$37)      ; test FLAGX  - Is this INPUT LINE ?
  4605.         JP      Z,L101E         ; jump to ED-IGNORE if not, else
  4606.  
  4607.         CALL    L15D4           ; routine WAIT-KEY - input address is KEY-NEXT
  4608.                                 ; but is reset to KEY-INPUT
  4609.         LD      E,A             ; save first in E
  4610.  
  4611. ;; ED-CONTR
  4612. L0F6C:  CALL    L15D4           ; routine WAIT-KEY for control.
  4613.                                 ; input address will be key-next.
  4614.  
  4615.         PUSH    DE              ; saved code/parameters
  4616.         LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4617.         RES     0,(IY+$07)      ; set MODE to 'L'
  4618.  
  4619.         CALL    L1655           ; routine MAKE-ROOM makes 2/3 spaces at cursor
  4620.  
  4621.         POP     BC              ; restore code/parameters
  4622.         INC     HL              ; address first location
  4623.         LD      (HL),B          ; place code (ink etc.)
  4624.         INC     HL              ; address next
  4625.         LD      (HL),C          ; place possible parameter. If only one
  4626.                                 ; then DE points to this location also.
  4627.         JR      L0F8B           ; forward to ADD-CH-1
  4628.  
  4629. ; ------------------------
  4630. ; Add code to current line
  4631. ; ------------------------
  4632. ; this is the branch used to add normal non-control characters
  4633. ; with ED-LOOP as the stacked return address.
  4634. ; it is also the OUTPUT service routine for system channel 'R'.
  4635.  
  4636. ;; ADD-CHAR
  4637. L0F81:  RES     0,(IY+$07)      ; set MODE to 'L'
  4638.  
  4639. X0F85:  LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4640.         CALL    L1652           ; routine ONE-SPACE creates one space.
  4641.  
  4642. ; either a continuation of above or from ED-CONTR with ED-LOOP on stack.
  4643.  
  4644. ;; ADD-CH-1
  4645. L0F8B:  LD      (DE),A          ; load current character to last new location.
  4646.         INC     DE              ; address next
  4647.         LD      ($5C5B),DE      ; and update K_CUR system variable.
  4648.         RET                     ; return - either a simple return
  4649.                                 ; from ADD-CHAR or to ED-LOOP on stack.
  4650.  
  4651. ; ---
  4652.  
  4653. ; a branch of the editing loop to deal with control characters
  4654. ; using a look-up table.
  4655.  
  4656. ;; ED-KEYS
  4657. L0F92:  LD      E,A             ; character to E.
  4658.         LD      D,$00           ; prepare to add.
  4659.         LD      HL,L0FA0 - 7    ; base address of editing keys table. $0F99
  4660.         ADD     HL,DE           ; add E
  4661.         LD      E,(HL)          ; fetch offset to E
  4662.         ADD     HL,DE           ; add offset for address of handling routine.
  4663.         PUSH    HL              ; push the address on machine stack.
  4664.         LD      HL,($5C5B)      ; load address of cursor from K_CUR.
  4665.         RET                     ; an make an indirect jump forward to routine.
  4666.  
  4667. ; ------------------
  4668. ; Editing keys table
  4669. ; ------------------
  4670. ; For each code in the range $07 to $0F this table contains a
  4671. ; single offset byte to the routine that services that code.
  4672. ; Note. for what was intended there should also have been an
  4673. ; entry for chr$ 6 with offset to ed-symbol.
  4674.  
  4675. ;; ed-keys-t
  4676. L0FA0:  DB    L0FA9 - $  ; 07d offset $09 to Address: ED-EDIT
  4677.         DB    L1007 - $  ; 08d offset $66 to Address: ED-LEFT
  4678.         DB    L100C - $  ; 09d offset $6A to Address: ED-RIGHT
  4679.         DB    L0FF3 - $  ; 10d offset $50 to Address: ED-DOWN
  4680.         DB    L1059 - $  ; 11d offset $B5 to Address: ED-UP
  4681.         DB    L1015 - $  ; 12d offset $70 to Address: ED-DELETE
  4682.         DB    L1024 - $  ; 13d offset $7E to Address: ED-ENTER
  4683.         DB    L1076 - $  ; 14d offset $CF to Address: ED-SYMBOL
  4684.         DB    L107C - $  ; 15d offset $D4 to Address: ED-GRAPH
  4685.  
  4686. ; ---------------
  4687. ; Handle EDIT key
  4688. ; ---------------
  4689. ; The user has pressed SHIFT 1 to bring edit line down to bottom of screen.
  4690. ; Alternatively the user wishes to clear the input buffer and start again.
  4691. ; Alternatively ...
  4692.  
  4693. ;; ED-EDIT
  4694. L0FA9:  LD      HL,($5C49)      ; fetch E_PPC the last line number entered.
  4695.                                 ; Note. may not exist and may follow program.
  4696.         BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4697.         JP      NZ,L1097        ; jump forward to CLEAR-SP if not in editor.
  4698.  
  4699.         CALL    L196E           ; routine LINE-ADDR to find address of line
  4700.                                 ; or following line if it doesn't exist.
  4701.         CALL    L1695           ; routine LINE-NO will get line number from
  4702.                                 ; address or previous line if at end-marker.
  4703.         LD      A,D             ; if there is no program then DE will
  4704.         OR      E               ; contain zero so test for this.
  4705.         JP      Z,L1097         ; jump to to CLEAR-SP if so.
  4706.  
  4707. ; Note. at this point we have a validated line number, not just an
  4708. ; approximation and it would be best to update E_PPC with the true
  4709. ; cursor line value which would enable the line cursor to be suppressed
  4710. ; in all situations - see shortly.
  4711.  
  4712.         PUSH    HL              ; save address of line.
  4713.         INC     HL              ; address low byte of length.
  4714.         LD      C,(HL)          ; transfer to C
  4715.         INC     HL              ; next to high byte
  4716.         LD      B,(HL)          ; transfer to B.
  4717.         LD      HL,$000A        ; an overhead of ten bytes
  4718.         ADD     HL,BC           ; is added to length.
  4719.         LD      B,H             ; transfer adjusted value
  4720.         LD      C,L             ; to BC register.
  4721.         CALL    L1F05           ; routine TEST-ROOM checks free memory.
  4722.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  4723.         LD      HL,($5C51)      ; address CURCHL
  4724.         EX      (SP),HL         ; swap with line address on stack
  4725.         PUSH    HL              ; save line address underneath
  4726.  
  4727.         LD      A,$FF           ; select system channel 'R'
  4728.         CALL    L1601           ; routine CHAN-OPEN opens it
  4729.  
  4730.         POP     HL              ; drop line address
  4731.         DEC     HL              ; make it point to first byte of line num.
  4732.         DEC     (IY+$0F)        ; decrease E_PPC_lo to suppress line cursor.
  4733.                                 ; Note. ineffective when E_PPC is one
  4734.                                 ; greater than last line of program perhaps
  4735.                                 ; as a result of a delete.
  4736.                                 ; credit. Paul Harrison 1982.
  4737.  
  4738.         CALL    L1855           ; routine OUT-LINE outputs the BASIC line
  4739.                                 ; to the editing area.
  4740.         INC     (IY+$0F)        ; restore E_PPC_lo to the previous value.
  4741.         LD      HL,($5C59)      ; address E_LINE in editing area.
  4742.         INC     HL              ; advance
  4743.         INC     HL              ; past space
  4744.         INC     HL              ; and digit characters
  4745.         INC     HL              ; of line number.
  4746.  
  4747.         LD      ($5C5B),HL      ; update K_CUR to address start of BASIC.
  4748.         POP     HL              ; restore the address of CURCHL.
  4749.         CALL    L1615           ; routine CHAN-FLAG sets flags for it.
  4750.         RET                     ; RETURN to ED-LOOP.
  4751.  
  4752. ; -------------------
  4753. ; Cursor down editing
  4754. ; -------------------
  4755. ; The BASIC lines are displayed at the top of the screen and the user
  4756. ; wishes to move the cursor down one line in edit mode.
  4757. ; In input mode this key can be used as an alternative to entering STOP.
  4758.  
  4759. ;; ED-DOWN
  4760. L0FF3:  BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  4761.         JR      NZ,L1001        ; skip to ED-STOP if so
  4762.  
  4763.         LD      HL,$5C49        ; address E_PPC - 'current line'
  4764.         CALL    L190F           ; routine LN-FETCH fetches number of next
  4765.                                 ; line or same if at end of program.
  4766.         JR      L106E           ; forward to ED-LIST to produce an
  4767.                                 ; automatic listing.
  4768.  
  4769. ; ---
  4770.  
  4771. ;; ED-STOP
  4772. L1001:  LD      (IY+$00),$10    ; set ERR_NR to 'STOP in INPUT' code
  4773.         JR      L1024           ; forward to ED-ENTER to produce error.
  4774.  
  4775. ; -------------------
  4776. ; Cursor left editing
  4777. ; -------------------
  4778. ; This acts on the cursor in the lower section of the screen in both
  4779. ; editing and input mode.
  4780.  
  4781. ;; ED-LEFT
  4782. L1007:  CALL    L1031           ; routine ED-EDGE moves left if possible
  4783.         JR      L1011           ; forward to ED-CUR to update K-CUR
  4784.                                 ; and return to ED-LOOP.
  4785.  
  4786. ; --------------------
  4787. ; Cursor right editing
  4788. ; --------------------
  4789. ; This acts on the cursor in the lower screen in both editing and input
  4790. ; mode and moves it to the right.
  4791.  
  4792. ;; ED-RIGHT
  4793. L100C:  LD      A,(HL)          ; fetch addressed character.
  4794.         CP      $0D             ; is it carriage return ?
  4795.         RET     Z               ; return if so to ED-LOOP
  4796.  
  4797.         INC     HL              ; address next character
  4798.  
  4799. ;; ED-CUR
  4800. L1011:  LD      ($5C5B),HL      ; update K_CUR system variable
  4801.         RET                     ; return to ED-LOOP
  4802.  
  4803. ; --------------
  4804. ; DELETE editing
  4805. ; --------------
  4806. ; This acts on the lower screen and deletes the character to left of
  4807. ; cursor. If control characters are present these are deleted first
  4808. ; leaving the naked parameter (0-7) which appears as a '?' except in the
  4809. ; case of chr$ 6 which is the comma control character. It is not mandatory
  4810. ; to delete these second characters.
  4811.  
  4812. ;; ED-DELETE
  4813. L1015:  CALL    L1031           ; routine ED-EDGE moves cursor to left.
  4814.         LD      BC,$0001        ; of character to be deleted.
  4815.         JP      L19E8           ; to RECLAIM-2 reclaim the character.
  4816.  
  4817. ; ------------------------------------------
  4818. ; Ignore next 2 codes from key-input routine
  4819. ; ------------------------------------------
  4820. ; Since AT and TAB cannot be entered this point is never reached
  4821. ; from the keyboard. If inputting from a tape device or network then
  4822. ; the control and two following characters are ignored and processing
  4823. ; continues as if a carriage return had been received.
  4824. ; Here, perhaps, another Spectrum has said print #15; AT 0,0; "This is yellow"
  4825. ; and this one is interpreting input #15; a$.
  4826.  
  4827. ;; ED-IGNORE
  4828. L101E:  CALL    L15D4           ; routine WAIT-KEY to ignore keystroke.
  4829.         CALL    L15D4           ; routine WAIT-KEY to ignore next key.
  4830.  
  4831. ; -------------
  4832. ; Enter/newline
  4833. ; -------------
  4834. ; The enter key has been pressed to have BASIC line or input accepted.
  4835.  
  4836. ;; ED-ENTER
  4837. L1024:  POP     HL              ; discard address ED-LOOP
  4838.         POP     HL              ; drop address ED-ERROR
  4839.  
  4840. ;; ED-END
  4841. L1026:  POP     HL              ; the previous value of ERR_SP
  4842.         LD      ($5C3D),HL      ; is restored to ERR_SP system variable
  4843.         BIT     7,(IY+$00)      ; is ERR_NR $FF (= 'OK') ?
  4844.         RET     NZ              ; return if so
  4845.  
  4846.         LD      SP,HL           ; else put error routine on stack
  4847.         RET                     ; and make an indirect jump to it.
  4848.  
  4849. ; -----------------------------
  4850. ; Move cursor left when editing
  4851. ; -----------------------------
  4852. ; This routine moves the cursor left. The complication is that it must
  4853. ; not position the cursor between control codes and their parameters.
  4854. ; It is further complicated in that it deals with TAB and AT characters
  4855. ; which are never present from the keyboard.
  4856. ; The method is to advance from the beginning of the line each time,
  4857. ; jumping one, two, or three characters as necessary saving the original
  4858. ; position at each jump in DE. Once it arrives at the cursor then the next
  4859. ; legitimate leftmost position is in DE.
  4860.  
  4861. ;; ED-EDGE
  4862. L1031:  SCF                     ; carry flag must be set to call the nested
  4863.         CALL    L1195           ; subroutine SET-DE.
  4864.                                 ; if input   then DE=WORKSP
  4865.                                 ; if editing then DE=E_LINE
  4866.         SBC     HL,DE           ; subtract address from start of line
  4867.         ADD     HL,DE           ; and add back.
  4868.         INC     HL              ; adjust for carry.
  4869.         POP     BC              ; drop return address
  4870.         RET     C               ; return to ED-LOOP if already at left
  4871.                                 ; of line.
  4872.  
  4873.         PUSH    BC              ; resave return address - ED-LOOP.
  4874.         LD      B,H             ; transfer HL - cursor address
  4875.         LD      C,L             ; to BC register pair.
  4876.                                 ; at this point DE addresses start of line.
  4877.  
  4878. ;; ED-EDGE-1
  4879. L103E:  LD      H,D             ; transfer DE - leftmost pointer
  4880.         LD      L,E             ; to HL
  4881.         INC     HL              ; address next leftmost character to
  4882.                                 ; advance position each time.
  4883.         LD      A,(DE)          ; pick up previous in A
  4884.         AND     $F0             ; lose the low bits
  4885.         CP      $10             ; is it INK to TAB $10-$1F ?
  4886.                                 ; that is, is it followed by a parameter ?
  4887.         JR      NZ,L1051        ; to ED-EDGE-2 if not
  4888.                                 ; HL has been incremented once
  4889.  
  4890.         INC     HL              ; address next as at least one parameter.
  4891.  
  4892. ; in fact since 'tab' and 'at' cannot be entered the next section seems
  4893. ; superfluous.
  4894. ; The test will always fail and the jump to ED-EDGE-2 will be taken.
  4895.  
  4896.         LD      A,(DE)          ; reload leftmost character
  4897.         SUB     $17             ; decimal 23 ('tab')
  4898.         ADC     A,$00           ; will be 0 for 'tab' and 'at'.
  4899.         JR      NZ,L1051        ; forward to ED-EDGE-2 if not
  4900.                                 ; HL has been incremented twice
  4901.  
  4902.         INC     HL              ; increment a third time for 'at'/'tab'
  4903.  
  4904. ;; ED-EDGE-2
  4905. L1051:  AND     A               ; prepare for true subtraction
  4906.         SBC     HL,BC           ; subtract cursor address from pointer
  4907.         ADD     HL,BC           ; and add back
  4908.                                 ; Note when HL matches the cursor position BC,
  4909.                                 ; there is no carry and the previous
  4910.                                 ; position is in DE.
  4911.         EX      DE,HL           ; transfer result to DE if looping again.
  4912.                                 ; transfer DE to HL to be used as K-CUR
  4913.                                 ; if exiting loop.
  4914.         JR      C,L103E         ; back to ED-EDGE-1 if cursor not matched.
  4915.  
  4916.         RET                     ; return.
  4917.  
  4918. ; -----------------
  4919. ; Cursor up editing
  4920. ; -----------------
  4921. ; The main screen displays part of the BASIC program and the user wishes
  4922. ; to move up one line scrolling if necessary.
  4923. ; This has no alternative use in input mode.
  4924.  
  4925. ;; ED-UP
  4926. L1059:  BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4927.         RET     NZ              ; return if not in editor - to ED-LOOP.
  4928.  
  4929.         LD      HL,($5C49)      ; get current line from E_PPC
  4930.         CALL    L196E           ; routine LINE-ADDR gets address
  4931.         EX      DE,HL           ; and previous in DE
  4932.         CALL    L1695           ; routine LINE-NO gets prev line number
  4933.         LD      HL,$5C4A        ; set HL to E_PPC_hi as next routine stores
  4934.                                 ; top first.
  4935.         CALL    L191C           ; routine LN-STORE loads DE value to HL
  4936.                                 ; high byte first - E_PPC_lo takes E
  4937.  
  4938. ; this branch is also taken from ed-down.
  4939.  
  4940. ;; ED-LIST
  4941. L106E:  CALL    L1795           ; routine AUTO-LIST lists to upper screen
  4942.                                 ; including adjusted current line.
  4943.         LD      A,$00           ; select lower screen again
  4944.         JP      L1601           ; exit via CHAN-OPEN to ED-LOOP
  4945.  
  4946. ; --------------------------------
  4947. ; Use of symbol and graphics codes
  4948. ; --------------------------------
  4949. ; These will not be encountered with the keyboard but would be handled
  4950. ; otherwise as follows.
  4951. ; As noted earlier, Vickers says there should have been an entry in
  4952. ; the KEYS table for chr$ 6 which also pointed here.
  4953. ; If, for simplicity, two Spectrums were both using #15 as a bi-directional
  4954. ; channel connected to each other:-
  4955. ; then when the other Spectrum has said PRINT #15; x, y
  4956. ; input #15; i ; j  would treat the comma control as a newline and the
  4957. ; control would skip to input j.
  4958. ; You can get round the missing chr$ 6 handler by sending multiple print
  4959. ; items separated by a newline '.
  4960.  
  4961. ; chr$14 would have the same functionality.
  4962.  
  4963. ; This is chr$ 14.
  4964. ;; ED-SYMBOL
  4965. L1076:  BIT     7,(IY+$37)      ; test FLAGX - is this INPUT LINE ?
  4966.         JR      Z,L1024         ; back to ED-ENTER if not to treat as if
  4967.                                 ; enter had been pressed.
  4968.                                 ; else continue and add code to buffer.
  4969.  
  4970. ; Next is chr$ 15
  4971. ; Note that ADD-CHAR precedes the table so we can't offset to it directly.
  4972.  
  4973. ;; ED-GRAPH
  4974. L107C:  JP      L0F81           ; jump back to ADD-CHAR
  4975.  
  4976. ; --------------------
  4977. ; Editor error routine
  4978. ; --------------------
  4979. ; If an error occurs while editing, or inputting, then ERR_SP
  4980. ; points to the stack location holding address ED_ERROR.
  4981.  
  4982. ;; ED-ERROR
  4983. L107F:  BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  4984.         JR      Z,L1026         ; back to ED-END if not.
  4985.  
  4986. ; but as long as we're editing lines or inputting from the keyboard, then
  4987. ; we've run out of memory so give a short rasp.
  4988.  
  4989.         LD      (IY+$00),$FF    ; reset ERR_NR to 'OK'.
  4990.         LD      D,$00           ; prepare for beeper.
  4991.         LD      E,(IY-$02)      ; use RASP value.
  4992.         LD      HL,$1A90        ; set a duration.
  4993.         CALL    L03B5           ; routine BEEPER emits a warning rasp.
  4994.         JP      L0F30           ; to ED-AGAIN to re-stack address of
  4995.                                 ; this routine and make ERR_SP point to it.
  4996.  
  4997. ; ---------------------
  4998. ; Clear edit/work space
  4999. ; ---------------------
  5000. ; The editing area or workspace is cleared depending on context.
  5001. ; This is called from ED-EDIT to clear workspace if edit key is
  5002. ; used during input, to clear editing area if no program exists
  5003. ; and to clear editing area prior to copying the edit line to it.
  5004. ; It is also used by the error routine to clear the respective
  5005. ; area depending on FLAGX.
  5006.  
  5007. ;; CLEAR-SP
  5008. L1097:  PUSH    HL              ; preserve HL
  5009.         CALL    L1190           ; routine SET-HL
  5010.                                 ; if in edit   HL = WORKSP-1, DE = E_LINE
  5011.                                 ; if in input  HL = STKBOT,   DE = WORKSP
  5012.         DEC     HL              ; adjust
  5013.         CALL    L19E5           ; routine RECLAIM-1 reclaims space
  5014.         LD      ($5C5B),HL      ; set K_CUR to start of empty area
  5015.         LD      (IY+$07),$00    ; set MODE to 'KLC'
  5016.         POP     HL              ; restore HL.
  5017.         RET                     ; return.
  5018.  
  5019. ; ---------------------
  5020. ; Handle keyboard input
  5021. ; ---------------------
  5022. ; This is the service routine for the input stream of the keyboard
  5023. ; channel 'K'.
  5024.  
  5025. ;; KEY-INPUT
  5026. L10A8:  BIT     3,(IY+$02)      ; test TV_FLAG  - has a key been pressed in
  5027.                                 ; editor ?
  5028.         CALL    NZ,L111D        ; routine ED-COPY if so to reprint the lower
  5029.                                 ; screen at every keystroke.
  5030.         AND     A               ; clear carry - required exit condition.
  5031.         BIT     5,(IY+$01)      ; test FLAGS  - has a new key been pressed ?
  5032.         RET     Z               ; return if not.
  5033.  
  5034.         LD      A,($5C08)       ; system variable LASTK will hold last key -
  5035.                                 ; from the interrupt routine.
  5036.         RES     5,(IY+$01)      ; update FLAGS  - reset the new key flag.
  5037.         PUSH    AF              ; save the input character.
  5038.         BIT     5,(IY+$02)      ; test TV_FLAG  - clear lower screen ?
  5039.         CALL    NZ,L0D6E        ; routine CLS-LOWER if so.
  5040.  
  5041.         POP     AF              ; restore the character code.
  5042.         CP      $20             ; if space or higher then
  5043.         JR      NC,L111B        ; forward to KEY-DONE2 and return with carry
  5044.                                 ; set to signal key-found.
  5045.  
  5046.         CP      $10             ; with 16d INK and higher skip
  5047.         JR      NC,L10FA        ; forward to KEY-CONTR.
  5048.  
  5049.         CP      $06             ; for 6 - 15d
  5050.         JR      NC,L10DB        ; skip forward to KEY-M-CL to handle Modes
  5051.                                 ; and CapsLock.
  5052.  
  5053. ; that only leaves 0-5, the flash bright inverse switches.
  5054.  
  5055.         LD      B,A             ; save character in B
  5056.         AND     $01             ; isolate the embedded parameter (0/1).
  5057.         LD      C,A             ; and store in C
  5058.         LD      A,B             ; re-fetch copy (0-5)
  5059.         RRA                     ; halve it 0, 1 or 2.
  5060.         ADD     A,$12           ; add 18d gives 'flash', 'bright'
  5061.                                 ; and 'inverse'.
  5062.         JR      L1105           ; forward to KEY-DATA with the
  5063.                                 ; parameter (0/1) in C.
  5064.  
  5065. ; ---
  5066.  
  5067. ; Now separate capslock 06 from modes 7-15.
  5068.  
  5069. ;; KEY-M-CL
  5070. L10DB:  JR      NZ,L10E6        ; forward to KEY-MODE if not 06 (capslock)
  5071.  
  5072.         LD      HL,$5C6A        ; point to FLAGS2
  5073.         LD      A,$08           ; value 00000100
  5074.         XOR     (HL)            ; toggle BIT 2 of FLAGS2 the capslock bit
  5075.         LD      (HL),A          ; and store result in FLAGS2 again.
  5076.         JR      L10F4           ; forward to KEY-FLAG to signal no-key.
  5077.  
  5078. ; ---
  5079.  
  5080. ;; KEY-MODE
  5081. L10E6:  CP      $0E             ; compare with chr 14d
  5082.         RET     C               ; return with carry set "key found" for
  5083.                                 ; codes 7 - 13d leaving 14d and 15d
  5084.                                 ; which are converted to mode codes.
  5085.  
  5086.         SUB     $0D             ; subtract 13d leaving 1 and 2
  5087.                                 ; 1 is 'E' mode, 2 is 'G' mode.
  5088.         LD      HL,$5C41        ; address the MODE system variable.
  5089.         CP      (HL)            ; compare with existing value before
  5090.         LD      (HL),A          ; inserting the new value.
  5091.         JR      NZ,L10F4        ; forward to KEY-FLAG if it has changed.
  5092.  
  5093.         LD      (HL),$00        ; else make MODE zero - KLC mode
  5094.                                 ; Note. while in Extended/Graphics mode,
  5095.                                 ; the Extended Mode/Graphics key is pressed
  5096.                                 ; again to get out.
  5097.  
  5098. ;; KEY-FLAG
  5099. L10F4:  SET     3,(IY+$02)      ; update TV_FLAG  - show key state has changed
  5100.         CP      A               ; clear carry and reset zero flags -
  5101.                                 ; no actual key returned.
  5102.         RET                     ; make the return.
  5103.  
  5104. ; ---
  5105.  
  5106. ; now deal with colour controls - 16-23 ink, 24-31 paper
  5107.  
  5108. ;; KEY-CONTR
  5109. L10FA:  LD      B,A             ; make a copy of character.
  5110.         AND     $07             ; mask to leave bits 0-7
  5111.         LD      C,A             ; and store in C.
  5112.         LD      A,$10           ; initialize to 16d - INK.
  5113.         BIT     3,B             ; was it paper ?
  5114.         JR      NZ,L1105        ; forward to KEY-DATA with INK 16d and
  5115.                                 ; colour in C.
  5116.  
  5117.         INC     A               ; else change from INK to PAPER (17d) if so.
  5118.  
  5119. ;; KEY-DATA
  5120. L1105:  LD      (IY-$2D),C      ; put the colour (0-7)/state(0/1) in KDATA
  5121.         LD      DE,L110D        ; address: KEY-NEXT will be next input stream
  5122.         JR      L1113           ; forward to KEY-CHAN to change it ...
  5123.  
  5124. ; ---
  5125.  
  5126. ; ... so that INPUT_AD directs control to here at next call to WAIT-KEY
  5127.  
  5128. ;; KEY-NEXT
  5129. L110D:  LD      A,($5C0D)       ; pick up the parameter stored in KDATA.
  5130.         LD      DE,L10A8        ; address: KEY-INPUT will be next input stream
  5131.                                 ; continue to restore default channel and
  5132.                                 ; make a return with the control code.
  5133.  
  5134. ;; KEY-CHAN
  5135. L1113:  LD      HL,($5C4F)      ; address start of CHANNELS area using CHANS
  5136.                                 ; system variable.
  5137.                                 ; Note. One might have expected CURCHL to
  5138.                                 ; have been used.
  5139.         INC     HL              ; step over the
  5140.         INC     HL              ; output address
  5141.         LD      (HL),E          ; and update the input
  5142.         INC     HL              ; routine address for
  5143.         LD      (HL),D          ; the next call to WAIT-KEY.
  5144.  
  5145. ;; KEY-DONE2
  5146. L111B:  SCF                     ; set carry flag to show a key has been found
  5147.         RET                     ; and return.
  5148.  
  5149. ; --------------------
  5150. ; Lower screen copying
  5151. ; --------------------
  5152. ; This subroutine is called whenever the line in the editing area or
  5153. ; input workspace is required to be printed to the lower screen.
  5154. ; It is by calling this routine after any change that the cursor, for
  5155. ; instance, appears to move to the left.
  5156. ; Remember the edit line will contain characters and tokens
  5157. ; e.g. "1000 LET a = 1" is 12 characters.
  5158.  
  5159. ;; ED-COPY
  5160. L111D:  CALL    L0D4D           ; routine TEMPS sets temporary attributes.
  5161.         RES     3,(IY+$02)      ; update TV_FLAG  - signal no change in mode
  5162.         RES     5,(IY+$02)      ; update TV_FLAG  - signal don't clear lower
  5163.                                 ; screen.
  5164.         LD      HL,($5C8A)      ; fetch SPOSNL
  5165.         PUSH    HL              ; and save on stack.
  5166.  
  5167.         LD      HL,($5C3D)      ; fetch ERR_SP
  5168.         PUSH    HL              ; and save also
  5169.         LD      HL,L1167        ; address: ED-FULL
  5170.         PUSH    HL              ; is pushed as the error routine
  5171.         LD      ($5C3D),SP      ; and ERR_SP made to point to it.
  5172.  
  5173.         LD      HL,($5C82)      ; fetch ECHO_E
  5174.         PUSH    HL              ; and push also
  5175.  
  5176.         SCF                     ; set carry flag to control SET-DE
  5177.         CALL    L1195           ; call routine SET-DE
  5178.                                 ; if in input DE = WORKSP
  5179.                                 ; if in edit  DE = E_LINE
  5180.         EX      DE,HL           ; start address to HL
  5181.  
  5182.         CALL    L187D           ; routine OUT-LINE2 outputs entire line up to
  5183.                                 ; carriage return including initial
  5184.                                 ; characterized line number when present.
  5185.         EX      DE,HL           ; transfer new address to DE
  5186.         CALL    L18E1           ; routine OUT-CURS considers a
  5187.                                 ; terminating cursor.
  5188.  
  5189.         LD      HL,($5C8A)      ; fetch updated SPOSNL
  5190.         EX      (SP),HL         ; exchange with ECHO_E on stack
  5191.         EX      DE,HL           ; transfer ECHO_E to DE
  5192.         CALL    L0D4D           ; routine TEMPS to re-set attributes
  5193.                                 ; if altered.
  5194.  
  5195. ; the lower screen was not cleared, at the outset, so if deleting then old
  5196. ; text from a previous print may follow this line and requires blanking.
  5197.  
  5198. ;; ED-BLANK
  5199. L1150:  LD      A,($5C8B)       ; fetch SPOSNL_hi is current line
  5200.         SUB     D               ; compare with old
  5201.         JR      C,L117C         ; forward to ED-C-DONE if no blanking
  5202.  
  5203.         JR      NZ,L115E        ; forward to ED-SPACES if line has changed
  5204.  
  5205.         LD      A,E             ; old column to A
  5206.         SUB     (IY+$50)        ; subtract new in SPOSNL_lo
  5207.         JR      NC,L117C        ; forward to ED-C-DONE if no backfilling.
  5208.  
  5209. ;; ED-SPACES
  5210. L115E:  LD      A,$20           ; prepare a space.
  5211.         PUSH    DE              ; save old line/column.
  5212.         CALL    L09F4           ; routine PRINT-OUT prints a space over
  5213.                                 ; any text from previous print.
  5214.                                 ; Note. Since the blanking only occurs when
  5215.                                 ; using $09F4 to print to the lower screen,
  5216.                                 ; there is no need to vector via a RST 10
  5217.                                 ; and we can use this alternate set.
  5218.         POP     DE              ; restore the old line column.
  5219.         JR      L1150           ; back to ED-BLANK until all old text blanked.
  5220.  
  5221. ; -------
  5222. ; ED-FULL
  5223. ; -------
  5224. ; this is the error routine addressed by ERR_SP. This is not for the out of
  5225. ; memory situation as we're just printing. The pitch and duration are exactly
  5226. ; the same as used by ED-ERROR from which this has been augmented. The
  5227. ; situation is that the lower screen is full and a rasp is given to suggest
  5228. ; that this is perhaps not the best idea you've had that day.
  5229.  
  5230. ;; ED-FULL
  5231. L1167:  LD      D,$00           ; prepare to moan.
  5232.         LD      E,(IY-$02)      ; fetch RASP value.
  5233.         LD      HL,$1A90        ; set duration.
  5234.         CALL    L03B5           ; routine BEEPER.
  5235.         LD      (IY+$00),$FF    ; clear ERR_NR.
  5236.         LD      DE,($5C8A)      ; fetch SPOSNL.
  5237.         JR      L117E           ; forward to ED-C-END
  5238.  
  5239. ; -------
  5240.  
  5241. ; the exit point from line printing continues here.
  5242.  
  5243. ;; ED-C-DONE
  5244. L117C:  POP     DE              ; fetch new line/column.
  5245.         POP     HL              ; fetch the error address.
  5246.  
  5247. ; the error path rejoins here.
  5248.  
  5249. ;; ED-C-END
  5250. L117E:  POP     HL              ; restore the old value of ERR_SP.
  5251.         LD      ($5C3D),HL      ; update the system variable ERR_SP
  5252.         POP     BC              ; old value of SPOSN_L
  5253.         PUSH    DE              ; save new value
  5254.         CALL    L0DD9           ; routine CL-SET and PO-STORE
  5255.                                 ; update ECHO_E and SPOSN_L from BC
  5256.         POP     HL              ; restore new value
  5257.         LD      ($5C82),HL      ; and update ECHO_E
  5258.         LD      (IY+$26),$00    ; make error pointer X_PTR_hi out of bounds
  5259.         RET                     ; return
  5260.  
  5261. ; -----------------------------------------------
  5262. ; Point to first and last locations of work space
  5263. ; -----------------------------------------------
  5264. ; These two nested routines ensure that the appropriate pointers are
  5265. ; selected for the editing area or workspace. The routines that call
  5266. ; these routines are designed to work on either area.
  5267.  
  5268. ; this routine is called once
  5269. ;; SET-HL
  5270. L1190:  LD      HL,($5C61)      ; fetch WORKSP to HL.
  5271.         DEC     HL              ; point to last location of editing area.
  5272.         AND     A               ; clear carry to limit exit points to first
  5273.                                 ; or last.
  5274.  
  5275. ; this routine is called with carry set and exits at a conditional return.
  5276.  
  5277. ;; SET-DE
  5278. L1195:  LD      DE,($5C59)      ; fetch E_LINE to DE
  5279.         BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  5280.         RET     Z               ; return now if in editing mode
  5281.  
  5282.         LD      DE,($5C61)      ; fetch WORKSP to DE
  5283.         RET     C               ; return if carry set ( entry = set-de)
  5284.  
  5285.         LD      HL,($5C63)      ; fetch STKBOT to HL as well
  5286.         RET                     ; and return  (entry = set-hl (in input))
  5287.  
  5288. ; -------------------------------
  5289. ; Remove floating point from line
  5290. ; -------------------------------
  5291. ; When a BASIC LINE or the INPUT BUFFER is parsed any numbers will have
  5292. ; an invisible chr 14d inserted after them and the 5-byte integer or
  5293. ; floating point form inserted after that. Similar invisible value holders
  5294. ; are also created after the numeric and string variables in a DEF FN list.
  5295. ; This routine removes these 'compiled' numbers from the edit line or
  5296. ; input workspace.
  5297.  
  5298. ;; REMOVE-FP
  5299. L11A7:  LD      A,(HL)          ; fetch character
  5300.         CP      $0E             ; is it the number marker ?
  5301.         LD      BC,$0006        ; prepare for six bytes
  5302.         CALL    Z,L19E8         ; routine RECLAIM-2 reclaims space if $0E
  5303.         LD      A,(HL)          ; reload next (or same) character
  5304.         INC     HL              ; and advance address
  5305.         CP      $0D             ; end of line or input buffer ?
  5306.         JR      NZ,L11A7        ; back to REMOVE-FP until entire line done.
  5307.  
  5308.         RET                     ; return
  5309.  
  5310.  
  5311. ;*********************************
  5312. ;** Part 6. EXECUTIVE ROUTINES  **
  5313. ;*********************************
  5314.  
  5315.  
  5316. ; The memory.
  5317. ;
  5318. ; +---------+-----------+------------+--------------+-------------+--
  5319. ; | BASIC   |  Display  | Attributes | ZX Printer   |    System   |
  5320. ; |  ROM    |   File    |    File    |   Buffer     |  Variables  |
  5321. ; +---------+-----------+------------+--------------+-------------+--
  5322. ; ^         ^           ^            ^              ^             ^
  5323. ; $0000   $4000       $5800        $5B00          $5C00         $5CB6 = CHANS
  5324. ;
  5325. ;
  5326. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5327. ;    | Channel  |$80|  BASIC  | Variables |$80| Edit Line  |NL|$80|
  5328. ;    |   Info   |   | Program |   Area    |   | or Command |  |   |
  5329. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5330. ;    ^              ^         ^               ^                   ^
  5331. ;  CHANS           PROG      VARS           E_LINE              WORKSP
  5332. ;
  5333. ;
  5334. ;                             ---5-->         <---2---  <--3---
  5335. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5336. ;    | INPUT |NL| Temporary  | Calc. | Spare | Machine | GOSUB |?|$3E| UDGs |
  5337. ;    | data  |  | Work Space | Stack |       |  Stack  | Stack | |   |      |
  5338. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5339. ;    ^                       ^       ^       ^                   ^   ^      ^
  5340. ;  WORKSP                  STKBOT  STKEND   sp               RAMTOP UDG  P_RAMT
  5341. ;                                                                        
  5342.  
  5343. ; -------------------
  5344. ; Handle NEW command
  5345. ; -------------------
  5346. ; The NEW command is about to set all RAM below RAMTOP to zero and
  5347. ; then re-initialize the system. All RAM above RAMTOP should, and will be,
  5348. ; preserved.
  5349. ; There is nowhere to store values in RAM or on the stack which becomes
  5350. ; inoperable. Similarly PUSH and CALL instructions cannot be used to
  5351. ; store values or section common code. The alternate register set is the only
  5352. ; place available to store 3 persistent 16-bit system variables.
  5353.  
  5354. ;; NEW
  5355. L11B7:  DI                      ; disable interrupts - machine stack will be
  5356.                                 ; cleared.
  5357.         LD      A,$FF           ; flag coming from NEW.
  5358.         LD      DE,($5CB2)      ; fetch RAMTOP as top value.
  5359.         EXX                     ; switch in alternate set.
  5360.         LD      BC,($5CB4)      ; fetch P-RAMT differs on 16K/48K machines.
  5361.         LD      DE,($5C38)      ; fetch RASP/PIP.
  5362.         LD      HL,($5C7B)      ; fetch UDG    differs on 16K/48K machines.
  5363.         EXX                     ; switch back to main set and continue into...
  5364.  
  5365. ; ---------------------------
  5366. ; Main entry (initialization)
  5367. ; ---------------------------
  5368. ; This common code tests ram and sets it to zero re-initializing
  5369. ; all the non-zero system variables and channel information.
  5370. ; The A register tells if coming from START or NEW
  5371.  
  5372. ;; START-NEW
  5373. L11CB:  LD      B,A             ; save the flag for later branching.
  5374.  
  5375.         LD      A,$07           ; select a white border
  5376.         OUT     ($FE),A         ; and set it now.
  5377.  
  5378.         LD      A,$3F           ; load accumulator with last page in ROM.
  5379.         LD      I,A             ; set the I register - this remains constant
  5380.                                 ; and can't be in range $40 - $7F as 'snow'
  5381.                                 ; appears on the screen.
  5382.         NOP                     ; these seem unnecessary.
  5383.         NOP                     ;
  5384.         NOP                     ;
  5385.         NOP                     ;
  5386.         NOP                     ;
  5387.         NOP                     ;
  5388.  
  5389. ; ------------
  5390. ; Check RAM
  5391. ; ------------
  5392. ; Typically a Spectrum will have 16K or 48K of Ram and this code will
  5393. ; test it all till it finds an unpopulated location or, less likely, a
  5394. ; faulty location. Usually it stops when it reaches the top $FFFF or
  5395. ; in the case of NEW the supplied top value. The entire screen turns
  5396. ; black with sometimes red stripes on black paper visible.
  5397.  
  5398. ;; ram-check
  5399. L11DA:  LD      H,D             ; transfer the top value to
  5400.         LD      L,E             ; the HL register pair.
  5401.  
  5402. ;; RAM-FILL
  5403. L11DC:  LD      (HL),$02        ; load with 2 - red ink on black paper
  5404.         DEC     HL              ; next lower
  5405.         CP      H               ; have we reached ROM - $3F ?
  5406.         JR      NZ,L11DC        ; back to RAM-FILL if not.
  5407.  
  5408. ;; RAM-READ
  5409. L11E2:  AND     A               ; clear carry - prepare to subtract
  5410.         SBC     HL,DE           ; subtract and add back setting
  5411.         ADD     HL,DE           ; carry when back at start.
  5412.         INC     HL              ; and increment for next iteration.
  5413.         JR      NC,L11EF        ; forward to RAM-DONE if we've got back to
  5414.                                 ; starting point with no errors.
  5415.  
  5416.         DEC     (HL)            ; decrement to 1.
  5417.         JR      Z,L11EF         ; forward to RAM-DONE if faulty.
  5418.  
  5419.         DEC     (HL)            ; decrement to zero.
  5420.         JR      Z,L11E2         ; back to RAM-READ if zero flag was set.
  5421.  
  5422. ;; RAM-DONE
  5423. L11EF:  DEC     HL              ; step back to last valid location.
  5424.         EXX                     ; regardless of state, set up possibly
  5425.                                 ; stored system variables in case from NEW.
  5426.         LD      ($5CB4),BC      ; insert P-RAMT.
  5427.         LD      ($5C38),DE      ; insert RASP/PIP.
  5428.         LD      ($5C7B),HL      ; insert UDG.
  5429.         EXX                     ; switch in main set.
  5430.         INC     B               ; now test if we arrived here from NEW.
  5431.         JR      Z,L1219         ; forward to RAM-SET if we did.
  5432.  
  5433. ; this section applies to START only.
  5434.  
  5435.         LD      ($5CB4),HL      ; set P-RAMT to the highest working RAM
  5436.                                 ; address.
  5437.         LD      DE,$3EAF        ; address of last byte of 'U' bitmap in ROM.
  5438.         LD      BC,$00A8        ; there are 21 user defined graphics.
  5439.         EX      DE,HL           ; switch pointers and make the UDGs a
  5440.         LDDR                    ; copy of the standard characters A - U.
  5441.         EX      DE,HL           ; switch the pointer to HL.
  5442.         INC     HL              ; update to start of 'A' in RAM.
  5443.         LD      ($5C7B),HL      ; make UDG system variable address the first
  5444.                                 ; bitmap.
  5445.         DEC     HL              ; point at RAMTOP again.
  5446.  
  5447.         LD      BC,$0040        ; set the values of
  5448.         LD      ($5C38),BC      ; the PIP and RASP system variables.
  5449.  
  5450. ; the NEW command path rejoins here.
  5451.  
  5452. ;; RAM-SET
  5453. L1219:  LD      ($5CB2),HL      ; set system variable RAMTOP to HL.
  5454.  
  5455.                 LD HL,CHARS-0X100       ;$3C00        ; a strange place to set the pointer to the
  5456.         LD      ($5C36),HL      ; character set, CHARS - as no printing yet.
  5457.  
  5458.         LD      HL,($5CB2)      ; fetch RAMTOP to HL again as we've lost it.
  5459.  
  5460.         LD      (HL),$3E        ; top of user ram holds GOSUB end marker
  5461.                                 ; an impossible line number - see RETURN.
  5462.                                 ; no significance in the number $3E. It has
  5463.                                 ; been traditional since the ZX80.
  5464.  
  5465.         DEC     HL              ; followed by empty byte (not important).
  5466.         LD      SP,HL           ; set up the machine stack pointer.
  5467.         DEC     HL              ;
  5468.         DEC     HL              ;
  5469.         LD      ($5C3D),HL      ; ERR_SP is where the error pointer is
  5470.                                 ; at moment empty - will take address MAIN-4
  5471.                                 ; at the call preceding that address,
  5472.                                 ; although interrupts and calls will make use
  5473.                                 ; of this location in meantime.
  5474.  
  5475.         IM      1               ; select interrupt mode 1.
  5476.         LD      IY,$5C3A        ; set IY to ERR_NR. IY can reach all standard
  5477.                                 ; system variables but shadow ROM system
  5478.                                 ; variables will be mostly out of range.
  5479.  
  5480.         EI                      ; enable interrupts now that we have a stack.
  5481.  
  5482.         LD      HL,$5CB6        ; the address of the channels - initially
  5483.                                 ; following system variables.
  5484.         LD      ($5C4F),HL      ; set the CHANS system variable.
  5485.  
  5486.         LD      DE,L15AF        ; address: init-chan in ROM.
  5487.         LD      BC,$0015        ; there are 21 bytes of initial data in ROM.
  5488.         EX      DE,HL           ; swap the pointers.
  5489.         LDIR                    ; copy the bytes to RAM.
  5490.  
  5491.         EX      DE,HL           ; swap pointers. HL points to program area.
  5492.         DEC     HL              ; decrement address.
  5493.         LD      ($5C57),HL      ; set DATADD to location before program area.
  5494.         INC     HL              ; increment again.
  5495.  
  5496.         LD      ($5C53),HL      ; set PROG the location where BASIC starts.
  5497.         LD      ($5C4B),HL      ; set VARS to same location with a
  5498.         LD      (HL),$80        ; variables end-marker.
  5499.         INC     HL              ; advance address.
  5500.         LD      ($5C59),HL      ; set E_LINE, where the edit line
  5501.                                 ; will be created.
  5502.                                 ; Note. it is not strictly necessary to
  5503.                                 ; execute the next fifteen bytes of code
  5504.                                 ; as this will be done by the call to SET-MIN.
  5505.                                 ; --
  5506.         LD      (HL),$0D        ; initially just has a carriage return
  5507.         INC     HL              ; followed by
  5508.         LD      (HL),$80        ; an end-marker.
  5509.         INC     HL              ; address the next location.
  5510.         LD      ($5C61),HL      ; set WORKSP - empty workspace.
  5511.         LD      ($5C63),HL      ; set STKBOT - bottom of the empty stack.
  5512.         LD      ($5C65),HL      ; set STKEND to the end of the empty stack.
  5513.                                 ; --
  5514.         LD      A,$38           ; the colour system is set to white paper,
  5515.                                 ; black ink, no flash or bright.
  5516.         LD      ($5C8D),A       ; set ATTR_P permanent colour attributes.
  5517.         LD      ($5C8F),A       ; set ATTR_T temporary colour attributes.
  5518.         LD      ($5C48),A       ; set BORDCR the border colour/lower screen
  5519.                                 ; attributes.
  5520.  
  5521.         LD      HL,$0523        ; The keyboard repeat and delay values
  5522.         LD      ($5C09),HL      ; are loaded to REPDEL and REPPER.
  5523.  
  5524.         DEC     (IY-$3A)        ; set KSTATE-0 to $FF.
  5525.         DEC     (IY-$36)        ; set KSTATE-4 to $FF.
  5526.                                 ; thereby marking both available.
  5527.  
  5528.         LD      HL,L15C6        ; set source to ROM Address: init-strm
  5529.         LD      DE,$5C10        ; set destination to system variable STRMS-FD
  5530.         LD      BC,$000E        ; copy the 14 bytes of initial 7 streams data
  5531.         LDIR                    ; from ROM to RAM.
  5532.  
  5533.         SET     1,(IY+$01)      ; update FLAGS  - signal printer in use.
  5534.  
  5535. ;===============================
  5536.                 IF AY_PRINTER
  5537.                 CALL PRINTER_INITER
  5538.                 ELSE
  5539.                 CALL L0EDF                      ; call routine CLEAR-PRB to initialize system
  5540.                                                 ; variables associated with printer.
  5541.                 ENDIF
  5542. ;===============================
  5543.  
  5544.         LD      (IY+$31),$02    ; set DF_SZ the lower screen display size to
  5545.                                 ; two lines
  5546.         CALL    L0D6B           ; call routine CLS to set up system
  5547.                                 ; variables associated with screen and clear
  5548.                                 ; the screen and set attributes.
  5549.         XOR     A               ; clear accumulator so that we can address
  5550.         LD      DE,L1539 - 1    ; the message table directly.
  5551.         CALL    L0C0A           ; routine PO-MSG puts
  5552.                                 ; '(c) 1982 Sinclair Research Ltd'
  5553.                                 ; at bottom of display.
  5554.         SET     5,(IY+$02)      ; update TV_FLAG  - signal lower screen will
  5555.                                 ; require clearing.
  5556.  
  5557.         JR      L12A9           ; forward to MAIN-1
  5558.  
  5559. ; -------------------
  5560. ; Main execution loop
  5561. ; -------------------
  5562. ;
  5563. ;
  5564.  
  5565. ;; MAIN-EXEC
  5566. L12A2:  LD      (IY+$31),$02    ; set DF_SZ lower screen display file
  5567.                                 ; size to 2 lines.
  5568.         CALL    L1795           ; routine AUTO-LIST
  5569.  
  5570. ;; MAIN-1
  5571. L12A9:  CALL    L16B0           ; routine SET-MIN clears work areas.
  5572.  
  5573. ;; MAIN-2
  5574. L12AC:  LD      A,$00           ; select channel 'K' the keyboard
  5575.         CALL    L1601           ; routine CHAN-OPEN opens it
  5576.         CALL    L0F2C           ; routine EDITOR is called.
  5577.                                 ; Note the above routine is where the Spectrum
  5578.                                 ; waits for user-interaction. Perhaps the
  5579.                                 ; most common input at this stage
  5580.                                 ; is LOAD "".
  5581.         CALL    L1B17           ; routine LINE-SCAN scans the input.
  5582.         BIT     7,(IY+$00)      ; test ERR_NR - will be $FF if syntax
  5583.                                 ; is correct.
  5584.         JR      NZ,L12CF        ; forward, if correct, to MAIN-3.
  5585.  
  5586. ;
  5587.  
  5588.         BIT     4,(IY+$30)      ; test FLAGS2 - K channel in use ?
  5589.         JR      Z,L1303         ; forward to MAIN-4 if not.
  5590.  
  5591. ;
  5592.  
  5593.         LD      HL,($5C59)      ; an editing error so address E_LINE.
  5594.         CALL    L11A7           ; routine REMOVE-FP removes the hidden
  5595.                                 ; floating-point forms.
  5596.         LD      (IY+$00),$FF    ; system variable ERR_NR is reset to 'OK'.
  5597.         JR      L12AC           ; back to MAIN-2 to allow user to correct.
  5598.  
  5599. ; ---
  5600.  
  5601. ; the branch was here if syntax has passed test.
  5602.  
  5603. ;; MAIN-3
  5604. L12CF:  LD      HL,($5C59)      ; fetch the edit line address from E_LINE.
  5605.         LD      ($5C5D),HL      ; system variable CH_ADD is set to first
  5606.                                 ; character of edit line.
  5607.                                 ; Note. the above two instructions are a little
  5608.                                 ; inadequate.
  5609.                                 ; They are repeated with a subtle difference
  5610.                                 ; at the start of the next subroutine and are
  5611.                                 ; therefore not required above.
  5612.  
  5613.         CALL    L19FB           ; routine E-LINE-NO will fetch any line
  5614.                                 ; number to BC if this is a program line.
  5615.  
  5616.         LD      A,B             ; test if the number of
  5617.         OR      C               ; the line is non-zero.
  5618.         JP      NZ,L155D        ; jump forward to MAIN-ADD if so to add the
  5619.                                 ; line to the BASIC program.
  5620.  
  5621. ; Has the user just pressed the ENTER key ?
  5622.  
  5623.         RST     18H             ; GET-CHAR gets character addressed by CH_ADD.
  5624.         CP      $0D             ; is it a carriage return ?
  5625.         JR      Z,L12A2         ; back to MAIN-EXEC if so for an automatic
  5626.                                 ; listing.
  5627.  
  5628. ; this must be a direct command.
  5629.  
  5630.         BIT     0,(IY+$30)      ; test FLAGS2 - clear the main screen ?
  5631.         CALL    NZ,L0DAF        ; routine CL-ALL, if so, e.g. after listing.
  5632.         CALL    L0D6E           ; routine CLS-LOWER anyway.
  5633.         LD      A,$19           ; compute scroll count to 25 minus
  5634.         SUB     (IY+$4F)        ; value of S_POSN_hi.
  5635.         LD      ($5C8C),A       ; update SCR_CT system variable.
  5636.         SET     7,(IY+$01)      ; update FLAGS - signal running program.
  5637.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK'.
  5638.         LD      (IY+$0A),$01    ; set NSPPC to one for first statement.
  5639.         CALL    L1B8A           ; call routine LINE-RUN to run the line.
  5640.                                 ; sysvar ERR_SP therefore addresses MAIN-4
  5641.  
  5642. ; Examples of direct commands are RUN, CLS, LOAD "", PRINT USR 40000,
  5643. ; LPRINT "A"; etc..
  5644. ; If a user written machine-code program disables interrupts then it
  5645. ; must enable them to pass the next step. We also jumped to here if the
  5646. ; keyboard was not being used.
  5647.  
  5648. ;; MAIN-4
  5649. L1303:  HALT                    ; wait for interrupt.
  5650.  
  5651.         RES     5,(IY+$01)      ; update FLAGS - signal no new key.
  5652.         BIT     1,(IY+$30)      ; test FLAGS2 - is printer buffer clear ?
  5653.         CALL    NZ,L0ECD        ; call routine COPY-BUFF if not.
  5654.                                 ; Note. the programmer has neglected
  5655.                                 ; to set bit 1 of FLAGS first.
  5656.  
  5657.         LD      A,($5C3A)       ; fetch ERR_NR
  5658.         INC     A               ; increment to give true code.
  5659.  
  5660. ; Now deal with a runtime error as opposed to an editing error.
  5661. ; However if the error code is now zero then the OK message will be printed.
  5662.  
  5663. ;; MAIN-G
  5664. L1313:  PUSH    AF              ; save the error number.
  5665.  
  5666.         LD      HL,$0000        ; prepare to clear some system variables.
  5667.         LD      (IY+$37),H      ; clear all the bits of FLAGX.
  5668.         LD      (IY+$26),H      ; blank X_PTR_hi to suppress error marker.
  5669.         LD      ($5C0B),HL      ; blank DEFADD to signal that no defined
  5670.                                 ; function is currently being evaluated.
  5671.  
  5672.         LD      HL,$0001        ; explicit - inc hl would do.
  5673.         LD      ($5C16),HL      ; ensure STRMS-00 is keyboard.
  5674.  
  5675.         CALL    L16B0           ; routine SET-MIN clears workspace etc.
  5676.         RES     5,(IY+$37)      ; update FLAGX - signal in EDIT not INPUT mode.
  5677.                                 ; Note. all the bits were reset earlier.
  5678.  
  5679.         CALL    L0D6E           ; call routine CLS-LOWER.
  5680.         SET     5,(IY+$02)      ; update TV_FLAG - signal lower screen
  5681.                                 ; requires clearing.
  5682.  
  5683.         POP     AF              ; bring back the error number
  5684.         LD      B,A             ; and make a copy in B.
  5685.         CP      $0A             ; is it a print-ready digit ?
  5686.         JR      C,L133C         ; forward to MAIN-5 if so.
  5687.  
  5688.         ADD     A,$07           ; add ASCII offset to letters.
  5689.  
  5690. ;; MAIN-5
  5691. L133C:  CALL    L15EF           ; call routine OUT-CODE to print the code.
  5692.  
  5693.         LD      A,$20           ; followed by a space.
  5694.         RST     10H             ; PRINT-A
  5695.  
  5696.         LD      A,B             ; fetch stored report code.
  5697.         LD      DE,L1391        ; address: rpt-mesgs.
  5698.         CALL    L0C0A           ; call routine PO-MSG to print.
  5699.  
  5700. X1349
  5701.                 IF BAS48_ONLY
  5702.                 XOR A
  5703.                 LD DE,L1536
  5704.                 ELSE
  5705.                 CALL L3B3B              ; Spectrum 128 patch
  5706.                 NOP
  5707.                 ENDIF
  5708.  
  5709. L134D:  CALL    L0C0A           ; routine PO-MSG prints them although it would
  5710.                                 ; be more succinct to use RST $10.
  5711.  
  5712.         LD      BC,($5C45)      ; fetch PPC the current line number.
  5713.         CALL    L1A1B           ; routine OUT-NUM-1 will print that
  5714.         LD      A,$3A           ; then a ':'.
  5715.         RST     10H             ; PRINT-A
  5716.  
  5717.         LD      C,(IY+$0D)      ; then SUBPPC for statement
  5718.         LD      B,$00           ; limited to 127
  5719.         CALL    L1A1B           ; routine OUT-NUM-1
  5720.  
  5721.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  5722.                                 ; which probably contained 'RUN'.
  5723.         LD      A,($5C3A)       ; fetch ERR_NR again
  5724.         INC     A               ; test for no error originally $FF.
  5725.         JR      Z,L1386         ; forward to MAIN-9 if no error.
  5726.  
  5727.         CP      $09             ; is code Report 9 STOP ?
  5728.         JR      Z,L1373         ; forward to MAIN-6 if so
  5729.  
  5730.         CP      $15             ; is code Report L Break ?
  5731.         JR      NZ,L1376        ; forward to MAIN-7 if not
  5732.  
  5733. ; Stop or Break was encountered so consider CONTINUE.
  5734.  
  5735. ;; MAIN-6
  5736. L1373:  INC     (IY+$0D)        ; increment SUBPPC to next statement.
  5737.  
  5738. ;; MAIN-7
  5739. L1376:  LD      BC,$0003        ; prepare to copy 3 system variables to
  5740.         LD      DE,$5C70        ; address OSPPC - statement for CONTINUE.
  5741.                                 ; also updating OLDPPC line number below.
  5742.  
  5743.         LD      HL,$5C44        ; set source top to NSPPC next statement.
  5744.         BIT     7,(HL)          ; did BREAK occur before the jump ?
  5745.                                 ; e.g. between GO TO and next statement.
  5746.         JR      Z,L1384         ; skip forward to MAIN-8, if not, as setup
  5747.                                 ; is correct.
  5748.  
  5749.         ADD     HL,BC           ; set source to SUBPPC number of current
  5750.                                 ; statement/line which will be repeated.
  5751.  
  5752. ;; MAIN-8
  5753. L1384:  LDDR                    ; copy PPC to OLDPPC and SUBPPC to OSPCC
  5754.                                 ; or NSPPC to OLDPPC and NEWPPC to OSPCC
  5755.  
  5756. ;; MAIN-9
  5757. L1386:  LD      (IY+$0A),$FF    ; update NSPPC - signal 'no jump'.
  5758.         RES     3,(IY+$01)      ; update FLAGS  - signal use 'K' mode for
  5759.                                 ; the first character in the editor and
  5760.         JP      L12AC           ; jump back to MAIN-2.
  5761.  
  5762.  
  5763. ; ----------------------
  5764. ; Canned report messages
  5765. ; ----------------------
  5766. ; The Error reports with the last byte inverted. The first entry
  5767. ; is a dummy entry. The last, which begins with $7F, the Spectrum
  5768. ; character for copyright symbol, is placed here for convenience
  5769. ; as is the preceding comma and space.
  5770. ; The report line must accommodate a 4-digit line number and a 3-digit
  5771. ; statement number which limits the length of the message text to twenty
  5772. ; characters.
  5773. ; e.g.  "B Integer out of range, 1000:127"
  5774.  
  5775. ;; rpt-mesgs
  5776. L1391           DB $80
  5777.                 DC "OK"                         ;DB    'O','K'+$80              ; 0
  5778.                 DC "NEXT without FOR"           ;DEFM    "NEXT without FO"
  5779.                                                 ;DB    'R'+$80          ; 1
  5780.                 DC "Variable not found"         ;DEFM    "Variable not foun"
  5781.                                                 ;DB    'd'+$80          ; 2
  5782.                 DC "Subscript wrong"            ;DEFM    "Subscript wron"
  5783.                                                 ;DB    'g'+$80          ; 3
  5784.                 DC "Out of memory"              ;DEFM    "Out of memor"
  5785.                                                 ;DB    'y'+$80          ; 4
  5786.                 DC "Out of screen"              ;DEFM    "Out of scree"
  5787.                                                 ;DB    'n'+$80          ; 5
  5788.                 DC "Number too big"             ;DEFM    "Number too bi"
  5789.                                                 ;DB    'g'+$80          ; 6
  5790.                 DC "RETURN without GOSUB"       ;DEFM    "RETURN without GOSU"
  5791.                                                 ;DB    'B'+$80          ; 7
  5792.                 DC "End of file"                ;DEFM    "End of fil"
  5793.                                                 ;DB    'e'+$80          ; 8
  5794.                 DC "STOP statement"             ;DEFM    "STOP statemen"
  5795.                                                 ;DB    't'+$80          ; 9
  5796.                 DC "Invalid argument"           ;DEFM    "Invalid argumen"
  5797.                                                 ;DB    't'+$80          ; A
  5798.                 DC "Integer out of range"       ;DEFM    "Integer out of rang"
  5799.                                                 ;DB    'e'+$80          ; B
  5800.                 DC "Nonsense in BASIC"          ;DEFM    "Nonsense in BASI"
  5801.                                                 ;DB    'C'+$80          ; C
  5802.                 DC "BREAK - CONT repeats"       ;DEFM    "BREAK - CONT repeat"
  5803.                                                 ;DB    's'+$80          ; D
  5804.                 DC "Out of DATA"                ;DEFM    "Out of DAT"
  5805.                                                 ;DB    'A'+$80          ; E
  5806.                 DC "Invalid file name"          ;DEFM    "Invalid file nam"
  5807.                                                 ;DB    'e'+$80          ; F
  5808.                 DC "No room for line"           ;DEFM    "No room for lin"
  5809.                                                 ;DB    'e'+$80          ; G
  5810.                 DC "STOP in INPUT"              ;DEFM    "STOP in INPU"
  5811.                                                 ;DB    'T'+$80          ; H
  5812.                 DC "FOR without NEXT"           ;DEFM    "FOR without NEX"
  5813.                                                 ;DB    'T'+$80          ; I
  5814.                 DC "Invalid I/O device"         ;DEFM    "Invalid I/O devic"
  5815.                                                 ;DB    'e'+$80          ; J
  5816.                 DC "Invalid colour"             ;DEFM    "Invalid colou"
  5817.                                                 ;DB    'r'+$80          ; K
  5818.                 DC "BREAK into program"         ;DEFM    "BREAK into progra"
  5819.                                                 ;DB    'm'+$80          ; L
  5820.                 DC "RAMTOP no good"             ;DEFM    "RAMTOP no goo"
  5821.                                                 ;DB    'd'+$80          ; M
  5822.                 DC "Statement lost"             ;DEFM    "Statement los"
  5823.                                                 ;DB    't'+$80          ; N
  5824.                 DC "Invalid stream"             ;DEFM    "Invalid strea"
  5825.                                                 ;DB    'm'+$80          ; O
  5826.                 DC "FN without DEF"             ;DEFM    "FN without DE"
  5827.                                                 ;DB    'F'+$80          ; P
  5828.                 DC "Parameter error"            ;DEFM    "Parameter erro"
  5829.                                                 ;DB    'r'+$80          ; Q
  5830.                 DC "Tape loading error"         ;DEFM    "Tape loading erro"
  5831.                                                 ;DB    'r'+$80          ; R
  5832. L1536           EQU $-1
  5833. ;; comma-sp  
  5834. L1537           DC ", "                         ;DB    ',',' '+$80              ; used in report line.
  5835. ;; copyright
  5836. L1539           DB $7F                          ; copyright
  5837.                 DC " 1982 Sinclair Research Ltd"        ;DEFM    " 1982 Sinclair Research Lt"
  5838.                                                         ;DB    'd'+$80
  5839.  
  5840.  
  5841. ; -------------
  5842. ; REPORT-G
  5843. ; -------------
  5844. ; Note ERR_SP points here during line entry which allows the
  5845. ; normal 'Out of Memory' report to be augmented to the more
  5846. ; precise 'No Room for line' report.
  5847.  
  5848. ;; REPORT-G
  5849. ; No Room for line
  5850. L1555:  LD      A,$10           ; i.e. 'G' -$30 -$07
  5851.         LD      BC,$0000        ; this seems unnecessary.
  5852.         JP      L1313           ; jump back to MAIN-G
  5853.  
  5854. ; -----------------------------
  5855. ; Handle addition of BASIC line
  5856. ; -----------------------------
  5857. ; Note this is not a subroutine but a branch of the main execution loop.
  5858. ; System variable ERR_SP still points to editing error handler.
  5859. ; A new line is added to the BASIC program at the appropriate place.
  5860. ; An existing line with same number is deleted first.
  5861. ; Entering an existing line number deletes that line.
  5862. ; Entering a non-existent line allows the subsequent line to be edited next.
  5863.  
  5864. ;; MAIN-ADD
  5865. L155D:  LD      ($5C49),BC      ; set E_PPC to extracted line number.
  5866.         LD      HL,($5C5D)      ; fetch CH_ADD - points to location after the
  5867.                                 ; initial digits (set in E_LINE_NO).
  5868.         EX      DE,HL           ; save start of BASIC in DE.
  5869.  
  5870.         LD      HL,L1555        ; Address: REPORT-G
  5871.         PUSH    HL              ; is pushed on stack and addressed by ERR_SP.
  5872.                                 ; the only error that can occur is
  5873.                                 ; 'Out of memory'.
  5874.  
  5875.         LD      HL,($5C61)      ; fetch WORKSP - end of line.
  5876.         SCF                     ; prepare for true subtraction.
  5877.         SBC     HL,DE           ; find length of BASIC and
  5878.         PUSH    HL              ; save it on stack.
  5879.         LD      H,B             ; transfer line number
  5880.         LD      L,C             ; to HL register.
  5881.         CALL    L196E           ; routine LINE-ADDR will see if
  5882.                                 ; a line with the same number exists.
  5883.         JR      NZ,L157D        ; forward if no existing line to MAIN-ADD1.
  5884.  
  5885.         CALL    L19B8           ; routine NEXT-ONE finds the existing line.
  5886.         CALL    L19E8           ; routine RECLAIM-2 reclaims it.
  5887.  
  5888. ;; MAIN-ADD1
  5889. L157D:  POP     BC              ; retrieve the length of the new line.
  5890.         LD      A,C             ; and test if carriage return only
  5891.         DEC     A               ; i.e. one byte long.
  5892.         OR      B               ; result would be zero.
  5893.         JR      Z,L15AB         ; forward to MAIN-ADD2 is so.
  5894.  
  5895.         PUSH    BC              ; save the length again.
  5896.         INC     BC              ; adjust for inclusion
  5897.         INC     BC              ; of line number (two bytes)
  5898.         INC     BC              ; and line length
  5899.         INC     BC              ; (two bytes).
  5900.         DEC     HL              ; HL points to location before the destination
  5901.  
  5902.         LD      DE,($5C53)      ; fetch the address of PROG
  5903.         PUSH    DE              ; and save it on the stack
  5904.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces in
  5905.                                 ; program area and updates pointers.
  5906.         POP     HL              ; restore old program pointer.
  5907.         LD      ($5C53),HL      ; and put back in PROG as it may have been
  5908.                                 ; altered by the POINTERS routine.
  5909.  
  5910.         POP     BC              ; retrieve BASIC length
  5911.         PUSH    BC              ; and save again.
  5912.  
  5913.         INC     DE              ; points to end of new area.
  5914.         LD      HL,($5C61)      ; set HL to WORKSP - location after edit line.
  5915.         DEC     HL              ; decrement to address end marker.
  5916.         DEC     HL              ; decrement to address carriage return.
  5917.         LDDR                    ; copy the BASIC line back to initial command.
  5918.  
  5919.         LD      HL,($5C49)      ; fetch E_PPC - line number.
  5920.         EX      DE,HL           ; swap it to DE, HL points to last of
  5921.                                 ; four locations.
  5922.         POP     BC              ; retrieve length of line.
  5923.         LD      (HL),B          ; high byte last.
  5924.         DEC     HL              ;
  5925.         LD      (HL),C          ; then low byte of length.
  5926.         DEC     HL              ;
  5927.         LD      (HL),E          ; then low byte of line number.
  5928.         DEC     HL              ;
  5929.         LD      (HL),D          ; then high byte range $0 - $27 (1-9999).
  5930.  
  5931. ;; MAIN-ADD2
  5932. L15AB:  POP     AF              ; drop the address of Report G
  5933.         JP      L12A2           ; and back to MAIN-EXEC producing a listing
  5934.                                 ; and to reset ERR_SP in EDITOR.
  5935.  
  5936.  
  5937. ; ---------------------------
  5938. ; Initial channel information
  5939. ; ---------------------------
  5940. ; This initial channel information is copied from ROM to RAM,
  5941. ; during initialization. It's new location is after the system
  5942. ; variables and is addressed by the system variable CHANS
  5943. ; which means that it can slide up and down in memory.
  5944. ; The table is never searched and the last character which could be anything
  5945. ; other than a comma provides a convenient resting place for DATADD.
  5946.  
  5947. ;; init-chan
  5948. L15AF           DW L09F4                ; PRINT-OUT
  5949.                 DW L10A8                ; KEY-INPUT
  5950.                 DB "K"
  5951.                 DW L09F4                ; PRINT-OUT
  5952.                 DW L15C4                ; REPORT-J
  5953.                 DB "S"
  5954.                 DW L0F81                ; ADD-CHAR
  5955.                 DW L15C4                ; REPORT-J
  5956.                 DB "R"
  5957. ;=======================================
  5958.                 IF AY_PRINTER
  5959.                 DW PRN_TOKEN
  5960.                 ELSE
  5961.                 DW L09F4                ; PRINT-OUT
  5962.                 ENDIF
  5963. ;=======================================
  5964.                 DW L15C4                ; REPORT-J
  5965.                 DB "P"
  5966.  
  5967.         DB    $80             ; End Marker
  5968.  
  5969. ;; REPORT-J
  5970. L15C4:  RST     08H             ; ERROR-1
  5971.         DB    $12             ; Error Report: Invalid I/O device
  5972.  
  5973.  
  5974. ; -------------------
  5975. ; Initial stream data
  5976. ; -------------------
  5977. ; This is the initial stream data for the seven streams $FD - $03 that is
  5978. ; copied from ROM to the STRMS system variables area during initialization.
  5979. ; There are reserved locations there for another 12 streams.
  5980. ; Each location contains an offset to the second byte of a channel.
  5981. ; The first byte of a channel can't be used as that would result in an
  5982. ; offset of zero for some and zero is used to denote that a stream is closed.
  5983.  
  5984. ;; init-strm
  5985. L15C6:  DB    $01, $00        ; stream $FD offset to channel 'K'
  5986.         DB    $06, $00        ; stream $FE offset to channel 'S'
  5987.         DB    $0B, $00        ; stream $FF offset to channel 'R'
  5988.  
  5989.         DB    $01, $00        ; stream $00 offset to channel 'K'
  5990.         DB    $01, $00        ; stream $01 offset to channel 'K'
  5991.         DB    $06, $00        ; stream $02 offset to channel 'S'
  5992.         DB    $10, $00        ; stream $03 offset to channel 'P'
  5993.  
  5994. ; ----------------------------
  5995. ; Control for input subroutine
  5996. ; ----------------------------
  5997. ;
  5998.  
  5999. ;; WAIT-KEY
  6000. L15D4:  BIT     5,(IY+$02)      ; test TV_FLAG - clear lower screen ?
  6001.         JR      NZ,L15DE        ; forward to WAIT-KEY1 if so.
  6002.  
  6003.         SET     3,(IY+$02)      ; update TV_FLAG - signal reprint the edit
  6004.                                 ; line to the lower screen.
  6005.  
  6006. ;; WAIT-KEY1
  6007. L15DE:  CALL    L15E6           ; routine INPUT-AD is called.
  6008.         RET     C               ; return with acceptable keys.
  6009.  
  6010.         JR      Z,L15DE         ; back to WAIT-KEY1 if no key is pressed
  6011.                                 ; or it has been handled within INPUT-AD.
  6012.  
  6013. ; Note. When inputting from the keyboard all characters are returned with
  6014. ; above conditions so this path is never taken.
  6015.  
  6016. ;; REPORT-8
  6017. L15E4:  RST     08H             ; ERROR-1
  6018.         DB    $07             ; Error Report: End of file
  6019.  
  6020. ; ------------------------------
  6021. ; Make HL point to input address
  6022. ; ------------------------------
  6023. ; This routine fetches the address of the input stream from the current
  6024. ; channel area using system variable CURCHL.
  6025.  
  6026. ;; INPUT-AD
  6027. L15E6:  EXX                     ; switch in alternate set.
  6028.         PUSH    HL              ; save HL register
  6029.         LD      HL,($5C51)      ; fetch address of CURCHL - current channel.
  6030.         INC     HL              ; step over output routine
  6031.         INC     HL              ; to point to low byte of input routine.
  6032.         JR      L15F7           ; forward to CALL-SUB.
  6033.  
  6034. ; -------------------
  6035. ; Main Output Routine
  6036. ; -------------------
  6037. ; The entry point OUT-CODE is called on five occasions to print
  6038. ; the ASCII equivalent of a value 0-9.
  6039. ;
  6040. ; PRINT-A-2 is a continuation of the RST 10 to print any character.
  6041. ; Both print to the current channel and the printing of control codes
  6042. ; may alter that channel to divert subsequent RST 10 instructions
  6043. ; to temporary routines. The normal channel is $09F4.
  6044.  
  6045. ;; OUT-CODE
  6046. L15EF:  LD      E,$30           ; add 48 decimal to give ASCII
  6047.         ADD     A,E             ; character '0' to '9'.
  6048.  
  6049. ;; PRINT-A-2
  6050. L15F2:  EXX                     ; switch in alternate set
  6051.         PUSH    HL              ; save HL register
  6052.         LD      HL,($5C51)      ; fetch CURCHL the current channel.
  6053.  
  6054. ; input-ad rejoins here also.
  6055.  
  6056. ;; CALL-SUB
  6057. L15F7:  LD      E,(HL)          ; put the low byte in E.
  6058.         INC     HL              ; advance address.
  6059.         LD      D,(HL)          ; put the high byte to D.
  6060.         EX      DE,HL           ; transfer the stream to HL.
  6061.         CALL    L162C           ; use routine CALL-JUMP.
  6062.                                 ; in effect CALL (HL).
  6063.  
  6064.         POP     HL              ; restore saved HL register.
  6065.         EXX                     ; switch back to the main set and
  6066.         RET                     ; return.
  6067.  
  6068. ; ------------
  6069. ; Open channel
  6070. ; ------------
  6071. ; This subroutine is used by the ROM to open a channel 'K', 'S', 'R' or 'P'.
  6072. ; This is either for its own use or in response to a user's request, for
  6073. ; example, when '#' is encountered with output - PRINT, LIST etc.
  6074. ; or with input - INPUT, INKEY$ etc.
  6075. ; it is entered with a system stream $FD - $FF, or a user stream $00 - $0F
  6076. ; in the accumulator.
  6077.  
  6078. ;; CHAN-OPEN
  6079. L1601:  ADD     A,A             ; double the stream ($FF will become $FE etc.)
  6080.         ADD     A,$16           ; add the offset to stream 0 from $5C00
  6081.         LD      L,A             ; result to L
  6082.         LD      H,$5C           ; now form the address in STRMS area.
  6083.         LD      E,(HL)          ; fetch low byte of CHANS offset
  6084.         INC     HL              ; address next
  6085.         LD      D,(HL)          ; fetch high byte of offset
  6086.         LD      A,D             ; test that the stream is open.
  6087.         OR      E               ; zero if closed.
  6088.         JR      NZ,L1610        ; forward to CHAN-OP-1 if open.
  6089.  
  6090. ;; REPORT-Oa
  6091. L160E:  RST     08H             ; ERROR-1
  6092.         DB    $17             ; Error Report: Invalid stream
  6093.  
  6094. ; continue here if stream was open. Note that the offset is from CHANS
  6095. ; to the second byte of the channel.
  6096.  
  6097. ;; CHAN-OP-1
  6098. L1610:  DEC     DE              ; reduce offset so it points to the channel.
  6099.         LD      HL,($5C4F)      ; fetch CHANS the location of the base of
  6100.                                 ; the channel information area
  6101.         ADD     HL,DE           ; and add the offset to address the channel.
  6102.                                 ; and continue to set flags.
  6103.  
  6104. ; -----------------
  6105. ; Set channel flags
  6106. ; -----------------
  6107. ; This subroutine is used from ED-EDIT, str$ and read-in to reset the
  6108. ; current channel when it has been temporarily altered.
  6109.  
  6110. ;; CHAN-FLAG
  6111. L1615:  LD      ($5C51),HL      ; set CURCHL system variable to the
  6112.                                 ; address in HL
  6113.         RES     4,(IY+$30)      ; update FLAGS2  - signal K channel not in use.
  6114.                                 ; Note. provide a default for channel 'R'.
  6115.         INC     HL              ; advance past
  6116.         INC     HL              ; output routine.
  6117.         INC     HL              ; advance past
  6118.         INC     HL              ; input routine.
  6119.         LD      C,(HL)          ; pick up the letter.
  6120.         LD      HL,L162D        ; address: chn-cd-lu
  6121.         CALL    L16DC           ; routine INDEXER finds offset to a
  6122.                                 ; flag-setting routine.
  6123.  
  6124.         RET     NC              ; but if the letter wasn't found in the
  6125.                                 ; table just return now. - channel 'R'.
  6126.  
  6127.         LD      D,$00           ; prepare to add
  6128.         LD      E,(HL)          ; offset to E
  6129.         ADD     HL,DE           ; add offset to location of offset to form
  6130.                                 ; address of routine
  6131.  
  6132. ;; CALL-JUMP
  6133. L162C:  JP      (HL)            ; jump to the routine
  6134.  
  6135. ; Footnote. calling any location that holds JP (HL) is the equivalent to
  6136. ; a pseudo Z80 instruction CALL (HL). The ROM uses the instruction above.
  6137.  
  6138. ; --------------------------
  6139. ; Channel code look-up table
  6140. ; --------------------------
  6141. ; This table is used by the routine above to find one of the three
  6142. ; flag setting routines below it.
  6143. ; A zero end-marker is required as channel 'R' is not present.
  6144.  
  6145. ;; chn-cd-lu
  6146. L162D:  DB    'K', L1634-$-1  ; offset $06 to CHAN-K
  6147.         DB    'S', L1642-$-1  ; offset $12 to CHAN-S
  6148.         DB    'P', L164D-$-1  ; offset $1B to CHAN-P
  6149.  
  6150.         DB    $00             ; end marker.
  6151.  
  6152. ; --------------
  6153. ; Channel K flag
  6154. ; --------------
  6155. ; routine to set flags for lower screen/keyboard channel.
  6156.  
  6157. ;; CHAN-K
  6158. L1634:  SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6159.         RES     5,(IY+$01)      ; update FLAGS    - signal no new key
  6160.         SET     4,(IY+$30)      ; update FLAGS2   - signal K channel in use
  6161.         JR      L1646           ; forward to CHAN-S-1 for indirect exit
  6162.  
  6163. ; --------------
  6164. ; Channel S flag
  6165. ; --------------
  6166. ; routine to set flags for upper screen channel.
  6167.  
  6168. ;; CHAN-S
  6169. L1642:  RES     0,(IY+$02)      ; TV_FLAG  - signal main screen in use
  6170.  
  6171. ;; CHAN-S-1
  6172. L1646:  RES     1,(IY+$01)      ; update FLAGS  - signal printer not in use
  6173.         JP      L0D4D           ; jump back to TEMPS and exit via that
  6174.                                 ; routine after setting temporary attributes.
  6175. ; --------------
  6176. ; Channel P flag
  6177. ; --------------
  6178. ; This routine sets a flag so that subsequent print related commands
  6179. ; print to printer or update the relevant system variables.
  6180. ; This status remains in force until reset by the routine above.
  6181.  
  6182. ;; CHAN-P
  6183. L164D:  SET     1,(IY+$01)      ; update FLAGS  - signal printer in use
  6184.         RET                     ; return
  6185.  
  6186. ; -----------------------
  6187. ; Just one space required
  6188. ; -----------------------
  6189. ; This routine is called once only to create a single space
  6190. ; in workspace by ADD-CHAR. It is slightly quicker than using a RST $30.
  6191. ; There are several instances in the calculator where the sequence
  6192. ; ld bc, 1; rst $30 could be replaced by a call to this routine but it
  6193. ; only gives a saving of one byte each time.
  6194.  
  6195. ;; ONE-SPACE
  6196. L1652:  LD      BC,$0001        ; create space for a single character.
  6197.  
  6198. ; ---------
  6199. ; Make Room
  6200. ; ---------
  6201. ; This entry point is used to create BC spaces in various areas such as
  6202. ; program area, variables area, workspace etc..
  6203. ; The entire free RAM is available to each BASIC statement.
  6204. ; On entry, HL addresses where the first location is to be created.
  6205. ; Afterwards, HL will point to the location before this.
  6206.  
  6207. ;; MAKE-ROOM
  6208. L1655:  PUSH    HL              ; save the address pointer.
  6209.         CALL    L1F05           ; routine TEST-ROOM checks if room
  6210.                                 ; exists and generates an error if not.
  6211.         POP     HL              ; restore the address pointer.
  6212.         CALL    L1664           ; routine POINTERS updates the
  6213.                                 ; dynamic memory location pointers.
  6214.                                 ; DE now holds the old value of STKEND.
  6215.         LD      HL,($5C65)      ; fetch new STKEND the top destination.
  6216.  
  6217.         EX      DE,HL           ; HL now addresses the top of the area to
  6218.                                 ; be moved up - old STKEND.
  6219.         LDDR                    ; the program, variables, etc are moved up.
  6220.         RET                     ; return with new area ready to be populated.
  6221.                                 ; HL points to location before new area,
  6222.                                 ; and DE to last of new locations.
  6223.  
  6224. ; -----------------------------------------------
  6225. ; Adjust pointers before making or reclaiming room
  6226. ; -----------------------------------------------
  6227. ; This routine is called by MAKE-ROOM to adjust upwards and by RECLAIM to
  6228. ; adjust downwards the pointers within dynamic memory.
  6229. ; The fourteen pointers to dynamic memory, starting with VARS and ending
  6230. ; with STKEND, are updated adding BC if they are higher than the position
  6231. ; in HL.  
  6232. ; The system variables are in no particular order except that STKEND, the first
  6233. ; free location after dynamic memory must be the last encountered.
  6234.  
  6235. ;; POINTERS
  6236. L1664:  PUSH    AF              ; preserve accumulator.
  6237.         PUSH    HL              ; put pos pointer on stack.
  6238.         LD      HL,$5C4B        ; address VARS the first of the
  6239.         LD      A,$0E           ; fourteen variables to consider.
  6240.  
  6241. ;; PTR-NEXT
  6242. L166B:  LD      E,(HL)          ; fetch the low byte of the system variable.
  6243.         INC     HL              ; advance address.
  6244.         LD      D,(HL)          ; fetch high byte of the system variable.
  6245.         EX      (SP),HL         ; swap pointer on stack with the variable
  6246.                                 ; pointer.
  6247.         AND     A               ; prepare to subtract.
  6248.         SBC     HL,DE           ; subtract variable address
  6249.         ADD     HL,DE           ; and add back
  6250.         EX      (SP),HL         ; swap pos with system variable pointer
  6251.         JR      NC,L167F        ; forward to PTR-DONE if var before pos
  6252.  
  6253.         PUSH    DE              ; save system variable address.
  6254.         EX      DE,HL           ; transfer to HL
  6255.         ADD     HL,BC           ; add the offset
  6256.         EX      DE,HL           ; back to DE
  6257.         LD      (HL),D          ; load high byte
  6258.         DEC     HL              ; move back
  6259.         LD      (HL),E          ; load low byte
  6260.         INC     HL              ; advance to high byte
  6261.         POP     DE              ; restore old system variable address.
  6262.  
  6263. ;; PTR-DONE
  6264. L167F:  INC     HL              ; address next system variable.
  6265.         DEC     A               ; decrease counter.
  6266.         JR      NZ,L166B        ; back to PTR-NEXT if more.
  6267.         EX      DE,HL           ; transfer old value of STKEND to HL.
  6268.                                 ; Note. this has always been updated.
  6269.         POP     DE              ; pop the address of the position.
  6270.  
  6271.         POP     AF              ; pop preserved accumulator.
  6272.         AND     A               ; clear carry flag preparing to subtract.
  6273.  
  6274.         SBC     HL,DE           ; subtract position from old stkend
  6275.         LD      B,H             ; to give number of data bytes
  6276.         LD      C,L             ; to be moved.
  6277.         INC     BC              ; increment as we also copy byte at old STKEND.
  6278.         ADD     HL,DE           ; recompute old stkend.
  6279.         EX      DE,HL           ; transfer to DE.
  6280.         RET                     ; return.
  6281.  
  6282.  
  6283.  
  6284. ; -------------------
  6285. ; Collect line number
  6286. ; -------------------
  6287. ; This routine extracts a line number, at an address that has previously
  6288. ; been found using LINE-ADDR, and it is entered at LINE-NO. If it encounters
  6289. ; the program 'end-marker' then the previous line is used and if that
  6290. ; should also be unacceptable then zero is used as it must be a direct
  6291. ; command. The program end-marker is the variables end-marker $80, or
  6292. ; if variables exist, then the first character of any variable name.
  6293.  
  6294. ;; LINE-ZERO
  6295. L168F:  DB    $00, $00        ; dummy line number used for direct commands
  6296.  
  6297.  
  6298. ;; LINE-NO-A
  6299. L1691:  EX      DE,HL           ; fetch the previous line to HL and set
  6300.         LD      DE,$168F        ; DE to LINE-ZERO should HL also fail.
  6301.  
  6302. ; -> The Entry Point.
  6303.  
  6304. ;; LINE-NO
  6305. L1695:  LD      A,(HL)          ; fetch the high byte - max $2F
  6306.         AND     $C0             ; mask off the invalid bits.
  6307.         JR      NZ,L1691        ; to LINE-NO-A if an end-marker.
  6308.  
  6309.         LD      D,(HL)          ; reload the high byte.
  6310.         INC     HL              ; advance address.
  6311.         LD      E,(HL)          ; pick up the low byte.
  6312.         RET                     ; return from here.
  6313.  
  6314. ; -------------------
  6315. ; Handle reserve room
  6316. ; -------------------
  6317. ; This is a continuation of the restart BC-SPACES
  6318.  
  6319. ;; RESERVE
  6320. L169E:  LD      HL,($5C63)      ; STKBOT first location of calculator stack
  6321.         DEC     HL              ; make one less than new location
  6322.         CALL    L1655           ; routine MAKE-ROOM creates the room.
  6323.         INC     HL              ; address the first new location
  6324.         INC     HL              ; advance to second
  6325.         POP     BC              ; restore old WORKSP
  6326.         LD      ($5C61),BC      ; system variable WORKSP was perhaps
  6327.                                 ; changed by POINTERS routine.
  6328.         POP     BC              ; restore count for return value.
  6329.         EX      DE,HL           ; switch. DE = location after first new space
  6330.         INC     HL              ; HL now location after new space
  6331.         RET                     ; return.
  6332.  
  6333. ; ---------------------------
  6334. ; Clear various editing areas
  6335. ; ---------------------------
  6336. ; This routine sets the editing area, workspace and calculator stack
  6337. ; to their minimum configurations as at initialization and indeed this
  6338. ; routine could have been relied on to perform that task.
  6339. ; This routine uses HL only and returns with that register holding
  6340. ; WORKSP/STKBOT/STKEND though no use is made of this. The routines also
  6341. ; reset MEM to its usual place in the systems variable area should it
  6342. ; have been relocated to a FOR-NEXT variable. The main entry point
  6343. ; SET-MIN is called at the start of the MAIN-EXEC loop and prior to
  6344. ; displaying an error.
  6345.  
  6346. ;; SET-MIN
  6347. L16B0:  LD      HL,($5C59)      ; fetch E_LINE
  6348.         LD      (HL),$0D        ; insert carriage return
  6349.         LD      ($5C5B),HL      ; make K_CUR keyboard cursor point there.
  6350.         INC     HL              ; next location
  6351.         LD      (HL),$80        ; holds end-marker $80
  6352.         INC     HL              ; next location becomes
  6353.         LD      ($5C61),HL      ; start of WORKSP
  6354.  
  6355. ; This entry point is used prior to input and prior to the execution,
  6356. ; or parsing, of each statement.
  6357.  
  6358. ;; SET-WORK
  6359. L16BF:  LD      HL,($5C61)      ; fetch WORKSP value
  6360.         LD      ($5C63),HL      ; and place in STKBOT
  6361.  
  6362. ; This entry point is used to move the stack back to its normal place
  6363. ; after temporary relocation during line entry and also from ERROR-3
  6364.  
  6365. ;; SET-STK
  6366. L16C5:  LD      HL,($5C63)      ; fetch STKBOT value
  6367.         LD      ($5C65),HL      ; and place in STKEND.
  6368.  
  6369.         PUSH    HL              ; perhaps an obsolete entry point.
  6370.         LD      HL,$5C92        ; normal location of MEM-0
  6371.         LD      ($5C68),HL      ; is restored to system variable MEM.
  6372.         POP     HL              ; saved value not required.
  6373.         RET                     ; return.
  6374.  
  6375. ; ------------------
  6376. ; Reclaim edit-line?
  6377. ; ------------------
  6378. ; This seems to be legacy code from the ZX80/ZX81 as it is
  6379. ; not used in this ROM.
  6380. ; That task, in fact, is performed here by the dual-area routine CLEAR-SP.
  6381. ; This routine is designed to deal with something that is known to be in the
  6382. ; edit buffer and not workspace.
  6383. ; On entry, HL must point to the end of the something to be deleted.
  6384.  
  6385. ;; REC-EDIT
  6386. L16D4:  LD      DE,($5C59)      ; fetch start of edit line from E_LINE.
  6387.         JP      L19E5           ; jump forward to RECLAIM-1.
  6388.  
  6389. ; --------------------------
  6390. ; The Table INDEXING routine
  6391. ; --------------------------
  6392. ; This routine is used to search two-byte hash tables for a character
  6393. ; held in C, returning the address of the following offset byte.
  6394. ; if it is known that the character is in the table e.g. for priorities,
  6395. ; then the table requires no zero end-marker. If this is not known at the
  6396. ; outset then a zero end-marker is required and carry is set to signal
  6397. ; success.
  6398.  
  6399. ;; INDEXER-1
  6400. L16DB:  INC     HL              ; address the next pair of values.
  6401.  
  6402. ; -> The Entry Point.
  6403.  
  6404. ;; INDEXER
  6405. L16DC:  LD      A,(HL)          ; fetch the first byte of pair
  6406.         AND     A               ; is it the end-marker ?
  6407.         RET     Z               ; return with carry reset if so.
  6408.  
  6409.         CP      C               ; is it the required character ?
  6410.         INC     HL              ; address next location.
  6411.         JR      NZ,L16DB        ; back to INDEXER-1 if no match.
  6412.  
  6413.         SCF                     ; else set the carry flag.
  6414.         RET                     ; return with carry set
  6415.  
  6416. ; --------------------------------
  6417. ; The Channel and Streams Routines
  6418. ; --------------------------------
  6419. ; A channel is an input/output route to a hardware device
  6420. ; and is identified to the system by a single letter e.g. 'K' for
  6421. ; the keyboard. A channel can have an input and output route
  6422. ; associated with it in which case it is bi-directional like
  6423. ; the keyboard. Others like the upper screen 'S' are output
  6424. ; only and the input routine usually points to a report message.
  6425. ; Channels 'K' and 'S' are system channels and it would be inappropriate
  6426. ; to close the associated streams so a mechanism is provided to
  6427. ; re-attach them. When the re-attachment is no longer required, then
  6428. ; closing these streams resets them as at initialization.
  6429. ; The same also would have applied to channel 'R', the RS232 channel
  6430. ; as that is used by the system. It's input stream seems to have been
  6431. ; removed and it is not available to the user. However the channel could
  6432. ; not be removed entirely as its output routine was used by the system.
  6433. ; As a result of removing this channel, channel 'P', the printer is
  6434. ; erroneously treated as a system channel.
  6435. ; Ironically the tape streamer is not accessed through streams and
  6436. ; channels.
  6437. ; Early demonstrations of the Spectrum showed a single microdrive being
  6438. ; controlled by this ROM. Adverts also said that the network and RS232
  6439. ; were in this ROM. Channels 'M' and 'N' are user channels and have been
  6440. ; removed successfully if, as seems vaguely possible, they existed.
  6441.  
  6442. ; ---------------------
  6443. ; Handle CLOSE# command
  6444. ; ---------------------
  6445. ; This command allows streams to be closed after use.
  6446. ; Any temporary memory areas used by the stream would be reclaimed and
  6447. ; finally flags set or reset if necessary.
  6448.  
  6449. ;; CLOSE
  6450. L16E5:  CALL    L171E           ; routine STR-DATA fetches parameter
  6451.                                 ; from calculator stack and gets the
  6452.                                 ; existing STRMS data pointer address in HL
  6453.                                 ; and stream offset from CHANS in BC.
  6454.  
  6455.                                 ; Note. this offset could be zero if the
  6456.                                 ; stream is already closed. A check for this
  6457.                                 ; should occur now and an error should be
  6458.                                 ; generated, for example,
  6459.                                 ; Report S 'Stream already closed'.
  6460.  
  6461.         CALL    L1701           ; routine CLOSE-2 would perform any actions
  6462.                                 ; peculiar to that stream without disturbing
  6463.                                 ; data pointer to STRMS entry in HL.
  6464.  
  6465.         LD      BC,$0000        ; the stream is to be blanked.
  6466.         LD      DE,$A3E2        ; the number of bytes from stream 4, $5C1E,
  6467.                                 ; to $10000
  6468.         EX      DE,HL           ; transfer offset to HL, STRMS data pointer
  6469.                                 ; to DE.
  6470.         ADD     HL,DE           ; add the offset to the data pointer.  
  6471.         JR      C,L16FC         ; forward to CLOSE-1 if a non-system stream.
  6472.                                 ; i.e. higher than 3.
  6473.  
  6474. ; proceed with a negative result.
  6475.  
  6476.         LD      BC,L15C6 + 14   ; prepare the address of the byte after
  6477.                                 ; the initial stream data in ROM. ($15D4)
  6478.         ADD     HL,BC           ; index into the data table with negative value.
  6479.         LD      C,(HL)          ; low byte to C
  6480.         INC     HL              ; address next.
  6481.         LD      B,(HL)          ; high byte to B.
  6482.  
  6483. ; and for streams 0 - 3 just enter the initial data back into the STRMS entry
  6484. ; streams 0 - 2 can't be closed as they are shared by the operating system.
  6485. ; -> for streams 4 - 15 then blank the entry.
  6486.  
  6487. ;; CLOSE-1
  6488. L16FC:  EX      DE,HL           ; address of stream to HL.
  6489.         LD      (HL),C          ; place zero (or low byte).
  6490.         INC     HL              ; next address.
  6491.         LD      (HL),B          ; place zero (or high byte).
  6492.         RET                     ; return.
  6493.  
  6494. ; ------------------
  6495. ; CLOSE-2 Subroutine
  6496. ; ------------------
  6497. ; There is not much point in coming here.
  6498. ; The purpose was once to find the offset to a special closing routine,
  6499. ; in this ROM and within 256 bytes of the close stream look up table that
  6500. ; would reclaim any buffers associated with a stream. At least one has been
  6501. ; removed.
  6502.  
  6503. ;; CLOSE-2
  6504. L1701:  PUSH    HL              ; * save address of stream data pointer
  6505.                                 ; in STRMS on the machine stack.
  6506.         LD      HL,($5C4F)      ; fetch CHANS address to HL
  6507.         ADD     HL,BC           ; add the offset to address the second
  6508.                                 ; byte of the output routine hopefully.
  6509.         INC     HL              ; step past
  6510.         INC     HL              ; the input routine.
  6511.         INC     HL              ; to address channel's letter
  6512.         LD      C,(HL)          ; pick it up in C.
  6513.                                 ; Note. but if stream is already closed we
  6514.                                 ; get the value $10 (the byte preceding 'K').
  6515.         EX      DE,HL           ; save the pointer to the letter in DE.
  6516.         LD      HL,L1716        ; address: cl-str-lu in ROM.
  6517.         CALL    L16DC           ; routine INDEXER uses the code to get
  6518.                                 ; the 8-bit offset from the current point to
  6519.                                 ; the address of the closing routine in ROM.
  6520.                                 ; Note. it won't find $10 there!
  6521.         LD      C,(HL)          ; transfer the offset to C.
  6522.         LD      B,$00           ; prepare to add.
  6523.         ADD     HL,BC           ; add offset to point to the address of the
  6524.                                 ; routine that closes the stream.
  6525.                                 ; (and presumably removes any buffers that
  6526.                                 ; are associated with it.)
  6527.         JP      (HL)            ; jump to that routine.
  6528.  
  6529. ; --------------------------
  6530. ; CLOSE stream look-up table
  6531. ; --------------------------
  6532. ; This table contains an entry for a letter found in the CHANS area.
  6533. ; followed by an 8-bit displacement, from that byte's address in the
  6534. ; table to the routine that performs any ancillary actions associated
  6535. ; with closing the stream of that channel.
  6536. ; The table doesn't require a zero end-marker as the letter has been
  6537. ; picked up from a channel that has an open stream.
  6538.  
  6539. ;; cl-str-lu
  6540. L1716:  DB    'K', L171C-$-1  ; offset 5 to CLOSE-STR
  6541.         DB    'S', L171C-$-1  ; offset 3 to CLOSE-STR
  6542.         DB    'P', L171C-$-1  ; offset 1 to CLOSE-STR
  6543.  
  6544.  
  6545. ; ------------------------
  6546. ; Close Stream Subroutines
  6547. ; ------------------------
  6548. ; The close stream routines in fact have no ancillary actions to perform
  6549. ; which is not surprising with regard to 'K' and 'S'.
  6550.  
  6551. ;; CLOSE-STR                    
  6552. L171C:  POP     HL              ; * now just restore the stream data pointer
  6553.         RET                     ; in STRMS and return.
  6554.  
  6555. ; -----------
  6556. ; Stream data
  6557. ; -----------
  6558. ; This routine finds the data entry in the STRMS area for the specified
  6559. ; stream which is passed on the calculator stack. It returns with HL
  6560. ; pointing to this system variable and BC holding a displacement from
  6561. ; the CHANS area to the second byte of the stream's channel. If BC holds
  6562. ; zero, then that signifies that the stream is closed.
  6563.  
  6564. ;; STR-DATA
  6565. L171E:  CALL    L1E94           ; routine FIND-INT1 fetches parameter to A
  6566.         CP      $10             ; is it less than 16d ?
  6567.         JR      C,L1727         ; skip forward to STR-DATA1 if so.
  6568.  
  6569. ;; REPORT-Ob
  6570. L1725:  RST     08H             ; ERROR-1
  6571.         DB    $17             ; Error Report: Invalid stream
  6572.  
  6573. ;; STR-DATA1
  6574. L1727:  ADD     A,$03           ; add the offset for 3 system streams.
  6575.                                 ; range 00 - 15d becomes 3 - 18d.
  6576.         RLCA                    ; double as there are two bytes per
  6577.                                 ; stream - now 06 - 36d
  6578.         LD      HL,$5C10        ; address STRMS - the start of the streams
  6579.                                 ; data area in system variables.
  6580.         LD      C,A             ; transfer the low byte to A.
  6581.         LD      B,$00           ; prepare to add offset.
  6582.         ADD     HL,BC           ; add to address the data entry in STRMS.
  6583.  
  6584. ; the data entry itself contains an offset from CHANS to the address of the
  6585. ; stream
  6586.  
  6587.         LD      C,(HL)          ; low byte of displacement to C.
  6588.         INC     HL              ; address next.
  6589.         LD      B,(HL)          ; high byte of displacement to B.
  6590.         DEC     HL              ; step back to leave HL pointing to STRMS
  6591.                                 ; data entry.
  6592.         RET                     ; return with CHANS displacement in BC
  6593.                                 ; and address of stream data entry in HL.
  6594.  
  6595. ; --------------------
  6596. ; Handle OPEN# command
  6597. ; --------------------
  6598. ; Command syntax example: OPEN #5,"s"
  6599. ; On entry the channel code entry is on the calculator stack with the next
  6600. ; value containing the stream identifier. They have to swapped.
  6601.  
  6602. ;; OPEN
  6603. L1736:  RST     28H             ;; FP-CALC    ;s,c.
  6604.         DB    $01             ;;exchange    ;c,s.
  6605.         DB    $38             ;;end-calc
  6606.  
  6607.         CALL    L171E           ; routine STR-DATA fetches the stream off
  6608.                                 ; the stack and returns with the CHANS
  6609.                                 ; displacement in BC and HL addressing
  6610.                                 ; the STRMS data entry.
  6611.         LD      A,B             ; test for zero which
  6612.         OR      C               ; indicates the stream is closed.
  6613.         JR      Z,L1756         ; skip forward to OPEN-1 if so.
  6614.  
  6615. ; if it is a system channel then it can re-attached.
  6616.  
  6617.         EX      DE,HL           ; save STRMS address in DE.
  6618.         LD      HL,($5C4F)      ; fetch CHANS.
  6619.         ADD     HL,BC           ; add the offset to address the second
  6620.                                 ; byte of the channel.
  6621.         INC     HL              ; skip over the
  6622.         INC     HL              ; input routine.
  6623.         INC     HL              ; and address the letter.
  6624.         LD      A,(HL)          ; pick up the letter.
  6625.         EX      DE,HL           ; save letter pointer and bring back
  6626.                                 ; the STRMS pointer.
  6627.  
  6628.         CP      $4B             ; is it 'K' ?
  6629.         JR      Z,L1756         ; forward to OPEN-1 if so
  6630.  
  6631.         CP      $53             ; is it 'S' ?
  6632.         JR      Z,L1756         ; forward to OPEN-1 if so
  6633.  
  6634.         CP      $50             ; is it 'P' ?
  6635.         JR      NZ,L1725        ; back to REPORT-Ob if not.
  6636.                                 ; to report 'Invalid stream'.
  6637.  
  6638. ; continue if one of the upper-case letters was found.
  6639. ; and rejoin here from above if stream was closed.
  6640.  
  6641. ;; OPEN-1
  6642. L1756:  CALL    L175D           ; routine OPEN-2 opens the stream.
  6643.  
  6644. ; it now remains to update the STRMS variable.
  6645.  
  6646.         LD      (HL),E          ; insert or overwrite the low byte.
  6647.         INC     HL              ; address high byte in STRMS.
  6648.         LD      (HL),D          ; insert or overwrite the high byte.
  6649.         RET                     ; return.
  6650.  
  6651. ; -----------------
  6652. ; OPEN-2 Subroutine
  6653. ; -----------------
  6654. ; There is some point in coming here as, as well as once creating buffers,
  6655. ; this routine also sets flags.
  6656.  
  6657. ;; OPEN-2
  6658. L175D:  PUSH    HL              ; * save the STRMS data entry pointer.
  6659.         CALL    L2BF1           ; routine STK-FETCH now fetches the
  6660.                                 ; parameters of the channel string.
  6661.                                 ; start in DE, length in BC.
  6662.  
  6663.         LD      A,B             ; test that it is not
  6664.         OR      C               ; the null string.
  6665.         JR      NZ,L1767        ; skip forward to OPEN-3 with 1 character
  6666.                                 ; or more!
  6667.  
  6668. ;; REPORT-Fb
  6669. L1765:  RST     08H             ; ERROR-1
  6670.         DB    $0E             ; Error Report: Invalid file name
  6671.  
  6672. ;; OPEN-3
  6673. L1767:  PUSH    BC              ; save the length of the string.
  6674.         LD      A,(DE)          ; pick up the first character.
  6675.                                 ; Note. if the second character is used to
  6676.                                 ; distinguish between a binary or text
  6677.                                 ; channel then it will be simply a matter
  6678.                                 ; of setting bit 7 of FLAGX.
  6679.         AND     $DF             ; make it upper-case.
  6680.         LD      C,A             ; place it in C.
  6681.         LD      HL,L177A        ; address: op-str-lu is loaded.
  6682.         CALL    L16DC           ; routine INDEXER will search for letter.
  6683.         JR      NC,L1765        ; back to REPORT-F if not found
  6684.                                 ; 'Invalid filename'
  6685.  
  6686.         LD      C,(HL)          ; fetch the displacement to opening routine.
  6687.         LD      B,$00           ; prepare to add.
  6688.         ADD     HL,BC           ; now form address of opening routine.
  6689.         POP     BC              ; restore the length of string.
  6690.         JP      (HL)            ; now jump forward to the relevant routine.
  6691.  
  6692. ; -------------------------
  6693. ; OPEN stream look-up table
  6694. ; -------------------------
  6695. ; The open stream look-up table consists of matched pairs.
  6696. ; The channel letter is followed by an 8-bit displacement to the
  6697. ; associated stream-opening routine in this ROM.
  6698. ; The table requires a zero end-marker as the letter has been
  6699. ; provided by the user and not the operating system.
  6700.  
  6701. ;; op-str-lu
  6702. L177A:  DB    'K', L1781-$-1  ; $06 offset to OPEN-K
  6703.         DB    'S', L1785-$-1  ; $08 offset to OPEN-S
  6704.         DB    'P', L1789-$-1  ; $0A offset to OPEN-P
  6705.  
  6706.         DB    $00             ; end-marker.
  6707.  
  6708. ; ----------------------------
  6709. ; The Stream Opening Routines.
  6710. ; ----------------------------
  6711. ; These routines would have opened any buffers associated with the stream
  6712. ; before jumping forward to to OPEN-END with the displacement value in E
  6713. ; and perhaps a modified value in BC. The strange pathing does seem to
  6714. ; provide for flexibility in this respect.
  6715. ;
  6716. ; There is no need to open the printer buffer as it is there already
  6717. ; even if you are still saving up for a ZX Printer or have moved onto
  6718. ; something bigger. In any case it would have to be created after
  6719. ; the system variables but apart from that it is a simple task
  6720. ; and all but one of the ROM routines can handle a buffer in that position.
  6721. ; (PR-ALL-6 would require an extra 3 bytes of code).
  6722. ; However it wouldn't be wise to have two streams attached to the ZX Printer
  6723. ; as you can now, so one assumes that if PR_CC_hi was non-zero then
  6724. ; the OPEN-P routine would have refused to attach a stream if another
  6725. ; stream was attached.
  6726.  
  6727. ; Something of significance is being passed to these ghost routines in the
  6728. ; second character. Strings 'RB', 'RT' perhaps or a drive/station number.
  6729. ; The routine would have to deal with that and exit to OPEN_END with BC
  6730. ; containing $0001 or more likely there would be an exit within the routine.
  6731. ; Anyway doesn't matter, these routines are long gone.
  6732.  
  6733. ; -----------------
  6734. ; OPEN-K Subroutine
  6735. ; -----------------
  6736. ; Open Keyboard stream.
  6737.  
  6738. ;; OPEN-K
  6739. L1781:  LD      E,$01           ; 01 is offset to second byte of channel 'K'.
  6740.         JR      L178B           ; forward to OPEN-END
  6741.  
  6742. ; -----------------
  6743. ; OPEN-S Subroutine
  6744. ; -----------------
  6745. ; Open Screen stream.
  6746.  
  6747. ;; OPEN-S
  6748. L1785:  LD      E,$06           ; 06 is offset to 2nd byte of channel 'S'
  6749.         JR      L178B           ; to OPEN-END
  6750.  
  6751. ; -----------------
  6752. ; OPEN-P Subroutine
  6753. ; -----------------
  6754. ; Open Printer stream.
  6755.  
  6756. ;; OPEN-P
  6757. L1789:  LD      E,$10           ; 16d is offset to 2nd byte of channel 'P'
  6758.  
  6759. ;; OPEN-END
  6760. L178B:  DEC     BC              ; the stored length of 'K','S','P' or
  6761.                                 ; whatever is now tested. ??
  6762.         LD      A,B             ; test now if initial or residual length
  6763.         OR      C               ; is one character.
  6764.         JR      NZ,L1765        ; to REPORT-Fb 'Invalid file name' if not.
  6765.  
  6766.         LD      D,A             ; load D with zero to form the displacement
  6767.                                 ; in the DE register.
  6768.         POP     HL              ; * restore the saved STRMS pointer.
  6769.         RET                     ; return to update STRMS entry thereby
  6770.                                 ; signaling stream is open.
  6771.  
  6772. ; ----------------------------------------
  6773. ; Handle CAT, ERASE, FORMAT, MOVE commands
  6774. ; ----------------------------------------
  6775. ; These just generate an error report as the ROM is 'incomplete'.
  6776. ;
  6777. ; Luckily this provides a mechanism for extending these in a shadow ROM
  6778. ; but without the powerful mechanisms set up in this ROM.
  6779. ; An instruction fetch on $0008 may page in a peripheral ROM,
  6780. ; e.g. the Sinclair Interface 1 ROM, to handle these commands.
  6781. ; However that wasn't the plan.
  6782. ; Development of this ROM continued for another three months until the cost
  6783. ; of replacing it and the manual became unfeasible.
  6784. ; The ultimate power of channels and streams died at birth.
  6785.  
  6786. ;; CAT-ETC
  6787. L1793:  JR      L1725           ; to REPORT-Ob
  6788.  
  6789. ; -----------------
  6790. ; Perform AUTO-LIST
  6791. ; -----------------
  6792. ; This produces an automatic listing in the upper screen.
  6793.  
  6794. ;; AUTO-LIST
  6795. L1795:  LD      ($5C3F),SP      ; save stack pointer in LIST_SP
  6796.         LD      (IY+$02),$10    ; update TV_FLAG set bit 3
  6797.         CALL    L0DAF           ; routine CL-ALL.
  6798.         SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6799.  
  6800.         LD      B,(IY+$31)      ; fetch DF_SZ to B.
  6801.         CALL    L0E44           ; routine CL-LINE clears lower display
  6802.                                 ; preserving B.
  6803.         RES     0,(IY+$02)      ; update TV_FLAG  - signal main screen in use
  6804.         SET     0,(IY+$30)      ; update FLAGS2  - signal unnecessary to
  6805.                                 ; clear main screen.
  6806.         LD      HL,($5C49)      ; fetch E_PPC current edit line to HL.
  6807.         LD      DE,($5C6C)      ; fetch S_TOP to DE, the current top line
  6808.                                 ; (initially zero)
  6809.         AND     A               ; prepare for true subtraction.
  6810.         SBC     HL,DE           ; subtract and
  6811.         ADD     HL,DE           ; add back.
  6812.         JR      C,L17E1         ; to AUTO-L-2 if S_TOP higher than E_PPC
  6813.                                 ; to set S_TOP to E_PPC
  6814.  
  6815.         PUSH    DE              ; save the top line number.
  6816.         CALL    L196E           ; routine LINE-ADDR gets address of E_PPC.
  6817.         LD      DE,$02C0        ; prepare known number of characters in
  6818.                                 ; the default upper screen.
  6819.         EX      DE,HL           ; offset to HL, program address to DE.
  6820.         SBC     HL,DE           ; subtract high value from low to obtain
  6821.                                 ; negated result used in addition.
  6822.         EX      (SP),HL         ; swap result with top line number on stack.
  6823.         CALL    L196E           ; routine LINE-ADDR  gets address of that
  6824.                                 ; top line in HL and next line in DE.
  6825.         POP     BC              ; restore the result to balance stack.
  6826.  
  6827. ;; AUTO-L-1
  6828. L17CE:  PUSH    BC              ; save the result.
  6829.         CALL    L19B8           ; routine NEXT-ONE gets address in HL of
  6830.                                 ; line after auto-line (in DE).
  6831.         POP     BC              ; restore result.
  6832.         ADD     HL,BC           ; compute back.
  6833.         JR      C,L17E4         ; to AUTO-L-3 if line 'should' appear
  6834.  
  6835.         EX      DE,HL           ; address of next line to HL.
  6836.         LD      D,(HL)          ; get line
  6837.         INC     HL              ; number
  6838.         LD      E,(HL)          ; in DE.
  6839.         DEC     HL              ; adjust back to start.
  6840.         LD      ($5C6C),DE      ; update S_TOP.
  6841.         JR      L17CE           ; to AUTO-L-1 until estimate reached.
  6842.  
  6843. ; ---
  6844.  
  6845. ; the jump was to here if S_TOP was greater than E_PPC
  6846.  
  6847. ;; AUTO-L-2
  6848. L17E1:  LD      ($5C6C),HL      ; make S_TOP the same as E_PPC.
  6849.  
  6850. ; continue here with valid starting point from above or good estimate
  6851. ; from computation
  6852.  
  6853. ;; AUTO-L-3
  6854. L17E4:  LD      HL,($5C6C)      ; fetch S_TOP line number to HL.
  6855.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  6856.                                 ; address of next in DE.
  6857.         JR      Z,L17ED         ; to AUTO-L-4 if line exists.
  6858.  
  6859.         EX      DE,HL           ; else use address of next line.
  6860.  
  6861. ;; AUTO-L-4
  6862. L17ED:  CALL    L1833           ; routine LIST-ALL                >>>
  6863.  
  6864. ; The return will be to here if no scrolling occurred
  6865.  
  6866.         RES     4,(IY+$02)      ; update TV_FLAG  - signal no auto listing.
  6867.         RET                     ; return.
  6868.  
  6869. ; ------------
  6870. ; Handle LLIST
  6871. ; ------------
  6872. ; A short form of LIST #3. The listing goes to stream 3 - default printer.
  6873.  
  6874. ;; LLIST
  6875. L17F5:  LD      A,$03           ; the usual stream for ZX Printer
  6876.         JR      L17FB           ; forward to LIST-1
  6877.  
  6878. ; -----------
  6879. ; Handle LIST
  6880. ; -----------
  6881. ; List to any stream.
  6882. ; Note. While a starting line can be specified it is
  6883. ; not possible to specify an end line.
  6884. ; Just listing a line makes it the current edit line.
  6885.  
  6886. ;; LIST
  6887. L17F9:  LD      A,$02           ; default is stream 2 - the upper screen.
  6888.  
  6889. ;; LIST-1
  6890. L17FB:  LD      (IY+$02),$00    ; the TV_FLAG is initialized with bit 0 reset
  6891.                                 ; indicating upper screen in use.
  6892.         CALL    L2530           ; routine SYNTAX-Z - checking syntax ?
  6893.         CALL    NZ,L1601        ; routine CHAN-OPEN if in run-time.
  6894.  
  6895.         RST     18H             ; GET-CHAR
  6896.         CALL    L2070           ; routine STR-ALTER will alter if '#'.
  6897.         JR      C,L181F         ; forward to LIST-4 not a '#' .
  6898.  
  6899.  
  6900.         RST     18H             ; GET-CHAR
  6901.         CP      $3B             ; is it ';' ?
  6902.         JR      Z,L1814         ; skip to LIST-2 if so.
  6903.  
  6904.         CP      $2C             ; is it ',' ?
  6905.         JR      NZ,L181A        ; forward to LIST-3 if neither separator.
  6906.  
  6907. ; we have, say,  LIST #15, and a number must follow the separator.
  6908.  
  6909. ;; LIST-2
  6910. L1814:  RST     20H             ; NEXT-CHAR
  6911.         CALL    L1C82           ; routine EXPT-1NUM
  6912.         JR      L1822           ; forward to LIST-5
  6913.  
  6914. ; ---
  6915.  
  6916. ; the branch was here with just LIST #3 etc.
  6917.  
  6918. ;; LIST-3
  6919. L181A:  CALL    L1CE6           ; routine USE-ZERO
  6920.         JR      L1822           ; forward to LIST-5
  6921.  
  6922. ; ---
  6923.  
  6924. ; the branch was here with LIST
  6925.  
  6926. ;; LIST-4
  6927. L181F:  CALL    L1CDE           ; routine FETCH-NUM checks if a number
  6928.                                 ; follows else uses zero.
  6929.  
  6930. ;; LIST-5
  6931. L1822:  CALL    L1BEE           ; routine CHECK-END quits if syntax OK >>>
  6932.  
  6933.         CALL    L1E99           ; routine FIND-INT2 fetches the number
  6934.                                 ; from the calculator stack in run-time.
  6935.         LD      A,B             ; fetch high byte of line number and
  6936.         AND     $3F             ; make less than $40 so that NEXT-ONE
  6937.                                 ; (from LINE-ADDR) doesn't lose context.
  6938.                                 ; Note. this is not satisfactory and the typo
  6939.                                 ; LIST 20000 will list an entirely different
  6940.                                 ; section than LIST 2000. Such typos are not
  6941.                                 ; available for checking if they are direct
  6942.                                 ; commands.
  6943.  
  6944.         LD      H,A             ; transfer the modified
  6945.         LD      L,C             ; line number to HL.
  6946.         LD      ($5C49),HL      ; update E_PPC to new line number.
  6947.         CALL    L196E           ; routine LINE-ADDR gets the address of the
  6948.                                 ; line.
  6949.  
  6950. ; This routine is called from AUTO-LIST
  6951.  
  6952. ;; LIST-ALL
  6953. L1833:  LD      E,$01           ; signal current line not yet printed
  6954.  
  6955. ;; LIST-ALL-2
  6956. L1835:  CALL    L1855           ; routine OUT-LINE outputs a BASIC line
  6957.                                 ; using PRINT-OUT and makes an early return
  6958.                                 ; when no more lines to print. >>>
  6959.  
  6960.         RST     10H             ; PRINT-A prints the carriage return (in A)
  6961.  
  6962.         BIT     4,(IY+$02)      ; test TV_FLAG  - automatic listing ?
  6963.         JR      Z,L1835         ; back to LIST-ALL-2 if not
  6964.                                 ; (loop exit is via OUT-LINE)
  6965.  
  6966. ; continue here if an automatic listing required.
  6967.  
  6968.         LD      A,($5C6B)       ; fetch DF_SZ lower display file size.
  6969.         SUB     (IY+$4F)        ; subtract S_POSN_hi ithe current line number.
  6970.         JR      NZ,L1835        ; back to LIST-ALL-2 if upper screen not full.
  6971.  
  6972.         XOR     E               ; A contains zero, E contains one if the
  6973.                                 ; current edit line has not been printed
  6974.                                 ; or zero if it has (from OUT-LINE).
  6975.         RET     Z               ; return if the screen is full and the line
  6976.                                 ; has been printed.
  6977.  
  6978. ; continue with automatic listings if the screen is full and the current
  6979. ; edit line is missing. OUT-LINE will scroll automatically.
  6980.  
  6981.         PUSH    HL              ; save the pointer address.
  6982.         PUSH    DE              ; save the E flag.
  6983.         LD      HL,$5C6C        ; fetch S_TOP the rough estimate.
  6984.         CALL    L190F           ; routine LN-FETCH updates S_TOP with
  6985.                                 ; the number of the next line.
  6986.         POP     DE              ; restore the E flag.
  6987.         POP     HL              ; restore the address of the next line.
  6988.         JR      L1835           ; back to LIST-ALL-2.
  6989.  
  6990. ; ------------------------
  6991. ; Print a whole BASIC line
  6992. ; ------------------------
  6993. ; This routine prints a whole BASIC line and it is called
  6994. ; from LIST-ALL to output the line to current channel
  6995. ; and from ED-EDIT to 'sprint' the line to the edit buffer.
  6996.  
  6997. ;; OUT-LINE
  6998. L1855:  LD      BC,($5C49)      ; fetch E_PPC the current line which may be
  6999.                                 ; unchecked and not exist.
  7000.         CALL    L1980           ; routine CP-LINES finds match or line after.
  7001.         LD      D,$3E           ; prepare cursor '>' in D.
  7002.         JR      Z,L1865         ; to OUT-LINE1 if matched or line after.
  7003.  
  7004.         LD      DE,$0000        ; put zero in D, to suppress line cursor.
  7005.         RL      E               ; pick up carry in E if line before current
  7006.                                 ; leave E zero if same or after.
  7007.  
  7008. ;; OUT-LINE1
  7009. L1865:  LD      (IY+$2D),E      ; save flag in BREG which is spare.
  7010.         LD      A,(HL)          ; get high byte of line number.
  7011.         CP      $40             ; is it too high ($2F is maximum possible) ?
  7012.         POP     BC              ; drop the return address and
  7013.         RET     NC              ; make an early return if so >>>
  7014.  
  7015.         PUSH    BC              ; save return address
  7016.         CALL    L1A28           ; routine OUT-NUM-2 to print addressed number
  7017.                                 ; with leading space.
  7018.         INC     HL              ; skip low number byte.
  7019.         INC     HL              ; and the two
  7020.         INC     HL              ; length bytes.
  7021.         RES     0,(IY+$01)      ; update FLAGS - signal leading space required.
  7022.         LD      A,D             ; fetch the cursor.
  7023.         AND     A               ; test for zero.
  7024.         JR      Z,L1881         ; to OUT-LINE3 if zero.
  7025.  
  7026.  
  7027.         RST     10H             ; PRINT-A prints '>' the current line cursor.
  7028.  
  7029. ; this entry point is called from ED-COPY
  7030.  
  7031. ;; OUT-LINE2
  7032. L187D:  SET     0,(IY+$01)      ; update FLAGS - suppress leading space.
  7033.  
  7034. ;; OUT-LINE3
  7035. L1881:  PUSH    DE              ; save flag E for a return value.
  7036.         EX      DE,HL           ; save HL address in DE.
  7037.         RES     2,(IY+$30)      ; update FLAGS2 - signal NOT in QUOTES.
  7038.  
  7039.         LD      HL,$5C3B        ; point to FLAGS.
  7040.         RES     2,(HL)          ; signal 'K' mode. (starts before keyword)
  7041.         BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7042.         JR      Z,L1894         ; forward to OUT-LINE4 if not.
  7043.  
  7044.         SET     2,(HL)          ; signal 'L' mode. (used for input)
  7045.  
  7046. ;; OUT-LINE4
  7047. L1894:  LD      HL,($5C5F)      ; fetch X_PTR - possibly the error pointer
  7048.                                 ; address.
  7049.         AND     A               ; clear the carry flag.
  7050.         SBC     HL,DE           ; test if an error address has been reached.
  7051.         JR      NZ,L18A1        ; forward to OUT-LINE5 if not.
  7052.  
  7053.         LD      A,$3F           ; load A with '?' the error marker.
  7054.         CALL    L18C1           ; routine OUT-FLASH to print flashing marker.
  7055.  
  7056. ;; OUT-LINE5
  7057. L18A1:  CALL    L18E1           ; routine OUT-CURS will print the cursor if
  7058.                                 ; this is the right position.
  7059.         EX      DE,HL           ; restore address pointer to HL.
  7060.         LD      A,(HL)          ; fetch the addressed character.
  7061.         CALL    L18B6           ; routine NUMBER skips a hidden floating
  7062.                                 ; point number if present.
  7063.         INC     HL              ; now increment the pointer.
  7064.         CP      $0D             ; is character end-of-line ?
  7065.         JR      Z,L18B4         ; to OUT-LINE6, if so, as line is finished.
  7066.  
  7067.         EX      DE,HL           ; save the pointer in DE.
  7068.         CALL    L1937           ; routine OUT-CHAR to output character/token.
  7069.  
  7070.         JR      L1894           ; back to OUT-LINE4 until entire line is done.
  7071.  
  7072. ; ---
  7073.  
  7074. ;; OUT-LINE6
  7075. L18B4:  POP     DE              ; bring back the flag E, zero if current
  7076.                                 ; line printed else 1 if still to print.
  7077.         RET                     ; return with A holding $0D
  7078.  
  7079. ; -------------------------
  7080. ; Check for a number marker
  7081. ; -------------------------
  7082. ; this subroutine is called from two processes. while outputting BASIC lines
  7083. ; and while searching statements within a BASIC line.
  7084. ; during both, this routine will pass over an invisible number indicator
  7085. ; and the five bytes floating-point number that follows it.
  7086. ; Note that this causes floating point numbers to be stripped from
  7087. ; the BASIC line when it is fetched to the edit buffer by OUT_LINE.
  7088. ; the number marker also appears after the arguments of a DEF FN statement
  7089. ; and may mask old 5-byte string parameters.
  7090.  
  7091. ;; NUMBER
  7092. L18B6:  CP      $0E             ; character fourteen ?
  7093.         RET     NZ              ; return if not.
  7094.  
  7095.         INC     HL              ; skip the character
  7096.         INC     HL              ; and five bytes
  7097.         INC     HL              ; following.
  7098.         INC     HL              ;
  7099.         INC     HL              ;
  7100.         INC     HL              ;
  7101.         LD      A,(HL)          ; fetch the following character
  7102.         RET                     ; for return value.
  7103.  
  7104. ; --------------------------
  7105. ; Print a flashing character
  7106. ; --------------------------
  7107. ; This subroutine is called from OUT-LINE to print a flashing error
  7108. ; marker '?' or from the next routine to print a flashing cursor e.g. 'L'.
  7109. ; However, this only gets called from OUT-LINE when printing the edit line
  7110. ; or the input buffer to the lower screen so a direct call to $09F4 can
  7111. ; be used, even though out-line outputs to other streams.
  7112. ; In fact the alternate set is used for the whole routine.
  7113.  
  7114. ;; OUT-FLASH
  7115. L18C1:  EXX                     ; switch in alternate set
  7116.  
  7117.         LD      HL,($5C8F)      ; fetch L = ATTR_T, H = MASK-T
  7118.         PUSH    HL              ; save masks.
  7119.         RES     7,H             ; reset flash mask bit so active.
  7120.         SET     7,L             ; make attribute FLASH.
  7121.         LD      ($5C8F),HL      ; resave ATTR_T and MASK-T
  7122.  
  7123.         LD      HL,$5C91        ; address P_FLAG
  7124.         LD      D,(HL)          ; fetch to D
  7125.         PUSH    DE              ; and save.
  7126.         LD      (HL),$00        ; clear inverse, over, ink/paper 9
  7127.  
  7128.         CALL    L09F4           ; routine PRINT-OUT outputs character
  7129.                                 ; without the need to vector via RST 10.
  7130.  
  7131.         POP     HL              ; pop P_FLAG to H.
  7132.         LD      (IY+$57),H      ; and restore system variable P_FLAG.
  7133.         POP     HL              ; restore temporary masks
  7134.         LD      ($5C8F),HL      ; and restore system variables ATTR_T/MASK_T
  7135.  
  7136.         EXX                     ; switch back to main set
  7137.         RET                     ; return
  7138.  
  7139. ; ----------------
  7140. ; Print the cursor
  7141. ; ----------------
  7142. ; This routine is called before any character is output while outputting
  7143. ; a BASIC line or the input buffer. This includes listing to a printer
  7144. ; or screen, copying a BASIC line to the edit buffer and printing the
  7145. ; input buffer or edit buffer to the lower screen. It is only in the
  7146. ; latter two cases that it has any relevance and in the last case it
  7147. ; performs another very important function also.
  7148.  
  7149. ;; OUT-CURS
  7150. L18E1:  LD      HL,($5C5B)      ; fetch K_CUR the current cursor address
  7151.         AND     A               ; prepare for true subtraction.
  7152.         SBC     HL,DE           ; test against pointer address in DE and
  7153.         RET     NZ              ; return if not at exact position.
  7154.  
  7155. ; the value of MODE, maintained by KEY-INPUT, is tested and if non-zero
  7156. ; then this value 'E' or 'G' will take precedence.
  7157.  
  7158.         LD      A,($5C41)       ; fetch MODE  0='KLC', 1='E', 2='G'.
  7159.                 DB 0XCB
  7160.                 RLCA               ; double the value and set flags.
  7161.         JR      Z,L18F3         ; to OUT-C-1 if still zero ('KLC').
  7162.  
  7163.         ADD     A,$43           ; add 'C' - will become 'E' if originally 1
  7164.                                 ; or 'G' if originally 2.
  7165.         JR      L1909           ; forward to OUT-C-2 to print.
  7166.  
  7167. ; ---
  7168.  
  7169. ; If mode was zero then, while printing a BASIC line, bit 2 of flags has been
  7170. ; set if 'THEN' or ':' was encountered as a main character and reset otherwise.
  7171. ; This is now used to determine if the 'K' cursor is to be printed but this
  7172. ; transient state is also now transferred permanently to bit 3 of FLAGS
  7173. ; to let the interrupt routine know how to decode the next key.
  7174.  
  7175. ;; OUT-C-1
  7176. L18F3:  LD      HL,$5C3B        ; Address FLAGS
  7177.         RES     3,(HL)          ; signal 'K' mode initially.
  7178.         LD      A,$4B           ; prepare letter 'K'.
  7179.         BIT     2,(HL)          ; test FLAGS - was the
  7180.                                 ; previous main character ':' or 'THEN' ?
  7181.         JR      Z,L1909         ; forward to OUT-C-2 if so to print.
  7182.  
  7183.         SET     3,(HL)          ; signal 'L' mode to interrupt routine.
  7184.                                 ; Note. transient bit has been made permanent.
  7185.         INC     A               ; augment from 'K' to 'L'.
  7186.  
  7187.         BIT     3,(IY+$30)      ; test FLAGS2 - consider caps lock ?
  7188.                                 ; which is maintained by KEY-INPUT.
  7189.         JR      Z,L1909         ; forward to OUT-C-2 if not set to print.
  7190.  
  7191.         LD      A,$43           ; alter 'L' to 'C'.
  7192.  
  7193. ;; OUT-C-2
  7194. L1909:  PUSH    DE              ; save address pointer but OK as OUT-FLASH
  7195.                                 ; uses alternate set without RST 10.
  7196.  
  7197.         CALL    L18C1           ; routine OUT-FLASH to print.
  7198.  
  7199.         POP     DE              ; restore and
  7200.         RET                     ; return.
  7201.  
  7202. ; ----------------------------
  7203. ; Get line number of next line
  7204. ; ----------------------------
  7205. ; These two subroutines are called while editing.
  7206. ; This entry point is from ED-DOWN with HL addressing E_PPC
  7207. ; to fetch the next line number.
  7208. ; Also from AUTO-LIST with HL addressing S_TOP just to update S_TOP
  7209. ; with the value of the next line number. It gets fetched but is discarded.
  7210. ; These routines never get called while the editor is being used for input.
  7211.  
  7212. ;; LN-FETCH
  7213. L190F:  LD      E,(HL)          ; fetch low byte
  7214.         INC     HL              ; address next
  7215.         LD      D,(HL)          ; fetch high byte.
  7216.         PUSH    HL              ; save system variable hi pointer.
  7217.         EX      DE,HL           ; line number to HL,
  7218.         INC     HL              ; increment as a starting point.
  7219.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  7220.         CALL    L1695           ; routine LINE-NO gets line number in DE.
  7221.         POP     HL              ; restore system variable hi pointer.
  7222.  
  7223. ; This entry point is from the ED-UP with HL addressing E_PPC_hi
  7224.  
  7225. ;; LN-STORE
  7226. L191C:  BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7227.         RET     NZ              ; return if so.
  7228.                                 ; Note. above already checked by ED-UP/ED-DOWN.
  7229.  
  7230.         LD      (HL),D          ; save high byte of line number.
  7231.         DEC     HL              ; address lower
  7232.         LD      (HL),E          ; save low byte of line number.
  7233.         RET                     ; return.
  7234.  
  7235. ; -----------------------------------------
  7236. ; Outputting numbers at start of BASIC line
  7237. ; -----------------------------------------
  7238. ; This routine entered at OUT-SP-NO is used to compute then output the first
  7239. ; three digits of a 4-digit BASIC line printing a space if necessary.
  7240. ; The line number, or residual part, is held in HL and the BC register
  7241. ; holds a subtraction value -1000, -100 or -10.
  7242. ; Note. for example line number 200 -
  7243. ; space(out_char), 2(out_code), 0(out_char) final number always out-code.
  7244.  
  7245. ;; OUT-SP-2
  7246. L1925:  LD      A,E             ; will be space if OUT-CODE not yet called.
  7247.                                 ; or $FF if spaces are suppressed.
  7248.                                 ; else $30 ('0').
  7249.                                 ; (from the first instruction at OUT-CODE)
  7250.                                 ; this guy is just too clever.
  7251.         AND     A               ; test bit 7 of A.
  7252.         RET     M               ; return if $FF, as leading spaces not
  7253.                                 ; required. This is set when printing line
  7254.                                 ; number and statement in MAIN-5.
  7255.  
  7256.         JR      L1937           ; forward to exit via OUT-CHAR.
  7257.  
  7258. ; ---
  7259.  
  7260. ; -> the single entry point.
  7261.  
  7262. ;; OUT-SP-NO
  7263. L192A:  XOR     A               ; initialize digit to 0
  7264.  
  7265. ;; OUT-SP-1
  7266. L192B:  ADD     HL,BC           ; add negative number to HL.
  7267.         INC     A               ; increment digit
  7268.         JR      C,L192B         ; back to OUT-SP-1 until no carry from
  7269.                                 ; the addition.
  7270.  
  7271.         SBC     HL,BC           ; cancel the last addition
  7272.         DEC     A               ; and decrement the digit.
  7273.         JR      Z,L1925         ; back to OUT-SP-2 if it is zero.
  7274.  
  7275.         JP      L15EF           ; jump back to exit via OUT-CODE.    ->
  7276.  
  7277.  
  7278. ; -------------------------------------
  7279. ; Outputting characters in a BASIC line
  7280. ; -------------------------------------
  7281. ; This subroutine ...
  7282.  
  7283. ;; OUT-CHAR
  7284. L1937:  CALL    L2D1B           ; routine NUMERIC tests if it is a digit ?
  7285.         JR      NC,L196C        ; to OUT-CH-3 to print digit without
  7286.                                 ; changing mode. Will be 'K' mode if digits
  7287.                                 ; are at beginning of edit line.
  7288.  
  7289.         CP      $21             ; less than quote character ?
  7290.         JR      C,L196C         ; to OUT-CH-3 to output controls and space.
  7291.  
  7292.         RES     2,(IY+$01)      ; initialize FLAGS to 'K' mode and leave
  7293.                                 ; unchanged if this character would precede
  7294.                                 ; a keyword.
  7295.  
  7296.         CP      $CB             ; is character 'THEN' token ?
  7297.         JR      Z,L196C         ; to OUT-CH-3 to output if so.
  7298.  
  7299.         CP      $3A             ; is it ':' ?
  7300.         JR      NZ,L195A        ; to OUT-CH-1 if not statement separator
  7301.                                 ; to change mode back to 'L'.
  7302.  
  7303.         BIT     5,(IY+$37)      ; FLAGX  - Input Mode ??
  7304.         JR      NZ,L1968        ; to OUT-CH-2 if in input as no statements.
  7305.                                 ; Note. this check should seemingly be at
  7306.                                 ; the start. Commands seem inappropriate in
  7307.                                 ; INPUT mode and are rejected by the syntax
  7308.                                 ; checker anyway.
  7309.                                 ; unless INPUT LINE is being used.
  7310.  
  7311.         BIT     2,(IY+$30)      ; test FLAGS2 - is the ':' within quotes ?
  7312.         JR      Z,L196C         ; to OUT-CH-3 if ':' is outside quoted text.
  7313.  
  7314.         JR      L1968           ; to OUT-CH-2 as ':' is within quotes
  7315.  
  7316. ; ---
  7317.  
  7318. ;; OUT-CH-1
  7319. L195A:  CP      $22             ; is it quote character '"'  ?
  7320.         JR      NZ,L1968        ; to OUT-CH-2 with others to set 'L' mode.
  7321.  
  7322.         PUSH    AF              ; save character.
  7323.         LD      A,($5C6A)       ; fetch FLAGS2.
  7324.         XOR     $04             ; toggle the quotes flag.
  7325.         LD      ($5C6A),A       ; update FLAGS2
  7326.         POP     AF              ; and restore character.
  7327.  
  7328. ;; OUT-CH-2
  7329. L1968:  SET     2,(IY+$01)      ; update FLAGS - signal L mode if the cursor
  7330.                                 ; is next.
  7331.  
  7332. ;; OUT-CH-3
  7333. L196C:  RST     10H             ; PRINT-A vectors the character to
  7334.                                 ; channel 'S', 'K', 'R' or 'P'.
  7335.         RET                     ; return.
  7336.  
  7337. ; -------------------------------------------
  7338. ; Get starting address of line, or line after
  7339. ; -------------------------------------------
  7340. ; This routine is used often to get the address, in HL, of a BASIC line
  7341. ; number supplied in HL, or failing that the address of the following line
  7342. ; and the address of the previous line in DE.
  7343.  
  7344. ;; LINE-ADDR
  7345. L196E:  PUSH    HL              ; save line number in HL register
  7346.         LD      HL,($5C53)      ; fetch start of program from PROG
  7347.         LD      D,H             ; transfer address to
  7348.         LD      E,L             ; the DE register pair.
  7349.  
  7350. ;; LINE-AD-1
  7351. L1974:  POP     BC              ; restore the line number to BC
  7352.         CALL    L1980           ; routine CP-LINES compares with that
  7353.                                 ; addressed by HL
  7354.         RET     NC              ; return if line has been passed or matched.
  7355.                                 ; if NZ, address of previous is in DE
  7356.  
  7357.         PUSH    BC              ; save the current line number
  7358.         CALL    L19B8           ; routine NEXT-ONE finds address of next
  7359.                                 ; line number in DE, previous in HL.
  7360.         EX      DE,HL           ; switch so next in HL
  7361.         JR      L1974           ; back to LINE-AD-1 for another comparison
  7362.  
  7363. ; --------------------
  7364. ; Compare line numbers
  7365. ; --------------------
  7366. ; This routine compares a line number supplied in BC with an addressed
  7367. ; line number pointed to by HL.
  7368.  
  7369. ;; CP-LINES
  7370. L1980:  LD      A,(HL)          ; Load the high byte of line number and
  7371.         CP      B               ; compare with that of supplied line number.
  7372.         RET     NZ              ; return if yet to match (carry will be set).
  7373.  
  7374.         INC     HL              ; address low byte of
  7375.         LD      A,(HL)          ; number and pick up in A.
  7376.         DEC     HL              ; step back to first position.
  7377.         CP      C               ; now compare.
  7378.         RET                     ; zero set if exact match.
  7379.                                 ; carry set if yet to match.
  7380.                                 ; no carry indicates a match or
  7381.                                 ; next available BASIC line or
  7382.                                 ; program end marker.
  7383.  
  7384. ; -------------------
  7385. ; Find each statement
  7386. ; -------------------
  7387. ; The single entry point EACH-STMT is used to
  7388. ; 1) To find the D'th statement in a line.
  7389. ; 2) To find a token in held E.
  7390.  
  7391. ;; not-used
  7392. L1988:  INC     HL              ;
  7393.         INC     HL              ;
  7394.         INC     HL              ;
  7395.  
  7396. ; -> entry point.
  7397.  
  7398. ;; EACH-STMT
  7399. L198B:  LD      ($5C5D),HL      ; save HL in CH_ADD
  7400.         LD      C,$00           ; initialize quotes flag
  7401.  
  7402. ;; EACH-S-1
  7403. L1990:  DEC     D               ; decrease statement count
  7404.         RET     Z               ; return if zero
  7405.  
  7406.  
  7407.         RST     20H             ; NEXT-CHAR
  7408.         CP      E               ; is it the search token ?
  7409.         JR      NZ,L199A        ; forward to EACH-S-3 if not
  7410.  
  7411.         AND     A               ; clear carry
  7412.         RET                     ; return signalling success.
  7413.  
  7414. ; ---
  7415.  
  7416. ;; EACH-S-2
  7417. L1998:  INC     HL              ; next address
  7418.         LD      A,(HL)          ; next character
  7419.  
  7420. ;; EACH-S-3
  7421. L199A:  CALL    L18B6           ; routine NUMBER skips if number marker
  7422.         LD      ($5C5D),HL      ; save in CH_ADD
  7423.         CP      $22             ; is it quotes '"' ?
  7424.         JR      NZ,L19A5        ; to EACH-S-4 if not
  7425.  
  7426.         DEC     C               ; toggle bit 0 of C
  7427.  
  7428. ;; EACH-S-4
  7429. L19A5:  CP      $3A             ; is it ':'
  7430.         JR      Z,L19AD         ; to EACH-S-5
  7431.  
  7432.         CP      $CB             ; 'THEN'
  7433.         JR      NZ,L19B1        ; to EACH-S-6
  7434.  
  7435. ;; EACH-S-5
  7436. L19AD:  BIT     0,C             ; is it in quotes
  7437.         JR      Z,L1990         ; to EACH-S-1 if not
  7438.  
  7439. ;; EACH-S-6
  7440. L19B1:  CP      $0D             ; end of line ?
  7441.         JR      NZ,L1998        ; to EACH-S-2
  7442.  
  7443.         DEC     D               ; decrease the statement counter
  7444.                                 ; which should be zero else
  7445.                                 ; 'Statement Lost'.
  7446.         SCF                     ; set carry flag - not found
  7447.         RET                     ; return
  7448.  
  7449. ; -----------------------------------------------------------------------
  7450. ; Storage of variables. For full details - see chapter 24.
  7451. ; ZX Spectrum BASIC Programming by Steven Vickers 1982.
  7452. ; It is bits 7-5 of the first character of a variable that allow
  7453. ; the six types to be distinguished. Bits 4-0 are the reduced letter.
  7454. ; So any variable name is higher that $3F and can be distinguished
  7455. ; also from the variables area end-marker $80.
  7456. ;
  7457. ; 76543210 meaning                               brief outline of format.
  7458. ; -------- ------------------------              -----------------------
  7459. ; 010      string variable.                      2 byte length + contents.
  7460. ; 110      string array.                         2 byte length + contents.
  7461. ; 100      array of numbers.                     2 byte length + contents.
  7462. ; 011      simple numeric variable.              5 bytes.
  7463. ; 101      variable length named numeric.        5 bytes.
  7464. ; 111      for-next loop variable.               18 bytes.
  7465. ; 10000000 the variables area end-marker.
  7466. ;
  7467. ; Note. any of the above seven will serve as a program end-marker.
  7468. ;
  7469. ; -----------------------------------------------------------------------
  7470.  
  7471. ; ------------
  7472. ; Get next one
  7473. ; ------------
  7474. ; This versatile routine is used to find the address of the next line
  7475. ; in the program area or the next variable in the variables area.
  7476. ; The reason one routine is made to handle two apparently unrelated tasks
  7477. ; is that it can be called indiscriminately when merging a line or a
  7478. ; variable.
  7479.  
  7480. ;; NEXT-ONE
  7481. L19B8:  PUSH    HL              ; save the pointer address.
  7482.         LD      A,(HL)          ; get first byte.
  7483.         CP      $40             ; compare with upper limit for line numbers.
  7484.         JR      C,L19D5         ; forward to NEXT-O-3 if within BASIC area.
  7485.  
  7486. ; the continuation here is for the next variable unless the supplied
  7487. ; line number was erroneously over 16383. see RESTORE command.
  7488.  
  7489.         BIT     5,A             ; is it a string or an array variable ?
  7490.         JR      Z,L19D6         ; forward to NEXT-O-4 to compute length.
  7491.  
  7492.         ADD     A,A             ; test bit 6 for single-character variables.
  7493.         JP      M,L19C7         ; forward to NEXT-O-1 if so
  7494.  
  7495.         CCF                     ; clear the carry for long-named variables.
  7496.                                 ; it remains set for for-next loop variables.
  7497.  
  7498. ;; NEXT-O-1
  7499. L19C7:  LD      BC,$0005        ; set BC to 5 for floating point number
  7500.         JR      NC,L19CE        ; forward to NEXT-O-2 if not a for/next
  7501.                                 ; variable.
  7502.  
  7503.         LD      C,$12           ; set BC to eighteen locations.
  7504.                                 ; value, limit, step, line and statement.
  7505.  
  7506. ; now deal with long-named variables
  7507.  
  7508. ;; NEXT-O-2
  7509. L19CE:  RLA                     ; test if character inverted. carry will also
  7510.                                 ; be set for single character variables
  7511.         INC     HL              ; address next location.
  7512.         LD      A,(HL)          ; and load character.
  7513.         JR      NC,L19CE        ; back to NEXT-O-2 if not inverted bit.
  7514.                                 ; forward immediately with single character
  7515.                                 ; variable names.
  7516.  
  7517.         JR      L19DB           ; forward to NEXT-O-5 to add length of
  7518.                                 ; floating point number(s etc.).
  7519.  
  7520. ; ---
  7521.  
  7522. ; this branch is for line numbers.
  7523.  
  7524. ;; NEXT-O-3
  7525. L19D5:  INC     HL              ; increment pointer to low byte of line no.
  7526.  
  7527. ; strings and arrays rejoin here
  7528.  
  7529. ;; NEXT-O-4
  7530. L19D6:  INC     HL              ; increment to address the length low byte.
  7531.         LD      C,(HL)          ; transfer to C and
  7532.         INC     HL              ; point to high byte of length.
  7533.         LD      B,(HL)          ; transfer that to B
  7534.         INC     HL              ; point to start of BASIC/variable contents.
  7535.  
  7536. ; the three types of numeric variables rejoin here
  7537.  
  7538. ;; NEXT-O-5
  7539. L19DB:  ADD     HL,BC           ; add the length to give address of next
  7540.                                 ; line/variable in HL.
  7541.         POP     DE              ; restore previous address to DE.
  7542.  
  7543. ; ------------------
  7544. ; Difference routine
  7545. ; ------------------
  7546. ; This routine terminates the above routine and is also called from the
  7547. ; start of the next routine to calculate the length to reclaim.
  7548.  
  7549. ;; DIFFER
  7550. L19DD:  AND     A               ; prepare for true subtraction.
  7551.         SBC     HL,DE           ; subtract the two pointers.
  7552.         LD      B,H             ; transfer result
  7553.         LD      C,L             ; to BC register pair.
  7554.         ADD     HL,DE           ; add back
  7555.         EX      DE,HL           ; and switch pointers
  7556.         RET                     ; return values are the length of area in BC,
  7557.                                 ; low pointer (previous) in HL,
  7558.                                 ; high pointer (next) in DE.
  7559.  
  7560. ; -----------------------
  7561. ; Handle reclaiming space
  7562. ; -----------------------
  7563. ;
  7564.  
  7565. ;; RECLAIM-1
  7566. L19E5:  CALL    L19DD           ; routine DIFFER immediately above
  7567.  
  7568. ;; RECLAIM-2
  7569. L19E8:  PUSH    BC              ;
  7570.  
  7571.         LD      A,B             ;
  7572.         CPL                     ;
  7573.         LD      B,A             ;
  7574.         LD      A,C             ;
  7575.         CPL                     ;
  7576.         LD      C,A             ;
  7577.         INC     BC              ;
  7578.  
  7579.         CALL    L1664           ; routine POINTERS
  7580.         EX      DE,HL           ;
  7581.         POP     HL              ;
  7582.  
  7583.         ADD     HL,DE           ;
  7584.         PUSH    DE              ;
  7585.         LDIR                    ; copy bytes
  7586.  
  7587.         POP     HL              ;
  7588.         RET                     ;
  7589.  
  7590. ; ----------------------------------------
  7591. ; Read line number of line in editing area
  7592. ; ----------------------------------------
  7593. ; This routine reads a line number in the editing area returning the number
  7594. ; in the BC register or zero if no digits exist before commands.
  7595. ; It is called from LINE-SCAN to check the syntax of the digits.
  7596. ; It is called from MAIN-3 to extract the line number in preparation for
  7597. ; inclusion of the line in the BASIC program area.
  7598. ;
  7599. ; Interestingly the calculator stack is moved from its normal place at the
  7600. ; end of dynamic memory to an adequate area within the system variables area.
  7601. ; This ensures that in a low memory situation, that valid line numbers can
  7602. ; be extracted without raising an error and that memory can be reclaimed
  7603. ; by deleting lines. If the stack was in its normal place then a situation
  7604. ; arises whereby the Spectrum becomes locked with no means of reclaiming space.
  7605.  
  7606. ;; E-LINE-NO
  7607. L19FB:  LD      HL,($5C59)      ; load HL from system variable E_LINE.
  7608.  
  7609.         DEC     HL              ; decrease so that NEXT_CHAR can be used
  7610.                                 ; without skipping the first digit.
  7611.  
  7612.         LD      ($5C5D),HL      ; store in the system variable CH_ADD.
  7613.  
  7614.         RST     20H             ; NEXT-CHAR skips any noise and white-space
  7615.                                 ; to point exactly at the first digit.
  7616.  
  7617.         LD      HL,$5C92        ; use MEM-0 as a temporary calculator stack
  7618.                                 ; an overhead of three locations are needed.
  7619.         LD      ($5C65),HL      ; set new STKEND.
  7620.  
  7621.         CALL    L2D3B           ; routine INT-TO-FP will read digits till
  7622.                                 ; a non-digit found.
  7623.         CALL    L2DA2           ; routine FP-TO-BC will retrieve number
  7624.                                 ; from stack at membot.
  7625.         JR      C,L1A15         ; forward to E-L-1 if overflow i.e. > 65535.
  7626.                                 ; 'Nonsense in BASIC'
  7627.  
  7628.         LD      HL,$D8F0        ; load HL with value -9999
  7629.         ADD     HL,BC           ; add to line number in BC
  7630.  
  7631. ;; E-L-1
  7632. L1A15:  JP      C,L1C8A         ; to REPORT-C 'Nonsense in BASIC' if over.
  7633.                                 ; Note. As ERR_SP points to ED_ERROR
  7634.                                 ; the report is never produced although
  7635.                                 ; the RST $08 will update X_PTR leading to
  7636.                                 ; the error marker being displayed when
  7637.                                 ; the ED_LOOP is reiterated.
  7638.                                 ; in fact, since it is immediately
  7639.                                 ; cancelled, any report will do.
  7640.  
  7641. ; a line in the range 0 - 9999 has been entered.
  7642.  
  7643.         JP      L16C5           ; jump back to SET-STK to set the calculator
  7644.                                 ; stack back to its normal place and exit
  7645.                                 ; from there.
  7646.  
  7647. ; ---------------------------------
  7648. ; Report and line number outputting
  7649. ; ---------------------------------
  7650. ; Entry point OUT-NUM-1 is used by the Error Reporting code to print
  7651. ; the line number and later the statement number held in BC.
  7652. ; If the statement was part of a direct command then -2 is used as a
  7653. ; dummy line number so that zero will be printed in the report.
  7654. ; This routine is also used to print the exponent of E-format numbers.
  7655. ;
  7656. ; Entry point OUT-NUM-2 is used from OUT-LINE to output the line number
  7657. ; addressed by HL with leading spaces if necessary.
  7658.  
  7659. ;; OUT-NUM-1
  7660. L1A1B:  PUSH    DE              ; save the
  7661.         PUSH    HL              ; registers.
  7662.         XOR     A               ; set A to zero.
  7663.         BIT     7,B             ; is the line number minus two ?
  7664.         JR      NZ,L1A42        ; forward to OUT-NUM-4 if so to print zero
  7665.                                 ; for a direct command.
  7666.  
  7667.         LD      H,B             ; transfer the
  7668.         LD      L,C             ; number to HL.
  7669.         LD      E,$FF           ; signal 'no leading zeros'.
  7670.         JR      L1A30           ; forward to continue at OUT-NUM-3
  7671.  
  7672. ; ---
  7673.  
  7674. ; from OUT-LINE - HL addresses line number.
  7675.  
  7676. ;; OUT-NUM-2
  7677. L1A28:  PUSH    DE              ; save flags
  7678.         LD      D,(HL)          ; high byte to D
  7679.         INC     HL              ; address next
  7680.         LD      E,(HL)          ; low byte to E
  7681.         PUSH    HL              ; save pointer
  7682.         EX      DE,HL           ; transfer number to HL
  7683.         LD      E,$20           ; signal 'output leading spaces'
  7684.  
  7685. ;; OUT-NUM-3
  7686. L1A30:  LD      BC,$FC18        ; value -1000
  7687.         CALL    L192A           ; routine OUT-SP-NO outputs space or number
  7688.         LD      BC,$FF9C        ; value -100
  7689.         CALL    L192A           ; routine OUT-SP-NO
  7690.         LD      C,$F6           ; value -10 ( B is still $FF )
  7691.         CALL    L192A           ; routine OUT-SP-NO
  7692.         LD      A,L             ; remainder to A.
  7693.  
  7694. ;; OUT-NUM-4
  7695. L1A42:  CALL    L15EF           ; routine OUT-CODE for final digit.
  7696.                                 ; else report code zero wouldn't get
  7697.                                 ; printed.
  7698.         POP     HL              ; restore the
  7699.         POP     DE              ; registers and
  7700.         RET                     ; return.
  7701.  
  7702.  
  7703. ;***************************************************
  7704. ;** Part 7. BASIC LINE AND COMMAND INTERPRETATION **
  7705. ;***************************************************
  7706.  
  7707. ; ----------------
  7708. ; The offset table
  7709. ; ----------------
  7710. ; The BASIC interpreter has found a command code $CE - $FF
  7711. ; which is then reduced to range $00 - $31 and added to the base address
  7712. ; of this table to give the address of an offset which, when added to
  7713. ; the offset therein, gives the location in the following parameter table
  7714. ; where a list of class codes, separators and addresses relevant to the
  7715. ; command exists.
  7716.  
  7717. ;; offst-tbl
  7718. L1A48:  DB    L1AF9 - $       ; B1 offset to Address: P-DEF-FN
  7719.         DB    L1B14 - $       ; CB offset to Address: P-CAT
  7720.         DB    L1B06 - $       ; BC offset to Address: P-FORMAT
  7721.         DB    L1B0A - $       ; BF offset to Address: P-MOVE
  7722.         DB    L1B10 - $       ; C4 offset to Address: P-ERASE
  7723.         DB    L1AFC - $       ; AF offset to Address: P-OPEN
  7724.         DB    L1B02 - $       ; B4 offset to Address: P-CLOSE
  7725.         DB    L1AE2 - $       ; 93 offset to Address: P-MERGE
  7726.         DB    L1AE1 - $       ; 91 offset to Address: P-VERIFY
  7727.         DB    L1AE3 - $       ; 92 offset to Address: P-BEEP
  7728.         DB    L1AE7 - $       ; 95 offset to Address: P-CIRCLE
  7729.         DB    L1AEB - $       ; 98 offset to Address: P-INK
  7730.         DB    L1AEC - $       ; 98 offset to Address: P-PAPER
  7731.         DB    L1AED - $       ; 98 offset to Address: P-FLASH
  7732.         DB    L1AEE - $       ; 98 offset to Address: P-BRIGHT
  7733.         DB    L1AEF - $       ; 98 offset to Address: P-INVERSE
  7734.         DB    L1AF0 - $       ; 98 offset to Address: P-OVER
  7735.         DB    L1AF1 - $       ; 98 offset to Address: P-OUT
  7736.         DB    L1AD9 - $       ; 7F offset to Address: P-LPRINT
  7737.         DB    L1ADC - $       ; 81 offset to Address: P-LLIST
  7738.         DB    L1A8A - $       ; 2E offset to Address: P-STOP
  7739.         DB    L1AC9 - $       ; 6C offset to Address: P-READ
  7740.         DB    L1ACC - $       ; 6E offset to Address: P-DATA
  7741.         DB    L1ACF - $       ; 70 offset to Address: P-RESTORE
  7742.         DB    L1AA8 - $       ; 48 offset to Address: P-NEW
  7743.         DB    L1AF5 - $       ; 94 offset to Address: P-BORDER
  7744.         DB    L1AB8 - $       ; 56 offset to Address: P-CONT
  7745.         DB    L1AA2 - $       ; 3F offset to Address: P-DIM
  7746.         DB    L1AA5 - $       ; 41 offset to Address: P-REM
  7747.         DB    L1A90 - $       ; 2B offset to Address: P-FOR
  7748.         DB    L1A7D - $       ; 17 offset to Address: P-GO-TO
  7749.         DB    L1A86 - $       ; 1F offset to Address: P-GO-SUB
  7750.         DB    L1A9F - $       ; 37 offset to Address: P-INPUT
  7751.         DB    L1AE0 - $       ; 77 offset to Address: P-LOAD
  7752.         DB    L1AAE - $       ; 44 offset to Address: P-LIST
  7753.         DB    L1A7A - $       ; 0F offset to Address: P-LET
  7754.         DB    L1AC5 - $       ; 59 offset to Address: P-PAUSE
  7755.         DB    L1A98 - $       ; 2B offset to Address: P-NEXT
  7756.         DB    L1AB1 - $       ; 43 offset to Address: P-POKE
  7757.         DB    L1A9C - $       ; 2D offset to Address: P-PRINT
  7758.         DB    L1AC1 - $       ; 51 offset to Address: P-PLOT
  7759.         DB    L1AAB - $       ; 3A offset to Address: P-RUN
  7760.         DB    L1ADF - $       ; 6D offset to Address: P-SAVE
  7761.         DB    L1AB5 - $       ; 42 offset to Address: P-RANDOM
  7762.         DB    L1A81 - $       ; 0D offset to Address: P-IF
  7763.         DB    L1ABE - $       ; 49 offset to Address: P-CLS
  7764.         DB    L1AD2 - $       ; 5C offset to Address: P-DRAW
  7765.         DB    L1ABB - $       ; 44 offset to Address: P-CLEAR
  7766.         DB    L1A8D - $       ; 15 offset to Address: P-RETURN
  7767.         DB    L1AD6 - $       ; 5D offset to Address: P-COPY
  7768.  
  7769.  
  7770. ; -------------------------------
  7771. ; The parameter or "Syntax" table
  7772. ; -------------------------------
  7773. ; For each command there exists a variable list of parameters.
  7774. ; If the character is greater than a space it is a required separator.
  7775. ; If less, then it is a command class in the range 00 - 0B.
  7776. ; Note that classes 00, 03 and 05 will fetch the addresses from this table.
  7777. ; Some classes e.g. 07 and 0B have the same address in all invocations
  7778. ; and the command is re-computed from the low-byte of the parameter address.
  7779. ; Some e.g. 02 are only called once so a call to the command is made from
  7780. ; within the class routine rather than holding the address within the table.
  7781. ; Some class routines check syntax entirely and some leave this task for the
  7782. ; command itself.
  7783. ; Others for example CIRCLE (x,y,z) check the first part (x,y) using the
  7784. ; class routine and the final part (,z) within the command.
  7785. ; The last few commands appear to have been added in a rush but their syntax
  7786. ; is rather simple e.g. MOVE "M1","M2"
  7787.  
  7788. ;; P-LET
  7789. L1A7A:  DB    $01             ; Class-01 - A variable is required.
  7790.         DB    $3D             ; Separator:  '='
  7791.         DB    $02             ; Class-02 - An expression, numeric or string,
  7792.                                 ; must follow.
  7793.  
  7794. ;; P-GO-TO
  7795. L1A7D:  DB    $06             ; Class-06 - A numeric expression must follow.
  7796.         DB    $00             ; Class-00 - No further operands.
  7797.         DEFW    L1E67           ; Address: $1E67; Address: GO-TO
  7798.  
  7799. ;; P-IF
  7800. L1A81:  DB    $06             ; Class-06 - A numeric expression must follow.
  7801.         DB    $CB             ; Separator:  'THEN'
  7802.         DB    $05             ; Class-05 - Variable syntax checked
  7803.                                 ; by routine.
  7804.         DEFW    L1CF0           ; Address: $1CF0; Address: IF
  7805.  
  7806. ;; P-GO-SUB
  7807. L1A86:  DB    $06             ; Class-06 - A numeric expression must follow.
  7808.         DB    $00             ; Class-00 - No further operands.
  7809.         DEFW    L1EED           ; Address: $1EED; Address: GO-SUB
  7810.  
  7811. ;; P-STOP
  7812. L1A8A:  DB    $00             ; Class-00 - No further operands.
  7813.         DEFW    L1CEE           ; Address: $1CEE; Address: STOP
  7814.  
  7815. ;; P-RETURN
  7816. L1A8D:  DB    $00             ; Class-00 - No further operands.
  7817.         DEFW    L1F23           ; Address: $1F23; Address: RETURN
  7818.  
  7819. ;; P-FOR
  7820. L1A90:  DB    $04             ; Class-04 - A single character variable must
  7821.                                 ; follow.
  7822.         DB    $3D             ; Separator:  '='
  7823.         DB    $06             ; Class-06 - A numeric expression must follow.
  7824.         DB    $CC             ; Separator:  'TO'
  7825.         DB    $06             ; Class-06 - A numeric expression must follow.
  7826.         DB    $05             ; Class-05 - Variable syntax checked
  7827.                                 ; by routine.
  7828.         DEFW    L1D03           ; Address: $1D03; Address: FOR
  7829.  
  7830. ;; P-NEXT
  7831. L1A98:  DB    $04             ; Class-04 - A single character variable must
  7832.                                 ; follow.
  7833.         DB    $00             ; Class-00 - No further operands.
  7834.         DEFW    L1DAB           ; Address: $1DAB; Address: NEXT
  7835.  
  7836. ;; P-PRINT
  7837. L1A9C:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7838.                                 ; by routine.
  7839.         DEFW    L1FCD           ; Address: $1FCD; Address: PRINT
  7840.  
  7841. ;; P-INPUT
  7842. L1A9F:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7843.                                 ; by routine.
  7844.         DEFW    L2089           ; Address: $2089; Address: INPUT
  7845.  
  7846. ;; P-DIM
  7847. L1AA2:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7848.                                 ; by routine.
  7849.         DEFW    L2C02           ; Address: $2C02; Address: DIM
  7850.  
  7851. ;; P-REM
  7852. L1AA5:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7853.                                 ; by routine.
  7854.         DEFW    L1BB2           ; Address: $1BB2; Address: REM
  7855.  
  7856. ;; P-NEW
  7857. L1AA8:  DB    $00             ; Class-00 - No further operands.
  7858.         DEFW    L11B7           ; Address: $11B7; Address: NEW
  7859.  
  7860. ;; P-RUN
  7861. L1AAB:  DB    $03             ; Class-03 - A numeric expression may follow
  7862.                                 ; else default to zero.
  7863.         DEFW    L1EA1           ; Address: $1EA1; Address: RUN
  7864.  
  7865. ;; P-LIST
  7866. L1AAE:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7867.                                 ; by routine.
  7868.         DEFW    L17F9           ; Address: $17F9; Address: LIST
  7869.  
  7870. ;; P-POKE
  7871. L1AB1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7872.                                 ; expressions required.
  7873.         DB    $00             ; Class-00 - No further operands.
  7874.         DEFW    L1E80           ; Address: $1E80; Address: POKE
  7875.  
  7876. ;; P-RANDOM
  7877. L1AB5:  DB    $03             ; Class-03 - A numeric expression may follow
  7878.                                 ; else default to zero.
  7879.         DEFW    L1E4F           ; Address: $1E4F; Address: RANDOMIZE
  7880.  
  7881. ;; P-CONT
  7882. L1AB8:  DB    $00             ; Class-00 - No further operands.
  7883.         DEFW    L1E5F           ; Address: $1E5F; Address: CONTINUE
  7884.  
  7885. ;; P-CLEAR
  7886. L1ABB:  DB    $03             ; Class-03 - A numeric expression may follow
  7887.                                 ; else default to zero.
  7888.         DEFW    L1EAC           ; Address: $1EAC; Address: CLEAR
  7889.  
  7890. ;; P-CLS
  7891. L1ABE:  DB    $00             ; Class-00 - No further operands.
  7892.         DEFW    L0D6B           ; Address: $0D6B; Address: CLS
  7893.  
  7894. ;; P-PLOT
  7895. L1AC1:  DB    $09             ; Class-09 - Two comma-separated numeric
  7896.                                 ; expressions required with optional colour
  7897.                                 ; items.
  7898.         DB    $00             ; Class-00 - No further operands.
  7899.         DEFW    L22DC           ; Address: $22DC; Address: PLOT
  7900.  
  7901. ;; P-PAUSE
  7902. L1AC5:  DB    $06             ; Class-06 - A numeric expression must follow.
  7903.         DB    $00             ; Class-00 - No further operands.
  7904.         DEFW    L1F3A           ; Address: $1F3A; Address: PAUSE
  7905.  
  7906. ;; P-READ
  7907. L1AC9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7908.                                 ; by routine.
  7909.         DEFW    L1DED           ; Address: $1DED; Address: READ
  7910.  
  7911. ;; P-DATA
  7912. L1ACC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7913.                                 ; by routine.
  7914.         DEFW    L1E27           ; Address: $1E27; Address: DATA
  7915.  
  7916. ;; P-RESTORE
  7917. L1ACF:  DB    $03             ; Class-03 - A numeric expression may follow
  7918.                                 ; else default to zero.
  7919.         DEFW    L1E42           ; Address: $1E42; Address: RESTORE
  7920.  
  7921. ;; P-DRAW
  7922. L1AD2:  DB    $09             ; Class-09 - Two comma-separated numeric
  7923.                                 ; expressions required with optional colour
  7924.                                 ; items.
  7925.         DB    $05             ; Class-05 - Variable syntax checked
  7926.                                 ; by routine.
  7927.         DEFW    L2382           ; Address: $2382; Address: DRAW
  7928.  
  7929. ;; P-COPY
  7930. L1AD6:  DB    $00             ; Class-00 - No further operands.
  7931.         DEFW    L0EAC           ; Address: $0EAC; Address: COPY
  7932.  
  7933. ;; P-LPRINT
  7934. L1AD9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7935.                                 ; by routine.
  7936.         DEFW    L1FC9           ; Address: $1FC9; Address: LPRINT
  7937.  
  7938. ;; P-LLIST
  7939. L1ADC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7940.                                 ; by routine.
  7941.         DEFW    L17F5           ; Address: $17F5; Address: LLIST
  7942.  
  7943. ;; P-SAVE
  7944. L1ADF:  DB    $0B             ; Class-0B - Offset address converted to tape
  7945.                                 ; command.
  7946.  
  7947. ;; P-LOAD
  7948. L1AE0:  DB    $0B             ; Class-0B - Offset address converted to tape
  7949.                                 ; command.
  7950.  
  7951. ;; P-VERIFY
  7952. L1AE1:  DB    $0B             ; Class-0B - Offset address converted to tape
  7953.                                 ; command.
  7954.  
  7955. ;; P-MERGE
  7956. L1AE2:  DB    $0B             ; Class-0B - Offset address converted to tape
  7957.                                 ; command.
  7958.  
  7959. ;; P-BEEP
  7960. L1AE3:  DB    $08             ; Class-08 - Two comma-separated numeric
  7961.                                 ; expressions required.
  7962.         DB    $00             ; Class-00 - No further operands.
  7963.         DEFW    L03F8           ; Address: $03F8; Address: BEEP
  7964.  
  7965. ;; P-CIRCLE
  7966. L1AE7:  DB    $09             ; Class-09 - Two comma-separated numeric
  7967.                                 ; expressions required with optional colour
  7968.                                 ; items.
  7969.         DB    $05             ; Class-05 - Variable syntax checked
  7970.                                 ; by routine.
  7971.         DEFW    L2320           ; Address: $2320; Address: CIRCLE
  7972.  
  7973. ;; P-INK
  7974. L1AEB:  DB    $07             ; Class-07 - Offset address is converted to
  7975.                                 ; colour code.
  7976.  
  7977. ;; P-PAPER
  7978. L1AEC:  DB    $07             ; Class-07 - Offset address is converted to
  7979.                                 ; colour code.
  7980.  
  7981. ;; P-FLASH
  7982. L1AED:  DB    $07             ; Class-07 - Offset address is converted to
  7983.                                 ; colour code.
  7984.  
  7985. ;; P-BRIGHT
  7986. L1AEE:  DB    $07             ; Class-07 - Offset address is converted to
  7987.                                 ; colour code.
  7988.  
  7989. ;; P-INVERSE
  7990. L1AEF:  DB    $07             ; Class-07 - Offset address is converted to
  7991.                                 ; colour code.
  7992.  
  7993. ;; P-OVER
  7994. L1AF0:  DB    $07             ; Class-07 - Offset address is converted to
  7995.                                 ; colour code.
  7996.  
  7997. ;; P-OUT
  7998. L1AF1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7999.                                 ; expressions required.
  8000.         DB    $00             ; Class-00 - No further operands.
  8001.         DEFW    L1E7A           ; Address: $1E7A; Address: OUT
  8002.  
  8003. ;; P-BORDER
  8004. L1AF5:  DB    $06             ; Class-06 - A numeric expression must follow.
  8005.         DB    $00             ; Class-00 - No further operands.
  8006.         DEFW    L2294           ; Address: $2294; Address: BORDER
  8007.  
  8008. ;; P-DEF-FN
  8009. L1AF9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  8010.                                 ; by routine.
  8011.         DEFW    L1F60           ; Address: $1F60; Address: DEF-FN
  8012.  
  8013. ;; P-OPEN
  8014. L1AFC:  DB    $06             ; Class-06 - A numeric expression must follow.
  8015.         DB    $2C             ; Separator:  ','          see Footnote *
  8016.         DB    $0A             ; Class-0A - A string expression must follow.
  8017.         DB    $00             ; Class-00 - No further operands.
  8018.         DEFW    L1736           ; Address: $1736; Address: OPEN
  8019.  
  8020. ;; P-CLOSE
  8021. L1B02:  DB    $06             ; Class-06 - A numeric expression must follow.
  8022.         DB    $00             ; Class-00 - No further operands.
  8023.         DEFW    L16E5           ; Address: $16E5; Address: CLOSE
  8024.  
  8025. ;; P-FORMAT
  8026. L1B06:  DB    $0A             ; Class-0A - A string expression must follow.
  8027.         DB    $00             ; Class-00 - No further operands.
  8028.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8029.  
  8030. ;; P-MOVE
  8031. L1B0A:  DB    $0A             ; Class-0A - A string expression must follow.
  8032.         DB    $2C             ; Separator:  ','
  8033.         DB    $0A             ; Class-0A - A string expression must follow.
  8034.         DB    $00             ; Class-00 - No further operands.
  8035.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8036.  
  8037. ;; P-ERASE
  8038. L1B10:  DB    $0A             ; Class-0A - A string expression must follow.
  8039.         DB    $00             ; Class-00 - No further operands.
  8040.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8041.  
  8042. ;; P-CAT
  8043. L1B14:  DB    $00             ; Class-00 - No further operands.
  8044.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8045.  
  8046. ; * Note that a comma is required as a separator with the OPEN command
  8047. ; but the Interface 1 programmers relaxed this allowing ';' as an
  8048. ; alternative for their channels creating a confusing mixture of
  8049. ; allowable syntax as it is this ROM which opens or re-opens the
  8050. ; normal channels.
  8051.  
  8052. ; -------------------------------
  8053. ; Main parser (BASIC interpreter)
  8054. ; -------------------------------
  8055. ; This routine is called once from MAIN-2 when the BASIC line is to
  8056. ; be entered or re-entered into the Program area and the syntax
  8057. ; requires checking.
  8058.  
  8059. ;; LINE-SCAN
  8060. L1B17:  RES     7,(IY+$01)      ; update FLAGS - signal checking syntax
  8061.         CALL    L19FB           ; routine E-LINE-NO              >>
  8062.                                 ; fetches the line number if in range.
  8063.  
  8064.         XOR     A               ; clear the accumulator.
  8065.         LD      ($5C47),A       ; set statement number SUBPPC to zero.
  8066.         DEC     A               ; set accumulator to $FF.
  8067.         LD      ($5C3A),A       ; set ERR_NR to 'OK' - 1.
  8068.         JR      L1B29           ; forward to continue at STMT-L-1.
  8069.  
  8070. ; --------------
  8071. ; Statement loop
  8072. ; --------------
  8073. ;
  8074. ;
  8075.  
  8076. ;; STMT-LOOP
  8077. L1B28:  RST     20H             ; NEXT-CHAR
  8078.  
  8079. ; -> the entry point from above or LINE-RUN
  8080. ;; STMT-L-1
  8081. L1B29:  CALL    L16BF           ; routine SET-WORK clears workspace etc.
  8082.  
  8083.         INC     (IY+$0D)        ; increment statement number SUBPPC
  8084.         JP      M,L1C8A         ; to REPORT-C to raise
  8085.                                 ; 'Nonsense in BASIC' if over 127.
  8086.  
  8087.         RST     18H             ; GET-CHAR
  8088.  
  8089.         LD      B,$00           ; set B to zero for later indexing.
  8090.                                 ; early so any other reason ???
  8091.  
  8092.         CP      $0D             ; is character carriage return ?
  8093.                                 ; i.e. an empty statement.
  8094.         JR      Z,L1BB3         ; forward to LINE-END if so.
  8095.  
  8096.         CP      $3A             ; is it statement end marker ':' ?
  8097.                                 ; i.e. another type of empty statement.
  8098.         JR      Z,L1B28         ; back to STMT-LOOP if so.
  8099.  
  8100.         LD      HL,L1B76        ; address: STMT-RET
  8101.         PUSH    HL              ; is now pushed as a return address
  8102.         LD      C,A             ; transfer the current character to C.
  8103.  
  8104. ; advance CH_ADD to a position after command and test if it is a command.
  8105.  
  8106.         RST     20H             ; NEXT-CHAR to advance pointer
  8107.         LD      A,C             ; restore current character
  8108.         SUB     $CE             ; subtract 'DEF FN' - first command
  8109.         JP      C,L1C8A         ; jump to REPORT-C if less than a command
  8110.                                 ; raising
  8111.                                 ; 'Nonsense in BASIC'
  8112.  
  8113.         LD      C,A             ; put the valid command code back in C.
  8114.                                 ; register B is zero.
  8115.         LD      HL,L1A48        ; address: offst-tbl
  8116.         ADD     HL,BC           ; index into table with one of 50 commands.
  8117.         LD      C,(HL)          ; pick up displacement to syntax table entry.
  8118.         ADD     HL,BC           ; add to address the relevant entry.
  8119.         JR      L1B55           ; forward to continue at GET-PARAM
  8120.  
  8121. ; ----------------------
  8122. ; The main scanning loop
  8123. ; ----------------------
  8124. ; not documented properly
  8125. ;
  8126.  
  8127. ;; SCAN-LOOP
  8128. L1B52:  LD      HL,($5C74)      ; fetch temporary address from T_ADDR
  8129.                                 ; during subsequent loops.
  8130.  
  8131. ; -> the initial entry point with HL addressing start of syntax table entry.
  8132.  
  8133. ;; GET-PARAM
  8134. L1B55:  LD      A,(HL)          ; pick up the parameter.
  8135.         INC     HL              ; address next one.
  8136.         LD      ($5C74),HL      ; save pointer in system variable T_ADDR
  8137.  
  8138.         LD      BC,L1B52        ; address: SCAN-LOOP
  8139.         PUSH    BC              ; is now pushed on stack as looping address.
  8140.         LD      C,A             ; store parameter in C.
  8141.         CP      $20             ; is it greater than ' '  ?
  8142.         JR      NC,L1B6F        ; forward to SEPARATOR to check that correct
  8143.                                 ; separator appears in statement if so.
  8144.  
  8145.         LD      HL,L1C01        ; address: class-tbl.
  8146.         LD      B,$00           ; prepare to index into the class table.
  8147.         ADD     HL,BC           ; index to find displacement to routine.
  8148.         LD      C,(HL)          ; displacement to BC
  8149.         ADD     HL,BC           ; add to address the CLASS routine.
  8150.         PUSH    HL              ; push the address on the stack.
  8151.  
  8152.         RST     18H             ; GET-CHAR - HL points to place in statement.
  8153.  
  8154.         DEC     B               ; reset the zero flag - the initial state
  8155.                                 ; for all class routines.
  8156.  
  8157.         RET                     ; and make an indirect jump to routine
  8158.                                 ; and then SCAN-LOOP (also on stack).
  8159.  
  8160. ; Note. one of the class routines will eventually drop the return address
  8161. ; off the stack breaking out of the above seemingly endless loop.
  8162.  
  8163. ; ----------------
  8164. ; Verify separator
  8165. ; ----------------
  8166. ; This routine is called once to verify that the mandatory separator
  8167. ; present in the parameter table is also present in the correct
  8168. ; location following the command. For example, the 'THEN' token after
  8169. ; the 'IF' token and expression.
  8170.  
  8171. ;; SEPARATOR
  8172. L1B6F:  RST     18H             ; GET-CHAR
  8173.         CP      C               ; does it match the character in C ?
  8174.         JP      NZ,L1C8A        ; jump forward to REPORT-C if not
  8175.                                 ; 'Nonsense in BASIC'.
  8176.  
  8177.         RST     20H             ; NEXT-CHAR advance to next character
  8178.         RET                     ; return.
  8179.  
  8180. ; ------------------------------
  8181. ; Come here after interpretation
  8182. ; ------------------------------
  8183. ;
  8184. ;
  8185.  
  8186. ;; STMT-RET
  8187. L1B76:  CALL    L1F54           ; routine BREAK-KEY is tested after every
  8188.                                 ; statement.
  8189.         JR      C,L1B7D         ; step forward to STMT-R-1 if not pressed.
  8190.  
  8191. ;; REPORT-L
  8192. L1B7B:  RST     08H             ; ERROR-1
  8193.         DB    $14             ; Error Report: BREAK into program
  8194.  
  8195. ;; STMT-R-1
  8196. L1B7D           IF BAS48_ONLY
  8197.                 BIT 7,(IY+0X0A)
  8198.                 ELSE
  8199.                 CALL L3B4D              ; Spectrum 128 patch
  8200.                 NOP
  8201.                 ENDIF
  8202.  
  8203. L1B81:  JR      NZ,L1BF4        ; forward to STMT-NEXT if a program line.
  8204.  
  8205.         LD      HL,($5C42)      ; fetch line number from NEWPPC
  8206.         BIT     7,H             ; will be set if minus two - direct command(s)
  8207.         JR      Z,L1B9E         ; forward to LINE-NEW if a jump is to be
  8208.                                 ; made to a new program line/statement.
  8209.  
  8210. ; --------------------
  8211. ; Run a direct command
  8212. ; --------------------
  8213. ; A direct command is to be run or, if continuing from above,
  8214. ; the next statement of a direct command is to be considered.
  8215.  
  8216. ;; LINE-RUN
  8217. L1B8A:  LD      HL,$FFFE        ; The dummy value minus two
  8218.         LD      ($5C45),HL      ; is set/reset as line number in PPC.
  8219.         LD      HL,($5C61)      ; point to end of line + 1 - WORKSP.
  8220.         DEC     HL              ; now point to $80 end-marker.
  8221.         LD      DE,($5C59)      ; address the start of line E_LINE.
  8222.         DEC     DE              ; now location before - for GET-CHAR.
  8223.         LD      A,($5C44)       ; load statement to A from NSPPC.
  8224.         JR      L1BD1           ; forward to NEXT-LINE.
  8225.  
  8226. ; ------------------------------
  8227. ; Find start address of new line
  8228. ; ------------------------------
  8229. ; The branch was to here if a jump is to made to a new line number
  8230. ; and statement.
  8231. ; That is the previous statement was a GO TO, GO SUB, RUN, RETURN, NEXT etc..
  8232.  
  8233. ;; LINE-NEW
  8234. L1B9E:  CALL    L196E           ; routine LINE-ADDR gets address of line
  8235.                                 ; returning zero flag set if line found.
  8236.         LD      A,($5C44)       ; fetch new statement from NSPPC
  8237.         JR      Z,L1BBF         ; forward to LINE-USE if line matched.
  8238.  
  8239. ; continue as must be a direct command.
  8240.  
  8241.         AND     A               ; test statement which should be zero
  8242.         JR      NZ,L1BEC        ; forward to REPORT-N if not.
  8243.                                 ; 'Statement lost'
  8244.  
  8245. ;
  8246.  
  8247.         LD      B,A             ; save statement in B. ?
  8248.         LD      A,(HL)          ; fetch high byte of line number.
  8249.         AND     $C0             ; test if using direct command
  8250.                                 ; a program line is less than $3F
  8251.         LD      A,B             ; retrieve statement.
  8252.                                 ; (we can assume it is zero).
  8253.         JR      Z,L1BBF         ; forward to LINE-USE if was a program line
  8254.  
  8255. ; Alternatively a direct statement has finished correctly.
  8256.  
  8257. ;; REPORT-0
  8258. L1BB0:  RST     08H             ; ERROR-1
  8259.         DB    $FF             ; Error Report: OK
  8260.  
  8261. ; ------------------
  8262. ; Handle REM command
  8263. ; ------------------
  8264. ; The REM command routine.
  8265. ; The return address STMT-RET is dropped and the rest of line ignored.
  8266.  
  8267. ;; REM
  8268. L1BB2:  POP     BC              ; drop return address STMT-RET and
  8269.                                 ; continue ignoring rest of line.
  8270.  
  8271. ; ------------
  8272. ; End of line?
  8273. ; ------------
  8274. ;
  8275. ;
  8276.  
  8277. ;; LINE-END
  8278. L1BB3:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8279.         RET     Z               ; return if checking syntax.
  8280.  
  8281.         LD      HL,($5C55)      ; fetch NXTLIN to HL.
  8282.         LD      A,$C0           ; test against the
  8283.         AND     (HL)            ; system limit $3F.
  8284.         RET     NZ              ; return if more as must be
  8285.                                 ; end of program.
  8286.                                 ; (or direct command)
  8287.  
  8288.         XOR     A               ; set statement to zero.
  8289.  
  8290. ; and continue to set up the next following line and then consider this new one.
  8291.  
  8292. ; ---------------------
  8293. ; General line checking
  8294. ; ---------------------
  8295. ; The branch was here from LINE-NEW if BASIC is branching.
  8296. ; or a continuation from above if dealing with a new sequential line.
  8297. ; First make statement zero number one leaving others unaffected.
  8298.  
  8299. ;; LINE-USE
  8300. L1BBF:  CP      $01             ; will set carry if zero.
  8301.         ADC     A,$00           ; add in any carry.
  8302.  
  8303.         LD      D,(HL)          ; high byte of line number to D.
  8304.         INC     HL              ; advance pointer.
  8305.         LD      E,(HL)          ; low byte of line number to E.
  8306.         LD      ($5C45),DE      ; set system variable PPC.
  8307.  
  8308.         INC     HL              ; advance pointer.
  8309.         LD      E,(HL)          ; low byte of line length to E.
  8310.         INC     HL              ; advance pointer.
  8311.         LD      D,(HL)          ; high byte of line length to D.
  8312.  
  8313.         EX      DE,HL           ; swap pointer to DE before
  8314.         ADD     HL,DE           ; adding to address the end of line.
  8315.         INC     HL              ; advance to start of next line.
  8316.  
  8317. ; -----------------------------
  8318. ; Update NEXT LINE but consider
  8319. ; previous line or edit line.
  8320. ; -----------------------------
  8321. ; The pointer will be the next line if continuing from above or to
  8322. ; edit line end-marker ($80) if from LINE-RUN.
  8323.  
  8324. ;; NEXT-LINE
  8325. L1BD1:  LD      ($5C55),HL      ; store pointer in system variable NXTLIN
  8326.  
  8327.         EX      DE,HL           ; bring back pointer to previous or edit line
  8328.         LD      ($5C5D),HL      ; and update CH_ADD with character address.
  8329.  
  8330.         LD      D,A             ; store statement in D.
  8331.         LD      E,$00           ; set E to zero to suppress token searching
  8332.                                 ; if EACH-STMT is to be called.
  8333.         LD      (IY+$0A),$FF    ; set statement NSPPC to $FF signalling
  8334.                                 ; no jump to be made.
  8335.         DEC     D               ; decrement and test statement
  8336.         LD      (IY+$0D),D      ; set SUBPPC to decremented statement number.
  8337.         JP      Z,L1B28         ; to STMT-LOOP if result zero as statement is
  8338.                                 ; at start of line and address is known.
  8339.  
  8340.         INC     D               ; else restore statement.
  8341.         CALL    L198B           ; routine EACH-STMT finds the D'th statement
  8342.                                 ; address as E does not contain a token.
  8343.         JR      Z,L1BF4         ; forward to STMT-NEXT if address found.
  8344.  
  8345. ;; REPORT-N
  8346. L1BEC:  RST     08H             ; ERROR-1
  8347.         DB    $16             ; Error Report: Statement lost
  8348.  
  8349. ; -----------------
  8350. ; End of statement?
  8351. ; -----------------
  8352. ; This combination of routines is called from 20 places when
  8353. ; the end of a statement should have been reached and all preceding
  8354. ; syntax is in order.
  8355.  
  8356. ;; CHECK-END
  8357. L1BEE:  CALL    L2530           ; routine SYNTAX-Z
  8358.         RET     NZ              ; return immediately in runtime
  8359.  
  8360.         POP     BC              ; drop address of calling routine.
  8361.         POP     BC              ; drop address STMT-RET.
  8362.                                 ; and continue to find next statement.
  8363.  
  8364. ; --------------------
  8365. ; Go to next statement
  8366. ; --------------------
  8367. ; Acceptable characters at this point are carriage return and ':'.
  8368. ; If so go to next statement which in the first case will be on next line.
  8369.  
  8370. ;; STMT-NEXT
  8371. L1BF4           IF BAS48_ONLY
  8372.                 RST 0X18
  8373.                 CP 0X0D
  8374.                 ELSE
  8375.                 CALL L3B5D              ; Spectrum 128 patch
  8376.                 ENDIF
  8377.  
  8378. L1BF7:  JR      Z,L1BB3         ; back to LINE-END if so.
  8379.  
  8380.         CP      $3A             ; is it ':' ?
  8381.         JP      Z,L1B28         ; jump back to STMT-LOOP to consider
  8382.                                 ; further statements
  8383.  
  8384.         JP      L1C8A           ; jump to REPORT-C with any other character
  8385.                                 ; 'Nonsense in BASIC'.
  8386.  
  8387. ; Note. the two-byte sequence 'rst 08; DB $0b' could replace the above jp.
  8388.  
  8389. ; -------------------
  8390. ; Command class table
  8391. ; -------------------
  8392. ;
  8393.  
  8394. ;; class-tbl
  8395. L1C01:  DB    L1C10 - $       ; 0F offset to Address: CLASS-00
  8396.         DB    L1C1F - $       ; 1D offset to Address: CLASS-01
  8397.         DB    L1C4E - $       ; 4B offset to Address: CLASS-02
  8398.         DB    L1C0D - $       ; 09 offset to Address: CLASS-03
  8399.         DB    L1C6C - $       ; 67 offset to Address: CLASS-04
  8400.         DB    L1C11 - $       ; 0B offset to Address: CLASS-05
  8401.         DB    L1C82 - $       ; 7B offset to Address: CLASS-06
  8402.         DB    L1C96 - $       ; 8E offset to Address: CLASS-07
  8403.         DB    L1C7A - $       ; 71 offset to Address: CLASS-08
  8404.         DB    L1CBE - $       ; B4 offset to Address: CLASS-09
  8405.         DB    L1C8C - $       ; 81 offset to Address: CLASS-0A
  8406.         DB    L1CDB - $       ; CF offset to Address: CLASS-0B
  8407.  
  8408.  
  8409. ; --------------------------------
  8410. ; Command classes---00, 03, and 05
  8411. ; --------------------------------
  8412. ; class-03 e.g RUN or RUN 200   ;  optional operand
  8413. ; class-00 e.g CONTINUE         ;  no operand
  8414. ; class-05 e.g PRINT            ;  variable syntax checked by routine
  8415.  
  8416. ;; CLASS-03
  8417. L1C0D:  CALL    L1CDE           ; routine FETCH-NUM
  8418.  
  8419. ;; CLASS-00
  8420.  
  8421. L1C10:  CP      A               ; reset zero flag.
  8422.  
  8423. ; if entering here then all class routines are entered with zero reset.
  8424.  
  8425. ;; CLASS-05
  8426. L1C11:  POP     BC              ; drop address SCAN-LOOP.
  8427.         CALL    Z,L1BEE         ; if zero set then call routine CHECK-END >>>
  8428.                                 ; as should be no further characters.
  8429.  
  8430.         EX      DE,HL           ; save HL to DE.
  8431.         LD      HL,($5C74)      ; fetch T_ADDR
  8432.         LD      C,(HL)          ; fetch low byte of routine
  8433.         INC     HL              ; address next.
  8434.         LD      B,(HL)          ; fetch high byte of routine.
  8435.         EX      DE,HL           ; restore HL from DE
  8436.         PUSH    BC              ; push the address
  8437.         RET                     ; and make an indirect jump to the command.
  8438.  
  8439. ; --------------------------------
  8440. ; Command classes---01, 02, and 04
  8441. ; --------------------------------
  8442. ; class-01  e.g LET A = 2*3     ; a variable is reqd
  8443.  
  8444. ; This class routine is also called from INPUT and READ to find the
  8445. ; destination variable for an assignment.
  8446.  
  8447. ;; CLASS-01
  8448. L1C1F:  CALL    L28B2           ; routine LOOK-VARS returns carry set if not
  8449.                                 ; found in runtime.
  8450.  
  8451. ; ----------------------
  8452. ; Variable in assignment
  8453. ; ----------------------
  8454. ;
  8455. ;
  8456.  
  8457. ;; VAR-A-1
  8458. L1C22:  LD      (IY+$37),$00    ; set FLAGX to zero
  8459.         JR      NC,L1C30        ; forward to VAR-A-2 if found or checking
  8460.                                 ; syntax.
  8461.  
  8462.         SET     1,(IY+$37)      ; FLAGX  - Signal a new variable
  8463.         JR      NZ,L1C46        ; to VAR-A-3 if not assigning to an array
  8464.                                 ; e.g. LET a$(3,3) = "X"
  8465.  
  8466. ;; REPORT-2
  8467. L1C2E:  RST     08H             ; ERROR-1
  8468.         DB    $01             ; Error Report: Variable not found
  8469.  
  8470. ;; VAR-A-2
  8471. L1C30:  CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  8472.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8473.         JR      NZ,L1C46        ; to VAR-A-3 if numeric
  8474.  
  8475.         XOR     A               ; default to array/slice - to be retained.
  8476.         CALL    L2530           ; routine SYNTAX-Z
  8477.         CALL    NZ,L2BF1        ; routine STK-FETCH is called in runtime
  8478.                                 ; may overwrite A with 1.
  8479.         LD      HL,$5C71        ; address system variable FLAGX
  8480.         OR      (HL)            ; set bit 0 if simple variable to be reclaimed
  8481.         LD      (HL),A          ; update FLAGX
  8482.         EX      DE,HL           ; start of string/subscript to DE
  8483.  
  8484. ;; VAR-A-3
  8485. L1C46:  LD      ($5C72),BC      ; update STRLEN
  8486.         LD      ($5C4D),HL      ; and DEST of assigned string.
  8487.         RET                     ; return.
  8488.  
  8489. ; -------------------------------------------------
  8490. ; class-02 e.g. LET a = 1 + 1   ; an expression must follow
  8491.  
  8492. ;; CLASS-02
  8493. L1C4E:  POP     BC              ; drop return address SCAN-LOOP
  8494.         CALL    L1C56           ; routine VAL-FET-1 is called to check
  8495.                                 ; expression and assign result in runtime
  8496.         CALL    L1BEE           ; routine CHECK-END checks nothing else
  8497.                                 ; is present in statement.
  8498.         RET                     ; Return
  8499.  
  8500. ; -------------
  8501. ; Fetch a value
  8502. ; -------------
  8503. ;
  8504. ;
  8505.  
  8506. ;; VAL-FET-1
  8507. L1C56:  LD      A,($5C3B)       ; initial FLAGS to A
  8508.  
  8509. ;; VAL-FET-2
  8510. L1C59:  PUSH    AF              ; save A briefly
  8511.         CALL    L24FB           ; routine SCANNING evaluates expression.
  8512.         POP     AF              ; restore A
  8513.         LD      D,(IY+$01)      ; post-SCANNING FLAGS to D
  8514.         XOR     D               ; xor the two sets of flags
  8515.         AND     $40             ; pick up bit 6 of xored FLAGS should be zero
  8516.         JR      NZ,L1C8A        ; forward to REPORT-C if not zero
  8517.                                 ; 'Nonsense in BASIC' - results don't agree.
  8518.  
  8519.         BIT     7,D             ; test FLAGS - is syntax being checked ?
  8520.         JP      NZ,L2AFF        ; jump forward to LET to make the assignment
  8521.                                 ; in runtime.
  8522.  
  8523.         RET                     ; but return from here if checking syntax.
  8524.  
  8525. ; ------------------
  8526. ; Command class---04
  8527. ; ------------------
  8528. ; class-04 e.g. FOR i            ; a single character variable must follow
  8529.  
  8530. ;; CLASS-04
  8531. L1C6C:  CALL    L28B2           ; routine LOOK-VARS
  8532.         PUSH    AF              ; preserve flags.
  8533.         LD      A,C             ; fetch type - should be 011xxxxx
  8534.         OR      $9F             ; combine with 10011111.
  8535.         INC     A               ; test if now $FF by incrementing.
  8536.         JR      NZ,L1C8A        ; forward to REPORT-C if result not zero.
  8537.  
  8538.         POP     AF              ; else restore flags.
  8539.         JR      L1C22           ; back to VAR-A-1
  8540.  
  8541.  
  8542. ; --------------------------------
  8543. ; Expect numeric/string expression
  8544. ; --------------------------------
  8545. ; This routine is used to get the two coordinates of STRING$, ATTR and POINT.
  8546. ; It is also called from PRINT-ITEM to get the two numeric expressions that
  8547. ; follow the AT ( in PRINT AT, INPUT AT).
  8548.  
  8549. ;; NEXT-2NUM
  8550. L1C79:  RST     20H             ; NEXT-CHAR advance past 'AT' or '('.
  8551.  
  8552. ; --------
  8553. ; class-08 e.g POKE 65535,2     ; two numeric expressions separated by comma
  8554. ;; CLASS-08
  8555. ;; EXPT-2NUM
  8556. L1C7A:  CALL    L1C82           ; routine EXPT-1NUM is called for first
  8557.                                 ; numeric expression
  8558.         CP      $2C             ; is character ',' ?
  8559.         JR      NZ,L1C8A        ; to REPORT-C if not required separator.
  8560.                                 ; 'Nonsense in BASIC'.
  8561.  
  8562.         RST     20H             ; NEXT-CHAR
  8563.  
  8564. ; ->
  8565. ;  class-06  e.g. GOTO a*1000   ; a numeric expression must follow
  8566. ;; CLASS-06
  8567. ;; EXPT-1NUM
  8568. L1C82:  CALL    L24FB           ; routine SCANNING
  8569.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8570.         RET     NZ              ; return if result is numeric.
  8571.  
  8572. ;; REPORT-C
  8573. L1C8A:  RST     08H             ; ERROR-1
  8574.         DB    $0B             ; Error Report: Nonsense in BASIC
  8575.  
  8576. ; ---------------------------------------------------------------
  8577. ; class-0A e.g. ERASE "????"    ; a string expression must follow.
  8578. ;                               ; these only occur in unimplemented commands
  8579. ;                               ; although the routine expt-exp is called
  8580. ;                               ; from SAVE-ETC
  8581.  
  8582. ;; CLASS-0A
  8583. ;; EXPT-EXP
  8584. L1C8C:  CALL    L24FB           ; routine SCANNING
  8585.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8586.         RET     Z               ; return if string result.
  8587.  
  8588.         JR      L1C8A           ; back to REPORT-C if numeric.
  8589.  
  8590. ; ---------------------
  8591. ; Set permanent colours
  8592. ; class 07
  8593. ; ---------------------
  8594. ; class-07 e.g PAPER 6          ; a single class for a collection of
  8595. ;                               ; similar commands. Clever.
  8596. ;
  8597. ; Note. these commands should ensure that current channel is 'S'
  8598.  
  8599. ;; CLASS-07
  8600. L1C96:  BIT     7,(IY+$01)      ; test FLAGS - checking syntax only ?
  8601.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8602.         CALL    NZ,L0D4D        ; routine TEMPS is called in runtime.
  8603.         POP     AF              ; drop return address SCAN-LOOP
  8604.         LD      A,($5C74)       ; T_ADDR_lo to accumulator.
  8605.                                 ; points to '$07' entry + 1
  8606.                                 ; e.g. for INK points to $EC now
  8607.  
  8608. ; Note if you move alter the syntax table next line may have to be altered.
  8609.  
  8610. ; Note. For ZASM assembler replace following expression with SUB $13.
  8611.  
  8612. L1CA5           SUB LOW (L1AEB)-$D8     ; % 256 ; convert $EB to $D8 ('INK') etc.
  8613.                                 ; ( is SUB $13 in standard ROM )
  8614.  
  8615.         CALL    L21FC           ; routine CO-TEMP-4
  8616.         CALL    L1BEE           ; routine CHECK-END check that nothing else
  8617.                                 ; in statement.
  8618.  
  8619. ; return here in runtime.
  8620.  
  8621.         LD      HL,($5C8F)      ; pick up ATTR_T and MASK_T
  8622.         LD      ($5C8D),HL      ; and store in ATTR_P and MASK_P
  8623.         LD      HL,$5C91        ; point to P_FLAG.
  8624.         LD      A,(HL)          ; pick up in A
  8625.         RLCA                    ; rotate to left
  8626.         XOR     (HL)            ; combine with HL
  8627.         AND     $AA             ; 10101010
  8628.         XOR     (HL)            ; only permanent bits affected
  8629.         LD      (HL),A          ; reload into P_FLAG.
  8630.         RET                     ; return.
  8631.  
  8632. ; ------------------
  8633. ; Command class---09
  8634. ; ------------------
  8635. ; e.g. PLOT PAPER 0; 128,88     ; two coordinates preceded by optional
  8636. ;                               ; embedded colour items.
  8637. ;
  8638. ; Note. this command should ensure that current channel is actually 'S'.
  8639.  
  8640. ;; CLASS-09
  8641. L1CBE:  CALL    L2530           ; routine SYNTAX-Z
  8642.         JR      Z,L1CD6         ; forward to CL-09-1 if checking syntax.
  8643.  
  8644.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8645.         CALL    L0D4D           ; routine TEMPS is called.
  8646.         LD      HL,$5C90        ; point to MASK_T
  8647.         LD      A,(HL)          ; fetch mask to accumulator.
  8648.         OR      $F8             ; or with 11111000 paper/bright/flash 8
  8649.         LD      (HL),A          ; mask back to MASK_T system variable.
  8650.         RES     6,(IY+$57)      ; reset P_FLAG  - signal NOT PAPER 9 ?
  8651.  
  8652.         RST     18H             ; GET-CHAR
  8653.  
  8654. ;; CL-09-1
  8655. L1CD6:  CALL    L21E2           ; routine CO-TEMP-2 deals with any embedded
  8656.                                 ; colour items.
  8657.         JR      L1C7A           ; exit via EXPT-2NUM to check for x,y.
  8658.  
  8659. ; Note. if either of the numeric expressions contain STR$ then the flag setting
  8660. ; above will be undone when the channel flags are reset during STR$.
  8661. ; e.g.
  8662. ; 10 BORDER 3 : PLOT VAL STR$ 128, VAL STR$ 100
  8663. ; credit John Elliott.
  8664.  
  8665. ; ------------------
  8666. ; Command class---0B
  8667. ; ------------------
  8668. ; Again a single class for four commands.
  8669. ; This command just jumps back to SAVE-ETC to handle the four tape commands.
  8670. ; The routine itself works out which command has called it by examining the
  8671. ; address in T_ADDR_lo. Note therefore that the syntax table has to be
  8672. ; located where these and other sequential command addresses are not split
  8673. ; over a page boundary.
  8674.  
  8675. ;; CLASS-0B
  8676. L1CDB:  JP      L0605           ; jump way back to SAVE-ETC
  8677.  
  8678. ; --------------
  8679. ; Fetch a number
  8680. ; --------------
  8681. ; This routine is called from CLASS-03 when a command may be followed by
  8682. ; an optional numeric expression e.g. RUN. If the end of statement has
  8683. ; been reached then zero is used as the default.
  8684. ; Also called from LIST-4.
  8685.  
  8686. ;; FETCH-NUM
  8687. L1CDE:  CP      $0D             ; is character a carriage return ?
  8688.         JR      Z,L1CE6         ; forward to USE-ZERO if so
  8689.  
  8690.         CP      $3A             ; is it ':' ?
  8691.         JR      NZ,L1C82        ; forward to EXPT-1NUM if not.
  8692.                                 ; else continue and use zero.
  8693.  
  8694. ; ----------------
  8695. ; Use zero routine
  8696. ; ----------------
  8697. ; This routine is called four times to place the value zero on the
  8698. ; calculator stack as a default value in runtime.
  8699.  
  8700. ;; USE-ZERO
  8701. L1CE6:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8702.         RET     Z               ;
  8703.  
  8704.         RST     28H             ;; FP-CALC
  8705.         DB    $A0             ;;stk-zero       ;0.
  8706.         DB    $38             ;;end-calc
  8707.  
  8708.         RET                     ; return.
  8709.  
  8710. ; -------------------
  8711. ; Handle STOP command
  8712. ; -------------------
  8713. ; Command Syntax: STOP
  8714. ; One of the shortest and least used commands. As with 'OK' not an error.
  8715.  
  8716. ;; REPORT-9
  8717. ;; STOP
  8718. L1CEE:  RST     08H             ; ERROR-1
  8719.         DB    $08             ; Error Report: STOP statement
  8720.  
  8721. ; -----------------
  8722. ; Handle IF command
  8723. ; -----------------
  8724. ; e.g. IF score>100 THEN PRINT "You Win"
  8725. ; The parser has already checked the expression the result of which is on
  8726. ; the calculator stack. The presence of the 'THEN' separator has also been
  8727. ; checked and CH-ADD points to the command after THEN.
  8728. ;
  8729.  
  8730. ;; IF
  8731. L1CF0:  POP     BC              ; drop return address - STMT-RET
  8732.         CALL    L2530           ; routine SYNTAX-Z
  8733.         JR      Z,L1D00         ; forward to IF-1 if checking syntax
  8734.                                 ; to check syntax of PRINT "You Win"
  8735.  
  8736.  
  8737.         RST     28H             ;; FP-CALC    score>100 (1=TRUE 0=FALSE)
  8738.         DB    $02             ;;delete      .
  8739.         DB    $38             ;;end-calc
  8740.  
  8741.         EX      DE,HL           ; make HL point to deleted value
  8742.         CALL    L34E9           ; routine TEST-ZERO
  8743.         JP      C,L1BB3         ; jump to LINE-END if FALSE (0)
  8744.  
  8745. ;; IF-1
  8746. L1D00:  JP      L1B29           ; to STMT-L-1, if true (1) to execute command
  8747.                                 ; after 'THEN' token.
  8748.  
  8749. ; ------------------
  8750. ; Handle FOR command
  8751. ; ------------------
  8752. ; e.g. FOR i = 0 TO 1 STEP 0.1
  8753. ; Using the syntax tables, the parser has already checked for a start and
  8754. ; limit value and also for the intervening separator.
  8755. ; the two values v,l are on the calculator stack.
  8756. ; CLASS-04 has also checked the variable and the name is in STRLEN_lo.
  8757. ; The routine begins by checking for an optional STEP.
  8758.  
  8759. ;; FOR
  8760. L1D03:  CP      $CD             ; is there a 'STEP' ?
  8761.         JR      NZ,L1D10        ; to F-USE-1 if not to use 1 as default.
  8762.  
  8763.         RST     20H             ; NEXT-CHAR
  8764.         CALL    L1C82           ; routine EXPT-1NUM
  8765.         CALL    L1BEE           ; routine CHECK-END
  8766.         JR      L1D16           ; to F-REORDER
  8767.  
  8768. ; ---
  8769.  
  8770. ;; F-USE-1
  8771. L1D10:  CALL    L1BEE           ; routine CHECK-END
  8772.  
  8773.         RST     28H             ;; FP-CALC      v,l.
  8774.         DB    $A1             ;;stk-one       v,l,1=s.
  8775.         DB    $38             ;;end-calc
  8776.  
  8777.  
  8778. ;; F-REORDER
  8779. L1D16:  RST     28H             ;; FP-CALC       v,l,s.
  8780.         DB    $C0             ;;st-mem-0       v,l,s.
  8781.         DB    $02             ;;delete         v,l.
  8782.         DB    $01             ;;exchange       l,v.
  8783.         DB    $E0             ;;get-mem-0      l,v,s.
  8784.         DB    $01             ;;exchange       l,s,v.
  8785.         DB    $38             ;;end-calc
  8786.  
  8787.         CALL    L2AFF           ; routine LET assigns the initial value v to
  8788.                                 ; the variable altering type if necessary.
  8789.         LD      ($5C68),HL      ; The system variable MEM is made to point to
  8790.                                 ; the variable instead of its normal
  8791.                                 ; location MEMBOT
  8792.         DEC     HL              ; point to single-character name
  8793.         LD      A,(HL)          ; fetch name
  8794.         SET     7,(HL)          ; set bit 7 at location
  8795.         LD      BC,$0006        ; add six to HL
  8796.         ADD     HL,BC           ; to address where limit should be.
  8797.         RLCA                    ; test bit 7 of original name.
  8798.         JR      C,L1D34         ; forward to F-L-S if already a FOR/NEXT
  8799.                                 ; variable
  8800.  
  8801.         LD      C,$0D           ; otherwise an additional 13 bytes are needed.
  8802.                                 ; 5 for each value, two for line number and
  8803.                                 ; 1 byte for looping statement.
  8804.         CALL    L1655           ; routine MAKE-ROOM creates them.
  8805.         INC     HL              ; make HL address limit.
  8806.  
  8807. ;; F-L-S
  8808. L1D34:  PUSH    HL              ; save position.
  8809.  
  8810.         RST     28H             ;; FP-CALC         l,s.
  8811.         DB    $02             ;;delete           l.
  8812.         DB    $02             ;;delete           .
  8813.         DB    $38             ;;end-calc
  8814.                                 ; DE points to STKEND, l.
  8815.  
  8816.         POP     HL              ; restore variable position
  8817.         EX      DE,HL           ; swap pointers
  8818.         LD      C,$0A           ; ten bytes to move
  8819.         LDIR                    ; Copy 'deleted' values to variable.
  8820.         LD      HL,($5C45)      ; Load with current line number from PPC
  8821.         EX      DE,HL           ; exchange pointers.
  8822.         LD      (HL),E          ; save the looping line
  8823.         INC     HL              ; in the next
  8824.         LD      (HL),D          ; two locations.
  8825.         LD      D,(IY+$0D)      ; fetch statement from SUBPPC system variable.
  8826.         INC     D               ; increment statement.
  8827.         INC     HL              ; and pointer
  8828.         LD      (HL),D          ; and store the looping statement.
  8829.                                 ;
  8830.         CALL    L1DDA           ; routine NEXT-LOOP considers an initial
  8831.         RET     NC              ; iteration. Return to STMT-RET if a loop is
  8832.                                 ; possible to execute next statement.
  8833.  
  8834. ; no loop is possible so execution continues after the matching 'NEXT'
  8835.  
  8836.         LD      B,(IY+$38)      ; get single-character name from STRLEN_lo
  8837.         LD      HL,($5C45)      ; get the current line from PPC
  8838.         LD      ($5C42),HL      ; and store it in NEWPPC
  8839.         LD      A,($5C47)       ; fetch current statement from SUBPPC
  8840.         NEG                     ; Negate as counter decrements from zero
  8841.                                 ; initially and we are in the middle of a
  8842.                                 ; line.
  8843.         LD      D,A             ; Store result in D.
  8844.         LD      HL,($5C5D)      ; get current address from CH_ADD
  8845.         LD      E,$F3           ; search will be for token 'NEXT'
  8846.  
  8847. ;; F-LOOP
  8848. L1D64:  PUSH    BC              ; save variable name.
  8849.         LD      BC,($5C55)      ; fetch NXTLIN
  8850.         CALL    L1D86           ; routine LOOK-PROG searches for 'NEXT' token.
  8851.         LD      ($5C55),BC      ; update NXTLIN
  8852.         POP     BC              ; and fetch the letter
  8853.         JR      C,L1D84         ; forward to REPORT-I if the end of program
  8854.                                 ; was reached by LOOK-PROG.
  8855.                                 ; 'FOR without NEXT'
  8856.  
  8857.         RST     20H             ; NEXT-CHAR fetches character after NEXT
  8858.         OR      $20             ; ensure it is upper-case.
  8859.         CP      B               ; compare with FOR variable name
  8860.         JR      Z,L1D7C         ; forward to F-FOUND if it matches.
  8861.  
  8862. ; but if no match i.e. nested FOR/NEXT loops then continue search.
  8863.  
  8864.         RST     20H             ; NEXT-CHAR
  8865.         JR      L1D64           ; back to F-LOOP
  8866.  
  8867. ; ---
  8868.  
  8869.  
  8870. ;; F-FOUND
  8871. L1D7C:  RST     20H             ; NEXT-CHAR
  8872.         LD      A,$01           ; subtract the negated counter from 1
  8873.         SUB     D               ; to give the statement after the NEXT
  8874.         LD      ($5C44),A       ; set system variable NSPPC
  8875.         RET                     ; return to STMT-RET to branch to new
  8876.                                 ; line and statement. ->
  8877. ; ---
  8878.  
  8879. ;; REPORT-I
  8880. L1D84:  RST     08H             ; ERROR-1
  8881.         DB    $11             ; Error Report: FOR without NEXT
  8882.  
  8883. ; ---------
  8884. ; LOOK-PROG
  8885. ; ---------
  8886. ; Find DATA, DEF FN or NEXT.
  8887. ; This routine searches the program area for one of the above three keywords.
  8888. ; On entry, HL points to start of search area.
  8889. ; The token is in E, and D holds a statement count, decremented from zero.
  8890.  
  8891. ;; LOOK-PROG
  8892. L1D86:  LD      A,(HL)          ; fetch current character
  8893.         CP      $3A             ; is it ':' a statement separator ?
  8894.         JR      Z,L1DA3         ; forward to LOOK-P-2 if so.
  8895.  
  8896. ; The starting point was PROG - 1 or the end of a line.
  8897.  
  8898. ;; LOOK-P-1
  8899. L1D8B:  INC     HL              ; increment pointer to address
  8900.         LD      A,(HL)          ; the high byte of line number
  8901.         AND     $C0             ; test for program end marker $80 or a
  8902.                                 ; variable
  8903.         SCF                     ; Set Carry Flag
  8904.         RET     NZ              ; return with carry set if at end
  8905.                                 ; of program.           ->
  8906.  
  8907.         LD      B,(HL)          ; high byte of line number to B
  8908.         INC     HL              ;
  8909.         LD      C,(HL)          ; low byte to C.
  8910.         LD      ($5C42),BC      ; set system variable NEWPPC.
  8911.         INC     HL              ;
  8912.         LD      C,(HL)          ; low byte of line length to C.
  8913.         INC     HL              ;
  8914.         LD      B,(HL)          ; high byte to B.
  8915.         PUSH    HL              ; save address
  8916.         ADD     HL,BC           ; add length to position.
  8917.         LD      B,H             ; and save result
  8918.         LD      C,L             ; in BC.
  8919.         POP     HL              ; restore address.
  8920.         LD      D,$00           ; initialize statement counter to zero.
  8921.  
  8922. ;; LOOK-P-2
  8923. L1DA3:  PUSH    BC              ; save address of next line
  8924.         CALL    L198B           ; routine EACH-STMT searches current line.
  8925.         POP     BC              ; restore address.
  8926.         RET     NC              ; return if match was found. ->
  8927.  
  8928.         JR      L1D8B           ; back to LOOK-P-1 for next line.
  8929.  
  8930. ; -------------------
  8931. ; Handle NEXT command
  8932. ; -------------------
  8933. ; e.g. NEXT i
  8934. ; The parameter tables have already evaluated the presence of a variable
  8935.  
  8936. ;; NEXT
  8937. L1DAB:  BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  8938.         JP      NZ,L1C2E        ; jump back to REPORT-2 if so
  8939.                                 ; 'Variable not found'
  8940.  
  8941. ; now test if found variable is a simple variable uninitialized by a FOR.
  8942.  
  8943.         LD      HL,($5C4D)      ; load address of variable from DEST
  8944.         BIT     7,(HL)          ; is it correct type ?
  8945.         JR      Z,L1DD8         ; forward to REPORT-1 if not
  8946.                                 ; 'NEXT without FOR'
  8947.  
  8948.         INC     HL              ; step past variable name
  8949.         LD      ($5C68),HL      ; and set MEM to point to three 5-byte values
  8950.                                 ; value, limit, step.
  8951.  
  8952.         RST     28H             ;; FP-CALC     add step and re-store
  8953.         DB    $E0             ;;get-mem-0    v.
  8954.         DB    $E2             ;;get-mem-2    v,s.
  8955.         DB    $0F             ;;addition     v+s.
  8956.         DB    $C0             ;;st-mem-0     v+s.
  8957.         DB    $02             ;;delete       .
  8958.         DB    $38             ;;end-calc
  8959.  
  8960.         CALL    L1DDA           ; routine NEXT-LOOP tests against limit.
  8961.         RET     C               ; return if no more iterations possible.
  8962.  
  8963.         LD      HL,($5C68)      ; find start of variable contents from MEM.
  8964.         LD      DE,$000F        ; add 3*5 to
  8965.         ADD     HL,DE           ; address the looping line number
  8966.         LD      E,(HL)          ; low byte to E
  8967.         INC     HL              ;
  8968.         LD      D,(HL)          ; high byte to D
  8969.         INC     HL              ; address looping statement
  8970.         LD      H,(HL)          ; and store in H
  8971.         EX      DE,HL           ; swap registers
  8972.         JP      L1E73           ; exit via GO-TO-2 to execute another loop.
  8973.  
  8974. ; ---
  8975.  
  8976. ;; REPORT-1
  8977. L1DD8:  RST     08H             ; ERROR-1
  8978.         DB    $00             ; Error Report: NEXT without FOR
  8979.  
  8980.  
  8981. ; -----------------
  8982. ; Perform NEXT loop
  8983. ; -----------------
  8984. ; This routine is called from the FOR command to test for an initial
  8985. ; iteration and from the NEXT command to test for all subsequent iterations.
  8986. ; the system variable MEM addresses the variable's contents which, in the
  8987. ; latter case, have had the step, possibly negative, added to the value.
  8988.  
  8989. ;; NEXT-LOOP
  8990. L1DDA:  RST     28H             ;; FP-CALC
  8991.         DB    $E1             ;;get-mem-1        l.
  8992.         DB    $E0             ;;get-mem-0        l,v.
  8993.         DB    $E2             ;;get-mem-2        l,v,s.
  8994.         DB    $36             ;;less-0           l,v,(1/0) negative step ?
  8995.         DB    $00             ;;jump-true        l,v.(1/0)
  8996.  
  8997.         DB    $02             ;;to L1DE2, NEXT-1 if step negative
  8998.  
  8999.         DB    $01             ;;exchange         v,l.
  9000.  
  9001. ;; NEXT-1
  9002. L1DE2:  DB    $03             ;;subtract         l-v OR v-l.
  9003.         DB    $37             ;;greater-0        (1/0)
  9004.         DB    $00             ;;jump-true        .
  9005.  
  9006.         DB    $04             ;;to L1DE9, NEXT-2 if no more iterations.
  9007.  
  9008.         DB    $38             ;;end-calc         .
  9009.  
  9010.         AND     A               ; clear carry flag signalling another loop.
  9011.         RET                     ; return
  9012.  
  9013. ; ---
  9014.  
  9015. ;; NEXT-2
  9016. L1DE9:  DB    $38             ;;end-calc         .
  9017.  
  9018.         SCF                     ; set carry flag signalling looping exhausted.
  9019.         RET                     ; return
  9020.  
  9021.  
  9022. ; -------------------
  9023. ; Handle READ command
  9024. ; -------------------
  9025. ; e.g. READ a, b$, c$(1000 TO 3000)
  9026. ; A list of comma-separated variables is assigned from a list of
  9027. ; comma-separated expressions.
  9028. ; As it moves along the first list, the character address CH_ADD is stored
  9029. ; in X_PTR while CH_ADD is used to read the second list.
  9030.  
  9031. ;; READ-3
  9032. L1DEC:  RST     20H             ; NEXT-CHAR
  9033.  
  9034. ; -> Entry point.
  9035. ;; READ
  9036. L1DED:  CALL    L1C1F           ; routine CLASS-01 checks variable.
  9037.         CALL    L2530           ; routine SYNTAX-Z
  9038.         JR      Z,L1E1E         ; forward to READ-2 if checking syntax
  9039.  
  9040.  
  9041.         RST     18H             ; GET-CHAR
  9042.         LD      ($5C5F),HL      ; save character position in X_PTR.
  9043.         LD      HL,($5C57)      ; load HL with Data Address DATADD, which is
  9044.                                 ; the start of the program or the address
  9045.                                 ; after the last expression that was read or
  9046.                                 ; the address of the line number of the
  9047.                                 ; last RESTORE command.
  9048.         LD      A,(HL)          ; fetch character
  9049.         CP      $2C             ; is it a comma ?
  9050.         JR      Z,L1E0A         ; forward to READ-1 if so.
  9051.  
  9052. ; else all data in this statement has been read so look for next DATA token
  9053.  
  9054.         LD      E,$E4           ; token 'DATA'
  9055.         CALL    L1D86           ; routine LOOK-PROG
  9056.         JR      NC,L1E0A        ; forward to READ-1 if DATA found
  9057.  
  9058. ; else report the error.
  9059.  
  9060. ;; REPORT-E
  9061. L1E08:  RST     08H             ; ERROR-1
  9062.         DB    $0D             ; Error Report: Out of DATA
  9063.  
  9064. ;; READ-1
  9065. L1E0A:  CALL    L0077           ; routine TEMP-PTR1 advances updating CH_ADD
  9066.                                 ; with new DATADD position.
  9067.         CALL    L1C56           ; routine VAL-FET-1 assigns value to variable
  9068.                                 ; checking type match and adjusting CH_ADD.
  9069.  
  9070.         RST     18H             ; GET-CHAR fetches adjusted character position
  9071.         LD      ($5C57),HL      ; store back in DATADD
  9072.         LD      HL,($5C5F)      ; fetch X_PTR  the original READ CH_ADD
  9073.         LD      (IY+$26),$00    ; now nullify X_PTR_hi
  9074.         CALL    L0078           ; routine TEMP-PTR2 restores READ CH_ADD
  9075.  
  9076. ;; READ-2
  9077. L1E1E:  RST     18H             ; GET-CHAR
  9078.         CP      $2C             ; is it ',' indicating more variables to read ?
  9079.         JR      Z,L1DEC         ; back to READ-3 if so
  9080.  
  9081.         CALL    L1BEE           ; routine CHECK-END
  9082.         RET                     ; return from here in runtime to STMT-RET.
  9083.  
  9084. ; -------------------
  9085. ; Handle DATA command
  9086. ; -------------------
  9087. ; In runtime this 'command' is passed by but the syntax is checked when such
  9088. ; a statement is found while parsing a line.
  9089. ; e.g. DATA 1, 2, "text", score-1, a$(location, room, object), FN r(49),
  9090. ;         wages - tax, TRUE, The meaning of life
  9091.  
  9092. ;; DATA
  9093. L1E27:  CALL    L2530           ; routine SYNTAX-Z to check status
  9094.         JR      NZ,L1E37        ; forward to DATA-2 if in runtime
  9095.  
  9096. ;; DATA-1
  9097. L1E2C:  CALL    L24FB           ; routine SCANNING to check syntax of
  9098.                                 ; expression
  9099.         CP      $2C             ; is it a comma ?
  9100.         CALL    NZ,L1BEE        ; routine CHECK-END checks that statement
  9101.                                 ; is complete. Will make an early exit if
  9102.                                 ; so. >>>
  9103.         RST     20H             ; NEXT-CHAR
  9104.         JR      L1E2C           ; back to DATA-1
  9105.  
  9106. ; ---
  9107.  
  9108. ;; DATA-2
  9109. L1E37:  LD      A,$E4           ; set token to 'DATA' and continue into
  9110.                                 ; the the PASS-BY routine.
  9111.  
  9112.  
  9113. ; ----------------------------------
  9114. ; Check statement for DATA or DEF FN
  9115. ; ----------------------------------
  9116. ; This routine is used to backtrack to a command token and then
  9117. ; forward to the next statement in runtime.
  9118.  
  9119. ;; PASS-BY
  9120. L1E39:  LD      B,A             ; Give BC enough space to find token.
  9121.         CPDR                    ; Compare decrement and repeat. (Only use).
  9122.                                 ; Work backwards till keyword is found which
  9123.                                 ; is start of statement before any quotes.
  9124.                                 ; HL points to location before keyword.
  9125.         LD      DE,$0200        ; count 1+1 statements, dummy value in E to
  9126.                                 ; inhibit searching for a token.
  9127.         JP      L198B           ; to EACH-STMT to find next statement
  9128.  
  9129. ; -----------------------------------------------------------------------
  9130. ; A General Note on Invalid Line Numbers.
  9131. ; =======================================
  9132. ; One of the revolutionary concepts of Sinclair BASIC was that it supported
  9133. ; virtual line numbers. That is the destination of a GO TO, RESTORE etc. need
  9134. ; not exist. It could be a point before or after an actual line number.
  9135. ; Zero suffices for a before but the after should logically be infinity.
  9136. ; Since the maximum actual line limit is 9999 then the system limit, 16383
  9137. ; when variables kick in, would serve fine as a virtual end point.
  9138. ; However, ironically, only the LOAD command gets it right. It will not
  9139. ; autostart a program that has been saved with a line higher than 16383.
  9140. ; All the other commands deal with the limit unsatisfactorily.
  9141. ; LIST, RUN, GO TO, GO SUB and RESTORE have problems and the latter may
  9142. ; crash the machine when supplied with an inappropriate virtual line number.
  9143. ; This is puzzling as very careful consideration must have been given to
  9144. ; this point when the new variable types were allocated their masks and also
  9145. ; when the routine NEXT-ONE was successfully re-written to reflect this.
  9146. ; An enigma.
  9147. ; -------------------------------------------------------------------------
  9148.  
  9149. ; ----------------------
  9150. ; Handle RESTORE command
  9151. ; ----------------------
  9152. ; The restore command sets the system variable for the data address to
  9153. ; point to the location before the supplied line number or first line
  9154. ; thereafter.
  9155. ; This alters the position where subsequent READ commands look for data.
  9156. ; Note. If supplied with inappropriate high numbers the system may crash
  9157. ; in the LINE-ADDR routine as it will pass the program/variables end-marker
  9158. ; and then lose control of what it is looking for - variable or line number.
  9159. ; - observation, Steven Vickers, 1984, Pitman.
  9160.  
  9161. ;; RESTORE
  9162. L1E42:  CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9163.                                 ; Note. B should be checked against limit $3F
  9164.                                 ; and an error generated if higher.
  9165.  
  9166. ; this entry point is used from RUN command with BC holding zero
  9167.  
  9168. ;; REST-RUN
  9169. L1E45:  LD      H,B             ; transfer the line
  9170.         LD      L,C             ; number to the HL register.
  9171.         CALL    L196E           ; routine LINE-ADDR to fetch the address.
  9172.         DEC     HL              ; point to the location before the line.
  9173.         LD      ($5C57),HL      ; update system variable DATADD.
  9174.         RET                     ; return to STMT-RET (or RUN)
  9175.  
  9176. ; ------------------------
  9177. ; Handle RANDOMIZE command
  9178. ; ------------------------
  9179. ; This command sets the SEED for the RND function to a fixed value.
  9180. ; With the parameter zero, a random start point is used depending on
  9181. ; how long the computer has been switched on.
  9182.  
  9183. ;; RANDOMIZE
  9184. L1E4F:  CALL    L1E99           ; routine FIND-INT2 puts parameter in BC.
  9185.         LD      A,B             ; test this
  9186.         OR      C               ; for zero.
  9187.         JR      NZ,L1E5A        ; forward to RAND-1 if not zero.
  9188.  
  9189.         LD      BC,($5C78)      ; use the lower two bytes at FRAMES1.
  9190.  
  9191. ;; RAND-1
  9192. L1E5A:  LD      ($5C76),BC      ; place in SEED system variable.
  9193.         RET                     ; return to STMT-RET
  9194.  
  9195. ; -----------------------
  9196. ; Handle CONTINUE command
  9197. ; -----------------------
  9198. ; The CONTINUE command transfers the OLD (but incremented) values of
  9199. ; line number and statement to the equivalent "NEW VALUE" system variables
  9200. ; by using the last part of GO TO and exits indirectly to STMT-RET.
  9201.  
  9202. ;; CONTINUE
  9203. L1E5F:  LD      HL,($5C6E)      ; fetch OLDPPC line number.
  9204.         LD      D,(IY+$36)      ; fetch OSPPC statement.
  9205.         JR      L1E73           ; forward to GO-TO-2
  9206.  
  9207. ; --------------------
  9208. ; Handle GO TO command
  9209. ; --------------------
  9210. ; The GO TO command routine is also called by GO SUB and RUN routines
  9211. ; to evaluate the parameters of both commands.
  9212. ; It updates the system variables used to fetch the next line/statement.
  9213. ; It is at STMT-RET that the actual change in control takes place.
  9214. ; Unlike some BASICs the line number need not exist.
  9215. ; Note. the high byte of the line number is incorrectly compared with $F0
  9216. ; instead of $3F. This leads to commands with operands greater than 32767
  9217. ; being considered as having been run from the editing area and the
  9218. ; error report 'Statement Lost' is given instead of 'OK'.
  9219. ; - Steven Vickers, 1984.
  9220.  
  9221. ;; GO-TO
  9222. L1E67:  CALL    L1E99           ; routine FIND-INT2 puts operand in BC
  9223.         LD      H,B             ; transfer line
  9224.         LD      L,C             ; number to HL.
  9225.         LD      D,$00           ; set statement to 0 - first.
  9226.         LD      A,H             ; compare high byte only
  9227.         CP      $F0             ; to $F0 i.e. 61439 in full.
  9228.         JR      NC,L1E9F        ; forward to REPORT-B if above.
  9229.  
  9230. ; This call entry point is used to update the system variables e.g. by RETURN.
  9231.  
  9232. ;; GO-TO-2
  9233. L1E73:  LD      ($5C42),HL      ; save line number in NEWPPC
  9234.         LD      (IY+$0A),D      ; and statement in NSPPC
  9235.         RET                     ; to STMT-RET (or GO-SUB command)
  9236.  
  9237. ; ------------------
  9238. ; Handle OUT command
  9239. ; ------------------
  9240. ; Syntax has been checked and the two comma-separated values are on the
  9241. ; calculator stack.
  9242.  
  9243. ;; OUT
  9244. L1E7A:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9245.                                 ; to BC and A.
  9246.         OUT     (C),A           ; perform the operation.
  9247.         RET                     ; return to STMT-RET.
  9248.  
  9249. ; -------------------
  9250. ; Handle POKE command
  9251. ; -------------------
  9252. ; This routine alters a single byte in the 64K address space.
  9253. ; Happily no check is made as to whether ROM or RAM is addressed.
  9254. ; Sinclair BASIC requires no poking of system variables.
  9255.  
  9256. ;; POKE
  9257. L1E80:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9258.                                 ; to BC and A.
  9259.         LD      (BC),A          ; load memory location with A.
  9260.         RET                     ; return to STMT-RET.
  9261.  
  9262. ; ------------------------------------
  9263. ; Fetch two  parameters from calculator stack
  9264. ; ------------------------------------
  9265. ; This routine fetches a byte and word from the calculator stack
  9266. ; producing an error if either is out of range.
  9267.  
  9268. ;; TWO-PARAM
  9269. L1E85:  CALL    L2DD5           ; routine FP-TO-A
  9270.         JR      C,L1E9F         ; forward to REPORT-B if overflow occurred
  9271.  
  9272.         JR      Z,L1E8E         ; forward to TWO-P-1 if positive
  9273.  
  9274.         NEG                     ; negative numbers are made positive
  9275.  
  9276. ;; TWO-P-1
  9277. L1E8E:  PUSH    AF              ; save the value
  9278.         CALL    L1E99           ; routine FIND-INT2 gets integer to BC
  9279.         POP     AF              ; restore the value
  9280.         RET                     ; return
  9281.  
  9282. ; -------------
  9283. ; Find integers
  9284. ; -------------
  9285. ; The first of these routines fetches a 8-bit integer (range 0-255) from the
  9286. ; calculator stack to the accumulator and is used for colours, streams,
  9287. ; durations and coordinates.
  9288. ; The second routine fetches 16-bit integers to the BC register pair
  9289. ; and is used to fetch command and function arguments involving line numbers
  9290. ; or memory addresses and also array subscripts and tab arguments.
  9291. ; ->
  9292.  
  9293. ;; FIND-INT1
  9294. L1E94:  CALL    L2DD5           ; routine FP-TO-A
  9295.         JR      L1E9C           ; forward to FIND-I-1 for common exit routine.
  9296.  
  9297. ; ---
  9298.  
  9299. ; ->
  9300.  
  9301. ;; FIND-INT2
  9302. L1E99:  CALL    L2DA2           ; routine FP-TO-BC
  9303.  
  9304. ;; FIND-I-1
  9305. L1E9C:  JR      C,L1E9F         ; to REPORT-Bb with overflow.
  9306.  
  9307.         RET     Z               ; return if positive.
  9308.  
  9309.  
  9310. ;; REPORT-Bb
  9311. L1E9F:  RST     08H             ; ERROR-1
  9312.         DB    $0A             ; Error Report: Integer out of range
  9313.  
  9314. ; ------------------
  9315. ; Handle RUN command
  9316. ; ------------------
  9317. ; This command runs a program starting at an optional line.
  9318. ; It performs a 'RESTORE 0' then CLEAR
  9319.  
  9320. ;; RUN
  9321. L1EA1:  CALL    L1E67           ; routine GO-TO puts line number in
  9322.                                 ; system variables.
  9323.         LD      BC,$0000        ; prepare to set DATADD to first line.
  9324.         CALL    L1E45           ; routine REST-RUN does the 'restore'.
  9325.                                 ; Note BC still holds zero.
  9326.         JR      L1EAF           ; forward to CLEAR-RUN to clear variables
  9327.                                 ; without disturbing RAMTOP and
  9328.                                 ; exit indirectly to STMT-RET
  9329.  
  9330. ; --------------------
  9331. ; Handle CLEAR command
  9332. ; --------------------
  9333. ; This command reclaims the space used by the variables.
  9334. ; It also clears the screen and the GO SUB stack.
  9335. ; With an integer expression, it sets the uppermost memory
  9336. ; address within the BASIC system.
  9337. ; "Contrary to the manual, CLEAR doesn't execute a RESTORE" -
  9338. ; Steven Vickers, Pitman Pocket Guide to the Spectrum, 1984.
  9339.  
  9340. ;; CLEAR
  9341. L1EAC:  CALL    L1E99           ; routine FIND-INT2 fetches to BC.
  9342.  
  9343. ;; CLEAR-RUN
  9344. L1EAF:  LD      A,B             ; test for
  9345.         OR      C               ; zero.
  9346.         JR      NZ,L1EB7        ; skip to CLEAR-1 if not zero.
  9347.  
  9348.         LD      BC,($5CB2)      ; use the existing value of RAMTOP if zero.
  9349.  
  9350. ;; CLEAR-1
  9351. L1EB7:  PUSH    BC              ; save ramtop value.
  9352.  
  9353.         LD      DE,($5C4B)      ; fetch VARS
  9354.         LD      HL,($5C59)      ; fetch E_LINE
  9355.         DEC     HL              ; adjust to point at variables end-marker.
  9356.         CALL    L19E5           ; routine RECLAIM-1 reclaims the space used by
  9357.                                 ; the variables.
  9358.         CALL    L0D6B           ; routine CLS to clear screen.
  9359.         LD      HL,($5C65)      ; fetch STKEND the start of free memory.
  9360.         LD      DE,$0032        ; allow for another 50 bytes.
  9361.         ADD     HL,DE           ; add the overhead to HL.
  9362.  
  9363.         POP     DE              ; restore the ramtop value.
  9364.         SBC     HL,DE           ; if HL is greater than the value then jump
  9365.         JR      NC,L1EDA        ; forward to REPORT-M
  9366.                                 ; 'RAMTOP no good'
  9367.  
  9368.         LD      HL,($5CB4)      ; now P-RAMT ($7FFF on 16K RAM machine)
  9369.         AND     A               ; exact this time.
  9370.         SBC     HL,DE           ; new ramtop must be lower or the same.
  9371.         JR      NC,L1EDC        ; skip to CLEAR-2 if in actual RAM.
  9372.  
  9373. ;; REPORT-M
  9374. L1EDA:  RST     08H             ; ERROR-1
  9375.         DB    $15             ; Error Report: RAMTOP no good
  9376.  
  9377. ;; CLEAR-2
  9378. L1EDC:  EX      DE,HL           ; transfer ramtop value to HL.
  9379.         LD      ($5CB2),HL      ; update system variable RAMTOP.
  9380.         POP     DE              ; pop the return address STMT-RET.
  9381.         POP     BC              ; pop the Error Address.
  9382.         LD      (HL),$3E        ; now put the GO SUB end-marker at RAMTOP.
  9383.         DEC     HL              ; leave a location beneath it.
  9384.         LD      SP,HL           ; initialize the machine stack pointer.
  9385.         PUSH    BC              ; push the error address.
  9386.         LD      ($5C3D),SP      ; make ERR_SP point to location.
  9387.         EX      DE,HL           ; put STMT-RET in HL.
  9388.         JP      (HL)            ; and go there directly.
  9389.  
  9390. ; ---------------------
  9391. ; Handle GO SUB command
  9392. ; ---------------------
  9393. ; The GO SUB command diverts BASIC control to a new line number
  9394. ; in a very similar manner to GO TO but
  9395. ; the current line number and current statement + 1
  9396. ; are placed on the GO SUB stack as a RETURN point.
  9397.  
  9398. ;; GO-SUB
  9399. L1EED:  POP     DE              ; drop the address STMT-RET
  9400.         LD      H,(IY+$0D)      ; fetch statement from SUBPPC and
  9401.         INC     H               ; increment it
  9402.         EX      (SP),HL         ; swap - error address to HL,
  9403.                                 ; H (statement) at top of stack,
  9404.                                 ; L (unimportant) beneath.
  9405.         INC     SP              ; adjust to overwrite unimportant byte
  9406.         LD      BC,($5C45)      ; fetch the current line number from PPC
  9407.         PUSH    BC              ; and PUSH onto GO SUB stack.
  9408.                                 ; the empty machine-stack can be rebuilt
  9409.         PUSH    HL              ; push the error address.
  9410.         LD      ($5C3D),SP      ; make system variable ERR_SP point to it.
  9411.         PUSH    DE              ; push the address STMT-RET.
  9412.         CALL    L1E67           ; call routine GO-TO to update the system
  9413.                                 ; variables NEWPPC and NSPPC.
  9414.                                 ; then make an indirect exit to STMT-RET via
  9415.         LD      BC,$0014        ; a 20-byte overhead memory check.
  9416.  
  9417. ; ----------------------
  9418. ; Check available memory
  9419. ; ----------------------
  9420. ; This routine is used on many occasions when extending a dynamic area
  9421. ; upwards or the GO SUB stack downwards.
  9422.  
  9423. ;; TEST-ROOM
  9424. L1F05:  LD      HL,($5C65)      ; fetch STKEND
  9425.         ADD     HL,BC           ; add the supplied test value
  9426.         JR      C,L1F15         ; forward to REPORT-4 if over $FFFF
  9427.  
  9428.         EX      DE,HL           ; was less so transfer to DE
  9429.         LD      HL,$0050        ; test against another 80 bytes
  9430.         ADD     HL,DE           ; anyway
  9431.         JR      C,L1F15         ; forward to REPORT-4 if this passes $FFFF
  9432.  
  9433.         SBC     HL,SP           ; if less than the machine stack pointer
  9434.         RET     C               ; then return - OK.
  9435.  
  9436. ;; REPORT-4
  9437. L1F15:  LD      L,$03           ; prepare 'Out of Memory'
  9438.         JP      L0055           ; jump back to ERROR-3 at $0055
  9439.                                 ; Note. this error can't be trapped at $0008
  9440.  
  9441. ; ------------------------------
  9442. ; THE 'FREE MEMORY' USER ROUTINE
  9443. ; ------------------------------
  9444. ; This routine is not used by the ROM but allows users to evaluate
  9445. ; approximate free memory with PRINT 65536 - USR 7962.
  9446.  
  9447. ;; free-mem
  9448. L1F1A:  LD      BC,$0000        ; allow no overhead.
  9449.  
  9450.         CALL    L1F05           ; routine TEST-ROOM.
  9451.  
  9452.         LD      B,H             ; transfer the result
  9453.         LD      C,L             ; to the BC register.
  9454.         RET                     ; the USR function returns value of BC.
  9455.  
  9456. ; --------------------
  9457. ; THE 'RETURN' COMMAND
  9458. ; --------------------
  9459. ; As with any command, there are two values on the machine stack at the time
  9460. ; it is invoked.  The machine stack is below the GOSUB stack.  Both grow
  9461. ; downwards, the machine stack by two bytes, the GOSUB stack by 3 bytes.
  9462. ; The highest location is a statement byte followed by a two-byte line number.
  9463.  
  9464. ;; RETURN
  9465. L1F23:  POP     BC              ; drop the address STMT-RET.
  9466.         POP     HL              ; now the error address.
  9467.         POP     DE              ; now a possible BASIC return line.
  9468.         LD      A,D             ; the high byte $00 - $27 is
  9469.         CP      $3E             ; compared with the traditional end-marker $3E.
  9470.         JR      Z,L1F36         ; forward to REPORT-7 with a match.
  9471.                                 ; 'RETURN without GOSUB'
  9472.  
  9473. ; It was not the end-marker so a single statement byte remains at the base of
  9474. ; the calculator stack. It can't be popped off.
  9475.  
  9476.         DEC     SP              ; adjust stack pointer to create room for two
  9477.                                 ; bytes.
  9478.         EX      (SP),HL         ; statement to H, error address to base of
  9479.                                 ; new machine stack.
  9480.         EX      DE,HL           ; statement to D,  BASIC line number to HL.
  9481.         LD      ($5C3D),SP      ; adjust ERR_SP to point to new stack pointer
  9482.         PUSH    BC              ; now re-stack the address STMT-RET
  9483.         JP      L1E73           ; to GO-TO-2 to update statement and line
  9484.                                 ; system variables and exit indirectly to the
  9485.                                 ; address just pushed on stack.
  9486.  
  9487. ; ---
  9488.  
  9489. ;; REPORT-7
  9490. L1F36:  PUSH    DE              ; replace the end-marker.
  9491.         PUSH    HL              ; now restore the error address
  9492.                                 ; as will be required in a few clock cycles.
  9493.  
  9494.         RST     08H             ; ERROR-1
  9495.         DB    $06             ; Error Report: RETURN without GOSUB
  9496.  
  9497. ; --------------------
  9498. ; Handle PAUSE command
  9499. ; --------------------
  9500. ; The pause command takes as its parameter the number of interrupts
  9501. ; for which to wait. PAUSE 50 pauses for about a second.
  9502. ; PAUSE 0 pauses indefinitely.
  9503. ; Both forms can be finished by pressing a key.
  9504.  
  9505. ;; PAUSE
  9506. L1F3A:  CALL    L1E99           ; routine FIND-INT2 puts value in BC
  9507.  
  9508. ;; PAUSE-1
  9509. L1F3D:  HALT                    ; wait for interrupt.
  9510.         DEC     BC              ; decrease counter.
  9511.         LD      A,B             ; test if
  9512.         OR      C               ; result is zero.
  9513.         JR      Z,L1F4F         ; forward to PAUSE-END if so.
  9514.  
  9515.         LD      A,B             ; test if
  9516.         AND     C               ; now $FFFF
  9517.         INC     A               ; that is, initially zero.
  9518.         JR      NZ,L1F49        ; skip forward to PAUSE-2 if not.
  9519.  
  9520.         INC     BC              ; restore counter to zero.
  9521.  
  9522. ;; PAUSE-2
  9523. L1F49:  BIT     5,(IY+$01)      ; test FLAGS - has a new key been pressed ?
  9524.         JR      Z,L1F3D         ; back to PAUSE-1 if not.
  9525.  
  9526. ;; PAUSE-END
  9527. L1F4F:  RES     5,(IY+$01)      ; update FLAGS - signal no new key
  9528.         RET                     ; and return.
  9529.  
  9530. ; -------------------
  9531. ; Check for BREAK key
  9532. ; -------------------
  9533. ; This routine is called from COPY-LINE, when interrupts are disabled,
  9534. ; to test if BREAK (SHIFT - SPACE) is being pressed.
  9535. ; It is also called at STMT-RET after every statement.
  9536.  
  9537. ;; BREAK-KEY
  9538. L1F54:  LD      A,$7F           ; Input address: $7FFE
  9539.         IN      A,($FE)         ; read lower right keys
  9540.         RRA                     ; rotate bit 0 - SPACE
  9541.         RET     C               ; return if not reset
  9542.  
  9543.         LD      A,$FE           ; Input address: $FEFE
  9544.         IN      A,($FE)         ; read lower left keys
  9545.         RRA                     ; rotate bit 0 - SHIFT
  9546.         RET                     ; carry will be set if not pressed.
  9547.                                 ; return with no carry if both keys
  9548.                                 ; pressed.
  9549.  
  9550. ; ---------------------
  9551. ; Handle DEF FN command
  9552. ; ---------------------
  9553. ; e.g DEF FN r$(a$,a) = a$(a TO )
  9554. ; this 'command' is ignored in runtime but has its syntax checked
  9555. ; during line-entry.
  9556.  
  9557. ;; DEF-FN
  9558. L1F60:  CALL    L2530           ; routine SYNTAX-Z
  9559.         JR      Z,L1F6A         ; forward to DEF-FN-1 if parsing
  9560.  
  9561.         LD      A,$CE           ; else load A with 'DEF FN' and
  9562.         JP      L1E39           ; jump back to PASS-BY
  9563.  
  9564. ; ---
  9565.  
  9566. ; continue here if checking syntax.
  9567.  
  9568. ;; DEF-FN-1
  9569. L1F6A:  SET      6,(IY+$01)     ; set FLAGS  - Assume numeric result
  9570.         CALL    L2C8D           ; call routine ALPHA
  9571.         JR      NC,L1F89        ; if not then to DEF-FN-4 to jump to
  9572.                                 ; 'Nonsense in BASIC'
  9573.  
  9574.  
  9575.         RST     20H             ; NEXT-CHAR
  9576.         CP      $24             ; is it '$' ?
  9577.         JR      NZ,L1F7D        ; to DEF-FN-2 if not as numeric.
  9578.  
  9579.         RES     6,(IY+$01)      ; set FLAGS  - Signal string result
  9580.  
  9581.         RST     20H             ; get NEXT-CHAR
  9582.  
  9583. ;; DEF-FN-2
  9584. L1F7D:  CP      $28             ; is it '(' ?
  9585.         JR      NZ,L1FBD        ; to DEF-FN-7 'Nonsense in BASIC'
  9586.  
  9587.  
  9588.         RST     20H             ; NEXT-CHAR
  9589.         CP      $29             ; is it ')' ?
  9590.         JR      Z,L1FA6         ; to DEF-FN-6 if null argument
  9591.  
  9592. ;; DEF-FN-3
  9593. L1F86:  CALL    L2C8D           ; routine ALPHA checks that it is the expected
  9594.                                 ; alphabetic character.
  9595.  
  9596. ;; DEF-FN-4
  9597. L1F89:  JP      NC,L1C8A        ; to REPORT-C  if not
  9598.                                 ; 'Nonsense in BASIC'.
  9599.  
  9600.         EX      DE,HL           ; save pointer in DE
  9601.  
  9602.         RST     20H             ; NEXT-CHAR re-initializes HL from CH_ADD
  9603.                                 ; and advances.
  9604.         CP      $24             ; '$' ? is it a string argument.
  9605.         JR      NZ,L1F94        ; forward to DEF-FN-5 if not.
  9606.  
  9607.         EX      DE,HL           ; save pointer to '$' in DE
  9608.  
  9609.         RST     20H             ; NEXT-CHAR re-initializes HL and advances
  9610.  
  9611. ;; DEF-FN-5
  9612. L1F94:  EX      DE,HL           ; bring back pointer.
  9613.         LD      BC,$0006        ; the function requires six hidden bytes for
  9614.                                 ; each parameter passed.
  9615.                                 ; The first byte will be $0E
  9616.                                 ; then 5-byte numeric value
  9617.                                 ; or 5-byte string pointer.
  9618.  
  9619.         CALL    L1655           ; routine MAKE-ROOM creates space in program
  9620.                                 ; area.
  9621.  
  9622.         INC     HL              ; adjust HL (set by LDDR)
  9623.         INC     HL              ; to point to first location.
  9624.         LD      (HL),$0E        ; insert the 'hidden' marker.
  9625.  
  9626. ; Note. these invisible storage locations hold nothing meaningful for the
  9627. ; moment. They will be used every time the corresponding function is
  9628. ; evaluated in runtime.
  9629. ; Now consider the following character fetched earlier.
  9630.  
  9631.         CP      $2C             ; is it ',' ? (more than one parameter)
  9632.         JR      NZ,L1FA6        ; to DEF-FN-6 if not
  9633.  
  9634.  
  9635.         RST     20H             ; else NEXT-CHAR
  9636.         JR      L1F86           ; and back to DEF-FN-3
  9637.  
  9638. ; ---
  9639.  
  9640. ;; DEF-FN-6
  9641. L1FA6:  CP      $29             ; should close with a ')'
  9642.         JR      NZ,L1FBD        ; to DEF-FN-7 if not
  9643.                                 ; 'Nonsense in BASIC'
  9644.  
  9645.  
  9646.         RST     20H             ; get NEXT-CHAR
  9647.         CP      $3D             ; is it '=' ?
  9648.         JR      NZ,L1FBD        ; to DEF-FN-7 if not 'Nonsense...'
  9649.  
  9650.  
  9651.         RST     20H             ; address NEXT-CHAR
  9652.         LD      A,($5C3B)       ; get FLAGS which has been set above
  9653.         PUSH    AF              ; and preserve
  9654.  
  9655.         CALL    L24FB           ; routine SCANNING checks syntax of expression
  9656.                                 ; and also sets flags.
  9657.  
  9658.         POP     AF              ; restore previous flags
  9659.         XOR     (IY+$01)        ; xor with FLAGS - bit 6 should be same
  9660.                                 ; therefore will be reset.
  9661.         AND     $40             ; isolate bit 6.
  9662.  
  9663. ;; DEF-FN-7
  9664. L1FBD:  JP      NZ,L1C8A        ; jump back to REPORT-C if the expected result
  9665.                                 ; is not the same type.
  9666.                                 ; 'Nonsense in BASIC'
  9667.  
  9668.         CALL    L1BEE           ; routine CHECK-END will return early if
  9669.                                 ; at end of statement and move onto next
  9670.                                 ; else produce error report. >>>
  9671.  
  9672.                                 ; There will be no return to here.
  9673.  
  9674. ; -------------------------------
  9675. ; Returning early from subroutine
  9676. ; -------------------------------
  9677. ; All routines are capable of being run in two modes - syntax checking mode
  9678. ; and runtime mode.  This routine is called often to allow a routine to return
  9679. ; early if checking syntax.
  9680.  
  9681. ;; UNSTACK-Z
  9682. L1FC3:  CALL    L2530           ; routine SYNTAX-Z sets zero flag if syntax
  9683.                                 ; is being checked.
  9684.  
  9685.         POP     HL              ; drop the return address.
  9686.         RET      Z              ; return to previous call in chain if checking
  9687.                                 ; syntax.
  9688.  
  9689.         JP      (HL)            ; jump to return address as BASIC program is
  9690.                                 ; actually running.
  9691.  
  9692. ; ---------------------
  9693. ; Handle LPRINT command
  9694. ; ---------------------
  9695. ; A simple form of 'PRINT #3' although it can output to 16 streams.
  9696. ; Probably for compatibility with other BASICs particularly ZX81 BASIC.
  9697. ; An extra UDG might have been better.
  9698.  
  9699. ;; LPRINT
  9700. L1FC9:  LD      A,$03           ; the printer channel
  9701.         JR      L1FCF           ; forward to PRINT-1
  9702.  
  9703. ; ---------------------
  9704. ; Handle PRINT commands
  9705. ; ---------------------
  9706. ; The Spectrum's main stream output command.
  9707. ; The default stream is stream 2 which is normally the upper screen
  9708. ; of the computer. However the stream can be altered in range 0 - 15.
  9709.  
  9710. ;; PRINT
  9711. L1FCD:  LD      A,$02           ; the stream for the upper screen.
  9712.  
  9713. ; The LPRINT command joins here.
  9714.  
  9715. ;; PRINT-1
  9716. L1FCF:  CALL    L2530           ; routine SYNTAX-Z checks if program running
  9717.         CALL    NZ,L1601        ; routine CHAN-OPEN if so
  9718.         CALL    L0D4D           ; routine TEMPS sets temporary colours.
  9719.         CALL    L1FDF           ; routine PRINT-2 - the actual item
  9720.         CALL    L1BEE           ; routine CHECK-END gives error if not at end
  9721.                                 ; of statement
  9722.         RET                     ; and return >>>
  9723.  
  9724. ; ------------------------------------
  9725. ; this subroutine is called from above
  9726. ; and also from INPUT.
  9727.  
  9728. ;; PRINT-2
  9729. L1FDF:  RST     18H             ; GET-CHAR gets printable character
  9730.         CALL    L2045           ; routine PR-END-Z checks if more printing
  9731.         JR      Z,L1FF2         ; to PRINT-4 if not     e.g. just 'PRINT :'
  9732.  
  9733. ; This tight loop deals with combinations of positional controls and
  9734. ; print items. An early return can be made from within the loop
  9735. ; if the end of a print sequence is reached.
  9736.  
  9737. ;; PRINT-3
  9738. L1FE5:  CALL    L204E           ; routine PR-POSN-1 returns zero if more
  9739.                                 ; but returns early at this point if
  9740.                                 ; at end of statement!
  9741.                                 ;
  9742.         JR      Z,L1FE5         ; to PRINT-3 if consecutive positioners
  9743.  
  9744.         CALL    L1FFC           ; routine PR-ITEM-1 deals with strings etc.
  9745.         CALL    L204E           ; routine PR-POSN-1 for more position codes
  9746.         JR      Z,L1FE5         ; loop back to PRINT-3 if so
  9747.  
  9748. ;; PRINT-4
  9749. L1FF2:  CP      $29             ; return now if this is ')' from input-item.
  9750.                                 ; (see INPUT.)
  9751.         RET     Z               ; or continue and print carriage return in
  9752.                                 ; runtime
  9753.  
  9754. ; ---------------------
  9755. ; Print carriage return
  9756. ; ---------------------
  9757. ; This routine which continues from above prints a carriage return
  9758. ; in run-time. It is also called once from PRINT-POSN.
  9759.  
  9760. ;; PRINT-CR
  9761. L1FF5:  CALL    L1FC3           ; routine UNSTACK-Z
  9762.  
  9763.         LD      A,$0D           ; prepare a carriage return
  9764.  
  9765.         RST     10H             ; PRINT-A
  9766.         RET                     ; return
  9767.  
  9768.  
  9769. ; -----------
  9770. ; Print items
  9771. ; -----------
  9772. ; This routine deals with print items as in
  9773. ; PRINT AT 10,0;"The value of A is ";a
  9774. ; It returns once a single item has been dealt with as it is part
  9775. ; of a tight loop that considers sequences of positional and print items
  9776.  
  9777. ;; PR-ITEM-1
  9778. L1FFC:  RST     18H             ; GET-CHAR
  9779.         CP      $AC             ; is character 'AT' ?
  9780.         JR      NZ,L200E        ; forward to PR-ITEM-2 if not.
  9781.  
  9782.         CALL    L1C79           ; routine NEXT-2NUM  check for two comma
  9783.                                 ; separated numbers placing them on the
  9784.                                 ; calculator stack in runtime.
  9785.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9786.  
  9787.         CALL    L2307           ; routine STK-TO-BC get the numbers in B and C.
  9788.         LD      A,$16           ; prepare the 'at' control.
  9789.         JR      L201E           ; forward to PR-AT-TAB to print the sequence.
  9790.  
  9791. ; ---
  9792.  
  9793. ;; PR-ITEM-2
  9794. L200E:  CP      $AD             ; is character 'TAB' ?
  9795.         JR      NZ,L2024        ; to PR-ITEM-3 if not
  9796.  
  9797.  
  9798.         RST     20H             ; NEXT-CHAR to address next character
  9799.         CALL    L1C82           ; routine EXPT-1NUM
  9800.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9801.  
  9802.         CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9803.         LD      A,$17           ; prepare the 'tab' control.
  9804.  
  9805. ;; PR-AT-TAB
  9806. L201E:  RST     10H             ; PRINT-A outputs the control
  9807.  
  9808.         LD      A,C             ; first value to A
  9809.         RST     10H             ; PRINT-A outputs it.
  9810.  
  9811.         LD      A,B             ; second value
  9812.         RST     10H             ; PRINT-A
  9813.  
  9814.         RET                     ; return - item finished >>>
  9815.  
  9816. ; ---
  9817.  
  9818. ; Now consider paper 2; #2; a$
  9819.  
  9820. ;; PR-ITEM-3
  9821. L2024:  CALL    L21F2           ; routine CO-TEMP-3 will print any colour
  9822.         RET     NC              ; items - return if success.
  9823.  
  9824.         CALL    L2070           ; routine STR-ALTER considers new stream
  9825.         RET     NC              ; return if altered.
  9826.  
  9827.         CALL    L24FB           ; routine SCANNING now to evaluate expression
  9828.         CALL    L1FC3           ; routine UNSTACK-Z if not runtime.
  9829.  
  9830.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  9831.         CALL    Z,L2BF1         ; routine STK-FETCH if string.
  9832.                                 ; note no flags affected.
  9833.         JP      NZ,L2DE3        ; to PRINT-FP to print if numeric >>>
  9834.  
  9835. ; It was a string expression - start in DE, length in BC
  9836. ; Now enter a loop to print it
  9837.  
  9838. ;; PR-STRING
  9839. L203C:  LD      A,B             ; this tests if the
  9840.         OR      C               ; length is zero and sets flag accordingly.
  9841.         DEC     BC              ; this doesn't but decrements counter.
  9842.         RET     Z               ; return if zero.
  9843.  
  9844.         LD      A,(DE)          ; fetch character.
  9845.         INC     DE              ; address next location.
  9846.  
  9847.         RST     10H             ; PRINT-A.
  9848.  
  9849.         JR      L203C           ; loop back to PR-STRING.
  9850.  
  9851. ; ---------------
  9852. ; End of printing
  9853. ; ---------------
  9854. ; This subroutine returns zero if no further printing is required
  9855. ; in the current statement.
  9856. ; The first terminator is found in  escaped input items only,
  9857. ; the others in print_items.
  9858.  
  9859. ;; PR-END-Z
  9860. L2045:  CP      $29             ; is character a ')' ?
  9861.         RET     Z               ; return if so -        e.g. INPUT (p$); a$
  9862.  
  9863. ;; PR-ST-END
  9864. L2048:  CP      $0D             ; is it a carriage return ?
  9865.         RET     Z               ; return also -         e.g. PRINT a
  9866.  
  9867.         CP      $3A             ; is character a ':' ?
  9868.         RET                     ; return - zero flag will be set if so.
  9869.                                 ;                       e.g. PRINT a :
  9870.  
  9871. ; --------------
  9872. ; Print position
  9873. ; --------------
  9874. ; This routine considers a single positional character ';', ',', '''
  9875.  
  9876. ;; PR-POSN-1
  9877. L204E:  RST     18H             ; GET-CHAR
  9878.         CP      $3B             ; is it ';' ?            
  9879.                                 ; i.e. print from last position.
  9880.         JR      Z,L2067         ; forward to PR-POSN-3 if so.
  9881.                                 ; i.e. do nothing.
  9882.  
  9883.         CP      $2C             ; is it ',' ?
  9884.                                 ; i.e. print at next tabstop.
  9885.         JR      NZ,L2061        ; forward to PR-POSN-2 if anything else.
  9886.  
  9887.         CALL    L2530           ; routine SYNTAX-Z
  9888.         JR      Z,L2067         ; forward to PR-POSN-3 if checking syntax.
  9889.  
  9890.         LD      A,$06           ; prepare the 'comma' control character.
  9891.  
  9892.         RST     10H             ; PRINT-A  outputs to current channel in
  9893.                                 ; run-time.
  9894.  
  9895.         JR      L2067           ; skip to PR-POSN-3.
  9896.  
  9897. ; ---
  9898.  
  9899. ; check for newline.
  9900.  
  9901. ;; PR-POSN-2
  9902. L2061:  CP      $27             ; is character a "'" ? (newline)
  9903.         RET     NZ              ; return if no match              >>>
  9904.  
  9905.         CALL    L1FF5           ; routine PRINT-CR outputs a carriage return
  9906.                                 ; in runtime only.
  9907.  
  9908. ;; PR-POSN-3
  9909. L2067:  RST     20H             ; NEXT-CHAR to A.
  9910.         CALL    L2045           ; routine PR-END-Z checks if at end.
  9911.         JR      NZ,L206E        ; to PR-POSN-4 if not.
  9912.  
  9913.         POP     BC              ; drop return address if at end.
  9914.  
  9915. ;; PR-POSN-4
  9916. L206E:  CP      A               ; reset the zero flag.
  9917.         RET                     ; and return to loop or quit.
  9918.  
  9919. ; ------------
  9920. ; Alter stream
  9921. ; ------------
  9922. ; This routine is called from PRINT ITEMS above, and also LIST as in
  9923. ; LIST #15
  9924.  
  9925. ;; STR-ALTER
  9926. L2070:  CP      $23             ; is character '#' ?
  9927.         SCF                     ; set carry flag.
  9928.         RET     NZ              ; return if no match.
  9929.  
  9930.  
  9931.         RST      20H            ; NEXT-CHAR
  9932.         CALL    L1C82           ; routine EXPT-1NUM gets stream number
  9933.         AND     A               ; prepare to exit early with carry reset
  9934.         CALL    L1FC3           ; routine UNSTACK-Z exits early if parsing
  9935.         CALL    L1E94           ; routine FIND-INT1 gets number off stack
  9936.         CP      $10             ; must be range 0 - 15 decimal.
  9937.         JP      NC,L160E        ; jump back to REPORT-Oa if not
  9938.                                 ; 'Invalid stream'.
  9939.  
  9940.         CALL    L1601           ; routine CHAN-OPEN
  9941.         AND     A               ; clear carry - signal item dealt with.
  9942.         RET                     ; return
  9943.  
  9944. ; --------------------
  9945. ; Handle INPUT command
  9946. ; --------------------
  9947. ; This command
  9948. ;
  9949.  
  9950. ;; INPUT
  9951. L2089:  CALL    L2530           ; routine SYNTAX-Z to check if in runtime.
  9952.         JR      Z,L2096         ; forward to INPUT-1 if checking syntax.
  9953.  
  9954.         LD      A,$01           ; select channel 'K' the keyboard for input.
  9955.         CALL    L1601           ; routine CHAN-OPEN opens it.
  9956.         CALL    L0D6E           ; routine CLS-LOWER clears the lower screen
  9957.                                 ; and sets DF_SZ to two.
  9958.  
  9959. ;; INPUT-1
  9960. L2096:  LD      (IY+$02),$01    ; update TV_FLAG - signal lower screen in use
  9961.                                 ; ensuring that the correct set of system
  9962.                                 ; variables are updated and that the border
  9963.                                 ; colour is used.
  9964.  
  9965.         CALL    L20C1           ; routine IN-ITEM-1 to handle the input.
  9966.  
  9967.         CALL    L1BEE           ; routine CHECK-END will make an early exit
  9968.                                 ; if checking syntax. >>>
  9969.  
  9970. ; keyboard input has been made and it remains to adjust the upper
  9971. ; screen in case the lower two lines have been extended upwards.
  9972.  
  9973.         LD      BC,($5C88)      ; fetch S_POSN current line/column of
  9974.                                 ; the upper screen.
  9975.         LD      A,($5C6B)       ; fetch DF_SZ the display file size of
  9976.                                 ; the lower screen.
  9977.         CP      B               ; test that lower screen does not overlap
  9978.         JR      C,L20AD         ; forward to INPUT-2 if not.
  9979.  
  9980. ; the two screens overlap so adjust upper screen.
  9981.  
  9982.         LD      C,$21           ; set column of upper screen to leftmost.
  9983.         LD      B,A             ; and line to one above lower screen.
  9984.                                 ; continue forward to update upper screen
  9985.                                 ; print position.
  9986.  
  9987. ;; INPUT-2
  9988. L20AD:  LD      ($5C88),BC      ; set S_POSN update upper screen line/column.
  9989.         LD      A,$19           ; subtract from twenty five
  9990.         SUB     B               ; the new line number.
  9991.         LD      ($5C8C),A       ; and place result in SCR_CT - scroll count.
  9992.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use.
  9993.         CALL    L0DD9           ; routine CL-SET sets the print position
  9994.                                 ; system variables for the upper screen.
  9995.         JP      L0D6E           ; jump back to CLS-LOWER and make
  9996.                                 ; an indirect exit >>.
  9997.  
  9998. ; ---------------------
  9999. ; INPUT ITEM subroutine
  10000. ; ---------------------
  10001. ; This subroutine deals with the input items and print items.
  10002. ; from  the current input channel.
  10003. ; It is only called from the above INPUT routine but was obviously
  10004. ; once called from somewhere else in another context.
  10005.  
  10006. ;; IN-ITEM-1
  10007. L20C1:  CALL    L204E           ; routine PR-POSN-1 deals with a single
  10008.                                 ; position item at each call.
  10009.         JR      Z,L20C1         ; back to IN-ITEM-1 until no more in a
  10010.                                 ; sequence.
  10011.  
  10012.         CP      $28             ; is character '(' ?
  10013.         JR      NZ,L20D8        ; forward to IN-ITEM-2 if not.
  10014.  
  10015. ; any variables within braces will be treated as part, or all, of the prompt
  10016. ; instead of being used as destination variables.
  10017.  
  10018.         RST     20H             ; NEXT-CHAR
  10019.         CALL    L1FDF           ; routine PRINT-2 to output the dynamic
  10020.                                 ; prompt.
  10021.  
  10022.         RST     18H             ; GET-CHAR
  10023.         CP      $29             ; is character a matching ')' ?
  10024.         JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  10025.                                 ; 'Nonsense in BASIC'.
  10026.  
  10027.         RST     20H             ; NEXT-CHAR
  10028.         JP      L21B2           ; forward to IN-NEXT-2
  10029.  
  10030. ; ---
  10031.  
  10032. ;; IN-ITEM-2
  10033. L20D8:  CP      $CA             ; is the character the token 'LINE' ?
  10034.         JR      NZ,L20ED        ; forward to IN-ITEM-3 if not.
  10035.  
  10036.         RST     20H             ; NEXT-CHAR - variable must come next.
  10037.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10038.                                 ; address of variable to be assigned.
  10039.                                 ; or generates an error if no variable
  10040.                                 ; at this position.
  10041.  
  10042.         SET     7,(IY+$37)      ; update FLAGX  - signal handling INPUT LINE
  10043.         BIT     6,(IY+$01)      ; test FLAGS  - numeric or string result ?
  10044.         JP      NZ,L1C8A        ; jump back to REPORT-C if not string
  10045.                                 ; 'Nonsense in BASIC'.
  10046.  
  10047.         JR      L20FA           ; forward to IN-PROMPT to set up workspace.
  10048.  
  10049. ; ---
  10050.  
  10051. ; the jump was here for other variables.
  10052.  
  10053. ;; IN-ITEM-3
  10054. L20ED:  CALL     L2C8D          ; routine ALPHA checks if character is
  10055.                                 ; a suitable variable name.
  10056.         JP      NC,L21AF        ; forward to IN-NEXT-1 if not
  10057.  
  10058.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10059.                                 ; address of variable to be assigned.
  10060.         RES     7,(IY+$37)      ; update FLAGX  - signal not INPUT LINE.
  10061.  
  10062. ;; IN-PROMPT
  10063. L20FA:  CALL    L2530           ; routine SYNTAX-Z
  10064.         JP      Z,L21B2         ; forward to IN-NEXT-2 if checking syntax.
  10065.  
  10066.         CALL    L16BF           ; routine SET-WORK clears workspace.
  10067.         LD      HL,$5C71        ; point to system variable FLAGX
  10068.         RES     6,(HL)          ; signal string result.
  10069.         SET     5,(HL)          ; signal in Input Mode for editor.
  10070.         LD      BC,$0001        ; initialize space required to one for
  10071.                                 ; the carriage return.
  10072.         BIT     7,(HL)          ; test FLAGX - INPUT LINE in use ?
  10073.         JR      NZ,L211C        ; forward to IN-PR-2 if so as that is
  10074.                                 ; all the space that is required.
  10075.  
  10076.         LD      A,($5C3B)       ; load accumulator from FLAGS
  10077.         AND     $40             ; mask to test BIT 6 of FLAGS and clear
  10078.                                 ; the other bits in A.
  10079.                                 ; numeric result expected ?
  10080.         JR      NZ,L211A        ; forward to IN-PR-1 if so
  10081.  
  10082.         LD      C,$03           ; increase space to three bytes for the
  10083.                                 ; pair of surrounding quotes.
  10084.  
  10085. ;; IN-PR-1
  10086. L211A:  OR      (HL)            ; if numeric result, set bit 6 of FLAGX.
  10087.         LD      (HL),A          ; and update system variable
  10088.  
  10089. ;; IN-PR-2
  10090. L211C:  RST     30H             ; BC-SPACES opens 1 or 3 bytes in workspace
  10091.         LD      (HL),$0D        ; insert carriage return at last new location.
  10092.         LD      A,C             ; fetch the length, one or three.
  10093.         RRCA                    ; lose bit 0.
  10094.         RRCA                    ; test if quotes required.
  10095.         JR      NC,L2129        ; forward to IN-PR-3 if not.
  10096.  
  10097.         LD      A,$22           ; load the '"' character
  10098.         LD      (DE),A          ; place quote in first new location at DE.
  10099.         DEC     HL              ; decrease HL - from carriage return.
  10100.         LD      (HL),A          ; and place a quote in second location.
  10101.  
  10102. ;; IN-PR-3
  10103. L2129:  LD      ($5C5B),HL      ; set keyboard cursor K_CUR to HL
  10104.         BIT     7,(IY+$37)      ; test FLAGX  - is this INPUT LINE ??
  10105.         JR      NZ,L215E        ; forward to IN-VAR-3 if so as input will
  10106.                                 ; be accepted without checking its syntax.
  10107.  
  10108.         LD      HL,($5C5D)      ; fetch CH_ADD
  10109.         PUSH    HL              ; and save on stack.
  10110.         LD      HL,($5C3D)      ; fetch ERR_SP
  10111.         PUSH    HL              ; and save on stack
  10112.  
  10113. ;; IN-VAR-1
  10114. L213A:  LD      HL,L213A        ; address: IN-VAR-1 - this address
  10115.         PUSH    HL              ; is saved on stack to handle errors.
  10116.         BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  10117.         JR      Z,L2148         ; forward to IN-VAR-2 if not using the
  10118.                                 ; keyboard for input. (??)
  10119.  
  10120.         LD      ($5C3D),SP      ; set ERR_SP to point to IN-VAR-1 on stack.
  10121.  
  10122. ;; IN-VAR-2
  10123. L2148:  LD      HL,($5C61)      ; set HL to WORKSP - start of workspace.
  10124.         CALL    L11A7           ; routine REMOVE-FP removes floating point
  10125.                                 ; forms when looping in error condition.
  10126.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK' cancelling the error.
  10127.                                 ; but X_PTR causes flashing error marker
  10128.                                 ; to be displayed at each call to the editor.
  10129.         CALL    L0F2C           ; routine EDITOR allows input to be entered
  10130.                                 ; or corrected if this is second time around.
  10131.  
  10132. ; if we pass to next then there are no system errors
  10133.  
  10134.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax
  10135.         CALL    L21B9           ; routine IN-ASSIGN checks syntax using
  10136.                                 ; the VAL-FET-2 and powerful SCANNING routines.
  10137.                                 ; any syntax error and its back to IN-VAR-1.
  10138.                                 ; but with the flashing error marker showing
  10139.                                 ; where the error is.
  10140.                                 ; Note. the syntax of string input has to be
  10141.                                 ; checked as the user may have removed the
  10142.                                 ; bounding quotes or escaped them as with
  10143.                                 ; "hat" + "stand" for example.
  10144. ; proceed if syntax passed.
  10145.  
  10146.         JR      L2161           ; jump forward to IN-VAR-4
  10147.  
  10148. ; ---
  10149.  
  10150. ; the jump was to here when using INPUT LINE.
  10151.  
  10152. ;; IN-VAR-3
  10153. L215E:  CALL    L0F2C           ; routine EDITOR is called for input
  10154.  
  10155. ; when ENTER received rejoin other route but with no syntax check.
  10156.  
  10157. ; INPUT and INPUT LINE converge here.
  10158.  
  10159. ;; IN-VAR-4
  10160. L2161:  LD      (IY+$22),$00    ; set K_CUR_hi to a low value so that the cursor
  10161.                                 ; no longer appears in the input line.
  10162.  
  10163.         CALL    L21D6           ; routine IN-CHAN-K tests if the keyboard
  10164.                                 ; is being used for input.
  10165.         JR      NZ,L2174        ; forward to IN-VAR-5 if using another input
  10166.                                 ; channel.
  10167.  
  10168. ; continue here if using the keyboard.
  10169.  
  10170.         CALL    L111D           ; routine ED-COPY overprints the edit line
  10171.                                 ; to the lower screen. The only visible
  10172.                                 ; affect is that the cursor disappears.
  10173.                                 ; if you're inputting more than one item in
  10174.                                 ; a statement then that becomes apparent.
  10175.  
  10176.         LD      BC,($5C82)      ; fetch line and column from ECHO_E
  10177.         CALL    L0DD9           ; routine CL-SET sets S-POSNL to those
  10178.                                 ; values.
  10179.  
  10180. ; if using another input channel rejoin here.
  10181.  
  10182. ;; IN-VAR-5
  10183. L2174:  LD      HL,$5C71        ; point HL to FLAGX
  10184.         RES     5,(HL)          ; signal not in input mode
  10185.         BIT     7,(HL)          ; is this INPUT LINE ?
  10186.         RES     7,(HL)          ; cancel the bit anyway.
  10187.         JR      NZ,L219B        ; forward to IN-VAR-6 if INPUT LINE.
  10188.  
  10189.         POP     HL              ; drop the looping address
  10190.         POP     HL              ; drop the the address of previous
  10191.                                 ; error handler.
  10192.         LD      ($5C3D),HL      ; set ERR_SP to point to it.
  10193.         POP     HL              ; drop original CH_ADD which points to
  10194.                                 ; INPUT command in BASIC line.
  10195.         LD      ($5C5F),HL      ; save in X_PTR while input is assigned.
  10196.         SET     7,(IY+$01)      ; update FLAGS - Signal running program
  10197.         CALL    L21B9           ; routine IN-ASSIGN is called again
  10198.                                 ; this time the variable will be assigned
  10199.                                 ; the input value without error.
  10200.                                 ; Note. the previous example now
  10201.                                 ; becomes "hatstand"
  10202.  
  10203.         LD      HL,($5C5F)      ; fetch stored CH_ADD value from X_PTR.
  10204.         LD      (IY+$26),$00    ; set X_PTR_hi so that iy is no longer relevant.
  10205.         LD      ($5C5D),HL      ; put restored value back in CH_ADD
  10206.         JR      L21B2           ; forward to IN-NEXT-2 to see if anything
  10207.                                 ; more in the INPUT list.
  10208.  
  10209. ; ---
  10210.  
  10211. ; the jump was to here with INPUT LINE only
  10212.  
  10213. ;; IN-VAR-6
  10214. L219B:  LD      HL,($5C63)      ; STKBOT points to the end of the input.
  10215.         LD      DE,($5C61)      ; WORKSP points to the beginning.
  10216.         SCF                     ; prepare for true subtraction.
  10217.         SBC     HL,DE           ; subtract to get length
  10218.         LD      B,H             ; transfer it to
  10219.         LD      C,L             ; the BC register pair.
  10220.         CALL    L2AB2           ; routine STK-STO-$ stores parameters on
  10221.                                 ; the calculator stack.
  10222.         CALL    L2AFF           ; routine LET assigns it to destination.
  10223.         JR      L21B2           ; forward to IN-NEXT-2 as print items
  10224.                                 ; not allowed with INPUT LINE.
  10225.                                 ; Note. that "hat" + "stand" will, for
  10226.                                 ; example, be unchanged as also would
  10227.                                 ; 'PRINT "Iris was here"'.
  10228.  
  10229. ; ---
  10230.  
  10231. ; the jump was to here when ALPHA found more items while looking for
  10232. ; a variable name.
  10233.  
  10234. ;; IN-NEXT-1
  10235. L21AF:  CALL    L1FFC           ; routine PR-ITEM-1 considers further items.
  10236.  
  10237. ;; IN-NEXT-2
  10238. L21B2:  CALL    L204E           ; routine PR-POSN-1 handles a position item.
  10239.         JP      Z,L20C1         ; jump back to IN-ITEM-1 if the zero flag
  10240.                                 ; indicates more items are present.
  10241.  
  10242.         RET                     ; return.
  10243.  
  10244. ; ---------------------------
  10245. ; INPUT ASSIGNMENT Subroutine
  10246. ; ---------------------------
  10247. ; This subroutine is called twice from the INPUT command when normal
  10248. ; keyboard input is assigned. On the first occasion syntax is checked
  10249. ; using SCANNING. The final call with the syntax flag reset is to make
  10250. ; the assignment.
  10251.  
  10252. ;; IN-ASSIGN
  10253. L21B9:  LD      HL,($5C61)      ; fetch WORKSP start of input
  10254.         LD      ($5C5D),HL      ; set CH_ADD to first character
  10255.  
  10256.         RST     18H             ; GET-CHAR ignoring leading white-space.
  10257.         CP      $E2             ; is it 'STOP'
  10258.         JR      Z,L21D0         ; forward to IN-STOP if so.
  10259.  
  10260.         LD      A,($5C71)       ; load accumulator from FLAGX
  10261.         CALL    L1C59           ; routine VAL-FET-2 makes assignment
  10262.                                 ; or goes through the motions if checking
  10263.                                 ; syntax. SCANNING is used.
  10264.  
  10265.         RST     18H             ; GET-CHAR
  10266.         CP      $0D             ; is it carriage return ?
  10267.         RET     Z               ; return if so
  10268.                                 ; either syntax is OK
  10269.                                 ; or assignment has been made.
  10270.  
  10271. ; if another character was found then raise an error.
  10272. ; User doesn't see report but the flashing error marker
  10273. ; appears in the lower screen.
  10274.  
  10275. ;; REPORT-Cb
  10276. L21CE:  RST     08H             ; ERROR-1
  10277.         DB    $0B             ; Error Report: Nonsense in BASIC
  10278.  
  10279. ;; IN-STOP
  10280. L21D0:  CALL    L2530           ; routine SYNTAX-Z (UNSTACK-Z?)
  10281.         RET     Z               ; return if checking syntax
  10282.                                 ; as user wouldn't see error report.
  10283.                                 ; but generate visible error report
  10284.                                 ; on second invocation.
  10285.  
  10286. ;; REPORT-H
  10287. L21D4:  RST     08H             ; ERROR-1
  10288.         DB    $10             ; Error Report: STOP in INPUT
  10289.  
  10290. ; ------------------
  10291. ; Test for channel K
  10292. ; ------------------
  10293. ; This subroutine is called once from the keyboard
  10294. ; INPUT command to check if the input routine in
  10295. ; use is the one for the keyboard.
  10296.  
  10297. ;; IN-CHAN-K
  10298. L21D6:  LD      HL,($5C51)      ; fetch address of current channel CURCHL
  10299.         INC     HL              ;
  10300.         INC     HL              ; advance past
  10301.         INC     HL              ; input and
  10302.         INC     HL              ; output streams
  10303.         LD      A,(HL)          ; fetch the channel identifier.
  10304.         CP      $4B             ; test for 'K'
  10305.         RET                     ; return with zero set if keyboard is use.
  10306.  
  10307. ; --------------------
  10308. ; Colour Item Routines
  10309. ; --------------------
  10310. ;
  10311. ; These routines have 3 entry points -
  10312. ; 1) CO-TEMP-2 to handle a series of embedded Graphic colour items.
  10313. ; 2) CO-TEMP-3 to handle a single embedded print colour item.
  10314. ; 3) CO TEMP-4 to handle a colour command such as FLASH 1
  10315. ;
  10316. ; "Due to a bug, if you bring in a peripheral channel and later use a colour
  10317. ;  statement, colour controls will be sent to it by mistake." - Steven Vickers
  10318. ;  Pitman Pocket Guide, 1984.
  10319. ;
  10320. ; To be fair, this only applies if the last channel was other than 'K', 'S'
  10321. ; or 'P', which are all that are supported by this ROM, but if that last
  10322. ; channel was a microdrive file, network channel etc. then
  10323. ; PAPER 6; CLS will not turn the screen yellow and
  10324. ; CIRCLE INK 2; 128,88,50 will not draw a red circle.
  10325. ;
  10326. ; This bug does not apply to embedded PRINT items as it is quite permissible
  10327. ; to mix stream altering commands and colour items.
  10328. ; The fix therefore would be to ensure that CLASS-07 and CLASS-09 make
  10329. ; channel 'S' the current channel when not checking syntax.
  10330. ; -----------------------------------------------------------------
  10331.  
  10332. ;; CO-TEMP-1
  10333. L21E1:  RST     20H             ; NEXT-CHAR
  10334.  
  10335. ; -> Entry point from CLASS-09. Embedded Graphic colour items.
  10336. ; e.g. PLOT INK 2; PAPER 8; 128,88
  10337. ; Loops till all colour items output, finally addressing the coordinates.
  10338.  
  10339. ;; CO-TEMP-2
  10340. L21E2:  CALL    L21F2           ; routine CO-TEMP-3 to output colour control.
  10341.         RET     C               ; return if nothing more to output. ->
  10342.  
  10343.  
  10344.         RST     18H             ; GET-CHAR
  10345.         CP      $2C             ; is it ',' separator ?
  10346.         JR      Z,L21E1         ; back if so to CO-TEMP-1
  10347.  
  10348.         CP      $3B             ; is it ';' separator ?
  10349.         JR      Z,L21E1         ; back to CO-TEMP-1 for more.
  10350.  
  10351.         JP      L1C8A           ; to REPORT-C (REPORT-Cb is within range)
  10352.                                 ; 'Nonsense in BASIC'
  10353.  
  10354. ; -------------------
  10355. ; CO-TEMP-3
  10356. ; -------------------
  10357. ; -> this routine evaluates and outputs a colour control and parameter.
  10358. ; It is called from above and also from PR-ITEM-3 to handle a single embedded
  10359. ; print item e.g. PRINT PAPER 6; "Hi". In the latter case, the looping for
  10360. ; multiple items is within the PR-ITEM routine.
  10361. ; It is quite permissible to send these to any stream.
  10362.  
  10363. ;; CO-TEMP-3
  10364. L21F2:  CP      $D9             ; is it 'INK' ?
  10365.         RET     C               ; return if less.
  10366.  
  10367.         CP      $DF             ; compare with 'OUT'
  10368.         CCF                     ; Complement Carry Flag
  10369.         RET     C               ; return if greater than 'OVER', $DE.
  10370.  
  10371.         PUSH    AF              ; save the colour token.
  10372.  
  10373.         RST     20H             ; address NEXT-CHAR
  10374.         POP     AF              ; restore token and continue.
  10375.  
  10376. ; -> this entry point used by CLASS-07. e.g. the command PAPER 6.
  10377.  
  10378. ;; CO-TEMP-4
  10379. L21FC:  SUB     $C9             ; reduce to control character $10 (INK)
  10380.                                 ; thru $15 (OVER).
  10381.         PUSH    AF              ; save control.
  10382.         CALL    L1C82           ; routine EXPT-1NUM stacks addressed
  10383.                                 ; parameter on calculator stack.
  10384.         POP     AF              ; restore control.
  10385.         AND     A               ; clear carry
  10386.  
  10387.         CALL    L1FC3           ; routine UNSTACK-Z returns if checking syntax.
  10388.  
  10389.         PUSH    AF              ; save again
  10390.         CALL    L1E94           ; routine FIND-INT1 fetches parameter to A.
  10391.         LD      D,A             ; transfer now to D
  10392.         POP     AF              ; restore control.
  10393.  
  10394.         RST     10H             ; PRINT-A outputs the control to current
  10395.                                 ; channel.
  10396.         LD      A,D             ; transfer parameter to A.
  10397.  
  10398.         RST     10H             ; PRINT-A outputs parameter.
  10399.         RET                     ; return. ->
  10400.  
  10401. ; -------------------------------------------------------------------------
  10402. ;
  10403. ;         {fl}{br}{   paper   }{  ink    }    The temporary colour attributes
  10404. ;          ___ ___ ___ ___ ___ ___ ___ ___    system variable.
  10405. ; ATTR_T  |   |   |   |   |   |   |   |   |
  10406. ;         |   |   |   |   |   |   |   |   |
  10407. ; 23695   |___|___|___|___|___|___|___|___|
  10408. ;           7   6   5   4   3   2   1   0
  10409. ;
  10410. ;
  10411. ;         {fl}{br}{   paper   }{  ink    }    The temporary mask used for
  10412. ;          ___ ___ ___ ___ ___ ___ ___ ___    transparent colours. Any bit
  10413. ; MASK_T  |   |   |   |   |   |   |   |   |   that is 1 shows that the
  10414. ;         |   |   |   |   |   |   |   |   |   corresponding attribute is
  10415. ; 23696   |___|___|___|___|___|___|___|___|   taken not from ATTR-T but from
  10416. ;           7   6   5   4   3   2   1   0     what is already on the screen.
  10417. ;
  10418. ;
  10419. ;         {paper9 }{ ink9 }{ inv1 }{ over1}   The print flags. Even bits are
  10420. ;          ___ ___ ___ ___ ___ ___ ___ ___    temporary flags. The odd bits
  10421. ; P_FLAG  |   |   |   |   |   |   |   |   |   are the permanent flags.
  10422. ;         | p | t | p | t | p | t | p | t |
  10423. ; 23697   |___|___|___|___|___|___|___|___|
  10424. ;           7   6   5   4   3   2   1   0
  10425. ;
  10426. ; -----------------------------------------------------------------------
  10427.  
  10428. ; ------------------------------------
  10429. ;  The colour system variable handler.
  10430. ; ------------------------------------
  10431. ; This is an exit branch from PO-1-OPER, PO-2-OPER
  10432. ; A holds control $10 (INK) to $15 (OVER)
  10433. ; D holds parameter 0-9 for ink/paper 0,1 or 8 for bright/flash,
  10434. ; 0 or 1 for over/inverse.
  10435.  
  10436. ;; CO-TEMP-5
  10437. L2211:  SUB     $11             ; reduce range $FF-$04
  10438.         ADC     A,$00           ; add in carry if INK
  10439.         JR      Z,L2234         ; forward to CO-TEMP-7 with INK and PAPER.
  10440.  
  10441.         SUB     $02             ; reduce range $FF-$02
  10442.         ADC     A,$00           ; add carry if FLASH
  10443.         JR      Z,L2273         ; forward to CO-TEMP-C with FLASH and BRIGHT.
  10444.  
  10445.         CP      $01             ; is it 'INVERSE' ?
  10446.         LD      A,D             ; fetch parameter for INVERSE/OVER
  10447.         LD      B,$01           ; prepare OVER mask setting bit 0.
  10448.         JR      NZ,L2228        ; forward to CO-TEMP-6 if OVER
  10449.  
  10450.         RLCA                    ; shift bit 0
  10451.         RLCA                    ; to bit 2
  10452.         LD      B,$04           ; set bit 2 of mask for inverse.
  10453.  
  10454. ;; CO-TEMP-6
  10455. L2228:  LD      C,A             ; save the A
  10456.         LD      A,D             ; re-fetch parameter
  10457.         CP      $02             ; is it less than 2
  10458.         JR      NC,L2244        ; to REPORT-K if not 0 or 1.
  10459.                                 ; 'Invalid colour'.
  10460.  
  10461.         LD      A,C             ; restore A
  10462.         LD      HL,$5C91        ; address system variable P_FLAG
  10463.         JR      L226C           ; forward to exit via routine CO-CHANGE
  10464.  
  10465. ; ---
  10466.  
  10467. ; the branch was here with INK/PAPER and carry set for INK.
  10468.  
  10469. ;; CO-TEMP-7
  10470. L2234:  LD      A,D             ; fetch parameter
  10471.         LD      B,$07           ; set ink mask 00000111
  10472.         JR      C,L223E         ; forward to CO-TEMP-8 with INK
  10473.  
  10474.         RLCA                    ; shift bits 0-2
  10475.         RLCA                    ; to
  10476.         RLCA                    ; bits 3-5
  10477.         LD      B,$38           ; set paper mask 00111000
  10478.  
  10479. ; both paper and ink rejoin here
  10480.  
  10481. ;; CO-TEMP-8
  10482. L223E:  LD      C,A             ; value to C
  10483.         LD      A,D             ; fetch parameter
  10484.         CP      $0A             ; is it less than 10d ?
  10485.         JR      C,L2246         ; forward to CO-TEMP-9 if so.
  10486.  
  10487. ; ink 10 etc. is not allowed.
  10488.  
  10489. ;; REPORT-K
  10490. L2244:  RST     08H             ; ERROR-1
  10491.         DB    $13             ; Error Report: Invalid colour
  10492.  
  10493. ;; CO-TEMP-9
  10494. L2246:  LD      HL,$5C8F        ; address system variable ATTR_T initially.
  10495.         CP      $08             ; compare with 8
  10496.         JR      C,L2258         ; forward to CO-TEMP-B with 0-7.
  10497.  
  10498.         LD      A,(HL)          ; fetch temporary attribute as no change.
  10499.         JR      Z,L2257         ; forward to CO-TEMP-A with INK/PAPER 8
  10500.  
  10501. ; it is either ink 9 or paper 9 (contrasting)
  10502.  
  10503.         OR      B               ; or with mask to make white
  10504.         CPL                     ; make black and change other to dark
  10505.         AND     $24             ; 00100100
  10506.         JR      Z,L2257         ; forward to CO-TEMP-A if black and
  10507.                                 ; originally light.
  10508.  
  10509.         LD      A,B             ; else just use the mask (white)
  10510.  
  10511. ;; CO-TEMP-A
  10512. L2257:  LD      C,A             ; save A in C
  10513.  
  10514. ;; CO-TEMP-B
  10515. L2258:  LD      A,C             ; load colour to A
  10516.         CALL    L226C           ; routine CO-CHANGE addressing ATTR-T
  10517.  
  10518.         LD      A,$07           ; put 7 in accumulator
  10519.         CP      D               ; compare with parameter
  10520.         SBC     A,A             ; $00 if 0-7, $FF if 8
  10521.         CALL    L226C           ; routine CO-CHANGE addressing MASK-T
  10522.                                 ; mask returned in A.
  10523.  
  10524. ; now consider P-FLAG.
  10525.  
  10526.         RLCA                    ; 01110000 or 00001110
  10527.         RLCA                    ; 11100000 or 00011100
  10528.         AND     $50             ; 01000000 or 00010000  (AND 01010000)
  10529.         LD      B,A             ; transfer to mask
  10530.         LD      A,$08           ; load A with 8
  10531.         CP      D               ; compare with parameter
  10532.         SBC     A,A             ; $FF if was 9,  $00 if 0-8
  10533.                                 ; continue while addressing P-FLAG
  10534.                                 ; setting bit 4 if ink 9
  10535.                                 ; setting bit 6 if paper 9
  10536.  
  10537. ; -----------------------
  10538. ; Handle change of colour
  10539. ; -----------------------
  10540. ; This routine addresses a system variable ATTR_T, MASK_T or P-FLAG in HL.
  10541. ; colour value in A, mask in B.
  10542.  
  10543. ;; CO-CHANGE
  10544. L226C:  XOR     (HL)            ; impress bits specified
  10545.         AND     B               ; by mask
  10546.         XOR     (HL)            ; on system variable.
  10547.         LD      (HL),A          ; update system variable.
  10548.         INC     HL              ; address next location.
  10549.         LD      A,B             ; put current value of mask in A
  10550.         RET                     ; return.
  10551.  
  10552. ; ---
  10553.  
  10554. ; the branch was here with flash and bright
  10555.  
  10556. ;; CO-TEMP-C
  10557. L2273:  SBC     A,A             ; set zero flag for bright.
  10558.         LD      A,D             ; fetch original parameter 0,1 or 8
  10559.         RRCA                    ; rotate bit 0 to bit 7
  10560.         LD      B,$80           ; mask for flash 10000000
  10561.         JR      NZ,L227D        ; forward to CO-TEMP-D if flash
  10562.  
  10563.         RRCA                    ; rotate bit 7 to bit 6
  10564.         LD      B,$40           ; mask for bright 01000000
  10565.  
  10566. ;; CO-TEMP-D
  10567. L227D:  LD      C,A             ; store value in C
  10568.         LD      A,D             ; fetch parameter
  10569.         CP      $08             ; compare with 8
  10570.         JR      Z,L2287         ; forward to CO-TEMP-E if 8
  10571.  
  10572.         CP      $02             ; test if 0 or 1
  10573.         JR      NC,L2244        ; back to REPORT-K if not
  10574.                                 ; 'Invalid colour'
  10575.  
  10576. ;; CO-TEMP-E
  10577. L2287:  LD      A,C             ; value to A
  10578.         LD      HL,$5C8F        ; address ATTR_T
  10579.         CALL    L226C           ; routine CO-CHANGE addressing ATTR_T
  10580.         LD      A,C             ; fetch value
  10581.         RRCA                    ; for flash8/bright8 complete
  10582.         RRCA                    ; rotations to put set bit in
  10583.         RRCA                    ; bit 7 (flash) bit 6 (bright)
  10584.         JR      L226C           ; back to CO-CHANGE addressing MASK_T
  10585.                                 ; and indirect return.
  10586.  
  10587. ; ---------------------
  10588. ; Handle BORDER command
  10589. ; ---------------------
  10590. ; Command syntax example: BORDER 7
  10591. ; This command routine sets the border to one of the eight colours.
  10592. ; The colours used for the lower screen are based on this.
  10593.  
  10594. ;; BORDER
  10595. L2294:  CALL    L1E94           ; routine FIND-INT1
  10596.         CP      $08             ; must be in range 0 (black) to 7 (white)
  10597.         JR      NC,L2244        ; back to REPORT-K if not
  10598.                                 ; 'Invalid colour'.
  10599.  
  10600.         OUT     ($FE),A         ; outputting to port effects an immediate
  10601.                                 ; change.
  10602.         RLCA                    ; shift the colour to
  10603.         RLCA                    ; the paper bits setting the
  10604.         RLCA                    ; ink colour black.
  10605.         BIT     5,A             ; is the number light coloured ?
  10606.                                 ; i.e. in the range green to white.
  10607.         JR      NZ,L22A6        ; skip to BORDER-1 if so
  10608.  
  10609.         XOR     $07             ; make the ink white.
  10610.  
  10611. ;; BORDER-1
  10612. L22A6:  LD      ($5C48),A       ; update BORDCR with new paper/ink
  10613.         RET                     ; return.
  10614.  
  10615. ; -----------------
  10616. ; Get pixel address
  10617. ; -----------------
  10618. ;
  10619. ;
  10620.  
  10621. ;; PIXEL-ADD
  10622. L22AA:  LD      A,$AF           ; load with 175 decimal.
  10623.         SUB     B               ; subtract the y value.
  10624.         JP      C,L24F9         ; jump forward to REPORT-Bc if greater.
  10625.                                 ; 'Integer out of range'
  10626.  
  10627. ; the high byte is derived from Y only.
  10628. ; the first 3 bits are always 010
  10629. ; the next 2 bits denote in which third of the screen the byte is.
  10630. ; the last 3 bits denote in which of the 8 scan lines within a third
  10631. ; the byte is located. There are 24 discrete values.
  10632.  
  10633.  
  10634.         LD      B,A             ; the line number from top of screen to B.
  10635.         AND     A               ; clear carry (already clear)
  10636.         RRA                     ;                     0xxxxxxx
  10637.         SCF                     ; set carry flag
  10638.         RRA                     ;                     10xxxxxx
  10639.         AND     A               ; clear carry flag
  10640.         RRA                     ;                     010xxxxx
  10641.  
  10642.         XOR     B               ;
  10643.         AND     $F8             ; keep the top 5 bits 11111000
  10644.         XOR     B               ;                     010xxbbb
  10645.         LD      H,A             ; transfer high byte to H.
  10646.  
  10647. ; the low byte is derived from both X and Y.
  10648.  
  10649.         LD      A,C             ; the x value 0-255.
  10650.         RLCA                    ;
  10651.         RLCA                    ;
  10652.         RLCA                    ;
  10653.         XOR     B               ; the y value
  10654.         AND     $C7             ; apply mask             11000111
  10655.         XOR     B               ; restore unmasked bits  xxyyyxxx
  10656.         RLCA                    ; rotate to              xyyyxxxx
  10657.         RLCA                    ; required position.     yyyxxxxx
  10658.         LD      L,A             ; low byte to L.
  10659.  
  10660. ; finally form the pixel position in A.
  10661.  
  10662.         LD      A,C             ; x value to A
  10663.         AND     $07             ; mod 8
  10664.         RET                     ; return
  10665.  
  10666. ; ----------------
  10667. ; Point Subroutine
  10668. ; ----------------
  10669. ; The point subroutine is called from s-point via the scanning functions
  10670. ; table.
  10671.  
  10672. ;; POINT-SUB
  10673. L22CB:  CALL    L2307           ; routine STK-TO-BC
  10674.         CALL    L22AA           ; routine PIXEL-ADD finds address of pixel.
  10675.         LD      B,A             ; pixel position to B, 0-7.
  10676.         INC     B               ; increment to give rotation count 1-8.
  10677.         LD      A,(HL)          ; fetch byte from screen.
  10678.  
  10679. ;; POINT-LP
  10680. L22D4:  RLCA                    ; rotate and loop back
  10681.         DJNZ    L22D4           ; to POINT-LP until pixel at right.
  10682.  
  10683.         AND      $01            ; test to give zero or one.
  10684.         JP      L2D28           ; jump forward to STACK-A to save result.
  10685.  
  10686. ; -------------------
  10687. ; Handle PLOT command
  10688. ; -------------------
  10689. ; Command Syntax example: PLOT 128,88
  10690. ;
  10691.  
  10692. ;; PLOT
  10693. L22DC:  CALL    L2307           ; routine STK-TO-BC
  10694.         CALL    L22E5           ; routine PLOT-SUB
  10695.         JP      L0D4D           ; to TEMPS
  10696.  
  10697. ; -------------------
  10698. ; The Plot subroutine
  10699. ; -------------------
  10700. ; A screen byte holds 8 pixels so it is necessary to rotate a mask
  10701. ; into the correct position to leave the other 7 pixels unaffected.
  10702. ; However all 64 pixels in the character cell take any embedded colour
  10703. ; items.
  10704. ; A pixel can be reset (inverse 1), toggled (over 1), or set ( with inverse
  10705. ; and over switches off). With both switches on, the byte is simply put
  10706. ; back on the screen though the colours may change.
  10707.  
  10708. ;; PLOT-SUB
  10709. L22E5:  LD      ($5C7D),BC      ; store new x/y values in COORDS
  10710.         CALL    L22AA           ; routine PIXEL-ADD gets address in HL,
  10711.                                 ; count from left 0-7 in B.
  10712.         LD      B,A             ; transfer count to B.
  10713.         INC     B               ; increase 1-8.
  10714.         LD      A,$FE           ; 11111110 in A.
  10715.  
  10716. ;; PLOT-LOOP
  10717. L22F0:  RRCA                    ; rotate mask.
  10718.         DJNZ    L22F0           ; to PLOT-LOOP until B circular rotations.
  10719.  
  10720.         LD      B,A             ; load mask to B
  10721.         LD      A,(HL)          ; fetch screen byte to A
  10722.  
  10723.         LD      C,(IY+$57)      ; P_FLAG to C
  10724.         BIT     0,C             ; is it to be OVER 1 ?
  10725.         JR      NZ,L22FD        ; forward to PL-TST-IN if so.
  10726.  
  10727. ; was over 0
  10728.  
  10729.         AND     B               ; combine with mask to blank pixel.
  10730.  
  10731. ;; PL-TST-IN
  10732. L22FD:  BIT     2,C             ; is it inverse 1 ?
  10733.         JR      NZ,L2303        ; to PLOT-END if so.
  10734.  
  10735.         XOR     B               ; switch the pixel
  10736.         CPL                     ; restore other 7 bits
  10737.  
  10738. ;; PLOT-END
  10739. L2303:  LD      (HL),A          ; load byte to the screen.
  10740.         JP      L0BDB           ; exit to PO-ATTR to set colours for cell.
  10741.  
  10742. ; ------------------------------
  10743. ; Put two numbers in BC register
  10744. ; ------------------------------
  10745. ;
  10746. ;
  10747.  
  10748. ;; STK-TO-BC
  10749. L2307:  CALL    L2314           ; routine STK-TO-A
  10750.         LD      B,A             ;
  10751.         PUSH    BC              ;
  10752.         CALL    L2314           ; routine STK-TO-A
  10753.         LD      E,C             ;
  10754.         POP     BC              ;
  10755.         LD      D,C             ;
  10756.         LD      C,A             ;
  10757.         RET                     ;
  10758.  
  10759. ; -----------------------
  10760. ; Put stack in A register
  10761. ; -----------------------
  10762. ; This routine puts the last value on the calculator stack into the accumulator
  10763. ; deleting the last value.
  10764.  
  10765. ;; STK-TO-A
  10766. L2314:  CALL    L2DD5           ; routine FP-TO-A compresses last value into
  10767.                                 ; accumulator. e.g. PI would become 3.
  10768.                                 ; zero flag set if positive.
  10769.         JP      C,L24F9         ; jump forward to REPORT-Bc if >= 255.5.
  10770.  
  10771.         LD      C,$01           ; prepare a positive sign byte.
  10772.         RET     Z               ; return if FP-TO-BC indicated positive.
  10773.  
  10774.         LD      C,$FF           ; prepare negative sign byte and
  10775.         RET                     ; return.
  10776.  
  10777.  
  10778. ; ---------------------
  10779. ; Handle CIRCLE command
  10780. ; ---------------------
  10781. ;
  10782. ; syntax has been partly checked using the class for draw command.
  10783.  
  10784. ;; CIRCLE
  10785. L2320:  RST     18H             ; GET-CHAR
  10786.         CP      $2C             ; is it required comma ?
  10787.         JP      NZ,L1C8A        ; jump to REPORT-C if not
  10788.  
  10789.  
  10790.         RST     20H             ; NEXT-CHAR
  10791.         CALL    L1C82           ; routine EXPT-1NUM fetches radius
  10792.         CALL    L1BEE           ; routine CHECK-END will return here if
  10793.                                 ; nothing follows command.
  10794.  
  10795.         RST     28H             ;; FP-CALC
  10796.         DB    $2A             ;;abs           ; make radius positive
  10797.         DB    $3D             ;;re-stack      ; in full floating point form
  10798.         DB    $38             ;;end-calc
  10799.  
  10800.         LD      A,(HL)          ; fetch first floating point byte
  10801.         CP      $81             ; compare to one
  10802.         JR      NC,L233B        ; forward to C-R-GRE-1 if circle radius
  10803.                                 ; is greater than one.
  10804.  
  10805.  
  10806.         RST     28H             ;; FP-CALC
  10807.         DB    $02             ;;delete        ; delete the radius from stack.
  10808.         DB    $38             ;;end-calc
  10809.  
  10810.         JR      L22DC           ; to PLOT to just plot x,y.
  10811.  
  10812. ; ---
  10813.  
  10814.  
  10815. ;; C-R-GRE-1
  10816. L233B:  RST     28H             ;; FP-CALC      ; x, y, r
  10817.         DB    $A3             ;;stk-pi/2      ; x, y, r, pi/2.
  10818.         DB    $38             ;;end-calc
  10819.  
  10820.         LD      (HL),$83        ;               ; x, y, r, 2*PI
  10821.  
  10822.         RST     28H             ;; FP-CALC
  10823.         DB    $C5             ;;st-mem-5      ; store 2*PI in mem-5
  10824.         DB    $02             ;;delete        ; x, y, z.
  10825.         DB    $38             ;;end-calc
  10826.  
  10827.         CALL    L247D           ; routine CD-PRMS1
  10828.         PUSH    BC              ;
  10829.  
  10830.         RST     28H             ;; FP-CALC
  10831.         DB    $31             ;;duplicate
  10832.         DB    $E1             ;;get-mem-1
  10833.         DB    $04             ;;multiply
  10834.         DB    $38             ;;end-calc
  10835.  
  10836.         LD      A,(HL)          ;
  10837.         CP      $80             ;
  10838.         JR      NC,L235A        ; to C-ARC-GE1
  10839.  
  10840.  
  10841.         RST     28H             ;; FP-CALC
  10842.         DB    $02             ;;delete
  10843.         DB    $02             ;;delete
  10844.         DB    $38             ;;end-calc
  10845.  
  10846.         POP     BC              ;
  10847.         JP      L22DC           ; JUMP to PLOT
  10848.  
  10849. ; ---
  10850.  
  10851.  
  10852. ;; C-ARC-GE1
  10853. L235A:  RST     28H             ;; FP-CALC
  10854.         DB    $C2             ;;st-mem-2
  10855.         DB    $01             ;;exchange
  10856.         DB    $C0             ;;st-mem-0
  10857.         DB    $02             ;;delete
  10858.         DB    $03             ;;subtract
  10859.         DB    $01             ;;exchange
  10860.         DB    $E0             ;;get-mem-0
  10861.         DB    $0F             ;;addition
  10862.         DB    $C0             ;;st-mem-0
  10863.         DB    $01             ;;exchange
  10864.         DB    $31             ;;duplicate
  10865.         DB    $E0             ;;get-mem-0
  10866.         DB    $01             ;;exchange
  10867.         DB    $31             ;;duplicate
  10868.         DB    $E0             ;;get-mem-0
  10869.         DB    $A0             ;;stk-zero
  10870.         DB    $C1             ;;st-mem-1
  10871.         DB    $02             ;;delete
  10872.         DB    $38             ;;end-calc
  10873.  
  10874.         INC     (IY+$62)        ; MEM-2-1st
  10875.         CALL     L1E94          ; routine FIND-INT1
  10876.         LD      L,A             ;
  10877.         PUSH    HL              ;
  10878.         CALL    L1E94           ; routine FIND-INT1
  10879.         POP     HL              ;
  10880.         LD      H,A             ;
  10881.         LD      ($5C7D),HL      ; COORDS
  10882.         POP     BC              ;
  10883.         JP      L2420           ; to DRW-STEPS
  10884.  
  10885.  
  10886. ; -------------------
  10887. ; Handle DRAW command
  10888. ; -------------------
  10889. ;
  10890. ;
  10891.  
  10892. ;; DRAW
  10893. L2382:  RST     18H             ; GET-CHAR
  10894.         CP      $2C             ;
  10895.         JR      Z,L238D         ; to DR-3-PRMS
  10896.  
  10897.         CALL    L1BEE           ; routine CHECK-END
  10898.         JP      L2477           ; to LINE-DRAW
  10899.  
  10900. ; ---
  10901.  
  10902. ;; DR-3-PRMS
  10903. L238D:  RST     20H             ; NEXT-CHAR
  10904.         CALL    L1C82           ; routine EXPT-1NUM
  10905.         CALL    L1BEE           ; routine CHECK-END
  10906.  
  10907.         RST     28H             ;; FP-CALC
  10908.         DB    $C5             ;;st-mem-5
  10909.         DB    $A2             ;;stk-half
  10910.         DB    $04             ;;multiply
  10911.         DB    $1F             ;;sin
  10912.         DB    $31             ;;duplicate
  10913.         DB    $30             ;;not
  10914.         DB    $30             ;;not
  10915.         DB    $00             ;;jump-true
  10916.  
  10917.         DB    $06             ;;to L23A3, DR-SIN-NZ
  10918.  
  10919.         DB    $02             ;;delete
  10920.         DB    $38             ;;end-calc
  10921.  
  10922.         JP      L2477           ; to LINE-DRAW
  10923.  
  10924. ; ---
  10925.  
  10926. ;; DR-SIN-NZ
  10927. L23A3:  DB    $C0             ;;st-mem-0
  10928.         DB    $02             ;;delete
  10929.         DB    $C1             ;;st-mem-1
  10930.         DB    $02             ;;delete
  10931.         DB    $31             ;;duplicate
  10932.         DB    $2A             ;;abs
  10933.         DB    $E1             ;;get-mem-1
  10934.         DB    $01             ;;exchange
  10935.         DB    $E1             ;;get-mem-1
  10936.         DB    $2A             ;;abs
  10937.         DB    $0F             ;;addition
  10938.         DB    $E0             ;;get-mem-0
  10939.         DB    $05             ;;division
  10940.         DB    $2A             ;;abs
  10941.         DB    $E0             ;;get-mem-0
  10942.         DB    $01             ;;exchange
  10943.         DB    $3D             ;;re-stack
  10944.         DB    $38             ;;end-calc
  10945.  
  10946.         LD      A,(HL)          ;
  10947.         CP      $81             ;
  10948.         JR      NC,L23C1        ; to DR-PRMS
  10949.  
  10950.  
  10951.         RST     28H             ;; FP-CALC
  10952.         DB    $02             ;;delete
  10953.         DB    $02             ;;delete
  10954.         DB    $38             ;;end-calc
  10955.  
  10956.         JP      L2477           ; to LINE-DRAW
  10957.  
  10958. ; ---
  10959.  
  10960. ;; DR-PRMS
  10961. L23C1:  CALL    L247D           ; routine CD-PRMS1
  10962.         PUSH    BC              ;
  10963.  
  10964.         RST     28H             ;; FP-CALC
  10965.         DB    $02             ;;delete
  10966.         DB    $E1             ;;get-mem-1
  10967.         DB    $01             ;;exchange
  10968.         DB    $05             ;;division
  10969.         DB    $C1             ;;st-mem-1
  10970.         DB    $02             ;;delete
  10971.         DB    $01             ;;exchange
  10972.         DB    $31             ;;duplicate
  10973.         DB    $E1             ;;get-mem-1
  10974.         DB    $04             ;;multiply
  10975.         DB    $C2             ;;st-mem-2
  10976.         DB    $02             ;;delete
  10977.         DB    $01             ;;exchange
  10978.         DB    $31             ;;duplicate
  10979.         DB    $E1             ;;get-mem-1
  10980.         DB    $04             ;;multiply
  10981.         DB    $E2             ;;get-mem-2
  10982.         DB    $E5             ;;get-mem-5
  10983.         DB    $E0             ;;get-mem-0
  10984.         DB    $03             ;;subtract
  10985.         DB    $A2             ;;stk-half
  10986.         DB    $04             ;;multiply
  10987.         DB    $31             ;;duplicate
  10988.         DB    $1F             ;;sin
  10989.         DB    $C5             ;;st-mem-5
  10990.         DB    $02             ;;delete
  10991.         DB    $20             ;;cos
  10992.         DB    $C0             ;;st-mem-0
  10993.         DB    $02             ;;delete
  10994.         DB    $C2             ;;st-mem-2
  10995.         DB    $02             ;;delete
  10996.         DB    $C1             ;;st-mem-1
  10997.         DB    $E5             ;;get-mem-5
  10998.         DB    $04             ;;multiply
  10999.         DB    $E0             ;;get-mem-0
  11000.         DB    $E2             ;;get-mem-2
  11001.         DB    $04             ;;multiply
  11002.         DB    $0F             ;;addition
  11003.         DB    $E1             ;;get-mem-1
  11004.         DB    $01             ;;exchange
  11005.         DB    $C1             ;;st-mem-1
  11006.         DB    $02             ;;delete
  11007.         DB    $E0             ;;get-mem-0
  11008.         DB    $04             ;;multiply
  11009.         DB    $E2             ;;get-mem-2
  11010.         DB    $E5             ;;get-mem-5
  11011.         DB    $04             ;;multiply
  11012.         DB    $03             ;;subtract
  11013.         DB    $C2             ;;st-mem-2
  11014.         DB    $2A             ;;abs
  11015.         DB    $E1             ;;get-mem-1
  11016.         DB    $2A             ;;abs
  11017.         DB    $0F             ;;addition
  11018.         DB    $02             ;;delete
  11019.         DB    $38             ;;end-calc
  11020.  
  11021.         LD      A,(DE)          ;
  11022.         CP       $81            ;
  11023.         POP     BC              ;
  11024.         JP      C,L2477         ; to LINE-DRAW
  11025.  
  11026.         PUSH    BC              ;
  11027.  
  11028.         RST     28H             ;; FP-CALC
  11029.         DB    $01             ;;exchange
  11030.         DB    $38             ;;end-calc
  11031.  
  11032.         LD      A,($5C7D)       ; COORDS-x
  11033.         CALL    L2D28           ; routine STACK-A
  11034.  
  11035.         RST     28H             ;; FP-CALC
  11036.         DB    $C0             ;;st-mem-0
  11037.         DB    $0F             ;;addition
  11038.         DB    $01             ;;exchange
  11039.         DB    $38             ;;end-calc
  11040.  
  11041.         LD      A,($5C7E)       ; COORDS-y
  11042.         CALL    L2D28           ; routine STACK-A
  11043.  
  11044.         RST     28H             ;; FP-CALC
  11045.         DB    $C5             ;;st-mem-5
  11046.         DB    $0F             ;;addition
  11047.         DB    $E0             ;;get-mem-0
  11048.         DB    $E5             ;;get-mem-5
  11049.         DB    $38             ;;end-calc
  11050.  
  11051.         POP     BC              ;
  11052.  
  11053. ;; DRW-STEPS
  11054. L2420:  DEC     B               ;
  11055.         JR      Z,L245F         ; to ARC-END
  11056.  
  11057.         JR      L2439           ; to ARC-START
  11058.  
  11059. ; ---
  11060.  
  11061.  
  11062. ;; ARC-LOOP
  11063. L2425:  RST     28H             ;; FP-CALC
  11064.         DB    $E1             ;;get-mem-1
  11065.         DB    $31             ;;duplicate
  11066.         DB    $E3             ;;get-mem-3
  11067.         DB    $04             ;;multiply
  11068.         DB    $E2             ;;get-mem-2
  11069.         DB    $E4             ;;get-mem-4
  11070.         DB    $04             ;;multiply
  11071.         DB    $03             ;;subtract
  11072.         DB    $C1             ;;st-mem-1
  11073.         DB    $02             ;;delete
  11074.         DB    $E4             ;;get-mem-4
  11075.         DB    $04             ;;multiply
  11076.         DB    $E2             ;;get-mem-2
  11077.         DB    $E3             ;;get-mem-3
  11078.         DB    $04             ;;multiply
  11079.         DB    $0F             ;;addition
  11080.         DB    $C2             ;;st-mem-2
  11081.         DB    $02             ;;delete
  11082.         DB    $38             ;;end-calc
  11083.  
  11084. ;; ARC-START
  11085. L2439:  PUSH    BC              ;
  11086.  
  11087.         RST     28H             ;; FP-CALC
  11088.         DB    $C0             ;;st-mem-0
  11089.         DB    $02             ;;delete
  11090.         DB    $E1             ;;get-mem-1
  11091.         DB    $0F             ;;addition
  11092.         DB    $31             ;;duplicate
  11093.         DB    $38             ;;end-calc
  11094.  
  11095.         LD      A,($5C7D)       ; COORDS-x
  11096.         CALL    L2D28           ; routine STACK-A
  11097.  
  11098.         RST     28H             ;; FP-CALC
  11099.         DB    $03             ;;subtract
  11100.         DB    $E0             ;;get-mem-0
  11101.         DB    $E2             ;;get-mem-2
  11102.         DB    $0F             ;;addition
  11103.         DB    $C0             ;;st-mem-0
  11104.         DB    $01             ;;exchange
  11105.         DB    $E0             ;;get-mem-0
  11106.         DB    $38             ;;end-calc
  11107.  
  11108.         LD      A,($5C7E)       ; COORDS-y
  11109.         CALL    L2D28           ; routine STACK-A
  11110.  
  11111.         RST     28H             ;; FP-CALC
  11112.         DB    $03             ;;subtract
  11113.         DB    $38             ;;end-calc
  11114.  
  11115.         CALL    L24B7           ; routine DRAW-LINE
  11116.         POP     BC              ;
  11117.         DJNZ    L2425           ; to ARC-LOOP
  11118.  
  11119.  
  11120. ;; ARC-END
  11121. L245F:  RST     28H             ;; FP-CALC
  11122.         DB    $02             ;;delete
  11123.         DB    $02             ;;delete
  11124.         DB    $01             ;;exchange
  11125.         DB    $38             ;;end-calc
  11126.  
  11127.         LD      A,($5C7D)       ; COORDS-x
  11128.         CALL    L2D28           ; routine STACK-A
  11129.  
  11130.         RST     28H             ;; FP-CALC
  11131.         DB    $03             ;;subtract
  11132.         DB    $01             ;;exchange
  11133.         DB    $38             ;;end-calc
  11134.  
  11135.         LD      A,($5C7E)       ; COORDS-y
  11136.         CALL    L2D28           ; routine STACK-A
  11137.  
  11138.         RST     28H             ;; FP-CALC
  11139.         DB    $03             ;;subtract
  11140.         DB    $38             ;;end-calc
  11141.  
  11142. ;; LINE-DRAW
  11143. L2477:  CALL    L24B7           ; routine DRAW-LINE
  11144.         JP      L0D4D           ; to TEMPS
  11145.  
  11146.  
  11147. ; ------------------
  11148. ; Initial parameters
  11149. ; ------------------
  11150. ;
  11151. ;
  11152.  
  11153. ;; CD-PRMS1
  11154. L247D:  RST     28H             ;; FP-CALC
  11155.         DB    $31             ;;duplicate
  11156.         DB    $28             ;;sqr
  11157.         DB    $34             ;;stk-data
  11158.         DB    $32             ;;Exponent: $82, Bytes: 1
  11159.         DB    $00             ;;(+00,+00,+00)
  11160.         DB    $01             ;;exchange
  11161.         DB    $05             ;;division
  11162.         DB    $E5             ;;get-mem-5
  11163.         DB    $01             ;;exchange
  11164.         DB    $05             ;;division
  11165.         DB    $2A             ;;abs
  11166.         DB    $38             ;;end-calc
  11167.  
  11168.         CALL    L2DD5           ; routine FP-TO-A
  11169.         JR      C,L2495         ; to USE-252
  11170.  
  11171.         AND     $FC             ;
  11172.         ADD     A,$04           ;
  11173.         JR      NC,L2497        ; to DRAW-SAVE
  11174.  
  11175. ;; USE-252
  11176. L2495:  LD      A,$FC           ;
  11177.  
  11178. ;; DRAW-SAVE
  11179. L2497:  PUSH    AF              ;
  11180.         CALL    L2D28           ; routine STACK-A
  11181.  
  11182.         RST     28H             ;; FP-CALC
  11183.         DB    $E5             ;;get-mem-5
  11184.         DB    $01             ;;exchange
  11185.         DB    $05             ;;division
  11186.         DB    $31             ;;duplicate
  11187.         DB    $1F             ;;sin
  11188.         DB    $C4             ;;st-mem-4
  11189.         DB    $02             ;;delete
  11190.         DB    $31             ;;duplicate
  11191.         DB    $A2             ;;stk-half
  11192.         DB    $04             ;;multiply
  11193.         DB    $1F             ;;sin
  11194.         DB    $C1             ;;st-mem-1
  11195.         DB    $01             ;;exchange
  11196.         DB    $C0             ;;st-mem-0
  11197.         DB    $02             ;;delete
  11198.         DB    $31             ;;duplicate
  11199.         DB    $04             ;;multiply
  11200.         DB    $31             ;;duplicate
  11201.         DB    $0F             ;;addition
  11202.         DB    $A1             ;;stk-one
  11203.         DB    $03             ;;subtract
  11204.         DB    $1B             ;;negate
  11205.         DB    $C3             ;;st-mem-3
  11206.         DB    $02             ;;delete
  11207.         DB    $38             ;;end-calc
  11208.  
  11209.         POP     BC              ;
  11210.         RET                     ;
  11211.  
  11212. ; ------------
  11213. ; Line drawing
  11214. ; ------------
  11215. ;
  11216. ;
  11217.  
  11218. ;; DRAW-LINE
  11219. L24B7:  CALL    L2307           ; routine STK-TO-BC
  11220.         LD      A,C             ;
  11221.         CP      B               ;
  11222.         JR      NC,L24C4        ; to DL-X-GE-Y
  11223.  
  11224.         LD      L,C             ;
  11225.         PUSH    DE              ;
  11226.         XOR     A               ;
  11227.         LD      E,A             ;
  11228.         JR      L24CB           ; to DL-LARGER
  11229.  
  11230. ; ---
  11231.  
  11232. ;; DL-X-GE-Y
  11233. L24C4:  OR      C               ;
  11234.         RET     Z               ;
  11235.  
  11236.         LD      L,B             ;
  11237.         LD      B,C             ;
  11238.         PUSH    DE              ;
  11239.         LD      D,$00           ;
  11240.  
  11241. ;; DL-LARGER
  11242. L24CB:  LD      H,B             ;
  11243.         LD      A,B             ;
  11244.         RRA                     ;
  11245.  
  11246. ;; D-L-LOOP
  11247. L24CE:  ADD     A,L             ;
  11248.         JR      C,L24D4         ; to D-L-DIAG
  11249.  
  11250.         CP      H               ;
  11251.         JR      C,L24DB         ; to D-L-HR-VT
  11252.  
  11253. ;; D-L-DIAG
  11254. L24D4:  SUB     H               ;
  11255.         LD      C,A             ;
  11256.         EXX                     ;
  11257.         POP     BC              ;
  11258.         PUSH    BC              ;
  11259.         JR      L24DF           ; to D-L-STEP
  11260.  
  11261. ; ---
  11262.  
  11263. ;; D-L-HR-VT
  11264. L24DB:  LD      C,A             ;
  11265.         PUSH    DE              ;
  11266.         EXX                     ;
  11267.         POP     BC              ;
  11268.  
  11269. ;; D-L-STEP
  11270. L24DF:  LD      HL,($5C7D)      ; COORDS
  11271.         LD      A,B             ;
  11272.         ADD     A,H             ;
  11273.         LD      B,A             ;
  11274.         LD      A,C             ;
  11275.         INC     A               ;
  11276.         ADD     A,L             ;
  11277.         JR      C,L24F7         ; to D-L-RANGE
  11278.  
  11279.         JR      Z,L24F9         ; to REPORT-Bc
  11280.  
  11281. ;; D-L-PLOT
  11282. L24EC:  DEC     A               ;
  11283.         LD      C,A             ;
  11284.         CALL    L22E5           ; routine PLOT-SUB
  11285.         EXX                     ;
  11286.         LD      A,C             ;
  11287.         DJNZ    L24CE           ; to D-L-LOOP
  11288.  
  11289.         POP     DE              ;
  11290.         RET                     ;
  11291.  
  11292. ; ---
  11293.  
  11294. ;; D-L-RANGE
  11295. L24F7:  JR      Z,L24EC         ; to D-L-PLOT
  11296.  
  11297.  
  11298. ;; REPORT-Bc
  11299. L24F9:  RST     08H             ; ERROR-1
  11300.         DB    $0A             ; Error Report: Integer out of range
  11301.  
  11302.  
  11303.  
  11304. ;***********************************
  11305. ;** Part 8. EXPRESSION EVALUATION **
  11306. ;***********************************
  11307. ;
  11308. ; It is a this stage of the ROM that the Spectrum ceases altogether to be
  11309. ; just a colourful novelty. One remarkable feature is that in all previous
  11310. ; commands when the Spectrum is expecting a number or a string then an
  11311. ; expression of the same type can be substituted ad infinitum.
  11312. ; This is the routine that evaluates that expression.
  11313. ; This is what causes 2 + 2 to give the answer 4.
  11314. ; That is quite easy to understand. However you don't have to make it much
  11315. ; more complex to start a remarkable juggling act.
  11316. ; e.g. PRINT 2 * (VAL "2+2" + TAN 3)
  11317. ; In fact, provided there is enough free RAM, the Spectrum can evaluate
  11318. ; an expression of unlimited complexity.
  11319. ; Apart from a couple of minor glitches, which you can now correct, the
  11320. ; system is remarkably robust.
  11321.  
  11322.  
  11323. ; ---------------------------------
  11324. ; Scan expression or sub-expression
  11325. ; ---------------------------------
  11326. ;
  11327. ;
  11328.  
  11329. ;; SCANNING
  11330. L24FB:  RST     18H             ; GET-CHAR
  11331.         LD      B,$00           ; priority marker zero is pushed on stack
  11332.                                 ; to signify end of expression when it is
  11333.                                 ; popped off again.
  11334.         PUSH    BC              ; put in on stack.
  11335.                                 ; and proceed to consider the first character
  11336.                                 ; of the expression.
  11337.  
  11338. ;; S-LOOP-1
  11339. L24FF:  LD      C,A             ; store the character while a look up is done.
  11340.         LD      HL,L2596        ; Address: scan-func
  11341.         CALL    L16DC           ; routine INDEXER is called to see if it is
  11342.                                 ; part of a limited range '+', '(', 'ATTR' etc.
  11343.  
  11344.         LD      A,C             ; fetch the character back
  11345.         JP      NC,L2684        ; jump forward to S-ALPHNUM if not in primary
  11346.                                 ; operators and functions to consider in the
  11347.                                 ; first instance a digit or a variable and
  11348.                                 ; then anything else.                >>>
  11349.  
  11350.         LD      B,$00           ; but here if it was found in table so
  11351.         LD      C,(HL)          ; fetch offset from table and make B zero.
  11352.         ADD     HL,BC           ; add the offset to position found
  11353.         JP      (HL)            ; and jump to the routine e.g. S-BIN
  11354.                                 ; making an indirect exit from there.
  11355.  
  11356. ; -------------------------------------------------------------------------
  11357. ; The four service subroutines for routines in the scannings function table
  11358. ; -------------------------------------------------------------------------
  11359.  
  11360. ; PRINT """Hooray!"" he cried."
  11361.  
  11362. ;; S-QUOTE-S
  11363. L250F:  CALL    L0074           ; routine CH-ADD+1 points to next character
  11364.                                 ; and fetches that character.
  11365.         INC     BC              ; increase length counter.
  11366.         CP      $0D             ; is it carriage return ?
  11367.                                 ; inside a quote.
  11368.         JP      Z,L1C8A         ; jump back to REPORT-C if so.
  11369.                                 ; 'Nonsense in BASIC'.
  11370.  
  11371.         CP      $22             ; is it a quote '"' ?
  11372.         JR      NZ,L250F        ; back to S-QUOTE-S if not for more.
  11373.  
  11374.         CALL    L0074           ; routine CH-ADD+1
  11375.         CP      $22             ; compare with possible adjacent quote
  11376.         RET                     ; return. with zero set if two together.
  11377.  
  11378. ; ---
  11379.  
  11380. ; This subroutine is used to get two coordinate expressions for the three
  11381. ; functions SCREEN$, ATTR and POINT that have two fixed parameters and
  11382. ; therefore require surrounding braces.
  11383.  
  11384. ;; S-2-COORD
  11385. L2522:  RST     20H             ; NEXT-CHAR
  11386.         CP      $28             ; is it the opening '(' ?
  11387.         JR      NZ,L252D        ; forward to S-RPORT-C if not
  11388.                                 ; 'Nonsense in BASIC'.
  11389.  
  11390.         CALL    L1C79           ; routine NEXT-2NUM gets two comma-separated
  11391.                                 ; numeric expressions. Note. this could cause
  11392.                                 ; many more recursive calls to SCANNING but
  11393.                                 ; the parent function will be evaluated fully
  11394.                                 ; before rejoining the main juggling act.
  11395.  
  11396.         RST     18H             ; GET-CHAR
  11397.         CP      $29             ; is it the closing ')' ?
  11398.  
  11399. ;; S-RPORT-C
  11400. L252D:  JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  11401.                                 ; 'Nonsense in BASIC'.
  11402.  
  11403. ; ------------
  11404. ; Check syntax
  11405. ; ------------
  11406. ; This routine is called on a number of occasions to check if syntax is being
  11407. ; checked or if the program is being run. To test the flag inline would use
  11408. ; four bytes of code, but a call instruction only uses 3 bytes of code.
  11409.  
  11410. ;; SYNTAX-Z
  11411. L2530:  BIT     7,(IY+$01)      ; test FLAGS  - checking syntax only ?
  11412.         RET                     ; return.
  11413.  
  11414. ; ----------------
  11415. ; Scanning SCREEN$
  11416. ; ----------------
  11417. ; This function returns the code of a bit-mapped character at screen
  11418. ; position at line C, column B. It is unable to detect the mosaic characters
  11419. ; which are not bit-mapped but detects the ASCII 32 - 127 range.
  11420. ; The bit-mapped UDGs are ignored which is curious as it requires only a
  11421. ; few extra bytes of code. As usual, anything to do with CHARS is weird.
  11422. ; If no match is found a null string is returned.
  11423. ; No actual check on ranges is performed - that's up to the BASIC programmer.
  11424. ; No real harm can come from SCREEN$(255,255) although the BASIC manual
  11425. ; says that invalid values will be trapped.
  11426. ; Interestingly, in the Pitman pocket guide, 1984, Vickers says that the
  11427. ; range checking will be performed.
  11428.  
  11429. ;; S-SCRN$-S
  11430. L2535:  CALL    L2307           ; routine STK-TO-BC.
  11431.         LD      HL,($5C36)      ; fetch address of CHARS.
  11432.         LD      DE,$0100        ; fetch offset to chr$ 32
  11433.         ADD     HL,DE           ; and find start of bitmaps.
  11434.                                 ; Note. not inc h. ??
  11435.         LD      A,C             ; transfer line to A.
  11436.         RRCA                    ; multiply
  11437.         RRCA                    ; by
  11438.         RRCA                    ; thirty-two.
  11439.         AND     $E0             ; and with 11100000
  11440.         XOR     B               ; combine with column $00 - $1F
  11441.         LD      E,A             ; to give the low byte of top line
  11442.         LD      A,C             ; column to A range 00000000 to 00011111
  11443.         AND     $18             ; and with 00011000
  11444.         XOR     $40             ; xor with 01000000 (high byte screen start)
  11445.         LD      D,A             ; register DE now holds start address of cell.
  11446.         LD      B,$60           ; there are 96 characters in ASCII set.
  11447.  
  11448. ;; S-SCRN-LP
  11449. L254F:  PUSH    BC              ; save count
  11450.         PUSH    DE              ; save screen start address
  11451.         PUSH    HL              ; save bitmap start
  11452.         LD      A,(DE)          ; first byte of screen to A
  11453.         XOR     (HL)            ; xor with corresponding character byte
  11454.         JR      Z,L255A         ; forward to S-SC-MTCH if they match
  11455.                                 ; if inverse result would be $FF
  11456.                                 ; if any other then mismatch
  11457.  
  11458.         INC     A               ; set to $00 if inverse
  11459.         JR      NZ,L2573        ; forward to S-SCR-NXT if a mismatch
  11460.  
  11461.         DEC     A               ; restore $FF
  11462.  
  11463. ; a match has been found so seven more to test.
  11464.  
  11465. ;; S-SC-MTCH
  11466. L255A:  LD      C,A             ; load C with inverse mask $00 or $FF
  11467.         LD      B,$07           ; count seven more bytes
  11468.  
  11469. ;; S-SC-ROWS
  11470. L255D:  INC     D               ; increment screen address.
  11471.         INC     HL              ; increment bitmap address.
  11472.         LD      A,(DE)          ; byte to A
  11473.         XOR     (HL)            ; will give $00 or $FF (inverse)
  11474.         XOR     C               ; xor with inverse mask
  11475.         JR      NZ,L2573        ; forward to S-SCR-NXT if no match.
  11476.  
  11477.         DJNZ    L255D           ; back to S-SC-ROWS until all eight matched.
  11478.  
  11479. ; continue if a match of all eight bytes was found
  11480.  
  11481.         POP     BC              ; discard the
  11482.         POP     BC              ; saved
  11483.         POP     BC              ; pointers
  11484.         LD      A,$80           ; the endpoint of character set
  11485.         SUB     B               ; subtract the counter
  11486.                                 ; to give the code 32-127
  11487.         LD      BC,$0001        ; make one space in workspace.
  11488.  
  11489.         RST     30H             ; BC-SPACES creates the space sliding
  11490.                                 ; the calculator stack upwards.
  11491.         LD      (DE),A          ; start is addressed by DE, so insert code
  11492.         JR      L257D           ; forward to S-SCR-STO
  11493.  
  11494. ; ---
  11495.  
  11496. ; the jump was here if no match and more bitmaps to test.
  11497.  
  11498. ;; S-SCR-NXT
  11499. L2573:  POP     HL              ; restore the last bitmap start
  11500.         LD      DE,$0008        ; and prepare to add 8.
  11501.         ADD     HL,DE           ; now addresses next character bitmap.
  11502.         POP     DE              ; restore screen address
  11503.         POP     BC              ; and character counter in B
  11504.         DJNZ    L254F           ; back to S-SCRN-LP if more characters.
  11505.  
  11506.         LD      C,B             ; B is now zero, so BC now zero.
  11507.  
  11508. ;; S-SCR-STO
  11509. L257D:  JP      L2AB2           ; to STK-STO-$ to store the string in
  11510.                                 ; workspace or a string with zero length.
  11511.                                 ; (value of DE doesn't matter in last case)
  11512.  
  11513. ; Note. this exit seems correct but the general-purpose routine S-STRING
  11514. ; that calls this one will also stack any of its string results so this
  11515. ; leads to a double storing of the result in this case.
  11516. ; The instruction at L257D should just be a RET.
  11517. ; credit Stephen Kelly and others, 1982.
  11518.  
  11519. ; -------------
  11520. ; Scanning ATTR
  11521. ; -------------
  11522. ; This function subroutine returns the attributes of a screen location -
  11523. ; a numeric result.
  11524. ; Again it's up to the BASIC programmer to supply valid values of line/column.
  11525.  
  11526. ;; S-ATTR-S
  11527. L2580:  CALL    L2307           ; routine STK-TO-BC fetches line to C,
  11528.                                 ; and column to B.
  11529.         LD      A,C             ; line to A $00 - $17   (max 00010111)
  11530.         RRCA                    ; rotate
  11531.         RRCA                    ; bits
  11532.         RRCA                    ; left.
  11533.         LD      C,A             ; store in C as an intermediate value.
  11534.  
  11535.         AND     $E0             ; pick up bits 11100000 ( was 00011100 )
  11536.         XOR     B               ; combine with column $00 - $1F
  11537.         LD      L,A             ; low byte now correct.
  11538.  
  11539.         LD      A,C             ; bring back intermediate result from C
  11540.         AND     $03             ; mask to give correct third of
  11541.                                 ; screen $00 - $02
  11542.         XOR     $58             ; combine with base address.
  11543.         LD      H,A             ; high byte correct.
  11544.         LD      A,(HL)          ; pick up the colour attribute.
  11545.         JP      L2D28           ; forward to STACK-A to store result
  11546.                                 ; and make an indirect exit.
  11547.  
  11548. ; -----------------------
  11549. ; Scanning function table
  11550. ; -----------------------
  11551. ; This table is used by INDEXER routine to find the offsets to
  11552. ; four operators and eight functions. e.g. $A8 is the token 'FN'.
  11553. ; This table is used in the first instance for the first character of an
  11554. ; expression or by a recursive call to SCANNING for the first character of
  11555. ; any sub-expression. It eliminates functions that have no argument or
  11556. ; functions that can have more than one argument and therefore require
  11557. ; braces. By eliminating and dealing with these now it can later take a
  11558. ; simplistic approach to all other functions and assume that they have
  11559. ; one argument.
  11560. ; Similarly by eliminating BIN and '.' now it is later able to assume that
  11561. ; all numbers begin with a digit and that the presence of a number or
  11562. ; variable can be detected by a call to ALPHANUM.
  11563. ; By default all expressions are positive and the spurious '+' is eliminated
  11564. ; now as in print +2. This should not be confused with the operator '+'.
  11565. ; Note. this does allow a degree of nonsense to be accepted as in
  11566. ; PRINT +"3 is the greatest.".
  11567. ; An acquired programming skill is the ability to include brackets where
  11568. ; they are not necessary.
  11569. ; A bracket at the start of a sub-expression may be spurious or necessary
  11570. ; to denote that the contained expression is to be evaluated as an entity.
  11571. ; In either case this is dealt with by recursive calls to SCANNING.
  11572. ; An expression that begins with a quote requires special treatment.
  11573.  
  11574. ;; scan-func
  11575. L2596:  DB    $22, L25B3-$-1  ; $1C offset to S-QUOTE
  11576.         DB    '(', L25E8-$-1  ; $4F offset to S-BRACKET
  11577.         DB    '.', L268D-$-1  ; $F2 offset to S-DECIMAL
  11578.         DB    '+', L25AF-$-1  ; $12 offset to S-U-PLUS
  11579.  
  11580.         DB    $A8, L25F5-$-1  ; $56 offset to S-FN
  11581.         DB    $A5, L25F8-$-1  ; $57 offset to S-RND
  11582.         DB    $A7, L2627-$-1  ; $84 offset to S-PI
  11583.         DB    $A6, L2634-$-1  ; $8F offset to S-INKEY$
  11584.         DB    $C4, L268D-$-1  ; $E6 offset to S-BIN
  11585.         DB    $AA, L2668-$-1  ; $BF offset to S-SCREEN$
  11586.         DB    $AB, L2672-$-1  ; $C7 offset to S-ATTR
  11587.         DB    $A9, L267B-$-1  ; $CE offset to S-POINT
  11588.  
  11589.         DB    $00             ; zero end marker
  11590.  
  11591. ; --------------------------
  11592. ; Scanning function routines
  11593. ; --------------------------
  11594. ; These are the 11 subroutines accessed by the above table.
  11595. ; S-BIN and S-DECIMAL are the same
  11596. ; The 1-byte offset limits their location to within 255 bytes of their
  11597. ; entry in the table.
  11598.  
  11599. ; ->
  11600. ;; S-U-PLUS
  11601. L25AF:  RST     20H             ; NEXT-CHAR just ignore
  11602.         JP      L24FF           ; to S-LOOP-1
  11603.  
  11604. ; ---
  11605.  
  11606. ; ->
  11607. ;; S-QUOTE
  11608. L25B3:  RST     18H             ; GET-CHAR
  11609.         INC     HL              ; address next character (first in quotes)
  11610.         PUSH    HL              ; save start of quoted text.
  11611.         LD      BC,$0000        ; initialize length of string to zero.
  11612.         CALL    L250F           ; routine S-QUOTE-S
  11613.         JR      NZ,L25D9        ; forward to S-Q-PRMS if
  11614.  
  11615. ;; S-Q-AGAIN
  11616. L25BE:  CALL    L250F           ; routine S-QUOTE-S copies string until a
  11617.                                 ; quote is encountered
  11618.         JR      Z,L25BE         ; back to S-Q-AGAIN if two quotes WERE
  11619.                                 ; together.
  11620.  
  11621. ; but if just an isolated quote then that terminates the string.
  11622.  
  11623.         CALL    L2530           ; routine SYNTAX-Z
  11624.         JR      Z,L25D9         ; forward to S-Q-PRMS if checking syntax.
  11625.  
  11626.  
  11627.         RST     30H             ; BC-SPACES creates the space for true
  11628.                                 ; copy of string in workspace.
  11629.         POP     HL              ; re-fetch start of quoted text.
  11630.         PUSH    DE              ; save start in workspace.
  11631.  
  11632. ;; S-Q-COPY
  11633. L25CB:  LD      A,(HL)          ; fetch a character from source.
  11634.         INC     HL              ; advance source address.
  11635.         LD      (DE),A          ; place in destination.
  11636.         INC     DE              ; advance destination address.
  11637.         CP      $22             ; was it a '"' just copied ?
  11638.         JR      NZ,L25CB        ; back to S-Q-COPY to copy more if not
  11639.  
  11640.         LD      A,(HL)          ; fetch adjacent character from source.
  11641.         INC     HL              ; advance source address.
  11642.         CP      $22             ; is this '"' ? - i.e. two quotes together ?
  11643.         JR      Z,L25CB         ; to S-Q-COPY if so including just one of the
  11644.                                 ; pair of quotes.
  11645.  
  11646. ; proceed when terminating quote encountered.
  11647.  
  11648. ;; S-Q-PRMS
  11649. L25D9:  DEC     BC              ; decrease count by 1.
  11650.         POP     DE              ; restore start of string in workspace.
  11651.  
  11652. ;; S-STRING
  11653. L25DB:  LD      HL,$5C3B        ; Address FLAGS system variable.
  11654.         RES     6,(HL)          ; signal string result.
  11655.         BIT     7,(HL)          ; is syntax being checked.
  11656.         CALL    NZ,L2AB2        ; routine STK-STO-$ is called in runtime.
  11657.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11658.  
  11659. ; ---
  11660.  
  11661. ; ->
  11662. ;; S-BRACKET
  11663. L25E8:  RST     20H             ; NEXT-CHAR
  11664.         CALL    L24FB           ; routine SCANNING is called recursively.
  11665.         CP      $29             ; is it the closing ')' ?
  11666.         JP      NZ,L1C8A        ; jump back to REPORT-C if not
  11667.                                 ; 'Nonsense in BASIC'
  11668.  
  11669.         RST     20H             ; NEXT-CHAR
  11670.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11671.  
  11672. ; ---
  11673.  
  11674. ; ->
  11675. ;; S-FN
  11676. L25F5:  JP      L27BD           ; jump forward to S-FN-SBRN.
  11677.  
  11678. ; ---
  11679.  
  11680. ; ->
  11681. ;; S-RND
  11682. L25F8:  CALL    L2530           ; routine SYNTAX-Z
  11683.         JR      Z,L2625         ; forward to S-RND-END if checking syntax.
  11684.  
  11685.         LD      BC,($5C76)      ; fetch system variable SEED
  11686.         CALL    L2D2B           ; routine STACK-BC places on calculator stack
  11687.  
  11688.         RST     28H             ;; FP-CALC           ;s.
  11689.         DB    $A1             ;;stk-one            ;s,1.
  11690.         DB    $0F             ;;addition           ;s+1.
  11691.         DB    $34             ;;stk-data           ;
  11692.         DB    $37             ;;Exponent: $87,
  11693.                                 ;;Bytes: 1
  11694.         DB    $16             ;;(+00,+00,+00)      ;s+1,75.
  11695.         DB    $04             ;;multiply           ;(s+1)*75 = v
  11696.         DB    $34             ;;stk-data           ;v.
  11697.         DB    $80             ;;Bytes: 3
  11698.         DB    $41             ;;Exponent $91
  11699.         DB    $00,$00,$80     ;;(+00)              ;v,65537.
  11700.         DB    $32             ;;n-mod-m            ;remainder, result.
  11701.         DB    $02             ;;delete             ;remainder.
  11702.         DB    $A1             ;;stk-one            ;remainder, 1.
  11703.         DB    $03             ;;subtract           ;remainder - 1. = rnd
  11704.         DB    $31             ;;duplicate          ;rnd,rnd.
  11705.         DB    $38             ;;end-calc
  11706.  
  11707.         CALL    L2DA2           ; routine FP-TO-BC
  11708.         LD      ($5C76),BC      ; store in SEED for next starting point.
  11709.         LD      A,(HL)          ; fetch exponent
  11710.         AND     A               ; is it zero ?
  11711.         JR      Z,L2625         ; forward if so to S-RND-END
  11712.  
  11713.         SUB     $10             ; reduce exponent by 2^16
  11714.         LD      (HL),A          ; place back
  11715.  
  11716. ;; S-RND-END
  11717. L2625:  JR      L2630           ; forward to S-PI-END
  11718.  
  11719. ; ---
  11720.  
  11721. ; the number PI 3.14159...
  11722.  
  11723. ; ->
  11724. ;; S-PI
  11725. L2627:  CALL    L2530           ; routine SYNTAX-Z
  11726.         JR      Z,L2630         ; to S-PI-END if checking syntax.
  11727.  
  11728.         RST     28H             ;; FP-CALC
  11729.         DB    $A3             ;;stk-pi/2                          pi/2.
  11730.         DB    $38             ;;end-calc
  11731.  
  11732.         INC     (HL)            ; increment the exponent leaving pi
  11733.                                 ; on the calculator stack.
  11734.  
  11735. ;; S-PI-END
  11736. L2630:  RST     20H             ; NEXT-CHAR
  11737.         JP      L26C3           ; jump forward to S-NUMERIC
  11738.  
  11739. ; ---
  11740.  
  11741. ; ->
  11742. ;; S-INKEY$
  11743. L2634:  LD      BC,$105A        ; priority $10, operation code $1A ('read-in')
  11744.                                 ; +$40 for string result, numeric operand.
  11745.                                 ; set this up now in case we need to use the
  11746.                                 ; calculator.
  11747.         RST     20H             ; NEXT-CHAR
  11748.         CP      $23             ; '#' ?
  11749.         JP      Z,L270D         ; to S-PUSH-PO if so to use the calculator
  11750.                                 ; single operation
  11751.                                 ; to read from network/RS232 etc. .
  11752.  
  11753. ; else read a key from the keyboard.
  11754.  
  11755.         LD      HL,$5C3B        ; fetch FLAGS
  11756.         RES     6,(HL)          ; signal string result.
  11757.         BIT     7,(HL)          ; checking syntax ?
  11758.         JR      Z,L2665         ; forward to S-INK$-EN if so
  11759.  
  11760. ;===============================
  11761.                 IF BAS48_ONLY
  11762.                 CALL L028E
  11763.                 ELSE
  11764.                 JP L3B6C                ; Spectrum 128 patch
  11765.                 ENDIF
  11766. ;===============================
  11767.  
  11768. L2649:  LD      C,$00           ; the length of an empty string
  11769.         JR      NZ,L2660        ; to S-IK$-STK to store empty string if
  11770.                                 ; no key returned.
  11771.  
  11772.         CALL    L031E           ; routine K-TEST get main code in A
  11773.         JR      NC,L2660        ; to S-IK$-STK to stack null string if
  11774.                                 ; invalid
  11775.  
  11776.         DEC     D               ; D is expected to be FLAGS so set bit 3 $FF
  11777.                                 ; 'L' Mode so no keywords.
  11778.         LD      E,A             ; main key to A
  11779.                                 ; C is MODE 0 'KLC' from above still.
  11780.         CALL    L0333           ; routine K-DECODE
  11781. L2657:  PUSH    AF              ; save the code
  11782.         LD      BC,$0001        ; make room for one character
  11783.  
  11784.         RST     30H             ; BC-SPACES
  11785.         POP     AF              ; bring the code back
  11786.         LD      (DE),A          ; put the key in workspace
  11787.         LD      C,$01           ; set C length to one
  11788.  
  11789. ;; S-IK$-STK
  11790. L2660:  LD      B,$00           ; set high byte of length to zero
  11791.         CALL    L2AB2           ; routine STK-STO-$
  11792.  
  11793. ;; S-INK$-EN
  11794. L2665:  JP      L2712           ; to S-CONT-2            ===>
  11795.  
  11796. ; ---
  11797.  
  11798. ; ->
  11799. ;; S-SCREEN$
  11800. L2668:  CALL    L2522           ; routine S-2-COORD
  11801.         CALL    NZ,L2535        ; routine S-SCRN$-S
  11802.  
  11803.         RST     20H             ; NEXT-CHAR
  11804.         JP      L25DB           ; forward to S-STRING to stack result
  11805.  
  11806. ; ---
  11807.  
  11808. ; ->
  11809. ;; S-ATTR
  11810. L2672:  CALL    L2522           ; routine S-2-COORD
  11811.         CALL    NZ,L2580        ; routine S-ATTR-S
  11812.  
  11813.         RST     20H             ; NEXT-CHAR
  11814.         JR      L26C3           ; forward to S-NUMERIC
  11815.  
  11816. ; ---
  11817.  
  11818. ; ->
  11819. ;; S-POINT
  11820. L267B:  CALL    L2522           ; routine S-2-COORD
  11821.         CALL    NZ,L22CB        ; routine POINT-SUB
  11822.  
  11823.         RST     20H             ; NEXT-CHAR
  11824.         JR      L26C3           ; forward to S-NUMERIC
  11825.  
  11826. ; -----------------------------
  11827.  
  11828. ; ==> The branch was here if not in table.
  11829.  
  11830. ;; S-ALPHNUM
  11831. L2684:  CALL    L2C88           ; routine ALPHANUM checks if variable or
  11832.                                 ; a digit.
  11833.         JR      NC,L26DF        ; forward to S-NEGATE if not to consider
  11834.                                 ; a '-' character then functions.
  11835.  
  11836.         CP      $41             ; compare 'A'
  11837.         JR      NC,L26C9        ; forward to S-LETTER if alpha       ->
  11838.                                 ; else must have been numeric so continue
  11839.                                 ; into that routine.
  11840.  
  11841. ; This important routine is called during runtime and from LINE-SCAN
  11842. ; when a BASIC line is checked for syntax. It is this routine that
  11843. ; inserts, during syntax checking, the invisible floating point numbers
  11844. ; after the numeric expression. During runtime it just picks these
  11845. ; numbers up. It also handles BIN format numbers.
  11846.  
  11847. ; ->
  11848. ;; S-BIN
  11849. ;; S-DECIMAL
  11850. L268D:  CALL    L2530           ; routine SYNTAX-Z
  11851.         JR      NZ,L26B5        ; to S-STK-DEC in runtime
  11852.  
  11853. ; this route is taken when checking syntax.
  11854.  
  11855.         CALL    L2C9B           ; routine DEC-TO-FP to evaluate number
  11856.  
  11857.         RST     18H             ; GET-CHAR to fetch HL
  11858.         LD      BC,$0006        ; six locations required
  11859.         CALL    L1655           ; routine MAKE-ROOM
  11860.         INC     HL              ; to first new location
  11861.         LD      (HL),$0E        ; insert number marker
  11862.         INC     HL              ; address next
  11863.         EX      DE,HL           ; make DE destination.
  11864.         LD      HL,($5C65)      ; STKEND points to end of stack.
  11865.         LD      C,$05           ; result is five locations lower
  11866.         AND     A               ; prepare for true subtraction
  11867.         SBC     HL,BC           ; point to start of value.
  11868.         LD      ($5C65),HL      ; update STKEND as we are taking number.
  11869.         LDIR                    ; Copy five bytes to program location
  11870.         EX      DE,HL           ; transfer pointer to HL
  11871.         DEC     HL              ; adjust
  11872.         CALL    L0077           ; routine TEMP-PTR1 sets CH-ADD
  11873.         JR      L26C3           ; to S-NUMERIC to record nature of result
  11874.  
  11875. ; ---
  11876.  
  11877. ; branch here in runtime.
  11878.  
  11879. ;; S-STK-DEC
  11880. L26B5:  RST     18H             ; GET-CHAR positions HL at digit.
  11881.  
  11882. ;; S-SD-SKIP
  11883. L26B6:  INC     HL              ; advance pointer
  11884.         LD      A,(HL)          ; until we find
  11885.         CP      $0E             ; chr 14d - the number indicator
  11886.         JR      NZ,L26B6        ; to S-SD-SKIP until a match
  11887.                                 ; it has to be here.
  11888.  
  11889.         INC     HL              ; point to first byte of number
  11890.         CALL    L33B4           ; routine STACK-NUM stacks it
  11891.         LD      ($5C5D),HL      ; update system variable CH_ADD
  11892.  
  11893. ;; S-NUMERIC
  11894. L26C3:  SET     6,(IY+$01)      ; update FLAGS  - Signal numeric result
  11895.         JR      L26DD           ; forward to S-CONT-1               ===>
  11896.                                 ; actually S-CONT-2 is destination but why
  11897.                                 ; waste a byte on a jump when a JR will do.
  11898.                                 ; actually a JR L2712 can be used. Rats.
  11899.  
  11900. ; end of functions accessed from scanning functions table.
  11901.  
  11902. ; --------------------------
  11903. ; Scanning variable routines
  11904. ; --------------------------
  11905. ;
  11906. ;
  11907.  
  11908. ;; S-LETTER
  11909. L26C9:  CALL    L28B2           ; routine LOOK-VARS
  11910.         JP      C,L1C2E         ; jump back to REPORT-2 if not found
  11911.                                 ; 'Variable not found'
  11912.                                 ; but a variable is always 'found' if syntax
  11913.                                 ; is being checked.
  11914.  
  11915.         CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  11916.         LD      A,($5C3B)       ; fetch FLAGS value
  11917.         CP      $C0             ; compare 11000000
  11918.         JR      C,L26DD         ; step forward to S-CONT-1 if string  ===>
  11919.  
  11920.         INC     HL              ; advance pointer
  11921.         CALL    L33B4           ; routine STACK-NUM
  11922.  
  11923. ;; S-CONT-1
  11924. L26DD:  JR      L2712           ; forward to S-CONT-2                 ===>
  11925.  
  11926. ; ----------------------------------------
  11927. ; -> the scanning branch was here if not alphanumeric.
  11928. ; All the remaining functions will be evaluated by a single call to the
  11929. ; calculator. The correct priority for the operation has to be placed in
  11930. ; the B register and the operation code, calculator literal in the C register.
  11931. ; the operation code has bit 7 set if result is numeric and bit 6 is
  11932. ; set if operand is numeric. so
  11933. ; $C0 = numeric result, numeric operand.            e.g. 'sin'
  11934. ; $80 = numeric result, string operand.             e.g. 'code'
  11935. ; $40 = string result, numeric operand.             e.g. 'str$'
  11936. ; $00 = string result, string operand.              e.g. 'val$'
  11937.  
  11938. ;; S-NEGATE
  11939. L26DF:  LD      BC,$09DB        ; prepare priority 09, operation code $C0 +
  11940.                                 ; 'negate' ($1B) - bits 6 and 7 set for numeric
  11941.                                 ; result and numeric operand.
  11942.  
  11943.         CP      $2D             ; is it '-' ?
  11944.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11945.  
  11946.         LD      BC,$1018        ; prepare priority $10, operation code 'val$' -
  11947.                                 ; bits 6 and 7 reset for string result and
  11948.                                 ; string operand.
  11949.        
  11950.         CP      $AE             ; is it 'VAL$' ?
  11951.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11952.  
  11953.         SUB     $AF             ; subtract token 'CODE' value to reduce
  11954.                                 ; functions 'CODE' to 'NOT' although the
  11955.                                 ; upper range is, as yet, unchecked.
  11956.                                 ; valid range would be $00 - $14.
  11957.  
  11958.         JP      C,L1C8A         ; jump back to REPORT-C with anything else
  11959.                                 ; 'Nonsense in BASIC'
  11960.  
  11961.         LD      BC,$04F0        ; prepare priority $04, operation $C0 +
  11962.                                 ; 'not' ($30)
  11963.  
  11964.         CP      $14             ; is it 'NOT'
  11965.         JR      Z,L270D         ; forward to S-PUSH-PO if so
  11966.  
  11967.         JP      NC,L1C8A        ; to REPORT-C if higher
  11968.                                 ; 'Nonsense in BASIC'
  11969.  
  11970.         LD      B,$10           ; priority $10 for all the rest
  11971.         ADD     A,$DC           ; make range $DC - $EF
  11972.                                 ; $C0 + 'code'($1C) thru 'chr$' ($2F)
  11973.  
  11974.         LD      C,A             ; transfer 'function' to C
  11975.         CP      $DF             ; is it 'sin' ?
  11976.         JR      NC,L2707        ; forward to S-NO-TO-$  with 'sin' through
  11977.                                 ; 'chr$' as operand is numeric.
  11978.  
  11979. ; all the rest 'cos' through 'chr$' give a numeric result except 'str$'
  11980. ; and 'chr$'.
  11981.  
  11982.         RES     6,C             ; signal string operand for 'code', 'val' and
  11983.                                 ; 'len'.
  11984.  
  11985. ;; S-NO-TO-$
  11986. L2707:  CP      $EE             ; compare 'str$'
  11987.         JR      C,L270D         ; forward to S-PUSH-PO if lower as result
  11988.                                 ; is numeric.
  11989.  
  11990.         RES     7,C             ; reset bit 7 of op code for 'str$', 'chr$'
  11991.                                 ; as result is string.
  11992.  
  11993. ; >> This is where they were all headed for.
  11994.  
  11995. ;; S-PUSH-PO
  11996. L270D:  PUSH    BC              ; push the priority and calculator operation
  11997.                                 ; code.
  11998.  
  11999.         RST     20H             ; NEXT-CHAR
  12000.         JP      L24FF           ; jump back to S-LOOP-1 to go round the loop
  12001.                                 ; again with the next character.
  12002.  
  12003. ; --------------------------------
  12004.  
  12005. ; ===>  there were many branches forward to here
  12006.  
  12007. ;; S-CONT-2
  12008. L2712:  RST     18H             ; GET-CHAR
  12009.  
  12010. ;; S-CONT-3
  12011. L2713:  CP      $28             ; is it '(' ?
  12012.         JR      NZ,L2723        ; forward to S-OPERTR if not    >
  12013.  
  12014.         BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  12015.         JR      NZ,L2734        ; forward to S-LOOP if numeric to evaluate  >
  12016.  
  12017. ; if a string preceded '(' then slice it.
  12018.  
  12019.         CALL    L2A52           ; routine SLICING
  12020.  
  12021.         RST     20H             ; NEXT-CHAR
  12022.         JR      L2713           ; back to S-CONT-3
  12023.  
  12024. ; ---------------------------
  12025.  
  12026. ; the branch was here when possibility of an operator '(' has been excluded.
  12027.  
  12028. ;; S-OPERTR
  12029. L2723:  LD      B,$00           ; prepare to add
  12030.         LD      C,A             ; possible operator to C
  12031.         LD      HL,L2795        ; Address: $2795 - tbl-of-ops
  12032.         CALL    L16DC           ; routine INDEXER
  12033.         JR      NC,L2734        ; forward to S-LOOP if not in table
  12034.  
  12035. ; but if found in table the priority has to be looked up.
  12036.  
  12037.         LD      C,(HL)          ; operation code to C ( B is still zero )
  12038.         LD      HL,L27B0 - $C3  ; $26ED is base of table
  12039.         ADD     HL,BC           ; index into table.
  12040.         LD      B,(HL)          ; priority to B.
  12041.  
  12042. ; ------------------
  12043. ; Scanning main loop
  12044. ; ------------------
  12045. ; the juggling act
  12046.  
  12047. ;; S-LOOP
  12048. L2734:  POP     DE              ; fetch last priority and operation
  12049.         LD      A,D             ; priority to A
  12050.         CP      B               ; compare with this one
  12051.         JR      C,L2773         ; forward to S-TIGHTER to execute the
  12052.                                 ; last operation before this one as it has
  12053.                                 ; higher priority.
  12054.  
  12055. ; the last priority was greater or equal this one.
  12056.  
  12057.         AND     A               ; if it is zero then so is this
  12058.         JP      Z,L0018         ; jump to exit via get-char pointing at
  12059.                                 ; next character.
  12060.                                 ; This may be the character after the
  12061.                                 ; expression or, if exiting a recursive call,
  12062.                                 ; the next part of the expression to be
  12063.                                 ; evaluated.
  12064.  
  12065.         PUSH    BC              ; save current priority/operation
  12066.                                 ; as it has lower precedence than the one
  12067.                                 ; now in DE.
  12068.  
  12069. ; the 'USR' function is special in that it is overloaded to give two types
  12070. ; of result.
  12071.  
  12072.         LD      HL,$5C3B        ; address FLAGS
  12073.         LD      A,E             ; new operation to A register
  12074.         CP      $ED             ; is it $C0 + 'usr-no' ($2D)  ?
  12075.         JR      NZ,L274C        ; forward to S-STK-LST if not
  12076.  
  12077.         BIT     6,(HL)          ; string result expected ?
  12078.                                 ; (from the lower priority operand we've
  12079.                                 ; just pushed on stack )
  12080.         JR      NZ,L274C        ; forward to S-STK-LST if numeric
  12081.                                 ; as operand bits match.
  12082.  
  12083.         LD      E,$99           ; reset bit 6 and substitute $19 'usr-$'
  12084.                                 ; for string operand.
  12085.  
  12086. ;; S-STK-LST
  12087. L274C:  PUSH    DE              ; now stack this priority/operation
  12088.         CALL    L2530           ; routine SYNTAX-Z
  12089.         JR      Z,L275B         ; forward to S-SYNTEST if checking syntax.
  12090.  
  12091.         LD      A,E             ; fetch the operation code
  12092.         AND     $3F             ; mask off the result/operand bits to leave
  12093.                                 ; a calculator literal.
  12094.         LD      B,A             ; transfer to B register
  12095.  
  12096. ; now use the calculator to perform the single operation - operand is on
  12097. ; the calculator stack.
  12098. ; Note. although the calculator is performing a single operation most
  12099. ; functions e.g. TAN are written using other functions and literals and
  12100. ; these in turn are written using further strings of calculator literals so
  12101. ; another level of magical recursion joins the juggling act for a while
  12102. ; as the calculator too is calling itself.
  12103.  
  12104.         RST     28H             ;; FP-CALC
  12105.         DB    $3B             ;;fp-calc-2
  12106. L2758:  DB    $38             ;;end-calc
  12107.  
  12108.         JR      L2764           ; forward to S-RUNTEST
  12109.  
  12110. ; ---
  12111.  
  12112. ; the branch was here if checking syntax only.
  12113.  
  12114. ;; S-SYNTEST
  12115. L275B:  LD      A,E             ; fetch the operation code to accumulator
  12116.         XOR     (IY+$01)        ; compare with bits of FLAGS
  12117.         AND     $40             ; bit 6 will be zero now if operand
  12118.                                 ; matched expected result.
  12119.  
  12120. ;; S-RPORT-C2
  12121. L2761:  JP      NZ,L1C8A        ; to REPORT-C if mismatch
  12122.                                 ; 'Nonsense in BASIC'
  12123.                                 ; else continue to set flags for next
  12124.  
  12125. ; the branch is to here in runtime after a successful operation.
  12126.  
  12127. ;; S-RUNTEST
  12128. L2764:  POP     DE              ; fetch the last operation from stack
  12129.         LD      HL,$5C3B        ; address FLAGS
  12130.         SET     6,(HL)          ; set default to numeric result in FLAGS
  12131.         BIT     7,E             ; test the operational result
  12132.         JR      NZ,L2770        ; forward to S-LOOPEND if numeric
  12133.  
  12134.         RES     6,(HL)          ; reset bit 6 of FLAGS to show string result.
  12135.  
  12136. ;; S-LOOPEND
  12137. L2770:  POP     BC              ; fetch the previous priority/operation
  12138.         JR      L2734           ; back to S-LOOP to perform these
  12139.  
  12140. ; ---
  12141.  
  12142. ; the branch was here when a stacked priority/operator had higher priority
  12143. ; than the current one.
  12144.  
  12145. ;; S-TIGHTER
  12146. L2773:  PUSH    DE              ; save high priority op on stack again
  12147.         LD      A,C             ; fetch lower priority operation code
  12148.         BIT     6,(IY+$01)      ; test FLAGS - Numeric or string result ?
  12149.         JR      NZ,L2790        ; forward to S-NEXT if numeric result
  12150.  
  12151. ; if this is lower priority yet has string then must be a comparison.
  12152. ; Since these can only be evaluated in context and were defaulted to
  12153. ; numeric in operator look up they must be changed to string equivalents.
  12154.  
  12155.         AND     $3F             ; mask to give true calculator literal
  12156.         ADD     A,$08           ; augment numeric literals to string
  12157.                                 ; equivalents.
  12158.                                 ; 'no-&-no'  => 'str-&-no'
  12159.                                 ; 'no-l-eql' => 'str-l-eql'
  12160.                                 ; 'no-gr-eq' => 'str-gr-eq'
  12161.                                 ; 'nos-neql' => 'strs-neql'
  12162.                                 ; 'no-grtr'  => 'str-grtr'
  12163.                                 ; 'no-less'  => 'str-less'
  12164.                                 ; 'nos-eql'  => 'strs-eql'
  12165.                                 ; 'addition' => 'strs-add'
  12166.         LD      C,A             ; put modified comparison operator back
  12167.         CP      $10             ; is it now 'str-&-no' ?
  12168.         JR      NZ,L2788        ; forward to S-NOT-AND  if not.
  12169.  
  12170.         SET     6,C             ; set numeric operand bit
  12171.         JR      L2790           ; forward to S-NEXT
  12172.  
  12173. ; ---
  12174.  
  12175. ;; S-NOT-AND
  12176. L2788:  JR      C,L2761         ; back to S-RPORT-C2 if less
  12177.                                 ; 'Nonsense in BASIC'.
  12178.                                 ; e.g. a$ * b$
  12179.  
  12180.         CP      $17             ; is it 'strs-add' ?
  12181.         JR      Z,L2790         ; forward to to S-NEXT if so
  12182.                                 ; (bit 6 and 7 are reset)
  12183.  
  12184.         SET     7,C             ; set numeric (Boolean) result for all others
  12185.  
  12186. ;; S-NEXT
  12187. L2790:  PUSH    BC              ; now save this priority/operation on stack
  12188.  
  12189.         RST     20H             ; NEXT-CHAR
  12190.         JP      L24FF           ; jump back to S-LOOP-1
  12191.  
  12192. ; ------------------
  12193. ; Table of operators
  12194. ; ------------------
  12195. ; This table is used to look up the calculator literals associated with
  12196. ; the operator character. The thirteen calculator operations $03 - $0F
  12197. ; have bits 6 and 7 set to signify a numeric result.
  12198. ; Some of these codes and bits may be altered later if the context suggests
  12199. ; a string comparison or operation.
  12200. ; that is '+', '=', '>', '<', '<=', '>=' or '<>'.
  12201.  
  12202. ;; tbl-of-ops
  12203. L2795:  DB    '+', $CF        ;        $C0 + 'addition'
  12204.         DB    '-', $C3        ;        $C0 + 'subtract'
  12205.         DB    '*', $C4        ;        $C0 + 'multiply'
  12206.         DB    '/', $C5        ;        $C0 + 'division'
  12207.         DB    '^', $C6        ;        $C0 + 'to-power'
  12208.         DB    '=', $CE        ;        $C0 + 'nos-eql'
  12209.         DB    '>', $CC        ;        $C0 + 'no-grtr'
  12210.         DB    '<', $CD        ;        $C0 + 'no-less'
  12211.  
  12212.         DB    $C7, $C9        ; '<='   $C0 + 'no-l-eql'
  12213.         DB    $C8, $CA        ; '>='   $C0 + 'no-gr-eql'
  12214.         DB    $C9, $CB        ; '<>'   $C0 + 'nos-neql'
  12215.         DB    $C5, $C7        ; 'OR'   $C0 + 'or'
  12216.         DB    $C6, $C8        ; 'AND'  $C0 + 'no-&-no'
  12217.  
  12218.         DB    $00             ; zero end-marker.
  12219.  
  12220.  
  12221. ; -------------------
  12222. ; Table of priorities
  12223. ; -------------------
  12224. ; This table is indexed with the operation code obtained from the above
  12225. ; table $C3 - $CF to obtain the priority for the respective operation.
  12226.  
  12227. ;; tbl-priors
  12228. L27B0:  DB    $06             ; '-'   opcode $C3
  12229.         DB    $08             ; '*'   opcode $C4
  12230.         DB    $08             ; '/'   opcode $C5
  12231.         DB    $0A             ; '^'   opcode $C6
  12232.         DB    $02             ; 'OR'  opcode $C7
  12233.         DB    $03             ; 'AND' opcode $C8
  12234.         DB    $05             ; '<='  opcode $C9
  12235.         DB    $05             ; '>='  opcode $CA
  12236.         DB    $05             ; '<>'  opcode $CB
  12237.         DB    $05             ; '>'   opcode $CC
  12238.         DB    $05             ; '<'   opcode $CD
  12239.         DB    $05             ; '='   opcode $CE
  12240.         DB    $06             ; '+'   opcode $CF
  12241.  
  12242. ; ----------------------
  12243. ; Scanning function (FN)
  12244. ; ----------------------
  12245. ; This routine deals with user-defined functions.
  12246. ; The definition can be anywhere in the program area but these are best
  12247. ; placed near the start of the program as we shall see.
  12248. ; The evaluation process is quite complex as the Spectrum has to parse two
  12249. ; statements at the same time. Syntax of both has been checked previously
  12250. ; and hidden locations have been created immediately after each argument
  12251. ; of the DEF FN statement. Each of the arguments of the FN function is
  12252. ; evaluated by SCANNING and placed in the hidden locations. Then the
  12253. ; expression to the right of the DEF FN '=' is evaluated by SCANNING and for
  12254. ; any variables encountered, a search is made in the DEF FN variable list
  12255. ; in the program area before searching in the normal variables area.
  12256. ;
  12257. ; Recursion is not allowed: i.e. the definition of a function should not use
  12258. ; the same function, either directly or indirectly ( through another function).
  12259. ; You'll normally get error 4, ('Out of memory'), although sometimes the sytem
  12260. ; will crash. - Vickers, Pitman 1984.
  12261. ;
  12262. ; As the definition is just an expression, there would seem to be no means
  12263. ; of breaking out of such recursion.
  12264. ; However, by the clever use of string expressions and VAL, such recursion is
  12265. ; possible.
  12266. ; e.g. DEF FN a(n) = VAL "n+FN a(n-1)+0" ((n<1) * 10 + 1 TO )
  12267. ; will evaluate the full 11-character expression for all values where n is
  12268. ; greater than zero but just the 11th character, "0", when n drops to zero
  12269. ; thereby ending the recursion producing the correct result.
  12270. ; Recursive string functions are possible using VAL$ instead of VAL and the
  12271. ; null string as the final addend.
  12272. ; - from a turn of the century newsgroup discussion initiated by Mike Wynne.
  12273.  
  12274. ;; S-FN-SBRN
  12275. L27BD:  CALL    L2530           ; routine SYNTAX-Z
  12276.         JR      NZ,L27F7        ; forward to SF-RUN in runtime
  12277.  
  12278.  
  12279.         RST     20H             ; NEXT-CHAR
  12280.         CALL    L2C8D           ; routine ALPHA check for letters A-Z a-z
  12281.         JP      NC,L1C8A        ; jump back to REPORT-C if not
  12282.                                 ; 'Nonsense in BASIC'
  12283.  
  12284.  
  12285.         RST     20H             ; NEXT-CHAR
  12286.         CP      $24             ; is it '$' ?
  12287.         PUSH    AF              ; save character and flags
  12288.         JR      NZ,L27D0        ; forward to SF-BRKT-1 with numeric function
  12289.  
  12290.  
  12291.         RST     20H             ; NEXT-CHAR
  12292.  
  12293. ;; SF-BRKT-1
  12294. L27D0:  CP      $28             ; is '(' ?
  12295.         JR      NZ,L27E6        ; forward to SF-RPRT-C if not
  12296.                                 ; 'Nonsense in BASIC'
  12297.  
  12298.  
  12299.         RST     20H             ; NEXT-CHAR
  12300.         CP      $29             ; is it ')' ?
  12301.         JR      Z,L27E9         ; forward to SF-FLAG-6 if no arguments.
  12302.  
  12303. ;; SF-ARGMTS
  12304. L27D9:  CALL    L24FB           ; routine SCANNING checks each argument
  12305.                                 ; which may be an expression.
  12306.  
  12307.         RST     18H             ; GET-CHAR
  12308.         CP      $2C             ; is it a ',' ?
  12309.         JR      NZ,L27E4        ; forward if not to SF-BRKT-2 to test bracket
  12310.  
  12311.  
  12312.         RST     20H             ; NEXT-CHAR if a comma was found
  12313.         JR      L27D9           ; back to SF-ARGMTS to parse all arguments.
  12314.  
  12315. ; ---
  12316.  
  12317. ;; SF-BRKT-2
  12318. L27E4:  CP      $29             ; is character the closing ')' ?
  12319.  
  12320. ;; SF-RPRT-C
  12321. L27E6:  JP      NZ,L1C8A        ; jump to REPORT-C
  12322.                                 ; 'Nonsense in BASIC'
  12323.  
  12324. ; at this point any optional arguments have had their syntax checked.
  12325.  
  12326. ;; SF-FLAG-6
  12327. L27E9:  RST     20H             ; NEXT-CHAR
  12328.         LD      HL,$5C3B        ; address system variable FLAGS
  12329.         RES     6,(HL)          ; signal string result
  12330.         POP     AF              ; restore test against '$'.
  12331.         JR      Z,L27F4         ; forward to SF-SYN-EN if string function.
  12332.  
  12333.         SET     6,(HL)          ; signal numeric result
  12334.  
  12335. ;; SF-SYN-EN
  12336. L27F4:  JP      L2712           ; jump back to S-CONT-2 to continue scanning.
  12337.  
  12338. ; ---
  12339.  
  12340. ; the branch was here in runtime.
  12341.  
  12342. ;; SF-RUN
  12343. L27F7:  RST     20H             ; NEXT-CHAR fetches name
  12344.         AND     $DF             ; AND 11101111 - reset bit 5 - upper-case.
  12345.         LD      B,A             ; save in B
  12346.  
  12347.         RST     20H             ; NEXT-CHAR
  12348.         SUB     $24             ; subtract '$'
  12349.         LD      C,A             ; save result in C
  12350.         JR      NZ,L2802        ; forward if not '$' to SF-ARGMT1
  12351.  
  12352.         RST     20H             ; NEXT-CHAR advances to bracket
  12353.  
  12354. ;; SF-ARGMT1
  12355. L2802:  RST     20H             ; NEXT-CHAR advances to start of argument
  12356.         PUSH    HL              ; save address
  12357.         LD      HL,($5C53)      ; fetch start of program area from PROG
  12358.         DEC     HL              ; the search starting point is the previous
  12359.                                 ; location.
  12360.  
  12361. ;; SF-FND-DF
  12362. L2808:  LD      DE,$00CE        ; search is for token 'DEF FN' in E,
  12363.                                 ; statement count in D.
  12364.         PUSH    BC              ; save C the string test, and B the letter.
  12365.         CALL    L1D86           ; routine LOOK-PROG will search for token.
  12366.         POP     BC              ; restore BC.
  12367.         JR      NC,L2814        ; forward to SF-CP-DEF if a match was found.
  12368.  
  12369.  
  12370. ;; REPORT-P
  12371. L2812:  RST     08H             ; ERROR-1
  12372.         DB    $18             ; Error Report: FN without DEF
  12373.  
  12374. ;; SF-CP-DEF
  12375. L2814:  PUSH    HL              ; save address of DEF FN
  12376.         CALL    L28AB           ; routine FN-SKPOVR skips over white-space etc.
  12377.                                 ; without disturbing CH-ADD.
  12378.         AND     $DF             ; make fetched character upper-case.
  12379.         CP      B               ; compare with FN name
  12380.         JR      NZ,L2825        ; forward to SF-NOT-FD if no match.
  12381.  
  12382. ; the letters match so test the type.
  12383.  
  12384.         CALL    L28AB           ; routine FN-SKPOVR skips white-space
  12385.         SUB     $24             ; subtract '$' from fetched character
  12386.         CP      C               ; compare with saved result of same operation
  12387.                                 ; on FN name.
  12388.         JR      Z,L2831         ; forward to SF-VALUES with a match.
  12389.  
  12390. ; the letters matched but one was string and the other numeric.
  12391.  
  12392. ;; SF-NOT-FD
  12393. L2825:  POP     HL              ; restore search point.
  12394.         DEC     HL              ; make location before
  12395.         LD      DE,$0200        ; the search is to be for the end of the
  12396.                                 ; current definition - 2 statements forward.
  12397.         PUSH    BC              ; save the letter/type
  12398.         CALL    L198B           ; routine EACH-STMT steps past rejected
  12399.                                 ; definition.
  12400.         POP     BC              ; restore letter/type
  12401.         JR      L2808           ; back to SF-FND-DF to continue search
  12402.  
  12403. ; ---
  12404.  
  12405. ; Success!
  12406. ; the branch was here with matching letter and numeric/string type.
  12407.  
  12408. ;; SF-VALUES
  12409. L2831:  AND     A               ; test A ( will be zero if string '$' - '$' )
  12410.  
  12411.         CALL    Z,L28AB         ; routine FN-SKPOVR advances HL past '$'.
  12412.  
  12413.         POP     DE              ; discard pointer to 'DEF FN'.
  12414.         POP     DE              ; restore pointer to first FN argument.
  12415.         LD      ($5C5D),DE      ; save in CH_ADD
  12416.  
  12417.         CALL    L28AB           ; routine FN-SKPOVR advances HL past '('
  12418.         PUSH    HL              ; save start address in DEF FN  ***
  12419.         CP      $29             ; is character a ')' ?
  12420.         JR      Z,L2885         ; forward to SF-R-BR-2 if no arguments.
  12421.  
  12422. ;; SF-ARG-LP
  12423. L2843:  INC     HL              ; point to next character.
  12424.         LD      A,(HL)          ; fetch it.
  12425.         CP      $0E             ; is it the number marker
  12426.         LD      D,$40           ; signal numeric in D.
  12427.         JR      Z,L2852         ; forward to SF-ARG-VL if numeric.
  12428.  
  12429.         DEC     HL              ; back to letter
  12430.         CALL    L28AB           ; routine FN-SKPOVR skips any white-space
  12431.         INC     HL              ; advance past the expected '$' to
  12432.                                 ; the 'hidden' marker.
  12433.         LD      D,$00           ; signal string.
  12434.  
  12435. ;; SF-ARG-VL
  12436. L2852:  INC     HL              ; now address first of 5-byte location.
  12437.         PUSH    HL              ; save address in DEF FN statement
  12438.         PUSH    DE              ; save D - result type
  12439.  
  12440.         CALL    L24FB           ; routine SCANNING evaluates expression in
  12441.                                 ; the FN statement setting FLAGS and leaving
  12442.                                 ; result as last value on calculator stack.
  12443.  
  12444.         POP     AF              ; restore saved result type to A
  12445.  
  12446.         XOR     (IY+$01)        ; xor with FLAGS
  12447.         AND     $40             ; and with 01000000 to test bit 6
  12448.         JR      NZ,L288B        ; forward to REPORT-Q if type mismatch.
  12449.                                 ; 'Parameter error'
  12450.  
  12451.         POP     HL              ; pop the start address in DEF FN statement
  12452.         EX      DE,HL           ; transfer to DE ?? pop straight into de ?
  12453.  
  12454.         LD      HL,($5C65)      ; set HL to STKEND location after value
  12455.         LD      BC,$0005        ; five bytes to move
  12456.         SBC     HL,BC           ; decrease HL by 5 to point to start.
  12457.         LD      ($5C65),HL      ; set STKEND 'removing' value from stack.
  12458.  
  12459.         LDIR                    ; copy value into DEF FN statement
  12460.         EX      DE,HL           ; set HL to location after value in DEF FN
  12461.         DEC     HL              ; step back one
  12462.         CALL    L28AB           ; routine FN-SKPOVR gets next valid character
  12463.         CP      $29             ; is it ')' end of arguments ?
  12464.         JR      Z,L2885         ; forward to SF-R-BR-2 if so.
  12465.  
  12466. ; a comma separator has been encountered in the DEF FN argument list.
  12467.  
  12468.         PUSH    HL              ; save position in DEF FN statement
  12469.  
  12470.         RST     18H             ; GET-CHAR from FN statement
  12471.         CP      $2C             ; is it ',' ?
  12472.         JR      NZ,L288B        ; forward to REPORT-Q if not
  12473.                                 ; 'Parameter error'
  12474.  
  12475.         RST     20H             ; NEXT-CHAR in FN statement advances to next
  12476.                                 ; argument.
  12477.  
  12478.         POP     HL              ; restore DEF FN pointer
  12479.         CALL    L28AB           ; routine FN-SKPOVR advances to corresponding
  12480.                                 ; argument.
  12481.  
  12482.         JR      L2843           ; back to SF-ARG-LP looping until all
  12483.                                 ; arguments are passed into the DEF FN
  12484.                                 ; hidden locations.
  12485.  
  12486. ; ---
  12487.  
  12488. ; the branch was here when all arguments passed.
  12489.  
  12490. ;; SF-R-BR-2
  12491. L2885:  PUSH    HL              ; save location of ')' in DEF FN
  12492.  
  12493.         RST     18H             ; GET-CHAR gets next character in FN
  12494.         CP      $29             ; is it a ')' also ?
  12495.         JR      Z,L288D         ; forward to SF-VALUE if so.
  12496.  
  12497.  
  12498. ;; REPORT-Q
  12499. L288B:  RST     08H             ; ERROR-1
  12500.         DB    $19             ; Error Report: Parameter error
  12501.  
  12502. ;; SF-VALUE
  12503. L288D:  POP     DE              ; location of ')' in DEF FN to DE.
  12504.         EX      DE,HL           ; now to HL, FN ')' pointer to DE.
  12505.         LD      ($5C5D),HL      ; initialize CH_ADD to this value.
  12506.  
  12507. ; At this point the start of the DEF FN argument list is on the machine stack.
  12508. ; We also have to consider that this defined function may form part of the
  12509. ; definition of another defined function (though not itself).
  12510. ; As this defined function may be part of a hierarchy of defined functions
  12511. ; currently being evaluated by recursive calls to SCANNING, then we have to
  12512. ; preserve the original value of DEFADD and not assume that it is zero.
  12513.  
  12514.         LD      HL,($5C0B)      ; get original DEFADD address
  12515.         EX      (SP),HL         ; swap with DEF FN address on stack ***
  12516.         LD      ($5C0B),HL      ; set DEFADD to point to this argument list
  12517.                                 ; during scanning.
  12518.  
  12519.         PUSH    DE              ; save FN ')' pointer.
  12520.  
  12521.         RST     20H             ; NEXT-CHAR advances past ')' in define
  12522.  
  12523.         RST     20H             ; NEXT-CHAR advances past '=' to expression
  12524.  
  12525.         CALL    L24FB           ; routine SCANNING evaluates but searches
  12526.                                 ; initially for variables at DEFADD
  12527.  
  12528.         POP     HL              ; pop the FN ')' pointer
  12529.         LD      ($5C5D),HL      ; set CH_ADD to this
  12530.         POP     HL              ; pop the original DEFADD value
  12531.         LD      ($5C0B),HL      ; and re-insert into DEFADD system variable.
  12532.  
  12533.         RST     20H             ; NEXT-CHAR advances to character after ')'
  12534.         JP      L2712           ; to S-CONT-2 - to continue current
  12535.                                 ; invocation of scanning
  12536.  
  12537. ; --------------------
  12538. ; Used to parse DEF FN
  12539. ; --------------------
  12540. ; e.g. DEF FN     s $ ( x )     =  b     $ (  TO  x  ) : REM exaggerated
  12541. ;
  12542. ; This routine is used 10 times to advance along a DEF FN statement
  12543. ; skipping spaces and colour control codes. It is similar to NEXT-CHAR
  12544. ; which is, at the same time, used to skip along the corresponding FN function
  12545. ; except the latter has to deal with AT and TAB characters in string
  12546. ; expressions. These cannot occur in a program area so this routine is
  12547. ; simpler as both colour controls and their parameters are less than space.
  12548.  
  12549. ;; FN-SKPOVR
  12550. L28AB:  INC     HL              ; increase pointer
  12551.         LD      A,(HL)          ; fetch addressed character
  12552.         CP      $21             ; compare with space + 1
  12553.         JR      C,L28AB         ; back to FN-SKPOVR if less
  12554.  
  12555.         RET                     ; return pointing to a valid character.
  12556.  
  12557. ; ---------
  12558. ; LOOK-VARS
  12559. ; ---------
  12560. ;
  12561. ;
  12562.  
  12563. ;; LOOK-VARS
  12564. L28B2:  SET     6,(IY+$01)      ; update FLAGS - presume numeric result
  12565.  
  12566.         RST     18H             ; GET-CHAR
  12567.         CALL    L2C8D           ; routine ALPHA tests for A-Za-z
  12568.         JP      NC,L1C8A        ; jump to REPORT-C if not.
  12569.                                 ; 'Nonsense in BASIC'
  12570.  
  12571.         PUSH    HL              ; save pointer to first letter       ^1
  12572.         AND     $1F             ; mask lower bits, 1 - 26 decimal     000xxxxx
  12573.         LD      C,A             ; store in C.
  12574.  
  12575.         RST     20H             ; NEXT-CHAR
  12576.         PUSH    HL              ; save pointer to second character   ^2
  12577.         CP      $28             ; is it '(' - an array ?
  12578.         JR      Z,L28EF         ; forward to V-RUN/SYN if so.
  12579.  
  12580.         SET     6,C             ; set 6 signaling string if solitary  010
  12581.         CP      $24             ; is character a '$' ?
  12582.         JR      Z,L28DE         ; forward to V-STR-VAR
  12583.  
  12584.         SET     5,C             ; signal numeric                       011
  12585.         CALL    L2C88           ; routine ALPHANUM sets carry if second
  12586.                                 ; character is alphanumeric.
  12587.         JR      NC,L28E3        ; forward to V-TEST-FN if just one character
  12588.  
  12589. ; it is more than one character but re-test current character so that 6 reset
  12590. ; Note. this is a rare lack of elegance. Bit 6 could be reset once before
  12591. ; entering the loop. Another puzzle is that this loop renders the similar
  12592. ; loop at V-PASS redundant.
  12593.  
  12594. ;; V-CHAR
  12595. L28D4:  CALL    L2C88           ; routine ALPHANUM
  12596.         JR      NC,L28EF        ; to V-RUN/SYN when no more
  12597.  
  12598.         RES     6,C             ; make long named type                 001
  12599.  
  12600.         RST     20H             ; NEXT-CHAR
  12601.         JR      L28D4           ; loop back to V-CHAR
  12602.  
  12603. ; ---
  12604.  
  12605.  
  12606. ;; V-STR-VAR
  12607. L28DE:  RST     20H             ; NEXT-CHAR advances past '$'
  12608.         RES     6,(IY+$01)      ; update FLAGS - signal string result.
  12609.  
  12610. ;; V-TEST-FN
  12611. L28E3:  LD      A,($5C0C)       ; load A with DEFADD_hi
  12612.         AND     A               ; and test for zero.
  12613.         JR      Z,L28EF         ; forward to V-RUN/SYN if a defined function
  12614.                                 ; is not being evaluated.
  12615.  
  12616. ; Note.
  12617.  
  12618.         CALL    L2530           ; routine SYNTAX-Z
  12619.         JP      NZ,L2951        ; JUMP to STK-F-ARG in runtime and then
  12620.                                 ; back to this point if no variable found.
  12621.  
  12622. ;; V-RUN/SYN
  12623. L28EF:  LD      B,C             ; save flags in B
  12624.         CALL    L2530           ; routine SYNTAX-Z
  12625.         JR      NZ,L28FD        ; to V-RUN to look for the variable in runtime
  12626.  
  12627. ; if checking syntax the letter is not returned
  12628.  
  12629.         LD      A,C             ; copy letter/flags to A
  12630.         AND     $E0             ; and with 11100000 to get rid of the letter
  12631.         SET     7,A             ; use spare bit to signal checking syntax.
  12632.         LD      C,A             ; and transfer to C.
  12633.         JR      L2934           ; forward to V-SYNTAX
  12634.  
  12635. ; ---
  12636.  
  12637. ; but in runtime search for the variable.
  12638.  
  12639. ;; V-RUN
  12640. L28FD:  LD      HL,($5C4B)      ; set HL to start of variables from VARS
  12641.  
  12642. ;; V-EACH
  12643. L2900:  LD      A,(HL)          ; get first character
  12644.         AND     $7F             ; and with 01111111
  12645.                                 ; ignoring bit 7 which distinguishes
  12646.                                 ; arrays or for/next variables.
  12647.  
  12648.         JR      Z,L2932         ; to V-80-BYTE if zero as must be 10000000
  12649.                                 ; the variables end-marker.
  12650.  
  12651.         CP      C               ; compare with supplied value.
  12652.         JR      NZ,L292A        ; forward to V-NEXT if no match.
  12653.  
  12654.         RLA                     ; destructively test
  12655.         ADD     A,A             ; bits 5 and 6 of A
  12656.                                 ; jumping if bit 5 reset or 6 set
  12657.  
  12658.         JP      P,L293F         ; to V-FOUND-2  strings and arrays
  12659.  
  12660.         JR      C,L293F         ; to V-FOUND-2  simple and for next
  12661.  
  12662. ; leaving long name variables.
  12663.  
  12664.         POP     DE              ; pop pointer to 2nd. char
  12665.         PUSH    DE              ; save it again
  12666.         PUSH    HL              ; save variable first character pointer
  12667.  
  12668. ;; V-MATCHES
  12669. L2912:  INC     HL              ; address next character in vars area
  12670.  
  12671. ;; V-SPACES
  12672. L2913:  LD      A,(DE)          ; pick up letter from prog area
  12673.         INC     DE              ; and advance address
  12674.         CP      $20             ; is it a space
  12675.         JR      Z,L2913         ; back to V-SPACES until non-space
  12676.  
  12677.         OR      $20             ; convert to range 1 - 26.
  12678.         CP      (HL)            ; compare with addressed variables character
  12679.         JR      Z,L2912         ; loop back to V-MATCHES if a match on an
  12680.                                 ; intermediate letter.
  12681.  
  12682.         OR      $80             ; now set bit 7 as last character of long
  12683.                                 ; names are inverted.
  12684.         CP      (HL)            ; compare again
  12685.         JR      NZ,L2929        ; forward to V-GET-PTR if no match
  12686.  
  12687. ; but if they match check that this is also last letter in prog area
  12688.  
  12689.         LD      A,(DE)          ; fetch next character
  12690.         CALL    L2C88           ; routine ALPHANUM sets carry if not alphanum
  12691.         JR      NC,L293E        ; forward to V-FOUND-1 with a full match.
  12692.  
  12693. ;; V-GET-PTR
  12694. L2929:  POP     HL              ; pop saved pointer to char 1
  12695.  
  12696. ;; V-NEXT
  12697. L292A:  PUSH    BC              ; save flags
  12698.         CALL    L19B8           ; routine NEXT-ONE gets next variable in DE
  12699.         EX      DE,HL           ; transfer to HL.
  12700.         POP     BC              ; restore the flags
  12701.         JR      L2900           ; loop back to V-EACH
  12702.                                 ; to compare each variable
  12703.  
  12704. ; ---
  12705.  
  12706. ;; V-80-BYTE
  12707. L2932:  SET     7,B             ; will signal not found
  12708.  
  12709. ; the branch was here when checking syntax
  12710.  
  12711. ;; V-SYNTAX
  12712. L2934:  POP     DE              ; discard the pointer to 2nd. character  v2
  12713.                                 ; in BASIC line/workspace.
  12714.  
  12715.         RST     18H             ; GET-CHAR gets character after variable name.
  12716.         CP      $28             ; is it '(' ?
  12717.         JR      Z,L2943         ; forward to V-PASS
  12718.                                 ; Note. could go straight to V-END ?
  12719.  
  12720.         SET     5,B             ; signal not an array
  12721.         JR      L294B           ; forward to V-END
  12722.  
  12723. ; ---------------------------
  12724.  
  12725. ; the jump was here when a long name matched and HL pointing to last character
  12726. ; in variables area.
  12727.  
  12728. ;; V-FOUND-1
  12729. L293E:  POP     DE              ; discard pointer to first var letter
  12730.  
  12731. ; the jump was here with all other matches HL points to first var char.
  12732.  
  12733. ;; V-FOUND-2
  12734. L293F:  POP     DE              ; discard pointer to 2nd prog char       v2
  12735.         POP     DE              ; drop pointer to 1st prog char          v1
  12736.         PUSH    HL              ; save pointer to last char in vars
  12737.  
  12738.         RST     18H             ; GET-CHAR
  12739.  
  12740. ;; V-PASS
  12741. L2943:  CALL    L2C88           ; routine ALPHANUM
  12742.         JR      NC,L294B        ; forward to V-END if not
  12743.  
  12744. ; but it never will be as we advanced past long-named variables earlier.
  12745.  
  12746.         RST     20H             ; NEXT-CHAR
  12747.         JR      L2943           ; back to V-PASS
  12748.  
  12749. ; ---
  12750.  
  12751. ;; V-END
  12752. L294B:  POP     HL              ; pop the pointer to first character in
  12753.                                 ; BASIC line/workspace.
  12754.         RL      B               ; rotate the B register left
  12755.                                 ; bit 7 to carry
  12756.         BIT     6,B             ; test the array indicator bit.
  12757.         RET                     ; return
  12758.  
  12759. ; -----------------------
  12760. ; Stack function argument
  12761. ; -----------------------
  12762. ; This branch is taken from LOOK-VARS when a defined function is currently
  12763. ; being evaluated.
  12764. ; Scanning is evaluating the expression after the '=' and the variable
  12765. ; found could be in the argument list to the left of the '=' or in the
  12766. ; normal place after the program. Preference will be given to the former.
  12767. ; The variable name to be matched is in C.
  12768.  
  12769. ;; STK-F-ARG
  12770. L2951:  LD      HL,($5C0B)      ; set HL to DEFADD
  12771.         LD      A,(HL)          ; load the first character
  12772.         CP      $29             ; is it ')' ?
  12773.         JP      Z,L28EF         ; JUMP back to V-RUN/SYN, if so, as there are
  12774.                                 ; no arguments.
  12775.  
  12776. ; but proceed to search argument list of defined function first if not empty.
  12777.  
  12778. ;; SFA-LOOP
  12779. L295A:  LD      A,(HL)          ; fetch character again.
  12780.         OR      $60             ; or with 01100000 presume a simple variable.
  12781.         LD      B,A             ; save result in B.
  12782.         INC     HL              ; address next location.
  12783.         LD      A,(HL)          ; pick up byte.
  12784.         CP      $0E             ; is it the number marker ?
  12785.         JR      Z,L296B         ; forward to SFA-CP-VR if so.
  12786.  
  12787. ; it was a string. White-space may be present but syntax has been checked.
  12788.  
  12789.         DEC     HL              ; point back to letter.
  12790.         CALL    L28AB           ; routine FN-SKPOVR skips to the '$'
  12791.         INC     HL              ; now address the hidden marker.
  12792.         RES     5,B             ; signal a string variable.
  12793.  
  12794. ;; SFA-CP-VR
  12795. L296B:  LD      A,B             ; transfer found variable letter to A.
  12796.         CP      C               ; compare with expected.
  12797.         JR      Z,L2981         ; forward to SFA-MATCH with a match.
  12798.  
  12799.         INC     HL              ; step
  12800.         INC     HL              ; past
  12801.         INC     HL              ; the
  12802.         INC     HL              ; five
  12803.         INC     HL              ; bytes.
  12804.  
  12805.         CALL    L28AB           ; routine FN-SKPOVR skips to next character
  12806.         CP      $29             ; is it ')' ?
  12807.         JP      Z,L28EF         ; jump back if so to V-RUN/SYN to look in
  12808.                                 ; normal variables area.
  12809.  
  12810.         CALL    L28AB           ; routine FN-SKPOVR skips past the ','
  12811.                                 ; all syntax has been checked and these
  12812.                                 ; things can be taken as read.
  12813.         JR      L295A           ; back to SFA-LOOP while there are more
  12814.                                 ; arguments.
  12815.  
  12816. ; ---
  12817.  
  12818. ;; SFA-MATCH
  12819. L2981:  BIT     5,C             ; test if numeric
  12820.         JR      NZ,L2991        ; to SFA-END if so as will be stacked
  12821.                                 ; by scanning
  12822.  
  12823.         INC     HL              ; point to start of string descriptor
  12824.         LD      DE,($5C65)      ; set DE to STKEND
  12825.         CALL    L33C0           ; routine MOVE-FP puts parameters on stack.
  12826.         EX      DE,HL           ; new free location to HL.
  12827.         LD      ($5C65),HL      ; use it to set STKEND system variable.
  12828.  
  12829. ;; SFA-END
  12830. L2991:  POP     DE              ; discard
  12831.         POP     DE              ; pointers.
  12832.         XOR     A               ; clear carry flag.
  12833.         INC     A               ; and zero flag.
  12834.         RET                     ; return.
  12835.  
  12836. ; ------------------------
  12837. ; Stack variable component
  12838. ; ------------------------
  12839. ; This is called to evaluate a complex structure that has been found, in
  12840. ; runtime, by LOOK-VARS in the variables area.
  12841. ; In this case HL points to the initial letter, bits 7-5
  12842. ; of which indicate the type of variable.
  12843. ; 010 - simple string, 110 - string array, 100 - array of numbers.
  12844. ;
  12845. ; It is called from CLASS-01 when assigning to a string or array including
  12846. ; a slice.
  12847. ; It is called from SCANNING to isolate the required part of the structure.
  12848. ;
  12849. ; An important part of the runtime process is to check that the number of
  12850. ; dimensions of the variable match the number of subscripts supplied in the
  12851. ; BASIC line.
  12852. ;
  12853. ; If checking syntax,
  12854. ; the B register, which counts dimensions is set to zero (256) to allow
  12855. ; the loop to continue till all subscripts are checked. While doing this it
  12856. ; is reading dimension sizes from some arbitrary area of memory. Although
  12857. ; these are meaningless it is of no concern as the limit is never checked by
  12858. ; int-exp during syntax checking.
  12859. ;
  12860. ; The routine is also called from the syntax path of DIM command to check the
  12861. ; syntax of both string and numeric arrays definitions except that bit 6 of C
  12862. ; is reset so both are checked as numeric arrays. This ruse avoids a terminal
  12863. ; slice being accepted as part of the DIM command.
  12864. ; All that is being checked is that there are a valid set of comma-separated
  12865. ; expressions before a terminal ')', although, as above, it will still go
  12866. ; through the motions of checking dummy dimension sizes.
  12867.  
  12868. ;; STK-VAR
  12869. L2996:  XOR     A               ; clear A
  12870.         LD      B,A             ; and B, the syntax dimension counter (256)
  12871.         BIT     7,C             ; checking syntax ?
  12872.         JR      NZ,L29E7        ; forward to SV-COUNT if so.
  12873.  
  12874. ; runtime evaluation.
  12875.  
  12876.         BIT     7,(HL)          ; will be reset if a simple string.
  12877.         JR      NZ,L29AE        ; forward to SV-ARRAYS otherwise
  12878.  
  12879.         INC     A               ; set A to 1, simple string.
  12880.  
  12881. ;; SV-SIMPLE$
  12882. L29A1:  INC     HL              ; address length low
  12883.         LD      C,(HL)          ; place in C
  12884.         INC     HL              ; address length high
  12885.         LD      B,(HL)          ; place in B
  12886.         INC     HL              ; address start of string
  12887.         EX      DE,HL           ; DE = start now.
  12888.         CALL    L2AB2           ; routine STK-STO-$ stacks string parameters
  12889.                                 ; DE start in variables area,
  12890.                                 ; BC length, A=1 simple string
  12891.  
  12892. ; the only thing now is to consider if a slice is required.
  12893.  
  12894.         RST     18H             ; GET-CHAR puts character at CH_ADD in A
  12895.         JP      L2A49           ; jump forward to SV-SLICE? to test for '('
  12896.  
  12897. ; --------------------------------------------------------
  12898.  
  12899. ; the branch was here with string and numeric arrays in runtime.
  12900.  
  12901. ;; SV-ARRAYS
  12902. L29AE:  INC     HL              ; step past
  12903.         INC     HL              ; the total length
  12904.         INC     HL              ; to address Number of dimensions.
  12905.         LD      B,(HL)          ; transfer to B overwriting zero.
  12906.         BIT     6,C             ; a numeric array ?
  12907.         JR      Z,L29C0         ; forward to SV-PTR with numeric arrays
  12908.  
  12909.         DEC     B               ; ignore the final element of a string array
  12910.                                 ; the fixed string size.
  12911.  
  12912.         JR      Z,L29A1         ; back to SV-SIMPLE$ if result is zero as has
  12913.                                 ; been created with DIM a$(10) for instance
  12914.                                 ; and can be treated as a simple string.
  12915.  
  12916. ; proceed with multi-dimensioned string arrays in runtime.
  12917.  
  12918.         EX      DE,HL           ; save pointer to dimensions in DE
  12919.  
  12920.         RST     18H             ; GET-CHAR looks at the BASIC line
  12921.         CP      $28             ; is character '(' ?
  12922.         JR      NZ,L2A20        ; to REPORT-3 if not
  12923.                                 ; 'Subscript wrong'
  12924.  
  12925.         EX      DE,HL           ; dimensions pointer to HL to synchronize
  12926.                                 ; with next instruction.
  12927.  
  12928. ; runtime numeric arrays path rejoins here.
  12929.  
  12930. ;; SV-PTR
  12931. L29C0:  EX      DE,HL           ; save dimension pointer in DE
  12932.         JR      L29E7           ; forward to SV-COUNT with true no of dims
  12933.                                 ; in B. As there is no initial comma the
  12934.                                 ; loop is entered at the midpoint.
  12935.  
  12936. ; ----------------------------------------------------------
  12937. ; the dimension counting loop which is entered at mid-point.
  12938.  
  12939. ;; SV-COMMA
  12940. L29C3:  PUSH    HL              ; save counter
  12941.  
  12942.         RST     18H             ; GET-CHAR
  12943.  
  12944.         POP     HL              ; pop counter
  12945.         CP      $2C             ; is character ',' ?
  12946.         JR      Z,L29EA         ; forward to SV-LOOP if so
  12947.  
  12948. ; in runtime the variable definition indicates a comma should appear here
  12949.  
  12950.         BIT     7,C             ; checking syntax ?
  12951.         JR      Z,L2A20         ; forward to REPORT-3 if not
  12952.                                 ; 'Subscript error'
  12953.  
  12954. ; proceed if checking syntax of an array?
  12955.  
  12956.         BIT     6,C             ; array of strings
  12957.         JR      NZ,L29D8        ; forward to SV-CLOSE if so
  12958.  
  12959. ; an array of numbers.
  12960.  
  12961.         CP      $29             ; is character ')' ?
  12962.         JR      NZ,L2A12        ; forward to SV-RPT-C if not
  12963.                                 ; 'Nonsense in BASIC'
  12964.  
  12965.         RST     20H             ; NEXT-CHAR moves CH-ADD past the statement
  12966.         RET                     ; return ->
  12967.  
  12968. ; ---
  12969.  
  12970. ; the branch was here with an array of strings.
  12971.  
  12972. ;; SV-CLOSE
  12973. L29D8:  CP      $29             ; as above ')' could follow the expression
  12974.         JR      Z,L2A48         ; forward to SV-DIM if so
  12975.  
  12976.         CP      $CC             ; is it 'TO' ?
  12977.         JR      NZ,L2A12        ; to SV-RPT-C with anything else
  12978.                                 ; 'Nonsense in BASIC'
  12979.  
  12980. ; now backtrack CH_ADD to set up for slicing routine.
  12981. ; Note. in a BASIC line we can safely backtrack to a colour parameter.
  12982.  
  12983. ;; SV-CH-ADD
  12984. L29E0:  RST     18H             ; GET-CHAR
  12985.         DEC     HL              ; backtrack HL
  12986.         LD      ($5C5D),HL      ; to set CH_ADD up for slicing routine
  12987.         JR      L2A45           ; forward to SV-SLICE and make a return
  12988.                                 ; when all slicing complete.
  12989.  
  12990. ; ----------------------------------------
  12991. ; -> the mid-point entry point of the loop
  12992.  
  12993. ;; SV-COUNT
  12994. L29E7:  LD      HL,$0000        ; initialize data pointer to zero.
  12995.  
  12996. ;; SV-LOOP
  12997. L29EA:  PUSH    HL              ; save the data pointer.
  12998.  
  12999.         RST     20H             ; NEXT-CHAR in BASIC area points to an
  13000.                                 ; expression.
  13001.  
  13002.         POP     HL              ; restore the data pointer.
  13003.         LD      A,C             ; transfer name/type to A.
  13004.         CP      $C0             ; is it 11000000 ?
  13005.                                 ; Note. the letter component is absent if
  13006.                                 ; syntax checking.
  13007.         JR      NZ,L29FB        ; forward to SV-MULT if not an array of
  13008.                                 ; strings.
  13009.  
  13010. ; proceed to check string arrays during syntax.
  13011.  
  13012.         RST     18H             ; GET-CHAR
  13013.         CP      $29             ; ')'  end of subscripts ?
  13014.         JR      Z,L2A48         ; forward to SV-DIM to consider further slice
  13015.  
  13016.         CP      $CC             ; is it 'TO' ?
  13017.         JR      Z,L29E0         ; back to SV-CH-ADD to consider a slice.
  13018.                                 ; (no need to repeat get-char at L29E0)
  13019.  
  13020. ; if neither, then an expression is required so rejoin runtime loop ??
  13021. ; registers HL and DE only point to somewhere meaningful in runtime so
  13022. ; comments apply to that situation.
  13023.  
  13024. ;; SV-MULT
  13025. L29FB:  PUSH    BC              ; save dimension number.
  13026.         PUSH    HL              ; push data pointer/rubbish.
  13027.                                 ; DE points to current dimension.
  13028.         CALL    L2AEE           ; routine DE,(DE+1) gets next dimension in DE
  13029.                                 ; and HL points to it.
  13030.         EX      (SP),HL         ; dim pointer to stack, data pointer to HL (*)
  13031.         EX      DE,HL           ; data pointer to DE, dim size to HL.
  13032.  
  13033.         CALL    L2ACC           ; routine INT-EXP1 checks integer expression
  13034.                                 ; and gets result in BC in runtime.
  13035.         JR      C,L2A20         ; to REPORT-3 if > HL
  13036.                                 ; 'Subscript out of range'
  13037.  
  13038.         DEC     BC              ; adjust returned result from 1-x to 0-x
  13039.         CALL    L2AF4           ; routine GET-HL*DE multiplies data pointer by
  13040.                                 ; dimension size.
  13041.         ADD     HL,BC           ; add the integer returned by expression.
  13042.         POP     DE              ; pop the dimension pointer.                              ***
  13043.         POP     BC              ; pop dimension counter.
  13044.         DJNZ    L29C3           ; back to SV-COMMA if more dimensions
  13045.                                 ; Note. during syntax checking, unless there
  13046.                                 ; are more than 256 subscripts, the branch
  13047.                                 ; back to SV-COMMA is always taken.
  13048.  
  13049.         BIT     7,C             ; are we checking syntax ?
  13050.                                 ; then we've got a joker here.
  13051.  
  13052. ;; SV-RPT-C
  13053. L2A12:  JR      NZ,L2A7A        ; forward to SL-RPT-C if so
  13054.                                 ; 'Nonsense in BASIC'
  13055.                                 ; more than 256 subscripts in BASIC line.
  13056.  
  13057. ; but in runtime the number of subscripts are at least the same as dims
  13058.  
  13059.         PUSH    HL              ; save data pointer.
  13060.         BIT     6,C             ; is it a string array ?
  13061.         JR      NZ,L2A2C        ; forward to SV-ELEM$ if so.
  13062.  
  13063. ; a runtime numeric array subscript.
  13064.  
  13065.         LD      B,D             ; register DE has advanced past all dimensions
  13066.         LD      C,E             ; and points to start of data in variable.
  13067.                                 ; transfer it to BC.
  13068.  
  13069.         RST     18H             ; GET-CHAR checks BASIC line
  13070.         CP      $29             ; must be a ')' ?
  13071.         JR      Z,L2A22         ; skip to SV-NUMBER if so
  13072.  
  13073. ; else more subscripts in BASIC line than the variable definition.
  13074.  
  13075. ;; REPORT-3
  13076. L2A20:  RST     08H             ; ERROR-1
  13077.         DB    $02             ; Error Report: Subscript wrong
  13078.  
  13079. ; continue if subscripts matched the numeric array.
  13080.  
  13081. ;; SV-NUMBER
  13082. L2A22:  RST     20H             ; NEXT-CHAR moves CH_ADD to next statement
  13083.                                 ; - finished parsing.
  13084.  
  13085.         POP     HL              ; pop the data pointer.
  13086.         LD      DE,$0005        ; each numeric element is 5 bytes.
  13087.         CALL    L2AF4           ; routine GET-HL*DE multiplies.
  13088.         ADD     HL,BC           ; now add to start of data in the variable.
  13089.  
  13090.         RET                     ; return with HL pointing at the numeric
  13091.                                 ; array subscript.                       ->
  13092.  
  13093. ; ---------------------------------------------------------------
  13094.  
  13095. ; the branch was here for string subscripts when the number of subscripts
  13096. ; in the BASIC line was one less than in variable definition.
  13097.  
  13098. ;; SV-ELEM$
  13099. L2A2C:  CALL    L2AEE           ; routine DE,(DE+1) gets final dimension
  13100.                                 ; the length of strings in this array.
  13101.         EX      (SP),HL         ; start pointer to stack, data pointer to HL.
  13102.         CALL    L2AF4           ; routine GET-HL*DE multiplies by element
  13103.                                 ; size.
  13104.         POP     BC              ; the start of data pointer is added
  13105.         ADD     HL,BC           ; in - now points to location before.
  13106.         INC     HL              ; point to start of required string.
  13107.         LD      B,D             ; transfer the length (final dimension size)
  13108.         LD      C,E             ; from DE to BC.
  13109.         EX      DE,HL           ; put start in DE.
  13110.         CALL    L2AB1           ; routine STK-ST-0 stores the string parameters
  13111.                                 ; with A=0 - a slice or subscript.
  13112.  
  13113. ; now check that there were no more subscripts in the BASIC line.
  13114.  
  13115.         RST     18H             ; GET-CHAR
  13116.         CP      $29             ; is it ')' ?
  13117.         JR      Z,L2A48         ; forward to SV-DIM to consider a separate
  13118.                                 ; subscript or/and a slice.
  13119.  
  13120.         CP      $2C             ; a comma is allowed if the final subscript
  13121.                                 ; is to be sliced e.g a$(2,3,4 TO 6).
  13122.         JR      NZ,L2A20        ; to REPORT-3 with anything else
  13123.                                 ; 'Subscript error'
  13124.  
  13125. ;; SV-SLICE
  13126. L2A45:  CALL    L2A52           ; routine SLICING slices the string.
  13127.  
  13128. ; but a slice of a simple string can itself be sliced.
  13129.  
  13130. ;; SV-DIM
  13131. L2A48:  RST     20H             ; NEXT-CHAR
  13132.  
  13133. ;; SV-SLICE?
  13134. L2A49:  CP      $28             ; is character '(' ?
  13135.         JR      Z,L2A45         ; loop back if so to SV-SLICE
  13136.  
  13137.         RES     6,(IY+$01)      ; update FLAGS  - Signal string result
  13138.         RET                     ; and return.
  13139.  
  13140. ; ---
  13141.  
  13142. ; The above section deals with the flexible syntax allowed.
  13143. ; DIM a$(3,3,10) can be considered as two dimensional array of ten-character
  13144. ; strings or a 3-dimensional array of characters.
  13145. ; a$(1,1) will return a 10-character string as will a$(1,1,1 TO 10)
  13146. ; a$(1,1,1) will return a single character.
  13147. ; a$(1,1) (1 TO 6) is the same as a$(1,1,1 TO 6)
  13148. ; A slice can itself be sliced ad infinitum
  13149. ; b$ () () () () () () (2 TO 10) (2 TO 9) (3) is the same as b$(5)
  13150.  
  13151.  
  13152.  
  13153. ; -------------------------
  13154. ; Handle slicing of strings
  13155. ; -------------------------
  13156. ; The syntax of string slicing is very natural and it is as well to reflect
  13157. ; on the permutations possible.
  13158. ; a$() and a$( TO ) indicate the entire string although just a$ would do
  13159. ; and would avoid coming here.
  13160. ; h$(16) indicates the single character at position 16.
  13161. ; a$( TO 32) indicates the first 32 characters.
  13162. ; a$(257 TO) indicates all except the first 256 characters.
  13163. ; a$(19000 TO 19999) indicates the thousand characters at position 19000.
  13164. ; Also a$(9 TO 5) returns a null string not an error.
  13165. ; This enables a$(2 TO) to return a null string if the passed string is
  13166. ; of length zero or 1.
  13167. ; A string expression in brackets can be sliced. e.g. (STR$ PI) (3 TO )
  13168. ; We arrived here from SCANNING with CH-ADD pointing to the initial '('
  13169. ; or from above.
  13170.  
  13171. ;; SLICING
  13172. L2A52:  CALL    L2530           ; routine SYNTAX-Z
  13173.         CALL    NZ,L2BF1        ; routine STK-FETCH fetches parameters of
  13174.                                 ; string at runtime, start in DE, length
  13175.                                 ; in BC. This could be an array subscript.
  13176.  
  13177.         RST     20H             ; NEXT-CHAR
  13178.         CP      $29             ; is it ')' ?     e.g. a$()
  13179.         JR      Z,L2AAD         ; forward to SL-STORE to store entire string.
  13180.  
  13181.         PUSH    DE              ; else save start address of string
  13182.  
  13183.         XOR     A               ; clear accumulator to use as a running flag.
  13184.         PUSH    AF              ; and save on stack before any branching.
  13185.  
  13186.         PUSH    BC              ; save length of string to be sliced.
  13187.         LD      DE,$0001        ; default the start point to position 1.
  13188.  
  13189.         RST     18H             ; GET-CHAR
  13190.  
  13191.         POP     HL              ; pop length to HL as default end point
  13192.                                 ; and limit.
  13193.  
  13194.         CP      $CC             ; is it 'TO' ?    e.g. a$( TO 10000)
  13195.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter.
  13196.  
  13197.         POP     AF              ; pop the running flag.
  13198.  
  13199.         CALL    L2ACD           ; routine INT-EXP2 fetches first parameter.
  13200.  
  13201.         PUSH    AF              ; save flag (will be $FF if parameter>limit)
  13202.  
  13203.         LD      D,B             ; transfer the start
  13204.         LD      E,C             ; to DE overwriting 0001.
  13205.         PUSH    HL              ; save original length.
  13206.  
  13207.         RST     18H             ; GET-CHAR
  13208.         POP     HL              ; pop the limit length.
  13209.         CP      $CC             ; is it 'TO' after a start ?
  13210.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter
  13211.  
  13212.         CP      $29             ; is it ')' ?       e.g. a$(365)
  13213.  
  13214. ;; SL-RPT-C
  13215. L2A7A:  JP      NZ,L1C8A        ; jump to REPORT-C with anything else
  13216.                                 ; 'Nonsense in BASIC'
  13217.  
  13218.         LD      H,D             ; copy start
  13219.         LD      L,E             ; to end - just a one character slice.
  13220.         JR      L2A94           ; forward to SL-DEFINE.
  13221.  
  13222. ; ---------------------
  13223.  
  13224. ;; SL-SECOND
  13225. L2A81:  PUSH    HL              ; save limit length.
  13226.  
  13227.         RST     20H             ; NEXT-CHAR
  13228.  
  13229.         POP     HL              ; pop the length.
  13230.  
  13231.         CP      $29             ; is character ')' ?        e.g a$(7 TO )
  13232.         JR      Z,L2A94         ; to SL-DEFINE using length as end point.
  13233.  
  13234.         POP     AF              ; else restore flag.
  13235.         CALL    L2ACD           ; routine INT-EXP2 gets second expression.
  13236.  
  13237.         PUSH    AF              ; save the running flag.
  13238.  
  13239.         RST     18H             ; GET-CHAR
  13240.  
  13241.         LD      H,B             ; transfer second parameter
  13242.         LD      L,C             ; to HL.              e.g. a$(42 to 99)
  13243.         CP      $29             ; is character a ')' ?
  13244.         JR      NZ,L2A7A        ; to SL-RPT-C if not
  13245.                                 ; 'Nonsense in BASIC'
  13246.  
  13247. ; we now have start in DE and an end in HL.
  13248.  
  13249. ;; SL-DEFINE
  13250. L2A94:  POP     AF              ; pop the running flag.
  13251.         EX      (SP),HL         ; put end point on stack, start address to HL
  13252.         ADD     HL,DE           ; add address of string to the start point.
  13253.         DEC     HL              ; point to first character of slice.
  13254.         EX      (SP),HL         ; start address to stack, end point to HL (*)
  13255.         AND     A               ; prepare to subtract.
  13256.         SBC     HL,DE           ; subtract start point from end point.
  13257.         LD      BC,$0000        ; default the length result to zero.
  13258.         JR      C,L2AA8         ; forward to SL-OVER if start > end.
  13259.  
  13260.         INC     HL              ; increment the length for inclusive byte.
  13261.  
  13262.         AND     A               ; now test the running flag.
  13263.         JP      M,L2A20         ; jump back to REPORT-3 if $FF.
  13264.                                 ; 'Subscript out of range'
  13265.  
  13266.         LD      B,H             ; transfer the length
  13267.         LD      C,L             ; to BC.
  13268.  
  13269. ;; SL-OVER
  13270. L2AA8:  POP     DE              ; restore start address from machine stack ***
  13271.         RES     6,(IY+$01)      ; update FLAGS - signal string result for
  13272.                                 ; syntax.
  13273.  
  13274. ;; SL-STORE
  13275. L2AAD:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  13276.         RET     Z               ; return if checking syntax.
  13277.                                 ; but continue to store the string in runtime.
  13278.  
  13279. ; ------------------------------------
  13280. ; other than from above, this routine is called from STK-VAR to stack
  13281. ; a known string array element.
  13282. ; ------------------------------------
  13283.  
  13284. ;; STK-ST-0
  13285. L2AB1:  XOR     A               ; clear to signal a sliced string or element.
  13286.  
  13287. ; -------------------------
  13288. ; this routine is called from chr$, scrn$ etc. to store a simple string result.
  13289. ; --------------------------
  13290.  
  13291. ;; STK-STO-$
  13292. L2AB2:  RES     6,(IY+$01)      ; update FLAGS - signal string result.
  13293.                                 ; and continue to store parameters of string.
  13294.  
  13295. ; ---------------------------------------
  13296. ; Pass five registers to calculator stack
  13297. ; ---------------------------------------
  13298. ; This subroutine puts five registers on the calculator stack.
  13299.  
  13300. ;; STK-STORE
  13301. L2AB6:  PUSH    BC              ; save two registers
  13302.         CALL    L33A9           ; routine TEST-5-SP checks room and puts 5
  13303.                                 ; in BC.
  13304.         POP     BC              ; fetch the saved registers.
  13305.         LD      HL,($5C65)      ; make HL point to first empty location STKEND
  13306.         LD      (HL),A          ; place the 5 registers.
  13307.         INC     HL              ;
  13308.         LD      (HL),E          ;
  13309.         INC     HL              ;
  13310.         LD      (HL),D          ;
  13311.         INC     HL              ;
  13312.         LD      (HL),C          ;
  13313.         INC     HL              ;
  13314.         LD      (HL),B          ;
  13315.         INC     HL              ;
  13316.         LD      ($5C65),HL      ; update system variable STKEND.
  13317.         RET                     ; and return.
  13318.  
  13319. ; -------------------------------------------
  13320. ; Return result of evaluating next expression
  13321. ; -------------------------------------------
  13322. ; This clever routine is used to check and evaluate an integer expression
  13323. ; which is returned in BC, setting A to $FF, if greater than a limit supplied
  13324. ; in HL. It is used to check array subscripts, parameters of a string slice
  13325. ; and the arguments of the DIM command. In the latter case, the limit check
  13326. ; is not required and H is set to $FF. When checking optional string slice
  13327. ; parameters, it is entered at the second entry point so as not to disturb
  13328. ; the running flag A, which may be $00 or $FF from a previous invocation.
  13329.  
  13330. ;; INT-EXP1
  13331. L2ACC:  XOR     A               ; set result flag to zero.
  13332.  
  13333. ; -> The entry point is here if A is used as a running flag.
  13334.  
  13335. ;; INT-EXP2
  13336. L2ACD:  PUSH    DE              ; preserve DE register throughout.
  13337.         PUSH    HL              ; save the supplied limit.
  13338.         PUSH    AF              ; save the flag.
  13339.  
  13340.         CALL    L1C82           ; routine EXPT-1NUM evaluates expression
  13341.                                 ; at CH_ADD returning if numeric result,
  13342.                                 ; with value on calculator stack.
  13343.  
  13344.         POP     AF              ; pop the flag.
  13345.         CALL    L2530           ; routine SYNTAX-Z
  13346.         JR      Z,L2AEB         ; forward to I-RESTORE if checking syntax so
  13347.                                 ; avoiding a comparison with supplied limit.
  13348.  
  13349.         PUSH    AF              ; save the flag.
  13350.  
  13351.         CALL    L1E99           ; routine FIND-INT2 fetches value from
  13352.                                 ; calculator stack to BC producing an error
  13353.                                 ; if too high.
  13354.  
  13355.         POP     DE              ; pop the flag to D.
  13356.         LD      A,B             ; test value for zero and reject
  13357.         OR      C               ; as arrays and strings begin at 1.
  13358.         SCF                     ; set carry flag.
  13359.         JR      Z,L2AE8         ; forward to I-CARRY if zero.
  13360.  
  13361.         POP     HL              ; restore the limit.
  13362.         PUSH    HL              ; and save.
  13363.         AND     A               ; prepare to subtract.
  13364.         SBC     HL,BC           ; subtract value from limit.
  13365.  
  13366. ;; I-CARRY
  13367. L2AE8:  LD      A,D             ; move flag to accumulator $00 or $FF.
  13368.         SBC     A,$00           ; will set to $FF if carry set.
  13369.  
  13370. ;; I-RESTORE
  13371. L2AEB:  POP     HL              ; restore the limit.
  13372.         POP     DE              ; and DE register.
  13373.         RET                     ; return.
  13374.  
  13375.  
  13376. ; -----------------------
  13377. ; LD DE,(DE+1) Subroutine
  13378. ; -----------------------
  13379. ; This routine just loads the DE register with the contents of the two
  13380. ; locations following the location addressed by DE.
  13381. ; It is used to step along the 16-bit dimension sizes in array definitions.
  13382. ; Note. Such code is made into subroutines to make programs easier to
  13383. ; write and it would use less space to include the five instructions in-line.
  13384. ; However, there are so many exchanges going on at the places this is invoked
  13385. ; that to implement it in-line would make the code hard to follow.
  13386. ; It probably had a zippier label though as the intention is to simplify the
  13387. ; program.
  13388.  
  13389. ;; DE,(DE+1)
  13390. L2AEE:  EX      DE,HL           ;
  13391.         INC     HL              ;
  13392.         LD      E,(HL)          ;
  13393.         INC     HL              ;
  13394.         LD      D,(HL)          ;
  13395.         RET                     ;
  13396.  
  13397. ; -------------------
  13398. ; HL=HL*DE Subroutine
  13399. ; -------------------
  13400. ; This routine calls the mathematical routine to multiply HL by DE in runtime.
  13401. ; It is called from STK-VAR and from DIM. In the latter case syntax is not
  13402. ; being checked so the entry point could have been at the second CALL
  13403. ; instruction to save a few clock-cycles.
  13404.  
  13405. ;; GET-HL*DE
  13406. L2AF4:  CALL    L2530           ; routine SYNTAX-Z.
  13407.         RET     Z               ; return if checking syntax.
  13408.  
  13409.         CALL    L30A9           ; routine HL-HL*DE.
  13410.         JP      C,L1F15         ; jump back to REPORT-4 if over 65535.
  13411.  
  13412.         RET                     ; else return with 16-bit result in HL.
  13413.  
  13414. ; -----------------
  13415. ; THE 'LET' COMMAND
  13416. ; -----------------
  13417. ; Sinclair BASIC adheres to the ANSI-78 standard and a LET is required in
  13418. ; assignments e.g. LET a = 1  :   LET h$ = "hat".
  13419. ;
  13420. ; Long names may contain spaces but not colour controls (when assigned).
  13421. ; a substring can appear to the left of the equals sign.
  13422.  
  13423. ; An earlier mathematician Lewis Carroll may have been pleased that
  13424. ; 10 LET Babies cannot manage crocodiles = Babies are illogical AND
  13425. ;    Nobody is despised who can manage a crocodile AND Illogical persons
  13426. ;    are despised
  13427. ; does not give the 'Nonsense..' error if the three variables exist.
  13428. ; I digress.
  13429.  
  13430. ;; LET
  13431. L2AFF:  LD      HL,($5C4D)      ; fetch system variable DEST to HL.
  13432.         BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  13433.         JR      Z,L2B66         ; forward to L-EXISTS if not.
  13434.  
  13435. ; continue for a new variable. DEST points to start in BASIC line.
  13436. ; from the CLASS routines.
  13437.  
  13438.         LD      BC,$0005        ; assume numeric and assign an initial 5 bytes
  13439.  
  13440. ;; L-EACH-CH
  13441. L2B0B:  INC     BC              ; increase byte count for each relevant
  13442.                                 ; character
  13443.  
  13444. ;; L-NO-SP
  13445. L2B0C:  INC     HL              ; increase pointer.
  13446.         LD      A,(HL)          ; fetch character.
  13447.         CP      $20             ; is it a space ?
  13448.         JR      Z,L2B0C         ; back to L-NO-SP is so.
  13449.  
  13450.         JR      NC,L2B1F        ; forward to L-TEST-CH if higher.
  13451.  
  13452.         CP      $10             ; is it $00 - $0F ?
  13453.         JR      C,L2B29         ; forward to L-SPACES if so.
  13454.  
  13455.         CP      $16             ; is it $16 - $1F ?
  13456.         JR      NC,L2B29        ; forward to L-SPACES if so.
  13457.  
  13458. ; it was $10 - $15  so step over a colour code.
  13459.  
  13460.         INC     HL              ; increase pointer.
  13461.         JR      L2B0C           ; loop back to L-NO-SP.
  13462.  
  13463. ; ---
  13464.  
  13465. ; the branch was to here if higher than space.
  13466.  
  13467. ;; L-TEST-CH
  13468. L2B1F:  CALL    L2C88           ; routine ALPHANUM sets carry if alphanumeric
  13469.         JR      C,L2B0B         ; loop back to L-EACH-CH for more if so.
  13470.  
  13471.         CP      $24             ; is it '$' ?
  13472.         JP      Z,L2BC0         ; jump forward if so, to L-NEW$
  13473.                                 ; with a new string.
  13474.  
  13475. ;; L-SPACES
  13476. L2B29:  LD      A,C             ; save length lo in A.
  13477.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  13478.         DEC     HL              ; point to location before, the variables
  13479.                                 ; end-marker.
  13480.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces
  13481.                                 ; for name and numeric value.
  13482.         INC     HL              ; advance to first new location.
  13483.         INC     HL              ; then to second.
  13484.         EX      DE,HL           ; set DE to second location.
  13485.         PUSH    DE              ; save this pointer.
  13486.         LD      HL,($5C4D)      ; reload HL with DEST.
  13487.         DEC     DE              ; point to first.
  13488.         SUB     $06             ; subtract six from length_lo.
  13489.         LD      B,A             ; save count in B.
  13490.         JR      Z,L2B4F         ; forward to L-SINGLE if it was just
  13491.                                 ; one character.
  13492.  
  13493. ; HL points to start of variable name after 'LET' in BASIC line.
  13494.  
  13495. ;; L-CHAR
  13496. L2B3E:  INC     HL              ; increase pointer.
  13497.         LD      A,(HL)          ; pick up character.
  13498.         CP      $21             ; is it space or higher ?
  13499.         JR      C,L2B3E         ; back to L-CHAR with space and less.
  13500.  
  13501.         OR      $20             ; make variable lower-case.
  13502.         INC     DE              ; increase destination pointer.
  13503.         LD      (DE),A          ; and load to edit line.
  13504.         DJNZ    L2B3E           ; loop back to L-CHAR until B is zero.
  13505.  
  13506.         OR      $80             ; invert the last character.
  13507.         LD      (DE),A          ; and overwrite that in edit line.
  13508.  
  13509. ; now consider first character which has bit 6 set
  13510.  
  13511.         LD      A,$C0           ; set A 11000000 is xor mask for a long name.
  13512.                                 ; %101      is xor/or  result
  13513.  
  13514. ; single character numerics rejoin here with %00000000 in mask.
  13515. ;                                            %011      will be xor/or result
  13516.  
  13517. ;; L-SINGLE
  13518. L2B4F:  LD      HL,($5C4D)      ; fetch DEST - HL addresses first character.
  13519.         XOR     (HL)            ; apply variable type indicator mask (above).
  13520.         OR      $20             ; make lowercase - set bit 5.
  13521.         POP     HL              ; restore pointer to 2nd character.
  13522.         CALL    L2BEA           ; routine L-FIRST puts A in first character.
  13523.                                 ; and returns with HL holding
  13524.                                 ; new E_LINE-1  the $80 vars end-marker.
  13525.  
  13526. ;; L-NUMERIC
  13527. L2B59:  PUSH    HL              ; save the pointer.
  13528.  
  13529. ; the value of variable is deleted but remains after calculator stack.
  13530.  
  13531.         RST     28H             ;; FP-CALC
  13532.         DB    $02             ;;delete      ; delete variable value
  13533.         DB    $38             ;;end-calc
  13534.  
  13535. ; DE (STKEND) points to start of value.
  13536.  
  13537.         POP     HL              ; restore the pointer.
  13538.         LD      BC,$0005        ; start of number is five bytes before.
  13539.         AND     A               ; prepare for true subtraction.
  13540.         SBC     HL,BC           ; HL points to start of value.
  13541.         JR      L2BA6           ; forward to L-ENTER  ==>
  13542.  
  13543. ; ---
  13544.  
  13545.  
  13546. ; the jump was to here if the variable already existed.
  13547.  
  13548. ;; L-EXISTS
  13549. L2B66:  BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  13550.         JR      Z,L2B72         ; skip forward to L-DELETE$   -*->
  13551.                                 ; if string result.
  13552.  
  13553. ; A numeric variable could be simple or an array element.
  13554. ; They are treated the same and the old value is overwritten.
  13555.  
  13556.         LD      DE,$0006        ; six bytes forward points to loc past value.
  13557.         ADD     HL,DE           ; add to start of number.
  13558.         JR      L2B59           ; back to L-NUMERIC to overwrite value.
  13559.  
  13560. ; ---
  13561.  
  13562. ; -*-> the branch was here if a string existed.
  13563.  
  13564. ;; L-DELETE$
  13565. L2B72:  LD      HL,($5C4D)      ; fetch DEST to HL.
  13566.                                 ; (still set from first instruction)
  13567.         LD      BC,($5C72)      ; fetch STRLEN to BC.
  13568.         BIT     0,(IY+$37)      ; test FLAGX - handling a complete simple
  13569.                                 ; string ?
  13570.         JR      NZ,L2BAF        ; forward to L-ADD$ if so.
  13571.  
  13572. ; must be a string array or a slice in workspace.
  13573. ; Note. LET a$(3 TO 6) = h$   will assign "hat " if h$ = "hat"
  13574. ;                                  and    "hats" if h$ = "hatstand".
  13575. ;
  13576. ; This is known as Procrustian lengthening and shortening after a
  13577. ; character Procrustes in Greek legend who made travellers sleep in his bed,
  13578. ; cutting off their feet or stretching them so they fitted the bed perfectly.
  13579. ; The bloke was hatstand and slain by Theseus.
  13580.  
  13581.         LD      A,B             ; test if length
  13582.         OR      C               ; is zero and
  13583.         RET     Z               ; return if so.
  13584.  
  13585.         PUSH    HL              ; save pointer to start.
  13586.  
  13587.         RST     30H             ; BC-SPACES creates room.
  13588.         PUSH    DE              ; save pointer to first new location.
  13589.         PUSH    BC              ; and length            (*)
  13590.         LD      D,H             ; set DE to point to last location.
  13591.         LD      E,L             ;
  13592.         INC     HL              ; set HL to next location.
  13593.         LD      (HL),$20        ; place a space there.
  13594.         LDDR                    ; copy bytes filling with spaces.
  13595.  
  13596.         PUSH    HL              ; save pointer to start.
  13597.         CALL    L2BF1           ; routine STK-FETCH start to DE,
  13598.                                 ; length to BC.
  13599.         POP     HL              ; restore the pointer.
  13600.         EX      (SP),HL         ; (*) length to HL, pointer to stack.
  13601.         AND     A               ; prepare for true subtraction.
  13602.         SBC     HL,BC           ; subtract old length from new.
  13603.         ADD     HL,BC           ; and add back.
  13604.         JR      NC,L2B9B        ; forward if it fits to L-LENGTH.
  13605.  
  13606.         LD      B,H             ; otherwise set
  13607.         LD      C,L             ; length to old length.
  13608.                                 ; "hatstand" becomes "hats"
  13609.  
  13610. ;; L-LENGTH
  13611. L2B9B:  EX      (SP),HL         ; (*) length to stack, pointer to HL.
  13612.         EX      DE,HL           ; pointer to DE, start of string to HL.
  13613.         LD      A,B             ; is the length zero ?
  13614.         OR      C               ;
  13615.         JR      Z,L2BA3         ; forward to L-IN-W/S if so
  13616.                                 ; leaving prepared spaces.
  13617.  
  13618.         LDIR                    ; else copy bytes overwriting some spaces.
  13619.  
  13620. ;; L-IN-W/S
  13621. L2BA3:  POP     BC              ; pop the new length.  (*)
  13622.         POP     DE              ; pop pointer to new area.
  13623.         POP     HL              ; pop pointer to variable in assignment.
  13624.                                 ; and continue copying from workspace
  13625.                                 ; to variables area.
  13626.  
  13627. ; ==> branch here from  L-NUMERIC
  13628.  
  13629. ;; L-ENTER
  13630. L2BA6:  EX      DE,HL           ; exchange pointers HL=STKEND DE=end of vars.
  13631.         LD      A,B             ; test the length
  13632.         OR      C               ; and make a
  13633.         RET     Z               ; return if zero (strings only).
  13634.  
  13635.         PUSH    DE              ; save start of destination.
  13636.         LDIR                    ; copy bytes.
  13637.         POP     HL              ; address the start.
  13638.         RET                     ; and return.
  13639.  
  13640. ; ---
  13641.  
  13642. ; the branch was here from L-DELETE$ if an existing simple string.
  13643. ; register HL addresses start of string in variables area.
  13644.  
  13645. ;; L-ADD$
  13646. L2BAF:  DEC     HL              ; point to high byte of length.
  13647.         DEC     HL              ; to low byte.
  13648.         DEC     HL              ; to letter.
  13649.         LD      A,(HL)          ; fetch masked letter to A.
  13650.         PUSH    HL              ; save the pointer on stack.
  13651.         PUSH    BC              ; save new length.
  13652.         CALL    L2BC6           ; routine L-STRING adds new string at end
  13653.                                 ; of variables area.
  13654.                                 ; if no room we still have old one.
  13655.         POP     BC              ; restore length.
  13656.         POP     HL              ; restore start.
  13657.         INC     BC              ; increase
  13658.         INC     BC              ; length by three
  13659.         INC     BC              ; to include character and length bytes.
  13660.         JP      L19E8           ; jump to indirect exit via RECLAIM-2
  13661.                                 ; deleting old version and adjusting pointers.
  13662.  
  13663. ; ---
  13664.  
  13665. ; the jump was here with a new string variable.
  13666.  
  13667. ;; L-NEW$
  13668. L2BC0:  LD      A,$DF           ; indicator mask %11011111 for
  13669.                                 ;                %010xxxxx will be result
  13670.         LD      HL,($5C4D)      ; address DEST first character.
  13671.         AND     (HL)            ; combine mask with character.
  13672.  
  13673. ;; L-STRING
  13674. L2BC6:  PUSH    AF              ; save first character and mask.
  13675.         CALL    L2BF1           ; routine STK-FETCH fetches parameters of
  13676.                                 ; the string.
  13677.         EX      DE,HL           ; transfer start to HL.
  13678.         ADD     HL,BC           ; add to length.
  13679.         PUSH    BC              ; save the length.
  13680.         DEC     HL              ; point to end of string.
  13681.         LD      ($5C4D),HL      ; save pointer in DEST.
  13682.                                 ; (updated by POINTERS if in workspace)
  13683.         INC     BC              ; extra byte for letter.
  13684.         INC     BC              ; two bytes
  13685.         INC     BC              ; for the length of string.
  13686.         LD      HL,($5C59)      ; address E_LINE.
  13687.         DEC     HL              ; now end of VARS area.
  13688.         CALL    L1655           ; routine MAKE-ROOM makes room for string.
  13689.                                 ; updating pointers including DEST.
  13690.         LD      HL,($5C4D)      ; pick up pointer to end of string from DEST.
  13691.         POP     BC              ; restore length from stack.
  13692.         PUSH    BC              ; and save again on stack.
  13693.         INC     BC              ; add a byte.
  13694.         LDDR                    ; copy bytes from end to start.
  13695.         EX      DE,HL           ; HL addresses length low
  13696.         INC     HL              ; increase to address high byte
  13697.         POP     BC              ; restore length to BC
  13698.         LD      (HL),B          ; insert high byte
  13699.         DEC     HL              ; address low byte location
  13700.         LD      (HL),C          ; insert that byte
  13701.         POP     AF              ; restore character and mask
  13702.  
  13703. ;; L-FIRST
  13704. L2BEA:  DEC     HL              ; address variable name
  13705.         LD      (HL),A          ; and insert character.
  13706.         LD      HL,($5C59)      ; load HL with E_LINE.
  13707.         DEC     HL              ; now end of VARS area.
  13708.         RET                     ; return
  13709.  
  13710. ; ------------------------------------
  13711. ; Get last value from calculator stack
  13712. ; ------------------------------------
  13713. ;
  13714. ;
  13715.  
  13716. ;; STK-FETCH
  13717. L2BF1:  LD      HL,($5C65)      ; STKEND
  13718.         DEC     HL              ;
  13719.         LD      B,(HL)          ;
  13720.         DEC     HL              ;
  13721.         LD      C,(HL)          ;
  13722.         DEC     HL              ;
  13723.         LD      D,(HL)          ;
  13724.         DEC     HL              ;
  13725.         LD      E,(HL)          ;
  13726.         DEC     HL              ;
  13727.         LD      A,(HL)          ;
  13728.         LD      ($5C65),HL      ; STKEND
  13729.         RET                     ;
  13730.  
  13731. ; ------------------
  13732. ; Handle DIM command
  13733. ; ------------------
  13734. ; e.g. DIM a(2,3,4,7): DIM a$(32) : DIM b$(300,2,768) : DIM c$(20000)
  13735. ; the only limit to dimensions is memory so, for example,
  13736. ; DIM a(2,2,2,2,2,2,2,2,2,2,2,2,2) is possible and creates a multi-
  13737. ; dimensional array of zeros. String arrays are initialized to spaces.
  13738. ; It is not possible to erase an array, but it can be re-dimensioned to
  13739. ; a minimal size of 1, after use, to free up memory.
  13740.  
  13741. ;; DIM
  13742. L2C02:  CALL    L28B2           ; routine LOOK-VARS
  13743.  
  13744. ;; D-RPORT-C
  13745. L2C05:  JP      NZ,L1C8A        ; jump to REPORT-C if a long-name variable.
  13746.                                 ; DIM lottery numbers(49) doesn't work.
  13747.  
  13748.         CALL    L2530           ; routine SYNTAX-Z
  13749.         JR      NZ,L2C15        ; forward to D-RUN in runtime.
  13750.  
  13751.         RES     6,C             ; signal 'numeric' array even if string as
  13752.                                 ; this simplifies the syntax checking.
  13753.  
  13754.         CALL    L2996           ; routine STK-VAR checks syntax.
  13755.         CALL    L1BEE           ; routine CHECK-END performs early exit ->
  13756.  
  13757. ; the branch was here in runtime.
  13758.  
  13759. ;; D-RUN
  13760. L2C15:  JR      C,L2C1F         ; skip to D-LETTER if variable did not exist.
  13761.                                 ; else reclaim the old one.
  13762.  
  13763.         PUSH    BC              ; save type in C.
  13764.         CALL    L19B8           ; routine NEXT-ONE find following variable
  13765.                                 ; or position of $80 end-marker.
  13766.         CALL    L19E8           ; routine RECLAIM-2 reclaims the
  13767.                                 ; space between.
  13768.         POP     BC              ; pop the type.
  13769.  
  13770. ;; D-LETTER
  13771. L2C1F:  SET     7,C             ; signal array.
  13772.         LD      B,$00           ; initialize dimensions to zero and
  13773.         PUSH    BC              ; save with the type.
  13774.         LD      HL,$0001        ; make elements one character presuming string
  13775.         BIT     6,C             ; is it a string ?
  13776.         JR      NZ,L2C2D        ; forward to D-SIZE if so.
  13777.  
  13778.         LD      L,$05           ; make elements 5 bytes as is numeric.
  13779.  
  13780. ;; D-SIZE
  13781. L2C2D:  EX      DE,HL           ; save the element size in DE.
  13782.  
  13783. ; now enter a loop to parse each of the integers in the list.
  13784.  
  13785. ;; D-NO-LOOP
  13786. L2C2E:  RST     20H             ; NEXT-CHAR
  13787.         LD      H,$FF           ; disable limit check by setting HL high
  13788.         CALL    L2ACC           ; routine INT-EXP1
  13789.         JP      C,L2A20         ; to REPORT-3 if > 65280 and then some
  13790.                                 ; 'Subscript out of range'
  13791.  
  13792.         POP     HL              ; pop dimension counter, array type
  13793.         PUSH    BC              ; save dimension size                     ***
  13794.         INC     H               ; increment the dimension counter
  13795.         PUSH    HL              ; save the dimension counter
  13796.         LD      H,B             ; transfer size
  13797.         LD      L,C             ; to HL
  13798.         CALL    L2AF4           ; routine GET-HL*DE multiplies dimension by
  13799.                                 ; running total of size required initially
  13800.                                 ; 1 or 5.
  13801.         EX      DE,HL           ; save running total in DE
  13802.  
  13803.         RST     18H             ; GET-CHAR
  13804.         CP      $2C             ; is it ',' ?
  13805.         JR      Z,L2C2E         ; loop back to D-NO-LOOP until all dimensions
  13806.                                 ; have been considered
  13807.  
  13808. ; when loop complete continue.
  13809.  
  13810.         CP      $29             ; is it ')' ?
  13811.         JR      NZ,L2C05        ; to D-RPORT-C with anything else
  13812.                                 ; 'Nonsense in BASIC'
  13813.  
  13814.  
  13815.         RST     20H             ; NEXT-CHAR advances to next statement/CR
  13816.  
  13817.         POP     BC              ; pop dimension counter/type
  13818.         LD      A,C             ; type to A
  13819.  
  13820. ; now calculate space required for array variable
  13821.  
  13822.         LD      L,B             ; dimensions to L since these require 16 bits
  13823.                                 ; then this value will be doubled
  13824.         LD      H,$00           ; set high byte to zero
  13825.  
  13826. ; another four bytes are required for letter(1), total length(2), number of
  13827. ; dimensions(1) but since we have yet to double allow for two
  13828.  
  13829.         INC     HL              ; increment
  13830.         INC     HL              ; increment
  13831.  
  13832.         ADD     HL,HL           ; now double giving 4 + dimensions * 2
  13833.  
  13834.         ADD     HL,DE           ; add to space required for array contents
  13835.  
  13836.         JP      C,L1F15         ; to REPORT-4 if > 65535
  13837.                                 ; 'Out of memory'
  13838.  
  13839.         PUSH    DE              ; save data space
  13840.         PUSH    BC              ; save dimensions/type
  13841.         PUSH    HL              ; save total space
  13842.         LD      B,H             ; total space
  13843.         LD      C,L             ; to BC
  13844.         LD      HL,($5C59)      ; address E_LINE - first location after
  13845.                                 ; variables area
  13846.         DEC     HL              ; point to location before - the $80 end-marker
  13847.         CALL    L1655           ; routine MAKE-ROOM creates the space if
  13848.                                 ; memory is available.
  13849.  
  13850.         INC     HL              ; point to first new location and
  13851.         LD      (HL),A          ; store letter/type
  13852.  
  13853.         POP     BC              ; pop total space
  13854.         DEC     BC              ; exclude name
  13855.         DEC     BC              ; exclude the 16-bit
  13856.         DEC     BC              ; counter itself
  13857.         INC     HL              ; point to next location the 16-bit counter
  13858.         LD      (HL),C          ; insert low byte
  13859.         INC     HL              ; address next
  13860.         LD      (HL),B          ; insert high byte
  13861.  
  13862.         POP     BC              ; pop the number of dimensions.
  13863.         LD      A,B             ; dimensions to A
  13864.         INC     HL              ; address next
  13865.         LD      (HL),A          ; and insert "No. of dims"
  13866.  
  13867.         LD      H,D             ; transfer DE space + 1 from make-room
  13868.         LD      L,E             ; to HL
  13869.         DEC     DE              ; set DE to next location down.
  13870.         LD      (HL),$00        ; presume numeric and insert a zero
  13871.         BIT     6,C             ; test bit 6 of C. numeric or string ?
  13872.         JR      Z,L2C7C         ; skip to DIM-CLEAR if numeric
  13873.  
  13874.         LD      (HL),$20        ; place a space character in HL
  13875.  
  13876. ;; DIM-CLEAR
  13877. L2C7C:  POP     BC              ; pop the data length
  13878.  
  13879.         LDDR                    ; LDDR sets to zeros or spaces
  13880.  
  13881. ; The number of dimensions is still in A.
  13882. ; A loop is now entered to insert the size of each dimension that was pushed
  13883. ; during the D-NO-LOOP working downwards from position before start of data.
  13884.  
  13885. ;; DIM-SIZES
  13886. L2C7F:  POP     BC              ; pop a dimension size                    ***
  13887.         LD      (HL),B          ; insert high byte at position
  13888.         DEC     HL              ; next location down
  13889.         LD      (HL),C          ; insert low byte
  13890.         DEC     HL              ; next location down
  13891.         DEC     A               ; decrement dimension counter
  13892.         JR      NZ,L2C7F        ; back to DIM-SIZES until all done.
  13893.  
  13894.         RET                     ; return.
  13895.  
  13896. ; -----------------------------
  13897. ; Check whether digit or letter
  13898. ; -----------------------------
  13899. ; This routine checks that the character in A is alphanumeric
  13900. ; returning with carry set if so.
  13901.  
  13902. ;; ALPHANUM
  13903. L2C88:  CALL    L2D1B           ; routine NUMERIC will reset carry if so.
  13904.         CCF                     ; Complement Carry Flag
  13905.         RET     C               ; Return if numeric else continue into
  13906.                                 ; next routine.
  13907.  
  13908. ; This routine checks that the character in A is alphabetic
  13909.  
  13910. ;; ALPHA
  13911. L2C8D:  CP      $41             ; less than 'A' ?
  13912.         CCF                     ; Complement Carry Flag
  13913.         RET     NC              ; return if so
  13914.  
  13915.         CP      $5B             ; less than 'Z'+1 ?
  13916.         RET     C               ; is within first range
  13917.  
  13918.         CP      $61             ; less than 'a' ?
  13919.         CCF                     ; Complement Carry Flag
  13920.         RET     NC              ; return if so.
  13921.  
  13922.         CP      $7B             ; less than 'z'+1 ?
  13923.         RET                     ; carry set if within a-z.
  13924.  
  13925. ; -------------------------
  13926. ; Decimal to floating point
  13927. ; -------------------------
  13928. ; This routine finds the floating point number represented by an expression
  13929. ; beginning with BIN, '.' or a digit.
  13930. ; Note that BIN need not have any '0's or '1's after it.
  13931. ; BIN is really just a notational symbol and not a function.
  13932.  
  13933. ;; DEC-TO-FP
  13934. L2C9B:  CP      $C4             ; 'BIN' token ?
  13935.         JR      NZ,L2CB8        ; to NOT-BIN if not
  13936.  
  13937.         LD      DE,$0000        ; initialize 16 bit buffer register.
  13938.  
  13939. ;; BIN-DIGIT
  13940. L2CA2:  RST     20H             ; NEXT-CHAR
  13941.         SUB     $31             ; '1'
  13942.         ADC     A,$00           ; will be zero if '1' or '0'
  13943.                                 ; carry will be set if was '0'
  13944.         JR      NZ,L2CB3        ; forward to BIN-END if result not zero
  13945.  
  13946.         EX      DE,HL           ; buffer to HL
  13947.         CCF                     ; Carry now set if originally '1'
  13948.         ADC     HL,HL           ; shift the carry into HL
  13949.         JP      C,L31AD         ; to REPORT-6 if overflow - too many digits
  13950.                                 ; after first '1'. There can be an unlimited
  13951.                                 ; number of leading zeros.
  13952.                                 ; 'Number too big' - raise an error
  13953.  
  13954.         EX      DE,HL           ; save the buffer
  13955.         JR      L2CA2           ; back to BIN-DIGIT for more digits
  13956.  
  13957. ; ---
  13958.  
  13959. ;; BIN-END
  13960. L2CB3:  LD      B,D             ; transfer 16 bit buffer
  13961.         LD      C,E             ; to BC register pair.
  13962.         JP      L2D2B           ; JUMP to STACK-BC to put on calculator stack
  13963.  
  13964. ; ---
  13965.  
  13966. ; continue here with .1,  42, 3.14, 5., 2.3 E -4
  13967.  
  13968. ;; NOT-BIN
  13969. L2CB8:  CP      $2E             ; '.' - leading decimal point ?
  13970.         JR      Z,L2CCB         ; skip to DECIMAL if so.
  13971.  
  13972.         CALL    L2D3B           ; routine INT-TO-FP to evaluate all digits
  13973.                                 ; This number 'x' is placed on stack.
  13974.         CP      $2E             ; '.' - mid decimal point ?
  13975.  
  13976.         JR      NZ,L2CEB        ; to E-FORMAT if not to consider that format
  13977.  
  13978.         RST     20H             ; NEXT-CHAR
  13979.         CALL    L2D1B           ; routine NUMERIC returns carry reset if 0-9
  13980.  
  13981.         JR      C,L2CEB         ; to E-FORMAT if not a digit e.g. '1.'
  13982.  
  13983.         JR      L2CD5           ; to DEC-STO-1 to add the decimal part to 'x'
  13984.  
  13985. ; ---
  13986.  
  13987. ; a leading decimal point has been found in a number.
  13988.  
  13989. ;; DECIMAL
  13990. L2CCB:  RST     20H             ; NEXT-CHAR
  13991.         CALL    L2D1B           ; routine NUMERIC will reset carry if digit
  13992.  
  13993. ;; DEC-RPT-C
  13994. L2CCF:  JP      C,L1C8A         ; to REPORT-C if just a '.'
  13995.                                 ; raise 'Nonsense in BASIC'
  13996.  
  13997. ; since there is no leading zero put one on the calculator stack.
  13998.  
  13999.         RST     28H             ;; FP-CALC
  14000.         DB    $A0             ;;stk-zero  ; 0.
  14001.         DB    $38             ;;end-calc
  14002.  
  14003. ; If rejoining from earlier there will be a value 'x' on stack.
  14004. ; If continuing from above the value zero.
  14005. ; Now store 1 in mem-0.
  14006. ; Note. At each pass of the digit loop this will be divided by ten.
  14007.  
  14008. ;; DEC-STO-1
  14009. L2CD5:  RST     28H             ;; FP-CALC
  14010.         DB    $A1             ;;stk-one   ;x or 0,1.
  14011.         DB    $C0             ;;st-mem-0  ;x or 0,1.
  14012.         DB    $02             ;;delete    ;x or 0.
  14013.         DB    $38             ;;end-calc
  14014.  
  14015.  
  14016. ;; NXT-DGT-1
  14017. L2CDA:  RST     18H             ; GET-CHAR
  14018.         CALL    L2D22           ; routine STK-DIGIT stacks single digit 'd'
  14019.         JR      C,L2CEB         ; exit to E-FORMAT when digits exhausted  >
  14020.  
  14021.  
  14022.         RST     28H             ;; FP-CALC   ;x or 0,d.           first pass.
  14023.         DB    $E0             ;;get-mem-0  ;x or 0,d,1.
  14024.         DB    $A4             ;;stk-ten    ;x or 0,d,1,10.
  14025.         DB    $05             ;;division   ;x or 0,d,1/10.
  14026.         DB    $C0             ;;st-mem-0   ;x or 0,d,1/10.
  14027.         DB    $04             ;;multiply   ;x or 0,d/10.
  14028.         DB    $0F             ;;addition   ;x or 0 + d/10.
  14029.         DB    $38             ;;end-calc   last value.
  14030.  
  14031.         RST     20H             ; NEXT-CHAR  moves to next character
  14032.         JR      L2CDA           ; back to NXT-DGT-1
  14033.  
  14034. ; ---
  14035.  
  14036. ; although only the first pass is shown it can be seen that at each pass
  14037. ; the new less significant digit is multiplied by an increasingly smaller
  14038. ; factor (1/100, 1/1000, 1/10000 ... ) before being added to the previous
  14039. ; last value to form a new last value.
  14040.  
  14041. ; Finally see if an exponent has been input.
  14042.  
  14043. ;; E-FORMAT
  14044. L2CEB:  CP      $45             ; is character 'E' ?
  14045.         JR      Z,L2CF2         ; to SIGN-FLAG if so
  14046.  
  14047.         CP      $65             ; 'e' is acceptable as well.
  14048.         RET     NZ              ; return as no exponent.
  14049.  
  14050. ;; SIGN-FLAG
  14051. L2CF2:  LD      B,$FF           ; initialize temporary sign byte to $FF
  14052.  
  14053.         RST     20H             ; NEXT-CHAR
  14054.         CP      $2B             ; is character '+' ?
  14055.         JR      Z,L2CFE         ; to SIGN-DONE
  14056.  
  14057.         CP      $2D             ; is character '-' ?
  14058.         JR      NZ,L2CFF        ; to ST-E-PART as no sign
  14059.  
  14060.         INC     B               ; set sign to zero
  14061.  
  14062. ; now consider digits of exponent.
  14063. ; Note. incidentally this is the only occasion in Spectrum BASIC when an
  14064. ; expression may not be used when a number is expected.
  14065.  
  14066. ;; SIGN-DONE
  14067. L2CFE:  RST     20H             ; NEXT-CHAR
  14068.  
  14069. ;; ST-E-PART
  14070. L2CFF:  CALL    L2D1B           ; routine NUMERIC
  14071.         JR      C,L2CCF         ; to DEC-RPT-C if not
  14072.                                 ; raise 'Nonsense in BASIC'.
  14073.  
  14074.         PUSH    BC              ; save sign (in B)
  14075.         CALL    L2D3B           ; routine INT-TO-FP places exponent on stack
  14076.         CALL    L2DD5           ; routine FP-TO-A  transfers it to A
  14077.         POP     BC              ; restore sign
  14078.         JP      C,L31AD         ; to REPORT-6 if overflow (over 255)
  14079.                                 ; raise 'Number too big'.
  14080.  
  14081.         AND     A               ; set flags
  14082.         JP      M,L31AD         ; to REPORT-6 if over '127'.
  14083.                                 ; raise 'Number too big'.
  14084.                                 ; 127 is still way too high and it is
  14085.                                 ; impossible to enter an exponent greater
  14086.                                 ; than 39 from the keyboard. The error gets
  14087.                                 ; raised later in E-TO-FP so two different
  14088.                                 ; error messages depending how high A is.
  14089.  
  14090.         INC     B               ; $FF to $00 or $00 to $01 - expendable now.
  14091.         JR      Z,L2D18         ; forward to E-FP-JUMP if exponent positive
  14092.  
  14093.         NEG                     ; Negate the exponent.
  14094.  
  14095. ;; E-FP-JUMP
  14096. L2D18:  JP      L2D4F           ; JUMP forward to E-TO-FP to assign to
  14097.                                 ; last value x on stack x * 10 to power A
  14098.                                 ; a relative jump would have done.
  14099.  
  14100. ; ---------------------
  14101. ; Check for valid digit
  14102. ; ---------------------
  14103. ; This routine checks that the ASCII character in A is numeric
  14104. ; returning with carry reset if so.
  14105.  
  14106. ;; NUMERIC
  14107. L2D1B:  CP      $30             ; '0'
  14108.         RET     C               ; return if less than zero character.
  14109.  
  14110.         CP      $3A             ; The upper test is '9'
  14111.         CCF                     ; Complement Carry Flag
  14112.         RET                     ; Return - carry clear if character '0' - '9'
  14113.  
  14114. ; -----------
  14115. ; Stack Digit
  14116. ; -----------
  14117. ; This subroutine is called from INT-TO-FP and DEC-TO-FP to stack a digit
  14118. ; on the calculator stack.
  14119.  
  14120. ;; STK-DIGIT
  14121. L2D22:  CALL    L2D1B           ; routine NUMERIC
  14122.         RET     C               ; return if not numeric character
  14123.  
  14124.         SUB     $30             ; convert from ASCII to digit
  14125.  
  14126. ; -----------------
  14127. ; Stack accumulator
  14128. ; -----------------
  14129. ;
  14130. ;
  14131.  
  14132. ;; STACK-A
  14133. L2D28:  LD      C,A             ; transfer to C
  14134.         LD      B,$00           ; and make B zero
  14135.  
  14136. ; ----------------------
  14137. ; Stack BC register pair
  14138. ; ----------------------
  14139. ;
  14140.  
  14141. ;; STACK-BC
  14142. L2D2B:  LD      IY,$5C3A        ; re-initialize ERR_NR
  14143.  
  14144.         XOR     A               ; clear to signal small integer
  14145.         LD      E,A             ; place in E for sign
  14146.         LD      D,C             ; LSB to D
  14147.         LD      C,B             ; MSB to C
  14148.         LD      B,A             ; last byte not used
  14149.         CALL    L2AB6           ; routine STK-STORE
  14150.  
  14151.         RST     28H             ;; FP-CALC
  14152.         DB    $38             ;;end-calc  make HL = STKEND-5
  14153.  
  14154.         AND     A               ; clear carry
  14155.         RET                     ; before returning
  14156.  
  14157. ; -------------------------
  14158. ; Integer to floating point
  14159. ; -------------------------
  14160. ; This routine places one or more digits found in a BASIC line
  14161. ; on the calculator stack multiplying the previous value by ten each time
  14162. ; before adding in the new digit to form a last value on calculator stack.
  14163.  
  14164. ;; INT-TO-FP
  14165. L2D3B:  PUSH    AF              ; save first character
  14166.  
  14167.         RST     28H             ;; FP-CALC
  14168.         DB    $A0             ;;stk-zero    ; v=0. initial value
  14169.         DB    $38             ;;end-calc
  14170.  
  14171.         POP     AF              ; fetch first character back.
  14172.  
  14173. ;; NXT-DGT-2
  14174. L2D40:  CALL    L2D22           ; routine STK-DIGIT puts 0-9 on stack
  14175.         RET     C               ; will return when character is not numeric >
  14176.  
  14177.         RST     28H             ;; FP-CALC    ; v, d.
  14178.         DB    $01             ;;exchange    ; d, v.
  14179.         DB    $A4             ;;stk-ten     ; d, v, 10.
  14180.         DB    $04             ;;multiply    ; d, v*10.
  14181.         DB    $0F             ;;addition    ; d + v*10 = newvalue
  14182.         DB    $38             ;;end-calc    ; v.
  14183.  
  14184.         CALL    L0074           ; routine CH-ADD+1 get next character
  14185.         JR      L2D40           ; back to NXT-DGT-2 to process as a digit
  14186.  
  14187.  
  14188. ;*********************************
  14189. ;** Part 9. ARITHMETIC ROUTINES **
  14190. ;*********************************
  14191.  
  14192. ; --------------------------
  14193. ; E-format to floating point
  14194. ; --------------------------
  14195. ; This subroutine is used by the PRINT-FP routine and the decimal to FP
  14196. ; routines to stack a number expressed in exponent format.
  14197. ; Note. Though not used by the ROM as such, it has also been set up as
  14198. ; a unary calculator literal but this will not work as the accumulator
  14199. ; is not available from within the calculator.
  14200.  
  14201. ; on entry there is a value x on the calculator stack and an exponent of ten
  14202. ; in A.    The required value is x + 10 ^ A
  14203.  
  14204. ;; e-to-fp
  14205. ;; E-TO-FP
  14206. L2D4F:  RLCA                    ; this will set the          x.
  14207.         RRCA                    ; carry if bit 7 is set
  14208.  
  14209.         JR      NC,L2D55        ; to E-SAVE  if positive.
  14210.  
  14211.         CPL                     ; make negative positive
  14212.         INC     A               ; without altering carry.
  14213.  
  14214. ;; E-SAVE
  14215. L2D55:  PUSH    AF              ; save positive exp and sign in carry
  14216.  
  14217.         LD      HL,$5C92        ; address MEM-0
  14218.  
  14219.         CALL    L350B           ; routine FP-0/1
  14220.                                 ; places an integer zero, if no carry,
  14221.                                 ; else a one in mem-0 as a sign flag
  14222.  
  14223.         RST     28H             ;; FP-CALC
  14224.         DB    $A4             ;;stk-ten                    x, 10.
  14225.         DB    $38             ;;end-calc
  14226.  
  14227.         POP     AF              ; pop the exponent.
  14228.  
  14229. ; now enter a loop
  14230.  
  14231. ;; E-LOOP
  14232. L2D60:  SRL     A               ; 0>76543210>C
  14233.  
  14234.         JR      NC,L2D71        ; forward to E-TST-END if no bit
  14235.  
  14236.         PUSH    AF              ; save shifted exponent.
  14237.  
  14238.         RST     28H             ;; FP-CALC
  14239.         DB    $C1             ;;st-mem-1                   x, 10.
  14240.         DB    $E0             ;;get-mem-0                  x, 10, (0/1).
  14241.         DB    $00             ;;jump-true
  14242.  
  14243.         DB    $04             ;;to L2D6D, E-DIVSN
  14244.  
  14245.         DB    $04             ;;multiply                   x*10.
  14246.         DB    $33             ;;jump
  14247.  
  14248.         DB    $02             ;;to L2D6E, E-FETCH
  14249.  
  14250. ;; E-DIVSN
  14251. L2D6D:  DB    $05             ;;division                   x/10.
  14252.  
  14253. ;; E-FETCH
  14254. L2D6E:  DB    $E1             ;;get-mem-1                  x/10 or x*10, 10.
  14255.         DB    $38             ;;end-calc                   new x, 10.
  14256.  
  14257.         POP     AF              ; restore shifted exponent
  14258.  
  14259. ; the loop branched to here with no carry
  14260.  
  14261. ;; E-TST-END
  14262. L2D71:  JR      Z,L2D7B         ; forward to E-END  if A emptied of bits
  14263.  
  14264.         PUSH    AF              ; re-save shifted exponent
  14265.  
  14266.         RST     28H             ;; FP-CALC
  14267.         DB    $31             ;;duplicate                  new x, 10, 10.
  14268.         DB    $04             ;;multiply                   new x, 100.
  14269.         DB    $38             ;;end-calc
  14270.  
  14271.         POP     AF              ; restore shifted exponent
  14272.         JR      L2D60           ; back to E-LOOP  until all bits done.
  14273.  
  14274. ; ---
  14275.  
  14276. ; although only the first pass is shown it can be seen that for each set bit
  14277. ; representing a power of two, x is multiplied or divided by the
  14278. ; corresponding power of ten.
  14279.  
  14280. ;; E-END
  14281. L2D7B:  RST     28H             ;; FP-CALC                   final x, factor.
  14282.         DB    $02             ;;delete                     final x.
  14283.         DB    $38             ;;end-calc                   x.
  14284.  
  14285.         RET                     ; return
  14286.  
  14287.  
  14288.  
  14289.  
  14290. ; -------------
  14291. ; Fetch integer
  14292. ; -------------
  14293. ; This routine is called by the mathematical routines - FP-TO-BC, PRINT-FP,
  14294. ; mult, re-stack and negate to fetch an integer from address HL.
  14295. ; HL points to the stack or a location in MEM and no deletion occurs.
  14296. ; If the number is negative then a similar process to that used in INT-STORE
  14297. ; is used to restore the twos complement number to normal in DE and a sign
  14298. ; in C.
  14299.  
  14300. ;; INT-FETCH
  14301. L2D7F:  INC     HL              ; skip zero indicator.
  14302.         LD      C,(HL)          ; fetch sign to C
  14303.         INC     HL              ; address low byte
  14304.         LD      A,(HL)          ; fetch to A
  14305.         XOR     C               ; two's complement
  14306.         SUB     C               ;
  14307.         LD      E,A             ; place in E
  14308.         INC     HL              ; address high byte
  14309.         LD      A,(HL)          ; fetch to A
  14310.         ADC     A,C             ; two's complement
  14311.         XOR     C               ;
  14312.         LD      D,A             ; place in D
  14313.         RET                     ; return
  14314.  
  14315. ; ------------------------
  14316. ; Store a positive integer
  14317. ; ------------------------
  14318. ; This entry point is not used in this ROM but would
  14319. ; store any integer as positive.
  14320.  
  14321. ;; p-int-sto
  14322. L2D8C:  LD      C,$00           ; make sign byte positive and continue
  14323.  
  14324. ; -------------
  14325. ; Store integer
  14326. ; -------------
  14327. ; this routine stores an integer in DE at address HL.
  14328. ; It is called from mult, truncate, negate and sgn.
  14329. ; The sign byte $00 +ve or $FF -ve is in C.
  14330. ; If negative, the number is stored in 2's complement form so that it is
  14331. ; ready to be added.
  14332.  
  14333. ;; INT-STORE
  14334. L2D8E:  PUSH    HL              ; preserve HL
  14335.  
  14336.         LD      (HL),$00        ; first byte zero shows integer not exponent
  14337.         INC     HL              ;
  14338.         LD      (HL),C          ; then store the sign byte
  14339.         INC     HL              ;
  14340.                                 ; e.g.             +1             -1
  14341.         LD      A,E             ; fetch low byte   00000001       00000001
  14342.         XOR     C               ; xor sign         00000000   or  11111111
  14343.                                 ; gives            00000001   or  11111110
  14344.         SUB     C               ; sub sign         00000000   or  11111111
  14345.                                 ; gives            00000001>0 or  11111111>C
  14346.         LD      (HL),A          ; store 2's complement.
  14347.         INC     HL              ;
  14348.         LD      A,D             ; high byte        00000000       00000000
  14349.         ADC     A,C             ; sign             00000000<0     11111111<C
  14350.                                 ; gives            00000000   or  00000000
  14351.         XOR     C               ; xor sign         00000000       11111111
  14352.         LD      (HL),A          ; store 2's complement.
  14353.         INC     HL              ;
  14354.         LD      (HL),$00        ; last byte always zero for integers.
  14355.                                 ; is not used and need not be looked at when
  14356.                                 ; testing for zero but comes into play should
  14357.                                 ; an integer be converted to fp.
  14358.         POP     HL              ; restore HL
  14359.         RET                     ; return.
  14360.  
  14361.  
  14362. ; -----------------------------
  14363. ; Floating point to BC register
  14364. ; -----------------------------
  14365. ; This routine gets a floating point number e.g. 127.4 from the calculator
  14366. ; stack to the BC register.
  14367.  
  14368. ;; FP-TO-BC
  14369. L2DA2:  RST     28H             ;; FP-CALC            set HL to
  14370.         DB    $38             ;;end-calc            point to last value.
  14371.  
  14372.         LD      A,(HL)          ; get first of 5 bytes
  14373.         AND     A               ; and test
  14374.         JR      Z,L2DAD         ; forward to FP-DELETE if an integer
  14375.  
  14376. ; The value is first rounded up and then converted to integer.
  14377.  
  14378.         RST     28H             ;; FP-CALC           x.
  14379.         DB    $A2             ;;stk-half           x. 1/2.
  14380.         DB    $0F             ;;addition           x + 1/2.
  14381.         DB    $27             ;;int                int(x + .5)
  14382.         DB    $38             ;;end-calc
  14383.  
  14384. ; now delete but leave HL pointing at integer
  14385.  
  14386. ;; FP-DELETE
  14387. L2DAD:  RST     28H             ;; FP-CALC
  14388.         DB    $02             ;;delete
  14389.         DB    $38             ;;end-calc
  14390.  
  14391.         PUSH    HL              ; save pointer.
  14392.         PUSH    DE              ; and STKEND.
  14393.         EX      DE,HL           ; make HL point to exponent/zero indicator
  14394.         LD      B,(HL)          ; indicator to B
  14395.         CALL    L2D7F           ; routine INT-FETCH
  14396.                                 ; gets int in DE sign byte to C
  14397.                                 ; but meaningless values if a large integer
  14398.  
  14399.         XOR     A               ; clear A
  14400.         SUB     B               ; subtract indicator byte setting carry
  14401.                                 ; if not a small integer.
  14402.  
  14403.         BIT     7,C             ; test a bit of the sign byte setting zero
  14404.                                 ; if positive.
  14405.  
  14406.         LD      B,D             ; transfer int
  14407.         LD      C,E             ; to BC
  14408.         LD      A,E             ; low byte to A as a useful return value.
  14409.  
  14410.         POP     DE              ; pop STKEND
  14411.         POP     HL              ; and pointer to last value
  14412.         RET                     ; return
  14413.                                 ; if carry is set then the number was too big.
  14414.  
  14415. ; ------------
  14416. ; LOG(2^A)
  14417. ; ------------
  14418. ; This routine is used when printing floating point numbers to calculate
  14419. ; the number of digits before the decimal point.
  14420.  
  14421. ; first convert a one-byte signed integer to its five byte form.
  14422.  
  14423. ;; LOG(2^A)
  14424. L2DC1:  LD      D,A             ; store a copy of A in D.
  14425.         RLA                     ; test sign bit of A.
  14426.         SBC     A,A             ; now $FF if negative or $00
  14427.         LD      E,A             ; sign byte to E.
  14428.         LD      C,A             ; and to C
  14429.         XOR     A               ; clear A
  14430.         LD      B,A             ; and B.
  14431.         CALL    L2AB6           ; routine STK-STORE stacks number AEDCB
  14432.  
  14433. ;  so 00 00 XX 00 00 (positive) or 00 FF XX FF 00 (negative).
  14434. ;  i.e. integer indicator, sign byte, low, high, unused.
  14435.  
  14436. ; now multiply exponent by log to the base 10 of two.
  14437.  
  14438.         RST      28H            ;; FP-CALC
  14439.  
  14440.         DB    $34             ;;stk-data                      .30103 (log 2)
  14441.         DB    $EF             ;;Exponent: $7F, Bytes: 4
  14442.         DB    $1A,$20,$9A,$85 ;;
  14443.         DB    $04             ;;multiply
  14444.  
  14445.         DB    $27             ;;int
  14446.  
  14447.         DB    $38             ;;end-calc
  14448.  
  14449. ; -------------------
  14450. ; Floating point to A
  14451. ; -------------------
  14452. ; this routine collects a floating point number from the stack into the
  14453. ; accumulator returning carry set if not in range 0 - 255.
  14454. ; Not all the calling routines raise an error with overflow so no attempt
  14455. ; is made to produce an error report here.
  14456.  
  14457. ;; FP-TO-A
  14458. L2DD5:  CALL    L2DA2           ; routine FP-TO-BC returns with C in A also.
  14459.         RET     C               ; return with carry set if > 65535, overflow
  14460.  
  14461.         PUSH    AF              ; save the value and flags
  14462.         DEC     B               ; and test that
  14463.         INC     B               ; the high byte is zero.
  14464.         JR      Z,L2DE1         ; forward  FP-A-END if zero
  14465.  
  14466. ; else there has been 8-bit overflow
  14467.  
  14468.         POP     AF              ; retrieve the value
  14469.         SCF                     ; set carry flag to show overflow
  14470.         RET                     ; and return.
  14471.  
  14472. ; ---
  14473.  
  14474. ;; FP-A-END
  14475. L2DE1:  POP     AF              ; restore value and success flag and
  14476.         RET                     ; return.
  14477.  
  14478.  
  14479. ; -----------------------------
  14480. ; Print a floating point number
  14481. ; -----------------------------
  14482. ; Not a trivial task.
  14483. ; Begin by considering whether to print a leading sign for negative numbers.
  14484.  
  14485. ;; PRINT-FP
  14486. L2DE3:  RST     28H             ;; FP-CALC
  14487.         DB    $31             ;;duplicate
  14488.         DB    $36             ;;less-0
  14489.         DB    $00             ;;jump-true
  14490.  
  14491.         DB    $0B             ;;to L2DF2, PF-NEGTVE
  14492.  
  14493.         DB    $31             ;;duplicate
  14494.         DB    $37             ;;greater-0
  14495.         DB    $00             ;;jump-true
  14496.  
  14497.         DB    $0D             ;;to L2DF8, PF-POSTVE
  14498.  
  14499. ; must be zero itself
  14500.  
  14501.         DB    $02             ;;delete
  14502.         DB    $38             ;;end-calc
  14503.  
  14504.         LD      A,$30           ; prepare the character '0'
  14505.  
  14506.         RST     10H             ; PRINT-A
  14507.         RET                     ; return.                 ->
  14508. ; ---
  14509.  
  14510. ;; PF-NEGTVE
  14511. L2DF2:  DB    $2A             ;;abs
  14512.         DB    $38             ;;end-calc
  14513.  
  14514.         LD      A,$2D           ; the character '-'
  14515.  
  14516.         RST     10H             ; PRINT-A
  14517.  
  14518. ; and continue to print the now positive number.
  14519.  
  14520.         RST     28H             ;; FP-CALC
  14521.  
  14522. ;; PF-POSTVE
  14523. L2DF8:  DB    $A0             ;;stk-zero     x,0.     begin by
  14524.         DB    $C3             ;;st-mem-3     x,0.     clearing a temporary
  14525.         DB    $C4             ;;st-mem-4     x,0.     output buffer to
  14526.         DB    $C5             ;;st-mem-5     x,0.     fifteen zeros.
  14527.         DB    $02             ;;delete       x.
  14528.         DB    $38             ;;end-calc     x.
  14529.  
  14530.         EXX                     ; in case called from 'str$' then save the
  14531.         PUSH    HL              ; pointer to whatever comes after
  14532.         EXX                     ; str$ as H'L' will be used.
  14533.  
  14534. ; now enter a loop?
  14535.  
  14536. ;; PF-LOOP
  14537. L2E01:  RST     28H             ;; FP-CALC
  14538.         DB    $31             ;;duplicate    x,x.
  14539.         DB    $27             ;;int          x,int x.
  14540.         DB    $C2             ;;st-mem-2     x,int x.
  14541.         DB    $03             ;;subtract     x-int x.     fractional part.
  14542.         DB    $E2             ;;get-mem-2    x-int x, int x.
  14543.         DB    $01             ;;exchange     int x, x-int x.
  14544.         DB    $C2             ;;st-mem-2     int x, x-int x.
  14545.         DB    $02             ;;delete       int x.
  14546.         DB    $38             ;;end-calc     int x.
  14547.                                 ;
  14548.                                 ; mem-2 holds the fractional part.
  14549.  
  14550. ; HL points to last value int x
  14551.  
  14552.         LD      A,(HL)          ; fetch exponent of int x.
  14553.         AND     A               ; test
  14554.         JR      NZ,L2E56        ; forward to PF-LARGE if a large integer
  14555.                                 ; > 65535
  14556.  
  14557. ; continue with small positive integer components in range 0 - 65535
  14558. ; if original number was say .999 then this integer component is zero.
  14559.  
  14560.         CALL    L2D7F           ; routine INT-FETCH gets x in DE
  14561.                                 ; (but x is not deleted)
  14562.  
  14563.         LD      B,$10           ; set B, bit counter, to 16d
  14564.  
  14565.         LD      A,D             ; test if
  14566.         AND     A               ; high byte is zero
  14567.         JR      NZ,L2E1E        ; forward to PF-SAVE if 16-bit integer.
  14568.  
  14569. ; and continue with integer in range 0 - 255.
  14570.  
  14571.         OR      E               ; test the low byte for zero
  14572.                                 ; i.e. originally just point something or other.
  14573.         JR      Z,L2E24         ; forward if so to PF-SMALL
  14574.  
  14575. ;
  14576.  
  14577.         LD      D,E             ; transfer E to D
  14578.         LD      B,$08           ; and reduce the bit counter to 8.
  14579.  
  14580. ;; PF-SAVE
  14581. L2E1E:  PUSH    DE              ; save the part before decimal point.
  14582.         EXX                     ;
  14583.         POP     DE              ; and pop in into D'E'
  14584.         EXX                     ;
  14585.         JR      L2E7B           ; forward to PF-BITS
  14586.  
  14587. ; ---------------------
  14588.  
  14589. ; the branch was here when 'int x' was found to be zero as in say 0.5.
  14590. ; The zero has been fetched from the calculator stack but not deleted and
  14591. ; this should occur now. This omission leaves the stack unbalanced and while
  14592. ; that causes no problems with a simple PRINT statement, it will if str$ is
  14593. ; being used in an expression e.g. "2" + STR$ 0.5 gives the result "0.5"
  14594. ; instead of the expected result "20.5".
  14595. ; credit Tony Stratton, 1982.
  14596. ; A DB 02 delete is required immediately on using the calculator.
  14597.  
  14598. ;; PF-SMALL
  14599. L2E24:  RST     28H             ;; FP-CALC       int x = 0.
  14600. L2E25:  DB    $E2             ;;get-mem-2      int x = 0, x-int x.
  14601.         DB    $38             ;;end-calc
  14602.  
  14603.         LD      A,(HL)          ; fetch exponent of positive fractional number
  14604.         SUB     $7E             ; subtract
  14605.  
  14606.         CALL    L2DC1           ; routine LOG(2^A) calculates leading digits.
  14607.  
  14608.         LD      D,A             ; transfer count to D
  14609.         LD      A,($5CAC)       ; fetch total MEM-5-1
  14610.         SUB     D               ;
  14611.         LD      ($5CAC),A       ; MEM-5-1
  14612.         LD      A,D             ;
  14613.         CALL    L2D4F           ; routine E-TO-FP
  14614.  
  14615.         RST     28H             ;; FP-CALC
  14616.         DB    $31             ;;duplicate
  14617.         DB    $27             ;;int
  14618.         DB    $C1             ;;st-mem-1
  14619.         DB    $03             ;;subtract
  14620.         DB    $E1             ;;get-mem-1
  14621.         DB    $38             ;;end-calc
  14622.  
  14623.         CALL    L2DD5           ; routine FP-TO-A
  14624.  
  14625.         PUSH    HL              ; save HL
  14626.         LD      ($5CA1),A       ; MEM-3-1
  14627.         DEC     A               ;
  14628.         RLA                     ;
  14629.         SBC     A,A             ;
  14630.         INC     A               ;
  14631.  
  14632.         LD      HL,$5CAB        ; address MEM-5-1 leading digit counter
  14633.         LD      (HL),A          ; store counter
  14634.         INC     HL              ; address MEM-5-2 total digits
  14635.         ADD     A,(HL)          ; add counter to contents
  14636.         LD      (HL),A          ; and store updated value
  14637.         POP     HL              ; restore HL
  14638.  
  14639.         JP      L2ECF           ; JUMP forward to PF-FRACTN
  14640.  
  14641. ; ---
  14642.  
  14643. ; Note. while it would be pedantic to comment on every occasion a JP
  14644. ; instruction could be replaced with a JR instruction, this applies to the
  14645. ; above, which is useful if you wish to correct the unbalanced stack error
  14646. ; by inserting a 'DB 02 delete' at L2E25, and maintain main addresses.
  14647.  
  14648. ; the branch was here with a large positive integer > 65535 e.g. 123456789
  14649. ; the accumulator holds the exponent.
  14650.  
  14651. ;; PF-LARGE
  14652. L2E56:  SUB     $80             ; make exponent positive
  14653.         CP      $1C             ; compare to 28
  14654.         JR      C,L2E6F         ; to PF-MEDIUM if integer <= 2^27
  14655.  
  14656.         CALL    L2DC1           ; routine LOG(2^A)
  14657.         SUB     $07             ;
  14658.         LD      B,A             ;
  14659.         LD      HL,$5CAC        ; address MEM-5-1 the leading digits counter.
  14660.         ADD     A,(HL)          ; add A to contents
  14661.         LD      (HL),A          ; store updated value.
  14662.         LD      A,B             ;
  14663.         NEG                     ; negate
  14664.         CALL    L2D4F           ; routine E-TO-FP
  14665.         JR      L2E01           ; back to PF-LOOP
  14666.  
  14667. ; ----------------------------
  14668.  
  14669. ;; PF-MEDIUM
  14670. L2E6F:  EX      DE,HL           ;
  14671.         CALL    L2FBA           ; routine FETCH-TWO
  14672.         EXX                     ;
  14673.         SET     7,D             ;
  14674.         LD      A,L             ;
  14675.         EXX                     ;
  14676.         SUB     $80             ;
  14677.         LD      B,A             ;
  14678.  
  14679. ; the branch was here to handle bits in DE with 8 or 16 in B  if small int
  14680. ; and integer in D'E', 6 nibbles will accommodate 065535 but routine does
  14681. ; 32-bit numbers as well from above
  14682.  
  14683. ;; PF-BITS
  14684. L2E7B:  SLA     E               ;  C<xxxxxxxx<0
  14685.         RL      D               ;  C<xxxxxxxx<C
  14686.         EXX                     ;
  14687.         RL      E               ;  C<xxxxxxxx<C
  14688.         RL      D               ;  C<xxxxxxxx<C
  14689.         EXX                     ;
  14690.  
  14691.         LD      HL,$5CAA        ; set HL to mem-4-5th last byte of buffer
  14692.         LD      C,$05           ; set byte count to 5 -  10 nibbles
  14693.  
  14694. ;; PF-BYTES
  14695. L2E8A:  LD      A,(HL)          ; fetch 0 or prev value
  14696.         ADC     A,A             ; shift left add in carry    C<xxxxxxxx<C
  14697.  
  14698.         DAA                     ; Decimal Adjust Accumulator.
  14699.                                 ; if greater than 9 then the left hand
  14700.                                 ; nibble is incremented. If greater than
  14701.                                 ; 99 then adjusted and carry set.
  14702.                                 ; so if we'd built up 7 and a carry came in
  14703.                                 ;      0000 0111 < C
  14704.                                 ;      0000 1111
  14705.                                 ; daa     1 0101  which is 15 in BCD
  14706.  
  14707.         LD      (HL),A          ; put back
  14708.         DEC     HL              ; work down thru mem 4
  14709.         DEC     C               ; decrease the 5 counter.
  14710.         JR      NZ,L2E8A        ; back to PF-BYTES until the ten nibbles rolled
  14711.  
  14712.         DJNZ    L2E7B           ; back to PF-BITS until 8 or 16 (or 32) done
  14713.  
  14714. ; at most 9 digits for 32-bit number will have been loaded with digits
  14715. ; each of the 9 nibbles in mem 4 is placed into ten bytes in mem-3 and mem 4
  14716. ; unless the nibble is zero as the buffer is already zero.
  14717. ; ( or in the case of mem-5 will become zero as a result of RLD instruction )
  14718.  
  14719.         XOR     A               ; clear to accept
  14720.         LD      HL,$5CA6        ; address MEM-4-0 byte destination.
  14721.         LD      DE,$5CA1        ; address MEM-3-0 nibble source.
  14722.         LD      B,$09           ; the count is 9 (not ten) as the first
  14723.                                 ; nibble is known to be blank.
  14724.  
  14725.         RLD                     ; shift RH nibble to left in (HL)
  14726.                                 ;    A           (HL)
  14727.                                 ; 0000 0000 < 0000 3210
  14728.                                 ; 0000 0000   3210 0000
  14729.                                 ; A picks up the blank nibble
  14730.  
  14731.  
  14732.         LD      C,$FF           ; set a flag to indicate when a significant
  14733.                                 ; digit has been encountered.
  14734.  
  14735. ;; PF-DIGITS
  14736. L2EA1:  RLD                     ; pick up leftmost nibble from (HL)
  14737.                                 ;    A           (HL)
  14738.                                 ; 0000 0000 < 7654 3210
  14739.                                 ; 0000 7654   3210 0000
  14740.  
  14741.  
  14742.         JR      NZ,L2EA9        ; to PF-INSERT if non-zero value picked up.
  14743.  
  14744.         DEC     C               ; test
  14745.         INC     C               ; flag
  14746.         JR      NZ,L2EB3        ; skip forward to PF-TEST-2 if flag still $FF
  14747.                                 ; indicating this is a leading zero.
  14748.  
  14749. ; but if the zero is a significant digit e.g. 10 then include in digit totals.
  14750. ; the path for non-zero digits rejoins here.
  14751.  
  14752. ;; PF-INSERT
  14753. L2EA9:  LD      (DE),A          ; insert digit at destination
  14754.         INC     DE              ; increase the destination pointer
  14755.         INC     (IY+$71)        ; increment MEM-5-1st  digit counter
  14756.         INC     (IY+$72)        ; increment MEM-5-2nd  leading digit counter
  14757.         LD      C,$00           ; set flag to zero indicating that any
  14758.                                 ; subsequent zeros are significant and not
  14759.                                 ; leading.
  14760.  
  14761. ;; PF-TEST-2
  14762. L2EB3:  BIT     0,B             ; test if the nibble count is even
  14763.         JR      Z,L2EB8         ; skip to PF-ALL-9 if so to deal with the
  14764.                                 ; other nibble in the same byte
  14765.  
  14766.         INC     HL              ; point to next source byte if not
  14767.  
  14768. ;; PF-ALL-9
  14769. L2EB8:  DJNZ    L2EA1           ; decrement the nibble count, back to PF-DIGITS
  14770.                                 ; if all nine not done.
  14771.  
  14772. ; For 8-bit integers there will be at most 3 digits.
  14773. ; For 16-bit integers there will be at most 5 digits.
  14774. ; but for larger integers there could be nine leading digits.
  14775. ; if nine digits complete then the last one is rounded up as the number will
  14776. ; be printed using E-format notation
  14777.  
  14778.         LD      A,($5CAB)       ; fetch digit count from MEM-5-1st
  14779.         SUB     $09             ; subtract 9 - max possible
  14780.         JR      C,L2ECB         ; forward if less to PF-MORE
  14781.  
  14782.         DEC     (IY+$71)        ; decrement digit counter MEM-5-1st to 8
  14783.         LD      A,$04           ; load A with the value 4.
  14784.         CP      (IY+$6F)        ; compare with MEM-4-4th - the ninth digit
  14785.         JR      L2F0C           ; forward to PF-ROUND
  14786.                                 ; to consider rounding.
  14787.  
  14788. ; ---------------------------------------
  14789.  
  14790. ; now delete int x from calculator stack and fetch fractional part.
  14791.  
  14792. ;; PF-MORE
  14793. L2ECB:  RST     28H             ;; FP-CALC        int x.
  14794.         DB    $02             ;;delete          .
  14795.         DB    $E2             ;;get-mem-2       x - int x = f.
  14796.         DB    $38             ;;end-calca       f.
  14797.  
  14798. ;; PF-FRACTN
  14799. L2ECF:  EX      DE,HL           ;
  14800.         CALL    L2FBA           ; routine FETCH-TWO
  14801.         EXX                     ;
  14802.         LD      A,$80           ;
  14803.         SUB     L               ;
  14804.         LD      L,$00           ;
  14805.         SET     7,D             ;
  14806.         EXX                     ;
  14807.         CALL    L2FDD           ; routine SHIFT-FP
  14808.  
  14809. ;; PF-FRN-LP
  14810. L2EDF:  LD      A,(IY+$71)      ; MEM-5-1st
  14811.         CP      $08             ;
  14812.         JR      C,L2EEC         ; to PF-FR-DGT
  14813.  
  14814.         EXX                     ;
  14815.         RL      D               ;
  14816.         EXX                     ;
  14817.         JR      L2F0C           ; to PF-ROUND
  14818.  
  14819. ; ---
  14820.  
  14821. ;; PF-FR-DGT
  14822. L2EEC:  LD      BC,$0200        ;
  14823.  
  14824. ;; PF-FR-EXX
  14825. L2EEF:  LD      A,E             ;
  14826.         CALL    L2F8B           ; routine CA-10*A+C
  14827.         LD      E,A             ;
  14828.         LD      A,D             ;
  14829.         CALL    L2F8B           ; routine CA-10*A+C
  14830.         LD      D,A             ;
  14831.         PUSH    BC              ;
  14832.         EXX                     ;
  14833.         POP     BC              ;
  14834.         DJNZ    L2EEF           ; to PF-FR-EXX
  14835.  
  14836.         LD      HL,$5CA1        ; MEM-3
  14837.         LD      A,C             ;
  14838.         LD      C,(IY+$71)      ; MEM-5-1st
  14839.         ADD     HL,BC           ;
  14840.         LD      (HL),A          ;
  14841.         INC     (IY+$71)        ; MEM-5-1st
  14842.         JR      L2EDF           ; to PF-FRN-LP
  14843.  
  14844. ; ----------------
  14845.  
  14846. ; 1) with 9 digits but 8 in mem-5-1 and A holding 4, carry set if rounding up.
  14847. ; e.g.
  14848. ;      999999999 is printed as 1E+9
  14849. ;      100000001 is printed as 1E+8
  14850. ;      100000009 is printed as 1.0000001E+8
  14851.  
  14852. ;; PF-ROUND
  14853. L2F0C:  PUSH    AF              ; save A and flags
  14854.         LD      HL,$5CA1        ; address MEM-3 start of digits
  14855.         LD      C,(IY+$71)      ; MEM-5-1st No. of digits to C
  14856.         LD      B,$00           ; prepare to add
  14857.         ADD     HL,BC           ; address last digit + 1
  14858.         LD      B,C             ; No. of digits to B counter
  14859.         POP     AF              ; restore A and carry flag from comparison.
  14860.  
  14861. ;; PF-RND-LP
  14862. L2F18:  DEC     HL              ; address digit at rounding position.
  14863.         LD      A,(HL)          ; fetch it
  14864.         ADC     A,$00           ; add carry from the comparison
  14865.         LD      (HL),A          ; put back result even if $0A.
  14866.         AND     A               ; test A
  14867.         JR      Z,L2F25         ; skip to PF-R-BACK if ZERO?
  14868.  
  14869.         CP      $0A             ; compare to 'ten' - overflow
  14870.         CCF                     ; complement carry flag so that set if ten.
  14871.         JR      NC,L2F2D        ; forward to PF-COUNT with 1 - 9.
  14872.  
  14873. ;; PF-R-BACK
  14874. L2F25:  DJNZ    L2F18           ; loop back to PF-RND-LP
  14875.  
  14876. ; if B counts down to zero then we've rounded right back as in 999999995.
  14877. ; and the first 8 locations all hold $0A.
  14878.  
  14879.  
  14880.         LD      (HL),$01        ; load first location with digit 1.
  14881.         INC     B               ; make B hold 1 also.
  14882.                                 ; could save an instruction byte here.
  14883.         INC     (IY+$72)        ; make MEM-5-2nd hold 1.
  14884.                                 ; and proceed to initialize total digits to 1.
  14885.  
  14886. ;; PF-COUNT
  14887. L2F2D:  LD      (IY+$71),B      ; MEM-5-1st
  14888.  
  14889. ; now balance the calculator stack by deleting  it
  14890.  
  14891.         RST     28H             ;; FP-CALC
  14892.         DB    $02             ;;delete
  14893.         DB    $38             ;;end-calc
  14894.  
  14895. ; note if used from str$ then other values may be on the calculator stack.
  14896. ; we can also restore the next literal pointer from its position on the
  14897. ; machine stack.
  14898.  
  14899.         EXX                     ;
  14900.         POP     HL              ; restore next literal pointer.
  14901.         EXX                     ;
  14902.  
  14903.         LD      BC,($5CAB)      ; set C to MEM-5-1st digit counter.
  14904.                                 ; set B to MEM-5-2nd leading digit counter.
  14905.         LD      HL,$5CA1        ; set HL to start of digits at MEM-3-1
  14906.         LD      A,B             ;
  14907.         CP      $09             ;
  14908.         JR      C,L2F46         ; to PF-NOT-E
  14909.  
  14910.         CP      $FC             ;
  14911.         JR      C,L2F6C         ; to PF-E-FRMT
  14912.  
  14913. ;; PF-NOT-E
  14914. L2F46:  AND     A               ; test for zero leading digits as in .123
  14915.  
  14916.         CALL    Z,L15EF         ; routine OUT-CODE prints a zero e.g. 0.123
  14917.  
  14918. ;; PF-E-SBRN
  14919. L2F4A:  XOR     A               ;
  14920.         SUB     B               ;
  14921.         JP      M,L2F52         ; skip forward to PF-OUT-LP if originally +ve
  14922.  
  14923.         LD      B,A             ; else negative count now +ve
  14924.         JR      L2F5E           ; forward to PF-DC-OUT       ->
  14925.  
  14926. ; ---
  14927.  
  14928. ;; PF-OUT-LP
  14929. L2F52:  LD      A,C             ; fetch total digit count
  14930.         AND     A               ; test for zero
  14931.         JR      Z,L2F59         ; forward to PF-OUT-DT if so
  14932.  
  14933.         LD      A,(HL)          ; fetch digit
  14934.         INC     HL              ; address next digit
  14935.         DEC     C               ; decrease total digit counter
  14936.  
  14937. ;; PF-OUT-DT
  14938. L2F59:  CALL    L15EF           ; routine OUT-CODE outputs it.
  14939.         DJNZ    L2F52           ; loop back to PF-OUT-LP until B leading
  14940.                                 ; digits output.
  14941.  
  14942. ;; PF-DC-OUT
  14943. L2F5E:  LD      A,C             ; fetch total digits and
  14944.         AND     A               ; test if also zero
  14945.         RET     Z               ; return if so              -->
  14946.  
  14947. ;
  14948.  
  14949.         INC     B               ; increment B
  14950.         LD      A,$2E           ; prepare the character '.'
  14951.  
  14952. ;; PF-DEC-0$
  14953. L2F64:  RST     10H             ; PRINT-A outputs the character '.' or '0'
  14954.  
  14955.         LD      A,$30           ; prepare the character '0'
  14956.                                 ; (for cases like .000012345678)
  14957.         DJNZ    L2F64           ; loop back to PF-DEC-0$ for B times.
  14958.  
  14959.         LD      B,C             ; load B with now trailing digit counter.
  14960.         JR      L2F52           ; back to PF-OUT-LP
  14961.  
  14962. ; ---------------------------------
  14963.  
  14964. ; the branch was here for E-format printing e.g 123456789 => 1.2345679e+8
  14965.  
  14966. ;; PF-E-FRMT
  14967. L2F6C:  LD      D,B             ; counter to D
  14968.         DEC     D               ; decrement
  14969.         LD      B,$01           ; load B with 1.
  14970.  
  14971.         CALL    L2F4A           ; routine PF-E-SBRN above
  14972.  
  14973.         LD      A,$45           ; prepare character 'e'
  14974.         RST     10H             ; PRINT-A
  14975.  
  14976.         LD      C,D             ; exponent to C
  14977.         LD      A,C             ; and to A
  14978.         AND     A               ; test exponent
  14979.         JP      P,L2F83         ; to PF-E-POS if positive
  14980.  
  14981.         NEG                     ; negate
  14982.         LD      C,A             ; positive exponent to C
  14983.         LD      A,$2D           ; prepare character '-'
  14984.         JR      L2F85           ; skip to PF-E-SIGN
  14985.  
  14986. ; ---
  14987.  
  14988. ;; PF-E-POS
  14989. L2F83:  LD      A,$2B           ; prepare character '+'
  14990.  
  14991. ;; PF-E-SIGN
  14992. L2F85:  RST     10H             ; PRINT-A outputs the sign
  14993.  
  14994.         LD      B,$00           ; make the high byte zero.
  14995.         JP      L1A1B           ; exit via OUT-NUM-1 to print exponent in BC
  14996.  
  14997. ; ------------------------------
  14998. ; Handle printing floating point
  14999. ; ------------------------------
  15000. ; This subroutine is called twice from above when printing floating-point
  15001. ; numbers. It returns 10*A +C in registers C and A
  15002.  
  15003. ;; CA-10*A+C
  15004. L2F8B:  PUSH    DE              ; preserve DE.
  15005.         LD      L,A             ; transfer A to L
  15006.         LD      H,$00           ; zero high byte.
  15007.         LD      E,L             ; copy HL
  15008.         LD      D,H             ; to DE.
  15009.         ADD     HL,HL           ; double (*2)
  15010.         ADD     HL,HL           ; double (*4)
  15011.         ADD     HL,DE           ; add DE (*5)
  15012.         ADD     HL,HL           ; double (*10)
  15013.         LD      E,C             ; copy C to E    (D is 0)
  15014.         ADD     HL,DE           ; and add to give required result.
  15015.         LD      C,H             ; transfer to
  15016.         LD      A,L             ; destination registers.
  15017.         POP     DE              ; restore DE
  15018.         RET                     ; return with result.
  15019.  
  15020. ; --------------
  15021. ; Prepare to add
  15022. ; --------------
  15023. ; This routine is called twice by addition to prepare the two numbers. The
  15024. ; exponent is picked up in A and the location made zero. Then the sign bit
  15025. ; is tested before being set to the implied state. Negative numbers are twos
  15026. ; complemented.
  15027.  
  15028. ;; PREP-ADD
  15029. L2F9B:  LD      A,(HL)          ; pick up exponent
  15030.         LD      (HL),$00        ; make location zero
  15031.         AND     A               ; test if number is zero
  15032.         RET     Z               ; return if so
  15033.  
  15034.         INC     HL              ; address mantissa
  15035.         BIT     7,(HL)          ; test the sign bit
  15036.         SET     7,(HL)          ; set it to implied state
  15037.         DEC     HL              ; point to exponent
  15038.         RET     Z               ; return if positive number.
  15039.  
  15040.         PUSH    BC              ; preserve BC
  15041.         LD      BC,$0005        ; length of number
  15042.         ADD     HL,BC           ; point HL past end
  15043.         LD      B,C             ; set B to 5 counter
  15044.         LD      C,A             ; store exponent in C
  15045.         SCF                     ; set carry flag
  15046.  
  15047. ;; NEG-BYTE
  15048. L2FAF:  DEC     HL              ; work from LSB to MSB
  15049.         LD      A,(HL)          ; fetch byte
  15050.         CPL                     ; complement
  15051.         ADC     A,$00           ; add in initial carry or from prev operation
  15052.         LD      (HL),A          ; put back
  15053.         DJNZ    L2FAF           ; loop to NEG-BYTE till all 5 done
  15054.  
  15055.         LD      A,C             ; stored exponent to A
  15056.         POP     BC              ; restore original BC
  15057.         RET                     ; return
  15058.  
  15059. ; -----------------
  15060. ; Fetch two numbers
  15061. ; -----------------
  15062. ; This routine is called twice when printing floating point numbers and also
  15063. ; to fetch two numbers by the addition, multiply and division routines.
  15064. ; HL addresses the first number, DE addresses the second number.
  15065. ; For arithmetic only, A holds the sign of the result which is stored in
  15066. ; the second location.
  15067.  
  15068. ;; FETCH-TWO
  15069. L2FBA:  PUSH    HL              ; save pointer to first number, result if math.
  15070.         PUSH    AF              ; save result sign.
  15071.  
  15072.         LD      C,(HL)          ;
  15073.         INC     HL              ;
  15074.  
  15075.         LD      B,(HL)          ;
  15076.         LD      (HL),A          ; store the sign at correct location in
  15077.                                 ; destination 5 bytes for arithmetic only.
  15078.         INC     HL              ;
  15079.  
  15080.         LD      A,C             ;
  15081.         LD      C,(HL)          ;
  15082.         PUSH    BC              ;
  15083.         INC     HL              ;
  15084.         LD      C,(HL)          ;
  15085.         INC     HL              ;
  15086.         LD      B,(HL)          ;
  15087.         EX      DE,HL           ;
  15088.         LD      D,A             ;
  15089.         LD      E,(HL)          ;
  15090.         PUSH    DE              ;
  15091.         INC     HL              ;
  15092.         LD      D,(HL)          ;
  15093.         INC     HL              ;
  15094.         LD      E,(HL)          ;
  15095.         PUSH    DE              ;
  15096.         EXX                     ;
  15097.         POP     DE              ;
  15098.         POP     HL              ;
  15099.         POP     BC              ;
  15100.         EXX                     ;
  15101.         INC     HL              ;
  15102.         LD      D,(HL)          ;
  15103.         INC     HL              ;
  15104.         LD      E,(HL)          ;
  15105.  
  15106.         POP     AF              ; restore possible result sign.
  15107.         POP     HL              ; and pointer to possible result.
  15108.         RET                     ; return.
  15109.  
  15110. ; ---------------------------------
  15111. ; Shift floating point number right
  15112. ; ---------------------------------
  15113. ;
  15114. ;
  15115.  
  15116. ;; SHIFT-FP
  15117. L2FDD:  AND     A               ;
  15118.         RET     Z               ;
  15119.  
  15120.         CP      $21             ;
  15121.         JR      NC,L2FF9        ; to ADDEND-0
  15122.  
  15123.         PUSH    BC              ;
  15124.         LD      B,A             ;
  15125.  
  15126. ;; ONE-SHIFT
  15127. L2FE5:  EXX                     ;
  15128.         SRA     L               ;
  15129.         RR      D               ;
  15130.         RR      E               ;
  15131.         EXX                     ;
  15132.         RR      D               ;
  15133.         RR      E               ;
  15134.         DJNZ    L2FE5           ; to ONE-SHIFT
  15135.  
  15136.         POP     BC              ;
  15137.         RET     NC              ;
  15138.  
  15139.         CALL    L3004           ; routine ADD-BACK
  15140.         RET     NZ              ;
  15141.  
  15142. ;; ADDEND-0
  15143. L2FF9:  EXX                     ;
  15144.         XOR     A               ;
  15145.  
  15146. ;; ZEROS-4/5
  15147. L2FFB:  LD      L,$00           ;
  15148.         LD      D,A             ;
  15149.         LD      E,L             ;
  15150.         EXX                     ;
  15151.         LD      DE,$0000        ;
  15152.         RET                     ;
  15153.  
  15154. ; ------------------
  15155. ; Add back any carry
  15156. ; ------------------
  15157. ;
  15158. ;
  15159.  
  15160. ;; ADD-BACK
  15161. L3004:  INC     E               ;
  15162.         RET     NZ              ;
  15163.  
  15164.         INC      D              ;
  15165.         RET     NZ              ;
  15166.  
  15167.         EXX                     ;
  15168.         INC     E               ;
  15169.         JR      NZ,L300D        ; to ALL-ADDED
  15170.  
  15171.         INC     D               ;
  15172.  
  15173. ;; ALL-ADDED
  15174. L300D:  EXX                     ;
  15175.         RET                     ;
  15176.  
  15177. ; -----------------------
  15178. ; Handle subtraction (03)
  15179. ; -----------------------
  15180. ; Subtraction is done by switching the sign byte/bit of the second number
  15181. ; which may be integer of floating point and continuing into addition.
  15182.  
  15183. ;; subtract
  15184. L300F:  EX      DE,HL           ; address second number with HL
  15185.  
  15186.         CALL    L346E           ; routine NEGATE switches sign
  15187.  
  15188.         EX      DE,HL           ; address first number again
  15189.                                 ; and continue.
  15190.  
  15191. ; --------------------
  15192. ; Handle addition (0F)
  15193. ; --------------------
  15194. ; HL points to first number, DE to second.
  15195. ; If they are both integers, then go for the easy route.
  15196.  
  15197. ;; addition
  15198. L3014:  LD      A,(DE)          ; fetch first byte of second
  15199.         OR      (HL)            ; combine with first byte of first
  15200.         JR      NZ,L303E        ; forward to FULL-ADDN if at least one was
  15201.                                 ; in floating point form.
  15202.  
  15203. ; continue if both were small integers.
  15204.  
  15205.         PUSH    DE              ; save pointer to lowest number for result.
  15206.  
  15207.         INC     HL              ; address sign byte and
  15208.         PUSH    HL              ; push the pointer.
  15209.  
  15210.         INC     HL              ; address low byte
  15211.         LD      E,(HL)          ; to E
  15212.         INC     HL              ; address high byte
  15213.         LD      D,(HL)          ; to D
  15214.         INC     HL              ; address unused byte
  15215.  
  15216.         INC     HL              ; address known zero indicator of 1st number
  15217.         INC     HL              ; address sign byte
  15218.  
  15219.         LD      A,(HL)          ; sign to A, $00 or $FF
  15220.  
  15221.         INC     HL              ; address low byte
  15222.         LD      C,(HL)          ; to C
  15223.         INC     HL              ; address high byte
  15224.         LD      B,(HL)          ; to B
  15225.  
  15226.         POP     HL              ; pop result sign pointer
  15227.         EX      DE,HL           ; integer to HL
  15228.  
  15229.         ADD     HL,BC           ; add to the other one in BC
  15230.                                 ; setting carry if overflow.
  15231.  
  15232.         EX      DE,HL           ; save result in DE bringing back sign pointer
  15233.  
  15234.         ADC     A,(HL)          ; if pos/pos A=01 with overflow else 00
  15235.                                 ; if neg/neg A=FF with overflow else FE
  15236.                                 ; if mixture A=00 with overflow else FF
  15237.  
  15238.         RRCA                    ; bit 0 to (C)
  15239.  
  15240.         ADC     A,$00           ; both acceptable signs now zero
  15241.  
  15242.         JR      NZ,L303C        ; forward to ADDN-OFLW if not
  15243.  
  15244.         SBC     A,A             ; restore a negative result sign
  15245.  
  15246.         LD      (HL),A          ;
  15247.         INC     HL              ;
  15248.         LD      (HL),E          ;
  15249.         INC     HL              ;
  15250.         LD      (HL),D          ;
  15251.         DEC     HL              ;
  15252.         DEC     HL              ;
  15253.         DEC     HL              ;
  15254.  
  15255.         POP     DE              ; STKEND
  15256.         RET                     ;
  15257.  
  15258. ; ---
  15259.  
  15260. ;; ADDN-OFLW
  15261. L303C:  DEC     HL              ;
  15262.         POP     DE              ;
  15263.  
  15264. ;; FULL-ADDN
  15265. L303E:  CALL    L3293           ; routine RE-ST-TWO
  15266.         EXX                     ;
  15267.         PUSH    HL              ;
  15268.         EXX                     ;
  15269.         PUSH    DE              ;
  15270.         PUSH    HL              ;
  15271.         CALL    L2F9B           ; routine PREP-ADD
  15272.         LD      B,A             ;
  15273.         EX      DE,HL           ;
  15274.         CALL    L2F9B           ; routine PREP-ADD
  15275.         LD       C,A            ;
  15276.         CP      B               ;
  15277.         JR      NC,L3055        ; to SHIFT-LEN
  15278.  
  15279.         LD      A,B             ;
  15280.         LD      B,C             ;
  15281.         EX      DE,HL           ;
  15282.  
  15283. ;; SHIFT-LEN
  15284. L3055:  PUSH    AF              ;
  15285.         SUB     B               ;
  15286.         CALL    L2FBA           ; routine FETCH-TWO
  15287.         CALL    L2FDD           ; routine SHIFT-FP
  15288.         POP     AF              ;
  15289.         POP     HL              ;
  15290.         LD      (HL),A          ;
  15291.         PUSH    HL              ;
  15292.         LD      L,B             ;
  15293.         LD      H,C             ;
  15294.         ADD     HL,DE           ;
  15295.         EXX                     ;
  15296.         EX      DE,HL           ;
  15297.         ADC     HL,BC           ;
  15298.         EX      DE,HL           ;
  15299.         LD      A,H             ;
  15300.         ADC     A,L             ;
  15301.         LD      L,A             ;
  15302.         RRA                     ;
  15303.         XOR     L               ;
  15304.         EXX                     ;
  15305.         EX      DE,HL           ;
  15306.         POP     HL              ;
  15307.         RRA                     ;
  15308.         JR      NC,L307C        ; to TEST-NEG
  15309.  
  15310.         LD      A,$01           ;
  15311.         CALL    L2FDD           ; routine SHIFT-FP
  15312.         INC     (HL)            ;
  15313.         JR      Z,L309F         ; to ADD-REP-6
  15314.  
  15315. ;; TEST-NEG
  15316. L307C:  EXX                     ;
  15317.         LD      A,L             ;
  15318.         AND     $80             ;
  15319.         EXX                     ;
  15320.         INC     HL              ;
  15321.         LD      (HL),A          ;
  15322.         DEC     HL              ;
  15323.         JR      Z,L30A5         ; to GO-NC-MLT
  15324.  
  15325.         LD      A,E             ;
  15326.         NEG                     ; Negate
  15327.         CCF                     ; Complement Carry Flag
  15328.         LD      E,A             ;
  15329.         LD      A,D             ;
  15330.         CPL                     ;
  15331.         ADC     A,$00           ;
  15332.         LD      D,A             ;
  15333.         EXX                     ;
  15334.         LD      A,E             ;
  15335.         CPL                     ;
  15336.         ADC     A,$00           ;
  15337.         LD      E,A             ;
  15338.         LD      A,D             ;
  15339.         CPL                     ;
  15340.         ADC     A,$00           ;
  15341.         JR      NC,L30A3        ; to END-COMPL
  15342.  
  15343.         RRA                     ;
  15344.         EXX                     ;
  15345.         INC     (HL)            ;
  15346.  
  15347. ;; ADD-REP-6
  15348. L309F:  JP      Z,L31AD         ; to REPORT-6
  15349.  
  15350.         EXX                     ;
  15351.  
  15352. ;; END-COMPL
  15353. L30A3:  LD      D,A             ;
  15354.         EXX                     ;
  15355.  
  15356. ;; GO-NC-MLT
  15357. L30A5:  XOR     A               ;
  15358.         JP      L3155           ; to TEST-NORM
  15359.  
  15360. ; -----------------------------
  15361. ; Used in 16 bit multiplication
  15362. ; -----------------------------
  15363. ; This routine is used, in the first instance, by the multiply calculator
  15364. ; literal to perform an integer multiplication in preference to
  15365. ; 32-bit multiplication to which it will resort if this overflows.
  15366. ;
  15367. ; It is also used by STK-VAR to calculate array subscripts and by DIM to
  15368. ; calculate the space required for multi-dimensional arrays.
  15369.  
  15370. ;; HL-HL*DE
  15371. L30A9:  PUSH    BC              ; preserve BC throughout
  15372.         LD      B,$10           ; set B to 16
  15373.         LD      A,H             ; save H in A high byte
  15374.         LD      C,L             ; save L in C low byte
  15375.         LD      HL,$0000        ; initialize result to zero
  15376.  
  15377. ; now enter a loop.
  15378.  
  15379. ;; HL-LOOP
  15380. L30B1:  ADD     HL,HL           ; double result
  15381.         JR      C,L30BE         ; to HL-END if overflow
  15382.  
  15383.         RL      C               ; shift AC left into carry
  15384.         RLA                     ;
  15385.         JR      NC,L30BC        ; to HL-AGAIN to skip addition if no carry
  15386.  
  15387.         ADD     HL,DE           ; add in DE
  15388.         JR      C,L30BE         ; to HL-END if overflow
  15389.  
  15390. ;; HL-AGAIN
  15391. L30BC:  DJNZ    L30B1           ; back to HL-LOOP for all 16 bits
  15392.  
  15393. ;; HL-END
  15394. L30BE:  POP     BC              ; restore preserved BC
  15395.         RET                     ; return with carry reset if successful
  15396.                                 ; and result in HL.
  15397.  
  15398. ; -----------------------------
  15399. ; Prepare to multiply or divide
  15400. ; -----------------------------
  15401. ; This routine is called in succession from multiply and divide to prepare
  15402. ; two mantissas by setting the leftmost bit that is used for the sign.
  15403. ; On the first call A holds zero and picks up the sign bit. On the second
  15404. ; call the two bits are XORed to form the result sign - minus * minus giving
  15405. ; plus etc. If either number is zero then this is flagged.
  15406. ; HL addresses the exponent.
  15407.  
  15408. ;; PREP-M/D
  15409. L30C0:  CALL    L34E9           ; routine TEST-ZERO  preserves accumulator.
  15410.         RET     C               ; return carry set if zero
  15411.  
  15412.         INC     HL              ; address first byte of mantissa
  15413.         XOR     (HL)            ; pick up the first or xor with first.
  15414.         SET     7,(HL)          ; now set to give true 32-bit mantissa
  15415.         DEC     HL              ; point to exponent
  15416.         RET                     ; return with carry reset
  15417.  
  15418. ; --------------------------
  15419. ; Handle multiplication (04)
  15420. ; --------------------------
  15421. ;
  15422. ;
  15423.  
  15424. ;; multiply
  15425. L30CA:  LD      A,(DE)          ;
  15426.         OR      (HL)            ;
  15427.         JR      NZ,L30F0        ; to MULT-LONG
  15428.  
  15429.         PUSH    DE              ;
  15430.         PUSH    HL              ;
  15431.         PUSH    DE              ;
  15432.         CALL    L2D7F           ; routine INT-FETCH
  15433.         EX      DE,HL           ;
  15434.         EX      (SP),HL         ;
  15435.         LD      B,C             ;
  15436.         CALL    L2D7F           ; routine INT-FETCH
  15437.         LD      A,B             ;
  15438.         XOR     C               ;
  15439.         LD      C,A             ;
  15440.         POP     HL              ;
  15441.         CALL    L30A9           ; routine HL-HL*DE
  15442.         EX      DE,HL           ;
  15443.         POP     HL              ;
  15444.         JR      C,L30EF         ; to MULT-OFLW
  15445.  
  15446.         LD      A,D             ;
  15447.         OR      E               ;
  15448.         JR      NZ,L30EA        ; to MULT-RSLT
  15449.  
  15450.         LD      C,A             ;
  15451.  
  15452. ;; MULT-RSLT
  15453. L30EA:  CALL    L2D8E           ; routine INT-STORE
  15454.         POP      DE             ;
  15455.         RET                     ;
  15456.  
  15457. ; ---
  15458.  
  15459. ;; MULT-OFLW
  15460. L30EF:  POP     DE              ;
  15461.  
  15462. ;; MULT-LONG
  15463. L30F0:  CALL    L3293           ; routine RE-ST-TWO
  15464.         XOR     A               ;
  15465.         CALL    L30C0           ; routine PREP-M/D
  15466.         RET     C               ;
  15467.  
  15468.         EXX                     ;
  15469.         PUSH    HL              ;
  15470.         EXX                     ;
  15471.         PUSH    DE              ;
  15472.         EX      DE,HL           ;
  15473.         CALL    L30C0           ; routine PREP-M/D
  15474.         EX      DE,HL           ;
  15475.         JR      C,L315D         ; to ZERO-RSLT
  15476.  
  15477.         PUSH    HL              ;
  15478.         CALL    L2FBA           ; routine FETCH-TWO
  15479.         LD      A,B             ;
  15480.         AND     A               ;
  15481.         SBC     HL,HL           ;
  15482.         EXX                     ;
  15483.         PUSH    HL              ;
  15484.         SBC     HL,HL           ;
  15485.         EXX                     ;
  15486.         LD      B,$21           ;
  15487.         JR      L3125           ; to STRT-MLT
  15488.  
  15489. ; ---
  15490.  
  15491. ;; MLT-LOOP
  15492. L3114:  JR      NC,L311B        ; to NO-ADD
  15493.  
  15494.         ADD     HL,DE           ;
  15495.         EXX                     ;
  15496.         ADC     HL,DE           ;
  15497.         EXX                     ;
  15498.  
  15499. ;; NO-ADD
  15500. L311B:  EXX                     ;
  15501.         RR      H               ;
  15502.         RR      L               ;
  15503.         EXX                     ;
  15504.         RR      H               ;
  15505.         RR      L               ;
  15506.  
  15507. ;; STRT-MLT
  15508. L3125:  EXX                     ;
  15509.         RR      B               ;
  15510.         RR      C               ;
  15511.         EXX                     ;
  15512.         RR      C               ;
  15513.         RRA                     ;
  15514.         DJNZ    L3114           ; to MLT-LOOP
  15515.  
  15516.         EX      DE,HL           ;
  15517.         EXX                     ;
  15518.         EX      DE,HL           ;
  15519.         EXX                     ;
  15520.         POP     BC              ;
  15521.         POP     HL              ;
  15522.         LD      A,B             ;
  15523.         ADD     A,C             ;
  15524.         JR      NZ,L313B        ; to MAKE-EXPT
  15525.  
  15526.         AND     A               ;
  15527.  
  15528. ;; MAKE-EXPT
  15529. L313B:  DEC     A               ;
  15530.         CCF                     ; Complement Carry Flag
  15531.  
  15532. ;; DIVN-EXPT
  15533. L313D:  RLA                     ;
  15534.         CCF                     ; Complement Carry Flag
  15535.         RRA                     ;
  15536.         JP      P,L3146         ; to OFLW1-CLR
  15537.  
  15538.         JR      NC,L31AD        ; to REPORT-6
  15539.  
  15540.         AND     A               ;
  15541.  
  15542. ;; OFLW1-CLR
  15543. L3146:  INC     A               ;
  15544.         JR      NZ,L3151        ; to OFLW2-CLR
  15545.  
  15546.         JR      C,L3151         ; to OFLW2-CLR
  15547.  
  15548.         EXX                     ;
  15549.         BIT     7,D             ;
  15550.         EXX                     ;
  15551.         JR      NZ,L31AD        ; to REPORT-6
  15552.  
  15553. ;; OFLW2-CLR
  15554. L3151:  LD      (HL),A          ;
  15555.         EXX                     ;
  15556.         LD      A,B             ;
  15557.         EXX                     ;
  15558.  
  15559. ;; TEST-NORM
  15560. L3155:  JR      NC,L316C        ; to NORMALISE
  15561.  
  15562.         LD      A,(HL)          ;
  15563.         AND     A               ;
  15564.  
  15565. ;; NEAR-ZERO
  15566. L3159:  LD      A,$80           ;
  15567.         JR      Z,L315E         ; to SKIP-ZERO
  15568.  
  15569. ;; ZERO-RSLT
  15570. L315D:  XOR     A               ;
  15571.  
  15572. ;; SKIP-ZERO
  15573. L315E:  EXX                     ;
  15574.         AND     D               ;
  15575.         CALL    L2FFB           ; routine ZEROS-4/5
  15576.         RLCA                    ;
  15577.         LD      (HL),A          ;
  15578.         JR      C,L3195         ; to OFLOW-CLR
  15579.  
  15580.         INC     HL              ;
  15581.         LD      (HL),A          ;
  15582.         DEC     HL              ;
  15583.         JR      L3195           ; to OFLOW-CLR
  15584.  
  15585. ; ---
  15586.  
  15587. ;; NORMALISE
  15588. L316C:  LD      B,$20           ;
  15589.  
  15590. ;; SHIFT-ONE
  15591. L316E:  EXX                     ;
  15592.         BIT     7,D             ;
  15593.         EXX                     ;
  15594.         JR      NZ,L3186        ; to NORML-NOW
  15595.  
  15596.         RLCA                    ;
  15597.         RL      E               ;
  15598.         RL      D               ;
  15599.         EXX                     ;
  15600.         RL      E               ;
  15601.         RL      D               ;
  15602.         EXX                     ;
  15603.         DEC     (HL)            ;
  15604.         JR      Z,L3159         ; to NEAR-ZERO
  15605.  
  15606.         DJNZ    L316E           ; to SHIFT-ONE
  15607.  
  15608.         JR      L315D           ; to ZERO-RSLT
  15609.  
  15610. ; ---
  15611.  
  15612. ;; NORML-NOW
  15613. L3186:  RLA                     ;
  15614.         JR      NC,L3195        ; to OFLOW-CLR
  15615.  
  15616.         CALL    L3004           ; routine ADD-BACK
  15617.         JR      NZ,L3195        ; to OFLOW-CLR
  15618.  
  15619.         EXX                     ;
  15620.         LD       D,$80          ;
  15621.         EXX                     ;
  15622.         INC     (HL)            ;
  15623.         JR      Z,L31AD         ; to REPORT-6
  15624.  
  15625. ;; OFLOW-CLR
  15626. L3195:  PUSH    HL              ;
  15627.         INC     HL              ;
  15628.         EXX                     ;
  15629.         PUSH    DE              ;
  15630.         EXX                     ;
  15631.         POP     BC              ;
  15632.         LD      A,B             ;
  15633.         RLA                     ;
  15634.         RL      (HL)            ;
  15635.         RRA                     ;
  15636.         LD      (HL),A          ;
  15637.         INC     HL              ;
  15638.         LD      (HL),C          ;
  15639.         INC     HL              ;
  15640.         LD      (HL),D          ;
  15641.         INC     HL              ;
  15642.         LD      (HL),E          ;
  15643.         POP     HL              ;
  15644.         POP     DE              ;
  15645.         EXX                     ;
  15646.         POP     HL              ;
  15647.         EXX                     ;
  15648.         RET                     ;
  15649.  
  15650. ; ---
  15651.  
  15652. ;; REPORT-6
  15653. L31AD:  RST     08H             ; ERROR-1
  15654.         DB    $05             ; Error Report: Number too big
  15655.  
  15656. ; --------------------
  15657. ; Handle division (05)
  15658. ; --------------------
  15659. ;
  15660. ;
  15661.  
  15662. ;; division
  15663. L31AF:  CALL    L3293           ; routine RE-ST-TWO
  15664.         EX      DE,HL           ;
  15665.         XOR     A               ;
  15666.         CALL    L30C0           ; routine PREP-M/D
  15667.         JR      C,L31AD         ; to REPORT-6
  15668.  
  15669.         EX      DE,HL           ;
  15670.         CALL    L30C0           ; routine PREP-M/D
  15671.         RET     C               ;
  15672.  
  15673.         EXX                     ;
  15674.         PUSH    HL              ;
  15675.         EXX                     ;
  15676.         PUSH    DE              ;
  15677.         PUSH    HL              ;
  15678.         CALL    L2FBA           ; routine FETCH-TWO
  15679.         EXX                     ;
  15680.         PUSH    HL              ;
  15681.         LD      H,B             ;
  15682.         LD      L,C             ;
  15683.         EXX                     ;
  15684.         LD      H,C             ;
  15685.         LD      L,B             ;
  15686.         XOR     A               ;
  15687.         LD      B,$DF           ;
  15688.         JR      L31E2           ; to DIV-START
  15689.  
  15690. ; ---
  15691.  
  15692. ;; DIV-LOOP
  15693. L31D2:  RLA                     ;
  15694.         RL      C               ;
  15695.         EXX                     ;
  15696.         RL      C               ;
  15697.         RL      B               ;
  15698.         EXX                     ;
  15699.  
  15700. ;; div-34th
  15701. L31DB:  ADD     HL,HL           ;
  15702.         EXX                     ;
  15703.         ADC     HL,HL           ;
  15704.         EXX                     ;
  15705.         JR      C,L31F2         ; to SUBN-ONLY
  15706.  
  15707. ;; DIV-START
  15708. L31E2:  SBC     HL,DE           ;
  15709.         EXX                     ;
  15710.         SBC     HL,DE           ;
  15711.         EXX                     ;
  15712.         JR      NC,L31F9        ; to NO-RSTORE
  15713.  
  15714.         ADD     HL,DE           ;
  15715.         EXX                     ;
  15716.         ADC     HL,DE           ;
  15717.         EXX                     ;
  15718.         AND     A               ;
  15719.         JR      L31FA           ; to COUNT-ONE
  15720.  
  15721. ; ---
  15722.  
  15723. ;; SUBN-ONLY
  15724. L31F2:  AND     A               ;
  15725.         SBC     HL,DE           ;
  15726.         EXX                     ;
  15727.         SBC     HL,DE           ;
  15728.         EXX                     ;
  15729.  
  15730. ;; NO-RSTORE
  15731. L31F9:  SCF                     ; Set Carry Flag
  15732.  
  15733. ;; COUNT-ONE
  15734. L31FA:  INC     B               ;
  15735.         JP      M,L31D2         ; to DIV-LOOP
  15736.  
  15737.         PUSH    AF              ;
  15738.         JR      Z,L31E2         ; to DIV-START
  15739.  
  15740. ;
  15741. ;
  15742. ;
  15743. ;
  15744.  
  15745.         LD      E,A             ;
  15746.         LD      D,C             ;
  15747.         EXX                     ;
  15748.         LD      E,C             ;
  15749.         LD      D,B             ;
  15750.         POP     AF              ;
  15751.         RR      B               ;
  15752.         POP     AF              ;
  15753.         RR      B               ;
  15754.         EXX                     ;
  15755.         POP     BC              ;
  15756.         POP     HL              ;
  15757.         LD      A,B             ;
  15758.         SUB     C               ;
  15759.         JP      L313D           ; jump back to DIVN-EXPT
  15760.  
  15761. ; ------------------------------------
  15762. ; Integer truncation towards zero ($3A)
  15763. ; ------------------------------------
  15764. ;
  15765. ;
  15766.  
  15767. ;; truncate
  15768. L3214:  LD      A,(HL)          ;
  15769.         AND     A               ;
  15770.         RET     Z               ;
  15771.  
  15772.         CP      $81             ;
  15773.         JR      NC,L3221        ; to T-GR-ZERO
  15774.  
  15775.         LD      (HL),$00        ;
  15776.         LD      A,$20           ;
  15777.         JR      L3272           ; to NIL-BYTES
  15778.  
  15779. ; ---
  15780.  
  15781. ;; T-GR-ZERO
  15782. L3221:  CP      $91             ;
  15783.         JR      NZ,L323F        ; to T-SMALL
  15784.  
  15785.         INC     HL              ;
  15786.         INC     HL              ;
  15787.         INC     HL              ;
  15788.         LD      A,$80           ;
  15789.         AND     (HL)            ;
  15790.         DEC      HL             ;
  15791.         OR      (HL)            ;
  15792.         DEC     HL              ;
  15793.         JR      NZ,L3233        ; to T-FIRST
  15794.  
  15795.         LD      A,$80           ;
  15796.         XOR     (HL)            ;
  15797.  
  15798. ;; T-FIRST
  15799. L3233:  DEC     HL              ;
  15800.         JR      NZ,L326C        ; to T-EXPNENT
  15801.  
  15802.         LD      (HL),A          ;
  15803.         INC     HL              ;
  15804.         LD      (HL),$FF        ;
  15805.         DEC     HL              ;
  15806.         LD      A,$18           ;
  15807.         JR      L3272           ; to NIL-BYTES
  15808.  
  15809. ; ---
  15810.  
  15811. ;; T-SMALL
  15812. L323F:  JR      NC,L326D        ; to X-LARGE
  15813.  
  15814.         PUSH    DE              ;
  15815.         CPL                     ;
  15816.         ADD     A,$91           ;
  15817.         INC     HL              ;
  15818.         LD      D,(HL)          ;
  15819.         INC     HL              ;
  15820.         LD      E,(HL)          ;
  15821.         DEC     HL              ;
  15822.         DEC     HL              ;
  15823.         LD      C,$00           ;
  15824.         BIT     7,D             ;
  15825.         JR      Z,L3252         ; to T-NUMERIC
  15826.  
  15827.         DEC     C               ;
  15828.  
  15829. ;; T-NUMERIC
  15830. L3252:  SET     7,D             ;
  15831.         LD      B,$08           ;
  15832.         SUB     B               ;
  15833.         ADD     A,B             ;
  15834.         JR      C,L325E         ; to T-TEST
  15835.  
  15836.         LD      E,D             ;
  15837.         LD      D,$00           ;
  15838.         SUB     B               ;
  15839.  
  15840. ;; T-TEST
  15841. L325E:  JR      Z,L3267         ; to T-STORE
  15842.  
  15843.         LD      B,A             ;
  15844.  
  15845. ;; T-SHIFT
  15846. L3261:  SRL     D               ;
  15847.         RR      E               ;
  15848.         DJNZ    L3261           ; to T-SHIFT
  15849.  
  15850. ;; T-STORE
  15851. L3267:  CALL    L2D8E           ; routine INT-STORE
  15852.         POP     DE              ;
  15853.         RET                     ;
  15854.  
  15855. ; ---
  15856.  
  15857. ;; T-EXPNENT
  15858. L326C:  LD      A,(HL)          ;
  15859.  
  15860. ;; X-LARGE
  15861. L326D:  SUB     $A0             ;
  15862.         RET     P               ;
  15863.  
  15864.         NEG                     ; Negate
  15865.  
  15866. ;; NIL-BYTES
  15867. L3272:  PUSH    DE              ;
  15868.         EX      DE,HL           ;
  15869.         DEC     HL              ;
  15870.         LD      B,A             ;
  15871.         SRL     B               ;
  15872.         SRL     B               ;
  15873.         SRL     B               ;
  15874.         JR      Z,L3283         ; to BITS-ZERO
  15875.  
  15876. ;; BYTE-ZERO
  15877. L327E:  LD      (HL),$00        ;
  15878.         DEC     HL              ;
  15879.         DJNZ    L327E           ; to BYTE-ZERO
  15880.  
  15881. ;; BITS-ZERO
  15882. L3283:  AND     $07             ;
  15883.         JR      Z,L3290         ; to IX-END
  15884.  
  15885.         LD      B,A             ;
  15886.         LD      A,$FF           ;
  15887.  
  15888. ;; LESS-MASK
  15889. L328A:  SLA     A               ;
  15890.         DJNZ    L328A           ; to LESS-MASK
  15891.  
  15892.         AND     (HL)            ;
  15893.         LD      (HL),A          ;
  15894.  
  15895. ;; IX-END
  15896. L3290:  EX      DE,HL           ;
  15897.         POP     DE              ;
  15898.         RET                     ;
  15899.  
  15900. ; ----------------------------------
  15901. ; Storage of numbers in 5 byte form.
  15902. ; ==================================
  15903. ; Both integers and floating-point numbers can be stored in five bytes.
  15904. ; Zero is a special case stored as 5 zeros.
  15905. ; For integers the form is
  15906. ; Byte 1 - zero,
  15907. ; Byte 2 - sign byte, $00 +ve, $FF -ve.
  15908. ; Byte 3 - Low byte of integer.
  15909. ; Byte 4 - High byte
  15910. ; Byte 5 - unused but always zero.
  15911. ;
  15912. ; it seems unusual to store the low byte first but it is just as easy either
  15913. ; way. Statistically it just increases the chances of trailing zeros which
  15914. ; is an advantage elsewhere in saving ROM code.
  15915. ;
  15916. ;             zero     sign     low      high    unused
  15917. ; So +1 is  00000000 00000000 00000001 00000000 00000000
  15918. ;
  15919. ; and -1 is 00000000 11111111 11111111 11111111 00000000
  15920. ;
  15921. ; much of the arithmetic found in BASIC lines can be done using numbers
  15922. ; in this form using the Z80's 16 bit register operation ADD.
  15923. ; (multiplication is done by a sequence of additions).
  15924. ;
  15925. ; Storing -ve integers in two's complement form, means that they are ready for
  15926. ; addition and you might like to add the numbers above to prove that the
  15927. ; answer is zero. If, as in this case, the carry is set then that denotes that
  15928. ; the result is positive. This only applies when the signs don't match.
  15929. ; With positive numbers a carry denotes the result is out of integer range.
  15930. ; With negative numbers a carry denotes the result is within range.
  15931. ; The exception to the last rule is when the result is -65536
  15932. ;
  15933. ; Floating point form is an alternative method of storing numbers which can
  15934. ; be used for integers and larger (or fractional) numbers.
  15935. ;
  15936. ; In this form 1 is stored as
  15937. ;           10000001 00000000 00000000 00000000 00000000
  15938. ;
  15939. ; When a small integer is converted to a floating point number the last two
  15940. ; bytes are always blank so they are omitted in the following steps
  15941. ;
  15942. ; first make exponent +1 +16d  (bit 7 of the exponent is set if positive)
  15943.  
  15944. ; 10010001 00000000 00000001
  15945. ; 10010000 00000000 00000010 <-  now shift left and decrement exponent
  15946. ; ...
  15947. ; 10000010 01000000 00000000 <-  until a 1 abuts the imaginary point
  15948. ; 10000001 10000000 00000000     to the left of the mantissa.
  15949. ;
  15950. ; however since the leftmost bit of the mantissa is always set then it can
  15951. ; be used to denote the sign of the mantissa and put back when needed by the
  15952. ; PREP routines which gives
  15953. ;
  15954. ; 10000001 00000000 00000000
  15955.  
  15956. ; -----------------------------
  15957. ; Re-stack two `small' integers
  15958. ; -----------------------------
  15959. ; This routine is called to re-stack two numbers in full floating point form
  15960. ; e.g. from mult when integer multiplication has overflowed.
  15961.  
  15962. ;; RE-ST-TWO
  15963. L3293:  CALL    L3296           ; routine RESTK-SUB  below and continue
  15964.                                 ; into the routine to do the other one.
  15965.  
  15966. ;; RESTK-SUB
  15967. L3296:  EX      DE,HL           ; swap pointers
  15968.  
  15969. ; --------------------------------
  15970. ; Re-stack one number in full form
  15971. ; --------------------------------
  15972. ; This routine re-stacks an integer usually on the calculator stack
  15973. ; in full floating point form.
  15974. ; HL points to first byte.
  15975.  
  15976. ;; re-stack
  15977. L3297:  LD      A,(HL)          ; Fetch Exponent byte to A
  15978.         AND     A               ; test it
  15979.         RET     NZ              ; return if not zero as already in full
  15980.                                 ; floating-point form.
  15981.  
  15982.         PUSH    DE              ; preserve DE.
  15983.         CALL    L2D7F           ; routine INT-FETCH
  15984.                                 ; integer to DE, sign to C.
  15985.  
  15986. ; HL points to 4th byte.
  15987.  
  15988.         XOR     A               ; clear accumulator.
  15989.         INC     HL              ; point to 5th.
  15990.         LD      (HL),A          ; and blank.
  15991.         DEC     HL              ; point to 4th.
  15992.         LD      (HL),A          ; and blank.
  15993.  
  15994.         LD      B,$91           ; set exponent byte +ve $81
  15995.                                 ; and imaginary dec point 16 bits to right
  15996.                                 ; of first bit.
  15997.  
  15998. ; we could skip to normalize now but it's quicker to avoid
  15999. ; normalizing through an empty D.
  16000.  
  16001.         LD      A,D             ; fetch the high byte D
  16002.         AND     A               ; is it zero ?
  16003.         JR      NZ,L32B1        ; skip to RS-NRMLSE if not.
  16004.  
  16005.         OR      E               ; low byte E to A and test for zero
  16006.         LD      B,D             ; set B exponent to 0
  16007.         JR      Z,L32BD         ; forward to RS-STORE if value is zero.
  16008.  
  16009.         LD      D,E             ; transfer E to D
  16010.         LD      E,B             ; set E to 0
  16011.         LD      B,$89           ; reduce the initial exponent by eight.
  16012.  
  16013.  
  16014. ;; RS-NRMLSE
  16015. L32B1:  EX      DE,HL           ; integer to HL, addr of 4th byte to DE.
  16016.  
  16017. ;; RSTK-LOOP
  16018. L32B2:  DEC     B               ; decrease exponent
  16019.         ADD     HL,HL           ; shift DE left
  16020.         JR      NC,L32B2        ; loop back to RSTK-LOOP
  16021.                                 ; until a set bit pops into carry
  16022.  
  16023.         RRC     C               ; now rotate the sign byte $00 or $FF
  16024.                                 ; into carry to give a sign bit
  16025.  
  16026.         RR      H               ; rotate the sign bit to left of H
  16027.         RR      L               ; rotate any carry into L
  16028.  
  16029.         EX      DE,HL           ; address 4th byte, normalized int to DE
  16030.  
  16031. ;; RS-STORE
  16032. L32BD:  DEC     HL              ; address 3rd byte
  16033.         LD      (HL),E          ; place E
  16034.         DEC     HL              ; address 2nd byte
  16035.         LD      (HL),D          ; place D
  16036.         DEC     HL              ; address 1st byte
  16037.         LD      (HL),B          ; store the exponent
  16038.  
  16039.         POP     DE              ; restore initial DE.
  16040.         RET                     ; return.
  16041.  
  16042. ;****************************************
  16043. ;** Part 10. FLOATING-POINT CALCULATOR **
  16044. ;****************************************
  16045.  
  16046. ; As a general rule the calculator avoids using the IY register.
  16047. ; exceptions are val, val$ and str$.
  16048. ; So an assembly language programmer who has disabled interrupts to use
  16049. ; IY for other purposes can still use the calculator for mathematical
  16050. ; purposes.
  16051.  
  16052.  
  16053. ; ------------------
  16054. ; Table of constants
  16055. ; ------------------
  16056. ;
  16057. ;
  16058.  
  16059. ; used 11 times
  16060. ;; stk-zero                                                 00 00 00 00 00
  16061. L32C5:  DB    $00             ;;Bytes: 1
  16062.         DB    $B0             ;;Exponent $00
  16063.         DB    $00             ;;(+00,+00,+00)
  16064.  
  16065. ; used 19 times
  16066. ;; stk-one                                                  00 00 01 00 00
  16067. L32C8:  DB    $40             ;;Bytes: 2
  16068.         DB    $B0             ;;Exponent $00
  16069.         DB    $00,$01         ;;(+00,+00)
  16070.  
  16071. ; used 9 times
  16072. ;; stk-half                                                 80 00 00 00 00
  16073. L32CC:  DB    $30             ;;Exponent: $80, Bytes: 1
  16074.         DB    $00             ;;(+00,+00,+00)
  16075.  
  16076. ; used 4 times.
  16077. ;; stk-pi/2                                                 81 49 0F DA A2
  16078. L32CE:  DB    $F1             ;;Exponent: $81, Bytes: 4
  16079.         DB    $49,$0F,$DA,$A2 ;;
  16080.  
  16081. ; used 3 times.
  16082. ;; stk-ten                                                  00 00 0A 00 00
  16083. L32D3:  DB    $40             ;;Bytes: 2
  16084.         DB    $B0             ;;Exponent $00
  16085.         DB    $00,$0A         ;;(+00,+00)
  16086.  
  16087.  
  16088. ; ------------------
  16089. ; Table of addresses
  16090. ; ------------------
  16091. ;
  16092. ; starts with binary operations which have two operands and one result.
  16093. ; three pseudo binary operations first.
  16094.  
  16095. ;; tbl-addrs
  16096. L32D7:  DEFW    L368F           ; $00 Address: $368F - jump-true
  16097.         DEFW    L343C           ; $01 Address: $343C - exchange
  16098.         DEFW    L33A1           ; $02 Address: $33A1 - delete
  16099.  
  16100. ; true binary operations.
  16101.  
  16102.         DEFW    L300F           ; $03 Address: $300F - subtract
  16103.         DEFW    L30CA           ; $04 Address: $30CA - multiply
  16104.         DEFW    L31AF           ; $05 Address: $31AF - division
  16105.         DEFW    L3851           ; $06 Address: $3851 - to-power
  16106.         DEFW    L351B           ; $07 Address: $351B - or
  16107.  
  16108.         DEFW    L3524           ; $08 Address: $3524 - no-&-no
  16109.         DEFW    L353B           ; $09 Address: $353B - no-l-eql
  16110.         DEFW    L353B           ; $0A Address: $353B - no-gr-eql
  16111.         DEFW    L353B           ; $0B Address: $353B - nos-neql
  16112.         DEFW    L353B           ; $0C Address: $353B - no-grtr
  16113.         DEFW    L353B           ; $0D Address: $353B - no-less
  16114.         DEFW    L353B           ; $0E Address: $353B - nos-eql
  16115.         DEFW    L3014           ; $0F Address: $3014 - addition
  16116.  
  16117.         DEFW    L352D           ; $10 Address: $352D - str-&-no
  16118.         DEFW    L353B           ; $11 Address: $353B - str-l-eql
  16119.         DEFW    L353B           ; $12 Address: $353B - str-gr-eql
  16120.         DEFW    L353B           ; $13 Address: $353B - strs-neql
  16121.         DEFW    L353B           ; $14 Address: $353B - str-grtr
  16122.         DEFW    L353B           ; $15 Address: $353B - str-less
  16123.         DEFW    L353B           ; $16 Address: $353B - strs-eql
  16124.         DEFW    L359C           ; $17 Address: $359C - strs-add
  16125.  
  16126. ; unary follow
  16127.  
  16128.         DEFW    L35DE           ; $18 Address: $35DE - val$
  16129.         DEFW    L34BC           ; $19 Address: $34BC - usr-$
  16130.         DEFW    L3645           ; $1A Address: $3645 - read-in
  16131.         DEFW    L346E           ; $1B Address: $346E - negate
  16132.  
  16133.         DEFW    L3669           ; $1C Address: $3669 - code
  16134.         DEFW    L35DE           ; $1D Address: $35DE - val
  16135.         DEFW    L3674           ; $1E Address: $3674 - len
  16136.         DEFW    L37B5           ; $1F Address: $37B5 - sin
  16137.         DEFW    L37AA           ; $20 Address: $37AA - cos
  16138.         DEFW    L37DA           ; $21 Address: $37DA - tan
  16139.         DEFW    L3833           ; $22 Address: $3833 - asn
  16140.         DEFW    L3843           ; $23 Address: $3843 - acs
  16141.         DEFW    L37E2           ; $24 Address: $37E2 - atn
  16142.         DEFW    L3713           ; $25 Address: $3713 - ln
  16143.         DEFW    L36C4           ; $26 Address: $36C4 - exp
  16144.         DEFW    L36AF           ; $27 Address: $36AF - int
  16145.         DEFW    L384A           ; $28 Address: $384A - sqr
  16146.         DEFW    L3492           ; $29 Address: $3492 - sgn
  16147.         DEFW    L346A           ; $2A Address: $346A - abs
  16148.         DEFW    L34AC           ; $2B Address: $34AC - peek
  16149.         DEFW    L34A5           ; $2C Address: $34A5 - in
  16150.         DEFW    L34B3           ; $2D Address: $34B3 - usr-no
  16151.         DEFW    L361F           ; $2E Address: $361F - str$
  16152.         DEFW    L35C9           ; $2F Address: $35C9 - chrs
  16153.         DEFW    L3501           ; $30 Address: $3501 - not
  16154.  
  16155. ; end of true unary
  16156.  
  16157.         DEFW    L33C0           ; $31 Address: $33C0 - duplicate
  16158.         DEFW    L36A0           ; $32 Address: $36A0 - n-mod-m
  16159.         DEFW    L3686           ; $33 Address: $3686 - jump
  16160.         DEFW    L33C6           ; $34 Address: $33C6 - stk-data
  16161.         DEFW    L367A           ; $35 Address: $367A - dec-jr-nz
  16162.         DEFW    L3506           ; $36 Address: $3506 - less-0
  16163.         DEFW    L34F9           ; $37 Address: $34F9 - greater-0
  16164.         DEFW    L369B           ; $38 Address: $369B - end-calc
  16165.         DEFW    L3783           ; $39 Address: $3783 - get-argt
  16166.         DEFW    L3214           ; $3A Address: $3214 - truncate
  16167.         DEFW    L33A2           ; $3B Address: $33A2 - fp-calc-2
  16168.         DEFW    L2D4F           ; $3C Address: $2D4F - e-to-fp
  16169.         DEFW    L3297           ; $3D Address: $3297 - re-stack
  16170.  
  16171. ; the following are just the next available slots for the 128 compound literals
  16172. ; which are in range $80 - $FF.
  16173.  
  16174.         DEFW    L3449           ; $3E Address: $3449 - series-xx    $80 - $9F.
  16175.         DEFW    L341B           ; $3F Address: $341B - stk-const-xx $A0 - $BF.
  16176.         DEFW    L342D           ; $40 Address: $342D - st-mem-xx    $C0 - $DF.
  16177.         DEFW    L340F           ; $41 Address: $340F - get-mem-xx   $E0 - $FF.
  16178.  
  16179. ; Aside: 3E - 7F are therefore unused calculator literals.
  16180. ;        3E - 7B would be available for expansion.
  16181.  
  16182. ; --------------
  16183. ; The Calculator
  16184. ; --------------
  16185. ;
  16186. ;
  16187.  
  16188. ;; CALCULATE
  16189. L335B:  CALL    L35BF           ; routine STK-PNTRS is called to set up the
  16190.                                 ; calculator stack pointers for a default
  16191.                                 ; unary operation. HL = last value on stack.
  16192.                                 ; DE = STKEND first location after stack.
  16193.  
  16194. ; the calculate routine is called at this point by the series generator...
  16195.  
  16196. ;; GEN-ENT-1
  16197. L335E:  LD      A,B             ; fetch the Z80 B register to A
  16198.         LD      ($5C67),A       ; and store value in system variable BREG.
  16199.                                 ; this will be the counter for dec-jr-nz
  16200.                                 ; or if used from fp-calc2 the calculator
  16201.                                 ; instruction.
  16202.  
  16203. ; ... and again later at this point
  16204.  
  16205. ;; GEN-ENT-2
  16206. L3362:  EXX                     ; switch sets
  16207.         EX      (SP),HL         ; and store the address of next instruction,
  16208.                                 ; the return address, in H'L'.
  16209.                                 ; If this is a recursive call the the H'L'
  16210.                                 ; of the previous invocation goes on stack.
  16211.                                 ; c.f. end-calc.
  16212.         EXX                     ; switch back to main set
  16213.  
  16214. ; this is the re-entry looping point when handling a string of literals.
  16215.  
  16216. ;; RE-ENTRY
  16217. L3365:  LD      ($5C65),DE      ; save end of stack in system variable STKEND
  16218.         EXX                     ; switch to alt
  16219.         LD      A,(HL)          ; get next literal
  16220.         INC     HL              ; increase pointer'
  16221.  
  16222. ; single operation jumps back to here
  16223.  
  16224. ;; SCAN-ENT
  16225. L336C:  PUSH    HL              ; save pointer on stack
  16226.         AND     A               ; now test the literal
  16227.         JP      P,L3380         ; forward to FIRST-3D if in range $00 - $3D
  16228.                                 ; anything with bit 7 set will be one of
  16229.                                 ; 128 compound literals.
  16230.  
  16231. ; compound literals have the following format.
  16232. ; bit 7 set indicates compound.
  16233. ; bits 6-5 the subgroup 0-3.
  16234. ; bits 4-0 the embedded parameter $00 - $1F.
  16235. ; The subgroup 0-3 needs to be manipulated to form the next available four
  16236. ; address places after the simple literals in the address table.
  16237.  
  16238.         LD      D,A             ; save literal in D
  16239.         AND     $60             ; and with 01100000 to isolate subgroup
  16240.         RRCA                    ; rotate bits
  16241.         RRCA                    ; 4 places to right
  16242.         RRCA                    ; not five as we need offset * 2
  16243.         RRCA                    ; 00000xx0
  16244.         ADD     A,$7C           ; add ($3E * 2) to give correct offset.
  16245.                                 ; alter above if you add more literals.
  16246.         LD      L,A             ; store in L for later indexing.
  16247.         LD      A,D             ; bring back compound literal
  16248.         AND     $1F             ; use mask to isolate parameter bits
  16249.         JR      L338E           ; forward to ENT-TABLE
  16250.  
  16251. ; ---
  16252.  
  16253. ; the branch was here with simple literals.
  16254.  
  16255. ;; FIRST-3D
  16256. L3380:  CP      $18             ; compare with first unary operations.
  16257.         JR      NC,L338C        ; to DOUBLE-A with unary operations
  16258.  
  16259. ; it is binary so adjust pointers.
  16260.  
  16261.         EXX                     ;
  16262.         LD      BC,$FFFB        ; the value -5
  16263.         LD      D,H             ; transfer HL, the last value, to DE.
  16264.         LD      E,L             ;
  16265.         ADD     HL,BC           ; subtract 5 making HL point to second
  16266.                                 ; value.
  16267.         EXX                     ;
  16268.  
  16269. ;; DOUBLE-A
  16270. L338C:  RLCA                    ; double the literal
  16271.         LD      L,A             ; and store in L for indexing
  16272.  
  16273. ;; ENT-TABLE
  16274. L338E:  LD      DE,L32D7        ; Address: tbl-addrs
  16275.         LD      H,$00           ; prepare to index
  16276.         ADD     HL,DE           ; add to get address of routine
  16277.         LD      E,(HL)          ; low byte to E
  16278.         INC     HL              ;
  16279.         LD      D,(HL)          ; high byte to D
  16280.         LD      HL,L3365        ; Address: RE-ENTRY
  16281.         EX      (SP),HL         ; goes to stack
  16282.         PUSH    DE              ; now address of routine
  16283.         EXX                     ; main set
  16284.                                 ; avoid using IY register.
  16285.         LD      BC,($5C66)      ; STKEND_hi
  16286.                                 ; nothing much goes to C but BREG to B
  16287.                                 ; and continue into next ret instruction
  16288.                                 ; which has a dual identity
  16289.  
  16290.  
  16291. ; ------------------
  16292. ; Handle delete (02)
  16293. ; ------------------
  16294. ; A simple return but when used as a calculator literal this
  16295. ; deletes the last value from the calculator stack.
  16296. ; On entry, as always with binary operations,
  16297. ; HL=first number, DE=second number
  16298. ; On exit, HL=result, DE=stkend.
  16299. ; So nothing to do
  16300.  
  16301. ;; delete
  16302. L33A1:  RET                     ; return - indirect jump if from above.
  16303.  
  16304. ; ---------------------
  16305. ; Single operation (3B)
  16306. ; ---------------------
  16307. ; this single operation is used, in the first instance, to evaluate most
  16308. ; of the mathematical and string functions found in BASIC expressions.
  16309.  
  16310. ;; fp-calc-2
  16311. L33A2:  POP     AF              ; drop return address.
  16312.         LD      A,($5C67)       ; load accumulator from system variable BREG
  16313.                                 ; value will be literal eg. 'tan'
  16314.         EXX                     ; switch to alt
  16315.         JR      L336C           ; back to SCAN-ENT
  16316.                                 ; next literal will be end-calc at L2758
  16317.  
  16318. ; ----------------
  16319. ; Test five-spaces
  16320. ; ----------------
  16321. ; This routine is called from MOVE-FP, STK-CONST and STK-STORE to
  16322. ; test that there is enough space between the calculator stack and the
  16323. ; machine stack for another five-byte value. It returns with BC holding
  16324. ; the value 5 ready for any subsequent LDIR.
  16325.  
  16326. ;; TEST-5-SP
  16327. L33A9:  PUSH    DE              ; save
  16328.         PUSH    HL              ; registers
  16329.         LD      BC,$0005        ; an overhead of five bytes
  16330.         CALL    L1F05           ; routine TEST-ROOM tests free RAM raising
  16331.                                 ; an error if not.
  16332.         POP     HL              ; else restore
  16333.         POP     DE              ; registers.
  16334.         RET                     ; return with BC set at 5.
  16335.  
  16336. ; ------------
  16337. ; Stack number
  16338. ; ------------
  16339. ; This routine is called to stack a hidden floating point number found in
  16340. ; a BASIC line. It is also called to stack a numeric variable value, and
  16341. ; from BEEP, to stack an entry in the semi-tone table. It is not part of the
  16342. ; calculator suite of routines.
  16343. ; On entry HL points to the number to be stacked.
  16344.  
  16345. ;; STACK-NUM
  16346. L33B4:  LD      DE,($5C65)      ; load destination from STKEND system variable.
  16347.         CALL    L33C0           ; routine MOVE-FP puts on calculator stack
  16348.                                 ; with a memory check.
  16349.         LD      ($5C65),DE      ; set STKEND to next free location.
  16350.         RET                     ; return.
  16351.  
  16352. ; ---------------------------------
  16353. ; Move a floating point number (31)
  16354. ; ---------------------------------
  16355. ; This simple routine is a 5-byte LDIR instruction
  16356. ; that incorporates a memory check.
  16357. ; When used as a calculator literal it duplicates the last value on the
  16358. ; calculator stack.
  16359. ; Unary so on entry HL points to last value, DE to stkend
  16360.  
  16361. ;; duplicate
  16362. ;; MOVE-FP
  16363. L33C0:  CALL    L33A9           ; routine TEST-5-SP test free memory
  16364.                                 ; and sets BC to 5.
  16365.         LDIR                    ; copy the five bytes.
  16366.         RET                     ; return with DE addressing new STKEND
  16367.                                 ; and HL addressing new last value.
  16368.  
  16369. ; -------------------
  16370. ; Stack literals ($34)
  16371. ; -------------------
  16372. ; When a calculator subroutine needs to put a value on the calculator
  16373. ; stack that is not a regular constant this routine is called with a
  16374. ; variable number of following data bytes that convey to the routine
  16375. ; the integer or floating point form as succinctly as is possible.
  16376.  
  16377. ;; stk-data
  16378. L33C6:  LD      H,D             ; transfer STKEND
  16379.         LD      L,E             ; to HL for result.
  16380.  
  16381. ;; STK-CONST
  16382. L33C8:  CALL    L33A9           ; routine TEST-5-SP tests that room exists
  16383.                                 ; and sets BC to $05.
  16384.  
  16385.         EXX                     ; switch to alternate set
  16386.         PUSH    HL              ; save the pointer to next literal on stack
  16387.         EXX                     ; switch back to main set
  16388.  
  16389.         EX      (SP),HL         ; pointer to HL, destination to stack.
  16390.  
  16391.         PUSH    BC              ; save BC - value 5 from test room ??.
  16392.  
  16393.         LD      A,(HL)          ; fetch the byte following 'stk-data'
  16394.         AND     $C0             ; isolate bits 7 and 6
  16395.         RLCA                    ; rotate
  16396.         RLCA                    ; to bits 1 and 0  range $00 - $03.
  16397.         LD      C,A             ; transfer to C
  16398.         INC     C               ; and increment to give number of bytes
  16399.                                 ; to read. $01 - $04
  16400.         LD      A,(HL)          ; reload the first byte
  16401.         AND     $3F             ; mask off to give possible exponent.
  16402.         JR      NZ,L33DE        ; forward to FORM-EXP if it was possible to
  16403.                                 ; include the exponent.
  16404.  
  16405. ; else byte is just a byte count and exponent comes next.
  16406.  
  16407.         INC     HL              ; address next byte and
  16408.         LD      A,(HL)          ; pick up the exponent ( - $50).
  16409.  
  16410. ;; FORM-EXP
  16411. L33DE:  ADD     A,$50           ; now add $50 to form actual exponent
  16412.         LD      (DE),A          ; and load into first destination byte.
  16413.         LD      A,$05           ; load accumulator with $05 and
  16414.         SUB     C               ; subtract C to give count of trailing
  16415.                                 ; zeros plus one.
  16416.         INC     HL              ; increment source
  16417.         INC     DE              ; increment destination
  16418.         LD      B,$00           ; prepare to copy
  16419.         LDIR                    ; copy C bytes
  16420.  
  16421.         POP     BC              ; restore 5 counter to BC ??.
  16422.  
  16423.         EX      (SP),HL         ; put HL on stack as next literal pointer
  16424.                                 ; and the stack value - result pointer -
  16425.                                 ; to HL.
  16426.  
  16427.         EXX                     ; switch to alternate set.
  16428.         POP     HL              ; restore next literal pointer from stack
  16429.                                 ; to H'L'.
  16430.         EXX                     ; switch back to main set.
  16431.  
  16432.         LD      B,A             ; zero count to B
  16433.         XOR     A               ; clear accumulator
  16434.  
  16435. ;; STK-ZEROS
  16436. L33F1:  DEC     B               ; decrement B counter
  16437.         RET     Z               ; return if zero.          >>
  16438.                                 ; DE points to new STKEND
  16439.                                 ; HL to new number.
  16440.  
  16441.         LD      (DE),A          ; else load zero to destination
  16442.         INC     DE              ; increase destination
  16443.         JR      L33F1           ; loop back to STK-ZEROS until done.
  16444.  
  16445. ; -------------------------------
  16446. ; THE 'SKIP CONSTANTS' SUBROUTINE
  16447. ; -------------------------------
  16448. ; This routine traverses variable-length entries in the table of constants,
  16449. ; stacking intermediate, unwanted constants onto a dummy calculator stack,
  16450. ; in the first five bytes of ROM. The destination DE normally points to the
  16451. ; end of the calculator stack which might be in the normal place or in the
  16452. ; system variables area during E-LINE-NO; INT-TO-FP; stk-ten. In any case,
  16453. ; it would be simpler all round if the routine just shoved unwanted values
  16454. ; where it is going to stick the wanted value.
  16455. ; The instruction LD DE, $0000 can be removed.
  16456.  
  16457. ;; SKIP-CONS
  16458. L33F7:  AND     A               ; test if initially zero.
  16459.  
  16460. ;; SKIP-NEXT
  16461. L33F8:  RET     Z               ; return if zero.          >>
  16462.  
  16463.         PUSH    AF              ; save count.
  16464.         PUSH    DE              ; and normal STKEND
  16465.  
  16466.         LD      DE,$0000        ; dummy value for STKEND at start of ROM
  16467.                                 ; Note. not a fault but this has to be
  16468.                                 ; moved elsewhere when running in RAM.
  16469.                                 ; e.g. with Expandor Systems 'Soft ROM'.
  16470.                                 ; Better still, write to the normal place.
  16471.         CALL    L33C8           ; routine STK-CONST works through variable
  16472.                                 ; length records.
  16473.  
  16474.         POP     DE              ; restore real STKEND
  16475.         POP     AF              ; restore count
  16476.         DEC     A               ; decrease
  16477.         JR      L33F8           ; loop back to SKIP-NEXT
  16478.  
  16479. ; ---------------
  16480. ; Memory location
  16481. ; ---------------
  16482. ; This routine, when supplied with a base address in HL and an index in A
  16483. ; will calculate the address of the A'th entry, where each entry occupies
  16484. ; five bytes. It is used for reading the semi-tone table and addressing
  16485. ; floating-point numbers in the calculator's memory area.
  16486.  
  16487. ;; LOC-MEM
  16488. L3406:  LD      C,A             ; store the original number $00-$1F.
  16489.         RLCA                    ; double.
  16490.         RLCA                    ; quadruple.
  16491.         ADD     A,C             ; now add original to multiply by five.
  16492.  
  16493.         LD      C,A             ; place the result in C.
  16494.         LD      B,$00           ; set B to 0.
  16495.         ADD     HL,BC           ; add to form address of start of number in HL.
  16496.         RET                     ; return.
  16497.  
  16498. ; ------------------------------
  16499. ; Get from memory area ($E0 etc.)
  16500. ; ------------------------------
  16501. ; Literals $E0 to $FF
  16502. ; A holds $00-$1F offset.
  16503. ; The calculator stack increases by 5 bytes.
  16504.  
  16505. ;; get-mem-xx
  16506. L340F:  PUSH    DE              ; save STKEND
  16507.         LD      HL,($5C68)      ; MEM is base address of the memory cells.
  16508.         CALL    L3406           ; routine LOC-MEM so that HL = first byte
  16509.         CALL    L33C0           ; routine MOVE-FP moves 5 bytes with memory
  16510.                                 ; check.
  16511.                                 ; DE now points to new STKEND.
  16512.         POP     HL              ; original STKEND is now RESULT pointer.
  16513.         RET                     ; return.
  16514.  
  16515. ; --------------------------
  16516. ; Stack a constant (A0 etc.)
  16517. ; --------------------------
  16518. ; This routine allows a one-byte instruction to stack up to 32 constants
  16519. ; held in short form in a table of constants. In fact only 5 constants are
  16520. ; required. On entry the A register holds the literal ANDed with 1F.
  16521. ; It isn't very efficient and it would have been better to hold the
  16522. ; numbers in full, five byte form and stack them in a similar manner
  16523. ; to that used for semi-tone table values.
  16524.  
  16525. ;; stk-const-xx
  16526. L341B:  LD      H,D             ; save STKEND - required for result
  16527.         LD      L,E             ;
  16528.         EXX                     ; swap
  16529.         PUSH    HL              ; save pointer to next literal
  16530.         LD      HL,L32C5        ; Address: stk-zero - start of table of
  16531.                                 ; constants
  16532.         EXX                     ;
  16533.         CALL    L33F7           ; routine SKIP-CONS
  16534.         CALL    L33C8           ; routine STK-CONST
  16535.         EXX                     ;
  16536.         POP     HL              ; restore pointer to next literal.
  16537.         EXX                     ;
  16538.         RET                     ; return.
  16539.  
  16540. ; --------------------------------
  16541. ; Store in a memory area ($C0 etc.)
  16542. ; --------------------------------
  16543. ; Offsets $C0 to $DF
  16544. ; Although 32 memory storage locations can be addressed, only six
  16545. ; $C0 to $C5 are required by the ROM and only the thirty bytes (6*5)
  16546. ; required for these are allocated. Spectrum programmers who wish to
  16547. ; use the floating point routines from assembly language may wish to
  16548. ; alter the system variable MEM to point to 160 bytes of RAM to have
  16549. ; use the full range available.
  16550. ; A holds the derived offset $00-$1F.
  16551. ; This is a unary operation, so on entry HL points to the last value and DE
  16552. ; points to STKEND.
  16553.  
  16554. ;; st-mem-xx
  16555. L342D:  PUSH    HL              ; save the result pointer.
  16556.         EX      DE,HL           ; transfer to DE.
  16557.         LD      HL,($5C68)      ; fetch MEM the base of memory area.
  16558.         CALL    L3406           ; routine LOC-MEM sets HL to the destination.
  16559.         EX      DE,HL           ; swap - HL is start, DE is destination.
  16560.         CALL    L33C0           ; routine MOVE-FP.
  16561.                                 ; note. a short ld bc,5; ldir
  16562.                                 ; the embedded memory check is not required
  16563.                                 ; so these instructions would be faster.
  16564.         EX      DE,HL           ; DE = STKEND
  16565.         POP     HL              ; restore original result pointer
  16566.         RET                     ; return.
  16567.  
  16568. ; ------------------------------------
  16569. ; Swap first number with second number
  16570. ; ------------------------------------
  16571. ; This routine exchanges the last two values on the calculator stack
  16572. ; On entry, as always with binary operations,
  16573. ; HL=first number, DE=second number
  16574. ; On exit, HL=result, DE=stkend.
  16575.  
  16576. ;; exchange
  16577. L343C:  LD      B,$05           ; there are five bytes to be swapped
  16578.  
  16579. ; start of loop.
  16580.  
  16581. ;; SWAP-BYTE
  16582. L343E:  LD      A,(DE)          ; each byte of second
  16583.         LD      C,(HL)          ; each byte of first
  16584.         EX      DE,HL           ; swap pointers
  16585.         LD      (DE),A          ; store each byte of first
  16586.         LD      (HL),C          ; store each byte of second
  16587.         INC     HL              ; advance both
  16588.         INC     DE              ; pointers.
  16589.         DJNZ    L343E           ; loop back to SWAP-BYTE until all 5 done.
  16590.  
  16591.         EX      DE,HL           ; even up the exchanges
  16592.                                 ; so that DE addresses STKEND.
  16593.         RET                     ; return.
  16594.  
  16595. ; --------------------------
  16596. ; Series generator (86 etc.)
  16597. ; --------------------------
  16598. ; The Spectrum uses Chebyshev polynomials to generate approximations for
  16599. ; SIN, ATN, LN and EXP. These are named after the Russian mathematician
  16600. ; Pafnuty Chebyshev, born in 1821, who did much pioneering work on numerical
  16601. ; series. As far as calculators are concerned, Chebyshev polynomials have an
  16602. ; advantage over other series, for example the Taylor series, as they can
  16603. ; reach an approximation in just six iterations for SIN, eight for EXP and
  16604. ; twelve for LN and ATN. The mechanics of the routine are interesting but
  16605. ; for full treatment of how these are generated with demonstrations in
  16606. ; Sinclair BASIC see "The Complete Spectrum ROM Disassembly" by Dr Ian Logan
  16607. ; and Dr Frank O'Hara, published 1983 by Melbourne House.
  16608.  
  16609. ;; series-xx
  16610. L3449:  LD      B,A             ; parameter $00 - $1F to B counter
  16611.         CALL    L335E           ; routine GEN-ENT-1 is called.
  16612.                                 ; A recursive call to a special entry point
  16613.                                 ; in the calculator that puts the B register
  16614.                                 ; in the system variable BREG. The return
  16615.                                 ; address is the next location and where
  16616.                                 ; the calculator will expect its first
  16617.                                 ; instruction - now pointed to by HL'.
  16618.                                 ; The previous pointer to the series of
  16619.                                 ; five-byte numbers goes on the machine stack.
  16620.  
  16621. ; The initialization phase.
  16622.  
  16623.         DB    $31             ;;duplicate       x,x
  16624.         DB    $0F             ;;addition        x+x
  16625.         DB    $C0             ;;st-mem-0        x+x
  16626.         DB    $02             ;;delete          .
  16627.         DB    $A0             ;;stk-zero        0
  16628.         DB    $C2             ;;st-mem-2        0
  16629.  
  16630. ; a loop is now entered to perform the algebraic calculation for each of
  16631. ; the numbers in the series
  16632.  
  16633. ;; G-LOOP
  16634. L3453:  DB    $31             ;;duplicate       v,v.
  16635.         DB    $E0             ;;get-mem-0       v,v,x+2
  16636.         DB    $04             ;;multiply        v,v*x+2
  16637.         DB    $E2             ;;get-mem-2       v,v*x+2,v
  16638.         DB    $C1             ;;st-mem-1
  16639.         DB    $03             ;;subtract
  16640.         DB    $38             ;;end-calc
  16641.  
  16642. ; the previous pointer is fetched from the machine stack to H'L' where it
  16643. ; addresses one of the numbers of the series following the series literal.
  16644.  
  16645.         CALL    L33C6           ; routine STK-DATA is called directly to
  16646.                                 ; push a value and advance H'L'.
  16647.         CALL    L3362           ; routine GEN-ENT-2 recursively re-enters
  16648.                                 ; the calculator without disturbing
  16649.                                 ; system variable BREG
  16650.                                 ; H'L' value goes on the machine stack and is
  16651.                                 ; then loaded as usual with the next address.
  16652.  
  16653.         DB    $0F             ;;addition
  16654.         DB    $01             ;;exchange
  16655.         DB    $C2             ;;st-mem-2
  16656.         DB    $02             ;;delete
  16657.  
  16658.         DB    $35             ;;dec-jr-nz
  16659.         DB    $EE             ;;back to L3453, G-LOOP
  16660.  
  16661. ; when the counted loop is complete the final subtraction yields the result
  16662. ; for example SIN X.
  16663.  
  16664.         DB    $E1             ;;get-mem-1
  16665.         DB    $03             ;;subtract
  16666.         DB    $38             ;;end-calc
  16667.  
  16668.         RET                     ; return with H'L' pointing to location
  16669.                                 ; after last number in series.
  16670.  
  16671. ; -----------------------
  16672. ; Absolute magnitude (2A)
  16673. ; -----------------------
  16674. ; This calculator literal finds the absolute value of the last value,
  16675. ; integer or floating point, on calculator stack.
  16676.  
  16677. ;; abs
  16678. L346A:  LD      B,$FF           ; signal abs
  16679.         JR      L3474           ; forward to NEG-TEST
  16680.  
  16681. ; -----------------------
  16682. ; Handle unary minus (1B)
  16683. ; -----------------------
  16684. ; Unary so on entry HL points to last value, DE to STKEND.
  16685.  
  16686. ;; NEGATE
  16687. ;; negate
  16688. L346E:  CALL    L34E9           ; call routine TEST-ZERO and
  16689.         RET     C               ; return if so leaving zero unchanged.
  16690.  
  16691.         LD      B,$00           ; signal negate required before joining
  16692.                                 ; common code.
  16693.  
  16694. ;; NEG-TEST
  16695. L3474:  LD      A,(HL)          ; load first byte and
  16696.         AND     A               ; test for zero
  16697.         JR      Z,L3483         ; forward to INT-CASE if a small integer
  16698.  
  16699. ; for floating point numbers a single bit denotes the sign.
  16700.  
  16701.         INC     HL              ; address the first byte of mantissa.
  16702.         LD      A,B             ; action flag $FF=abs, $00=neg.
  16703.         AND     $80             ; now         $80      $00
  16704.         OR      (HL)            ; sets bit 7 for abs
  16705.         RLA                     ; sets carry for abs and if number negative
  16706.         CCF                     ; complement carry flag
  16707.         RRA                     ; and rotate back in altering sign
  16708.         LD      (HL),A          ; put the altered adjusted number back
  16709.         DEC     HL              ; HL points to result
  16710.         RET                     ; return with DE unchanged
  16711.  
  16712. ; ---
  16713.  
  16714. ; for integer numbers an entire byte denotes the sign.
  16715.  
  16716. ;; INT-CASE
  16717. L3483:  PUSH    DE              ; save STKEND.
  16718.  
  16719.         PUSH    HL              ; save pointer to the last value/result.
  16720.  
  16721.         CALL    L2D7F           ; routine INT-FETCH puts integer in DE
  16722.                                 ; and the sign in C.
  16723.  
  16724.         POP     HL              ; restore the result pointer.
  16725.  
  16726.         LD      A,B             ; $FF=abs, $00=neg
  16727.         OR      C               ; $FF for abs, no change neg
  16728.         CPL                     ; $00 for abs, switched for neg
  16729.         LD      C,A             ; transfer result to sign byte.
  16730.  
  16731.         CALL    L2D8E           ; routine INT-STORE to re-write the integer.
  16732.  
  16733.         POP     DE              ; restore STKEND.
  16734.         RET                     ; return.
  16735.  
  16736. ; -----------
  16737. ; Signum (29)
  16738. ; -----------
  16739. ; This routine replaces the last value on the calculator stack,
  16740. ; which may be in floating point or integer form, with the integer values
  16741. ; zero if zero, with one if positive and  with -minus one if negative.
  16742.  
  16743. ;; sgn
  16744. L3492:  CALL    L34E9           ; call routine TEST-ZERO and
  16745.         RET     C               ; exit if so as no change is required.
  16746.  
  16747.         PUSH    DE              ; save pointer to STKEND.
  16748.  
  16749.         LD      DE,$0001        ; the result will be 1.
  16750.         INC     HL              ; skip over the exponent.
  16751.         RL      (HL)            ; rotate the sign bit into the carry flag.
  16752.         DEC     HL              ; step back to point to the result.
  16753.         SBC     A,A             ; byte will be $FF if negative, $00 if positive.
  16754.         LD      C,A             ; store the sign byte in the C register.
  16755.         CALL    L2D8E           ; routine INT-STORE to overwrite the last
  16756.                                 ; value with 0001 and sign.
  16757.  
  16758.         POP     DE              ; restore STKEND.
  16759.         RET                     ; return.
  16760.  
  16761. ; -----------------------
  16762. ; Handle IN function (2C)
  16763. ; -----------------------
  16764. ; This function reads a byte from an input port.
  16765.  
  16766. ;; in
  16767. L34A5:  CALL    L1E99           ; routine FIND-INT2 puts port address in BC.
  16768.                                 ; all 16 bits are put on the address line.
  16769.         IN      A,(C)           ; read the port.
  16770.  
  16771.         JR      L34B0           ; exit to STACK-A (via IN-PK-STK to save a byte
  16772.                                 ; of instruction code).
  16773.  
  16774. ; -------------------------
  16775. ; Handle PEEK function (2B)
  16776. ; -------------------------
  16777. ; This function returns the contents of a memory address.
  16778. ; The entire address space can be peeked including the ROM.
  16779.  
  16780. ;; peek
  16781. L34AC:  CALL    L1E99           ; routine FIND-INT2 puts address in BC.
  16782.         LD      A,(BC)          ; load contents into A register.
  16783.  
  16784. ;; IN-PK-STK
  16785. L34B0:  JP      L2D28           ; exit via STACK-A to put value on the
  16786.                                 ; calculator stack.
  16787.  
  16788. ; ---------------
  16789. ; USR number (2D)
  16790. ; ---------------
  16791. ; The USR function followed by a number 0-65535 is the method by which
  16792. ; the Spectrum invokes machine code programs. This function returns the
  16793. ; contents of the BC register pair.
  16794. ; Note. that STACK-BC re-initializes the IY register if a user-written
  16795. ; program has altered it.
  16796.  
  16797. ;; usr-no
  16798. L34B3:  CALL    L1E99           ; routine FIND-INT2 to fetch the
  16799.                                 ; supplied address into BC.
  16800.  
  16801.         LD      HL,L2D2B        ; address: STACK-BC is
  16802.         PUSH    HL              ; pushed onto the machine stack.
  16803.         PUSH    BC              ; then the address of the machine code
  16804.                                 ; routine.
  16805.  
  16806.         RET                     ; make an indirect jump to the routine
  16807.                                 ; and, hopefully, to STACK-BC also.
  16808.  
  16809. ; ---------------
  16810. ; USR string (19)
  16811. ; ---------------
  16812. ; The user function with a one-character string argument, calculates the
  16813. ; address of the User Defined Graphic character that is in the string.
  16814. ; As an alternative, the ASCII equivalent, upper or lower case,
  16815. ; may be supplied. This provides a user-friendly method of redefining
  16816. ; the 21 User Definable Graphics e.g.
  16817. ; POKE USR "a", BIN 10000000 will put a dot in the top left corner of the
  16818. ; character 144.
  16819. ; Note. the curious double check on the range. With 26 UDGs the first check
  16820. ; only is necessary. With anything less the second check only is required.
  16821. ; It is highly likely that the first check was written by Steven Vickers.
  16822.  
  16823. ;; usr-$
  16824. L34BC:  CALL    L2BF1           ; routine STK-FETCH fetches the string
  16825.                                 ; parameters.
  16826.         DEC     BC              ; decrease BC by
  16827.         LD      A,B             ; one to test
  16828.         OR      C               ; the length.
  16829.         JR      NZ,L34E7        ; to REPORT-A if not a single character.
  16830.  
  16831.         LD      A,(DE)          ; fetch the character
  16832.         CALL    L2C8D           ; routine ALPHA sets carry if 'A-Z' or 'a-z'.
  16833.         JR      C,L34D3         ; forward to USR-RANGE if ASCII.
  16834.  
  16835.         SUB     $90             ; make udgs range 0-20d
  16836.         JR      C,L34E7         ; to REPORT-A if too low. e.g. usr " ".
  16837.  
  16838.         CP      $15             ; Note. this test is not necessary.
  16839.         JR      NC,L34E7        ; to REPORT-A if higher than 20.
  16840.  
  16841.         INC     A               ; make range 1-21d to match LSBs of ASCII
  16842.  
  16843. ;; USR-RANGE
  16844. L34D3:  DEC     A               ; make range of bits 0-4 start at zero
  16845.         ADD     A,A             ; multiply by eight
  16846.         ADD     A,A             ; and lose any set bits
  16847.         ADD     A,A             ; range now 0 - 25*8
  16848.         CP      $A8             ; compare to 21*8
  16849.         JR      NC,L34E7        ; to REPORT-A if originally higher
  16850.                                 ; than 'U','u' or graphics U.
  16851.  
  16852.         LD      BC,($5C7B)      ; fetch the UDG system variable value.
  16853.         ADD     A,C             ; add the offset to character
  16854.         LD      C,A             ; and store back in register C.
  16855.         JR      NC,L34E4        ; forward to USR-STACK if no overflow.
  16856.  
  16857.         INC     B               ; increment high byte.
  16858.  
  16859. ;; USR-STACK
  16860. L34E4:  JP      L2D2B           ; jump back and exit via STACK-BC to store
  16861.  
  16862. ; ---
  16863.  
  16864. ;; REPORT-A
  16865. L34E7:  RST     08H             ; ERROR-1
  16866.         DB    $09             ; Error Report: Invalid argument
  16867.  
  16868. ; -------------
  16869. ; Test for zero
  16870. ; -------------
  16871. ; Test if top value on calculator stack is zero.
  16872. ; The carry flag is set if the last value is zero but no registers are altered.
  16873. ; All five bytes will be zero but first four only need be tested.
  16874. ; On entry HL points to the exponent the first byte of the value.
  16875.  
  16876. ;; TEST-ZERO
  16877. L34E9:  PUSH    HL              ; preserve HL which is used to address.
  16878.         PUSH    BC              ; preserve BC which is used as a store.
  16879.         LD      B,A             ; preserve A in B.
  16880.  
  16881.         LD      A,(HL)          ; load first byte to accumulator
  16882.         INC     HL              ; advance.
  16883.         OR      (HL)            ; OR with second byte and clear carry.
  16884.         INC     HL              ; advance.
  16885.         OR      (HL)            ; OR with third byte.
  16886.         INC     HL              ; advance.
  16887.         OR      (HL)            ; OR with fourth byte.
  16888.  
  16889.         LD      A,B             ; restore A without affecting flags.
  16890.         POP     BC              ; restore the saved
  16891.         POP     HL              ; registers.
  16892.  
  16893.         RET     NZ              ; return if not zero and with carry reset.
  16894.  
  16895.         SCF                     ; set the carry flag.
  16896.         RET                     ; return with carry set if zero.
  16897.  
  16898. ; -----------------------
  16899. ; Greater than zero ($37)
  16900. ; -----------------------
  16901. ; Test if the last value on the calculator stack is greater than zero.
  16902. ; This routine is also called directly from the end-tests of the comparison
  16903. ; routine.
  16904.  
  16905. ;; GREATER-0
  16906. ;; greater-0
  16907. L34F9:  CALL    L34E9           ; routine TEST-ZERO
  16908.         RET     C               ; return if was zero as this
  16909.                                 ; is also the Boolean 'false' value.
  16910.  
  16911.         LD      A,$FF           ; prepare XOR mask for sign bit
  16912.         JR      L3507           ; forward to SIGN-TO-C
  16913.                                 ; to put sign in carry
  16914.                                 ; (carry will become set if sign is positive)
  16915.                                 ; and then overwrite location with 1 or 0
  16916.                                 ; as appropriate.
  16917.  
  16918. ; ------------------------
  16919. ; Handle NOT operator ($30)
  16920. ; ------------------------
  16921. ; This overwrites the last value with 1 if it was zero else with zero
  16922. ; if it was any other value.
  16923. ;
  16924. ; e.g NOT 0 returns 1, NOT 1 returns 0, NOT -3 returns 0.
  16925. ;
  16926. ; The subroutine is also called directly from the end-tests of the comparison
  16927. ; operator.
  16928.  
  16929. ;; NOT
  16930. ;; not
  16931. L3501:  CALL    L34E9           ; routine TEST-ZERO sets carry if zero
  16932.  
  16933.         JR      L350B           ; to FP-0/1 to overwrite operand with
  16934.                                 ; 1 if carry is set else to overwrite with zero.
  16935.  
  16936. ; -------------------
  16937. ; Less than zero (36)
  16938. ; -------------------
  16939. ; Destructively test if last value on calculator stack is less than zero.
  16940. ; Bit 7 of second byte will be set if so.
  16941.  
  16942. ;; less-0
  16943. L3506:  XOR     A               ; set xor mask to zero
  16944.                                 ; (carry will become set if sign is negative).
  16945.  
  16946. ; transfer sign of mantissa to Carry Flag.
  16947.  
  16948. ;; SIGN-TO-C
  16949. L3507:  INC     HL              ; address 2nd byte.
  16950.         XOR     (HL)            ; bit 7 of HL will be set if number is negative.
  16951.         DEC     HL              ; address 1st byte again.
  16952.         RLCA                    ; rotate bit 7 of A to carry.
  16953.  
  16954. ; -----------
  16955. ; Zero or one
  16956. ; -----------
  16957. ; This routine places an integer value of zero or one at the addressed location
  16958. ; of the calculator stack or MEM area.  The value one is written if carry is
  16959. ; set on entry else zero.
  16960.  
  16961. ;; FP-0/1
  16962. L350B:  PUSH    HL              ; save pointer to the first byte
  16963.         LD      A,$00           ; load accumulator with zero - without
  16964.                                 ; disturbing flags.
  16965.         LD      (HL),A          ; zero to first byte
  16966.         INC     HL              ; address next
  16967.         LD      (HL),A          ; zero to 2nd byte
  16968.         INC     HL              ; address low byte of integer
  16969.         RLA                     ; carry to bit 0 of A
  16970.         LD      (HL),A          ; load one or zero to low byte.
  16971.         RRA                     ; restore zero to accumulator.
  16972.         INC     HL              ; address high byte of integer.
  16973.         LD      (HL),A          ; put a zero there.
  16974.         INC     HL              ; address fifth byte.
  16975.         LD      (HL),A          ; put a zero there.
  16976.         POP     HL              ; restore pointer to the first byte.
  16977.         RET                     ; return.
  16978.  
  16979. ; -----------------------
  16980. ; Handle OR operator (07)
  16981. ; -----------------------
  16982. ; The Boolean OR operator. eg. X OR Y
  16983. ; The result is zero if both values are zero else a non-zero value.
  16984. ;
  16985. ; e.g.    0 OR 0  returns 0.
  16986. ;        -3 OR 0  returns -3.
  16987. ;         0 OR -3 returns 1.
  16988. ;        -3 OR 2  returns 1.
  16989. ;
  16990. ; A binary operation.
  16991. ; On entry HL points to first operand (X) and DE to second operand (Y).
  16992.  
  16993. ;; or
  16994. L351B:  EX      DE,HL           ; make HL point to second number
  16995.         CALL    L34E9           ; routine TEST-ZERO
  16996.         EX      DE,HL           ; restore pointers
  16997.         RET     C               ; return if result was zero - first operand,
  16998.                                 ; now the last value, is the result.
  16999.  
  17000.         SCF                     ; set carry flag
  17001.         JR      L350B           ; back to FP-0/1 to overwrite the first operand
  17002.                                 ; with the value 1.
  17003.  
  17004.  
  17005. ; -----------------------------
  17006. ; Handle number AND number (08)
  17007. ; -----------------------------
  17008. ; The Boolean AND operator.
  17009. ;
  17010. ; e.g.    -3 AND 2  returns -3.
  17011. ;         -3 AND 0  returns 0.
  17012. ;          0 and -2 returns 0.
  17013. ;          0 and 0  returns 0.
  17014. ;
  17015. ; Compare with OR routine above.
  17016.  
  17017. ;; no-&-no
  17018. L3524:  EX      DE,HL           ; make HL address second operand.
  17019.  
  17020.         CALL    L34E9           ; routine TEST-ZERO sets carry if zero.
  17021.  
  17022.         EX      DE,HL           ; restore pointers.
  17023.         RET     NC              ; return if second non-zero, first is result.
  17024.  
  17025. ;
  17026.  
  17027.         AND     A               ; else clear carry.
  17028.         JR      L350B           ; back to FP-0/1 to overwrite first operand
  17029.                                 ; with zero for return value.
  17030.  
  17031. ; -----------------------------
  17032. ; Handle string AND number (10)
  17033. ; -----------------------------
  17034. ; e.g. "You Win" AND score>99 will return the string if condition is true
  17035. ; or the null string if false.
  17036.  
  17037. ;; str-&-no
  17038. L352D:  EX      DE,HL           ; make HL point to the number.
  17039.         CALL    L34E9           ; routine TEST-ZERO.
  17040.         EX      DE,HL           ; restore pointers.
  17041.         RET     NC              ; return if number was not zero - the string
  17042.                                 ; is the result.
  17043.  
  17044. ; if the number was zero (false) then the null string must be returned by
  17045. ; altering the length of the string on the calculator stack to zero.
  17046.  
  17047.         PUSH    DE              ; save pointer to the now obsolete number
  17048.                                 ; (which will become the new STKEND)
  17049.  
  17050.         DEC     DE              ; point to the 5th byte of string descriptor.
  17051.         XOR     A               ; clear the accumulator.
  17052.         LD      (DE),A          ; place zero in high byte of length.
  17053.         DEC     DE              ; address low byte of length.
  17054.         LD      (DE),A          ; place zero there - now the null string.
  17055.  
  17056.         POP     DE              ; restore pointer - new STKEND.
  17057.         RET                     ; return.
  17058.  
  17059. ; -----------------------------------
  17060. ; Perform comparison ($09-$0E, $11-$16)
  17061. ; -----------------------------------
  17062. ; True binary operations.
  17063. ;
  17064. ; A single entry point is used to evaluate six numeric and six string
  17065. ; comparisons. On entry, the calculator literal is in the B register and
  17066. ; the two numeric values, or the two string parameters, are on the
  17067. ; calculator stack.
  17068. ; The individual bits of the literal are manipulated to group similar
  17069. ; operations although the SUB 8 instruction does nothing useful and merely
  17070. ; alters the string test bit.
  17071. ; Numbers are compared by subtracting one from the other, strings are
  17072. ; compared by comparing every character until a mismatch, or the end of one
  17073. ; or both, is reached.
  17074. ;
  17075. ; Numeric Comparisons.
  17076. ; --------------------
  17077. ; The 'x>y' example is the easiest as it employs straight-thru logic.
  17078. ; Number y is subtracted from x and the result tested for greater-0 yielding
  17079. ; a final value 1 (true) or 0 (false).
  17080. ; For 'x<y' the same logic is used but the two values are first swapped on the
  17081. ; calculator stack.
  17082. ; For 'x=y' NOT is applied to the subtraction result yielding true if the
  17083. ; difference was zero and false with anything else.
  17084. ; The first three numeric comparisons are just the opposite of the last three
  17085. ; so the same processing steps are used and then a final NOT is applied.
  17086. ;
  17087. ; literal    Test   No  sub 8       ExOrNot  1st RRCA  exch sub  ?   End-Tests
  17088. ; =========  ====   == ======== === ======== ========  ==== ===  =  === === ===
  17089. ; no-l-eql   x<=y   09 00000001 dec 00000000 00000000  ---- x-y  ?  --- >0? NOT
  17090. ; no-gr-eql  x>=y   0A 00000010 dec 00000001 10000000c swap y-x  ?  --- >0? NOT
  17091. ; nos-neql   x<>y   0B 00000011 dec 00000010 00000001  ---- x-y  ?  NOT --- NOT
  17092. ; no-grtr    x>y    0C 00000100  -  00000100 00000010  ---- x-y  ?  --- >0? ---
  17093. ; no-less    x<y    0D 00000101  -  00000101 10000010c swap y-x  ?  --- >0? ---
  17094. ; nos-eql    x=y    0E 00000110  -  00000110 00000011  ---- x-y  ?  NOT --- ---
  17095. ;
  17096. ;                                                           comp -> C/F
  17097. ;                                                           ====    ===
  17098. ; str-l-eql  x$<=y$ 11 00001001 dec 00001000 00000100  ---- x$y$ 0  !or >0? NOT
  17099. ; str-gr-eql x$>=y$ 12 00001010 dec 00001001 10000100c swap y$x$ 0  !or >0? NOT
  17100. ; strs-neql  x$<>y$ 13 00001011 dec 00001010 00000101  ---- x$y$ 0  !or >0? NOT
  17101. ; str-grtr   x$>y$  14 00001100  -  00001100 00000110  ---- x$y$ 0  !or >0? ---
  17102. ; str-less   x$<y$  15 00001101  -  00001101 10000110c swap y$x$ 0  !or >0? ---
  17103. ; strs-eql   x$=y$  16 00001110  -  00001110 00000111  ---- x$y$ 0  !or >0? ---
  17104. ;
  17105. ; String comparisons are a little different in that the eql/neql carry flag
  17106. ; from the 2nd RRCA is, as before, fed into the first of the end tests but
  17107. ; along the way it gets modified by the comparison process. The result on the
  17108. ; stack always starts off as zero and the carry fed in determines if NOT is
  17109. ; applied to it. So the only time the greater-0 test is applied is if the
  17110. ; stack holds zero which is not very efficient as the test will always yield
  17111. ; zero. The most likely explanation is that there were once separate end tests
  17112. ; for numbers and strings.
  17113.  
  17114. ;; no-l-eql, etc.
  17115. L353B:  LD      A,B             ; transfer literal to accumulator.
  17116.         SUB     $08             ; subtract eight - which is not useful.
  17117.  
  17118.         BIT     2,A             ; isolate '>', '<', '='.
  17119.  
  17120.         JR      NZ,L3543        ; skip to EX-OR-NOT with these.
  17121.  
  17122.         DEC     A               ; else make $00-$02, $08-$0A to match bits 0-2.
  17123.  
  17124. ;; EX-OR-NOT
  17125. L3543:  RRCA                    ; the first RRCA sets carry for a swap.
  17126.         JR      NC,L354E        ; forward to NU-OR-STR with other 8 cases
  17127.  
  17128. ; for the other 4 cases the two values on the calculator stack are exchanged.
  17129.  
  17130.         PUSH    AF              ; save A and carry.
  17131.         PUSH    HL              ; save HL - pointer to first operand.
  17132.                                 ; (DE points to second operand).
  17133.  
  17134.         CALL    L343C           ; routine exchange swaps the two values.
  17135.                                 ; (HL = second operand, DE = STKEND)
  17136.  
  17137.         POP     DE              ; DE = first operand
  17138.         EX      DE,HL           ; as we were.
  17139.         POP     AF              ; restore A and carry.
  17140.  
  17141. ; Note. it would be better if the 2nd RRCA preceded the string test.
  17142. ; It would save two duplicate bytes and if we also got rid of that sub 8
  17143. ; at the beginning we wouldn't have to alter which bit we test.
  17144.  
  17145. ;; NU-OR-STR
  17146. L354E:  BIT     2,A             ; test if a string comparison.
  17147.         JR      NZ,L3559        ; forward to STRINGS if so.
  17148.  
  17149. ; continue with numeric comparisons.
  17150.  
  17151.         RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17152.         PUSH    AF              ; save A and carry
  17153.  
  17154.         CALL    L300F           ; routine subtract leaves result on stack.
  17155.         JR      L358C           ; forward to END-TESTS
  17156.  
  17157. ; ---
  17158.  
  17159. ;; STRINGS
  17160. L3559:  RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17161.         PUSH    AF              ; save A and carry.
  17162.  
  17163.         CALL    L2BF1           ; routine STK-FETCH gets 2nd string params
  17164.         PUSH    DE              ; save start2 *.
  17165.         PUSH    BC              ; and the length.
  17166.  
  17167.         CALL    L2BF1           ; routine STK-FETCH gets 1st string
  17168.                                 ; parameters - start in DE, length in BC.
  17169.         POP     HL              ; restore length of second to HL.
  17170.  
  17171. ; A loop is now entered to compare, by subtraction, each corresponding character
  17172. ; of the strings. For each successful match, the pointers are incremented and
  17173. ; the lengths decreased and the branch taken back to here. If both string
  17174. ; remainders become null at the same time, then an exact match exists.
  17175.  
  17176. ;; BYTE-COMP
  17177. L3564:  LD      A,H             ; test if the second string
  17178.         OR      L               ; is the null string and hold flags.
  17179.  
  17180.         EX      (SP),HL         ; put length2 on stack, bring start2 to HL *.
  17181.         LD      A,B             ; hi byte of length1 to A
  17182.  
  17183.         JR      NZ,L3575        ; forward to SEC-PLUS if second not null.
  17184.  
  17185.         OR      C               ; test length of first string.
  17186.  
  17187. ;; SECND-LOW
  17188. L356B:  POP     BC              ; pop the second length off stack.
  17189.         JR      Z,L3572         ; forward to BOTH-NULL if first string is also
  17190.                                 ; of zero length.
  17191.  
  17192. ; the true condition - first is longer than second (SECND-LESS)
  17193.  
  17194.         POP     AF              ; restore carry (set if eql/neql)
  17195.         CCF                     ; complement carry flag.
  17196.                                 ; Note. equality becomes false.
  17197.                                 ; Inequality is true. By swapping or applying
  17198.                                 ; a terminal 'not', all comparisons have been
  17199.                                 ; manipulated so that this is success path.
  17200.         JR      L3588           ; forward to leave via STR-TEST
  17201.  
  17202. ; ---
  17203. ; the branch was here with a match
  17204.  
  17205. ;; BOTH-NULL
  17206. L3572:  POP     AF              ; restore carry - set for eql/neql
  17207.         JR      L3588           ; forward to STR-TEST
  17208.  
  17209. ; ---  
  17210. ; the branch was here when 2nd string not null and low byte of first is yet
  17211. ; to be tested.
  17212.  
  17213.  
  17214. ;; SEC-PLUS
  17215. L3575:  OR      C               ; test the length of first string.
  17216.         JR      Z,L3585         ; forward to FRST-LESS if length is zero.
  17217.  
  17218. ; both strings have at least one character left.
  17219.  
  17220.         LD      A,(DE)          ; fetch character of first string.
  17221.         SUB     (HL)            ; subtract with that of 2nd string.
  17222.         JR      C,L3585         ; forward to FRST-LESS if carry set
  17223.  
  17224.         JR      NZ,L356B        ; back to SECND-LOW and then STR-TEST
  17225.                                 ; if not exact match.
  17226.  
  17227.         DEC     BC              ; decrease length of 1st string.
  17228.         INC     DE              ; increment 1st string pointer.
  17229.  
  17230.         INC     HL              ; increment 2nd string pointer.
  17231.         EX      (SP),HL         ; swap with length on stack
  17232.         DEC     HL              ; decrement 2nd string length
  17233.         JR      L3564           ; back to BYTE-COMP
  17234.  
  17235. ; ---
  17236. ; the false condition.
  17237.  
  17238. ;; FRST-LESS
  17239. L3585:  POP     BC              ; discard length
  17240.         POP     AF              ; pop A
  17241.         AND     A               ; clear the carry for false result.
  17242.  
  17243. ; ---
  17244. ; exact match and x$>y$ rejoin here
  17245.  
  17246. ;; STR-TEST
  17247. L3588:  PUSH    AF              ; save A and carry
  17248.  
  17249.         RST     28H             ;; FP-CALC
  17250.         DB    $A0             ;;stk-zero      an initial false value.
  17251.         DB    $38             ;;end-calc
  17252.  
  17253. ; both numeric and string paths converge here.
  17254.  
  17255. ;; END-TESTS
  17256. L358C:  POP     AF              ; pop carry  - will be set if eql/neql
  17257.         PUSH    AF              ; save it again.
  17258.  
  17259.         CALL    C,L3501         ; routine NOT sets true(1) if equal(0)
  17260.                                 ; or, for strings, applies true result.
  17261.  
  17262.         POP     AF              ; pop carry and
  17263.         PUSH    AF              ; save A
  17264.  
  17265.         CALL    NC,L34F9        ; routine GREATER-0 tests numeric subtraction
  17266.                                 ; result but also needlessly tests the string
  17267.                                 ; value for zero - it must be.
  17268.  
  17269.         POP     AF              ; pop A
  17270.         RRCA                    ; the third RRCA - test for '<=', '>=' or '<>'.
  17271.         CALL    NC,L3501        ; apply a terminal NOT if so.
  17272.         RET                     ; return.
  17273.  
  17274. ; -------------------------
  17275. ; String concatenation ($17)
  17276. ; -------------------------
  17277. ; This literal combines two strings into one e.g. LET a$ = b$ + c$
  17278. ; The two parameters of the two strings to be combined are on the stack.
  17279.  
  17280. ;; strs-add
  17281. L359C:  CALL    L2BF1           ; routine STK-FETCH fetches string parameters
  17282.                                 ; and deletes calculator stack entry.
  17283.         PUSH    DE              ; save start address.
  17284.         PUSH    BC              ; and length.
  17285.  
  17286.         CALL    L2BF1           ; routine STK-FETCH for first string
  17287.         POP     HL              ; re-fetch first length
  17288.         PUSH    HL              ; and save again
  17289.         PUSH    DE              ; save start of second string
  17290.         PUSH    BC              ; and its length.
  17291.  
  17292.         ADD     HL,BC           ; add the two lengths.
  17293.         LD      B,H             ; transfer to BC
  17294.         LD      C,L             ; and create
  17295.         RST     30H             ; BC-SPACES in workspace.
  17296.                                 ; DE points to start of space.
  17297.  
  17298.         CALL    L2AB2           ; routine STK-STO-$ stores parameters
  17299.                                 ; of new string updating STKEND.
  17300.  
  17301.         POP     BC              ; length of first
  17302.         POP     HL              ; address of start
  17303.         LD      A,B             ; test for
  17304.         OR      C               ; zero length.
  17305.         JR      Z,L35B7         ; to OTHER-STR if null string
  17306.  
  17307.         LDIR                    ; copy string to workspace.
  17308.  
  17309. ;; OTHER-STR
  17310. L35B7:  POP     BC              ; now second length
  17311.         POP     HL              ; and start of string
  17312.         LD      A,B             ; test this one
  17313.         OR      C               ; for zero length
  17314.         JR      Z,L35BF         ; skip forward to STK-PNTRS if so as complete.
  17315.  
  17316.         LDIR                    ; else copy the bytes.
  17317.                                 ; and continue into next routine which
  17318.                                 ; sets the calculator stack pointers.
  17319.  
  17320. ; --------------------
  17321. ; Check stack pointers
  17322. ; --------------------
  17323. ; Register DE is set to STKEND and HL, the result pointer, is set to five
  17324. ; locations below this.
  17325. ; This routine is used when it is inconvenient to save these values at the
  17326. ; time the calculator stack is manipulated due to other activity on the
  17327. ; machine stack.
  17328. ; This routine is also used to terminate the VAL and READ-IN  routines for
  17329. ; the same reason and to initialize the calculator stack at the start of
  17330. ; the CALCULATE routine.
  17331.  
  17332. ;; STK-PNTRS
  17333. L35BF:  LD      HL,($5C65)      ; fetch STKEND value from system variable.
  17334.         LD      DE,$FFFB        ; the value -5
  17335.         PUSH    HL              ; push STKEND value.
  17336.  
  17337.         ADD     HL,DE           ; subtract 5 from HL.
  17338.  
  17339.         POP     DE              ; pop STKEND to DE.
  17340.         RET                     ; return.
  17341.  
  17342. ; ----------------
  17343. ; Handle CHR$ (2F)
  17344. ; ----------------
  17345. ; This function returns a single character string that is a result of
  17346. ; converting a number in the range 0-255 to a string e.g. CHR$ 65 = "A".
  17347.  
  17348. ;; chrs
  17349. L35C9:  CALL    L2DD5           ; routine FP-TO-A puts the number in A.
  17350.  
  17351.         JR      C,L35DC         ; forward to REPORT-Bd if overflow
  17352.         JR      NZ,L35DC        ; forward to REPORT-Bd if negative
  17353.  
  17354.         PUSH    AF              ; save the argument.
  17355.  
  17356.         LD      BC,$0001        ; one space required.
  17357.         RST     30H             ; BC-SPACES makes DE point to start
  17358.  
  17359.         POP     AF              ; restore the number.
  17360.  
  17361.         LD      (DE),A          ; and store in workspace
  17362.  
  17363.         CALL    L2AB2           ; routine STK-STO-$ stacks descriptor.
  17364.  
  17365.         EX      DE,HL           ; make HL point to result and DE to STKEND.
  17366.         RET                     ; return.
  17367.  
  17368. ; ---
  17369.  
  17370. ;; REPORT-Bd
  17371. L35DC:  RST     08H             ; ERROR-1
  17372.         DB    $0A             ; Error Report: Integer out of range
  17373.  
  17374. ; ----------------------------
  17375. ; Handle VAL and VAL$ ($1D, $18)
  17376. ; ----------------------------
  17377. ; VAL treats the characters in a string as a numeric expression.
  17378. ;     e.g. VAL "2.3" = 2.3, VAL "2+4" = 6, VAL ("2" + "4") = 24.
  17379. ; VAL$ treats the characters in a string as a string expression.
  17380. ;     e.g. VAL$ (z$+"(2)") = a$(2) if z$ happens to be "a$".
  17381.  
  17382. ;; val
  17383. ;; val$
  17384. L35DE:  LD      HL,($5C5D)      ; fetch value of system variable CH_ADD
  17385.         PUSH    HL              ; and save on the machine stack.
  17386.         LD      A,B             ; fetch the literal (either $1D or $18).
  17387.         ADD     A,$E3           ; add $E3 to form $00 (setting carry) or $FB.
  17388.         SBC     A,A             ; now form $FF bit 6 = numeric result
  17389.                                 ; or $00 bit 6 = string result.
  17390.         PUSH    AF              ; save this mask on the stack
  17391.  
  17392.         CALL    L2BF1           ; routine STK-FETCH fetches the string operand
  17393.                                 ; from calculator stack.
  17394.  
  17395.         PUSH    DE              ; save the address of the start of the string.
  17396.         INC     BC              ; increment the length for a carriage return.
  17397.  
  17398.         RST     30H             ; BC-SPACES creates the space in workspace.
  17399.         POP     HL              ; restore start of string to HL.
  17400.         LD      ($5C5D),DE      ; load CH_ADD with start DE in workspace.
  17401.  
  17402.         PUSH    DE              ; save the start in workspace
  17403.         LDIR                    ; copy string from program or variables or
  17404.                                 ; workspace to the workspace area.
  17405.         EX      DE,HL           ; end of string + 1 to HL
  17406.         DEC     HL              ; decrement HL to point to end of new area.
  17407.         LD      (HL),$0D        ; insert a carriage return at end.
  17408.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax.
  17409.         CALL    L24FB           ; routine SCANNING evaluates string
  17410.                                 ; expression and result.
  17411.  
  17412.         RST     18H             ; GET-CHAR fetches next character.
  17413.         CP      $0D             ; is it the expected carriage return ?
  17414.         JR      NZ,L360C        ; forward to V-RPORT-C if not
  17415.                                 ; 'Nonsense in BASIC'.
  17416.  
  17417.         POP     HL              ; restore start of string in workspace.
  17418.         POP     AF              ; restore expected result flag (bit 6).
  17419.         XOR     (IY+$01)        ; xor with FLAGS now updated by SCANNING.
  17420.         AND     $40             ; test bit 6 - should be zero if result types
  17421.                                 ; match.
  17422.  
  17423. ;; V-RPORT-C
  17424. L360C:  JP      NZ,L1C8A        ; jump back to REPORT-C with a result mismatch.
  17425.  
  17426.         LD      ($5C5D),HL      ; set CH_ADD to the start of the string again.
  17427.         SET     7,(IY+$01)      ; update FLAGS  - signal running program.
  17428.         CALL    L24FB           ; routine SCANNING evaluates the string
  17429.                                 ; in full leaving result on calculator stack.
  17430.  
  17431.         POP     HL              ; restore saved character address in program.
  17432.         LD      ($5C5D),HL      ; and reset the system variable CH_ADD.
  17433.  
  17434.         JR      L35BF           ; back to exit via STK-PNTRS.
  17435.                                 ; resetting the calculator stack pointers
  17436.                                 ; HL and DE from STKEND as it wasn't possible
  17437.                                 ; to preserve them during this routine.
  17438.  
  17439. ; ----------------
  17440. ; Handle STR$ (2E)
  17441. ; ----------------
  17442. ;
  17443. ;
  17444.  
  17445. ;; str$
  17446. L361F:  LD      BC,$0001        ; create an initial byte in workspace
  17447.         RST     30H             ; using BC-SPACES restart.
  17448.  
  17449.         LD      ($5C5B),HL      ; set system variable K_CUR to new location.
  17450.         PUSH    HL              ; and save start on machine stack also.
  17451.  
  17452.         LD      HL,($5C51)      ; fetch value of system variable CURCHL
  17453.         PUSH    HL              ; and save that too.
  17454.  
  17455.         LD      A,$FF           ; select system channel 'R'.
  17456.         CALL    L1601           ; routine CHAN-OPEN opens it.
  17457.         CALL    L2DE3           ; routine PRINT-FP outputs the number to
  17458.                                 ; workspace updating K-CUR.
  17459.  
  17460.         POP     HL              ; restore current channel.
  17461.         CALL    L1615           ; routine CHAN-FLAG resets flags.
  17462.  
  17463.         POP     DE              ; fetch saved start of string to DE.
  17464.         LD      HL,($5C5B)      ; load HL with end of string from K_CUR.
  17465.  
  17466.         AND     A               ; prepare for true subtraction.
  17467.         SBC     HL,DE           ; subtract start from end to give length.
  17468.         LD      B,H             ; transfer the length to
  17469.         LD      C,L             ; the BC register pair.
  17470.  
  17471.         CALL    L2AB2           ; routine STK-STO-$ stores string parameters
  17472.                                 ; on the calculator stack.
  17473.  
  17474.         EX      DE,HL           ; HL = last value, DE = STKEND.
  17475.         RET                     ; return.
  17476.  
  17477. ; ------------
  17478. ; Read-in (1A)
  17479. ; ------------
  17480. ; This is the calculator literal used by the INKEY$ function when a '#'
  17481. ; is encountered after the keyword.
  17482. ; INKEY$ # does not interact correctly with the keyboard, #0 or #1, and
  17483. ; its uses are for other channels.
  17484.  
  17485. ;; read-in
  17486. L3645:  CALL    L1E94           ; routine FIND-INT1 fetches stream to A
  17487.         CP      $10             ; compare with 16 decimal.
  17488.         JP      NC,L1E9F        ; jump to REPORT-Bb if not in range 0 - 15.
  17489.                                 ; 'Integer out of range'
  17490.                                 ; (REPORT-Bd is within range)
  17491.  
  17492.         LD      HL,($5C51)      ; fetch current channel CURCHL
  17493.         PUSH    HL              ; save it
  17494.         CALL    L1601           ; routine CHAN-OPEN opens channel
  17495.  
  17496.         CALL    L15E6           ; routine INPUT-AD - the channel must have an
  17497.                                 ; input stream or else error here from stream
  17498.                                 ; stub.
  17499.         LD      BC,$0000        ; initialize length of string to zero
  17500.         JR      NC,L365F        ; forward to R-I-STORE if no key detected.
  17501.  
  17502.         INC     C               ; increase length to one.
  17503.  
  17504.         RST     30H             ; BC-SPACES creates space for one character
  17505.                                 ; in workspace.
  17506.         LD      (DE),A          ; the character is inserted.
  17507.  
  17508. ;; R-I-STORE
  17509. L365F:  CALL    L2AB2           ; routine STK-STO-$ stacks the string
  17510.                                 ; parameters.
  17511.         POP     HL              ; restore current channel address
  17512.         CALL    L1615           ; routine CHAN-FLAG resets current channel
  17513.                                 ; system variable and flags.
  17514.         JP      L35BF           ; jump back to STK-PNTRS
  17515.  
  17516. ; ----------------
  17517. ; Handle CODE (1C)
  17518. ; ----------------
  17519. ; Returns the ASCII code of a character or first character of a string
  17520. ; e.g. CODE "Aardvark" = 65, CODE "" = 0.
  17521.  
  17522. ;; code
  17523. L3669:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17524.                                 ; string parameters.
  17525.                                 ; DE points to the start, BC holds the length.
  17526.         LD      A,B             ; test length
  17527.         OR      C               ; of the string.
  17528.         JR      Z,L3671         ; skip to STK-CODE with zero if the null string.
  17529.  
  17530.         LD      A,(DE)          ; else fetch the first character.
  17531.  
  17532. ;; STK-CODE
  17533. L3671:  JP      L2D28           ; jump back to STACK-A (with memory check)
  17534.  
  17535. ; ---------------
  17536. ; Handle LEN (1E)
  17537. ; ---------------
  17538. ; Returns the length of a string.
  17539. ; In Sinclair BASIC strings can be more than twenty thousand characters long
  17540. ; so a sixteen-bit register is required to store the length
  17541.  
  17542. ;; len
  17543. L3674:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17544.                                 ; string parameters from the calculator stack.
  17545.                                 ; register BC now holds the length of string.
  17546.  
  17547.         JP      L2D2B           ; jump back to STACK-BC to save result on the
  17548.                                 ; calculator stack (with memory check).
  17549.  
  17550. ; -------------------------
  17551. ; Decrease the counter (35)
  17552. ; -------------------------
  17553. ; The calculator has an instruction that decrements a single-byte
  17554. ; pseudo-register and makes consequential relative jumps just like
  17555. ; the Z80's DJNZ instruction.
  17556.  
  17557. ;; dec-jr-nz
  17558. L367A:  EXX                     ; switch in set that addresses code
  17559.  
  17560.         PUSH    HL              ; save pointer to offset byte
  17561.         LD      HL,$5C67        ; address BREG in system variables
  17562.         DEC     (HL)            ; decrement it
  17563.         POP     HL              ; restore pointer
  17564.  
  17565.         JR      NZ,L3687        ; to JUMP-2 if not zero
  17566.  
  17567.         INC     HL              ; step past the jump length.
  17568.         EXX                     ; switch in the main set.
  17569.         RET                     ; return.
  17570.  
  17571. ; Note. as a general rule the calculator avoids using the IY register
  17572. ; otherwise the cumbersome 4 instructions in the middle could be replaced by
  17573. ; dec (iy+$2d) - three bytes instead of six.
  17574.  
  17575.  
  17576. ; ---------
  17577. ; Jump (33)
  17578. ; ---------
  17579. ; This enables the calculator to perform relative jumps just like
  17580. ; the Z80 chip's JR instruction
  17581.  
  17582. ;; jump
  17583. ;; JUMP
  17584. L3686:  EXX                     ;switch in pointer set
  17585.  
  17586. ;; JUMP-2
  17587. L3687:  LD      E,(HL)          ; the jump byte 0-127 forward, 128-255 back.
  17588.         LD      A,E             ; transfer to accumulator.
  17589.         RLA                     ; if backward jump, carry is set.
  17590.         SBC     A,A             ; will be $FF if backward or $00 if forward.
  17591.         LD      D,A             ; transfer to high byte.
  17592.         ADD     HL,DE           ; advance calculator pointer forward or back.
  17593.         EXX                     ; switch back.
  17594.         RET                     ; return.
  17595.  
  17596. ; -----------------
  17597. ; Jump on true (00)
  17598. ; -----------------
  17599. ; This enables the calculator to perform conditional relative jumps
  17600. ; dependent on whether the last test gave a true result
  17601.  
  17602. ;; jump-true
  17603. L368F:  INC     DE              ; collect the
  17604.         INC     DE              ; third byte
  17605.         LD      A,(DE)          ; of the test
  17606.         DEC     DE              ; result and
  17607.         DEC     DE              ; backtrack.
  17608.  
  17609.         AND     A               ; is result 0 or 1 ?
  17610.         JR      NZ,L3686        ; back to JUMP if true (1).
  17611.  
  17612.         EXX                     ; else switch in the pointer set.
  17613.         INC     HL              ; step past the jump length.
  17614.         EXX                     ; switch in the main set.
  17615.         RET                     ; return.
  17616.  
  17617. ; -----------------------
  17618. ; End of calculation (38)
  17619. ; -----------------------
  17620. ; The end-calc literal terminates a mini-program written in the Spectrum's
  17621. ; internal language.
  17622.  
  17623. ;; end-calc
  17624. L369B:  POP     AF              ; drop the calculator return address RE-ENTRY
  17625.         EXX                     ; switch to the other set.
  17626.  
  17627.         EX      (SP),HL         ; transfer H'L' to machine stack for the
  17628.                                 ; return address.
  17629.                                 ; when exiting recursion then the previous
  17630.                                 ; pointer is transferred to H'L'.
  17631.  
  17632.         EXX                     ; back to main set.
  17633.         RET                     ; return.
  17634.  
  17635.  
  17636. ; ------------------------
  17637. ; THE 'MODULUS' SUBROUTINE
  17638. ; ------------------------
  17639. ; (offset: $32 'n-mod-m')
  17640. ;
  17641. ;
  17642.  
  17643. ;; n-mod-m
  17644. L36A0:  RST     28H             ;; FP-CALC          17, 3.
  17645.         DB    $C0             ;;st-mem-0          17, 3.
  17646.         DB    $02             ;;delete            17.
  17647.         DB    $31             ;;duplicate         17, 17.
  17648.         DB    $E0             ;;get-mem-0         17, 17, 3.
  17649.         DB    $05             ;;division          17, 17/3.
  17650.         DB    $27             ;;int               17, 5.
  17651.         DB    $E0             ;;get-mem-0         17, 5, 3.
  17652.         DB    $01             ;;exchange          17, 3, 5.
  17653.         DB    $C0             ;;st-mem-0          17, 3, 5.
  17654.         DB    $04             ;;multiply          17, 15.
  17655.         DB    $03             ;;subtract          2.
  17656.         DB    $E0             ;;get-mem-0         2, 5.
  17657.         DB    $38             ;;end-calc          2, 5.
  17658.  
  17659.         RET                     ; return.
  17660.  
  17661.  
  17662. ; ------------------
  17663. ; THE 'INT' FUNCTION
  17664. ; ------------------
  17665. ; (offset $27: 'int' )
  17666. ;
  17667. ; This function returns the integer of x, which is just the same as truncate
  17668. ; for positive numbers. The truncate literal truncates negative numbers
  17669. ; upwards so that -3.4 gives -3 whereas the BASIC INT function has to
  17670. ; truncate negative numbers down so that INT -3.4 is -4.
  17671. ; It is best to work through using, say, +-3.4 as examples.
  17672.  
  17673. ;; int
  17674. L36AF:  RST     28H             ;; FP-CALC              x.    (= 3.4 or -3.4).
  17675.         DB    $31             ;;duplicate             x, x.
  17676.         DB    $36             ;;less-0                x, (1/0)
  17677.         DB    $00             ;;jump-true             x, (1/0)
  17678.         DB    $04             ;;to L36B7, X-NEG
  17679.  
  17680.         DB    $3A             ;;truncate              trunc 3.4 = 3.
  17681.         DB    $38             ;;end-calc              3.
  17682.  
  17683.         RET                     ; return with + int x on stack.
  17684.  
  17685. ; ---
  17686.  
  17687.  
  17688. ;; X-NEG
  17689. L36B7:  DB    $31             ;;duplicate             -3.4, -3.4.
  17690.         DB    $3A             ;;truncate              -3.4, -3.
  17691.         DB    $C0             ;;st-mem-0              -3.4, -3.
  17692.         DB    $03             ;;subtract              -.4
  17693.         DB    $E0             ;;get-mem-0             -.4, -3.
  17694.         DB    $01             ;;exchange              -3, -.4.
  17695.         DB    $30             ;;not                   -3, (0).
  17696.         DB    $00             ;;jump-true             -3.
  17697.         DB    $03             ;;to L36C2, EXIT        -3.
  17698.  
  17699.         DB    $A1             ;;stk-one               -3, 1.
  17700.         DB    $03             ;;subtract              -4.
  17701.  
  17702. ;; EXIT
  17703. L36C2:  DB    $38             ;;end-calc              -4.
  17704.  
  17705.         RET                     ; return.
  17706.  
  17707.  
  17708. ; ----------------
  17709. ; Exponential (26)
  17710. ; ----------------
  17711. ;
  17712. ;
  17713.  
  17714. ;; EXP
  17715. ;; exp
  17716. L36C4:  RST     28H             ;; FP-CALC
  17717.         DB    $3D             ;;re-stack
  17718.         DB    $34             ;;stk-data
  17719.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17720.         DB    $38,$AA,$3B,$29 ;;
  17721.         DB    $04             ;;multiply
  17722.         DB    $31             ;;duplicate
  17723.         DB    $27             ;;int
  17724.         DB    $C3             ;;st-mem-3
  17725.         DB    $03             ;;subtract
  17726.         DB    $31             ;;duplicate
  17727.         DB    $0F             ;;addition
  17728.         DB    $A1             ;;stk-one
  17729.         DB    $03             ;;subtract
  17730.         DB    $88             ;;series-08
  17731.         DB    $13             ;;Exponent: $63, Bytes: 1
  17732.         DB    $36             ;;(+00,+00,+00)
  17733.         DB    $58             ;;Exponent: $68, Bytes: 2
  17734.         DB    $65,$66         ;;(+00,+00)
  17735.         DB    $9D             ;;Exponent: $6D, Bytes: 3
  17736.         DB    $78,$65,$40     ;;(+00)
  17737.         DB    $A2             ;;Exponent: $72, Bytes: 3
  17738.         DB    $60,$32,$C9     ;;(+00)
  17739.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17740.         DB    $21,$F7,$AF,$24 ;;
  17741.         DB    $EB             ;;Exponent: $7B, Bytes: 4
  17742.         DB    $2F,$B0,$B0,$14 ;;
  17743.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  17744.         DB    $7E,$BB,$94,$58 ;;
  17745.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17746.         DB    $3A,$7E,$F8,$CF ;;
  17747.         DB    $E3             ;;get-mem-3
  17748.         DB    $38             ;;end-calc
  17749.  
  17750.         CALL    L2DD5           ; routine FP-TO-A
  17751.         JR      NZ,L3705        ; to N-NEGTV
  17752.  
  17753.         JR      C,L3703         ; to REPORT-6b
  17754.  
  17755.         ADD     A,(HL)          ;
  17756.         JR      NC,L370C        ; to RESULT-OK
  17757.  
  17758.  
  17759. ;; REPORT-6b
  17760. L3703:  RST     08H             ; ERROR-1
  17761.         DB    $05             ; Error Report: Number too big
  17762.  
  17763. ;; N-NEGTV
  17764. L3705:  JR      C,L370E         ; to RSLT-ZERO
  17765.  
  17766.         SUB     (HL)            ;
  17767.         JR      NC,L370E        ; to RSLT-ZERO
  17768.  
  17769.         NEG                     ; Negate
  17770.  
  17771. ;; RESULT-OK
  17772. L370C:  LD      (HL),A          ;
  17773.         RET                     ; return.
  17774.  
  17775. ; ---
  17776.  
  17777.  
  17778. ;; RSLT-ZERO
  17779. L370E:  RST     28H             ;; FP-CALC
  17780.         DB    $02             ;;delete
  17781.         DB    $A0             ;;stk-zero
  17782.         DB    $38             ;;end-calc
  17783.  
  17784.         RET                     ; return.
  17785.  
  17786.  
  17787. ; ----------------------
  17788. ; Natural logarithm (25)
  17789. ; ----------------------
  17790. ;
  17791. ;
  17792.  
  17793. ;; ln
  17794. L3713:  RST     28H             ;; FP-CALC
  17795.         DB    $3D             ;;re-stack
  17796.         DB    $31             ;;duplicate
  17797.         DB    $37             ;;greater-0
  17798.         DB    $00             ;;jump-true
  17799.         DB    $04             ;;to L371C, VALID
  17800.  
  17801.         DB    $38             ;;end-calc
  17802.  
  17803.  
  17804. ;; REPORT-Ab
  17805. L371A:  RST     08H             ; ERROR-1
  17806.         DB    $09             ; Error Report: Invalid argument
  17807.  
  17808. ;; VALID
  17809. L371C:  DB    $A0             ;;stk-zero
  17810.         DB    $02             ;;delete
  17811.         DB    $38             ;;end-calc
  17812.         LD      A,(HL)          ;
  17813.  
  17814.         LD      (HL),$80        ;
  17815.         CALL    L2D28           ; routine STACK-A
  17816.  
  17817.         RST     28H             ;; FP-CALC
  17818.         DB    $34             ;;stk-data
  17819.         DB    $38             ;;Exponent: $88, Bytes: 1
  17820.         DB    $00             ;;(+00,+00,+00)
  17821.         DB    $03             ;;subtract
  17822.         DB    $01             ;;exchange
  17823.         DB    $31             ;;duplicate
  17824.         DB    $34             ;;stk-data
  17825.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17826.         DB    $4C,$CC,$CC,$CD ;;
  17827.         DB    $03             ;;subtract
  17828.         DB    $37             ;;greater-0
  17829.         DB    $00             ;;jump-true
  17830.         DB    $08             ;;to L373D, GRE.8
  17831.  
  17832.         DB    $01             ;;exchange
  17833.         DB    $A1             ;;stk-one
  17834.         DB    $03             ;;subtract
  17835.         DB    $01             ;;exchange
  17836.         DB    $38             ;;end-calc
  17837.  
  17838.         INC     (HL)            ;
  17839.  
  17840.         RST     28H             ;; FP-CALC
  17841.  
  17842. ;; GRE.8
  17843. L373D:  DB    $01             ;;exchange
  17844.         DB    $34             ;;stk-data
  17845.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17846.         DB    $31,$72,$17,$F8 ;;
  17847.         DB    $04             ;;multiply
  17848.         DB    $01             ;;exchange
  17849.         DB    $A2             ;;stk-half
  17850.         DB    $03             ;;subtract
  17851.         DB    $A2             ;;stk-half
  17852.         DB    $03             ;;subtract
  17853.         DB    $31             ;;duplicate
  17854.         DB    $34             ;;stk-data
  17855.         DB    $32             ;;Exponent: $82, Bytes: 1
  17856.         DB    $20             ;;(+00,+00,+00)
  17857.         DB    $04             ;;multiply
  17858.         DB    $A2             ;;stk-half
  17859.         DB    $03             ;;subtract
  17860.         DB    $8C             ;;series-0C
  17861.         DB    $11             ;;Exponent: $61, Bytes: 1
  17862.         DB    $AC             ;;(+00,+00,+00)
  17863.         DB    $14             ;;Exponent: $64, Bytes: 1
  17864.         DB    $09             ;;(+00,+00,+00)
  17865.         DB    $56             ;;Exponent: $66, Bytes: 2
  17866.         DB    $DA,$A5         ;;(+00,+00)
  17867.         DB    $59             ;;Exponent: $69, Bytes: 2
  17868.         DB    $30,$C5         ;;(+00,+00)
  17869.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  17870.         DB    $90,$AA         ;;(+00,+00)
  17871.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  17872.         DB    $70,$6F,$61     ;;(+00)
  17873.         DB    $A1             ;;Exponent: $71, Bytes: 3
  17874.         DB    $CB,$DA,$96     ;;(+00)
  17875.         DB    $A4             ;;Exponent: $74, Bytes: 3
  17876.         DB    $31,$9F,$B4     ;;(+00)
  17877.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17878.         DB    $A0,$FE,$5C,$FC ;;
  17879.         DB    $EA             ;;Exponent: $7A, Bytes: 4
  17880.         DB    $1B,$43,$CA,$36 ;;
  17881.         DB    $ED             ;;Exponent: $7D, Bytes: 4
  17882.         DB    $A7,$9C,$7E,$5E ;;
  17883.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17884.         DB    $6E,$23,$80,$93 ;;
  17885.         DB    $04             ;;multiply
  17886.         DB    $0F             ;;addition
  17887.         DB    $38             ;;end-calc
  17888.  
  17889.         RET                     ; return.
  17890.  
  17891.  
  17892. ; -----------------------------
  17893. ; THE 'TRIGONOMETRIC' FUNCTIONS
  17894. ; -----------------------------
  17895. ; Trigonometry is rocket science. It is also used by carpenters and pyramid
  17896. ; builders.
  17897. ; Some uses can be quite abstract but the principles can be seen in simple
  17898. ; right-angled triangles. Triangles have some special properties -
  17899. ;
  17900. ; 1) The sum of the three angles is always PI radians (180 degrees).
  17901. ;    Very helpful if you know two angles and wish to find the third.
  17902. ; 2) In any right-angled triangle the sum of the squares of the two shorter
  17903. ;    sides is equal to the square of the longest side opposite the right-angle.
  17904. ;    Very useful if you know the length of two sides and wish to know the
  17905. ;    length of the third side.
  17906. ; 3) Functions sine, cosine and tangent enable one to calculate the length
  17907. ;    of an unknown side when the length of one other side and an angle is
  17908. ;    known.
  17909. ; 4) Functions arcsin, arccosine and arctan enable one to calculate an unknown
  17910. ;    angle when the length of two of the sides is known.
  17911.  
  17912. ;---------------------------------
  17913. ; THE 'REDUCE ARGUMENT' SUBROUTINE
  17914. ;---------------------------------
  17915. ; (offset $39: 'get-argt')
  17916. ;
  17917. ; This routine performs two functions on the angle, in radians, that forms
  17918. ; the argument to the sine and cosine functions.
  17919. ; First it ensures that the angle 'wraps round'. That if a ship turns through
  17920. ; an angle of, say, 3*PI radians (540 degrees) then the net effect is to turn
  17921. ; through an angle of PI radians (180 degrees).
  17922. ; Secondly it converts the angle in radians to a fraction of a right angle,
  17923. ; depending within which quadrant the angle lies, with the periodicity
  17924. ; resembling that of the desired sine value.
  17925. ; The result lies in the range -1 to +1.              
  17926. ;
  17927. ;                     90 deg.
  17928. ;
  17929. ;                     (pi/2)
  17930. ;              II       +1        I
  17931. ;                       |
  17932. ;        sin+      |\   |   /|    sin+
  17933. ;        cos-      | \  |  / |    cos+
  17934. ;        tan-      |  \ | /  |    tan+
  17935. ;                  |   \|/)  |          
  17936. ; 180 deg. (pi) 0 -|----+----|-- 0  (0)   0 degrees
  17937. ;                  |   /|\   |
  17938. ;        sin-      |  / | \  |    sin-
  17939. ;        cos-      | /  |  \ |    cos+
  17940. ;        tan+      |/   |   \|    tan-
  17941. ;                       |
  17942. ;              III      -1       IV
  17943. ;                     (3pi/2)
  17944. ;
  17945. ;                     270 deg.
  17946. ;
  17947.  
  17948. ;; get-argt
  17949. L3783:  RST     28H             ;; FP-CALC      X.
  17950.         DB    $3D             ;;re-stack
  17951.         DB    $34             ;;stk-data
  17952.         DB    $EE             ;;Exponent: $7E,
  17953.                                 ;;Bytes: 4
  17954.         DB    $22,$F9,$83,$6E ;;              X, 1/(2*PI)
  17955.         DB    $04             ;;multiply      X/(2*PI) = fraction
  17956.         DB    $31             ;;duplicate
  17957.         DB    $A2             ;;stk-half
  17958.         DB    $0F             ;;addition
  17959.         DB    $27             ;;int
  17960.  
  17961.         DB    $03             ;;subtract      now range -.5 to .5
  17962.  
  17963.         DB    $31             ;;duplicate
  17964.         DB    $0F             ;;addition      now range -1 to 1.
  17965.         DB    $31             ;;duplicate
  17966.         DB    $0F             ;;addition      now range -2 to +2.
  17967.  
  17968. ; quadrant I (0 to +1) and quadrant IV (-1 to 0) are now correct.
  17969. ; quadrant II ranges +1 to +2.
  17970. ; quadrant III ranges -2 to -1.
  17971.  
  17972.         DB    $31             ;;duplicate     Y, Y.
  17973.         DB    $2A             ;;abs           Y, abs(Y).    range 1 to 2
  17974.         DB    $A1             ;;stk-one       Y, abs(Y), 1.
  17975.         DB    $03             ;;subtract      Y, abs(Y)-1.  range 0 to 1
  17976.         DB    $31             ;;duplicate     Y, Z, Z.
  17977.         DB    $37             ;;greater-0     Y, Z, (1/0).
  17978.  
  17979.         DB    $C0             ;;st-mem-0         store as possible sign
  17980.                                 ;;                 for cosine function.
  17981.  
  17982.         DB    $00             ;;jump-true
  17983.         DB    $04             ;;to L37A1, ZPLUS  with quadrants II and III.
  17984.  
  17985. ; else the angle lies in quadrant I or IV and value Y is already correct.
  17986.  
  17987.         DB    $02             ;;delete        Y.   delete the test value.
  17988.         DB    $38             ;;end-calc      Y.
  17989.  
  17990.         RET                     ; return.       with Q1 and Q4           >>>
  17991.  
  17992. ; ---
  17993.  
  17994. ; the branch was here with quadrants II (0 to 1) and III (1 to 0).
  17995. ; Y will hold -2 to -1 if this is quadrant III.
  17996.  
  17997. ;; ZPLUS
  17998. L37A1:  DB    $A1             ;;stk-one         Y, Z, 1.
  17999.         DB    $03             ;;subtract        Y, Z-1.       Q3 = 0 to -1
  18000.         DB    $01             ;;exchange        Z-1, Y.
  18001.         DB    $36             ;;less-0          Z-1, (1/0).
  18002.         DB    $00             ;;jump-true       Z-1.
  18003.         DB    $02             ;;to L37A8, YNEG
  18004.                                 ;;if angle in quadrant III
  18005.  
  18006. ; else angle is within quadrant II (-1 to 0)
  18007.  
  18008.         DB    $1B             ;;negate          range +1 to 0.
  18009.  
  18010. ;; YNEG
  18011. L37A8:  DB    $38             ;;end-calc        quadrants II and III correct.
  18012.  
  18013.         RET                     ; return.
  18014.  
  18015.  
  18016. ;----------------------
  18017. ; THE 'COSINE' FUNCTION
  18018. ;----------------------
  18019. ; (offset $20: 'cos')
  18020. ; Cosines are calculated as the sine of the opposite angle rectifying the
  18021. ; sign depending on the quadrant rules.
  18022. ;
  18023. ;
  18024. ;           /|
  18025. ;        h /y|
  18026. ;         /  |o
  18027. ;        /x  |
  18028. ;       /----|    
  18029. ;         a
  18030. ;
  18031. ; The cosine of angle x is the adjacent side (a) divided by the hypotenuse 1.
  18032. ; However if we examine angle y then a/h is the sine of that angle.
  18033. ; Since angle x plus angle y equals a right-angle, we can find angle y by
  18034. ; subtracting angle x from pi/2.
  18035. ; However it's just as easy to reduce the argument first and subtract the
  18036. ; reduced argument from the value 1 (a reduced right-angle).
  18037. ; It's even easier to subtract 1 from the angle and rectify the sign.
  18038. ; In fact, after reducing the argument, the absolute value of the argument
  18039. ; is used and rectified using the test result stored in mem-0 by 'get-argt'
  18040. ; for that purpose.
  18041. ;
  18042.  
  18043. ;; cos
  18044. L37AA:  RST     28H             ;; FP-CALC              angle in radians.
  18045.         DB    $39             ;;get-argt              X     reduce -1 to +1
  18046.  
  18047.         DB    $2A             ;;abs                   ABS X.   0 to 1
  18048.         DB    $A1             ;;stk-one               ABS X, 1.
  18049.         DB    $03             ;;subtract              now opposite angle
  18050.                                 ;;                      although sign is -ve.
  18051.  
  18052.         DB    $E0             ;;get-mem-0             fetch the sign indicator
  18053.         DB    $00             ;;jump-true
  18054.         DB    $06             ;;fwd to L37B7, C-ENT
  18055.                                 ;;forward to common code if in QII or QIII.
  18056.  
  18057.         DB    $1B             ;;negate                else make sign +ve.
  18058.         DB    $33             ;;jump
  18059.         DB    $03             ;;fwd to L37B7, C-ENT
  18060.                                 ;; with quadrants I and IV.
  18061.  
  18062. ;--------------------
  18063. ; THE 'SINE' FUNCTION
  18064. ;--------------------
  18065. ; (offset $1F: 'sin')
  18066. ; This is a fundamental transcendental function from which others such as cos
  18067. ; and tan are directly, or indirectly, derived.
  18068. ; It uses the series generator to produce Chebyshev polynomials.
  18069. ;
  18070. ;
  18071. ;           /|
  18072. ;        1 / |
  18073. ;         /  |x
  18074. ;        /a  |
  18075. ;       /----|    
  18076. ;         y
  18077. ;
  18078. ; The 'get-argt' function is designed to modify the angle and its sign
  18079. ; in line with the desired sine value and afterwards it can launch straight
  18080. ; into common code.
  18081.  
  18082. ;; sin
  18083. L37B5:  RST     28H             ;; FP-CALC      angle in radians
  18084.         DB    $39             ;;get-argt      reduce - sign now correct.
  18085.  
  18086. ;; C-ENT
  18087. L37B7:  DB    $31             ;;duplicate
  18088.         DB    $31             ;;duplicate
  18089.         DB    $04             ;;multiply
  18090.         DB    $31             ;;duplicate
  18091.         DB    $0F             ;;addition
  18092.         DB    $A1             ;;stk-one
  18093.         DB    $03             ;;subtract
  18094.  
  18095.         DB    $86             ;;series-06
  18096.         DB    $14             ;;Exponent: $64, Bytes: 1
  18097.         DB    $E6             ;;(+00,+00,+00)
  18098.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  18099.         DB    $1F,$0B         ;;(+00,+00)
  18100.         DB    $A3             ;;Exponent: $73, Bytes: 3
  18101.         DB    $8F,$38,$EE     ;;(+00)
  18102.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18103.         DB    $15,$63,$BB,$23 ;;
  18104.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  18105.         DB    $92,$0D,$CD,$ED ;;
  18106.         DB    $F1             ;;Exponent: $81, Bytes: 4
  18107.         DB    $23,$5D,$1B,$EA ;;
  18108.         DB    $04             ;;multiply
  18109.         DB    $38             ;;end-calc
  18110.  
  18111.         RET                     ; return.
  18112.  
  18113. ;-----------------------
  18114. ; THE 'TANGENT' FUNCTION
  18115. ;-----------------------
  18116. ; (offset $21: 'tan')
  18117. ;
  18118. ; Evaluates tangent x as    sin(x) / cos(x).
  18119. ;
  18120. ;
  18121. ;           /|
  18122. ;        h / |
  18123. ;         /  |o
  18124. ;        /x  |
  18125. ;       /----|    
  18126. ;         a
  18127. ;
  18128. ; the tangent of angle x is the ratio of the length of the opposite side
  18129. ; divided by the length of the adjacent side. As the opposite length can
  18130. ; be calculates using sin(x) and the adjacent length using cos(x) then
  18131. ; the tangent can be defined in terms of the previous two functions.
  18132.  
  18133. ; Error 6 if the argument, in radians, is too close to one like pi/2
  18134. ; which has an infinite tangent. e.g. PRINT TAN (PI/2)  evaluates as 1/0.
  18135. ; Similarly PRINT TAN (3*PI/2), TAN (5*PI/2) etc.
  18136.  
  18137. ;; tan
  18138. L37DA:  RST     28H             ;; FP-CALC          x.
  18139.         DB    $31             ;;duplicate         x, x.
  18140.         DB    $1F             ;;sin               x, sin x.
  18141.         DB    $01             ;;exchange          sin x, x.
  18142.         DB    $20             ;;cos               sin x, cos x.
  18143.         DB    $05             ;;division          sin x/cos x (= tan x).
  18144.         DB    $38             ;;end-calc          tan x.
  18145.  
  18146.         RET                     ; return.
  18147.  
  18148. ;----------------------
  18149. ; THE 'ARCTAN' FUNCTION
  18150. ;----------------------
  18151. ; (Offset $24: 'atn')
  18152. ; the inverse tangent function with the result in radians.
  18153. ; This is a fundamental transcendental function from which others such as asn
  18154. ; and acs are directly, or indirectly, derived.
  18155. ; It uses the series generator to produce Chebyshev polynomials.
  18156.  
  18157. ;; atn
  18158. L37E2:  CALL    L3297           ; routine re-stack
  18159.         LD      A,(HL)          ; fetch exponent byte.
  18160.         CP      $81             ; compare to that for 'one'
  18161.         JR      C,L37F8         ; forward, if less, to SMALL
  18162.  
  18163.         RST     28H             ;; FP-CALC
  18164.         DB    $A1             ;;stk-one
  18165.         DB    $1B             ;;negate
  18166.         DB    $01             ;;exchange
  18167.         DB    $05             ;;division
  18168.         DB    $31             ;;duplicate
  18169.         DB    $36             ;;less-0
  18170.         DB    $A3             ;;stk-pi/2
  18171.         DB    $01             ;;exchange
  18172.         DB    $00             ;;jump-true
  18173.         DB    $06             ;;to L37FA, CASES
  18174.  
  18175.         DB    $1B             ;;negate
  18176.         DB    $33             ;;jump
  18177.         DB    $03             ;;to L37FA, CASES
  18178.  
  18179. ;; SMALL
  18180. L37F8:  RST     28H             ;; FP-CALC
  18181.         DB    $A0             ;;stk-zero
  18182.  
  18183. ;; CASES
  18184. L37FA:  DB    $01             ;;exchange
  18185.         DB    $31             ;;duplicate
  18186.         DB    $31             ;;duplicate
  18187.         DB    $04             ;;multiply
  18188.         DB    $31             ;;duplicate
  18189.         DB    $0F             ;;addition
  18190.         DB    $A1             ;;stk-one
  18191.         DB    $03             ;;subtract
  18192.         DB    $8C             ;;series-0C
  18193.         DB    $10             ;;Exponent: $60, Bytes: 1
  18194.         DB    $B2             ;;(+00,+00,+00)
  18195.         DB    $13             ;;Exponent: $63, Bytes: 1
  18196.         DB    $0E             ;;(+00,+00,+00)
  18197.         DB    $55             ;;Exponent: $65, Bytes: 2
  18198.         DB    $E4,$8D         ;;(+00,+00)
  18199.         DB    $58             ;;Exponent: $68, Bytes: 2
  18200.         DB    $39,$BC         ;;(+00,+00)
  18201.         DB    $5B             ;;Exponent: $6B, Bytes: 2
  18202.         DB    $98,$FD         ;;(+00,+00)
  18203.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  18204.         DB    $00,$36,$75     ;;(+00)
  18205.         DB    $A0             ;;Exponent: $70, Bytes: 3
  18206.         DB    $DB,$E8,$B4     ;;(+00)
  18207.         DB    $63             ;;Exponent: $73, Bytes: 2
  18208.         DB    $42,$C4         ;;(+00,+00)
  18209.         DB    $E6             ;;Exponent: $76, Bytes: 4
  18210.         DB    $B5,$09,$36,$BE ;;
  18211.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18212.         DB    $36,$73,$1B,$5D ;;
  18213.         DB    $EC             ;;Exponent: $7C, Bytes: 4
  18214.         DB    $D8,$DE,$63,$BE ;;
  18215.         DB    $F0             ;;Exponent: $80, Bytes: 4
  18216.         DB    $61,$A1,$B3,$0C ;;
  18217.         DB    $04             ;;multiply
  18218.         DB    $0F             ;;addition
  18219.         DB    $38             ;;end-calc
  18220.  
  18221.         RET                     ; return.
  18222.  
  18223.  
  18224. ;----------------------
  18225. ; THE 'ARCSIN' FUNCTION
  18226. ;----------------------
  18227. ; (Offset $22: 'asn')
  18228. ; the inverse sine function with result in radians.
  18229. ; derived from arctan function above.
  18230. ; Error A unless the argument is between -1 and +1 inclusive.
  18231. ; uses an adaptation of the formula asn(x) = atn(x/sqr(1-x*x))
  18232. ;
  18233. ;
  18234. ;           /|
  18235. ;        1 / |
  18236. ;         /  |x
  18237. ;        /a  |
  18238. ;       /----|    
  18239. ;         y
  18240. ;
  18241. ; e.g. we know the opposite side (x) and hypotenuse (1)
  18242. ; and we wish to find angle a in radians.
  18243. ; we can derive length y by Pythagorus and then use ATN instead.
  18244. ; since y*y + x*x = 1*1 (Pythagorus Theorem) then
  18245. ; y=sqr(1-x*x)                         - no need to multiply 1 by itself.
  18246. ; so, asn(a) = atn(x/y)
  18247. ; or more fully,
  18248. ; asn(a) = atn(x/sqr(1-x*x))
  18249.  
  18250. ; Close but no cigar.
  18251.  
  18252. ; While PRINT ATN (x/SQR (1-x*x)) gives the same results as PRINT ASN x,
  18253. ; it leads to division by zero when x is 1 or -1.
  18254. ; To overcome this, 1 is added to y giving half the required angle and the
  18255. ; result is then doubled.
  18256. ; That is PRINT ATN (x/(SQR (1-x*x) +1)) *2
  18257. ; A value higher than 1 gives the required error as attempting to find  the
  18258. ; square root of a negative number generates an error in Sinclair BASIC.
  18259.  
  18260. ;; asn
  18261. L3833:  RST     28H             ;; FP-CALC      x.
  18262.         DB    $31             ;;duplicate     x, x.
  18263.         DB    $31             ;;duplicate     x, x, x.
  18264.         DB    $04             ;;multiply      x, x*x.
  18265.         DB    $A1             ;;stk-one       x, x*x, 1.
  18266.         DB    $03             ;;subtract      x, x*x-1.
  18267.         DB    $1B             ;;negate        x, 1-x*x.
  18268.         DB    $28             ;;sqr           x, sqr(1-x*x) = y
  18269.         DB    $A1             ;;stk-one       x, y, 1.
  18270.         DB    $0F             ;;addition      x, y+1.
  18271.         DB    $05             ;;division      x/y+1.
  18272.         DB    $24             ;;atn           a/2       (half the angle)
  18273.         DB    $31             ;;duplicate     a/2, a/2.
  18274.         DB    $0F             ;;addition      a.
  18275.         DB    $38             ;;end-calc      a.
  18276.  
  18277.         RET                     ; return.
  18278.  
  18279.  
  18280. ;-------------------------
  18281. ; THE 'ARCCOS' FUNCTION
  18282. ;-------------------------
  18283. ; (Offset $23: 'acs')
  18284. ; the inverse cosine function with the result in radians.
  18285. ; Error A unless the argument is between -1 and +1.
  18286. ; Result in range 0 to pi.
  18287. ; Derived from asn above which is in turn derived from the preceding atn.
  18288. ; It could have been derived directly from atn using acs(x) = atn(sqr(1-x*x)/x).
  18289. ; However, as sine and cosine are horizontal translations of each other,
  18290. ; uses acs(x) = pi/2 - asn(x)
  18291.  
  18292. ; e.g. the arccosine of a known x value will give the required angle b in
  18293. ; radians.
  18294. ; We know, from above, how to calculate the angle a using asn(x).
  18295. ; Since the three angles of any triangle add up to 180 degrees, or pi radians,
  18296. ; and the largest angle in this case is a right-angle (pi/2 radians), then
  18297. ; we can calculate angle b as pi/2 (both angles) minus asn(x) (angle a).
  18298. ;
  18299. ;
  18300. ;           /|
  18301. ;        1 /b|
  18302. ;         /  |x
  18303. ;        /a  |
  18304. ;       /----|    
  18305. ;         y
  18306. ;
  18307.  
  18308. ;; acs
  18309. L3843:  RST     28H             ;; FP-CALC      x.
  18310.         DB    $22             ;;asn           asn(x).
  18311.         DB    $A3             ;;stk-pi/2      asn(x), pi/2.
  18312.         DB    $03             ;;subtract      asn(x) - pi/2.
  18313.         DB    $1B             ;;negate        pi/2 -asn(x)  =  acs(x).
  18314.         DB    $38             ;;end-calc      acs(x).
  18315.  
  18316.         RET                     ; return.
  18317.  
  18318.  
  18319. ; --------------------------
  18320. ; THE 'SQUARE ROOT' FUNCTION
  18321. ; --------------------------
  18322. ; (Offset $28: 'sqr')
  18323. ; This routine is remarkable only in its brevity - 7 bytes.
  18324. ; It wasn't written here but in the ZX81 where the programmers had to squeeze
  18325. ; a bulky operating sytem into an 8K ROM. It simply calculates
  18326. ; the square root by stacking the value .5 and continuing into the 'to-power'
  18327. ; routine. With more space available the much faster Newton-Raphson method
  18328. ; should have been used as on the Jupiter Ace.
  18329.  
  18330. ;; sqr
  18331. L384A:  RST     28H             ;; FP-CALC
  18332.         DB    $31             ;;duplicate
  18333.         DB    $30             ;;not
  18334.         DB    $00             ;;jump-true
  18335.         DB    $1E             ;;to L386C, LAST
  18336.  
  18337.         DB    $A2             ;;stk-half
  18338.         DB    $38             ;;end-calc
  18339.  
  18340.  
  18341. ; ------------------------------
  18342. ; THE 'EXPONENTIATION' OPERATION
  18343. ; ------------------------------
  18344. ; (Offset $06: 'to-power')
  18345. ; This raises the first number X to the power of the second number Y.
  18346. ; As with the ZX80,
  18347. ; 0 ^ 0 = 1.
  18348. ; 0 ^ +n = 0.
  18349. ; 0 ^ -n = arithmetic overflow.
  18350. ;
  18351.  
  18352. ;; to-power
  18353. L3851:  RST     28H             ;; FP-CALC              X, Y.
  18354.         DB    $01             ;;exchange              Y, X.
  18355.         DB    $31             ;;duplicate             Y, X, X.
  18356.         DB    $30             ;;not                   Y, X, (1/0).
  18357.         DB    $00             ;;jump-true
  18358.         DB    $07             ;;to L385D, XISO   if X is zero.
  18359.  
  18360. ; else X is non-zero. Function 'ln' will catch a negative value of X.
  18361.  
  18362.         DB    $25             ;;ln                    Y, LN X.
  18363.         DB    $04             ;;multiply              Y * LN X.
  18364.         DB    $38             ;;end-calc
  18365.  
  18366.         JP      L36C4           ; jump back to EXP routine   ->
  18367.  
  18368. ; ---
  18369.  
  18370. ; these routines form the three simple results when the number is zero.
  18371. ; begin by deleting the known zero to leave Y the power factor.
  18372.  
  18373. ;; XISO
  18374. L385D:  DB    $02             ;;delete                Y.
  18375.         DB    $31             ;;duplicate             Y, Y.
  18376.         DB    $30             ;;not                   Y, (1/0).
  18377.         DB    $00             ;;jump-true
  18378.         DB    $09             ;;to L386A, ONE         if Y is zero.
  18379.  
  18380.         DB    $A0             ;;stk-zero              Y, 0.
  18381.         DB    $01             ;;exchange              0, Y.
  18382.         DB    $37             ;;greater-0             0, (1/0).
  18383.         DB    $00             ;;jump-true             0.
  18384.         DB    $06             ;;to L386C, LAST        if Y was any positive
  18385.                                 ;;                      number.
  18386.  
  18387. ; else force division by zero thereby raising an Arithmetic overflow error.
  18388. ; There are some one and two-byte alternatives but perhaps the most formal
  18389. ; might have been to use end-calc; rst 08; DB 05.
  18390.  
  18391.         DB    $A1             ;;stk-one               0, 1.
  18392.         DB    $01             ;;exchange              1, 0.
  18393.         DB    $05             ;;division              1/0        ouch!
  18394.  
  18395. ; ---
  18396.  
  18397. ;; ONE
  18398. L386A:  DB    $02             ;;delete                .
  18399.         DB    $A1             ;;stk-one               1.
  18400.  
  18401. ;; LAST
  18402. L386C:  DB    $38             ;;end-calc              last value is 1 or 0.
  18403.  
  18404.         RET                     ; return.               Whew!
  18405.  
  18406. ;*********************************
  18407. ;** Spectrum 128 Patch Routines **
  18408. ;*********************************
  18409.  
  18410. ; The new code added to the standard 48K Spectrum ROM is mainly devoted to the scanning and decoding of the keypad.
  18411. ; 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',
  18412. ; 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.
  18413. ; Documented by Paul Farrow.
  18414.  
  18415. ; --------------------------------
  18416. ; SCAN THE KEYPAD AND THE KEYBOARD
  18417. ; --------------------------------
  18418. ; This patch will attempt to scan the keypad if in 128K mode and will then scan the keyboard.
  18419.  
  18420. ;; KEYS
  18421. L386E:  PUSH    IX
  18422.         BIT     4,(IY+$01)      ; [FLAGS] Test if in 128K mode
  18423.         JR      Z,L3879         ; Z=in 48K mode
  18424.  
  18425.         CALL    L3A42           ; Attempt to scan the keypad
  18426.  
  18427. ;; KEYS_CONT
  18428. L3879:  CALL    L02BF           ; Scan the keyboard
  18429.         POP     IX
  18430.         RET
  18431.  
  18432. ; ----------------------------------
  18433. ; READ THE STATE OF THE OUTPUT LINES
  18434. ; ----------------------------------
  18435. ; 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.
  18436. ; 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.
  18437.  
  18438. ;; READ_OUTPUTS
  18439. L387F:  LD      C,$FD           ; FFFD = Address of the
  18440.         LD      D,$FF           ; command register (register 7)
  18441.         LD      E,$BF           ; BFFD = Address of the
  18442.         LD      B,D             ; data register (register 14)
  18443.         LD      A,$07
  18444.         OUT     (C),A           ; Select command register
  18445.         IN      H,(C)           ; Read its status
  18446.         LD      A,$0E
  18447.         OUT     (C),A           ; Select data register
  18448.         IN      A,(C)           ; Read its status
  18449.         OR      $F0             ; Mask off the input lines
  18450.         LD      L,A             ; L=state of output lines at the
  18451.         RET                     ; keypad socket
  18452.  
  18453. ; --------------------------
  18454. ; SET THE OUTPUT LINE, BIT 0
  18455. ; --------------------------
  18456. ; The output line to the keypad is set via the LSB of L.
  18457.  
  18458. ;; SET_REG14
  18459. L3896:  LD      B,D
  18460.         LD      A,$0E
  18461.         OUT     (C),A           ; Select the data register
  18462.         LD      B,E
  18463.         OUT     (C),L           ; Send L out to the data register
  18464.         RET                     ; Set the output line
  18465.  
  18466. ; ----------------------------------------
  18467. ; FETCH THE STATE OF THE INPUT LINE, BIT 5
  18468. ; ----------------------------------------
  18469. ; Return the state of the input line from the keypad in bit 5 of A.
  18470.  
  18471. ;; GET_REG14
  18472. L389F:  LD      B,D
  18473.         LD      A,$0E
  18474.         OUT     (C),A           ; Select the data register
  18475.         IN      A,(C)           ; Read the input line
  18476.         RET
  18477.  
  18478. ; ------------------------------
  18479. ; SET THE OUTPUT LINE LOW, BIT 0
  18480. ; ------------------------------
  18481.  
  18482. ;; RESET_LINE
  18483. L38A7:  LD      A,L
  18484.         AND     $FE             ; Reset bit 0 of L
  18485.         LD      L,A
  18486.         JR      L3896           ; Send out L to the data register
  18487.  
  18488. ; -------------------------------
  18489. ; SET THE OUTPUT LINE HIGH, BIT 0
  18490. ; -------------------------------
  18491.  
  18492. ;; SET_LINE
  18493. L38AD:  LD      A,L
  18494.         OR      $01             ; Set bit 0 of L
  18495.         LD      L,A
  18496.         JR      L3896           ; Send out L to the data register
  18497.  
  18498. ; -------------------
  18499. ; MINOR DELAY ROUTINE
  18500. ; -------------------
  18501. ; Delay for (B*13)+5 T-States.
  18502.  
  18503. ;; DELAY
  18504. L38B3:  DJNZ    L38B3
  18505.         RET
  18506.  
  18507. ; -------------------
  18508. ; MAJOR DELAY ROUTINE
  18509. ; -------------------
  18510. ; Delay for (B*271)+5 T-states.
  18511.  
  18512. ;; DELAY2
  18513. L38B6:  PUSH    BC
  18514.         LD      B,$10
  18515.         CALL    L38B3           ; Inner delay of 135 T-States
  18516.         POP     BC
  18517.         DJNZ    L38B6
  18518.         RET
  18519.  
  18520. ; ------------------------------------
  18521. ; MONITOR FOR THE INPUT LINE TO GO LOW
  18522. ; ------------------------------------
  18523. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18524.  
  18525. ;; MON_B5_LO
  18526. L38C0:  PUSH    BC
  18527.         CALL    L389F           ; Read the state of the input line
  18528.         POP     BC
  18529.         AND     $20             ; Test bit 5, the input line
  18530.         JR      Z,L38CB         ; Exit if input line found low
  18531.         DJNZ    L38C0           ; Repeat until timeout expires
  18532.  
  18533. ;; EXT_MON_LO
  18534. L38CB:  RET
  18535.  
  18536. ; -------------------------------------
  18537. ; MONITOR FOR THE INPUT LINE TO GO HIGH
  18538. ; -------------------------------------
  18539. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18540.  
  18541. ;; MON_B5_HI
  18542. L38CC:  PUSH    BC
  18543.         CALL    L389F           ; Read the state of the input line
  18544.         POP     BC
  18545.         AND     $20             ; Test bit 5, the input line
  18546.         JR      NZ,L38D7        ; Exit if input line found low
  18547.         DJNZ    L38CC           ; Repeat until timeout expires
  18548.  
  18549. ;; EXT_MON_HI
  18550. L38D7:  RET
  18551.  
  18552. ; -------------------------
  18553. ; READ KEY PRESS STATUS BIT
  18554. ; -------------------------
  18555. ; 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.
  18556.  
  18557. ;; READ_STATUS
  18558. L38D8:  CALL    L387F           ; Read the output lines
  18559.         LD      B,$01           ; Read in one bit
  18560.         JR      L38E4
  18561.  
  18562. ; ----------------
  18563. ; READ IN A NIBBLE
  18564. ; ----------------
  18565. ; 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.
  18566. ; For a nibble of key press data, a bit read in as 1 indicates that the corresponding key was pressed.
  18567.  
  18568. ;; READ_NIBBLE
  18569. L38DF:  CALL    L387F           ; Read the state of the output lines
  18570.         LD      B,$04           ; Read in four bits
  18571.  
  18572. ;; READ_BIT
  18573. L38E4:  PUSH    BC
  18574.         CALL    L389F           ; Read the input line from the keypad
  18575.         POP     BC
  18576.         AND     $20             ; This line should initially be high
  18577.         JR      Z,L392D         ; Z=read in a 0, there must be an error
  18578.  
  18579.         XOR     A               ; The bits read in will be stored in register A
  18580.  
  18581. ;; BIT_LOOP
  18582. L38EE:  PUSH    BC              ; Preserve the loop count and any bits
  18583.         PUSH    AF              ; read in so far
  18584.         CALL    L38AD           ; Set the output line high
  18585.  
  18586.         LD      B,$A3           ; Monitor for 17609 T-states for the
  18587.         CALL    L38C0           ; input line to go low
  18588.         JR      NZ,L392B        ; NZ=the line did not go low
  18589.  
  18590.         CALL    L38A7           ; Set the output line low
  18591.         JR      L3901           ; Insert a delay of 12 T-states
  18592.  
  18593. L38FF:  DB    $FF, $FF
  18594.  
  18595. ;; BL_CONTINUE
  18596. L3901:  LD      B,$2B           ; Delay for 564 T-states
  18597.         CALL    L38B3
  18598.         CALL    L389F           ; Read in the bit value
  18599.         BIT     5,A
  18600.         JR      Z,L3911         ; Z=read in a 0
  18601.  
  18602.         POP     AF              ; Retrieve read in bits
  18603.         SCF                     ; Set carry bit
  18604.         JR      L3914
  18605.  
  18606. ;; BL_READ_0
  18607. L3911:  POP     AF              ; Retrieve read in bits
  18608.         SCF
  18609.         CCF                     ; Clear carry bit
  18610.  
  18611. ;; BL_STORE
  18612. L3914:  RRA                     ; Shift the carry bit into bit 0 of A
  18613.         PUSH    AF              ; Save bits read in
  18614.         CALL    L38AD           ; Set the output line high
  18615.  
  18616.         LD      B,$26           ; Delay for 499 T-states
  18617.         CALL    L38B3
  18618.  
  18619.         CALL    L38A7           ; Set the output line low
  18620.  
  18621.         LD      B,$23           ; Delay for 460 T-states
  18622.         CALL    L38B3
  18623.  
  18624.         POP     AF              ; Retrieve read in bits
  18625.         POP     BC              ; Retrieve loop counter and repeat
  18626.         DJNZ    L38EE           ; for all bits to read in
  18627.         RET
  18628.  
  18629. ; ----------
  18630. ; LINE ERROR
  18631. ; ----------
  18632. ; 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.
  18633. ; The upper nibble of system variable FLAGS/ROW3 will be cleared to indicate that communications to the keypad is no longer in progress.
  18634.  
  18635. ;; LINE_ERROR
  18636. L392B:  POP     AF
  18637.         POP     BC              ; Clear the stack
  18638.  
  18639. ;; LINE_ERROR2
  18640. L392D:  CALL    L38AD           ; Set the output line high
  18641.  
  18642.         XOR     A               ; Clear FLAGS nibble
  18643.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18644.  
  18645.         INC     A               ; Return zero flag reset
  18646.         SCF
  18647.         CCF                     ; Return carry flag reset
  18648.         RET
  18649.  
  18650. ; ---------------
  18651. ; POLL THE KEYPAD
  18652. ; ---------------
  18653. ; 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.
  18654. ; 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.
  18655. ; 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:
  18656. ;
  18657. ; A Register    Zero Flag       Carry Flag    Cause
  18658. ; 0             set             set           Communications already established
  18659. ; 0             set             reset         Nibble read in OK
  18660. ; 1             reset           reset         Nibble read in with an error or i/p line initially low
  18661. ; 1             reset           set           Poll counter has not yet reached zero
  18662. ;
  18663. ; The third bit of the nibble read in must be set for the poll to be subsequently accepted.
  18664.  
  18665. ;; ATTEMPT_POLL
  18666. L3938:  CALL    L387F           ; Read the output line states
  18667.  
  18668.         LD      A,($5B88)       ; [FLAGS/ROW3] Has communications already been
  18669.         AND     $80             ; established with the keypad?
  18670.         JR      NZ,L3999        ; NZ=yes, so skip the poll
  18671.  
  18672.         CALL    L389F           ; Read the input line
  18673.         AND     $20             ; It should be high initially
  18674.         JR      Z,L392D         ; Z=error, input line found low
  18675.  
  18676.         LD      A,($5B88)       ; [FLAGS/ROW3] Test if poll counter already zero thus
  18677.         AND     A               ; indicating a previous comms error
  18678.         JR      NZ,L395A        ; NZ=ready to poll the keypad
  18679.  
  18680.         INC     A               ; Indicate comms not established
  18681.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18682.         LD      A,$4C           ; Reset the poll counter
  18683.         LD      ($5B89),A       ; [ROW2/ROW1]
  18684.         JR      L399C           ; Exit the routine
  18685.  
  18686. ;;POLL_KEYPAD
  18687. L395A:  LD      A,($5B89)       ; [ROW2/ROW1] Decrement the poll counter
  18688.         DEC     A
  18689.         LD      ($5B89),A       ; [ROW2/ROW1]
  18690.         JR      NZ,L399C        ; Exit the routine if it is not yet zero
  18691.  
  18692. ; The poll counter has reached zero so a poll of the keypad can now occur.
  18693.  
  18694.         XOR     A
  18695.         LD      ($5B88),A       ; [FLAGS/ROW3] Indicate that a poll can occur
  18696.         LD      ($5B89),A       ; [ROW2/ROW1]
  18697.         LD      ($5B8A),A       ; [ROW4/ROW5] Clear all the row nibble stores
  18698.  
  18699.         CALL    L38A7           ; Set the output line low
  18700.  
  18701.         LD      B,$21           ; Wait up to 3569 T-States for the
  18702.         CALL    L38C0           ; input line to go low
  18703.         JR      NZ,L392D        ; NZ=line did not go low
  18704.  
  18705.         CALL    L38AD           ; Set the output line high
  18706.  
  18707.         LD      B,$24           ; Wait up to 3893 T-States for the
  18708.         CALL    L38CC           ; input line to go high
  18709.         JR      Z,L392D         ; NZ=line did not go high
  18710.  
  18711.         CALL    L38A7           ; Set the output line low
  18712.  
  18713.         LD      B,$0F
  18714.         CALL    L38B6           ; Delay for 4070 T-States
  18715.         CALL    L38DF           ; Read in a nibble of data
  18716.         JR      NZ,L392D        ; NZ=error occurred when reading in nibble
  18717.  
  18718.         SET     7,A             ; Set bit 7
  18719.         AND     $F0             ; Keep only the upper four bits
  18720.                                 ; (Bit 6 will be set if poll successful)
  18721.         LD      ($5B88),A       ; [FLAGS/ROW3] Store the flags nibble
  18722.         XOR     A
  18723.         SRL     A               ; Exit: Zero flag set, Carry flag reset
  18724.         RET
  18725.  
  18726. ;; AP_SKIP_POLL
  18727. L3999:  XOR     A               ; Communications already established
  18728.         SCF                     ; Exit: Zero flag set, Carry flag set
  18729.         RET
  18730.  
  18731. ;; PK_EXIT
  18732. L399C:  XOR     A               ; Poll counter not zero
  18733.         INC     A
  18734.         SCF                     ; Exit: Zero flag reset, Carry flag set
  18735.         RET
  18736.  
  18737. ; -----------------------
  18738. ; SCAN THE KEYPAD ROUTINE
  18739. ; -----------------------
  18740. ; If a successful poll of the keypad occurs then the five rows of keys are read in and a unique key code generated.
  18741.  
  18742. ;; KEYPAD_SCAN
  18743. L39A0:  CALL    L3938           ; Try to poll the keypad
  18744.  
  18745.         LD      A,($5B88)       ; [FLAGS/ROW3] Test the flags nibble
  18746.         CPL
  18747.         AND     $C0             ; Bits 6 and 7 must be set in FLAGS
  18748.         RET     NZ              ; NZ=poll was not successful
  18749.  
  18750. ; The poll was successful so now read in data for the five keypad rows.
  18751.  
  18752.         LD      IX,$5B8A        ; [ROW4/ROW5]
  18753.         LD      B,$05           ; The five rows
  18754.  
  18755. ;; KS_LOOP
  18756. L39B0:  PUSH    BC              ; Save counter
  18757.  
  18758.         CALL    L38D8           ; Read the key press status bit
  18759.         JP      NZ,L3A3A        ; NZ=error occurred
  18760.  
  18761.         BIT     7,A             ; Test the bit read in
  18762.         JR      Z,L39DC         ; Z=no key pressed in this row
  18763.  
  18764.         CALL    L38DF           ; Read in the row's nibble of data
  18765.         JR      NZ,L3A3A        ; NZ=error occurred
  18766.  
  18767.         POP     BC              ; Fetch the nibble loop counter
  18768.         PUSH    BC
  18769.         LD      C,A             ; Move the nibble read in to C
  18770.         LD      A,(IX+$00)      ; Fetch the nibble store
  18771.         BIT     0,B             ; Test if an upper or lower nibble
  18772.         JR      Z,L39D6         ; Z=upper nibble
  18773.  
  18774.         SRL     C               ; Shift the nibble to the lower position
  18775.         SRL     C
  18776.         SRL     C
  18777.         SRL     C
  18778.         AND     $F0             ; Mask off the lower nibble of the
  18779.         JR      L39D8           ; nibble store
  18780.  
  18781. ;; KS_UPPER
  18782. L39D6:  AND     $0F             ; Mask off the upper nibble of the nibble store
  18783.  
  18784. ;; KS_STORE
  18785. L39D8:  OR      C               ; Combine the existing and new
  18786.         LD      (IX+$00),A      ; nibbles and store them
  18787.  
  18788. ;; KS_NEXT
  18789. L39DC:  POP     BC              ; Retrieve the row counter
  18790.         BIT     0,B             ; Test if next nibble store is required
  18791.         JR      NZ,L39E3        ; NZ=use same nibble store
  18792.  
  18793.         DEC     IX              ; Point to the next nibble store
  18794.  
  18795. ;; KS_NEW
  18796. L39E3:  DJNZ    L39B0           ; Repeat for the next keypad row
  18797.  
  18798. ; All five rows have now been read so compose a unique code for the key pressed.
  18799.  
  18800.         LD      E,$80           ; Signal no key press found yet
  18801.         LD      IX,$5B88        ; [FLAGS/ROW3]
  18802.         LD      HL,$3A3F        ; Point to the key mask data
  18803.         LD      B,$03           ; Scan three nibbles
  18804.  
  18805. ;; GEN_LOOP
  18806. L39F0:  LD      A,(IX+$00)      ; Fetch a pair of nibbles
  18807.         AND     (HL)            ; This will mask off the FLAGS nibble and the SHIFT/0 key
  18808.  
  18809.         JR      Z,L3A17         ; Z=no key pressed in these nibbles
  18810.  
  18811.         BIT     7,E             ; Test if a key has already been found
  18812.         JR      Z,L3A3C         ; Z=multiple keys pressed
  18813.  
  18814.         PUSH    BC              ; Save the loop counter
  18815.         PUSH    AF              ; Save the byte of key bit data
  18816.         LD      A,B             ; Move loop counter to A
  18817.         JR      L3A01           ; A delay of 12 T-States
  18818.  
  18819. L39FF:  DB    $FF, $FF        ; Unused locations
  18820.  
  18821. ;; GEN_CONT
  18822. L3A01:  DEC     A               ; These lines of code generate base
  18823.         SLA     A               ; values of 7, 15 and 23 for the three
  18824.         SLA     A               ; nibble stores 5B88, 5B89 & 5B8A.
  18825.         SLA     A
  18826.         OR      $07
  18827.         LD      B,A             ; B=(loop counter-1)*8+7
  18828.         POP     AF              ; Fetch the byte of key press data
  18829.  
  18830. ;; GEN_BIT
  18831. L3A0C:  SLA     A               ; Shift until a set key bit drops into the
  18832.         JP      C,L3A13         ; carry flag
  18833.  
  18834.         DJNZ    L3A0C           ; Decrement B for each 'unsuccessful' shift of the A register
  18835.  
  18836. ;; GEN_FOUND
  18837. L3A13:  LD      E,B             ; E=a unique number for the key pressed, between 1 - 19 except 2 & 3
  18838.  
  18839.         POP     BC              ; As a result shifting the set key bit
  18840.                                 ; into the carry flag, the A register will
  18841.                                 ; hold 00 if only one key was pressed
  18842.         JR      NZ,L3A3C        ; NZ=multiple keys pressed
  18843.  
  18844. ;; GEN_NEXT
  18845. L3A17:  INC     IX              ; Point to the next nibble store
  18846.         INC     HL              ; Point to the corresponding mask data
  18847.         DJNZ    L39F0           ; Repeat for all three 'nibble' bytes
  18848.  
  18849.         BIT     7,E             ; Test if any keys were pressed
  18850.         JR      NZ,L3A27        ; NZ=no keys were pressed
  18851.  
  18852.         LD      A,E             ; Copy the key code
  18853.         AND     $FC             ; Test for the '.' key (E=1)
  18854.         JR      Z,L3A27         ; Z='.' key pressed
  18855.  
  18856.         DEC     E
  18857.         DEC     E               ; Key code in range 2 - 17
  18858.  
  18859. ; The E register now holds a unique key code value between 1 and 17.
  18860.  
  18861. ;; GEN_POINT
  18862. L3A27:  LD      A,($5B8A)       ; [ROW4/ROW5] Test if the SHIFT key was pressed
  18863.         AND     $08
  18864.         JR      Z,L3A34         ; Z=the SHIFT key was not pressed
  18865.  
  18866. ; The SHIFT key was pressed or no key was pressed.
  18867.  
  18868.         LD      A,E             ; Fetch the key code
  18869.         AND     $7F             ; Mask off 'no key pressed' bit
  18870.         ADD     A,$12           ; Add on a shift offset of 12
  18871.         LD      E,A
  18872.  
  18873. ; 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.
  18874.  
  18875. ;; GEN_NOSHIFT
  18876. L3A34:  LD      A,E
  18877.         ADD     A,$5A           ; Add a base offset of 5A
  18878.         LD      E,A             ; Return key codes in range 5B - 7D
  18879.         XOR     A
  18880.         RET                     ; Exit: Zero flag set, key found OK
  18881.  
  18882. ; 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.
  18883.  
  18884. ;; KS_ERROR
  18885. L3A3A:  POP     BC              ; Clear the stack and exit
  18886.         RET                     ; Exit: Zero flag reset
  18887.  
  18888. ;; GEN_INVALID
  18889. L3A3C:  XOR     A               ; Exit: Zero flag reset indicating an
  18890.         INC     A               ; invalid key press
  18891.         RET
  18892.  
  18893. ; ----------------
  18894. ; KEYPAD MASK DATA
  18895. ; ----------------
  18896.  
  18897. ;; KEY_MASKS
  18898. L3A3F:  DB    $0F, $FF, $F2   ; Key mask data
  18899.  
  18900. ; ---------------
  18901. ; READ THE KEYPAD
  18902. ; ---------------
  18903. ; 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.
  18904. ; 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.
  18905. ; 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.
  18906. ; The KSTATE system variables store the following data:
  18907. ;
  18908. ;       KSTATE0/4       Un-decoded Key Value (00-27 for keyboard, 5B-7D for keypad, FF for no key)
  18909. ;       KSTATE1/5       10 Call Counter
  18910. ;       KSTATE2/6       Repeat Delay
  18911. ;       KSTATE3/7       Decoded Key Value
  18912. ;
  18913. ; The code returned is then stored in system variable LAST_K (5C08) and a new key signalled by setting bit 5 of FLAGS (5C3B).
  18914. ;
  18915. ; 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.
  18916. ; 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
  18917. ; 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.
  18918. ;
  18919. ; 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
  18920. ; 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
  18921. ; a return upon every other call to KEYPAD and then to have used a '5 Call Counter' just as the keyboard routine does.
  18922. ;
  18923. ; 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
  18924. ; 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
  18925. ; decrement the KSTATE system variable Call Counters. The keypad routine 'knows' of the existence of keyboard key codes but the reverse is not true.
  18926.  
  18927. ;; KEYPAD
  18928. L3A42:  LD      E,$80           ; Signal no key pressed
  18929.         LD      A,($5C78)       ; [FRAMES]
  18930.         AND     $01             ; Scan the keypad every other
  18931.         JR      NZ,L3A4F        ; interrupt
  18932.  
  18933.         CALL    L39A0
  18934.         RET     NZ              ; NZ=no valid key pressed
  18935.  
  18936. ;; KP_CHECK
  18937. L3A4F:  LD HL,$5C00             ; [KSTATE0] Test the first KSTATE variable
  18938.  
  18939. ;; KP_LOOP
  18940. L3A52:  BIT     7,(HL)          ; Is the set free?
  18941.         JR      NZ,L3A62        ; NZ=yes
  18942.  
  18943.         LD      A,(HL)          ; Fetch the un-decoded key value
  18944.         CP      $5B             ; Is it a keyboard code?
  18945.         JR      C,L3A62         ; C=yes, so do not decrement counter
  18946.  
  18947.         INC     HL
  18948.         DEC     (HL)            ; Decrement the 10 Call Counter
  18949.         DEC     HL
  18950.         JR      NZ,L3A62        ; If the counter reaches zero, then
  18951.                                 ; signal the set is free
  18952.         LD      (HL),$FF
  18953.  
  18954. ;; KP_CH_SET
  18955. L3A62:  LD      A,L             ; Jump back and test the second set if
  18956.         LD      HL,$5C04        ; [KSTATE4] not yet considered
  18957.         CP      L
  18958.         JR      NZ,L3A52
  18959.  
  18960.         CALL    L3AAE           ; Test for valid key combinations and
  18961.         RET     NZ              ; return if invalid
  18962.  
  18963.         LD      A,E             ; Test if the key in the first set is being
  18964.         LD      HL,$5C00        ; [KSTATE0] repeated
  18965.         CP      (HL)
  18966.         JR      Z,L3A9E         ; Jump if being repeated
  18967.  
  18968.         EX      DE,HL           ; Save the address of KSTATE0
  18969.         LD      HL,$5C04        ; [KSTATE4] Test if the key in the second set is
  18970.         CP      (HL)            ; being repeated
  18971.         JR      Z,L3A9E         ; Jump if being repeated
  18972.  
  18973. ; A new key will not be accepted unless one of the KSTATE sets is free.
  18974.  
  18975.         BIT     7,(HL)          ; Test if the second set is free
  18976.         JR      NZ,L3A83        ; Jump if set is free
  18977.  
  18978.         EX      DE,HL
  18979.         BIT     7,(HL)          ; Test if the first set is free
  18980.         RET     Z               ; Return if no set is free
  18981.  
  18982. ;; KP_NEW
  18983. L3A83:  LD      E,A             ; Pass the key code to the E register
  18984.         LD      (HL),A          ; and to KSTATE0/4
  18985.         INC     HL
  18986.         LD      (HL),$0A        ; Set the '10 Call Counter' to 10
  18987.         INC     HL
  18988.  
  18989.         LD      A,($5C09)       ; [REPDEL] Fetch the initial repeat delay
  18990.         SRL     A               ; Divide delay by two
  18991.         LD      (HL),A          ; Store the repeat delay
  18992.         INC     HL
  18993.  
  18994.         CALL    L3AD7           ; Decode the keypad key code
  18995.         LD      (HL),E          ; and store it in KSTATE3/7
  18996.  
  18997. ; This section is common for both new keys and repeated keys.
  18998.  
  18999. ;; KP_END
  19000. L3A94:  LD      A,E
  19001.         LD      ($5C08),A       ; [LAST_K] Store the key value in LAST_K
  19002.         LD      HL,$5C3B        ; FLAGS
  19003.         SET     5,(HL)          ; Signal a new key pressed
  19004.         RET
  19005.  
  19006. ; -------------------------
  19007. ; THE KEY REPEAT SUBROUTINE
  19008. ; -------------------------
  19009.  
  19010. ;; KP_REPEAT
  19011. L3A9E:  INC     HL
  19012.         LD      (HL),$0A        ; Reset the '10 Call Counter' to 10
  19013.         INC     HL
  19014.         DEC     (HL)            ; Decrement the repeat delay
  19015.         RET     NZ              ; Return if not zero
  19016.  
  19017.         LD      A,($5C0A)       ; [REPPER] The subsequent repeat delay is
  19018.         SRL     A               ; divided by two and stored
  19019.         LD      (HL),A
  19020.         INC     HL
  19021.         LD      E,(HL)          ; The key repeating is fetched
  19022.         JR      L3A94           ; and then returned in LAST_K
  19023.  
  19024. ; ----------------------------------------
  19025. ; THE TEST FOR A VALID KEY CODE SUBROUTINE
  19026. ; ----------------------------------------
  19027. ; 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.
  19028.  
  19029. ;; KP_TEST
  19030. L3AAE:  LD      A,E
  19031.         LD      HL,$5B66        ; FLAGS3 Test if in BASIC or EDIT mode
  19032.         BIT     0,(HL)
  19033.         JR      Z,L3ABC         ; Z=EDIT mode
  19034.  
  19035. ; Test key codes when in BASIC/CALCULATOR mode
  19036.  
  19037.         CP      $6D             ; Test for shifted keys
  19038.         JR      NC,L3AD4        ; and signal an error if found
  19039.  
  19040. ;; KPT_OK
  19041. L3ABA:  XOR     A               ; Signal valid key code
  19042.         RET                     ; Exit: Zero flag set
  19043.  
  19044. ; Test key codes when in EDIT/MENU mode.
  19045.  
  19046. ;; KPT_EDIT
  19047. L3ABC:  CP      $80             ; Test for no key press
  19048.         JR      NC,L3AD4        ; NC=no key press
  19049.  
  19050.         CP      $6C             ; Test for SHIFT on its own
  19051.         JR      NZ,L3ABA        ; NZ=valid key code
  19052.  
  19053. L3AC4:  DB    $00, $00, $00   ; Delay for 64 T-States
  19054.         DB    $00, $00, $00
  19055.         DB    $00, $00, $00
  19056.         DB    $00, $00, $00
  19057.         DB    $00, $00, $00
  19058.         DB    $00
  19059.  
  19060. ;; KPT_INVALID
  19061. L3AD4:  XOR     A               ; Signal invalid key code
  19062.         INC     A
  19063.         RET                     ; Exit: Zero flag reset
  19064.  
  19065. ; ---------------------------
  19066. ; THE KEY DECODING SUBROUTINE
  19067. ; ---------------------------
  19068.  
  19069. ;; KP_DECODE
  19070. L3AD7:  PUSH    HL              ; Save the KSTATE pointer
  19071.         LD      A,E
  19072.         SUB     $5B             ; Reduce the key code range to
  19073.         LD      D,$00           ; 00 - 22 and transfer to DE
  19074.         LD      E,A
  19075.  
  19076.         LD      HL,$5B66        ; FLAGS3 Test if in EDIT or BASIC mode
  19077.         BIT     0,(HL)
  19078.         JR      Z,L3AEA         ; Z=EDIT/MENU mode
  19079.  
  19080. ; Use Table 1 when in CALCULATOR/BASIC mode.
  19081.  
  19082.         LD      HL,L3B13
  19083.         JR      L3B0F           ; Look up the key value
  19084.  
  19085. ; Deal with EDIT/MENU mode.
  19086.  
  19087. ;; KPD_EDIT
  19088. L3AEA:  LD      HL,L3B25        ; Use Table 4 for unshifted key
  19089.         CP      $11             ; presses
  19090.         JR      C,L3B0F
  19091.  
  19092. ; Deal with shifted keys in EDIT/MENU mode.
  19093.  
  19094. ; 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,
  19095. ; it actually performs no function when editing a BASIC program.
  19096.  
  19097.         LD      HL,L3B21
  19098.         CP      $15             ; Test for SHIFT 1
  19099.         JR      Z,L3B0F
  19100.  
  19101.         CP      $16             ; Test for SHIFT 2
  19102.         JR      Z,L3B0F
  19103.  
  19104.         JR      L3B01           ; Delay for 12 T-States
  19105.  
  19106. L3AFE:  DB    $00, $FF, $FF   ; Unused locations
  19107.  
  19108. ;; KPD_CONT
  19109. L3B01:  CP      $17             ; Test for SHIFT 3
  19110.         JR      Z,L3B0F
  19111.  
  19112. ; Use Table 2 with SHIFT 4 (delete to beginning of word) and SHIFT 5 (delete to end of word).
  19113.  
  19114.         LD      HL,L3B18
  19115.         CP      $21             ; Test for SHIFT 4 and above
  19116.         JR      NC,L3B0F
  19117.  
  19118. ;Use Table 1 for all other shifted key presses.
  19119.  
  19120.         LD      HL,L3B13
  19121.  
  19122. ;; KPD_EXIT
  19123. L3B0F:  ADD     HL,DE           ; Look up the key value
  19124.         LD      E,(HL)
  19125.         POP     HL              ; Retrieve the KSTATE address
  19126.         RET
  19127.  
  19128. ; --------------------------------
  19129. ; THE KEYPAD DECODE LOOK-UP TABLES
  19130. ; --------------------------------
  19131.  
  19132. ;; KPD_TABLE1
  19133. L3B13:  DB    $2E, $0D, $33   ; '.', ENTER, 3
  19134.         DB    $32, $31        ; 2, 1
  19135.  
  19136. ;; KPD_TABLE2
  19137. L3B18:  DB    $29, $28, $2A   ; ), (, *
  19138.         DB    $2F, $2D, $39   ; /, - , 9
  19139.         DB    $38, $37, $2B   ; 8, 7, +
  19140.  
  19141. ;; KPD_TABLE3
  19142. L3B21:  DB    $36, $35, $34   ; 6, 5, 4
  19143.         DB    $30             ; 0
  19144.  
  19145. ;; KPD_TABLE4
  19146. L3B25:  DB    $A5, $0D, $A6   ; Bottom, ENTER, Top
  19147.         DB    $A7, $A8, $A9   ; End of line, Start of line, TOGGLE
  19148.         DB    $AA, $0B, $0C   ; DEL right, Up, DEL
  19149.         DB    $07, $09, $0A   ; CMND, Right, Down
  19150.         DB    $08, $AC, $AD   ; Left, Down ten, Up ten
  19151.         DB    $AE, $AF        ; End word, Beginning of word
  19152.         DB    $B0, $B1, $B2   ; DEL to end of line, DEL to start of line, SHIFT TOGGLE
  19153.         DB    $B3, $B4        ; DEL to end of word, DEL to beginning of word
  19154.  
  19155. ; -----------------------------
  19156. ; PRINT NEW ERROR MESSAGE PATCH
  19157. ; -----------------------------
  19158.  
  19159. L3B3B:  BIT     4,(IY+$01)      ; FLAGS 3 - In 128K mode?
  19160.         JR      NZ,L3B46        ; NZ=128K mode
  19161.  
  19162. ; In 48K mode
  19163.  
  19164.         XOR     A               ; Replicate code from standard ROM that the patch over-wrote
  19165.         LD      DE,$1536
  19166.         RET    
  19167.  
  19168. ; In 128K mode
  19169.  
  19170. L3B46:  LD      HL,$010F        ; Vector table entry in Editor ROM -> JP $03A2
  19171.  
  19172. ; Return to Editor ROM at address in HL
  19173.  
  19174. L3B49:  EX      (SP),HL         ; Change the return address
  19175.         JP      $5B00           ; Page Editor ROM and return to the address on the stack
  19176.  
  19177. ; -------------------------------------
  19178. ; STATEMENT INTERPRETATION RETURN PATCH
  19179. ; -------------------------------------
  19180.  
  19181. L3B4D:  BIT     4,(IY+$01)      ; In 128K mode?
  19182.         JR      NZ,L3B58        ; NZ=128K mode
  19183.  
  19184. ; In 48K mode
  19185.  
  19186.         BIT     7,(IY+$0A)      ; replicate code from standard ROM that the patch over-wrote
  19187.         RET    
  19188.  
  19189. ; In 128K mode
  19190.  
  19191. L3B58:  LD      HL,$0112        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #182A
  19192.         JR      L3B49
  19193.  
  19194. ; --------------------------
  19195. ; GO TO NEXT STATEMENT PATCH
  19196. ; --------------------------
  19197.  
  19198. L3B5D:  BIT     4,(IY+$01)      ; In 128K mode?
  19199.         JR      NZ,L3B67        ; NZ=128K mode
  19200.  
  19201. ; In 48K mode
  19202.  
  19203.         RST     18H             ; replicate code from standard ROM that the patch over-wrote
  19204.         CP      $0D
  19205.         RET    
  19206.  
  19207. ; In 128K mode
  19208.  
  19209. L3B67:  LD      HL,$0115        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #18A8
  19210.         JR      L3B49
  19211.  
  19212. ; --------------------------------------
  19213. ; INKEY$ ROUTINE TO DEAL WITH THE KEYPAD
  19214. ; --------------------------------------
  19215.  
  19216. ;; KEYSCAN2
  19217. L3B6C:  CALL    L028E           ; KEYSCAN Scan the keyboard
  19218.         LD      C,$00
  19219.         JR      NZ,L3B80        ; NZ=multiple keys
  19220.  
  19221.         CALL    L031E           ; K_TEST
  19222.         JR      NC,L3B80        ; NC=shift only or no key
  19223.  
  19224.         DEC     D
  19225.         LD      E,A
  19226.         CALL    L0333           ; K_DECODE
  19227.         JP      L2657           ; S_CONT Get string and continue scanning
  19228.  
  19229. ;; KPI_SCAN
  19230. L3B80:  BIT     4,(IY+$01)      ; 128K mode?
  19231.         JP      Z,L2660         ; S_IK$_STK Z=no, stack keyboard code
  19232.  
  19233.         DI                      ; Disable interrupts whilst scanning
  19234.         CALL    L39A0           ; the keypad
  19235.         EI
  19236.         JR      NZ,L3B9A        ; NZ=multiple keys
  19237.  
  19238.         CALL    L3AAE           ; Test the keypad
  19239.         JR      NZ,L3B9A        ; NZ=no key, shift only or invalid combination
  19240.  
  19241.         CALL    L3AD7           ; Form the key code
  19242.         LD      A,E
  19243.         JP      L2657           ; S_CONT Get string and continue scanning
  19244.  
  19245. ;; KPI_INVALID
  19246. L3B9A:  LD      C,$00           ; Signal no key, i.e. length=0
  19247.         JP      L2660           ; S_IK$_STK
  19248.  
  19249. ; ---------------------
  19250. ; PRINT TOKEN/UDG PATCH
  19251. ; ---------------------
  19252.  
  19253. L3B9F:  CP      $A3             ; SPECTRUM (T)
  19254.         JR      Z,L3BAF
  19255.  
  19256.         CP      $A4             ; PLAY (U)
  19257.         JR      Z,L3BAF
  19258.  
  19259. ; In 48K mode here
  19260.  
  19261. L3BA7:  SUB     $A5             ; Check as per original ROM
  19262.         JP      NC,$0B5F
  19263.  
  19264.         JP      $0B56           ; Rejoin original ROM routine
  19265.  
  19266. L3BAF:  BIT     4,(IY+$01)      ; FLAGS3 - Bit 4=1 if in 128K mode
  19267.         JR      Z,L3BA7         ; Rejoin code for when in 48K mode
  19268.  
  19269. ; In 128K mode here
  19270.  
  19271.         LD      DE,L3BC9
  19272.         PUSH    DE              ; Stack return address
  19273.  
  19274.         SUB     $A3             ; Check whether the SPECTRUM token
  19275.  
  19276.         LD      DE,L3BD2        ; SPECTRUM token
  19277.         JR      Z,L3BC3
  19278.  
  19279.         LD      DE,L3BDA        ; PLAY token
  19280.  
  19281. L3BC3:  LD      A,$04           ; Signal not RND, INKEY$ or PI so that a trailing space is printed
  19282.         PUSH    AF
  19283.         JP      L0C17           ; Rejoin printing routine PO-TABLE+3
  19284.  
  19285. ; Return address from above
  19286.  
  19287. L3BC9:  SCF                     ; Return as if no trailing space
  19288.  
  19289.         BIT     1,(IY+$01)      ; Test if printer is in use
  19290.         RET     NZ              ; NZ=printer in use
  19291.  
  19292.         JP      $0B03           ; PO-FETCH - Return via Position Fetch routine
  19293.  
  19294. L3BD2           DC "SPECTRUM"   ;DEFM    "SPECTRU"       ; SPECTRUM token
  19295.                                 ;DB    'M'+$80
  19296.  
  19297. L3BDA           DC "PLAY"       ;DEFM    "PLA"           ; PLAY token
  19298.                                 ;DB    'Y'+$80
  19299.  
  19300. ;; KP_SCAN2
  19301. L3BDE:  JP      L3C01           ; This is not called from either ROM. It can be used to scan the keypad.
  19302.  
  19303. ;===============================
  19304.                 IF TAP_EMUL
  19305. INI_TAP         RST 0X08
  19306.                 DB TAPE_INIT
  19307.                 SCF
  19308.                 SBC HL,DE
  19309.                 RET
  19310.                 ENDIF
  19311.  
  19312.                 IF AY_PRINTER
  19313. PRINTER_INITER  RST 0X08
  19314.                 DB AY_PRN_INIT
  19315.                 JP L0EDF
  19316.                 ENDIF
  19317.  
  19318. PRN_TOKEN       RST 0X08
  19319.                 DB AY_PRN_TOKEN
  19320.                 RET
  19321.  
  19322.                 DUPL 0X3BFF-$,0
  19323.                 DW 0XFFFF
  19324. ;===============================
  19325.  
  19326. ;; KP_SCAN
  19327. 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.
  19328.  
  19329. ; -----------------------
  19330. ; TV TUNER VECTOR ENTRIES
  19331. ; -----------------------
  19332.  
  19333. L3C04:  JP      L3C10
  19334. L3C07:  JP      L3C10
  19335. L3C0A:  JP      L3C10
  19336. L3C0D:  JP      L3C10
  19337.  
  19338. ; ----------------
  19339. ; TV TUNER ROUTINE
  19340. ; ----------------
  19341. ; 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.
  19342. ; 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
  19343. ; 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
  19344. ; shown the year '1986' in varying ink colours. This leads to a display that shows all possible ink colours on all possible paper colours.
  19345.  
  19346. ;; TV_TUNER
  19347. L3C10:  LD      A,$7F           ; Test for the BREAK key
  19348.         IN      A,($FE)
  19349.         RRA    
  19350.         RET     C               ; C=SPACE not pressed
  19351.  
  19352.         LD      A,$FE
  19353.         IN      A,($FE)
  19354.         RRA    
  19355.         RET     C               ; C=SPACE not pressed
  19356.  
  19357.         LD      A,$07
  19358.         OUT     ($FE),A         ; Set the border to white
  19359.  
  19360.         LD      A,$02           ; Open channel 2 (main screen)
  19361.         CALL    $1601
  19362.  
  19363.         XOR     A
  19364.         LD      ($5C3C),A       ; [TV_FLAG] Signal using main screen
  19365.  
  19366.         LD      A,$16           ; Print character 'AT'
  19367.         RST     10H
  19368.  
  19369.         XOR     A               ; Print character '0'
  19370.         RST     10H
  19371.  
  19372.         XOR     A               ; Print character '0'
  19373.         RST     10H
  19374.  
  19375.         LD      E,$08           ; Number of characters per colour
  19376.         LD      B,E             ; Paper counter + 1
  19377.         LD      D,B             ; Ink counter + 1
  19378.  
  19379. ;; TVT_ROW
  19380. L3C34:  LD      A,B             ; Calculate the paper colour
  19381.         DEC     A               ; Bits 3-5 of each screen attribute
  19382.                 DB 0XCB
  19383.                 RLA               ; holds the paper colour; bits 0-2
  19384.                 DB 0XCB
  19385.                 RLA               ; the ink colour
  19386.                 DB 0XCB
  19387.                 RLA
  19388.         ADD     A,D             ; Add the ink colour
  19389.         DEC     A
  19390.         LD      ($5C8F),A       ; [ATTR_T] Store as temporary attribute value
  19391.  
  19392.         LD      HL,L3C8F        ; TVT_DATA Point to the 'year' data
  19393.         LD      C,E             ; Get number of characters to print
  19394.  
  19395. ;; TVT_YEAR
  19396. L3C45:  LD      A,(HL)          ; Fetch a character from the data
  19397.         RST     10H             ; Print it
  19398.         INC     HL
  19399.         DEC     C
  19400.         JR      NZ,L3C45        ; Repeat for the 8 characters
  19401.  
  19402.         DJNZ    L3C34           ; Repeat for all colours in this row
  19403.  
  19404.         LD      B,E             ; Reset paper colour
  19405.         DEC     D               ; Next ink colour
  19406.         JR      NZ,L3C34        ; Produce next row with new ink colour
  19407.  
  19408.         LD      HL,$4800        ; Point to 2nd third of display file
  19409.         LD      D,H
  19410.         LD      E,L
  19411.         INC     DE              ; Point to the next display cell
  19412.         XOR     A
  19413.         LD      (HL),A          ; Clear first display cell
  19414.         LD      BC,$0FFF
  19415.         LDIR                    ; Clear lower 2 thirds of display file
  19416.  
  19417.         EX      DE,HL           ; HL points to start of attributes file
  19418.         LD      DE,$5900        ; Point to 2nd third of attributes file
  19419.         LD      BC,$0200
  19420.         LDIR                    ; Copy screen attributes
  19421.  
  19422. ; 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).
  19423.  
  19424.         DI                      ; Disable interrupts so that a pure tone can be generated
  19425.  
  19426. ;; TVT_TONE
  19427. L3C68:  LD      DE,$0370        ; DE=twice the tone frequency in Hz
  19428.         LD      L,$07           ; Border colour of white
  19429.  
  19430. ;; TVT_DURATION
  19431. L3C6D:  LD      BC,$0099        ; Delay for 950.4us
  19432.  
  19433. ;; TVT_PERIOD
  19434. L3C70:  DEC     BC
  19435.         LD      A,B
  19436.         OR      C
  19437.         JR      NZ,L3C70
  19438.  
  19439.         LD      A,L
  19440.         XOR     $10              ; Toggle the speaker output whilst
  19441.         LD      L,A              ; preserving the border colour
  19442.         OUT     ($FE),A
  19443.  
  19444.         DEC     DE               ; Generate the tone for 1 second
  19445.         LD      A,D
  19446.         OR      E
  19447.         JR      NZ,L3C6D
  19448.  
  19449. ; At this point the speaker is turned off, so delay for 1 second.
  19450.  
  19451.         LD      BC,$0000         ; Delay for 480.4us
  19452.  
  19453. ;; TVT_DELAY1
  19454. L3C83:  DEC     BC
  19455.         LD      A,B
  19456.         OR      C
  19457.         JR      NZ,L3C83
  19458.  
  19459. ;; TVT_DELAY2
  19460. L3C88:  DEC     BC               ; Delay for 480.4us
  19461.         LD      A,B
  19462.         OR      C
  19463.         JR      NZ,L3C88
  19464.  
  19465.         JR      L3C68            ; Repeat the tone cycle
  19466.  
  19467. ;; TVT_DATA
  19468. L3C8F:  DB    $13, $00         ; Bright, off
  19469.         DB    $31, $39         ; '1', '9'
  19470.         DB    $13, $01         ; Bright, on
  19471.         DB    $38, $36         ; '8', '6'
  19472.  
  19473.                 include new_proc.a80
  19474.  
  19475. ; ------
  19476. ; UNUSED
  19477. ; ------
  19478.  
  19479.                 DUPL 0X3D00-$,0
  19480.  
  19481. ; -------------------------------
  19482. ; THE 'ZX SPECTRUM CHARACTER SET'
  19483. ; -------------------------------
  19484.  
  19485. ;; char-set
  19486.  
  19487. ; $20 - Character: ' '          CHR$(32)
  19488.  
  19489. CHARS           binclude shr_3d00.bin
  19490.