Subversion Repositories pentevo

Rev

Rev 548 | Rev 585 | 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: 01.01.2012 savelij
  5.  
  6.                 include rst8_equ.a80
  7.  
  8. TAP_EMU_BORDER  EQU 0
  9.  
  10. ; **************************************
  11. ; *** SPECTRUM 128 ROM 1 DISASSEMBLY ***
  12. ; **************************************
  13.  
  14. ; The Spectrum ROMs are copyright Amstrad, who have kindly given permission
  15. ; to reverse engineer and publish ROM disassemblies.
  16.  
  17.  
  18. ; =====
  19. ; NOTES
  20. ; =====
  21.  
  22. ; ------------
  23. ; Release Date
  24. ; ------------
  25. ; 23rd May 2009
  26.  
  27.  
  28. ; =================
  29. ; ASSEMBLER DEFINES
  30. ; =================
  31.  
  32. ;TASM directives:
  33.  
  34. ;#define DB .BYTE      
  35. ;#define DEFW .WORD
  36. ;#define DEFM .TEXT
  37. ;#DEFINE DEFS .FILL
  38. ;#define END  .END
  39. ;#define EQU  .EQU
  40. ;#define ORG  .ORG
  41.  
  42. ; The Sinclair Interface1 ROM written by Dr. Ian Logan calls numerous
  43. ; routines in this ROM. Non-standard entry points have a label beginning
  44. ; with X.
  45.  
  46.         ORG     $0000
  47.  
  48. ;*****************************************
  49. ;** Part 1. RESTART ROUTINES AND TABLES **
  50. ;*****************************************
  51.  
  52. ; -----------
  53. ; THE 'START'
  54. ; -----------
  55. ; At switch on, the Z80 chip is in interrupt mode 0.
  56. ; This location can also be 'called' to reset the machine.
  57. ; Typically with PRINT USR 0.
  58.  
  59. ;; START
  60. L0000:  DI                      ; disable interrupts.
  61.         XOR     A               ; signal coming from START.
  62.         LD      DE,$FFFF        ; top of possible physical RAM.
  63.         JP      L11CB           ; jump forward to common code at START-NEW.
  64.  
  65. ; -------------------
  66. ; THE 'ERROR' RESTART
  67. ; -------------------
  68. ; The error pointer is made to point to the position of the error to enable
  69. ; the editor to show the error if it occurred during syntax checking.
  70. ; It is used at 37 places in the program.
  71. ; An instruction fetch on address $0008 may page in a peripheral ROM
  72. ; such as the Sinclair Interface 1 or Disciple Disk Interface.
  73. ; This was not however an original design concept and not all errors pass
  74. ; through here.
  75.  
  76. ;; ERROR-1
  77. L0008           LD B,B
  78.                 LD B,B
  79.                 JP RST8_CMP
  80.  
  81.                 LD HL,($5C5D)           ; fetch the character address from CH_ADD.
  82. ;               LD ($5C5F),HL           ; copy it to the error pointer X_PTR.
  83. ;               JR L0053                ; forward to continue at ERROR-2.
  84.  
  85. ; -----------------------------
  86. ; THE 'PRINT CHARACTER' RESTART
  87. ; -----------------------------
  88. ; The A register holds the code of the character that is to be sent to
  89. ; the output stream of the current channel.
  90. ; The alternate register set is used to output a character in the A register
  91. ; so there is no need to preserve any of the current registers (HL,DE,BC).
  92. ; This restart is used 21 times.
  93.  
  94. ;; PRINT-A
  95. L0010:  JP      L15F2           ; jump forward to continue at PRINT-A-2.
  96.  
  97. ; ---
  98.  
  99.                 DUPL ADR_SEL_ROM-$,0XFF
  100. L0014           OUT (C),A
  101.                 NOP
  102.                 RET
  103.  
  104.                 DUPL 0X0018-$,0XFF
  105.  
  106. ;        DB    $FF             ; this byte is used by the SPECTRUM command in
  107.                                 ; ROM 0 to generate an error report "0 OK".
  108. ;        DB    $FF, $FF        ; four unused locations.
  109. ;        DB    $FF, $FF        ;
  110.  
  111. ; -------------------------------
  112. ; THE 'COLLECT CHARACTER' RESTART
  113. ; -------------------------------
  114. ; The contents of the location currently addressed by CH_ADD are fetched.
  115. ; A return is made if the value represents a character that has
  116. ; relevance to the BASIC parser. Otherwise CH_ADD is incremented and the
  117. ; tests repeated. CH_ADD will be addressing somewhere -
  118. ; 1) in the BASIC program area during line execution.
  119. ; 2) in workspace if evaluating, for example, a string expression.
  120. ; 3) in the edit buffer if parsing a direct command or a new BASIC line.
  121. ; 4) in workspace if accepting input but not that from INPUT LINE.
  122.  
  123. ;; GET-CHAR
  124. L0018:  LD      HL,($5C5D)      ; fetch the address from CH_ADD.
  125.         LD      A,(HL)          ; use it to pick up current character.
  126.  
  127. ;; TEST-CHAR
  128. L001C:  CALL    L007D           ; routine SKIP-OVER tests if the character
  129.         RET     NC              ; is relevant. Return if it is so.
  130.  
  131. ; ------------------------------------
  132. ; THE 'COLLECT NEXT CHARACTER' RESTART
  133. ; ------------------------------------
  134. ; As the BASIC commands and expressions are interpreted, this routine is
  135. ; called repeatedly to step along the line. It is used 83 times.
  136.  
  137. ;; NEXT-CHAR
  138. L0020:  CALL    L0074           ; routine CH-ADD+1 fetches the next immediate
  139.                                 ; character.
  140.         JR      L001C           ; jump back to TEST-CHAR until a valid
  141.                                 ; character is found.
  142.  
  143. ; ---
  144.  
  145.         DB    $FF, $FF, $FF   ; unused
  146.  
  147. ; -----------------------
  148. ; THE 'CALCULATE' RESTART
  149. ; -----------------------
  150. ; This restart enters the Spectrum's internal, floating-point,
  151. ; stack-based, FORTH-like language.
  152. ; It is further used recursively from within the calculator.
  153. ; It is used on 77 occasions.
  154.  
  155. ;; FP-CALC
  156. L0028:  JP      L335B           ; jump forward to the CALCULATE routine.
  157.  
  158. ; ---
  159.  
  160.         DB    $FF, $FF, $FF   ; spare - note that on the ZX81, space being a
  161.         DB    $FF, $FF        ; little cramped, these same locations were
  162.                                 ; used for the five-byte end-calc literal.
  163.  
  164. ; ------------------------------
  165. ; THE 'CREATE BC SPACES' RESTART
  166. ; ------------------------------
  167. ; This restart is used on only 12 occasions to create BC spaces
  168. ; between workspace and the calculator stack.
  169.  
  170. ;; BC-SPACES
  171. L0030:  PUSH    BC              ; save number of spaces.
  172.         LD      HL,($5C61)      ; fetch WORKSP.
  173.         PUSH    HL              ; save address of workspace.
  174.         JP      L169E           ; jump forward to continuation code RESERVE.
  175.  
  176. ; --------------------------------
  177. ; THE 'MASKABLE INTERRUPT' ROUTINE
  178. ; --------------------------------
  179. ; This routine increments the Spectrum's three-byte FRAMES counter
  180. ; fifty times a second (sixty times a second in the USA ).
  181. ; Both this routine and the called KEYBOARD subroutine use
  182. ; the IY register to access system variables and flags so a user-written
  183. ; program must disable interrupts to make use of the IY register.
  184.  
  185. ;; MASK-INT
  186. L0038:  PUSH    AF              ; save the registers.
  187.         PUSH    HL              ; but not IY unfortunately.
  188.         LD      HL,($5C78)      ; fetch two bytes at FRAMES1.
  189.         INC     HL              ; increment lowest two bytes of counter.
  190.         LD      ($5C78),HL      ; place back in FRAMES1.
  191.         LD      A,H             ; test if the result
  192.         OR      L               ; was zero.
  193.         JR      NZ,L0048        ; forward to KEY-INT if not.
  194.  
  195.         INC     (IY+$40)        ; otherwise increment FRAMES3 the third byte.
  196.  
  197. ; now save the rest of the main registers and read and decode the keyboard.
  198.  
  199. ;; KEY-INT
  200. L0048:  PUSH    BC              ; save the other
  201.         PUSH    DE              ; main registers.
  202.  
  203.                 IF BAS48_ONLY=1
  204.                 CALL L02BF
  205.                 ELSE
  206.                 CALL L386E              ; Spectrum 128 patch: read the keypad and keyboard
  207.                                         ; in the process of reading a key-press.
  208.                 ENDIF
  209.  
  210. L004D:  POP     DE              ;
  211.         POP     BC              ; restore registers.
  212.  
  213.         POP     HL              ;
  214.         POP     AF              ;
  215.         EI                      ; enable interrupts.
  216.         RET                     ; return.
  217.  
  218. ; ---------------------
  219. ; THE 'ERROR-2' ROUTINE
  220. ; ---------------------
  221. ; A continuation of the code at 0008.
  222. ; The error code is stored and after clearing down stacks,
  223. ; an indirect jump is made to MAIN-4, etc. to handle the error.
  224.  
  225. ;; ERROR-2
  226. L0053:  POP     HL              ; drop the return address - the location
  227.                                 ; after the RST 08H instruction.
  228.         LD      L,(HL)          ; fetch the error code that follows.
  229.                                 ; (nice to see this instruction used.)
  230.  
  231. ; Note. this entry point is used when out of memory at REPORT-4.
  232. ; The L register has been loaded with the report code but X-PTR is not
  233. ; updated.
  234.  
  235. ;; ERROR-3
  236. L0055:  LD      (IY+$00),L      ; store it in the system variable ERR_NR.
  237.         LD      SP,($5C3D)      ; ERR_SP points to an error handler on the
  238.                                 ; machine stack. There may be a hierarchy
  239.                                 ; of routines.
  240.                                 ; to MAIN-4 initially at base.
  241.                                 ; or REPORT-G on line entry.
  242.                                 ; or  ED-ERROR when editing.
  243.                                 ; or   ED-FULL during ed-enter.
  244.                                 ; or  IN-VAR-1 during runtime input etc.
  245.  
  246.         JP      L16C5           ; jump to SET-STK to clear the calculator
  247.                                 ; stack and reset MEM to usual place in the
  248.                                 ; systems variables area.
  249.                                 ; and then indirectly to MAIN-4, etc.
  250.  
  251. ; ---
  252.  
  253. ;        DB    $FF, $FF, $FF   ; unused locations
  254. ;        DB    $FF, $FF, $FF   ; before the fixed-position
  255. ;        DB    $FF             ; NMI routine.
  256.  
  257. L0070           POP HL          ; restore the
  258.                 POP AF          ; registers.
  259.                 RETN            ; return to previous interrupt state.
  260.  
  261. ; ------------------------------------
  262. ; THE 'NON-MASKABLE INTERRUPT' ROUTINE
  263. ; ------------------------------------
  264. ; There is no NMI switch on the standard Spectrum.
  265. ; When activated, a location in the system variables is tested
  266. ; and if the contents are zero a jump made to that location else
  267. ; a return is made. Perhaps a disabled development feature but
  268. ; if the logic was reversed, no program would be safe from
  269. ; copy-protection and the Spectrum would have had no software base.
  270. ; The location NMIADD was later used by Interface 1 for other purposes.
  271. ; On later Spectrums, and the Brazilian Spectrum, the logic of this
  272. ; routine was reversed.
  273.  
  274. ;; RESET
  275.                 DUPL 0X0066-$,0XFF
  276. L0066           NOP
  277.                 PUSH AF         ; save the
  278.                 PUSH HL         ; registers.
  279.                 LD HL,($5CB0)   ; fetch the system variable NMIADD.
  280.                 LD A,H          ; test address
  281.                 OR L            ; for zero.
  282. ;               JR NZ,L0070     ; skip to NO-RESET if NOT ZERO
  283.                 JR Z,L0070
  284.                 JP (HL)         ; jump to routine ( i.e. L0000 )
  285.  
  286. ;; NO-RESET
  287. ;L0070          POP HL          ; restore the
  288. ;               POP AF          ; registers.
  289. ;               RETN            ; return to previous interrupt state.
  290.  
  291. ; ---------------------------
  292. ; THE 'CH ADD + 1' SUBROUTINE
  293. ; ---------------------------
  294. ; This subroutine is called from RST 20, and three times from elsewhere
  295. ; to fetch the next immediate character following the current valid character
  296. ; address and update the associated system variable.
  297. ; The entry point TEMP-PTR1 is used from the SCANNING routine.
  298. ; Both TEMP-PTR1 and TEMP-PTR2 are used by the READ command routine.
  299.  
  300. ;; CH-ADD+1
  301.                 DUPL 0X0074-$,0XFF
  302. L0074:  LD      HL,($5C5D)      ; fetch address from CH_ADD.
  303.  
  304. ;; TEMP-PTR1
  305. L0077:  INC     HL              ; increase the character address by one.
  306.  
  307. ;; TEMP-PTR2
  308. L0078:  LD      ($5C5D),HL      ; update CH_ADD with character address.
  309.  
  310. X007B:  LD      A,(HL)          ; load character to A from HL.
  311.         RET                     ; and return.
  312.  
  313. ; --------------------------
  314. ; THE 'SKIP OVER' SUBROUTINE
  315. ; --------------------------
  316. ; This subroutine is called once from RST 18 to skip over white-space and
  317. ; other characters irrelevant to the parsing of a BASIC line etc. .
  318. ; Initially the A register holds the character to be considered
  319. ; and HL holds its address which will not be within quoted text
  320. ; when a BASIC line is parsed.
  321. ; Although the 'tab' and 'at' characters will not appear in a BASIC line,
  322. ; they could be present in a string expression, and in other situations.
  323. ; Note. although white-space is usually placed in a program to indent loops
  324. ; and make it more readable, it can also be used for the opposite effect and
  325. ; spaces may appear in variable names although the parser never sees them.
  326. ; It is this routine that helps make the variables 'Anum bEr5 3BUS' and
  327. ; 'a number 53 bus' appear the same to the parser.
  328.  
  329. ;; SKIP-OVER
  330. L007D:  CP      $21             ; test if higher than space.
  331.         RET     NC              ; return with carry clear if so.
  332.  
  333.         CP      $0D             ; carriage return ?
  334.         RET     Z               ; return also with carry clear if so.
  335.  
  336.                                 ; all other characters have no relevance
  337.                                 ; to the parser and must be returned with
  338.                                 ; carry set.
  339.  
  340.         CP      $10             ; test if 0-15d
  341.         RET     C               ; return, if so, with carry set.
  342.  
  343.         CP      $18             ; test if 24-32d
  344.         CCF                     ; complement carry flag.
  345.         RET     C               ; return with carry set if so.
  346.  
  347.                                 ; now leaves 16d-23d
  348.  
  349.         INC     HL              ; all above have at least one extra character
  350.                                 ; to be stepped over.
  351.  
  352.         CP      $16             ; controls 22d ('at') and 23d ('tab') have two.
  353.         JR      C,L0090         ; forward to SKIPS with ink, paper, flash,
  354.                                 ; bright, inverse or over controls.
  355.                                 ; Note. the high byte of tab is for RS232 only.
  356.                                 ; it has no relevance on this machine.
  357.  
  358.         INC     HL              ; step over the second character of 'at'/'tab'.
  359.  
  360. ;; SKIPS
  361. L0090:  SCF                     ; set the carry flag
  362.         LD      ($5C5D),HL      ; update the CH_ADD system variable.
  363.         RET                     ; return with carry set.
  364.  
  365.  
  366. ; ------------------
  367. ; THE 'TOKEN TABLES'
  368. ; ------------------
  369. ; The tokenized characters 134d (RND) to 255d (COPY) are expanded using
  370. ; this table. The last byte of a token is inverted to denote the end of
  371. ; the word. The first is an inverted step-over byte.
  372.  
  373. ;; TKN-TABLE
  374. L0095           DC "?"          ;DB    '?'+$80
  375.                 DC "RND"        ;DEFM    "RN"
  376.                                 ;DB    'D'+$80
  377.                 DC "INKEY$"     ;DEFM    "INKEY"
  378.                                 ;DB    '$'+$80
  379.                 DC "PI"         ;DB    'P','I'+$80
  380.                 DC "FN"         ;DB    'F','N'+$80
  381.                 DC "POINT"      ;DEFM    "POIN"
  382.                                 ;DB    'T'+$80
  383.                 DC "SCREEN$"    ;DEFM    "SCREEN"
  384.                                 ;DB    '$'+$80
  385.                 DC "ATTR"       ;DEFM    "ATT"
  386.                                 ;DB    'R'+$80
  387.                 DC "AT"         ;DB    'A','T'+$80
  388.                 DC "TAB"        ;DEFM    "TA"
  389.                                 ;DB    'B'+$80
  390.                 DC "VAL$"       ;DEFM    "VAL"
  391.                                 ;DB    '$'+$80
  392.                 DC "CODE"       ;DEFM    "COD"
  393.                                 ;DB    'E'+$80
  394.                 DC "VAL"        ;DEFM    "VA"
  395.                                 ;DB    'L'+$80
  396.                 DC "LEN"        ;DEFM    "LE"
  397.                                 ;DB    'N'+$80
  398.                 DC "SIN"        ;DEFM    "SI"
  399.                                 ;DB    'N'+$80
  400.                 DC "COS"        ;DEFM    "CO"
  401.                                 ;DB    'S'+$80
  402.                 DC "TAN"        ;DEFM    "TA"
  403.                                 ;DB    'N'+$80
  404.                 DC "ASN"        ;DEFM    "AS"
  405.                                 ;DB    'N'+$80
  406.                 DC "ACS"        ;DEFM    "AC"
  407.                                 ;DB    'S'+$80
  408.                 DC "ATN"        ;DEFM    "AT"
  409.                                 ;DB    'N'+$80
  410.                 DC "LN"         ;DB    'L','N'+$80
  411.                 DC "EXP"        ;DEFM    "EX"
  412.                                 ;DB    'P'+$80
  413.                 DC "INT"        ;DEFM    "IN"
  414.                                 ;DB    'T'+$80
  415.                 DC "SQR"        ;DEFM    "SQ"
  416.                                 ;DB    'R'+$80
  417.                 DC "SGN"        ;DEFM    "SG"
  418.                                 ;DB    'N'+$80
  419.                 DC "ABS"        ;DEFM    "AB"
  420.                                 ;DB    'S'+$80
  421.                 DC "PEEK"       ;DEFM    "PEE"
  422.                                 ;DB    'K'+$80
  423.                 DC "IN"         ;DB    'I','N'+$80
  424.                 DC "USR"        ;DEFM    "US"
  425.                                 ;DB    'R'+$80
  426.                 DC "STR$"       ;DEFM    "STR"
  427.                                 ;DB    '$'+$80
  428.                 DC "CHR$"       ;DEFM    "CHR"
  429.                                 ;DB    '$'+$80
  430.                 DC "NOT"        ;DEFM    "NO"
  431.                                 ;DB    'T'+$80
  432.                 DC "BIN"        ;DEFM    "BI"
  433.                                 ;DB    'N'+$80
  434.  
  435. ;   The previous 32 function-type words are printed without a leading space
  436. ;   The following have a leading space if they begin with a letter
  437.  
  438.                 DC "OR"         ;DB    'O','R'+$80
  439.                 DC "AND"        ;DEFM    "AN"
  440.                                 ;DB    'D'+$80
  441.                 DC "<="         ;DB    $3C,'='+$80             ; <=
  442.                 DC ">="         ;DB    $3E,'='+$80             ; >=
  443.                 DC "<>"         ;DB    $3C,$3E+$80             ; <>
  444.                 DC "LINE"       ;DEFM    "LIN"
  445.                                 ;DB    'E'+$80
  446.                 DC "THEN"       ;DEFM    "THE"
  447.                                 ;DB    'N'+$80
  448.                 DC "TO"         ;DB    'T','O'+$80
  449.                 DC "STEP"       ;DEFM    "STE"
  450.                                 ;DB    'P'+$80
  451.                 DC "DEF FN"     ;DEFM    "DEF F"
  452.                                 ;DB    'N'+$80
  453.                 DC "CAT"        ;DEFM    "CA"
  454.                                 ;DB    'T'+$80
  455.                 DC "FORMAT"     ;DEFM    "FORMA"
  456.                                 ;DB    'T'+$80
  457.                 DC "MOVE"       ;DEFM    "MOV"
  458.                                 ;DB    'E'+$80
  459.                 DC "ERASE"      ;DEFM    "ERAS"
  460.                                 ;DB    'E'+$80
  461.                 DC "OPEN #"     ;DEFM    "OPEN "
  462.                                 ;DB    '#'+$80
  463.                 DC "CLOSE #"    ;DEFM    "CLOSE "
  464.                                 ;DB    '#'+$80
  465.                 DC "MERGE"      ;DEFM    "MERG"
  466.                                 ;DB    'E'+$80
  467.                 DC "VERIFY"     ;DEFM    "VERIF"
  468.                                 ;DB    'Y'+$80
  469.                 DC "BEEP"       ;DEFM    "BEE"
  470.                                 ;DB    'P'+$80
  471.                 DC "CIRCLE"     ;DEFM    "CIRCL"
  472.                                 ;DB    'E'+$80
  473.                 DC "INK"        ;DEFM    "IN"
  474.                                 ;DB    'K'+$80
  475.                 DC "PAPER"      ;DEFM    "PAPE"
  476.                                 ;DB    'R'+$80
  477.                 DC "FLASH"      ;DEFM    "FLAS"
  478.                                 ;DB    'H'+$80
  479.                 DC "BRIGHT"     ;DEFM    "BRIGH"
  480.                                 ;DB    'T'+$80
  481.                 DC "INVERSE"    ;DEFM    "INVERS"
  482.                                 ;DB    'E'+$80
  483.                 DC "OVER"       ;DEFM    "OVE"
  484.                                 ;DB    'R'+$80
  485.                 DC "OUT"        ;DEFM    "OU"
  486.                                 ;DB    'T'+$80
  487.                 DC "LPRINT"     ;DEFM    "LPRIN"
  488.                                 ;DB    'T'+$80
  489.                 DC "LLIST"      ;DEFM    "LLIS"
  490.                                 ;DB    'T'+$80
  491.                 DC "STOP"       ;DEFM    "STO"
  492.                                 ;DB    'P'+$80
  493.                 DC "READ"       ;DEFM    "REA"
  494.                                 ;DB    'D'+$80
  495.                 DC "DATA"       ;DEFM    "DAT"
  496.                                 ;DB    'A'+$80
  497.                 DC "RESTORE"    ;DEFM    "RESTOR"
  498.                                 ;DB    'E'+$80
  499.                 DC "NEW"        ;DEFM    "NE"
  500.                                 ;DB    'W'+$80
  501.                 DC "BORDER"     ;DEFM    "BORDE"
  502.                                 ;DB    'R'+$80
  503.                 DC "CONTINUE"   ;DEFM    "CONTINU"
  504.                                 ;DB    'E'+$80
  505.                 DC "DIM"        ;DEFM    "DI"
  506.                                 ;DB    'M'+$80
  507.                 DC "REM"        ;DEFM    "RE"
  508.                                 ;DB    'M'+$80
  509.                 DC "FOR"        ;DEFM    "FO"
  510.                                 ;DB    'R'+$80
  511.                 DC "GO TO"      ;DEFM    "GO T"
  512.                                 ;DB    'O'+$80
  513.                 DC "GO SUB"     ;DEFM    "GO SU"
  514.                                 ;DB    'B'+$80
  515.                 DC "INPUT"      ;DEFM    "INPU"
  516.                                 ;DB    'T'+$80
  517.                 DC "LOAD"       ;DEFM    "LOA"
  518.                                 ;DB    'D'+$80
  519.                 DC "LIST"       ;DEFM    "LIS"
  520.                                 ;DB    'T'+$80
  521.                 DC "LET"        ;DEFM    "LE"
  522.                                 ;DB    'T'+$80
  523.                 DC "PAUSE"      ;DEFM    "PAUS"
  524.                                 ;DB    'E'+$80
  525.                 DC "NEXT"       ;DEFM    "NEX"
  526.                                 ;DB    'T'+$80
  527.                 DC "POKE"       ;DEFM    "POK"
  528.                                 ;DB    'E'+$80
  529.                 DC "PRINT"      ;DEFM    "PRIN"
  530.                                 ;DB    'T'+$80
  531.                 DC "PLOT"       ;DEFM    "PLO"
  532.                                 ;DB    'T'+$80
  533.                 DC "RUN"        ;DEFM    "RU"
  534.                                 ;DB    'N'+$80
  535.                 DC "SAVE"       ;DEFM    "SAV"
  536.                                 ;DB    'E'+$80
  537.                 DC "RANDOMIZE"  ;DEFM    "RANDOMIZ"
  538.                                 ;DB    'E'+$80
  539.                 DC "IF"         ;DB    'I','F'+$80
  540.                 DC "CLS"        ;DEFM    "CL"
  541.                                 ;DB    'S'+$80
  542.                 DC "DRAW"       ;DEFM    "DRA"
  543.                                 ;DB    'W'+$80
  544.                 DC "CLEAR"      ;DEFM    "CLEA"
  545.                                 ;DB    'R'+$80
  546.                 DC "RETURN"     ;DEFM    "RETUR"
  547.                                 ;DB    'N'+$80
  548.                 DC "COPY"       ;DEFM    "COP"
  549.                                 ;DB    'Y'+$80
  550.  
  551. ; ----------------
  552. ; THE 'KEY' TABLES
  553. ; ----------------
  554. ; These six look-up tables are used by the keyboard reading routine
  555. ; to decode the key values.
  556.  
  557. ; The first table contains the maps for the 39 keys of the standard
  558. ; 40-key Spectrum keyboard. The remaining key [SHIFT $27] is read directly.
  559. ; The keys consist of the 26 upper-case alphabetic characters, the 10 digit
  560. ; keys and the space, ENTER and symbol shift key.
  561. ; Unshifted alphabetic keys have $20 added to the value.
  562. ; The keywords for the main alphabetic keys are obtained by adding $A5 to
  563. ; the values obtained from this table.
  564.  
  565. ;; MAIN-KEYS
  566. L0205:  DB    $42             ; B
  567.         DB    $48             ; H
  568.         DB    $59             ; Y
  569.         DB    $36             ; 6
  570.         DB    $35             ; 5
  571.         DB    $54             ; T
  572.         DB    $47             ; G
  573.         DB    $56             ; V
  574.         DB    $4E             ; N
  575.         DB    $4A             ; J
  576.         DB    $55             ; U
  577.         DB    $37             ; 7
  578.         DB    $34             ; 4
  579.         DB    $52             ; R
  580.         DB    $46             ; F
  581.         DB    $43             ; C
  582.         DB    $4D             ; M
  583.         DB    $4B             ; K
  584.         DB    $49             ; I
  585.         DB    $38             ; 8
  586.         DB    $33             ; 3
  587.         DB    $45             ; E
  588.         DB    $44             ; D
  589.         DB    $58             ; X
  590.         DB    $0E             ; SYMBOL SHIFT
  591.         DB    $4C             ; L
  592.         DB    $4F             ; O
  593.         DB    $39             ; 9
  594.         DB    $32             ; 2
  595.         DB    $57             ; W
  596.         DB    $53             ; S
  597.         DB    $5A             ; Z
  598.         DB    $20             ; SPACE
  599.         DB    $0D             ; ENTER
  600.         DB    $50             ; P
  601.         DB    $30             ; 0
  602.         DB    $31             ; 1
  603.         DB    $51             ; Q
  604.         DB    $41             ; A
  605.  
  606.  
  607. ;; E-UNSHIFT
  608. ;  The 26 unshifted extended mode keys for the alphabetic characters.
  609. ;  The green keywords on the original keyboard.
  610. L022C:  DB    $E3             ; READ
  611.         DB    $C4             ; BIN
  612.         DB    $E0             ; LPRINT
  613.         DB    $E4             ; DATA
  614.         DB    $B4             ; TAN
  615.         DB    $BC             ; SGN
  616.         DB    $BD             ; ABS
  617.         DB    $BB             ; SQR
  618.         DB    $AF             ; CODE
  619.         DB    $B0             ; VAL
  620.         DB    $B1             ; LEN
  621.         DB    $C0             ; USR
  622.         DB    $A7             ; PI
  623.         DB    $A6             ; INKEY$
  624.         DB    $BE             ; PEEK
  625.         DB    $AD             ; TAB
  626.         DB    $B2             ; SIN
  627.         DB    $BA             ; INT
  628.         DB    $E5             ; RESTORE
  629.         DB    $A5             ; RND
  630.         DB    $C2             ; CHR$
  631.         DB    $E1             ; LLIST
  632.         DB    $B3             ; COS
  633.         DB    $B9             ; EXP
  634.         DB    $C1             ; STR$
  635.         DB    $B8             ; LN
  636.  
  637.  
  638. ;; EXT-SHIFT
  639. ;  The 26 shifted extended mode keys for the alphabetic characters.
  640. ;  The red keywords below keys on the original keyboard.
  641. L0246:  DB    $7E             ; ~
  642.         DB    $DC             ; BRIGHT
  643.         DB    $DA             ; PAPER
  644.         DB    $5C             ;
  645.         DB    $B7             ; ATN
  646.         DB    $7B             ; {
  647.         DB    $7D             ; }
  648.         DB    $D8             ; CIRCLE
  649.         DB    $BF             ; IN
  650.         DB    $AE             ; VAL$
  651.         DB    $AA             ; SCREEN$
  652.         DB    $AB             ; ATTR
  653.         DB    $DD             ; INVERSE
  654.         DB    $DE             ; OVER
  655.         DB    $DF             ; OUT
  656.         DB    $7F             ; (Copyright character)
  657.         DB    $B5             ; ASN
  658.         DB    $D6             ; VERIFY
  659.         DB    $7C             ; |
  660.         DB    $D5             ; MERGE
  661.         DB    $5D             ; ]
  662.         DB    $DB             ; FLASH
  663.         DB    $B6             ; ACS
  664.         DB    $D9             ; INK
  665.         DB    $5B             ; [
  666.         DB    $D7             ; BEEP
  667.  
  668.  
  669. ;; CTL-CODES
  670. ;  The ten control codes assigned to the top line of digits when the shift
  671. ;  key is pressed.
  672. L0260:  DB    $0C             ; DELETE
  673.         DB    $07             ; EDIT
  674.         DB    $06             ; CAPS LOCK
  675.         DB    $04             ; TRUE VIDEO
  676.         DB    $05             ; INVERSE VIDEO
  677.         DB    $08             ; CURSOR LEFT
  678.         DB    $0A             ; CURSOR DOWN
  679.         DB    $0B             ; CURSOR UP
  680.         DB    $09             ; CURSOR RIGHT
  681.         DB    $0F             ; GRAPHICS
  682.  
  683.  
  684. ;; SYM-CODES
  685. ;  The 26 red symbols assigned to the alphabetic characters of the keyboard.
  686. ;  The ten single-character digit symbols are converted without the aid of
  687. ;  a table using subtraction and minor manipulation.
  688. L026A:  DB    $E2             ; STOP
  689.         DB    $2A             ; *
  690.         DB    $3F             ; ?
  691.         DB    $CD             ; STEP
  692.         DB    $C8             ; >=
  693.         DB    $CC             ; TO
  694.         DB    $CB             ; THEN
  695.         DB    $5E             ; ^
  696.         DB    $AC             ; AT
  697.         DB    $2D             ; -
  698.         DB    $2B             ; +
  699.         DB    $3D             ; =
  700.         DB    $2E             ; .
  701.         DB    $2C             ; ,
  702.         DB    $3B             ; ;
  703.         DB    $22             ; "
  704.         DB    $C7             ; <=
  705.         DB    $3C             ; <
  706.         DB    $C3             ; NOT
  707.         DB    $3E             ; >
  708.         DB    $C5             ; OR
  709.         DB    $2F             ; /
  710.         DB    $C9             ; <>
  711.         DB    $60             ; pound
  712.         DB    $C6             ; AND
  713.         DB    $3A             ; :
  714.  
  715. ;; E-DIGITS
  716. ;  The ten keywords assigned to the digits in extended mode.
  717. ;  The remaining red keywords below the keys.
  718. L0284:  DB    $D0             ; FORMAT
  719.         DB    $CE             ; DEF FN
  720.         DB    $A8             ; FN
  721.         DB    $CA             ; LINE
  722.         DB    $D3             ; OPEN#
  723.         DB    $D4             ; CLOSE#
  724.         DB    $D1             ; MOVE
  725.         DB    $D2             ; ERASE
  726.         DB    $A9             ; POINT
  727.         DB    $CF             ; CAT
  728.  
  729.  
  730. ;*******************************
  731. ;** Part 2. KEYBOARD ROUTINES **
  732. ;*******************************
  733.  
  734. ; Using shift keys and a combination of modes the Spectrum 40-key keyboard
  735. ; can be mapped to 256 input characters
  736.  
  737. ; ---------------------------------------------------------------------------
  738. ;
  739. ;         0     1     2     3     4 -Bits-  4     3     2     1     0
  740. ; PORT                                                                    PORT
  741. ;
  742. ; F7FE  [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]  |  [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 0 ]   EFFE
  743. ;  ^                                   |                                   v
  744. ; FBFE  [ Q ] [ W ] [ E ] [ R ] [ T ]  |  [ Y ] [ U ] [ I ] [ O ] [ P ]   DFFE
  745. ;  ^                                   |                                   v
  746. ; FDFE  [ A ] [ S ] [ D ] [ F ] [ G ]  |  [ H ] [ J ] [ K ] [ L ] [ ENT ] BFFE
  747. ;  ^                                   |                                   v
  748. ; FEFE  [SHI] [ Z ] [ X ] [ C ] [ V ]  |  [ B ] [ N ] [ M ] [sym] [ SPC ] 7FFE
  749. ;  ^     $27                                                 $18           v
  750. ; Start                                                                   End
  751. ;        00100111                                            00011000
  752. ;
  753. ; ---------------------------------------------------------------------------
  754. ; The above map may help in reading.
  755. ; The neat arrangement of ports means that the B register need only be
  756. ; rotated left to work up the left hand side and then down the right
  757. ; hand side of the keyboard. When the reset bit drops into the carry
  758. ; then all 8 half-rows have been read. Shift is the first key to be
  759. ; read. The lower six bits of the shifts are unambiguous.
  760.  
  761. ; -------------------------------
  762. ; THE 'KEYBOARD SCANNING' ROUTINE
  763. ; -------------------------------
  764. ; from keyboard and s-inkey$
  765. ; returns 1 or 2 keys in DE, most significant shift first if any
  766. ; key values 0-39 else 255
  767.  
  768. ;; KEY-SCAN
  769. L028E:  LD      L,$2F           ; initial key value
  770.                                 ; valid values are obtained by subtracting
  771.                                 ; eight five times.
  772.         LD      DE,$FFFF        ; a buffer to receive 2 keys.
  773.  
  774.         LD      BC,$FEFE        ; the commencing port address
  775.                                 ; B holds 11111110 initially and is also
  776.                                 ; used to count the 8 half-rows
  777. ;; KEY-LINE
  778. L0296:  IN      A,(C)           ; read the port to A - bits will be reset
  779.                                 ; if a key is pressed else set.
  780.         CPL                     ; complement - pressed key-bits are now set
  781.         AND     $1F             ; apply 00011111 mask to pick up the
  782.                                 ; relevant set bits.
  783.  
  784.         JR      Z,L02AB         ; forward to KEY-DONE if zero and therefore
  785.                                 ; no keys pressed in row at all.
  786.  
  787.         LD      H,A             ; transfer row bits to H
  788.         LD      A,L             ; load the initial key value to A
  789.  
  790. ;; KEY-3KEYS
  791. L029F:  INC     D               ; now test the key buffer
  792.         RET     NZ              ; if we have collected 2 keys already
  793.                                 ; then too many so quit.
  794.  
  795. ;; KEY-BITS
  796. L02A1:  SUB     $08             ; subtract 8 from the key value
  797.                                 ; cycling through key values (top = $27)
  798.                                 ; e.g. 2F>   27>1F>17>0F>07
  799.                                 ;      2E>   26>1E>16>0E>06
  800.         SRL     H               ; shift key bits right into carry.
  801.         JR      NC,L02A1        ; back to KEY-BITS if not pressed
  802.                                 ; but if pressed we have a value (0-39d)
  803.  
  804.         LD      D,E             ; transfer a possible previous key to D
  805.         LD      E,A             ; transfer the new key to E
  806.         JR      NZ,L029F        ; back to KEY-3KEYS if there were more
  807.                                 ; set bits - H was not yet zero.
  808.  
  809. ;; KEY-DONE
  810. L02AB:  DEC     L               ; cycles 2F>2E>2D>2C>2B>2A>29>28 for
  811.                                 ; each half-row.
  812.         RLC     B               ; form next port address e.g. FEFE > FDFE
  813.         JR      C,L0296         ; back to KEY-LINE if still more rows to do.
  814.  
  815.         LD      A,D             ; now test if D is still FF ?
  816.         INC     A               ; if it is zero we have at most 1 key
  817.                                 ; range now $01-$28  (1-40d)
  818.         RET     Z               ; return if one key or no key.
  819.  
  820.         CP      $28             ; is it capsshift (was $27) ?
  821.         RET     Z               ; return if so.
  822.  
  823.         CP      $19             ; is it symbol shift (was $18) ?
  824.         RET     Z               ; return also
  825.  
  826.         LD      A,E             ; now test E
  827.         LD      E,D             ; but first switch
  828.         LD      D,A             ; the two keys.
  829.         CP      $18             ; is it symbol shift ?
  830.         RET                     ; return (with zero set if it was).
  831.                                 ; but with symbol shift now in D
  832.  
  833. ; ------------------------------
  834. ; Scan keyboard and decode value
  835. ; ------------------------------
  836. ; from interrupt 50 times a second
  837. ;
  838.  
  839. ;; KEYBOARD
  840. L02BF:  CALL    L028E           ; routine KEY-SCAN
  841.         RET     NZ              ; return if invalid combinations
  842.  
  843. ; then decrease the counters within the two key-state maps
  844. ; as this could cause one to become free.
  845. ; if the keyboard has not been pressed during the last five interrupts
  846. ; then both sets will be free.
  847.  
  848.  
  849.         LD      HL,$5C00        ; point to KSTATE-0
  850.  
  851. ;; K-ST-LOOP
  852. L02C6:  BIT     7,(HL)          ; is it free ?  ($FF)
  853.         JR      NZ,L02D1        ; forward to K-CH-SET if so
  854.  
  855.         INC     HL              ; address 5-counter
  856.         DEC     (HL)            ; decrease counter
  857.         DEC     HL              ; step back
  858.         JR      NZ,L02D1        ; forward to K-CH-SET if not at end of count
  859.  
  860.         LD      (HL),$FF        ; else mark it free.
  861.  
  862. ;; K-CH-SET
  863. L02D1:  LD      A,L             ; store low address byte.
  864.         LD      HL,$5C04        ; point to KSTATE-4
  865.                                 ; (ld l, $04)
  866.         CP      L               ; have 2 been done ?
  867.         JR      NZ,L02C6        ; back to K-ST-LOOP to consider this 2nd set
  868.  
  869. ; now the raw key (0-38) is converted to a main key (uppercase).
  870.  
  871.         CALL    L031E           ; routine K-TEST to get main key in A
  872.         RET     NC              ; return if single shift
  873.  
  874.         LD      HL,$5C00        ; point to KSTATE-0
  875.         CP      (HL)            ; does it match ?
  876.         JR      Z,L0310         ; forward to K-REPEAT if so
  877.  
  878. ; if not consider the second key map.
  879.  
  880.         EX      DE,HL           ; save kstate-0 in de
  881.         LD      HL,$5C04        ; point to KSTATE-4
  882.         CP      (HL)            ; does it match ?
  883.         JR      Z,L0310         ; forward to K-REPEAT if so
  884.  
  885. ; having excluded a repeating key we can now consider a new key.
  886. ; the second set is always examined before the first.
  887.  
  888.         BIT     7,(HL)          ; is it free ?
  889.         JR      NZ,L02F1        ; forward to K-NEW if so.
  890.  
  891.         EX      DE,HL           ; bring back kstate-0
  892.         BIT     7,(HL)          ; is it free ?
  893.         RET     Z               ; return if not.
  894.                                 ; as we have a key but nowhere to put it yet.
  895.  
  896. ; continue or jump to here if one of the buffers was free.
  897.  
  898. ;; K-NEW
  899. L02F1:  LD      E,A             ; store key in E
  900.         LD      (HL),A          ; place in free location
  901.         INC     HL              ; advance to interrupt counter
  902.         LD      (HL),$05        ; and initialize to 5
  903.         INC     HL              ; advance to delay
  904.         LD      A,($5C09)       ; pick up system variable REPDEL
  905.         LD      (HL),A          ; and insert that for first repeat delay.
  906.         INC     HL              ; advance to last location of state map.
  907.  
  908.         LD      C,(IY+$07)      ; pick up MODE  (3 bytes)
  909.         LD      D,(IY+$01)      ; pick up FLAGS (3 bytes)
  910.         PUSH    HL              ; save state map location
  911.                                 ; Note. could now have used.
  912.                                 ; ld l,$41; ld c,(hl); ld l,$3B; ld d,(hl).
  913.                                 ; six and two threes of course.
  914.         CALL    L0333           ; routine K-DECODE
  915.         POP     HL              ; restore map pointer
  916.         LD      (HL),A          ; put decoded key in last location of map.
  917.  
  918. ;; K-END
  919. L0308:  LD      ($5C08),A       ; update LASTK system variable.
  920.         SET     5,(IY+$01)      ; update FLAGS  - signal new key.
  921.         RET                     ; done
  922.  
  923. ; ---------------------------
  924. ; THE 'REPEAT KEY' SUBROUTINE
  925. ; ---------------------------
  926. ; A possible repeat has been identified. HL addresses the raw (main) key.
  927. ; The last location holds the decoded key (from the first context).
  928.  
  929. ;; K-REPEAT
  930. L0310:  INC     HL              ; advance
  931.         LD      (HL),$05        ; maintain interrupt counter at 5
  932.         INC     HL              ; advance
  933.         DEC     (HL)            ; decrease REPDEL value.
  934.         RET     NZ              ; return if not yet zero.
  935.  
  936.         LD      A,($5C0A)       ; REPPER
  937.         LD      (HL),A          ; but for subsequent repeats REPPER will be used.
  938.         INC     HL              ; advance
  939.                                 ;
  940.         LD      A,(HL)          ; pick up the key decoded possibly in another
  941.                                 ; context.
  942.         JR      L0308           ; back to K-END
  943.  
  944. ; --------------
  945. ; Test key value
  946. ; --------------
  947. ; also called from s-inkey$
  948. ; begin by testing for a shift with no other.
  949.  
  950. ;; K-TEST
  951. L031E:  LD      B,D             ; load most significant key to B
  952.                                 ; will be $FF if not shift.
  953.         LD      D,$00           ; and reset D to index into main table
  954.         LD      A,E             ; load least significant key from E
  955.         CP      $27             ; is it higher than 39d   i.e. FF
  956.         RET     NC              ; return with just a shift (in B now)
  957.  
  958.         CP      $18             ; is it symbol shift ?
  959.         JR      NZ,L032C        ; forward to K-MAIN if not
  960.  
  961. ; but we could have just symbol shift and no other
  962.  
  963.         BIT     7,B             ; is other key $FF (ie not shift)
  964.         RET     NZ              ; return with solitary symbol shift
  965.  
  966.  
  967. ;; K-MAIN
  968. L032C:  LD      HL,L0205        ; address: MAIN-KEYS
  969.         ADD     HL,DE           ; add offset 0-38
  970.         LD      A,(HL)          ; pick up main key value
  971.         SCF                     ; set carry flag
  972.         RET                     ; return    (B has other key still)
  973.  
  974. ; -----------------
  975. ; Keyboard decoding
  976. ; -----------------
  977. ; also called from s-inkey$
  978.  
  979. ;; K-DECODE
  980. L0333:  LD      A,E             ; pick up the stored main key
  981.         CP      $3A             ; an arbitrary point between digits and letters
  982.         JR      C,L0367         ; forward to K-DIGIT with digits, space, enter.
  983.  
  984.         DEC     C               ; decrease MODE ( 0='KLC', 1='E', 2='G')
  985.  
  986.         JP      M,L034F         ; to K-KLC-LET if was zero
  987.  
  988.         JR      Z,L0341         ; to K-E-LET if was 1 for extended letters.
  989.  
  990. ; proceed with graphic codes.
  991. ; Note. should selectively drop return address if code > 'U' ($55).
  992. ; i.e. abort the KEYBOARD call.
  993. ; e.g. cp 'V'; jr c addit; pop af; ;;addit etc. (5 bytes of instruction).
  994. ; (s-inkey$ never gets into graphics mode.)
  995.  
  996. ;; addit
  997.         ADD     A,$4F           ; add offset to augment 'A' to graphics A say.
  998.         RET                     ; return.
  999.                                 ; Note. ( but [GRAPH] V gives RND, etc ).
  1000.  
  1001. ; ---
  1002.  
  1003. ; the jump was to here with extended mode with uppercase A-Z.
  1004.  
  1005. ;; K-E-LET
  1006. L0341:  LD      HL,L022C-$41    ; base address of E-UNSHIFT L022c
  1007.                                 ; ( $01EB in standard ROM )
  1008.         INC     B               ; test B is it empty i.e. not a shift
  1009.         JR      Z,L034A         ; forward to K-LOOK-UP if neither shift
  1010.  
  1011.         LD      HL,L0246-$41    ; Address: $0205 L0246-$41 EXT-SHIFT base
  1012.  
  1013. ;; K-LOOK-UP
  1014. L034A:  LD      D,$00           ; prepare to index
  1015.         ADD     HL,DE           ; add the main key value
  1016.         LD      A,(HL)          ; pick up other mode value
  1017.         RET                     ; return
  1018.  
  1019. ; ---
  1020.  
  1021. ; the jump was here with mode = 0
  1022.  
  1023. ;; K-KLC-LET
  1024. L034F:  LD      HL,L026A-$41    ; prepare base of sym-codes
  1025.         BIT     0,B             ; shift=$27 sym-shift=$18
  1026.         JR      Z,L034A         ; back to K-LOOK-UP with symbol-shift
  1027.  
  1028.         BIT     3,D             ; test FLAGS is it 'K' mode (from OUT-CURS)
  1029.         JR      Z,L0364         ; skip to K-TOKENS if so
  1030.  
  1031.         BIT     3,(IY+$30)      ; test FLAGS2 - consider CAPS LOCK ?
  1032.         RET     NZ              ; return if so with main code.
  1033.  
  1034.         INC     B               ; is shift being pressed ?
  1035.                                 ; result zero if not
  1036.         RET     NZ              ; return if shift pressed.
  1037.  
  1038.         ADD     A,$20           ; else convert the code to lower case.
  1039.         RET                     ; return.
  1040.  
  1041. ; ---
  1042.  
  1043. ; the jump was here for tokens
  1044.  
  1045. ;; K-TOKENS
  1046. L0364:  ADD     A,$A5           ; add offset to main code so that 'A'
  1047.                                 ; becomes 'NEW' etc.
  1048.         RET                     ; return
  1049.  
  1050. ; ---
  1051.  
  1052. ; the jump was here with digits, space, enter and symbol shift (< $xx)
  1053.  
  1054. ;; K-DIGIT
  1055. L0367:  CP      $30             ; is it '0' or higher ?
  1056.         RET     C               ; return with space, enter and symbol-shift
  1057.  
  1058.         DEC     C               ; test MODE (was 0='KLC', 1='E', 2='G')
  1059.         JP      M,L039D         ; jump to K-KLC-DGT if was 0.
  1060.  
  1061.         JR      NZ,L0389        ; forward to K-GRA-DGT if mode was 2.
  1062.  
  1063. ; continue with extended digits 0-9.
  1064.  
  1065.         LD      HL,L0284-$30    ; $0254 - base of E-DIGITS
  1066.         BIT     5,B             ; test - shift=$27 sym-shift=$18
  1067.         JR      Z,L034A         ; to K-LOOK-UP if sym-shift
  1068.  
  1069.         CP      $38             ; is character '8' ?
  1070.         JR      NC,L0382        ; to K-8-&-9 if greater than '7'
  1071.  
  1072.         SUB     $20             ; reduce to ink range $10-$17
  1073.         INC     B               ; shift ?
  1074.         RET     Z               ; return if not.
  1075.  
  1076.         ADD     A,$08           ; add 8 to give paper range $18 - $1F
  1077.         RET                     ; return
  1078.  
  1079. ; ---
  1080.  
  1081. ; 89
  1082.  
  1083. ;; K-8-&-9
  1084. L0382:  SUB     $36             ; reduce to 02 and 03  bright codes
  1085.         INC     B               ; test if shift pressed.
  1086.         RET     Z               ; return if not.
  1087.  
  1088.         ADD     A,$FE           ; subtract 2 setting carry
  1089.         RET                     ; to give 0 and 1    flash codes.
  1090.  
  1091. ; ---
  1092.  
  1093. ;  graphics mode with digits
  1094.  
  1095. ;; K-GRA-DGT
  1096. L0389:  LD      HL,L0260-$30    ; $0230 base address of CTL-CODES
  1097.  
  1098.         CP      $39             ; is key '9' ?
  1099.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0F, GRAPHICS.
  1100.  
  1101.         CP      $30             ; is key '0' ?
  1102.         JR      Z,L034A         ; back to K-LOOK-UP - changed to $0C, delete.
  1103.  
  1104. ; for keys '0' - '7' we assign a mosaic character depending on shift.
  1105.  
  1106.         AND     $07             ; convert character to number. 0 - 7.
  1107.         ADD     A,$80           ; add offset - they start at $80
  1108.  
  1109.         INC     B               ; destructively test for shift
  1110.         RET     Z               ; and return if not pressed.
  1111.  
  1112.         XOR     $0F             ; toggle bits becomes range $88-$8F
  1113.         RET                     ; return.
  1114.  
  1115. ; ---
  1116.  
  1117. ; now digits in 'KLC' mode
  1118.  
  1119. ;; K-KLC-DGT
  1120. L039D:  INC     B               ; return with digit codes if neither
  1121.         RET     Z               ; shift key pressed.
  1122.  
  1123.         BIT     5,B             ; test for caps shift.
  1124.  
  1125.         LD      HL,L0260-$30    ; prepare base of table CTL-CODES.
  1126.         JR      NZ,L034A        ; back to K-LOOK-UP if shift pressed.
  1127.  
  1128. ; must have been symbol shift
  1129.  
  1130.         SUB     $10             ; for ASCII most will now be correct
  1131.                                 ; on a standard typewriter.
  1132.         CP      $22             ; but '@' is not - see below.
  1133.         JR      Z,L03B2         ; forward to to K-@-CHAR if so
  1134.  
  1135.         CP      $20             ; '_' is the other one that fails
  1136.         RET     NZ              ; return if not.
  1137.  
  1138.         LD      A,$5F           ; substitute ASCII '_'
  1139.         RET                     ; return.
  1140.  
  1141. ; ---
  1142.  
  1143. ;; K-@-CHAR
  1144. L03B2:  LD      A,$40           ; substitute ASCII '@'
  1145.         RET                     ; return.
  1146.  
  1147.  
  1148. ; ------------------------------------------------------------------------
  1149. ; The Spectrum Input character keys. One or two are abbreviated.
  1150. ; From $00 Flash 0 to $FF COPY. The routine above has decoded all these.
  1151.  
  1152. ;  | 00 Fl0| 01 Fl1| 02 Br0| 03 Br1| 04 In0| 05 In1| 06 CAP| 07 EDT|
  1153. ;  | 08 LFT| 09 RIG| 0A DWN| 0B UP | 0C DEL| 0D ENT| 0E SYM| 0F GRA|
  1154. ;  | 10 Ik0| 11 Ik1| 12 Ik2| 13 Ik3| 14 Ik4| 15 Ik5| 16 Ik6| 17 Ik7|
  1155. ;  | 18 Pa0| 19 Pa1| 1A Pa2| 1B Pa3| 1C Pa4| 1D Pa5| 1E Pa6| 1F Pa7|
  1156. ;  | 20 SP | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  ' |
  1157. ;  | 28  ( | 29  ) | 2A  * | 2B  + | 2C  , | 2D  - | 2E  . | 2F  / |
  1158. ;  | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |
  1159. ;  | 38  8 | 39  9 | 3A  : | 3B  ; | 3C  < | 3D  = | 3E  > | 3F  ? |
  1160. ;  | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |
  1161. ;  | 48  H | 49  I | 4A  J | 4B  K | 4C  L | 4D  M | 4E  N | 4F  O |
  1162. ;  | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |
  1163. ;  | 58  X | 59  Y | 5A  Z | 5B  [ | 5C  \ | 5D  ] | 5E  ^ | 5F  _ |
  1164. ;  | 60 ukp| 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |
  1165. ;  | 68  h | 69  i | 6A  j | 6B  k | 6C  l | 6D  m | 6E  n | 6F  o |
  1166. ;  | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |
  1167. ;  | 78  x | 79  y | 7A  z | 7B  { | 7C  | | 7D  } | 7E  ~ | 7F (c)|
  1168. ;  | 80 128| 81 129| 82 130| 83 131| 84 132| 85 133| 86 134| 87 135|
  1169. ;  | 88 136| 89 137| 8A 138| 8B 139| 8C 140| 8D 141| 8E 142| 8F 143|
  1170. ;  | 90 [A]| 91 [B]| 92 [C]| 93 [D]| 94 [E]| 95 [F]| 96 [G]| 97 [H]|
  1171. ;  | 98 [I]| 99 [J]| 9A [K]| 9B [L]| 9C [M]| 9D [N]| 9E [O]| 9F [P]|
  1172. ;  | A0 [Q]| A1 [R]| A2 [S]| A3 [T]| A4 [U]| A5 RND| A6 IK$| A7 PI |
  1173. ;  | A8 FN | A9 PNT| AA SC$| AB ATT| AC AT | AD TAB| AE VL$| AF COD|
  1174. ;  | B0 VAL| B1 LEN| B2 SIN| B3 COS| B4 TAN| B5 ASN| B6 ACS| B7 ATN|
  1175. ;  | B8 LN | B9 EXP| BA INT| BB SQR| BC SGN| BD ABS| BE PEK| BF IN |
  1176. ;  | C0 USR| C1 ST$| C2 CH$| C3 NOT| C4 BIN| C5 OR | C6 AND| C7 <= |
  1177. ;  | C8 >= | C9 <> | CA LIN| CB THN| CC TO | CD STP| CE DEF| CF CAT|
  1178. ;  | D0 FMT| D1 MOV| D2 ERS| D3 OPN| D4 CLO| D5 MRG| D6 VFY| D7 BEP|
  1179. ;  | D8 CIR| D9 INK| DA PAP| DB FLA| DC BRI| DD INV| DE OVR| DF OUT|
  1180. ;  | E0 LPR| E1 LLI| E2 STP| E3 REA| E4 DAT| E5 RES| E6 NEW| E7 BDR|
  1181. ;  | E8 CON| E9 DIM| EA REM| EB FOR| EC GTO| ED GSB| EE INP| EF LOA|
  1182. ;  | F0 LIS| F1 LET| F2 PAU| F3 NXT| F4 POK| F5 PRI| F6 PLO| F7 RUN|
  1183. ;  | F8 SAV| F9 RAN| FA IF | FB CLS| FC DRW| FD CLR| FE RET| FF CPY|
  1184.  
  1185. ; Note that for simplicity, Sinclair have located all the control codes
  1186. ; below the space character.
  1187. ; ASCII DEL, $7F, has been made a copyright symbol.
  1188. ; Also $60, '`', not used in BASIC but used in other languages, has been
  1189. ; allocated the local currency symbol for the relevant country -
  1190. ; ukp in most Spectrums.
  1191.  
  1192. ; ------------------------------------------------------------------------
  1193.  
  1194. ;**********************************
  1195. ;** Part 3. LOUDSPEAKER ROUTINES **
  1196. ;**********************************
  1197.  
  1198.  
  1199. ; Documented by Alvin Albrecht.
  1200.  
  1201.  
  1202. ; ------------------------------
  1203. ; Routine to control loudspeaker
  1204. ; ------------------------------
  1205. ; Outputs a square wave of given duration and frequency
  1206. ; to the loudspeaker.
  1207. ;   Enter with: DE = #cycles - 1
  1208. ;               HL = tone period as described next
  1209. ;
  1210. ; The tone period is measured in T states and consists of
  1211. ; three parts: a coarse part (H register), a medium part
  1212. ; (bits 7..2 of L) and a fine part (bits 1..0 of L) which
  1213. ; contribute to the waveform timing as follows:
  1214. ;
  1215. ;                          coarse    medium       fine
  1216. ; duration of low  = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1217. ; duration of hi   = 118 + 1024*H + 16*(L>>2) + 4*(L&0x3)
  1218. ; Tp = tone period = 236 + 2048*H + 32*(L>>2) + 8*(L&0x3)
  1219. ;                  = 236 + 2048*H + 8*L = 236 + 8*HL
  1220. ;
  1221. ; As an example, to output five seconds of middle C (261.624 Hz):
  1222. ;   (a) Tone period = 1/261.624 = 3.822ms
  1223. ;   (b) Tone period in T-States = 3.822ms*fCPU = 13378
  1224. ;         where fCPU = clock frequency of the CPU = 3.5MHz
  1225. ;   (c) Find H and L for desired tone period:
  1226. ;         HL = (Tp - 236) / 8 = (13378 - 236) / 8 = 1643 = 0x066B
  1227. ;   (d) Tone duration in cycles = 5s/3.822ms = 1308 cycles
  1228. ;         DE = 1308 - 1 = 0x051B
  1229. ;
  1230. ; The resulting waveform has a duty ratio of exactly 50%.
  1231. ;
  1232. ;
  1233. ;; BEEPER
  1234. L03B5:  DI                      ; Disable Interrupts so they don't disturb timing
  1235.         LD      A,L             ;
  1236.         SRL     L               ;
  1237.         SRL     L               ; L = medium part of tone period
  1238.         CPL                     ;
  1239.         AND     $03             ; A = 3 - fine part of tone period
  1240.         LD      C,A             ;
  1241.         LD      B,$00           ;
  1242.         LD      IX,L03D1        ; Address: BE-IX+3
  1243.         ADD     IX,BC           ;   IX holds address of entry into the loop
  1244.                                 ;   the loop will contain 0-3 NOPs, implementing
  1245.                                 ;   the fine part of the tone period.
  1246.         LD      A,($5C48)       ; BORDCR
  1247.         AND     $38             ; bits 5..3 contain border colour
  1248.         RRCA                    ; border colour bits moved to 2..0
  1249.         RRCA                    ;   to match border bits on port #FE
  1250.         RRCA                    ;
  1251.         OR       $08            ; bit 3 set (tape output bit on port #FE)
  1252.                                 ;   for loud sound output
  1253. ;; BE-IX+3
  1254. L03D1:  NOP              ;(4)   ; optionally executed NOPs for small
  1255.                                 ;   adjustments to tone period
  1256. ;; BE-IX+2
  1257. L03D2:  NOP              ;(4)   ;
  1258.  
  1259. ;; BE-IX+1
  1260. L03D3:  NOP              ;(4)   ;
  1261.  
  1262. ;; BE-IX+0
  1263. L03D4:  INC     B        ;(4)   ;
  1264.         INC     C        ;(4)   ;
  1265.  
  1266. ;; BE-H&L-LP
  1267. L03D6:  DEC     C        ;(4)   ; timing loop for duration of
  1268.         JR      NZ,L03D6 ;(12/7);   high or low pulse of waveform
  1269.  
  1270.         LD      C,$3F    ;(7)   ;
  1271.         DEC     B        ;(4)   ;
  1272.         JP      NZ,L03D6 ;(10)  ; to BE-H&L-LP
  1273.  
  1274.         XOR     $10      ;(7)   ; toggle output beep bit
  1275.         OUT     ($FE),A  ;(11)  ; output pulse
  1276.         LD      B,H      ;(4)   ; B = coarse part of tone period
  1277.         LD      C,A      ;(4)   ; save port #FE output byte
  1278.         BIT     4,A      ;(8)   ; if new output bit is high, go
  1279.         JR      NZ,L03F2 ;(12/7);   to BE-AGAIN
  1280.  
  1281.         LD      A,D      ;(4)   ; one cycle of waveform has completed
  1282.         OR      E        ;(4)   ;   (low->low). if cycle countdown = 0
  1283.         JR      Z,L03F6  ;(12/7);   go to BE-END
  1284.  
  1285.         LD      A,C      ;(4)   ; restore output byte for port #FE
  1286.         LD      C,L      ;(4)   ; C = medium part of tone period
  1287.         DEC     DE       ;(6)   ; decrement cycle count
  1288.         JP      (IX)     ;(8)   ; do another cycle
  1289.  
  1290. ;; BE-AGAIN                     ; halfway through cycle
  1291. L03F2:  LD      C,L      ;(4)   ; C = medium part of tone period
  1292.         INC     C        ;(4)   ; adds 16 cycles to make duration of high = duration of low
  1293.         JP      (IX)     ;(8)   ; do high pulse of tone
  1294.  
  1295. ;; BE-END
  1296. L03F6:  EI                      ; Enable Interrupts
  1297.         RET                     ;
  1298.  
  1299.  
  1300. ; -------------------
  1301. ; Handle BEEP command
  1302. ; -------------------
  1303. ; BASIC interface to BEEPER subroutine.
  1304. ; Invoked in BASIC with:
  1305. ;   BEEP dur, pitch
  1306. ;   where dur   = duration in seconds
  1307. ;         pitch = # of semitones above/below middle C
  1308. ;
  1309. ; Enter with: pitch on top of calculator stack
  1310. ;             duration next on calculator stack
  1311. ;
  1312. ;; beep
  1313. L03F8:  RST     28H             ;; FP-CALC
  1314.         DB    $31             ;;duplicate                  ; duplicate pitch
  1315.         DB    $27             ;;int                        ; convert to integer
  1316.         DB    $C0             ;;st-mem-0                   ; store integer pitch to memory 0
  1317.         DB    $03             ;;subtract                   ; calculate fractional part of pitch = fp_pitch - int_pitch
  1318.         DB    $34             ;;stk-data                   ; push constant
  1319.         DB    $EC             ;;Exponent: $7C, Bytes: 4    ; constant = 0.05762265
  1320.         DB    $6C,$98,$1F,$F5 ;;($6C,$98,$1F,$F5)
  1321.         DB    $04             ;;multiply                   ; compute:
  1322.         DB    $A1             ;;stk-one                    ; 1 + 0.05762265 * fraction_part(pitch)
  1323.         DB    $0F             ;;addition
  1324.         DB    $38             ;;end-calc                   ; leave on calc stack
  1325.  
  1326.         LD      HL,$5C92        ; MEM-0: number stored here is in 16 bit integer format (pitch)
  1327.                                 ;   0, 0/FF (pos/neg), LSB, MSB, 0
  1328.                                 ;   LSB/MSB is stored in two's complement
  1329.                                 ; In the following, the pitch is checked if it is in the range -128<=p<=127
  1330.         LD      A,(HL)          ; First byte must be zero, otherwise
  1331.         AND     A               ;   error in integer conversion
  1332.         JR      NZ,L046C        ; to REPORT-B
  1333.  
  1334.         INC     HL              ;
  1335.         LD      C,(HL)          ; C = pos/neg flag = 0/FF
  1336.         INC     HL              ;
  1337.         LD      B,(HL)          ; B = LSB, two's complement
  1338.         LD      A,B             ;
  1339.         RLA                     ;
  1340.         SBC     A,A             ; A = 0/FF if B is pos/neg
  1341.         CP      C               ; must be the same as C if the pitch is -128<=p<=127
  1342.         JR      NZ,L046C        ; if no, error REPORT-B
  1343.  
  1344.         INC     HL              ; if -128<=p<=127, MSB will be 0/FF if B is pos/neg
  1345.         CP      (HL)            ; verify this
  1346.         JR      NZ,L046C        ; if no, error REPORT-B
  1347.                                 ; now we know -128<=p<=127
  1348.         LD      A,B             ; A = pitch + 60
  1349.         ADD     A,$3C           ; if -60<=pitch<=67,
  1350.         JP      P,L0425         ;   goto BE-i-OK
  1351.  
  1352.         JP      PO,L046C        ; if pitch <= 67 goto REPORT-B
  1353.                                 ;   lower bound of pitch set at -60
  1354.  
  1355. ;; BE-I-OK                      ; here, -60<=pitch<=127
  1356.                                 ; and A=pitch+60 -> 0<=A<=187
  1357.  
  1358. L0425:  LD      B,$FA           ; 6 octaves below middle C
  1359.  
  1360. ;; BE-OCTAVE                    ; A=# semitones above 5 octaves below middle C
  1361. L0427:  INC     B               ; increment octave
  1362.         SUB     $0C             ; 12 semitones = one octave
  1363.         JR      NC,L0427        ; to BE-OCTAVE
  1364.  
  1365.         ADD     A,$0C           ; A = # semitones above C (0-11)
  1366.         PUSH    BC              ; B = octave displacement from middle C, 2's complement: -5<=B<=10
  1367.         LD      HL,L046E        ; Address: semi-tone
  1368.         CALL    L3406           ; routine LOC-MEM
  1369.                                 ;   HL = 5*A + $046E
  1370.         CALL    L33B4           ; routine STACK-NUM
  1371.                                 ;   read FP value (freq) from semitone table (HL) and push onto calc stack
  1372.  
  1373.         RST     28H             ;; FP-CALC
  1374.         DB    $04             ;;multiply   mult freq by 1 + 0.0576 * fraction_part(pitch) stacked earlier
  1375.                                 ;;             thus taking into account fractional part of pitch.
  1376.                                 ;;           the number 0.0576*frequency is the distance in Hz to the next
  1377.                                 ;;             note (verify with the frequencies recorded in the semitone
  1378.                                 ;;             table below) so that the fraction_part of the pitch does
  1379.                                 ;;             indeed represent a fractional distance to the next note.
  1380.         DB    $38             ;;end-calc   HL points to first byte of fp num on stack = middle frequency to generate
  1381.  
  1382.         POP     AF              ; A = octave displacement from middle C, 2's complement: -5<=A<=10
  1383.         ADD     A,(HL)          ; increase exponent by A (equivalent to multiplying by 2^A)
  1384.         LD      (HL),A          ;
  1385.  
  1386.         RST     28H             ;; FP-CALC
  1387.         DB    $C0             ;;st-mem-0          ; store frequency in memory 0
  1388.         DB    $02             ;;delete            ; remove from calc stack
  1389.         DB    $31             ;;duplicate         ; duplicate duration (seconds)
  1390.         DB    $38             ;;end-calc
  1391.  
  1392.         CALL    L1E94           ; routine FIND-INT1 ; FP duration to A
  1393.         CP      $0B             ; if dur > 10 seconds,
  1394.         JR      NC,L046C        ;   goto REPORT-B
  1395.  
  1396.         ;;; The following calculation finds the tone period for HL and the cycle count
  1397.         ;;; for DE expected in the BEEPER subroutine.  From the example in the BEEPER comments,
  1398.         ;;;
  1399.         ;;; HL = ((fCPU / f) - 236) / 8 = fCPU/8/f - 236/8 = 437500/f -29.5
  1400.         ;;; DE = duration * frequency - 1
  1401.         ;;;
  1402.         ;;; Note the different constant (30.125) used in the calculation of HL
  1403.         ;;; below.  This is probably an error.
  1404.  
  1405.         RST     28H             ;; FP-CALC
  1406.         DB    $E0             ;;get-mem-0                 ; push frequency
  1407.         DB    $04             ;;multiply                  ; result1: #cycles = duration * frequency
  1408.         DB    $E0             ;;get-mem-0                 ; push frequency
  1409.         DB    $34             ;;stk-data                  ; push constant
  1410.         DB    $80             ;;Exponent $93, Bytes: 3    ; constant = 437500
  1411.         DB    $43,$55,$9F,$80 ;;($55,$9F,$80,$00)
  1412.         DB    $01             ;;exchange                  ; frequency on top
  1413.         DB    $05             ;;division                  ; 437500 / frequency
  1414.         DB    $34             ;;stk-data                  ; push constant
  1415.         DB    $35             ;;Exponent: $85, Bytes: 1   ; constant = 30.125
  1416.         DB    $71             ;;($71,$00,$00,$00)
  1417.         DB    $03             ;;subtract                  ; result2: tone_period(HL) = 437500 / freq - 30.125
  1418.         DB    $38             ;;end-calc
  1419.  
  1420.         CALL    L1E99           ; routine FIND-INT2
  1421.         PUSH    BC              ;   BC = tone_period(HL)
  1422.         CALL    L1E99           ; routine FIND-INT2, BC = #cycles to generate
  1423.         POP     HL              ; HL = tone period
  1424.         LD      D,B             ;
  1425.         LD      E,C             ; DE = #cycles
  1426.         LD      A,D             ;
  1427.         OR      E               ;
  1428.         RET     Z               ; if duration = 0, skip BEEP and avoid 65536 cycle
  1429.                                 ;   boondoggle that would occur next
  1430.         DEC     DE              ; DE = #cycles - 1
  1431.         JP      L03B5           ; to BEEPER
  1432.  
  1433. ; ---
  1434.  
  1435.  
  1436. ;; REPORT-B
  1437. L046C:  RST     08H             ; ERROR-1
  1438.         DB    $0A             ; Error Report: Integer out of range
  1439.  
  1440.  
  1441.  
  1442. ; ---------------
  1443. ; Semi-tone table
  1444. ; ---------------
  1445. ;
  1446. ; Holds frequencies corresponding to semitones in middle octave.
  1447. ; To move n octaves higher or lower, frequencies are multiplied by 2^n.
  1448.  
  1449. ;; semi-tone         five byte fp         decimal freq     note (middle)
  1450. L046E:  DB    $89, $02, $D0, $12, $86;  261.625565290         C
  1451.         DB    $89, $0A, $97, $60, $75;  277.182631135         C#
  1452.         DB    $89, $12, $D5, $17, $1F;  293.664768100         D
  1453.         DB    $89, $1B, $90, $41, $02;  311.126983881         D#
  1454.         DB    $89, $24, $D0, $53, $CA;  329.627557039         E
  1455.         DB    $89, $2E, $9D, $36, $B1;  349.228231549         F
  1456.         DB    $89, $38, $FF, $49, $3E;  369.994422674         F#
  1457.         DB    $89, $43, $FF, $6A, $73;  391.995436072         G
  1458.         DB    $89, $4F, $A7, $00, $54;  415.304697513         G#
  1459.         DB    $89, $5C, $00, $00, $00;  440.000000000         A
  1460.         DB    $89, $69, $14, $F6, $24;  466.163761616         A#
  1461.         DB    $89, $76, $F1, $10, $05;  493.883301378         B
  1462.  
  1463.  
  1464. ;****************************************
  1465. ;** Part 4. CASSETTE HANDLING ROUTINES **
  1466. ;****************************************
  1467.  
  1468. ; These routines begin with the service routines followed by a single
  1469. ; command entry point.
  1470. ; The first of these service routines is a curiosity.
  1471.  
  1472. ; -----------------------
  1473. ; THE 'ZX81 NAME' ROUTINE
  1474. ; -----------------------
  1475. ;   This routine fetches a filename in ZX81 format and is not used by the
  1476. ;   cassette handling routines in this ROM.
  1477.  
  1478. ;; zx81-name
  1479. L04AA:  CALL    L24FB           ; routine SCANNING to evaluate expression.
  1480.         LD      A,($5C3B)       ; fetch system variable FLAGS.
  1481.         ADD     A,A             ; test bit 7 - syntax, bit 6 - result type.
  1482.         JP      M,L1C8A         ; to REPORT-C if not string result
  1483.                                 ; 'Nonsense in BASIC'.
  1484.  
  1485.         POP     HL              ; drop return address.
  1486.         RET     NC              ; return early if checking syntax.
  1487.  
  1488.         PUSH    HL              ; re-save return address.
  1489.         CALL    L2BF1           ; routine STK-FETCH fetches string parameters.
  1490.         LD      H,D             ; transfer start of filename
  1491.         LD      L,E             ; to the HL register.
  1492.         DEC     C               ; adjust to point to last character and
  1493.         RET     M               ; return if the null string.
  1494.                                 ; or multiple of 256!
  1495.  
  1496.         ADD     HL,BC           ; find last character of the filename.
  1497.                                 ; and also clear carry.
  1498.         SET     7,(HL)          ; invert it.
  1499.         RET                     ; return.
  1500.  
  1501. ; =========================================
  1502. ;
  1503. ; PORT 254 ($FE)
  1504. ;
  1505. ;                      spk mic { border  }  
  1506. ;          ___ ___ ___ ___ ___ ___ ___ ___
  1507. ; PORT    |   |   |   |   |   |   |   |   |
  1508. ; 254     |   |   |   |   |   |   |   |   |
  1509. ; $FE     |___|___|___|___|___|___|___|___|
  1510. ;           7   6   5   4   3   2   1   0
  1511. ;
  1512.  
  1513. ; ----------------------------------
  1514. ; Save header and program/data bytes
  1515. ; ----------------------------------
  1516. ; This routine saves a section of data. It is called from SA-CTRL to save the
  1517. ; seventeen bytes of header data. It is also the exit route from that routine
  1518. ; when it is set up to save the actual data.
  1519. ; On entry -
  1520. ; HL points to start of data.
  1521. ; IX points to descriptor.
  1522. ; The accumulator is set to  $00 for a header, $FF for data.
  1523.  
  1524. ;; SA-BYTES
  1525. L04C2:  LD      HL,L053F        ; address: SA/LD-RET
  1526.         PUSH    HL              ; is pushed as common exit route.
  1527.                                 ; however there is only one non-terminal exit
  1528.                                 ; point.
  1529.  
  1530.         LD      HL,$1F80        ; a timing constant H=$1F, L=$80
  1531.                                 ; inner and outer loop counters
  1532.                                 ; a five second lead-in is used for a header.
  1533.  
  1534.         BIT     7,A             ; test one bit of accumulator.
  1535.                                 ; (AND A ?)
  1536.         JR      Z,L04D0         ; skip to SA-FLAG if a header is being saved.
  1537.  
  1538. ; else is data bytes and a shorter lead-in is used.
  1539.  
  1540.         LD      HL,$0C98        ; another timing value H=$0C, L=$98.
  1541.                                 ; a two second lead-in is used for the data.
  1542.  
  1543.  
  1544. ;; SA-FLAG
  1545. L04D0:  EX      AF,AF'          ; save flag
  1546.        INC     DE              ; increase length by one.
  1547.        DEC     IX              ; decrease start.
  1548.  
  1549.        DI                      ; disable interrupts
  1550.  
  1551.        LD      A,$02           ; select red for border, microphone bit on.
  1552.        LD      B,A             ; also does as an initial slight counter value.
  1553.  
  1554. ;; SA-LEADER
  1555. L04D8:  DJNZ    L04D8           ; self loop to SA-LEADER for delay.
  1556.                                ; after initial loop, count is $A4 (or $A3)
  1557.  
  1558.        OUT     ($FE),A         ; output byte $02/$0D to tape port.
  1559.  
  1560.        XOR     $0F             ; switch from RED (mic on) to CYAN (mic off).
  1561.  
  1562.        LD      B,$A4           ; hold count. also timed instruction.
  1563.  
  1564.        DEC     L               ; originally $80 or $98.
  1565.                                ; but subsequently cycles 256 times.
  1566.        JR      NZ,L04D8        ; back to SA-LEADER until L is zero.
  1567.  
  1568. ; the outer loop is counted by H
  1569.  
  1570.        DEC     B               ; decrement count
  1571.        DEC     H               ; originally  twelve or thirty-one.
  1572.        JP      P,L04D8         ; back to SA-LEADER until H becomes $FF
  1573.  
  1574. ; now send a synch pulse. At this stage mic is off and A holds value
  1575. ; for mic on.
  1576. ; A synch pulse is much shorter than the steady pulses of the lead-in.
  1577.  
  1578.        LD      B,$2F           ; another short timed delay.
  1579.  
  1580. ;; SA-SYNC-1
  1581. L04EA:  DJNZ    L04EA           ; self loop to SA-SYNC-1
  1582.  
  1583.        OUT     ($FE),A         ; switch to mic on and red.
  1584.        LD      A,$0D           ; prepare mic off - cyan
  1585.        LD      B,$37           ; another short timed delay.
  1586.  
  1587. ;; SA-SYNC-2
  1588. L04F2:  DJNZ    L04F2           ; self loop to SA-SYNC-2
  1589.  
  1590.        OUT     ($FE),A         ; output mic off, cyan border.
  1591.        LD      BC,$3B0E        ; B=$3B time(*), C=$0E, YELLOW, MIC OFF.
  1592.  
  1593. ;
  1594.  
  1595.        EX      AF,AF'          ; restore saved flag
  1596.                                 ; which is 1st byte to be saved.
  1597.  
  1598.         LD      L,A             ; and transfer to L.
  1599.                                 ; the initial parity is A, $FF or $00.
  1600.         JP      L0507           ; JUMP forward to SA-START     ->
  1601.                                 ; the mid entry point of loop.
  1602.  
  1603. ; -------------------------
  1604. ; During the save loop a parity byte is maintained in H.
  1605. ; the save loop begins by testing if reduced length is zero and if so
  1606. ; the final parity byte is saved reducing count to $FFFF.
  1607.  
  1608. ;; SA-LOOP
  1609. L04FE:  LD      A,D             ; fetch high byte
  1610.         OR      E               ; test against low byte.
  1611.         JR      Z,L050E         ; forward to SA-PARITY if zero.
  1612.  
  1613.         LD      L,(IX+$00)      ; load currently addressed byte to L.
  1614.  
  1615. ;; SA-LOOP-P
  1616. L0505:  LD      A,H             ; fetch parity byte.
  1617.         XOR     L               ; exclusive or with new byte.
  1618.  
  1619. ; -> the mid entry point of loop.
  1620.  
  1621. ;; SA-START
  1622. L0507:  LD      H,A             ; put parity byte in H.
  1623.         LD      A,$01           ; prepare blue, mic=on.
  1624.         SCF                     ; set carry flag ready to rotate in.
  1625.         JP      L0525           ; JUMP forward to SA-8-BITS            -8->
  1626.  
  1627. ; ---
  1628.  
  1629. ;; SA-PARITY
  1630. L050E:  LD      L,H             ; transfer the running parity byte to L and
  1631.         JR      L0505           ; back to SA-LOOP-P
  1632.                                 ; to output that byte before quitting normally.
  1633.  
  1634. ; ---
  1635.  
  1636. ; entry point to save yellow part of bit.
  1637. ; a bit consists of a period with mic on and blue border followed by
  1638. ; a period of mic off with yellow border.
  1639. ; Note. since the DJNZ instruction does not affect flags, the zero flag is used
  1640. ; to indicate which of the two passes is in effect and the carry maintains the
  1641. ; state of the bit to be saved.
  1642.  
  1643. ;; SA-BIT-2
  1644. L0511:  LD      A,C             ; fetch 'mic on and yellow' which is
  1645.                                 ; held permanently in C.
  1646.         BIT     7,B             ; set the zero flag. B holds $3E.
  1647.  
  1648. ; entry point to save 1 entire bit. For first bit B holds $3B(*).
  1649. ; Carry is set if saved bit is 1. zero is reset NZ on entry.
  1650.  
  1651. ;; SA-BIT-1
  1652. L0514:  DJNZ    L0514           ; self loop for delay to SA-BIT-1
  1653.  
  1654.         JR      NC,L051C        ; forward to SA-OUT if bit is 0.
  1655.  
  1656. ; but if bit is 1 then the mic state is held for longer.
  1657.  
  1658.         LD      B,$42           ; set timed delay. (66 decimal)
  1659.  
  1660. ;; SA-SET
  1661. L051A:  DJNZ    L051A           ; self loop to SA-SET
  1662.                                 ; (roughly an extra 66*13 clock cycles)
  1663.  
  1664. ;; SA-OUT
  1665. L051C:  OUT     ($FE),A         ; blue and mic on OR  yellow and mic off.
  1666.  
  1667.         LD      B,$3E           ; set up delay
  1668.         JR      NZ,L0511        ; back to SA-BIT-2 if zero reset NZ (first pass)
  1669.  
  1670. ; proceed when the blue and yellow bands have been output.
  1671.  
  1672.         DEC     B               ; change value $3E to $3D.
  1673.         XOR     A               ; clear carry flag (ready to rotate in).
  1674.         INC     A               ; reset zero flag ie. NZ.
  1675.  
  1676. ; -8->
  1677.  
  1678. ;; SA-8-BITS
  1679. L0525:  RL      L               ; rotate left through carry
  1680.                                 ; C<76543210<C  
  1681.         JP      NZ,L0514        ; JUMP back to SA-BIT-1
  1682.                                 ; until all 8 bits done.
  1683.  
  1684. ; when the initial set carry is passed out again then a byte is complete.
  1685.  
  1686.         DEC     DE              ; decrease length
  1687.         INC     IX              ; increase byte pointer
  1688.         LD      B,$31           ; set up timing.
  1689.  
  1690.         LD      A,$7F           ; test the space key and
  1691.         IN      A,($FE)         ; return to common exit (to restore border)
  1692.         RRA                     ; if a space is pressed
  1693.         RET     NC              ; return to SA/LD-RET.   - - >
  1694.  
  1695. ; now test if byte counter has reached $FFFF.
  1696.  
  1697.         LD      A,D             ; fetch high byte
  1698.         INC     A               ; increment.
  1699.         JP      NZ,L04FE        ; JUMP to SA-LOOP if more bytes.
  1700.  
  1701.         LD      B,$3B           ; a final delay.
  1702.  
  1703. ;; SA-DELAY
  1704. L053C:  DJNZ    L053C           ; self loop to SA-DELAY
  1705.  
  1706.         RET                     ; return - - >
  1707.  
  1708. ; --------------------------------------------------
  1709. ; Reset border and check BREAK key for LOAD and SAVE
  1710. ; --------------------------------------------------
  1711. ; the address of this routine is pushed on the stack prior to any load/save
  1712. ; operation and it handles normal completion with the restoration of the
  1713. ; border and also abnormal termination when the break key, or to be more
  1714. ; precise the space key is pressed during a tape operation.
  1715. ; - - >
  1716.  
  1717. ;; SA/LD-RET
  1718. L053F:  PUSH    AF              ; preserve accumulator throughout.
  1719.         LD      A,($5C48)       ; fetch border colour from BORDCR.
  1720.         AND     $38             ; mask off paper bits.
  1721.         RRCA                    ; rotate
  1722.         RRCA                    ; to the
  1723.         RRCA                    ; range 0-7.
  1724.  
  1725. ;===============================
  1726.                 IF TAP_EMU_BORDER=1
  1727.                 DB 0,0
  1728.                 ELSE
  1729.                 OUT ($FE),A     ; change the border colour.
  1730.                 ENDIF
  1731. ;===============================
  1732.  
  1733.         LD      A,$7F           ; read from port address $7FFE the
  1734.         IN      A,($FE)         ; row with the space key at outside.
  1735.  
  1736.         RRA                     ; test for space key pressed.
  1737.         EI                      ; enable interrupts
  1738.         JR      C,L0554         ; forward to SA/LD-END if not
  1739.  
  1740.  
  1741. ;; REPORT-Da
  1742. L0552:  RST     08H             ; ERROR-1
  1743.         DB    $0C             ; Error Report: BREAK - CONT repeats
  1744.  
  1745. ; ---
  1746.  
  1747. ;; SA/LD-END
  1748. L0554:  POP     AF              ; restore the accumulator.
  1749.         RET                     ; return.
  1750.  
  1751. ; ------------------------------------
  1752. ; Load header or block of information
  1753. ; ------------------------------------
  1754. ; This routine is used to load bytes and on entry A is set to $00 for a
  1755. ; header or to $FF for data.  IX points to the start of receiving location
  1756. ; and DE holds the length of bytes to be loaded. If, on entry the carry flag
  1757. ; is set then data is loaded, if reset then it is verified.
  1758.  
  1759. ;; LD-BYTES
  1760. L0556:  INC     D               ; reset the zero flag without disturbing carry.
  1761.         EX      AF,AF'          ; preserve entry flags.
  1762.        DEC     D               ; restore high byte of length.
  1763.  
  1764.        DI                      ; disable interrupts
  1765.  
  1766.        LD      A,$0F           ; make the border white and mic off.
  1767.  
  1768. ;===============================
  1769.                 IF TAP_EMU_BORDER=1
  1770.                 DB 0,0
  1771.                 ELSE
  1772.                 OUT ($FE),A     ; output to port.
  1773.                 ENDIF
  1774. ;===============================
  1775.  
  1776.        LD      HL,L053F        ; Address: SA/LD-RET
  1777.        PUSH    HL              ; is saved on stack as terminating routine.
  1778.  
  1779. ; the reading of the EAR bit (D6) will always be preceded by a test of the
  1780. ; space key (D0), so store the initial post-test state.
  1781.  
  1782.        IN      A,($FE)         ; read the ear state - bit 6.
  1783.        RRA                     ; rotate to bit 5.
  1784.        AND     $20             ; isolate this bit.
  1785.        OR      $02             ; combine with red border colour.
  1786.  
  1787. ;===============================
  1788.                 RST 0X08
  1789.                 DB TAPE_EMUL
  1790. ;               LD C,A          ; and store initial state long-term in C.
  1791. ;               CP A            ; set the zero flag.
  1792. ;===============================
  1793.  
  1794. ;; LD-BREAK
  1795. L056B:  RET     NZ              ; return if at any time space is pressed.
  1796.  
  1797. ;; LD-START
  1798. L056C:  CALL    L05E7           ; routine LD-EDGE-1
  1799.        JR      NC,L056B        ; back to LD-BREAK with time out and no
  1800.                                ; edge present on tape.
  1801.  
  1802. ; but continue when a transition is found on tape.
  1803.  
  1804.        LD      HL,$0415        ; set up 16-bit outer loop counter for
  1805.                                ; approx 1 second delay.
  1806.  
  1807. ;; LD-WAIT
  1808. L0574:  DJNZ    L0574           ; self loop to LD-WAIT (for 256 times)
  1809.  
  1810.        DEC     HL              ; decrease outer loop counter.
  1811.        LD      A,H             ; test for
  1812.        OR      L               ; zero.
  1813.        JR      NZ,L0574        ; back to LD-WAIT, if not zero, with zero in B.
  1814.  
  1815. ; continue after delay with H holding zero and B also.
  1816. ; sample 256 edges to check that we are in the middle of a lead-in section.
  1817.  
  1818.        CALL    L05E3           ; routine LD-EDGE-2
  1819.        JR      NC,L056B        ; back to LD-BREAK
  1820.                                ; if no edges at all.
  1821.  
  1822. ;; LD-LEADER
  1823. L0580:  LD      B,$9C           ; set timing value.
  1824.        CALL    L05E3           ; routine LD-EDGE-2
  1825.        JR      NC,L056B        ; back to LD-BREAK if time-out
  1826.  
  1827.        LD      A,$C6           ; two edges must be spaced apart.
  1828.        CP      B               ; compare
  1829.        JR      NC,L056C        ; back to LD-START if too close together for a
  1830.                                ; lead-in.
  1831.  
  1832.        INC     H               ; proceed to test 256 edged sample.
  1833.        JR      NZ,L0580        ; back to LD-LEADER while more to do.
  1834.  
  1835. ; sample indicates we are in the middle of a two or five second lead-in.
  1836. ; Now test every edge looking for the terminal synch signal.
  1837.  
  1838. ;; LD-SYNC
  1839. L058F:  LD      B,$C9           ; initial timing value in B.
  1840.        CALL    L05E7           ; routine LD-EDGE-1
  1841.        JR      NC,L056B        ; back to LD-BREAK with time-out.
  1842.  
  1843.        LD      A,B             ; fetch augmented timing value from B.
  1844.        CP      $D4             ; compare
  1845.        JR      NC,L058F        ; back to LD-SYNC if gap too big, that is,
  1846.                                ; a normal lead-in edge gap.
  1847.  
  1848. ; but a short gap will be the synch pulse.
  1849. ; in which case another edge should appear before B rises to $FF
  1850.  
  1851.        CALL    L05E7           ; routine LD-EDGE-1
  1852.        RET     NC              ; return with time-out.
  1853.  
  1854. ; proceed when the synch at the end of the lead-in is found.
  1855. ; We are about to load data so change the border colours.
  1856.  
  1857.        LD      A,C             ; fetch long-term mask from C
  1858.        XOR     $03             ; and make blue/yellow.
  1859.  
  1860.        LD      C,A             ; store the new long-term byte.
  1861.  
  1862.        LD      H,$00           ; set up parity byte as zero.
  1863.        LD      B,$B0           ; timing.
  1864.        JR      L05C8           ; forward to LD-MARKER
  1865.                                ; the loop mid entry point with the alternate
  1866.                                ; zero flag reset to indicate first byte
  1867.                                ; is discarded.
  1868.  
  1869. ; --------------
  1870. ; the loading loop loads each byte and is entered at the mid point.
  1871.  
  1872. ;; LD-LOOP
  1873. L05A9:  EX      AF,AF'          ; restore entry flags and type in A.
  1874.         JR      NZ,L05B3        ; forward to LD-FLAG if awaiting initial flag
  1875.                                 ; which is to be discarded.
  1876.  
  1877.         JR      NC,L05BD        ; forward to LD-VERIFY if not to be loaded.
  1878.  
  1879.         LD      (IX+$00),L      ; place loaded byte at memory location.
  1880.         JR      L05C2           ; forward to LD-NEXT
  1881.  
  1882. ; ---
  1883.  
  1884. ;; LD-FLAG
  1885. L05B3:  RL      C               ; preserve carry (verify) flag in long-term
  1886.                                 ; state byte. Bit 7 can be lost.
  1887.  
  1888.         XOR     L               ; compare type in A with first byte in L.
  1889.         RET     NZ              ; return if no match e.g. CODE vs DATA.
  1890.  
  1891. ; continue when data type matches.
  1892.  
  1893.         LD      A,C             ; fetch byte with stored carry
  1894.         RRA                     ; rotate it to carry flag again
  1895.         LD      C,A             ; restore long-term port state.
  1896.  
  1897.         INC     DE              ; increment length ??
  1898.         JR      L05C4           ; forward to LD-DEC.
  1899.                                 ; but why not to location after ?
  1900.  
  1901. ; ---
  1902. ; for verification the byte read from tape is compared with that in memory.
  1903.  
  1904. ;; LD-VERIFY
  1905. L05BD:  LD      A,(IX+$00)      ; fetch byte from memory.
  1906.         XOR     L               ; compare with that on tape
  1907.         RET     NZ              ; return if not zero.
  1908.  
  1909. ;; LD-NEXT
  1910. L05C2:  INC     IX              ; increment byte pointer.
  1911.  
  1912. ;; LD-DEC
  1913. L05C4:  DEC     DE              ; decrement length.
  1914.         EX      AF,AF'          ; store the flags.
  1915.        LD      B,$B2           ; timing.
  1916.  
  1917. ; when starting to read 8 bits the receiving byte is marked with bit at right.
  1918. ; when this is rotated out again then 8 bits have been read.
  1919.  
  1920. ;; LD-MARKER
  1921. L05C8:  LD      L,$01           ; initialize as %00000001
  1922.  
  1923. ;; LD-8-BITS
  1924. L05CA:  CALL    L05E3           ; routine LD-EDGE-2 increments B relative to
  1925.                                ; gap between 2 edges.
  1926.        RET     NC              ; return with time-out.
  1927.  
  1928.        LD      A,$CB           ; the comparison byte.
  1929.        CP      B               ; compare to incremented value of B.
  1930.                                ; if B is higher then bit on tape was set.
  1931.                                ; if <= then bit on tape is reset.
  1932.  
  1933.        RL      L               ; rotate the carry bit into L.
  1934.  
  1935.        LD      B,$B0           ; reset the B timer byte.
  1936.        JP      NC,L05CA        ; JUMP back to LD-8-BITS
  1937.  
  1938. ; when carry set then marker bit has been passed out and byte is complete.
  1939.  
  1940.        LD      A,H             ; fetch the running parity byte.
  1941.        XOR     L               ; include the new byte.
  1942.        LD      H,A             ; and store back in parity register.
  1943.  
  1944.        LD      A,D             ; check length of
  1945.        OR      E               ; expected bytes.
  1946.        JR      NZ,L05A9        ; back to LD-LOOP
  1947.                                ; while there are more.
  1948.  
  1949. ; when all bytes loaded then parity byte should be zero.
  1950.  
  1951.        LD      A,H             ; fetch parity byte.
  1952.        CP      $01             ; set carry if zero.
  1953.        RET                     ; return
  1954.                                ; in no carry then error as checksum disagrees.
  1955.  
  1956. ; -------------------------
  1957. ; Check signal being loaded
  1958. ; -------------------------
  1959. ; An edge is a transition from one mic state to another.
  1960. ; More specifically a change in bit 6 of value input from port $FE.
  1961. ; Graphically it is a change of border colour, say, blue to yellow.
  1962. ; The first entry point looks for two adjacent edges. The second entry point
  1963. ; is used to find a single edge.
  1964. ; The B register holds a count, up to 256, within which the edge (or edges)
  1965. ; must be found. The gap between two edges will be more for a '1' than a '0'
  1966. ; so the value of B denotes the state of the bit (two edges) read from tape.
  1967.  
  1968. ; ->
  1969.  
  1970. ;; LD-EDGE-2
  1971. L05E3:  CALL    L05E7           ; call routine LD-EDGE-1 below.
  1972.        RET     NC              ; return if space pressed or time-out.
  1973.                                ; else continue and look for another adjacent
  1974.                                ; edge which together represent a bit on the
  1975.                                ; tape.
  1976.  
  1977. ; ->
  1978. ; this entry point is used to find a single edge from above but also
  1979. ; when detecting a read-in signal on the tape.
  1980.  
  1981. ;; LD-EDGE-1
  1982. L05E7:  LD      A,$16           ; a delay value of twenty two.
  1983.  
  1984. ;; LD-DELAY
  1985. L05E9:  DEC     A               ; decrement counter
  1986.        JR      NZ,L05E9        ; loop back to LD-DELAY 22 times.
  1987.  
  1988.        AND      A              ; clear carry.
  1989.  
  1990. ;; LD-SAMPLE
  1991. L05ED:  INC     B               ; increment the time-out counter.
  1992.        RET     Z               ; return with failure when $FF passed.
  1993.  
  1994.        LD      A,$7F           ; prepare to read keyboard and EAR port
  1995.        IN      A,($FE)         ; row $7FFE. bit 6 is EAR, bit 0 is SPACE key.
  1996.        RRA                     ; test outer key the space. (bit 6 moves to 5)
  1997.        RET     NC              ; return if space pressed.  >>>
  1998.  
  1999.        XOR     C               ; compare with initial long-term state.
  2000.        AND     $20             ; isolate bit 5
  2001.        JR      Z,L05ED         ; back to LD-SAMPLE if no edge.
  2002.  
  2003. ; but an edge, a transition of the EAR bit, has been found so switch the
  2004. ; long-term comparison byte containing both border colour and EAR bit.
  2005.  
  2006.        LD      A,C             ; fetch comparison value.
  2007.        CPL                     ; switch the bits
  2008.        LD      C,A             ; and put back in C for long-term.
  2009.  
  2010.        AND     $07             ; isolate new colour bits.
  2011.        OR      $08             ; set bit 3 - MIC off.
  2012.        OUT     ($FE),A         ; send to port to effect change of colour.
  2013.  
  2014.        SCF                     ; set carry flag signaling edge found within
  2015.                                ; time allowed.
  2016.        RET                     ; return.
  2017.  
  2018. ; ---------------------------------
  2019. ; Entry point for all tape commands
  2020. ; ---------------------------------
  2021. ; This is the single entry point for the four tape commands.
  2022. ; The routine first determines in what context it has been called by examining
  2023. ; the low byte of the Syntax table entry which was stored in T_ADDR.
  2024. ; Subtracting $EO (the present arrangement) gives a value of
  2025. ; $00 - SAVE
  2026. ; $01 - LOAD
  2027. ; $02 - VERIFY
  2028. ; $03 - MERGE
  2029. ; As with all commands the address STMT-RET is on the stack.
  2030.  
  2031. ;; SAVE-ETC
  2032. L0605:  POP     AF              ; discard address STMT-RET.
  2033.        LD      A,($5C74)       ; fetch T_ADDR
  2034.  
  2035. ; Now reduce the low byte of the Syntax table entry to give command.
  2036. ; Note. For ZASM use SUB $E0 as next instruction.
  2037.  
  2038. L0609           SUB LOW (L1ADF)+1       ; subtract the known offset.
  2039.                                ; ( is SUB $E0 in standard ROM )
  2040.  
  2041.        LD      ($5C74),A       ; and put back in T_ADDR as 0,1,2, or 3
  2042.                                ; for future reference.
  2043.  
  2044.        CALL    L1C8C           ; routine EXPT-EXP checks that a string
  2045.                                ; expression follows and stacks the
  2046.                                ; parameters in run-time.
  2047.  
  2048.        CALL    L2530           ; routine SYNTAX-Z
  2049.        JR      Z,L0652         ; forward to SA-DATA if checking syntax.
  2050.  
  2051.        LD      BC,$0011        ; presume seventeen bytes for a header.
  2052.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2053.        AND     A               ; test for zero - SAVE.
  2054.        JR      Z,L0621         ; forward to SA-SPACE if so.
  2055.  
  2056.        LD      C,$22           ; else double length to thirty four.
  2057.  
  2058. ;; SA-SPACE
  2059. L0621:  RST     30H             ; BC-SPACES creates 17/34 bytes in workspace.
  2060.  
  2061.        PUSH    DE              ; transfer the start of new space to
  2062.        POP     IX              ; the available index register.
  2063.  
  2064. ; ten spaces are required for the default filename but it is simpler to
  2065. ; overwrite the first file-type indicator byte as well.
  2066.  
  2067.        LD      B,$0B           ; set counter to eleven.
  2068.        LD      A,$20           ; prepare a space.
  2069.  
  2070. ;; SA-BLANK
  2071. L0629:  LD      (DE),A          ; set workspace location to space.
  2072.        INC     DE              ; next location.
  2073.        DJNZ    L0629           ; loop back to SA-BLANK till all eleven done.
  2074.  
  2075.        LD      (IX+$01),$FF    ; set first byte of ten character filename
  2076.                                ; to $FF as a default to signal null string.
  2077.  
  2078.        CALL    L2BF1           ; routine STK-FETCH fetches the filename
  2079.                                ; parameters from the calculator stack.
  2080.                                ; length of string in BC.
  2081.                                ; start of string in DE.
  2082.  
  2083.        LD      HL,$FFF6        ; prepare the value minus ten.
  2084.        DEC     BC              ; decrement length.
  2085.                                ; ten becomes nine, zero becomes $FFFF.
  2086.        ADD     HL,BC           ; trial addition.
  2087.        INC     BC              ; restore true length.
  2088.        JR      NC,L064B        ; forward to SA-NAME if length is one to ten.
  2089.  
  2090. ; the filename is more than ten characters in length or the null string.
  2091.  
  2092.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2093.        AND     A               ; test for zero - SAVE.
  2094.        JR      NZ,L0644        ; forward to SA-NULL if not the SAVE command.
  2095.  
  2096. ; but no more than ten characters are allowed for SAVE.
  2097. ; The first ten characters of any other command parameter are acceptable.
  2098. ; Weird, but necessary, if saving to sectors.
  2099. ; Note. the golden rule that there are no restriction on anything is broken.
  2100.  
  2101. ;; REPORT-Fa
  2102. L0642:  RST     08H             ; ERROR-1
  2103.        DB    $0E             ; Error Report: Invalid file name
  2104.  
  2105. ; continue with LOAD, MERGE, VERIFY and also SAVE within ten character limit.
  2106.  
  2107. ;; SA-NULL
  2108. L0644:  LD      A,B             ; test length of filename
  2109.        OR      C               ; for zero.
  2110.        JR      Z,L0652         ; forward to SA-DATA if so using the 255
  2111.                                ; indicator followed by spaces.
  2112.  
  2113.        LD      BC,$000A        ; else trim length to ten.
  2114.  
  2115. ; other paths rejoin here with BC holding length in range 1 - 10.
  2116.  
  2117. ;; SA-NAME
  2118. L064B:  PUSH    IX              ; push start of file descriptor.
  2119.        POP     HL              ; and pop into HL.
  2120.  
  2121.        INC     HL              ; HL now addresses first byte of filename.
  2122.        EX      DE,HL           ; transfer destination address to DE, start
  2123.                                ; of string in command to HL.
  2124.        LDIR                    ; copy up to ten bytes
  2125.                                ; if less than ten then trailing spaces follow.
  2126.  
  2127. ; the case for the null string rejoins here.
  2128.  
  2129. ;; SA-DATA
  2130. L0652:  RST     18H             ; GET-CHAR
  2131.        CP      $E4             ; is character after filename the token 'DATA' ?
  2132.        JR      NZ,L06A0        ; forward to SA-SCR$ to consider SCREEN$ if
  2133.                                ; not.
  2134.  
  2135. ; continue to consider DATA.
  2136.  
  2137.        LD      A,($5C74)       ; fetch command from T_ADDR
  2138.        CP      $03             ; is it 'VERIFY' ?
  2139.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2140.                                ; 'Nonsense in BASIC'
  2141.                                ; VERIFY "d" DATA is not allowed.
  2142.  
  2143. ; continue with SAVE, LOAD, MERGE of DATA.
  2144.  
  2145.        RST     20H             ; NEXT-CHAR
  2146.        CALL    L28B2           ; routine LOOK-VARS searches variables area
  2147.                                ; returning with carry reset if found or
  2148.                                ; checking syntax.
  2149.        SET     7,C             ; this converts a simple string to a
  2150.                                ; string array. The test for an array or string
  2151.                                ; comes later.
  2152.        JR      NC,L0672        ; forward to SA-V-OLD if variable found.
  2153.  
  2154.        LD      HL,$0000        ; set destination to zero as not fixed.
  2155.        LD      A,($5C74)       ; fetch command from T_ADDR
  2156.        DEC     A               ; test for 1 - LOAD
  2157.        JR      Z,L0685         ; forward to SA-V-NEW with LOAD DATA.
  2158.                                ; to load a new array.
  2159.  
  2160. ; otherwise the variable was not found in run-time with SAVE/MERGE.
  2161.  
  2162. ;; REPORT-2a
  2163. L0670:  RST     08H             ; ERROR-1
  2164.        DB    $01             ; Error Report: Variable not found
  2165.  
  2166. ; continue with SAVE/LOAD  DATA
  2167.  
  2168. ;; SA-V-OLD
  2169. L0672:  JP      NZ,L1C8A        ; to REPORT-C if not an array variable.
  2170.                                ; or erroneously a simple string.
  2171.                                ; 'Nonsense in BASIC'
  2172.  
  2173.  
  2174.        CALL    L2530           ; routine SYNTAX-Z
  2175.        JR      Z,L0692         ; forward to SA-DATA-1 if checking syntax.
  2176.  
  2177.        INC     HL              ; step past single character variable name.
  2178.        LD      A,(HL)          ; fetch low byte of length.
  2179.        LD      (IX+$0B),A      ; place in descriptor.
  2180.        INC     HL              ; point to high byte.
  2181.        LD      A,(HL)          ; and transfer that
  2182.        LD      (IX+$0C),A      ; to descriptor.
  2183.        INC     HL              ; increase pointer within variable.
  2184.  
  2185. ;; SA-V-NEW
  2186. L0685:  LD      (IX+$0E),C      ; place character array name in  header.
  2187.        LD      A,$01           ; default to type numeric.
  2188.        BIT     6,C             ; test result from look-vars.
  2189.        JR      Z,L068F         ; forward to SA-V-TYPE if numeric.
  2190.  
  2191.        INC     A               ; set type to 2 - string array.
  2192.  
  2193. ;; SA-V-TYPE
  2194. L068F:  LD      (IX+$00),A      ; place type 0, 1 or 2 in descriptor.
  2195.  
  2196. ;; SA-DATA-1
  2197. L0692:  EX      DE,HL           ; save var pointer in DE
  2198.  
  2199.        RST     20H             ; NEXT-CHAR
  2200.        CP      $29             ; is character ')' ?
  2201.        JR      NZ,L0672        ; back if not to SA-V-OLD to report
  2202.                                ; 'Nonsense in BASIC'
  2203.  
  2204.        RST     20H             ; NEXT-CHAR advances character address.
  2205.        CALL    L1BEE           ; routine CHECK-END errors if not end of
  2206.                                ; the statement.
  2207.  
  2208.        EX      DE,HL           ; bring back variables data pointer.
  2209.        JP      L075A           ; jump forward to SA-ALL
  2210.  
  2211. ; ---
  2212. ; the branch was here to consider a 'SCREEN$', the display file.
  2213.  
  2214. ;; SA-SCR$
  2215. L06A0:  CP      $AA             ; is character the token 'SCREEN$' ?
  2216.        JR      NZ,L06C3        ; forward to SA-CODE if not.
  2217.  
  2218.        LD      A,($5C74)       ; fetch command from T_ADDR
  2219.        CP      $03             ; is it MERGE ?
  2220.        JP       Z,L1C8A        ; jump to REPORT-C if so.
  2221.                                ; 'Nonsense in BASIC'
  2222.  
  2223. ; continue with SAVE/LOAD/VERIFY SCREEN$.
  2224.  
  2225.        RST     20H             ; NEXT-CHAR
  2226.        CALL    L1BEE           ; routine CHECK-END errors if not at end of
  2227.                                ; statement.
  2228.  
  2229. ; continue in runtime.
  2230.  
  2231.        LD      (IX+$0B),$00    ; set descriptor length
  2232.        LD      (IX+$0C),$1B    ; to $1b00 to include bitmaps and attributes.
  2233.  
  2234.        LD      HL,$4000        ; set start to display file start.
  2235.        LD      (IX+$0D),L      ; place start in
  2236.        LD      (IX+$0E),H      ; the descriptor.
  2237.        JR      L0710           ; forward to SA-TYPE-3
  2238.  
  2239. ; ---
  2240. ; the branch was here to consider CODE.
  2241.  
  2242. ;; SA-CODE
  2243. L06C3:  CP      $AF             ; is character the token 'CODE' ?
  2244.        JR      NZ,L0716        ; forward if not to SA-LINE to consider an
  2245.                                ; auto-started BASIC program.
  2246.  
  2247.        LD      A,($5C74)       ; fetch command from T_ADDR
  2248.        CP      $03             ; is it MERGE ?
  2249.        JP      Z,L1C8A         ; jump forward to REPORT-C if so.
  2250.                                ; 'Nonsense in BASIC'
  2251.  
  2252.  
  2253.        RST     20H             ; NEXT-CHAR advances character address.
  2254.        CALL    L2048           ; routine PR-ST-END checks if a carriage
  2255.                                ; return or ':' follows.
  2256.        JR      NZ,L06E1        ; forward to SA-CODE-1 if there are parameters.
  2257.  
  2258.        LD      A,($5C74)       ; else fetch the command from T_ADDR.
  2259.        AND     A               ; test for zero - SAVE without a specification.
  2260.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2261.                                ; 'Nonsense in BASIC'
  2262.  
  2263. ; for LOAD/VERIFY put zero on stack to signify handle at location saved from.
  2264.  
  2265.        CALL    L1CE6           ; routine USE-ZERO
  2266.        JR      L06F0           ; forward to SA-CODE-2
  2267.  
  2268. ; ---
  2269. ; if there are more characters after CODE expect start and possibly length.
  2270.  
  2271. ;; SA-CODE-1
  2272. L06E1:  CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2273.                                ; expression and stacks it in run-time.
  2274.  
  2275.        RST     18H             ; GET-CHAR
  2276.        CP      $2C             ; does a comma follow ?
  2277.        JR      Z,L06F5         ; forward if so to SA-CODE-3
  2278.  
  2279. ; else allow saved code to be loaded to a specified address.
  2280.  
  2281.        LD      A,($5C74)       ; fetch command from T_ADDR.
  2282.        AND     A               ; is the command SAVE which requires length ?
  2283.        JP      Z,L1C8A         ; jump to REPORT-C if so.
  2284.                                ; 'Nonsense in BASIC'
  2285.  
  2286. ; the command LOAD code may rejoin here with zero stacked as start.
  2287.  
  2288. ;; SA-CODE-2
  2289. L06F0:  CALL    L1CE6           ; routine USE-ZERO stacks zero for length.
  2290.        JR      L06F9           ; forward to SA-CODE-4
  2291.  
  2292. ; ---
  2293. ; the branch was here with SAVE CODE start,
  2294.  
  2295. ;; SA-CODE-3
  2296. L06F5:  RST     20H             ; NEXT-CHAR advances character address.
  2297.        CALL    L1C82           ; routine EXPT-1NUM checks for expression
  2298.                                ; and stacks in run-time.
  2299.  
  2300. ; paths converge here and nothing must follow.
  2301.  
  2302. ;; SA-CODE-4
  2303. L06F9:  CALL    L1BEE           ; routine CHECK-END errors with extraneous
  2304.                                ; characters and quits if checking syntax.
  2305.  
  2306. ; in run-time there are two 16-bit parameters on the calculator stack.
  2307.  
  2308.        CALL    L1E99           ; routine FIND-INT2 gets length.
  2309.        LD      (IX+$0B),C      ; place length
  2310.        LD      (IX+$0C),B      ; in descriptor.
  2311.        CALL    L1E99           ; routine FIND-INT2 gets start.
  2312.        LD      (IX+$0D),C      ; place start
  2313.        LD      (IX+$0E),B      ; in descriptor.
  2314.        LD      H,B             ; transfer the
  2315.        LD      L,C             ; start to HL also.
  2316.  
  2317. ;; SA-TYPE-3
  2318. L0710:  LD      (IX+$00),$03    ; place type 3 - code in descriptor.
  2319.        JR      L075A           ; forward to SA-ALL.
  2320.  
  2321. ; ---
  2322. ; the branch was here with BASIC to consider an optional auto-start line
  2323. ; number.
  2324.  
  2325. ;; SA-LINE
  2326. L0716:  CP      $CA             ; is character the token 'LINE' ?
  2327.        JR      Z,L0723         ; forward to SA-LINE-1 if so.
  2328.  
  2329. ; else all possibilities have been considered and nothing must follow.
  2330.  
  2331.        CALL    L1BEE           ; routine CHECK-END
  2332.  
  2333. ; continue in run-time to save BASIC without auto-start.
  2334.  
  2335.        LD      (IX+$0E),$80    ; place high line number in descriptor to
  2336.                                ; disable auto-start.
  2337.        JR      L073A           ; forward to SA-TYPE-0 to save program.
  2338.  
  2339. ; ---
  2340. ; the branch was here to consider auto-start.
  2341.  
  2342. ;; SA-LINE-1
  2343. L0723:  LD      A,($5C74)       ; fetch command from T_ADDR
  2344.        AND     A               ; test for SAVE.
  2345.        JP      NZ,L1C8A        ; jump forward to REPORT-C with anything else.
  2346.                                ; 'Nonsense in BASIC'
  2347.  
  2348. ;
  2349.  
  2350.        RST     20H             ; NEXT-CHAR
  2351.        CALL    L1C82           ; routine EXPT-1NUM checks for numeric
  2352.                                ; expression and stacks in run-time.
  2353.        CALL    L1BEE           ; routine CHECK-END quits if syntax path.
  2354.        CALL    L1E99           ; routine FIND-INT2 fetches the numeric
  2355.                                ; expression.
  2356.        LD      (IX+$0D),C      ; place the auto-start
  2357.        LD      (IX+$0E),B      ; line number in the descriptor.
  2358.  
  2359. ; Note. this isn't checked, but is subsequently handled by the system.
  2360. ; If the user typed 40000 instead of 4000 then it won't auto-start
  2361. ; at line 4000, or indeed, at all.
  2362.  
  2363. ; continue to save program and any variables.
  2364.  
  2365. ;; SA-TYPE-0
  2366. L073A:  LD      (IX+$00),$00    ; place type zero - program in descriptor.
  2367.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  2368.         LD      DE,($5C53)      ; fetch PROG to DE.
  2369.  
  2370. ;===============================
  2371.                 RST 0X08
  2372.                 DB TAPE_INIT
  2373.                 NOP
  2374. ;               SCF             ; set carry flag to calculate from end of
  2375.                                 ; variables E_LINE -1.
  2376. ;               SBC HL,DE       ; subtract to give total length.
  2377.  
  2378.         LD      (IX+$0B),L      ; place total length
  2379.         LD      (IX+$0C),H      ; in descriptor.
  2380.         LD      HL,($5C4B)      ; load HL from system variable VARS
  2381.         SBC     HL,DE           ; subtract to give program length.
  2382.         LD      (IX+$0F),L      ; place length of program
  2383.         LD      (IX+$10),H      ; in the descriptor.
  2384.         EX      DE,HL           ; start to HL, length to DE.
  2385.  
  2386. ;; SA-ALL
  2387. L075A:  LD      A,($5C74)       ; fetch command from T_ADDR
  2388.         AND     A               ; test for zero - SAVE.
  2389.         JP      Z,L0970         ; jump forward to SA-CONTRL with SAVE  ->
  2390.  
  2391. ; ---
  2392. ; continue with LOAD, MERGE and VERIFY.
  2393.  
  2394.         PUSH    HL              ; save start.
  2395.         LD      BC,$0011        ; prepare to add seventeen
  2396.         ADD     IX,BC           ; to point IX at second descriptor.
  2397.  
  2398. ;; LD-LOOK-H
  2399. L0767:  PUSH    IX              ; save IX
  2400.         LD      DE,$0011        ; seventeen bytes
  2401.         XOR     A               ; reset zero flag
  2402.         SCF                     ; set carry flag
  2403.         CALL    L0556           ; routine LD-BYTES loads a header from tape
  2404.                                 ; to second descriptor.
  2405.         POP     IX              ; restore IX.
  2406.         JR      NC,L0767        ; loop back to LD-LOOK-H until header found.
  2407.  
  2408.         LD      A,$FE           ; select system channel 'S'
  2409.         CALL    L1601           ; routine CHAN-OPEN opens it.
  2410.  
  2411.         LD      (IY+$52),$03    ; set SCR_CT to 3 lines.
  2412.  
  2413.         LD      C,$80           ; C has bit 7 set to indicate type mismatch as
  2414.                                 ; a default startpoint.
  2415.  
  2416.         LD      A,(IX+$00)      ; fetch loaded header type to A
  2417.         CP      (IX-$11)        ; compare with expected type.
  2418.         JR      NZ,L078A        ; forward to LD-TYPE with mis-match.
  2419.  
  2420.         LD      C,$F6           ; set C to minus ten - will count characters
  2421.                                 ; up to zero.
  2422.  
  2423. ;; LD-TYPE
  2424. L078A:  CP      $04             ; check if type in acceptable range 0 - 3.
  2425.         JR      NC,L0767        ; back to LD-LOOK-H with 4 and over.
  2426.  
  2427. ; else A indicates type 0-3.
  2428.  
  2429.         LD      DE,L09C0        ; address base of last 4 tape messages
  2430.         PUSH    BC              ; save BC
  2431.         CALL    L0C0A           ; routine PO-MSG outputs relevant message.
  2432.                                 ; Note. all messages have a leading newline.
  2433.         POP     BC              ; restore BC
  2434.  
  2435.         PUSH    IX              ; transfer IX,
  2436.         POP     DE              ; the 2nd descriptor, to DE.
  2437.         LD      HL,$FFF0        ; prepare minus seventeen.
  2438.         ADD     HL,DE           ; add to point HL to 1st descriptor.
  2439.         LD      B,$0A           ; the count will be ten characters for the
  2440.                                 ; filename.
  2441.  
  2442.         LD      A,(HL)          ; fetch first character and test for
  2443.         INC     A               ; value 255.
  2444.         JR      NZ,L07A6        ; forward to LD-NAME if not the wildcard.
  2445.  
  2446. ; but if it is the wildcard, then add ten to C which is minus ten for a type
  2447. ; match or -128 for a type mismatch. Although characters have to be counted
  2448. ; bit 7 of C will not alter from state set here.
  2449.  
  2450.         LD      A,C             ; transfer $F6 or $80 to A
  2451.         ADD     A,B             ; add $0A
  2452.         LD      C,A             ; place result, zero or -118, in C.
  2453.  
  2454. ; At this point we have either a type mismatch, a wildcard match or ten
  2455. ; characters to be counted. The characters must be shown on the screen.
  2456.  
  2457. ;; LD-NAME
  2458. L07A6:  INC     DE              ; address next input character
  2459.         LD      A,(DE)          ; fetch character
  2460.         CP      (HL)            ; compare to expected
  2461.         INC     HL              ; address next expected character
  2462.         JR      NZ,L07AD        ; forward to LD-CH-PR with mismatch
  2463.  
  2464.         INC     C               ; increment matched character count
  2465.  
  2466. ;; LD-CH-PR
  2467. L07AD:  RST     10H             ; PRINT-A prints character
  2468.         DJNZ    L07A6           ; loop back to LD-NAME for ten characters.
  2469.  
  2470. ; if ten characters matched and the types previously matched then C will
  2471. ; now hold zero.
  2472.  
  2473.         BIT     7,C             ; test if all matched
  2474.         JR      NZ,L0767        ; back to LD-LOOK-H if not
  2475.  
  2476. ; else print a terminal carriage return.
  2477.  
  2478.         LD      A,$0D           ; prepare carriage return.
  2479.         RST     10H             ; PRINT-A outputs it.
  2480.  
  2481. ; The various control routines for LOAD, VERIFY and MERGE are executed
  2482. ; during the one-second gap following the header on tape.
  2483.  
  2484.         POP     HL              ; restore xx
  2485.         LD      A,(IX+$00)      ; fetch incoming type
  2486.         CP      $03             ; compare with CODE
  2487.         JR      Z,L07CB         ; forward to VR-CONTROL if it is CODE.
  2488.  
  2489. ;  type is a program or an array.
  2490.  
  2491.         LD      A,($5C74)       ; fetch command from T_ADDR
  2492.         DEC     A               ; was it LOAD ?
  2493.         JP      Z,L0808         ; JUMP forward to LD-CONTRL if so to
  2494.                                 ; load BASIC or variables.
  2495.  
  2496.         CP      $02             ; was command MERGE ?
  2497.         JP      Z,L08B6         ; jump forward to ME-CONTRL if so.
  2498.  
  2499. ; else continue into VERIFY control routine to verify.
  2500.  
  2501. ; ---------------------
  2502. ; Handle VERIFY control
  2503. ; ---------------------
  2504. ; There are two branches to this routine.
  2505. ; 1) From above to verify a program or array
  2506. ; 2) from earlier with no carry to load or verify code.
  2507.  
  2508. ;; VR-CONTROL
  2509. L07CB:  PUSH    HL              ; save pointer to data.
  2510.         LD      L,(IX-$06)      ; fetch length of old data
  2511.         LD      H,(IX-$05)      ; to HL.
  2512.         LD      E,(IX+$0B)      ; fetch length of new data
  2513.         LD      D,(IX+$0C)      ; to DE.
  2514.         LD      A,H             ; check length of old
  2515.         OR      L               ; for zero.
  2516.         JR      Z,L07E9         ; forward to VR-CONT-1 if length unspecified
  2517.                                 ; e.g LOAD "x" CODE
  2518.  
  2519. ; as opposed to, say, LOAD 'x' CODE 32768,300.
  2520.  
  2521.         SBC     HL,DE           ; subtract the two lengths.
  2522.         JR      C,L0806         ; forward to REPORT-R if the length on tape is
  2523.                                 ; larger than that specified in command.
  2524.                                 ; 'Tape loading error'
  2525.  
  2526.         JR      Z,L07E9         ; forward to VR-CONT-1 if lengths match.
  2527.  
  2528. ; a length on tape shorter than expected is not allowed for CODE
  2529.  
  2530.         LD      A,(IX+$00)      ; else fetch type from tape.
  2531.         CP      $03             ; is it CODE ?
  2532.         JR      NZ,L0806        ; forward to REPORT-R if so
  2533.                                 ; 'Tape loading error'
  2534.  
  2535. ;; VR-CONT-1
  2536. L07E9:  POP     HL              ; pop pointer to data
  2537.         LD      A,H             ; test for zero
  2538.         OR      L               ; e.g. LOAD 'x' CODE
  2539.         JR      NZ,L07F4        ; forward to VR-CONT-2 if destination specified.
  2540.  
  2541.         LD      L,(IX+$0D)      ; else use the destination in the header
  2542.         LD      H,(IX+$0E)      ; and load code at address saved from.
  2543.  
  2544. ;; VR-CONT-2
  2545. L07F4:  PUSH    HL              ; push pointer to start of data block.
  2546.         POP     IX              ; transfer to IX.
  2547.         LD      A,($5C74)       ; fetch reduced command from T_ADDR
  2548.         CP      $02             ; is it VERIFY ?
  2549.         SCF                     ; prepare a set carry flag
  2550.         JR      NZ,L0800        ; skip to VR-CONT-3 if not
  2551.  
  2552.         AND     A               ; clear carry flag for VERIFY so that
  2553.                                 ; data is not loaded.
  2554.  
  2555. ;; VR-CONT-3
  2556. L0800:  LD      A,$FF           ; signal data block to be loaded
  2557.  
  2558. ; -----------------
  2559. ; Load a data block
  2560. ; -----------------
  2561. ; This routine is called from 3 places other than above to load a data block.
  2562. ; In all cases the accumulator is first set to $FF so the routine could be
  2563. ; called at the previous instruction.
  2564.  
  2565. ;; LD-BLOCK
  2566. L0802:  CALL    L0556           ; routine LD-BYTES
  2567.         RET     C               ; return if successful.
  2568.  
  2569.  
  2570. ;; REPORT-R
  2571. L0806:  RST     08H             ; ERROR-1
  2572.         DB    $1A             ; Error Report: Tape loading error
  2573.  
  2574. ; -------------------
  2575. ; Handle LOAD control
  2576. ; -------------------
  2577. ; This branch is taken when the command is LOAD with type 0, 1 or 2.
  2578.  
  2579. ;; LD-CONTRL
  2580. L0808:  LD      E,(IX+$0B)      ; fetch length of found data block
  2581.         LD      D,(IX+$0C)      ; from 2nd descriptor.
  2582.         PUSH    HL              ; save destination
  2583.         LD      A,H             ; test for zero
  2584.         OR      L               ;
  2585.         JR      NZ,L0819        ; forward if not to LD-CONT-1
  2586.  
  2587.         INC     DE              ; increase length
  2588.         INC     DE              ; for letter name
  2589.         INC     DE              ; and 16-bit length
  2590.         EX      DE,HL           ; length to HL,
  2591.         JR      L0825           ; forward to LD-CONT-2
  2592.  
  2593. ; ---
  2594.  
  2595. ;; LD-CONT-1
  2596. L0819:  LD      L,(IX-$06)      ; fetch length from
  2597.         LD      H,(IX-$05)      ; the first header.
  2598.         EX      DE,HL           ;
  2599.         SCF                     ; set carry flag
  2600.         SBC     HL,DE           ;
  2601.         JR      C,L082E         ; to LD-DATA
  2602.  
  2603. ;; LD-CONT-2
  2604. L0825:  LD      DE,$0005        ; allow overhead of five bytes.
  2605.         ADD     HL,DE           ; add in the difference in data lengths.
  2606.         LD      B,H             ; transfer to
  2607.         LD      C,L             ; the BC register pair
  2608.         CALL    L1F05           ; routine TEST-ROOM fails if not enough room.
  2609.  
  2610. ;; LD-DATA
  2611. L082E:  POP     HL              ; pop destination
  2612.         LD      A,(IX+$00)      ; fetch type 0, 1 or 2.
  2613.         AND     A               ; test for program and variables.
  2614.         JR      Z,L0873         ; forward if so to LD-PROG
  2615.  
  2616. ; the type is a numeric or string array.
  2617.  
  2618.         LD      A,H             ; test the destination for zero
  2619.         OR      L               ; indicating variable does not already exist.
  2620.         JR      Z,L084C         ; forward if so to LD-DATA-1
  2621.  
  2622. ; else the destination is the first dimension within the array structure
  2623.  
  2624.         DEC     HL              ; address high byte of total length
  2625.         LD      B,(HL)          ; transfer to B.
  2626.         DEC     HL              ; address low byte of total length.
  2627.         LD      C,(HL)          ; transfer to C.
  2628.         DEC     HL              ; point to letter of variable.
  2629.         INC     BC              ; adjust length to
  2630.         INC     BC              ; include these
  2631.         INC     BC              ; three bytes also.
  2632.         LD      ($5C5F),IX      ; save header pointer in X_PTR.
  2633.         CALL    L19E8           ; routine RECLAIM-2 reclaims the old variable
  2634.                                 ; sliding workspace including the two headers
  2635.                                 ; downwards.
  2636.         LD      IX,($5C5F)      ; reload IX from X_PTR which will have been
  2637.                                 ; adjusted down by POINTERS routine.
  2638.  
  2639. ;; LD-DATA-1
  2640. L084C:  LD      HL,($5C59)      ; address E_LINE
  2641.         DEC     HL              ; now point to the $80 variables end-marker.
  2642.         LD      C,(IX+$0B)      ; fetch new data length
  2643.         LD      B,(IX+$0C)      ; from 2nd header.
  2644.         PUSH    BC              ; * save it.
  2645.         INC     BC              ; adjust the
  2646.         INC     BC              ; length to include
  2647.         INC     BC              ; letter name and total length.
  2648.         LD      A,(IX-$03)      ; fetch letter name from old header.
  2649.         PUSH    AF              ; preserve accumulator though not corrupted.
  2650.  
  2651.         CALL    L1655           ; routine MAKE-ROOM creates space for variable
  2652.                                 ; sliding workspace up. IX no longer addresses
  2653.                                 ; anywhere meaningful.
  2654.         INC     HL              ; point to first new location.
  2655.  
  2656.         POP     AF              ; fetch back the letter name.
  2657.         LD      (HL),A          ; place in first new location.
  2658.         POP     DE              ; * pop the data length.
  2659.         INC     HL              ; address 2nd location
  2660.         LD      (HL),E          ; store low byte of length.
  2661.         INC     HL              ; address next.
  2662.         LD      (HL),D          ; store high byte.
  2663.         INC     HL              ; address start of data.
  2664.         PUSH    HL              ; transfer address
  2665.         POP     IX              ; to IX register pair.
  2666.         SCF                     ; set carry flag indicating load not verify.
  2667.         LD      A,$FF           ; signal data not header.
  2668.         JP      L0802           ; JUMP back to LD-BLOCK
  2669.  
  2670. ; -----------------
  2671. ; the branch is here when a program as opposed to an array is to be loaded.
  2672.  
  2673. ;; LD-PROG
  2674. L0873:  EX      DE,HL           ; transfer dest to DE.
  2675.         LD      HL,($5C59)      ; address E_LINE
  2676.         DEC     HL              ; now variables end-marker.
  2677.         LD      ($5C5F),IX      ; place the IX header pointer in X_PTR
  2678.         LD      C,(IX+$0B)      ; get new length
  2679.         LD      B,(IX+$0C)      ; from 2nd header
  2680.         PUSH    BC              ; and save it.
  2681.  
  2682.         CALL    L19E5           ; routine RECLAIM-1 reclaims program and vars.
  2683.                                 ; adjusting X-PTR.
  2684.  
  2685.         POP     BC              ; restore new length.
  2686.         PUSH    HL              ; * save start
  2687.         PUSH    BC              ; ** and length.
  2688.  
  2689.         CALL    L1655           ; routine MAKE-ROOM creates the space.
  2690.  
  2691.         LD      IX,($5C5F)      ; reload IX from adjusted X_PTR
  2692.         INC     HL              ; point to start of new area.
  2693.         LD      C,(IX+$0F)      ; fetch length of BASIC on tape
  2694.         LD      B,(IX+$10)      ; from 2nd descriptor
  2695.         ADD     HL,BC           ; add to address the start of variables.
  2696.         LD      ($5C4B),HL      ; set system variable VARS
  2697.  
  2698.         LD      H,(IX+$0E)      ; fetch high byte of autostart line number.
  2699.         LD      A,H             ; transfer to A
  2700.         AND     $C0             ; test if greater than $3F.
  2701.         JR      NZ,L08AD        ; forward to LD-PROG-1 if so with no autostart.
  2702.  
  2703.         LD      L,(IX+$0D)      ; else fetch the low byte.
  2704.         LD      ($5C42),HL      ; set sytem variable to line number NEWPPC
  2705.         LD      (IY+$0A),$00    ; set statement NSPPC to zero.
  2706.  
  2707. ;; LD-PROG-1
  2708. L08AD:  POP     DE              ; ** pop the length
  2709.         POP     IX              ; * and start.
  2710.         SCF                     ; set carry flag
  2711.         LD      A,$FF           ; signal data as opposed to a header.
  2712.         JP      L0802           ; jump back to LD-BLOCK
  2713.  
  2714. ; --------------------
  2715. ; Handle MERGE control
  2716. ; --------------------
  2717. ; the branch was here to merge a program and its variables or an array.
  2718. ;
  2719.  
  2720. ;; ME-CONTRL
  2721. L08B6:  LD      C,(IX+$0B)      ; fetch length
  2722.         LD      B,(IX+$0C)      ; of data block on tape.
  2723.         PUSH    BC              ; save it.
  2724.         INC     BC              ; one for the pot.
  2725.  
  2726.         RST     30H             ; BC-SPACES creates room in workspace.
  2727.                                 ; HL addresses last new location.
  2728.         LD      (HL),$80        ; place end-marker at end.
  2729.         EX      DE,HL           ; transfer first location to HL.
  2730.         POP     DE              ; restore length to DE.
  2731.         PUSH    HL              ; save start.
  2732.  
  2733.         PUSH    HL              ; and transfer it
  2734.         POP     IX              ; to IX register.
  2735.         SCF                     ; set carry flag to load data on tape.
  2736.         LD      A,$FF           ; signal data not a header.
  2737.         CALL    L0802           ; routine LD-BLOCK loads to workspace.
  2738.         POP     HL              ; restore first location in workspace to HL.
  2739. X08CE   LD      DE,($5C53)      ; set DE from system variable PROG.
  2740.  
  2741. ; now enter a loop to merge the data block in workspace with the program and
  2742. ; variables.
  2743.  
  2744. ;; ME-NEW-LP
  2745. L08D2:  LD      A,(HL)          ; fetch next byte from workspace.
  2746.         AND     $C0             ; compare with $3F.
  2747.         JR      NZ,L08F0        ; forward to ME-VAR-LP if a variable or
  2748.                                 ; end-marker.
  2749.  
  2750. ; continue when HL addresses a BASIC line number.
  2751.  
  2752. ;; ME-OLD-LP
  2753. L08D7:  LD      A,(DE)          ; fetch high byte from program area.
  2754.         INC     DE              ; bump prog address.
  2755.         CP      (HL)            ; compare with that in workspace.
  2756.         INC     HL              ; bump workspace address.
  2757.         JR      NZ,L08DF        ; forward to ME-OLD-L1 if high bytes don't match
  2758.  
  2759.         LD      A,(DE)          ; fetch the low byte of program line number.
  2760.         CP      (HL)            ; compare with that in workspace.
  2761.  
  2762. ;; ME-OLD-L1
  2763. L08DF:  DEC     DE              ; point to start of
  2764.         DEC     HL              ; respective lines again.
  2765.         JR      NC,L08EB        ; forward to ME-NEW-L2 if line number in
  2766.                                 ; workspace is less than or equal to current
  2767.                                 ; program line as has to be added to program.
  2768.  
  2769.         PUSH    HL              ; else save workspace pointer.
  2770.         EX      DE,HL           ; transfer prog pointer to HL
  2771.         CALL    L19B8           ; routine NEXT-ONE finds next line in DE.
  2772.         POP     HL              ; restore workspace pointer
  2773.         JR      L08D7           ; back to ME-OLD-LP until destination position
  2774.                                 ; in program area found.
  2775.  
  2776. ; ---
  2777. ; the branch was here with an insertion or replacement point.
  2778.  
  2779. ;; ME-NEW-L2
  2780. L08EB:  CALL    L092C           ; routine ME-ENTER enters the line
  2781.         JR      L08D2           ; loop back to ME-NEW-LP.
  2782.  
  2783. ; ---
  2784. ; the branch was here when the location in workspace held a variable.
  2785.  
  2786. ;; ME-VAR-LP
  2787. L08F0:  LD      A,(HL)          ; fetch first byte of workspace variable.
  2788.         LD      C,A             ; copy to C also.
  2789.         CP      $80             ; is it the end-marker ?
  2790.         RET     Z               ; return if so as complete.  >>>>>
  2791.  
  2792.         PUSH    HL              ; save workspace area pointer.
  2793.         LD      HL,($5C4B)      ; load HL with VARS - start of variables area.
  2794.  
  2795. ;; ME-OLD-VP
  2796. L08F9:  LD      A,(HL)          ; fetch first byte.
  2797.         CP      $80             ; is it the end-marker ?
  2798.         JR      Z,L0923         ; forward if so to ME-VAR-L2 to add
  2799.                                 ; variable at end of variables area.
  2800.  
  2801.         CP      C               ; compare with variable in workspace area.
  2802.         JR      Z,L0909         ; forward to ME-OLD-V2 if a match to replace.
  2803.  
  2804. ; else entire variables area has to be searched.
  2805.  
  2806. ;; ME-OLD-V1
  2807. L0901:  PUSH    BC              ; save character in C.
  2808.         CALL    L19B8           ; routine NEXT-ONE gets following variable
  2809.                                 ; address in DE.
  2810.         POP     BC              ; restore character in C
  2811.         EX      DE,HL           ; transfer next address to HL.
  2812.         JR      L08F9           ; loop back to ME-OLD-VP
  2813.  
  2814. ; ---
  2815. ; the branch was here when first characters of name matched.
  2816.  
  2817. ;; ME-OLD-V2
  2818. L0909:  AND     $E0             ; keep bits 11100000
  2819.         CP      $A0             ; compare   10100000 - a long-named variable.
  2820.  
  2821.         JR      NZ,L0921        ; forward to ME-VAR-L1 if just one-character.
  2822.  
  2823. ; but long-named variables have to be matched character by character.
  2824.  
  2825.         POP     DE              ; fetch workspace 1st character pointer
  2826.         PUSH    DE              ; and save it on the stack again.
  2827.         PUSH    HL              ; save variables area pointer on stack.
  2828.  
  2829. ;; ME-OLD-V3
  2830. L0912:  INC     HL              ; address next character in vars area.
  2831.         INC     DE              ; address next character in workspace area.
  2832.         LD      A,(DE)          ; fetch workspace character.
  2833.         CP      (HL)            ; compare to variables character.
  2834.         JR      NZ,L091E        ; forward to ME-OLD-V4 with a mismatch.
  2835.  
  2836.         RLA                     ; test if the terminal inverted character.
  2837.         JR      NC,L0912        ; loop back to ME-OLD-V3 if more to test.
  2838.  
  2839. ; otherwise the long name matches in its entirety.
  2840.  
  2841.         POP     HL              ; restore pointer to first character of variable
  2842.         JR      L0921           ; forward to ME-VAR-L1
  2843.  
  2844. ; ---
  2845. ; the branch is here when two characters don't match
  2846.  
  2847. ;; ME-OLD-V4
  2848. L091E:  POP     HL              ; restore the prog/vars pointer.
  2849.         JR      L0901           ; back to ME-OLD-V1 to resume search.
  2850.  
  2851. ; ---
  2852. ; branch here when variable is to replace an existing one
  2853.  
  2854. ;; ME-VAR-L1
  2855. L0921:  LD      A,$FF           ; indicate a replacement.
  2856.  
  2857. ; this entry point is when A holds $80 indicating a new variable.
  2858.  
  2859. ;; ME-VAR-L2
  2860. L0923:  POP     DE              ; pop workspace pointer.
  2861.         EX      DE,HL           ; now make HL workspace pointer, DE vars pointer
  2862.         INC     A               ; zero flag set if replacement.
  2863.         SCF                     ; set carry flag indicating a variable not a
  2864.                                 ; program line.
  2865.         CALL    L092C           ; routine ME-ENTER copies variable in.
  2866.         JR      L08F0           ; loop back to ME-VAR-LP
  2867.  
  2868. ; ------------------------
  2869. ; Merge a Line or Variable
  2870. ; ------------------------
  2871. ; A BASIC line or variable is inserted at the current point. If the line numbers
  2872. ; or variable names match (zero flag set) then a replacement takes place.
  2873.  
  2874. ;; ME-ENTER
  2875. L092C:  JR      NZ,L093E        ; forward to ME-ENT-1 for insertion only.
  2876.  
  2877. ; but the program line or variable matches so old one is reclaimed.
  2878.  
  2879.         EX      AF,AF'          ; save flag??
  2880.        LD      ($5C5F),HL      ; preserve workspace pointer in dynamic X_PTR
  2881.        EX      DE,HL           ; transfer program dest pointer to HL.
  2882.        CALL    L19B8           ; routine NEXT-ONE finds following location
  2883.                                ; in program or variables area.
  2884.        CALL    L19E8           ; routine RECLAIM-2 reclaims the space between.
  2885.        EX      DE,HL           ; transfer program dest pointer back to DE.
  2886.        LD      HL,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2887.        EX      AF,AF'          ; restore flags.
  2888.  
  2889. ; now the new line or variable is entered.
  2890.  
  2891. ;; ME-ENT-1
  2892. L093E:  EX      AF,AF'          ; save or re-save flags.
  2893.        PUSH    DE              ; save dest pointer in prog/vars area.
  2894.        CALL    L19B8           ; routine NEXT-ONE finds next in workspace.
  2895.                                ; gets next in DE, difference in BC.
  2896.                                ; prev addr in HL
  2897.        LD      ($5C5F),HL      ; store pointer in X_PTR
  2898.        LD      HL,($5C53)      ; load HL from system variable PROG
  2899.        EX      (SP),HL         ; swap with prog/vars pointer on stack.
  2900.        PUSH    BC              ; ** save length of new program line/variable.
  2901.        EX      AF,AF'          ; fetch flags back.
  2902.         JR      C,L0955         ; skip to ME-ENT-2 if variable
  2903.  
  2904.         DEC     HL              ; address location before pointer
  2905.         CALL    L1655           ; routine MAKE-ROOM creates room for BASIC line
  2906.         INC     HL              ; address next.
  2907.         JR      L0958           ; forward to ME-ENT-3
  2908.  
  2909. ; ---
  2910.  
  2911. ;; ME-ENT-2
  2912. L0955:  CALL    L1655           ; routine MAKE-ROOM creates room for variable.
  2913.  
  2914. ;; ME-ENT-3
  2915. L0958:  INC     HL              ; address next?
  2916.  
  2917.         POP     BC              ; ** pop length
  2918.         POP     DE              ; * pop value for PROG which may have been
  2919.                                 ; altered by POINTERS if first line.
  2920.         LD      ($5C53),DE      ; set PROG to original value.
  2921.         LD      DE,($5C5F)      ; fetch adjusted workspace pointer from X_PTR
  2922.         PUSH    BC              ; save length
  2923.         PUSH    DE              ; and workspace pointer
  2924.         EX      DE,HL           ; make workspace pointer source, prog/vars
  2925.                                 ; pointer the destination
  2926.         LDIR                    ; copy bytes of line or variable into new area.
  2927.         POP     HL              ; restore workspace pointer.
  2928.         POP     BC              ; restore length.
  2929.         PUSH    DE              ; save new prog/vars pointer.
  2930.         CALL    L19E8           ; routine RECLAIM-2 reclaims the space used
  2931.                                 ; by the line or variable in workspace block
  2932.                                 ; as no longer required and space could be
  2933.                                 ; useful for adding more lines.
  2934.         POP     DE              ; restore the prog/vars pointer
  2935.         RET                     ; return.
  2936.  
  2937. ; -------------------
  2938. ; Handle SAVE control
  2939. ; -------------------
  2940. ; A branch from the main SAVE-ETC routine at SAVE-ALL.
  2941. ; First the header data is saved. Then after a wait of 1 second
  2942. ; the data itself is saved.
  2943. ; HL points to start of data.
  2944. ; IX points to start of descriptor.
  2945.  
  2946. ;; SA-CONTRL
  2947. L0970:  PUSH    HL              ; save start of data
  2948.  
  2949.         LD      A,$FD           ; select system channel 'S'
  2950.         CALL    L1601           ; routine CHAN-OPEN
  2951.  
  2952.         XOR     A               ; clear to address table directly
  2953.         LD      DE,L09A1        ; address: tape-msgs
  2954.         CALL    L0C0A           ; routine PO-MSG -
  2955.                                 ; 'Start tape then press any key.'
  2956.  
  2957.         SET     5,(IY+$02)      ; TV_FLAG  - Signal lower screen requires
  2958.                                 ; clearing
  2959.         CALL    L15D4           ; routine WAIT-KEY
  2960.  
  2961.         PUSH    IX              ; save pointer to descriptor.
  2962.         LD      DE,$0011        ; there are seventeen bytes.
  2963.         XOR     A               ; signal a header.
  2964.         CALL    L04C2           ; routine SA-BYTES
  2965.  
  2966.         POP     IX              ; restore descriptor pointer.
  2967.  
  2968.         LD      B,$32           ; wait for a second - 50 interrupts.
  2969.  
  2970. ;; SA-1-SEC
  2971. L0991:  HALT                    ; wait for interrupt
  2972.         DJNZ    L0991           ; back to SA-1-SEC until pause complete.
  2973.  
  2974.         LD      E,(IX+$0B)      ; fetch length of bytes from the
  2975.         LD      D,(IX+$0C)      ; descriptor.
  2976.  
  2977.         LD      A,$FF           ; signal data bytes.
  2978.  
  2979.         POP     IX              ; retrieve pointer to start
  2980.         JP      L04C2           ; jump back to SA-BYTES
  2981.  
  2982.  
  2983. ; Arrangement of two headers in workspace.
  2984. ; Originally IX addresses first location and only one header is required
  2985. ; when saving.
  2986. ;
  2987. ;   OLD     NEW         PROG   DATA  DATA  CODE
  2988. ;   HEADER  HEADER             num   chr          NOTES.
  2989. ;   ------  ------      ----   ----  ----  ----   -----------------------------
  2990. ;   IX-$11  IX+$00      0      1     2     3      Type.
  2991. ;   IX-$10  IX+$01      x      x     x     x      F  ($FF if filename is null).
  2992. ;   IX-$0F  IX+$02      x      x     x     x      i
  2993. ;   IX-$0E  IX+$03      x      x     x     x      l
  2994. ;   IX-$0D  IX+$04      x      x     x     x      e
  2995. ;   IX-$0C  IX+$05      x      x     x     x      n
  2996. ;   IX-$0B  IX+$06      x      x     x     x      a
  2997. ;   IX-$0A  IX+$07      x      x     x     x      m
  2998. ;   IX-$09  IX+$08      x      x     x     x      e
  2999. ;   IX-$08  IX+$09      x      x     x     x      .
  3000. ;   IX-$07  IX+$0A      x      x     x     x      (terminal spaces).
  3001. ;   IX-$06  IX+$0B      lo     lo    lo    lo     Total  
  3002. ;   IX-$05  IX+$0C      hi     hi    hi    hi     Length of datablock.
  3003. ;   IX-$04  IX+$0D      Auto   -     -     Start  Various
  3004. ;   IX-$03  IX+$0E      Start  a-z   a-z   addr   ($80 if no autostart).
  3005. ;   IX-$02  IX+$0F      lo     -     -     -      Length of Program
  3006. ;   IX-$01  IX+$10      hi     -     -     -      only i.e. without variables.
  3007. ;
  3008.  
  3009.  
  3010. ; ------------------------
  3011. ; Canned cassette messages
  3012. ; ------------------------
  3013. ; The last-character-inverted Cassette messages.
  3014. ; Starts with normal initial step-over byte.
  3015.  
  3016. ;; tape-msgs
  3017. L09A1           DB $80
  3018.                 DC "Start tape, then press any key."    ;DEFM    "Start tape, then press any key"
  3019. L09C0           EQU $-1                                 ;DB    '.'+$80
  3020.                 DB $0D
  3021.                 DC "Program: "                          ;DEFM    "Program:"
  3022.                                                         ;DB    ' '+$80
  3023.                 DB $0D
  3024.                 DC "Number array: "                     ;DEFM    "Number array:"
  3025.                                                         ;DB    ' '+$80
  3026.                 DB $0D
  3027.                 DC "Character array: "                  ;DEFM    "Character array:"
  3028.                                                         ;DB    ' '+$80
  3029.                 DB $0D
  3030.                 DC "Bytes: "                            ;DEFM    "Bytes:"
  3031.                                                         ;DB    ' '+$80
  3032. ;               DB    ' '+$80
  3033.  
  3034.  
  3035. ;**************************************************
  3036. ;** Part 5. SCREEN AND PRINTER HANDLING ROUTINES **
  3037. ;**************************************************
  3038.  
  3039. ; ---------------------
  3040. ; General PRINT routine
  3041. ; ---------------------
  3042. ; This is the routine most often used by the RST 10 restart although the
  3043. ; subroutine is on two occasions called directly when it is known that
  3044. ; output will definitely be to the lower screen.
  3045.  
  3046. ;; PRINT-OUT
  3047. L09F4:  CALL    L0B03           ; routine PO-FETCH fetches print position
  3048.                                 ; to HL register pair.
  3049.         CP      $20             ; is character a space or higher ?
  3050.         JP      NC,L0AD9        ; jump forward to PO-ABLE if so.
  3051.  
  3052.         CP      $06             ; is character in range 00-05 ?
  3053.         JR      C,L0A69         ; to PO-QUEST to print '?' if so.
  3054.  
  3055.         CP      $18             ; is character in range 24d - 31d ?
  3056.         JR      NC,L0A69        ; to PO-QUEST to also print '?' if so.
  3057.  
  3058.         LD      HL,L0A11 - 6    ; address 0A0B - the base address of control
  3059.                                 ; character table - where zero would be.
  3060.         LD      E,A             ; control character 06 - 23d
  3061.         LD      D,$00           ; is transferred to DE.
  3062.  
  3063.         ADD     HL,DE           ; index into table.
  3064.  
  3065.         LD      E,(HL)          ; fetch the offset to routine.
  3066.         ADD     HL,DE           ; add to make HL the address.
  3067.         PUSH    HL              ; push the address.
  3068.         JP      L0B03           ; to PO-FETCH, as the screen/printer position
  3069.                                 ; has been disturbed, and indirectly to
  3070.                                 ; routine on stack.
  3071.  
  3072. ; -----------------------
  3073. ; Control character table
  3074. ; -----------------------
  3075. ; For control characters in the range 6 - 23d the following table
  3076. ; is indexed to provide an offset to the handling routine that
  3077. ; follows the table.
  3078.  
  3079. ;; ctlchrtab
  3080. L0A11:  DB    L0A5F - $       ; 06d offset $4E to Address: PO-COMMA
  3081.         DB    L0A69 - $       ; 07d offset $57 to Address: PO-QUEST
  3082.         DB    L0A23 - $       ; 08d offset $10 to Address: PO-BACK-1
  3083.         DB    L0A3D - $       ; 09d offset $29 to Address: PO-RIGHT
  3084.         DB    L0A69 - $       ; 10d offset $54 to Address: PO-QUEST
  3085.         DB    L0A69 - $       ; 11d offset $53 to Address: PO-QUEST
  3086.         DB    L0A69 - $       ; 12d offset $52 to Address: PO-QUEST
  3087.         DB    L0A4F - $       ; 13d offset $37 to Address: PO-ENTER
  3088.         DB    L0A69 - $       ; 14d offset $50 to Address: PO-QUEST
  3089.         DB    L0A69 - $       ; 15d offset $4F to Address: PO-QUEST
  3090.         DB    L0A7A - $       ; 16d offset $5F to Address: PO-1-OPER
  3091.         DB    L0A7A - $       ; 17d offset $5E to Address: PO-1-OPER
  3092.         DB    L0A7A - $       ; 18d offset $5D to Address: PO-1-OPER
  3093.         DB    L0A7A - $       ; 19d offset $5C to Address: PO-1-OPER
  3094.         DB    L0A7A - $       ; 20d offset $5B to Address: PO-1-OPER
  3095.         DB    L0A7A - $       ; 21d offset $5A to Address: PO-1-OPER
  3096.         DB    L0A75 - $       ; 22d offset $54 to Address: PO-2-OPER
  3097.         DB    L0A75 - $       ; 23d offset $53 to Address: PO-2-OPER
  3098.  
  3099.  
  3100. ; -------------------
  3101. ; Cursor left routine
  3102. ; -------------------
  3103. ; Backspace and up a line if that action is from the left of screen.
  3104. ; For ZX printer backspace up to first column but not beyond.
  3105.  
  3106. ;; PO-BACK-1
  3107. L0A23:  INC     C               ; move left one column.
  3108.         LD      A,$22           ; value $21 is leftmost column.
  3109.         CP      C               ; have we passed ?
  3110.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3111.  
  3112.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3113.         JR      NZ,L0A38        ; to PO-BACK-2 if so, as we are unable to
  3114.                                 ; backspace from the leftmost position.
  3115.  
  3116.  
  3117.         INC     B               ; move up one screen line
  3118.         LD      C,$02           ; the rightmost column position.
  3119.         LD      A,$18           ; Note. This should be $19
  3120.                                 ; credit. Dr. Frank O'Hara, 1982
  3121.  
  3122.         CP      B               ; has position moved past top of screen ?
  3123.         JR      NZ,L0A3A        ; to PO-BACK-3 if not and store new position.
  3124.  
  3125.         DEC     B               ; else back to $18.
  3126.  
  3127. ;; PO-BACK-2
  3128. L0A38:  LD      C,$21           ; the leftmost column position.
  3129.  
  3130. ;; PO-BACK-3
  3131. L0A3A:  JP      L0DD9           ; to CL-SET and PO-STORE to save new
  3132.                                 ; position in system variables.
  3133.  
  3134. ; --------------------
  3135. ; Cursor right routine
  3136. ; --------------------
  3137. ; This moves the print position to the right leaving a trail in the
  3138. ; current background colour.
  3139. ; "However the programmer has failed to store the new print position
  3140. ;  so CHR$ 9 will only work if the next print position is at a newly
  3141. ;  defined place.
  3142. ;   e.g. PRINT PAPER 2; CHR$ 9; AT 4,0;
  3143. ;  does work but is not very helpful"
  3144. ; - Dr. Ian Logan, Understanding Your Spectrum, 1982.
  3145.  
  3146. ;; PO-RIGHT
  3147. L0A3D:  LD      A,($5C91)       ; fetch P_FLAG value
  3148.         PUSH    AF              ; and save it on stack.
  3149.  
  3150.         LD      (IY+$57),$01    ; temporarily set P_FLAG 'OVER 1'.
  3151.         LD      A,$20           ; prepare a space.
  3152.         CALL    L0B65           ; routine PO-CHAR to print it.
  3153.                                 ; Note. could be PO-ABLE which would update
  3154.                                 ; the column position.
  3155.  
  3156.         POP     AF              ; restore the permanent flag.
  3157.         LD      ($5C91),A       ; and restore system variable P_FLAG
  3158.  
  3159.         RET                     ; return without updating column position
  3160.  
  3161. ; -----------------------
  3162. ; Perform carriage return
  3163. ; -----------------------
  3164. ; A carriage return is 'printed' to screen or printer buffer.
  3165.  
  3166. ;; PO-ENTER
  3167. L0A4F:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3168.         JP      NZ,L0ECD        ; to COPY-BUFF if so, to flush buffer and reset
  3169.                                 ; the print position.
  3170.  
  3171.         LD      C,$21           ; the leftmost column position.
  3172.         CALL    L0C55           ; routine PO-SCR handles any scrolling required.
  3173.         DEC     B               ; to next screen line.
  3174.         JP      L0DD9           ; jump forward to CL-SET to store new position.
  3175.  
  3176. ; -----------
  3177. ; Print comma
  3178. ; -----------
  3179. ; The comma control character. The 32 column screen has two 16 character
  3180. ; tabstops.  The routine is only reached via the control character table.
  3181.  
  3182. ;; PO-COMMA
  3183. L0A5F:  CALL    L0B03           ; routine PO-FETCH - seems unnecessary.
  3184.  
  3185.         LD      A,C             ; the column position. $21-$01
  3186.         DEC     A               ; move right. $20-$00
  3187.         DEC     A               ; and again   $1F-$00 or $FF if trailing
  3188.         AND     $10             ; will be $00 or $10.
  3189.         JR      L0AC3           ; forward to PO-FILL
  3190.  
  3191. ; -------------------
  3192. ; Print question mark
  3193. ; -------------------
  3194. ; This routine prints a question mark which is commonly
  3195. ; used to print an unassigned control character in range 0-31d.
  3196. ; there are a surprising number yet to be assigned.
  3197.  
  3198. ;; PO-QUEST
  3199. L0A69:  LD      A,$3F           ; prepare the character '?'.
  3200.         JR      L0AD9           ; forward to PO-ABLE.
  3201.  
  3202. ; --------------------------------
  3203. ; Control characters with operands
  3204. ; --------------------------------
  3205. ; Certain control characters are followed by 1 or 2 operands.
  3206. ; The entry points from control character table are PO-2-OPER and PO-1-OPER.
  3207. ; The routines alter the output address of the current channel so that
  3208. ; subsequent RST $10 instructions take the appropriate action
  3209. ; before finally resetting the output address back to PRINT-OUT.
  3210.  
  3211. ;; PO-TV-2
  3212. L0A6D:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3213.         LD      ($5C0F),A       ; store first operand in TVDATA-hi
  3214.         JR      L0A80           ; forward to PO-CHANGE >>
  3215.  
  3216. ; ---
  3217.  
  3218. ; -> This initial entry point deals with two operands - AT or TAB.
  3219.  
  3220. ;; PO-2-OPER
  3221. L0A75:  LD      DE,L0A6D        ; address: PO-TV-2 will be next output routine
  3222.         JR      L0A7D           ; forward to PO-TV-1
  3223.  
  3224. ; ---
  3225.  
  3226. ; -> This initial entry point deals with one operand INK to OVER.
  3227.  
  3228. ;; PO-1-OPER
  3229. L0A7A:  LD      DE,L0A87        ; address: PO-CONT will be next output routine
  3230.  
  3231. ;; PO-TV-1
  3232. L0A7D:  LD      ($5C0E),A       ; store control code in TVDATA-lo
  3233.  
  3234. ;; PO-CHANGE
  3235. L0A80:  LD      HL,($5C51)      ; use CURCHL to find current output channel.
  3236.         LD      (HL),E          ; make it
  3237.         INC     HL              ; the supplied
  3238.         LD      (HL),D          ; address from DE.
  3239.         RET                     ; return.
  3240.  
  3241. ; ---
  3242.  
  3243. ;; PO-CONT
  3244. L0A87:  LD      DE,L09F4        ; Address: PRINT-OUT
  3245.         CALL    L0A80           ; routine PO-CHANGE to restore normal channel.
  3246.         LD      HL,($5C0E)      ; TVDATA gives control code and possible
  3247.                                 ; subsequent character
  3248.         LD      D,A             ; save current character
  3249.         LD      A,L             ; the stored control code
  3250.         CP      $16             ; was it INK to OVER (1 operand) ?
  3251.         JP      C,L2211         ; to CO-TEMP-5
  3252.  
  3253.         JR      NZ,L0AC2        ; to PO-TAB if not 22d i.e. 23d TAB.
  3254.  
  3255.                                 ; else must have been 22d AT.
  3256.         LD      B,H             ; line to H   (0-23d)
  3257.         LD      C,D             ; column to C (0-31d)
  3258.         LD      A,$1F           ; the value 31d
  3259.         SUB     C               ; reverse the column number.
  3260.         JR      C,L0AAC         ; to PO-AT-ERR if C was greater than 31d.
  3261.  
  3262.         ADD     A,$02           ; transform to system range $02-$21
  3263.         LD      C,A             ; and place in column register.
  3264.  
  3265.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3266.         JR      NZ,L0ABF        ; to PO-AT-SET as line can be ignored.
  3267.  
  3268.         LD      A,$16           ; 22 decimal
  3269.         SUB     B               ; subtract line number to reverse
  3270.                                 ; 0 - 22 becomes 22 - 0.
  3271.  
  3272. ;; PO-AT-ERR
  3273. L0AAC:  JP      C,L1E9F         ; to REPORT-B if higher than 22 decimal
  3274.                                 ; Integer out of range.
  3275.  
  3276.         INC     A               ; adjust for system range $01-$17
  3277.         LD      B,A             ; place in line register
  3278.         INC     B               ; adjust to system range  $02-$18
  3279.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3280.         JP      NZ,L0C55        ; exit to PO-SCR to test for scrolling
  3281.  
  3282.         CP      (IY+$31)        ; Compare against DF_SZ
  3283.         JP      C,L0C86         ; to REPORT-5 if too low
  3284.                                 ; Out of screen.
  3285.  
  3286. ;; PO-AT-SET
  3287. L0ABF:  JP      L0DD9           ; print position is valid so exit via CL-SET
  3288.  
  3289. ; Continue here when dealing with TAB.
  3290. ; Note. In BASIC, TAB is followed by a 16-bit number and was initially
  3291. ; designed to work with any output device.
  3292.  
  3293. ;; PO-TAB
  3294. L0AC2:  LD      A,H             ; transfer parameter to A
  3295.                                 ; Losing current character -
  3296.                                 ; High byte of TAB parameter.
  3297.  
  3298.  
  3299. ;; PO-FILL
  3300. L0AC3:  CALL    L0B03           ; routine PO-FETCH, HL-addr, BC=line/column.
  3301.                                 ; column 1 (right), $21 (left)
  3302.         ADD     A,C             ; add operand to current column
  3303.         DEC     A               ; range 0 - 31+
  3304.         AND     $1F             ; make range 0 - 31d
  3305.         RET     Z               ; return if result zero
  3306.  
  3307.         LD      D,A             ; Counter to D
  3308.         SET     0,(IY+$01)      ; update FLAGS  - signal suppress leading space.
  3309.  
  3310. ;; PO-SPACE
  3311. L0AD0:  LD      A,$20           ; space character.
  3312.         CALL    L0C3B           ; routine PO-SAVE prints the character
  3313.                                 ; using alternate set (normal output routine)
  3314.         DEC     D               ; decrement counter.
  3315.         JR      NZ,L0AD0        ; to PO-SPACE until done
  3316.  
  3317.         RET                     ; return
  3318.  
  3319. ; ----------------------
  3320. ; Printable character(s)
  3321. ; ----------------------
  3322. ; This routine prints printable characters and continues into
  3323. ; the position store routine
  3324.  
  3325. ;; PO-ABLE
  3326. L0AD9:  CALL    L0B24           ; routine PO-ANY
  3327.                                 ; and continue into position store routine.
  3328.  
  3329. ; -------------------------------------
  3330. ; Store line, column, and pixel address
  3331. ; -------------------------------------
  3332. ; This routine updates the system variables associated with
  3333. ; The main screen, lower screen/input buffer or ZX printer.
  3334.  
  3335. ;; PO-STORE
  3336. L0ADC:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use ?
  3337.         JR      NZ,L0AFC        ; to PO-ST-PR if so
  3338.  
  3339.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3340.         JR      NZ,L0AF0        ; to PO-ST-E if so
  3341.  
  3342.         LD      ($5C88),BC      ; S_POSN line/column upper screen
  3343.         LD      ($5C84),HL      ; DF_CC  display file address
  3344.         RET                     ;
  3345.  
  3346. ; ---
  3347.  
  3348. ;; PO-ST-E
  3349. L0AF0:  LD      ($5C8A),BC      ; SPOSNL line/column lower screen
  3350.         LD      ($5C82),BC      ; ECHO_E line/column input buffer
  3351.         LD      ($5C86),HL      ; DFCCL  lower screen memory address
  3352.         RET                     ;
  3353.  
  3354. ; ---
  3355.  
  3356. ;; PO-ST-PR
  3357. L0AFC:  LD      (IY+$45),C      ; P_POSN column position printer
  3358.         LD      ($5C80),HL      ; PR_CC  full printer buffer memory address
  3359.         RET                     ;
  3360.  
  3361. ; -------------------------
  3362. ; Fetch position parameters
  3363. ; -------------------------
  3364. ; This routine fetches the line/column and display file address
  3365. ; of the upper and lower screen or, if the printer is in use,
  3366. ; the column position and absolute memory address.
  3367. ; Note. that PR-CC-hi (23681) is used by this routine and the one above
  3368. ; and if, in accordance with the manual (that says this is unused), the
  3369. ; location has been used for other purposes, then subsequent output
  3370. ; to the printer buffer could corrupt a 256-byte section of memory.
  3371.  
  3372. ;; PO-FETCH
  3373. L0B03:  BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3374.         JR      NZ,L0B1D        ; to PO-F-PR if so
  3375.  
  3376.                                 ; assume upper screen
  3377.         LD      BC,($5C88)      ; S_POSN
  3378.         LD      HL,($5C84)      ; DF_CC display file address
  3379.         BIT     0,(IY+$02)      ; TV_FLAG  - Lower screen in use ?
  3380.         RET     Z               ; return if upper screen
  3381.  
  3382.                                 ; ah well, was lower screen
  3383.         LD      BC,($5C8A)      ; SPOSNL
  3384.         LD      HL,($5C86)      ; DFCCL
  3385.         RET                     ; return
  3386.  
  3387. ; ---
  3388.  
  3389. ;; PO-F-PR
  3390. L0B1D:  LD      C,(IY+$45)      ; P_POSN column only
  3391.         LD      HL,($5C80)      ; PR_CC printer buffer address
  3392.         RET                     ; return
  3393.  
  3394. ; -------------------
  3395. ; Print any character
  3396. ; -------------------
  3397. ; This routine is used to print any character in range 32d - 255d
  3398. ; It is only called from PO-ABLE which continues into PO-STORE
  3399.  
  3400. ;; PO-ANY
  3401. L0B24:  CP      $80             ; ASCII ?
  3402.         JR      C,L0B65         ; to PO-CHAR is so.
  3403.  
  3404.         CP      $90             ; test if a block graphic character.
  3405.         JR      NC,L0B52        ; to PO-T&UDG to print tokens and udg's
  3406.  
  3407. ; The 16 2*2 mosaic characters 128-143 decimal are formed from
  3408. ; bits 0-3 of the character.
  3409.  
  3410.         LD      B,A             ; save character
  3411.         CALL    L0B38           ; routine PO-GR-1 to construct top half
  3412.                                 ; then bottom half.
  3413.         CALL    L0B03           ; routine PO-FETCH fetches print position.
  3414.         LD      DE,$5C92        ; MEM-0 is location of 8 bytes of character
  3415.         JR      L0B7F           ; to PR-ALL to print to screen or printer
  3416.  
  3417. ; ---
  3418.  
  3419. ;; PO-GR-1
  3420. L0B38:  LD      HL,$5C92        ; address MEM-0 - a temporary buffer in
  3421.                                 ; systems variables which is normally used
  3422.                                 ; by the calculator.
  3423.         CALL    L0B3E           ; routine PO-GR-2 to construct top half
  3424.                                 ; and continue into routine to construct
  3425.                                 ; bottom half.
  3426.  
  3427. ;; PO-GR-2
  3428. L0B3E:  RR      B               ; rotate bit 0/2 to carry
  3429.         SBC     A,A             ; result $00 or $FF
  3430.         AND     $0F             ; mask off right hand side
  3431.         LD      C,A             ; store part in C
  3432.         RR      B               ; rotate bit 1/3 of original chr to carry
  3433.         SBC     A,A             ; result $00 or $FF
  3434.         AND     $F0             ; mask off left hand side
  3435.         OR      C               ; combine with stored pattern
  3436.         LD      C,$04           ; four bytes for top/bottom half
  3437.  
  3438. ;; PO-GR-3
  3439. L0B4C:  LD      (HL),A          ; store bit patterns in temporary buffer
  3440.         INC     HL              ; next address
  3441.         DEC     C               ; jump back to
  3442.         JR      NZ,L0B4C        ; to PO-GR-3 until byte is stored 4 times
  3443.  
  3444.         RET                     ; return
  3445.  
  3446. ; ---
  3447.  
  3448. ; Tokens and User defined graphics are now separated.
  3449.  
  3450. ;; PO-T&UDG
  3451. L0B52           IF BAS48_ONLY=1
  3452.                 SUB 0XA5
  3453.                 JR NC,L0B5F
  3454.                 ELSE
  3455.                 JP L3B9F                ;Spectrum 128 patch
  3456.                 NOP
  3457.                 ENDIF
  3458.  
  3459. L0B56:  ADD     A,$15           ; add 21d to restore to 0 - 20
  3460.         PUSH    BC              ; save current print position
  3461.         LD      BC,($5C7B)      ; fetch UDG to address bit patterns
  3462.         JR      L0B6A           ; to PO-CHAR-2 - common code to lay down
  3463.                                 ; a bit patterned character
  3464.  
  3465. ; ---
  3466.  
  3467. ;; PO-T
  3468. L0B5F:  CALL    L0C10           ; routine PO-TOKENS prints tokens
  3469.         JP      L0B03           ; exit via a JUMP to PO-FETCH as this routine
  3470.                                 ; must continue into PO-STORE.
  3471.                                 ; A JR instruction could be used.
  3472.  
  3473. ; This point is used to print ASCII characters  32d - 127d.
  3474.  
  3475. ;; PO-CHAR
  3476. L0B65:  PUSH    BC              ; save print position
  3477.         LD      BC,($5C36)      ; address CHARS
  3478.  
  3479. ; This common code is used to transfer the character bytes to memory.
  3480.  
  3481. ;; PO-CHAR-2
  3482. L0B6A:  EX      DE,HL           ; transfer destination address to DE
  3483.         LD      HL,$5C3B        ; point to FLAGS
  3484.         RES     0,(HL)          ; allow for leading space
  3485.         CP      $20             ; is it a space ?
  3486.         JR      NZ,L0B76        ; to PO-CHAR-3 if not
  3487.  
  3488.         SET     0,(HL)          ; signal no leading space to FLAGS
  3489.  
  3490. ;; PO-CHAR-3
  3491. L0B76:  LD      H,$00           ; set high byte to 0
  3492.         LD      L,A             ; character to A
  3493.                                 ; 0-21 UDG or 32-127 ASCII.
  3494.         ADD     HL,HL           ; multiply
  3495.         ADD     HL,HL           ; by
  3496.         ADD     HL,HL           ; eight
  3497.         ADD     HL,BC           ; HL now points to first byte of character
  3498.         POP     BC              ; the source address CHARS or UDG
  3499.         EX      DE,HL           ; character address to DE
  3500.  
  3501. ; --------------------
  3502. ; Print all characters
  3503. ; --------------------
  3504. ; This entry point entered from above to print ASCII and UDGs
  3505. ; but also from earlier to print mosaic characters.
  3506. ; HL=destination
  3507. ; DE=character source
  3508. ; BC=line/column
  3509.  
  3510. ;; PR-ALL
  3511. L0B7F:  LD      A,C             ; column to A
  3512.         DEC     A               ; move right
  3513.         LD      A,$21           ; pre-load with leftmost position
  3514.         JR      NZ,L0B93        ; but if not zero to PR-ALL-1
  3515.  
  3516.         DEC     B               ; down one line
  3517.         LD      C,A             ; load C with $21
  3518.         BIT     1,(IY+$01)      ; test FLAGS  - Is printer in use
  3519.         JR      Z,L0B93         ; to PR-ALL-1 if not
  3520.  
  3521.         PUSH    DE              ; save source address
  3522.         CALL    L0ECD           ; routine COPY-BUFF outputs line to printer
  3523.         POP     DE              ; restore character source address
  3524.         LD      A,C             ; the new column number ($21) to C
  3525.  
  3526. ;; PR-ALL-1
  3527. L0B93:  CP      C               ; this test is really for screen - new line ?
  3528.         PUSH    DE              ; save source
  3529.  
  3530.         CALL    Z,L0C55         ; routine PO-SCR considers scrolling
  3531.  
  3532.         POP     DE              ; restore source
  3533.         PUSH    BC              ; save line/column
  3534.         PUSH    HL              ; and destination
  3535.         LD      A,($5C91)       ; fetch P_FLAG to accumulator
  3536.         LD      B,$FF           ; prepare OVER mask in B.
  3537.         RRA                     ; bit 0 set if OVER 1
  3538.         JR      C,L0BA4         ; to PR-ALL-2
  3539.  
  3540.         INC     B               ; set OVER mask to 0
  3541.  
  3542. ;; PR-ALL-2
  3543. L0BA4:  RRA                     ; skip bit 1 of P_FLAG
  3544.         RRA                     ; bit 2 is INVERSE
  3545.         SBC     A,A             ; will be FF for INVERSE 1 else zero
  3546.         LD      C,A             ; transfer INVERSE mask to C
  3547.         LD      A,$08           ; prepare to count 8 bytes
  3548.         AND     A               ; clear carry to signal screen
  3549.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3550.         JR      Z,L0BB6         ; to PR-ALL-3 if screen
  3551.  
  3552.         SET     1,(IY+$30)      ; update FLAGS2  - signal printer buffer has
  3553.                                 ; been used.
  3554.         SCF                     ; set carry flag to signal printer.
  3555.  
  3556. ;; PR-ALL-3
  3557. L0BB6:  EX      DE,HL           ; now HL=source, DE=destination
  3558.  
  3559. ;; PR-ALL-4
  3560. L0BB7:  EX      AF,AF'          ; save printer/screen flag
  3561.        LD      A,(DE)          ; fetch existing destination byte
  3562.        AND     B               ; consider OVER
  3563.        XOR     (HL)            ; now XOR with source
  3564.        XOR     C               ; now with INVERSE MASK
  3565.        LD      (DE),A          ; update screen/printer
  3566.        EX      AF,AF'          ; restore flag
  3567.         JR      C,L0BD3         ; to PR-ALL-6 - printer address update
  3568.  
  3569.         INC     D               ; gives next pixel line down screen
  3570.  
  3571. ;; PR-ALL-5
  3572. L0BC1:  INC     HL              ; address next character byte
  3573.         DEC     A               ; the byte count is decremented
  3574.         JR      NZ,L0BB7        ; back to PR-ALL-4 for all 8 bytes
  3575.  
  3576.         EX      DE,HL           ; destination to HL
  3577.         DEC     H               ; bring back to last updated screen position
  3578.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3579.         CALL    Z,L0BDB         ; if not, call routine PO-ATTR to update
  3580.                                 ; corresponding colour attribute.
  3581.         POP     HL              ; restore original screen/printer position
  3582.         POP     BC              ; and line column
  3583.         DEC     C               ; move column to right
  3584.         INC     HL              ; increase screen/printer position
  3585.         RET                     ; return and continue into PO-STORE
  3586.                                 ; within PO-ABLE
  3587.  
  3588. ; ---
  3589.  
  3590. ; This branch is used to update the printer position by 32 places
  3591. ; Note. The high byte of the address D remains constant (which it should).
  3592.  
  3593. ;; PR-ALL-6
  3594. L0BD3:  EX      AF,AF'          ; save the flag
  3595.        LD      A,$20           ; load A with 32 decimal
  3596.        ADD     A,E             ; add this to E
  3597.        LD      E,A             ; and store result in E
  3598.        EX      AF,AF'          ; fetch the flag
  3599.         JR      L0BC1           ; back to PR-ALL-5
  3600.  
  3601. ; -------------
  3602. ; Set attribute
  3603. ; -------------
  3604. ; This routine is entered with the HL register holding the last screen
  3605. ; address to be updated by PRINT or PLOT.
  3606. ; The Spectrum screen arrangement leads to the L register holding
  3607. ; the correct value for the attribute file and it is only necessary
  3608. ; to manipulate H to form the correct colour attribute address.
  3609.  
  3610. ;; PO-ATTR
  3611. L0BDB:  LD       A,H            ; fetch high byte $40 - $57
  3612.         RRCA                    ; shift
  3613.         RRCA                    ; bits 3 and 4
  3614.         RRCA                    ; to right.
  3615.         AND     $03             ; range is now 0 - 2
  3616.         OR      $58             ; form correct high byte for third of screen
  3617.         LD      H,A             ; HL is now correct
  3618.         LD      DE,($5C8F)      ; make D hold ATTR_T, E hold MASK-T
  3619.         LD      A,(HL)          ; fetch existing attribute
  3620.         XOR     E               ; apply masks
  3621.         AND     D               ;
  3622.         XOR     E               ;
  3623.         BIT     6,(IY+$57)      ; test P_FLAG  - is this PAPER 9 ??
  3624.         JR      Z,L0BFA         ; skip to PO-ATTR-1 if not.
  3625.  
  3626.         AND     $C7             ; set paper
  3627.         BIT     2,A             ; to contrast with ink
  3628.         JR      NZ,L0BFA        ; skip to PO-ATTR-1
  3629.  
  3630.         XOR     $38             ;
  3631.  
  3632. ;; PO-ATTR-1
  3633. L0BFA:  BIT     4,(IY+$57)      ; test P_FLAG  - Is this INK 9 ??
  3634.         JR      Z,L0C08         ; skip to PO-ATTR-2 if not
  3635.  
  3636.         AND     $F8             ; make ink
  3637.         BIT     5,A             ; contrast with paper.
  3638.         JR      NZ,L0C08        ; to PO-ATTR-2
  3639.  
  3640.         XOR     $07             ;
  3641.  
  3642. ;; PO-ATTR-2
  3643. L0C08:  LD      (HL),A          ; save the new attribute.
  3644.         RET                     ; return.
  3645.  
  3646. ; ----------------
  3647. ; Message printing
  3648. ; ----------------
  3649. ; This entry point is used to print tape, boot-up, scroll? and error messages
  3650. ; On entry the DE register points to an initial step-over byte or
  3651. ; the inverted end-marker of the previous entry in the table.
  3652. ; A contains the message number, often zero to print first message.
  3653. ; (HL has nothing important usually P_FLAG)
  3654.  
  3655. ;; PO-MSG
  3656. L0C0A:  PUSH    HL              ; put hi-byte zero on stack to suppress
  3657.         LD      H,$00           ; trailing spaces
  3658.         EX      (SP),HL         ; ld h,0; push hl would have done ?.
  3659.         JR      L0C14           ; forward to PO-TABLE.
  3660.  
  3661. ; ---
  3662.  
  3663. ; This entry point prints the BASIC keywords, '<>' etc. from alt set
  3664.  
  3665. ;; PO-TOKENS
  3666. L0C10:  LD      DE,L0095        ; address: TKN-TABLE
  3667.         PUSH    AF              ; save the token number to control
  3668.                                 ; trailing spaces - see later *
  3669.  
  3670. ;; PO-TABLE
  3671. L0C14:  CALL    L0C41           ; routine PO-SEARCH will set carry for
  3672.                                 ; all messages and function words.
  3673. L0C17:  JR      C,L0C22         ; forward to PO-EACH if not a command,
  3674.                                 ; '<>' etc.
  3675.  
  3676.         LD      A,$20           ; prepare leading space
  3677.         BIT     0,(IY+$01)      ; test FLAGS  - leading space if not set
  3678.         CALL    Z,L0C3B         ; routine PO-SAVE to print a space
  3679.                                 ; without disturbing registers
  3680.  
  3681. ;; PO-EACH
  3682. L0C22:  LD      A,(DE)          ; fetch character
  3683.         AND     $7F             ; remove any inverted bit
  3684.         CALL    L0C3B           ; routine PO-SAVE to print using alternate
  3685.                                 ; set of registers.
  3686.         LD      A,(DE)          ; re-fetch character.
  3687.         INC     DE              ; address next
  3688.         ADD     A,A             ; was character inverted ?
  3689.                                 ; (this also doubles character)
  3690.         JR      NC,L0C22        ; back to PO-EACH if not
  3691.  
  3692.         POP     DE              ; * re-fetch trailing space flag to D (was A)
  3693.         CP      $48             ; was last character '$' ($24*2)
  3694.         JR      Z,L0C35         ; forward to PO-TR-SP to consider trailing
  3695.                                 ; space if so.
  3696.  
  3697.         CP      $82             ; was it < 'A' i.e. '#','>','=' from tokens
  3698.                                 ; or ' ','.' (from tape) or '?' from scroll
  3699.         RET     C               ; no trailing space
  3700.  
  3701. ;; PO-TR-SP
  3702. L0C35:  LD      A,D             ; the trailing space flag (zero if an error msg)
  3703.         CP      $03             ; test against RND, INKEY$ and PI
  3704.                                 ; which have no parameters and
  3705.         RET     C               ; therefore no trailing space so return.
  3706.  
  3707.         LD      A,$20           ; else continue and print a trailing space.
  3708.  
  3709. ; -------------------------
  3710. ; Handle recursive printing
  3711. ; -------------------------
  3712. ; This routine which is part of PRINT-OUT allows RST $10 to be
  3713. ; used recursively to print tokens and the spaces associated with them.
  3714.  
  3715. ;; PO-SAVE
  3716. L0C3B:  PUSH    DE              ; save DE as CALL-SUB doesn't.
  3717.         EXX                     ; switch in main set
  3718.  
  3719.         RST     10H             ; PRINT-A prints using this alternate set.
  3720.  
  3721.         EXX                     ; back to this alternate set.
  3722.         POP     DE              ; restore initial DE.
  3723.         RET                     ; return.
  3724.  
  3725. ; ------------
  3726. ; Table search
  3727. ; ------------
  3728. ; This subroutine searches a message or the token table for the
  3729. ; message number held in A. DE holds the address of the table.
  3730.  
  3731. ;; PO-SEARCH
  3732. L0C41:  PUSH    AF              ; save the message/token number
  3733.         EX      DE,HL           ; transfer DE to HL
  3734.         INC     A               ; adjust for initial step-over byte
  3735.  
  3736. ;; PO-STEP
  3737. L0C44:  BIT     7,(HL)          ; is character inverted ?
  3738.         INC     HL              ; address next
  3739.         JR      Z,L0C44         ; back to PO-STEP if not inverted.
  3740.  
  3741.         DEC     A               ; decrease counter
  3742.         JR      NZ,L0C44        ; back to PO-STEP if not zero
  3743.  
  3744.         EX      DE,HL           ; transfer address to DE
  3745.         POP     AF              ; restore message/token number
  3746.         CP      $20             ; return with carry set
  3747.         RET     C               ; for all messages and function tokens
  3748.  
  3749.         LD      A,(DE)          ; test first character of token
  3750.         SUB     $41             ; and return with carry set
  3751.         RET                     ; if it is less that 'A'
  3752.                                 ; i.e. '<>', '<=', '>='
  3753.  
  3754. ; ---------------
  3755. ; Test for scroll
  3756. ; ---------------
  3757. ; This test routine is called when printing carriage return, when considering
  3758. ; PRINT AT and from the general PRINT ALL characters routine to test if
  3759. ; scrolling is required, prompting the user if necessary.
  3760. ; This is therefore using the alternate set.
  3761. ; The B register holds the current line.
  3762.  
  3763. ;; PO-SCR
  3764. L0C55:  BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  3765.         RET     NZ              ; return immediately if so.
  3766.  
  3767.         LD      DE,L0DD9        ; set DE to address: CL-SET
  3768.         PUSH    DE              ; and push for return address.
  3769.         LD      A,B             ; transfer the line to A.
  3770.         BIT     0,(IY+$02)      ; test TV_FLAG  - Lower screen in use ?
  3771.         JP      NZ,L0D02        ; jump forward to PO-SCR-4 if so.
  3772.  
  3773.         CP      (IY+$31)        ; greater than DF_SZ display file size ?
  3774.         JR      C,L0C86         ; forward to REPORT-5 if less.
  3775.                                 ; 'Out of screen'
  3776.  
  3777.         RET     NZ              ; return (via CL-SET) if greater
  3778.  
  3779.         BIT     4,(IY+$02)      ; test TV_FLAG  - Automatic listing ?
  3780.         JR      Z,L0C88         ; forward to PO-SCR-2 if not.
  3781.  
  3782.         LD      E,(IY+$2D)      ; fetch BREG - the count of scroll lines to E.
  3783.         DEC     E               ; decrease and jump
  3784.         JR      Z,L0CD2         ; to PO-SCR-3 if zero and scrolling required.
  3785.  
  3786.         LD      A,$00           ; explicit - select channel zero.
  3787.         CALL    L1601           ; routine CHAN-OPEN opens it.
  3788.  
  3789.         LD      SP,($5C3F)      ; set stack pointer to LIST_SP
  3790.  
  3791.         RES     4,(IY+$02)      ; reset TV_FLAG  - signal auto listing finished.
  3792.         RET                     ; return ignoring pushed value, CL-SET
  3793.                                 ; to MAIN or EDITOR without updating
  3794.                                 ; print position                         ->
  3795.  
  3796. ; ---
  3797.  
  3798.  
  3799. ;; REPORT-5
  3800. L0C86:  RST     08H             ; ERROR-1
  3801.         DB    $04             ; Error Report: Out of screen
  3802.  
  3803. ; continue here if not an automatic listing.
  3804.  
  3805. ;; PO-SCR-2
  3806. L0C88:  DEC     (IY+$52)        ; decrease SCR_CT
  3807.         JR      NZ,L0CD2        ; forward to PO-SCR-3 to scroll display if
  3808.                                 ; result not zero.
  3809.  
  3810. ; now produce prompt.
  3811.  
  3812.         LD      A,$18           ; reset
  3813.         SUB     B               ; the
  3814.         LD      ($5C8C),A       ; SCR_CT scroll count
  3815.         LD      HL,($5C8F)      ; L=ATTR_T, H=MASK_T
  3816.         PUSH    HL              ; save on stack
  3817.         LD      A,($5C91)       ; P_FLAG
  3818.         PUSH    AF              ; save on stack to prevent lower screen
  3819.                                 ; attributes (BORDCR etc.) being applied.
  3820.         LD      A,$FD           ; select system channel 'K'
  3821.         CALL    L1601           ; routine CHAN-OPEN opens it
  3822.         XOR     A               ; clear to address message directly
  3823.         LD      DE,L0CF8        ; make DE address: scrl-mssg
  3824.         CALL    L0C0A           ; routine PO-MSG prints to lower screen
  3825.         SET     5,(IY+$02)      ; set TV_FLAG  - signal lower screen requires
  3826.                                 ; clearing
  3827.         LD      HL,$5C3B        ; make HL address FLAGS
  3828.         SET     3,(HL)          ; signal 'L' mode.
  3829.         RES     5,(HL)          ; signal 'no new key'.
  3830.         EXX                     ; switch to main set.
  3831.                                 ; as calling chr input from alternative set.
  3832.         CALL    L15D4           ; routine WAIT-KEY waits for new key
  3833.                                 ; Note. this is the right routine but the
  3834.                                 ; stream in use is unsatisfactory. From the
  3835.                                 ; choices available, it is however the best.
  3836.  
  3837.         EXX                     ; switch back to alternate set.
  3838.         CP      $20             ; space is considered as BREAK
  3839.         JR      Z,L0D00         ; forward to REPORT-D if so
  3840.                                 ; 'BREAK - CONT repeats'
  3841.  
  3842.         CP      $E2             ; is character 'STOP' ?
  3843.         JR      Z,L0D00         ; forward to REPORT-D if so
  3844.  
  3845.         OR      $20             ; convert to lower-case
  3846.         CP      $6E             ; is character 'n' ?
  3847.         JR      Z,L0D00         ; forward to REPORT-D if so else scroll.
  3848.  
  3849.         LD      A,$FE           ; select system channel 'S'
  3850.         CALL    L1601           ; routine CHAN-OPEN
  3851.         POP     AF              ; restore original P_FLAG
  3852.         LD      ($5C91),A       ; and save in P_FLAG.
  3853.         POP     HL              ; restore original ATTR_T, MASK_T
  3854.         LD      ($5C8F),HL      ; and reset ATTR_T, MASK-T as 'scroll?' has
  3855.                                 ; been printed.
  3856.  
  3857. ;; PO-SCR-3
  3858. L0CD2:  CALL    L0DFE           ; routine CL-SC-ALL to scroll whole display
  3859.         LD      B,(IY+$31)      ; fetch DF_SZ to B
  3860.         INC     B               ; increase to address last line of display
  3861.         LD      C,$21           ; set C to $21 (was $21 from above routine)
  3862.         PUSH    BC              ; save the line and column in BC.
  3863.  
  3864.         CALL    L0E9B           ; routine CL-ADDR finds display address.
  3865.  
  3866.         LD      A,H             ; now find the corresponding attribute byte
  3867.         RRCA                    ; (this code sequence is used twice
  3868.         RRCA                    ; elsewhere and is a candidate for
  3869.         RRCA                    ; a subroutine.)
  3870.         AND     $03             ;
  3871.         OR      $58             ;
  3872.         LD      H,A             ;
  3873.  
  3874.         LD      DE,$5AE0        ; start of last 'line' of attribute area
  3875.         LD      A,(DE)          ; get attribute for last line
  3876.         LD      C,(HL)          ; transfer to base line of upper part
  3877.         LD      B,$20           ; there are thirty two bytes
  3878.         EX      DE,HL           ; swap the pointers.
  3879.  
  3880. ;; PO-SCR-3A
  3881. L0CF0:  LD      (DE),A          ; transfer
  3882.         LD      (HL),C          ; attributes.
  3883.         INC     DE              ; address next.
  3884.         INC     HL              ; address next.
  3885.         DJNZ    L0CF0           ; loop back to PO-SCR-3A for all adjacent
  3886.                                 ; attribute lines.
  3887.  
  3888.         POP     BC              ; restore the line/column.
  3889.         RET                     ; return via CL-SET (was pushed on stack).
  3890.  
  3891. ; ---
  3892.  
  3893. ; The message 'scroll?' appears here with last byte inverted.
  3894.  
  3895. ;; scrl-mssg
  3896. L0CF8           DB $80             ; initial step-over byte.
  3897.                 DC "scroll?"    ;DEFM    "scroll"
  3898.                                 ;DB    '?'+$80
  3899.  
  3900. ;; REPORT-D
  3901. L0D00:  RST     08H             ; ERROR-1
  3902.         DB    $0C             ; Error Report: BREAK - CONT repeats
  3903.  
  3904. ; continue here if using lower display - A holds line number.
  3905.  
  3906. ;; PO-SCR-4
  3907. L0D02:  CP      $02             ; is line number less than 2 ?
  3908.         JR      C,L0C86         ; to REPORT-5 if so
  3909.                                 ; 'Out of Screen'.
  3910.  
  3911.         ADD     A,(IY+$31)      ; add DF_SZ
  3912.         SUB     $19             ;
  3913.         RET     NC              ; return if scrolling unnecessary
  3914.  
  3915.         NEG                     ; Negate to give number of scrolls required.
  3916.         PUSH    BC              ; save line/column
  3917.         LD      B,A             ; count to B
  3918.         LD      HL,($5C8F)      ; fetch current ATTR_T, MASK_T to HL.
  3919.         PUSH    HL              ; and save
  3920.         LD      HL,($5C91)      ; fetch P_FLAG
  3921.         PUSH    HL              ; and save.
  3922.                                 ; to prevent corruption by input AT
  3923.  
  3924.         CALL    L0D4D           ; routine TEMPS sets to BORDCR etc
  3925.         LD      A,B             ; transfer scroll number to A.
  3926.  
  3927. ;; PO-SCR-4A
  3928. L0D1C:  PUSH    AF              ; save scroll number.
  3929.         LD      HL,$5C6B        ; address DF_SZ
  3930.         LD      B,(HL)          ; fetch old value
  3931.         LD      A,B             ; transfer to A
  3932.         INC     A               ; and increment
  3933.         LD      (HL),A          ; then put back.
  3934.         LD      HL,$5C89        ; address S_POSN_hi - line
  3935.         CP      (HL)            ; compare
  3936.         JR      C,L0D2D         ; forward to PO-SCR-4B if scrolling required
  3937.  
  3938.         INC     (HL)            ; else increment S_POSN_hi
  3939.         LD      B,$18           ; set count to whole display ??
  3940.                                 ; Note. should be $17 and the top line
  3941.                                 ; will be scrolled into the ROM which
  3942.                                 ; is harmless on the standard set up.
  3943.  
  3944. ;; PO-SCR-4B
  3945. L0D2D:  CALL    L0E00           ; routine CL-SCROLL scrolls B lines
  3946.         POP     AF              ; restore scroll counter.
  3947.         DEC     A               ; decrease
  3948.         JR      NZ,L0D1C        ; back to to PO-SCR-4A until done
  3949.  
  3950.         POP     HL              ; restore original P_FLAG.
  3951.         LD      (IY+$57),L      ; and overwrite system variable P_FLAG.
  3952.  
  3953.         POP     HL              ; restore original ATTR_T/MASK_T.
  3954.         LD      ($5C8F),HL      ; and update system variables.
  3955.  
  3956.         LD      BC,($5C88)      ; fetch S_POSN to BC.
  3957.         RES     0,(IY+$02)      ; signal to TV_FLAG  - main screen in use.
  3958.         CALL    L0DD9           ; call routine CL-SET for upper display.
  3959.  
  3960.         SET     0,(IY+$02)      ; signal to TV_FLAG  - lower screen in use.
  3961.         POP     BC              ; restore line/column
  3962.         RET                     ; return via CL-SET for lower display.
  3963.  
  3964. ; ----------------------
  3965. ; Temporary colour items
  3966. ; ----------------------
  3967. ; This subroutine is called 11 times to copy the permanent colour items
  3968. ; to the temporary ones.
  3969.  
  3970. ;; TEMPS
  3971. L0D4D:  XOR     A               ; clear the accumulator
  3972.         LD      HL,($5C8D)      ; fetch L=ATTR_P and H=MASK_P
  3973.         BIT     0,(IY+$02)      ; test TV_FLAG  - is lower screen in use ?
  3974.         JR      Z,L0D5B         ; skip to TEMPS-1 if not
  3975.  
  3976.         LD      H,A             ; set H, MASK P, to 00000000.
  3977.         LD      L,(IY+$0E)      ; fetch BORDCR to L which is used for lower
  3978.                                 ; screen.
  3979.  
  3980. ;; TEMPS-1
  3981. L0D5B:  LD      ($5C8F),HL      ; transfer values to ATTR_T and MASK_T
  3982.  
  3983. ; for the print flag the permanent values are odd bits, temporary even bits.
  3984.  
  3985.         LD      HL,$5C91        ; address P_FLAG.
  3986.         JR      NZ,L0D65        ; skip to TEMPS-2 if lower screen using A=0.
  3987.  
  3988.         LD      A,(HL)          ; else pick up flag bits.
  3989.         RRCA                    ; rotate permanent bits to temporary bits.
  3990.  
  3991. ;; TEMPS-2
  3992. L0D65:  XOR     (HL)            ;
  3993.         AND     $55             ; BIN 01010101
  3994.         XOR     (HL)            ; permanent now as original
  3995.         LD      (HL),A          ; apply permanent bits to temporary bits.
  3996.         RET                     ; and return.
  3997.  
  3998. ; ------------------
  3999. ; Handle CLS command
  4000. ; ------------------
  4001. ; clears the display.
  4002. ; if it's difficult to write it should be difficult to read.
  4003.  
  4004. ;; CLS
  4005. L0D6B:  CALL    L0DAF           ; routine CL-ALL  clears display and
  4006.                                 ; resets attributes to permanent.
  4007.                                 ; re-attaches it to this computer.
  4008.  
  4009. ; this routine called from INPUT, **
  4010.  
  4011. ;; CLS-LOWER
  4012. L0D6E:  LD      HL,$5C3C        ; address System Variable TV_FLAG.
  4013.         RES     5,(HL)          ; TV_FLAG - signal do not clear lower screen.
  4014.         SET     0,(HL)          ; TV_FLAG - signal lower screen in use.
  4015.         CALL    L0D4D           ; routine TEMPS picks up temporary colours.
  4016.         LD      B,(IY+$31)      ; fetch lower screen DF_SZ
  4017.         CALL    L0E44           ; routine CL-LINE clears lower part
  4018.                                 ; and sets permanent attributes.
  4019.  
  4020.         LD      HL,$5AC0        ; fetch attribute address leftmost cell,
  4021.                                 ; second line up.
  4022.         LD      A,($5C8D)       ; fetch permanent attribute from ATTR_P.
  4023.         DEC     B               ; decrement lower screen display file size
  4024.         JR      L0D8E           ; forward to CLS-3 ->
  4025.  
  4026. ; ---
  4027.  
  4028. ;; CLS-1
  4029. L0D87:  LD      C,$20           ; set counter to 32 characters per line
  4030.  
  4031. ;; CLS-2
  4032. L0D89:  DEC     HL              ; decrease attribute address.
  4033.         LD      (HL),A          ; and place attributes in next line up.
  4034.         DEC     C               ; decrease 32 counter.
  4035.         JR      NZ,L0D89        ; loop back to CLS-2 until all 32 done.
  4036.  
  4037. ;; CLS-3
  4038. L0D8E:  DJNZ    L0D87           ; decrease B counter and back to CLS-1
  4039.                                 ; if not zero.
  4040.  
  4041.         LD      (IY+$31),$02    ; set DF_SZ lower screen to 2
  4042.  
  4043. ; This entry point is called from CL-ALL below to
  4044. ; reset the system channel input and output addresses to normal.
  4045.  
  4046. ;; CL-CHAN
  4047. L0D94:  LD      A,$FD           ; select system channel 'K'
  4048.         CALL    L1601           ; routine CHAN-OPEN opens it.
  4049.         LD      HL,($5C51)      ; fetch CURCHL to HL to address current channel
  4050.         LD      DE,L09F4        ; set address to PRINT-OUT for first pass.
  4051.         AND     A               ; clear carry for first pass.
  4052.  
  4053. ;; CL-CHAN-A
  4054. L0DA0:  LD      (HL),E          ; insert output address first pass.
  4055.         INC     HL              ; or input address on second pass.
  4056.         LD      (HL),D          ;
  4057.         INC     HL              ;
  4058.         LD      DE,L10A8        ; fetch address KEY-INPUT for second pass
  4059.         CCF                     ; complement carry flag - will set on pass 1.
  4060.  
  4061.         JR      C,L0DA0         ; back to CL-CHAN-A if first pass else done.
  4062.  
  4063.         LD      BC,$1721        ; line 23 for lower screen
  4064.         JR      L0DD9           ; exit via CL-SET to set column
  4065.                                 ; for lower display
  4066.  
  4067. ; ---------------------------
  4068. ; Clearing whole display area
  4069. ; ---------------------------
  4070. ; This subroutine called from CLS, AUTO-LIST and MAIN-3
  4071. ; clears 24 lines of the display and resets the relevant system variables
  4072. ; and system channels.
  4073.  
  4074. ;; CL-ALL
  4075. L0DAF:  LD      HL,$0000        ; initialize plot coordinates.
  4076.         LD      ($5C7D),HL      ; set COORDS to 0,0.
  4077.         RES     0,(IY+$30)      ; update FLAGS2  - signal main screen is clear.
  4078.  
  4079.         CALL    L0D94           ; routine CL-CHAN makes channel 'K' 'normal'.
  4080.  
  4081.         LD      A,$FE           ; select system channel 'S'
  4082.         CALL    L1601           ; routine CHAN-OPEN opens it
  4083.         CALL    L0D4D           ; routine TEMPS picks up permanent values.
  4084.         LD      B,$18           ; There are 24 lines.
  4085.         CALL    L0E44           ; routine CL-LINE clears 24 text lines
  4086.                                 ; (and sets BC to $1821)
  4087.  
  4088.         LD      HL,($5C51)      ; fetch CURCHL make HL address current
  4089.                                 ; channel 'S'
  4090.         LD      DE,L09F4        ; address: PRINT-OUT
  4091.         LD      (HL),E          ; is made
  4092.         INC     HL              ; the normal
  4093.         LD      (HL),D          ; output address.
  4094.  
  4095.         LD      (IY+$52),$01    ; set SCR_CT - scroll count is set to default.
  4096.                                 ; Note. BC already contains $1821.
  4097.         LD      BC,$1821        ; reset column and line to 0,0
  4098.                                 ; and continue into CL-SET, below, exiting
  4099.                                 ; via PO-STORE (for upper screen).
  4100.  
  4101. ; ---------------------------
  4102. ; Set line and column numbers
  4103. ; ---------------------------
  4104. ; This important subroutine is used to calculate the character output
  4105. ; address for screens or printer based on the line/column for screens
  4106. ; or the column for printer.
  4107.  
  4108. ;; CL-SET
  4109. L0DD9:  LD      HL,$5B00        ; the base address of printer buffer
  4110.         BIT     1,(IY+$01)      ; test FLAGS  - is printer in use ?
  4111.         JR      NZ,L0DF4        ; forward to CL-SET-2 if so.
  4112.  
  4113.         LD      A,B             ; transfer line to A.
  4114.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4115.         JR      Z,L0DEE         ; skip to CL-SET-1 if handling upper part
  4116.  
  4117.         ADD     A,(IY+$31)      ; add DF_SZ for lower screen
  4118.         SUB     $18             ; and adjust.
  4119.  
  4120. ;; CL-SET-1
  4121. L0DEE:  PUSH    BC              ; save the line/column.
  4122.         LD      B,A             ; transfer line to B
  4123.                                 ; (adjusted if lower screen)
  4124.  
  4125.         CALL    L0E9B           ; routine CL-ADDR calculates address at left
  4126.                                 ; of screen.
  4127.         POP     BC              ; restore the line/column.
  4128.  
  4129. ;; CL-SET-2
  4130. L0DF4:  LD      A,$21           ; the column $1-$21 is reversed
  4131.         SUB     C               ; to range $00 - $20
  4132.         LD      E,A             ; now transfer to DE
  4133.         LD      D,$00           ; prepare for addition
  4134.         ADD     HL,DE           ; and add to base address
  4135.         JP      L0ADC           ; exit via PO-STORE to update relevant
  4136.                                 ; system variables.
  4137. ; ----------------
  4138. ; Handle scrolling
  4139. ; ----------------
  4140. ; The routine CL-SC-ALL is called once from PO to scroll all the display
  4141. ; and from the routine CL-SCROLL, once, to scroll part of the display.
  4142.  
  4143. ;; CL-SC-ALL
  4144. L0DFE:  LD      B,$17           ; scroll 23 lines, after 'scroll?'.
  4145.  
  4146. ;; CL-SCROLL
  4147. L0E00:  CALL    L0E9B           ; routine CL-ADDR gets screen address in HL.
  4148.         LD      C,$08           ; there are 8 pixel lines to scroll.
  4149.  
  4150. ;; CL-SCR-1
  4151. L0E05:  PUSH    BC              ; save counters.
  4152.         PUSH    HL              ; and initial address.
  4153.         LD      A,B             ; get line count.
  4154.         AND     $07             ; will set zero if all third to be scrolled.
  4155.         LD      A,B             ; re-fetch the line count.
  4156.         JR      NZ,L0E19        ; forward to CL-SCR-3 if partial scroll.
  4157.  
  4158. ; HL points to top line of third and must be copied to bottom of previous 3rd.
  4159. ; ( so HL = $4800 or $5000 ) ( but also sometimes $4000 )
  4160.  
  4161. ;; CL-SCR-2
  4162. L0E0D:  EX      DE,HL           ; copy HL to DE.
  4163.         LD      HL,$F8E0        ; subtract $08 from H and add $E0 to L -
  4164.         ADD     HL,DE           ; to make destination bottom line of previous
  4165.                                 ; third.
  4166.         EX      DE,HL           ; restore the source and destination.
  4167.         LD      BC,$0020        ; thirty-two bytes are to be copied.
  4168.         DEC     A               ; decrement the line count.
  4169.         LDIR                    ; copy a pixel line to previous third.
  4170.  
  4171. ;; CL-SCR-3
  4172. L0E19:  EX      DE,HL           ; save source in DE.
  4173.         LD      HL,$FFE0        ; load the value -32.
  4174.         ADD     HL,DE           ; add to form destination in HL.
  4175.         EX      DE,HL           ; switch source and destination
  4176.         LD      B,A             ; save the count in B.
  4177.         AND     $07             ; mask to find count applicable to current
  4178.         RRCA                    ; third and
  4179.         RRCA                    ; multiply by
  4180.         RRCA                    ; thirty two (same as 5 RLCAs)
  4181.  
  4182.         LD      C,A             ; transfer byte count to C ($E0 at most)
  4183.         LD      A,B             ; store line count to A
  4184.         LD      B,$00           ; make B zero
  4185.         LDIR                    ; copy bytes (BC=0, H incremented, L=0)
  4186.         LD      B,$07           ; set B to 7, C is zero.
  4187.         ADD     HL,BC           ; add 7 to H to address next third.
  4188.         AND     $F8             ; has last third been done ?
  4189.         JR      NZ,L0E0D        ; back to CL-SCR-2 if not
  4190.  
  4191.         POP     HL              ; restore topmost address.
  4192.         INC     H               ; next pixel line down.
  4193.         POP     BC              ; restore counts.
  4194.         DEC     C               ; reduce pixel line count.
  4195.         JR      NZ,L0E05        ; back to CL-SCR-1 if all eight not done.
  4196.  
  4197.         CALL    L0E88           ; routine CL-ATTR gets address in attributes
  4198.                                 ; from current 'ninth line', count in BC.
  4199.         LD      HL,$FFE0        ; set HL to the 16-bit value -32.
  4200.         ADD     HL,DE           ; and add to form destination address.
  4201.         EX      DE,HL           ; swap source and destination addresses.
  4202.         LDIR                    ; copy bytes scrolling the linear attributes.
  4203.         LD      B,$01           ; continue to clear the bottom line.
  4204.  
  4205. ; ---------------------------
  4206. ; Clear text lines of display
  4207. ; ---------------------------
  4208. ; This subroutine, called from CL-ALL, CLS-LOWER and AUTO-LIST and above,
  4209. ; clears text lines at bottom of display.
  4210. ; The B register holds on entry the number of lines to be cleared 1-24.
  4211.  
  4212. ;; CL-LINE
  4213. L0E44:  PUSH    BC              ; save line count
  4214.         CALL    L0E9B           ; routine CL-ADDR gets top address
  4215.         LD      C,$08           ; there are eight screen lines to a text line.
  4216.  
  4217. ;; CL-LINE-1
  4218. L0E4A:  PUSH    BC              ; save pixel line count
  4219.         PUSH    HL              ; and save the address
  4220.         LD      A,B             ; transfer the line to A (1-24).
  4221.  
  4222. ;; CL-LINE-2
  4223. L0E4D:  AND     $07             ; mask 0-7 to consider thirds at a time
  4224.         RRCA                    ; multiply
  4225.         RRCA                    ; by 32  (same as five RLCA instructions)
  4226.         RRCA                    ; now 32 - 256(0)
  4227.         LD      C,A             ; store result in C
  4228.         LD      A,B             ; save line in A (1-24)
  4229.         LD      B,$00           ; set high byte to 0, prepare for ldir.
  4230.         DEC     C               ; decrement count 31-255.
  4231.         LD      D,H             ; copy HL
  4232.         LD      E,L             ; to DE.
  4233.         LD      (HL),$00        ; blank the first byte.
  4234.         INC     DE              ; make DE point to next byte.
  4235.         LDIR                    ; ldir will clear lines.
  4236.         LD      DE,$0701        ; now address next third adjusting
  4237.         ADD     HL,DE           ; register E to address left hand side
  4238.         DEC     A               ; decrease the line count.
  4239.         AND     $F8             ; will be 16, 8 or 0  (AND $18 will do).
  4240.         LD      B,A             ; transfer count to B.
  4241.         JR      NZ,L0E4D        ; back to CL-LINE-2 if 16 or 8 to do
  4242.                                 ; the next third.
  4243.  
  4244.         POP     HL              ; restore start address.
  4245.         INC     H               ; address next line down.
  4246.         POP     BC              ; fetch counts.
  4247.         DEC     C               ; decrement pixel line count
  4248.         JR      NZ,L0E4A        ; back to CL-LINE-1 till all done.
  4249.  
  4250.         CALL    L0E88           ; routine CL-ATTR gets attribute address
  4251.                                 ; in DE and B * 32 in BC.
  4252.         LD      H,D             ; transfer the address
  4253.         LD      L,E             ; to HL.
  4254.  
  4255.         INC     DE              ; make DE point to next location.
  4256.  
  4257.         LD      A,($5C8D)       ; fetch ATTR_P - permanent attributes
  4258.         BIT     0,(IY+$02)      ; test TV_FLAG  - lower screen in use ?
  4259.         JR      Z,L0E80         ; skip to CL-LINE-3 if not.
  4260.  
  4261.         LD      A,($5C48)       ; else lower screen uses BORDCR as attribute.
  4262.  
  4263. ;; CL-LINE-3
  4264. L0E80:  LD      (HL),A          ; put attribute in first byte.
  4265.         DEC     BC              ; decrement the counter.
  4266.         LDIR                    ; copy bytes to set all attributes.
  4267.         POP     BC              ; restore the line $01-$24.
  4268.         LD      C,$21           ; make column $21. (No use is made of this)
  4269.         RET                     ; return to the calling routine.
  4270.  
  4271. ; ------------------
  4272. ; Attribute handling
  4273. ; ------------------
  4274. ; This subroutine is called from CL-LINE or CL-SCROLL with the HL register
  4275. ; pointing to the 'ninth' line and H needs to be decremented before or after
  4276. ; the division. Had it been done first then either present code or that used
  4277. ; at the start of PO-ATTR could have been used.
  4278. ; The Spectrum screen arrangement leads to the L register holding already
  4279. ; the correct value for the attribute file and it is only necessary
  4280. ; to manipulate H to form the correct colour attribute address.
  4281.  
  4282. ;; CL-ATTR
  4283. L0E88:  LD      A,H             ; fetch H to A - $48, $50, or $58.
  4284.         RRCA                    ; divide by
  4285.         RRCA                    ; eight.
  4286.         RRCA                    ; $09, $0A or $0B.
  4287.         DEC     A               ; $08, $09 or $0A.
  4288.         OR      $50             ; $58, $59 or $5A.
  4289.         LD      H,A             ; save high byte of attributes.
  4290.  
  4291.         EX      DE,HL           ; transfer attribute address to DE
  4292.         LD      H,C             ; set H to zero - from last LDIR.
  4293.         LD      L,B             ; load L with the line from B.
  4294.         ADD     HL,HL           ; multiply
  4295.         ADD     HL,HL           ; by
  4296.         ADD     HL,HL           ; thirty two
  4297.         ADD     HL,HL           ; to give count of attribute
  4298.         ADD     HL,HL           ; cells to end of display.
  4299.  
  4300.         LD      B,H             ; transfer result
  4301.         LD      C,L             ; to register BC.
  4302.  
  4303.         RET                     ; and return.
  4304.  
  4305. ; -------------------------------
  4306. ; Handle display with line number
  4307. ; -------------------------------
  4308. ; This subroutine is called from four places to calculate the address
  4309. ; of the start of a screen character line which is supplied in B.
  4310.  
  4311. ;; CL-ADDR
  4312. L0E9B:  LD      A,$18           ; reverse the line number
  4313.         SUB     B               ; to range $00 - $17.
  4314.         LD      D,A             ; save line in D for later.
  4315.         RRCA                    ; multiply
  4316.         RRCA                    ; by
  4317.         RRCA                    ; thirty-two.
  4318.  
  4319.         AND     $E0             ; mask off low bits to make
  4320.         LD      L,A             ; L a multiple of 32.
  4321.  
  4322.         LD      A,D             ; bring back the line to A.
  4323.  
  4324.         AND     $18             ; now $00, $08 or $10.
  4325.  
  4326.         OR      $40             ; add the base address of screen.
  4327.  
  4328.         LD      H,A             ; HL now has the correct address.
  4329.         RET                     ; return.
  4330.  
  4331. ; -------------------
  4332. ; Handle COPY command
  4333. ; -------------------
  4334. ; This command copies the top 176 lines to the ZX Printer
  4335. ; It is popular to call this from machine code at point
  4336. ; L0EAF with B holding 192 (and interrupts disabled) for a full-screen
  4337. ; copy. This particularly applies to 16K Spectrums as time-critical
  4338. ; machine code routines cannot be written in the first 16K of RAM as
  4339. ; it is shared with the ULA which has precedence over the Z80 chip.
  4340.  
  4341. ;; COPY
  4342.  
  4343. L0EAC:  DI                      ; disable interrupts as this is time-critical.
  4344. ;===============================
  4345.                 RST 0X08
  4346.                 DB AY_PRN_SCR
  4347. ;               LD B,$B0                ; top 176 lines.
  4348. ;===============================
  4349.  
  4350. L0EAF           LD HL,$4000             ; address start of the display file.
  4351.  
  4352. ; now enter a loop to handle each pixel line.
  4353.  
  4354. ;; COPY-1
  4355. L0EB2:  PUSH    HL              ; save the screen address.
  4356.         PUSH    BC              ; and the line counter.
  4357.  
  4358.         CALL    L0EF4           ; routine COPY-LINE outputs one line.
  4359.  
  4360.         POP     BC              ; restore the line counter.
  4361.         POP     HL              ; and display address.
  4362.         INC     H               ; next line down screen within 'thirds'.
  4363.         LD      A,H             ; high byte to A.
  4364.         AND     $07             ; result will be zero if we have left third.
  4365.         JR      NZ,L0EC9        ; forward to COPY-2 if not to continue loop.
  4366.  
  4367.         LD      A,L             ; consider low byte first.
  4368.         ADD     A,$20           ; increase by 32 - sets carry if back to zero.
  4369.         LD      L,A             ; will be next group of 8.
  4370.         CCF                     ; complement - carry set if more lines in
  4371.                                 ; the previous third.
  4372.         SBC     A,A             ; will be FF, if more, else 00.
  4373.         AND     $F8             ; will be F8 (-8) or 00.
  4374.         ADD     A,H             ; that is subtract 8, if more to do in third.
  4375.         LD      H,A             ; and reset address.
  4376.  
  4377. ;; COPY-2
  4378. L0EC9:  DJNZ    L0EB2           ; back to COPY-1 for all lines.
  4379.  
  4380.         JR      L0EDA           ; forward to COPY-END to switch off the printer
  4381.                                 ; motor and enable interrupts.
  4382.                                 ; Note. Nothing else required.
  4383.  
  4384. ; ------------------------------
  4385. ; Pass printer buffer to printer
  4386. ; ------------------------------
  4387. ; This routine is used to copy 8 text lines from the printer buffer
  4388. ; to the ZX Printer. These text lines are mapped linearly so HL does
  4389. ; not need to be adjusted at the end of each line.
  4390.  
  4391. ;; COPY-BUFF
  4392. L0ECD:  DI                      ; disable interrupts
  4393.         LD      HL,$5B00        ; the base address of the Printer Buffer.
  4394.         LD      B,$08           ; set count to 8 lines of 32 bytes.
  4395.  
  4396. ;; COPY-3
  4397. L0ED3:  PUSH    BC              ; save counter.
  4398.         CALL    L0EF4           ; routine COPY-LINE outputs 32 bytes
  4399.         POP     BC              ; restore counter.
  4400.         DJNZ    L0ED3           ; loop back to COPY-3 for all 8 lines.
  4401.                                 ; then stop motor and clear buffer.
  4402.  
  4403. ; Note. the COPY command rejoins here, essentially to execute the next
  4404. ; three instructions.
  4405.  
  4406. ;; COPY-END
  4407. L0EDA           LD A,$04                ; output value 4 to port
  4408.                 OUT ($FB),A             ; to stop the slowed printer motor.
  4409. L0EDE           EI                      ; enable interrupts.
  4410.  
  4411. ; --------------------
  4412. ; Clear Printer Buffer
  4413. ; --------------------
  4414. ; This routine clears an arbitrary 256 bytes of memory.
  4415. ; Note. The routine seems designed to clear a buffer that follows the
  4416. ; system variables.
  4417. ; The routine should check a flag or HL address and simply return if COPY
  4418. ; is in use.
  4419. ; (T-ADDR-lo would work for the system but not if COPY called externally.)
  4420. ; As a consequence of this omission the buffer will needlessly
  4421. ; be cleared when COPY is used and the screen/printer position may be set to
  4422. ; the start of the buffer and the line number to 0 (B)
  4423. ; giving an 'Out of Screen' error.
  4424. ; There seems to have been an unsuccessful attempt to circumvent the use
  4425. ; of PR_CC_hi.
  4426.  
  4427. ;; CLEAR-PRB
  4428. L0EDF:  LD      HL,$5B00        ; the location of the buffer.
  4429.         LD      (IY+$46),L      ; update PR_CC_lo - set to zero - superfluous.
  4430.         XOR     A               ; clear the accumulator.
  4431.         LD      B,A             ; set count to 256 bytes.
  4432.  
  4433. ;; PRB-BYTES
  4434. L0EE7:  LD      (HL),A          ; set addressed location to zero.
  4435.         INC     HL              ; address next byte - Note. not INC L.
  4436.         DJNZ    L0EE7           ; back to PRB-BYTES. repeat for 256 bytes.
  4437.  
  4438.         RES     1,(IY+$30)      ; set FLAGS2 - signal printer buffer is clear.
  4439.         LD      C,$21           ; set the column position .
  4440.         JP      L0DD9           ; exit via CL-SET and then PO-STORE.
  4441.  
  4442. ; -----------------
  4443. ; Copy line routine
  4444. ; -----------------
  4445. ; This routine is called from COPY and COPY-BUFF to output a line of
  4446. ; 32 bytes to the ZX Printer.
  4447. ; Output to port $FB -
  4448. ; bit 7 set - activate stylus.
  4449. ; bit 7 low - deactivate stylus.
  4450. ; bit 2 set - stops printer.
  4451. ; bit 2 reset - starts printer
  4452. ; bit 1 set - slows printer.
  4453. ; bit 1 reset - normal speed.
  4454.  
  4455. ;; COPY-LINE
  4456. ;===============================
  4457. L0EF4          
  4458.                 LD A,(HL)
  4459.                 RST 0X08
  4460.                 DB AY_PRN_A_
  4461.                 RET
  4462. ;               LD A,B                  ; fetch the counter 1-8 or 1-176
  4463. ;               CP $03                  ; is it 01 or 02 ?.
  4464. ;               SBC A,A                 ; result is $FF if so else $00.
  4465. ;===============================
  4466.                 AND $02                 ; result is 02 now else 00.
  4467.                                         ; bit 1 set slows the printer.
  4468.                 OUT ($FB),A             ; slow the printer for the
  4469.                                         ; last two lines.
  4470.         LD      D,A             ; save the mask to control the printer later.
  4471.  
  4472. ;; COPY-L-1
  4473. L0EFD:  CALL    L1F54           ; call BREAK-KEY to read keyboard immediately.
  4474.         JR      C,L0F0C         ; forward to COPY-L-2 if 'break' not pressed.
  4475.  
  4476.         LD      A,$04           ; else stop the
  4477.         OUT     ($FB),A         ; printer motor.
  4478.         EI                      ; enable interrupts.
  4479.         CALL    L0EDF           ; call routine CLEAR-PRB.
  4480.                                 ; Note. should not be cleared if COPY in use.
  4481.  
  4482. ;; REPORT-Dc
  4483. L0F0A:  RST     08H             ; ERROR-1
  4484.         DB      $0C             ; Error Report: BREAK - CONT repeats
  4485.  
  4486. ;; COPY-L-2
  4487. L0F0C:  IN      A,($FB)         ; test now to see if
  4488.         ADD     A,A             ; a printer is attached.
  4489.         RET     M               ; return if not - but continue with parent
  4490.                                 ; command.
  4491.  
  4492.         JR      NC,L0EFD        ; back to COPY-L-1 if stylus of printer not
  4493.                                 ; in position.
  4494.  
  4495.         LD      C,$20           ; set count to 32 bytes.
  4496.  
  4497. ;; COPY-L-3
  4498. L0F14:  LD      E,(HL)          ; fetch a byte from line.
  4499.         INC     HL              ; address next location. Note. not INC L.
  4500.         LD      B,$08           ; count the bits.
  4501.  
  4502. ;; COPY-L-4
  4503. L0F18:  RL      D               ; prepare mask to receive bit.
  4504.         RL      E               ; rotate leftmost print bit to carry
  4505.         RR      D               ; and back to bit 7 of D restoring bit 1
  4506.  
  4507. ;; COPY-L-5
  4508. L0F1E:  IN      A,($FB)         ; read the port.
  4509.         RRA                     ; bit 0 to carry.
  4510.         JR      NC,L0F1E        ; back to COPY-L-5 if stylus not in position.
  4511.  
  4512.         LD      A,D             ; transfer command bits to A.
  4513.         OUT     ($FB),A         ; and output to port.
  4514.         DJNZ    L0F18           ; loop back to COPY-L-4 for all 8 bits.
  4515.  
  4516.         DEC     C               ; decrease the byte count.
  4517.         JR      NZ,L0F14        ; back to COPY-L-3 until 256 bits done.
  4518.  
  4519.         RET                     ; return to calling routine COPY/COPY-BUFF.
  4520.  
  4521. ; ----------------------------------
  4522. ; Editor routine for BASIC and INPUT
  4523. ; ----------------------------------
  4524. ; The editor is called to prepare or edit a BASIC line.
  4525. ; It is also called from INPUT to input a numeric or string expression.
  4526. ; The behaviour and options are quite different in the various modes
  4527. ; and distinguished by bit 5 of FLAGX.
  4528. ;
  4529. ; This is a compact and highly versatile routine.
  4530.  
  4531. ;; EDITOR
  4532. L0F2C:  LD      HL,($5C3D)      ; fetch ERR_SP
  4533.         PUSH    HL              ; save on stack
  4534.  
  4535. ;; ED-AGAIN
  4536. L0F30:  LD      HL,L107F        ; address: ED-ERROR
  4537.         PUSH    HL              ; save address on stack and
  4538.         LD      ($5C3D),SP      ; make ERR_SP point to it.
  4539.  
  4540. ; Note. While in editing/input mode should an error occur then RST 08 will
  4541. ; update X_PTR to the location reached by CH_ADD and jump to ED-ERROR
  4542. ; where the error will be cancelled and the loop begin again from ED-AGAIN
  4543. ; above. The position of the error will be apparent when the lower screen is
  4544. ; reprinted. If no error then the re-iteration is to ED-LOOP below when
  4545. ; input is arriving from the keyboard.
  4546.  
  4547. ;; ED-LOOP
  4548. L0F38:  CALL    L15D4           ; routine WAIT-KEY gets key possibly
  4549.                                 ; changing the mode.
  4550.         PUSH    AF              ; save key.
  4551.         LD      D,$00           ; and give a short click based
  4552.         LD      E,(IY-$01)      ; on PIP value for duration.
  4553.         LD      HL,$00C8        ; and pitch.
  4554.         CALL    L03B5           ; routine BEEPER gives click - effective
  4555.                                 ; with rubber keyboard.
  4556.         POP     AF              ; get saved key value.
  4557.         LD      HL,L0F38        ; address: ED-LOOP is loaded to HL.
  4558.         PUSH    HL              ; and pushed onto stack.
  4559.  
  4560. ; At this point there is a looping return address on the stack, an error
  4561. ; handler and an input stream set up to supply characters.
  4562. ; The character that has been received can now be processed.
  4563.  
  4564.         CP      $18             ; range 24 to 255 ?
  4565.         JR      NC,L0F81        ; forward to ADD-CHAR if so.
  4566.  
  4567.         CP      $07             ; lower than 7 ?
  4568.         JR      C,L0F81         ; forward to ADD-CHAR also.
  4569.                                 ; Note. This is a 'bug' and chr$ 6, the comma
  4570.                                 ; control character, should have had an
  4571.                                 ; entry in the ED-KEYS table.
  4572.                                 ; Steven Vickers, 1984, Pitman.
  4573.  
  4574.         CP      $10             ; less than 16 ?
  4575.         JR      C,L0F92         ; forward to ED-KEYS if editing control
  4576.                                 ; range 7 to 15 dealt with by a table
  4577.  
  4578.         LD      BC,$0002        ; prepare for ink/paper etc.
  4579.         LD      D,A             ; save character in D
  4580.         CP      $16             ; is it ink/paper/bright etc. ?
  4581.         JR      C,L0F6C         ; forward to ED-CONTR if so
  4582.  
  4583.                                 ; leaves 22d AT and 23d TAB
  4584.                                 ; which can't be entered via KEY-INPUT.
  4585.                                 ; so this code is never normally executed
  4586.                                 ; when the keyboard is used for input.
  4587.  
  4588.         INC     BC              ; if it was AT/TAB - 3 locations required
  4589.         BIT     7,(IY+$37)      ; test FLAGX  - Is this INPUT LINE ?
  4590.         JP      Z,L101E         ; jump to ED-IGNORE if not, else
  4591.  
  4592.         CALL    L15D4           ; routine WAIT-KEY - input address is KEY-NEXT
  4593.                                 ; but is reset to KEY-INPUT
  4594.         LD      E,A             ; save first in E
  4595.  
  4596. ;; ED-CONTR
  4597. L0F6C:  CALL    L15D4           ; routine WAIT-KEY for control.
  4598.                                 ; input address will be key-next.
  4599.  
  4600.         PUSH    DE              ; saved code/parameters
  4601.         LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4602.         RES     0,(IY+$07)      ; set MODE to 'L'
  4603.  
  4604.         CALL    L1655           ; routine MAKE-ROOM makes 2/3 spaces at cursor
  4605.  
  4606.         POP     BC              ; restore code/parameters
  4607.         INC     HL              ; address first location
  4608.         LD      (HL),B          ; place code (ink etc.)
  4609.         INC     HL              ; address next
  4610.         LD      (HL),C          ; place possible parameter. If only one
  4611.                                 ; then DE points to this location also.
  4612.         JR      L0F8B           ; forward to ADD-CH-1
  4613.  
  4614. ; ------------------------
  4615. ; Add code to current line
  4616. ; ------------------------
  4617. ; this is the branch used to add normal non-control characters
  4618. ; with ED-LOOP as the stacked return address.
  4619. ; it is also the OUTPUT service routine for system channel 'R'.
  4620.  
  4621. ;; ADD-CHAR
  4622. L0F81:  RES     0,(IY+$07)      ; set MODE to 'L'
  4623.  
  4624. X0F85:  LD      HL,($5C5B)      ; fetch address of keyboard cursor from K_CUR
  4625.         CALL    L1652           ; routine ONE-SPACE creates one space.
  4626.  
  4627. ; either a continuation of above or from ED-CONTR with ED-LOOP on stack.
  4628.  
  4629. ;; ADD-CH-1
  4630. L0F8B:  LD      (DE),A          ; load current character to last new location.
  4631.         INC     DE              ; address next
  4632.         LD      ($5C5B),DE      ; and update K_CUR system variable.
  4633.         RET                     ; return - either a simple return
  4634.                                 ; from ADD-CHAR or to ED-LOOP on stack.
  4635.  
  4636. ; ---
  4637.  
  4638. ; a branch of the editing loop to deal with control characters
  4639. ; using a look-up table.
  4640.  
  4641. ;; ED-KEYS
  4642. L0F92:  LD      E,A             ; character to E.
  4643.         LD      D,$00           ; prepare to add.
  4644.         LD      HL,L0FA0 - 7    ; base address of editing keys table. $0F99
  4645.         ADD     HL,DE           ; add E
  4646.         LD      E,(HL)          ; fetch offset to E
  4647.         ADD     HL,DE           ; add offset for address of handling routine.
  4648.         PUSH    HL              ; push the address on machine stack.
  4649.         LD      HL,($5C5B)      ; load address of cursor from K_CUR.
  4650.         RET                     ; an make an indirect jump forward to routine.
  4651.  
  4652. ; ------------------
  4653. ; Editing keys table
  4654. ; ------------------
  4655. ; For each code in the range $07 to $0F this table contains a
  4656. ; single offset byte to the routine that services that code.
  4657. ; Note. for what was intended there should also have been an
  4658. ; entry for chr$ 6 with offset to ed-symbol.
  4659.  
  4660. ;; ed-keys-t
  4661. L0FA0:  DB    L0FA9 - $  ; 07d offset $09 to Address: ED-EDIT
  4662.         DB    L1007 - $  ; 08d offset $66 to Address: ED-LEFT
  4663.         DB    L100C - $  ; 09d offset $6A to Address: ED-RIGHT
  4664.         DB    L0FF3 - $  ; 10d offset $50 to Address: ED-DOWN
  4665.         DB    L1059 - $  ; 11d offset $B5 to Address: ED-UP
  4666.         DB    L1015 - $  ; 12d offset $70 to Address: ED-DELETE
  4667.         DB    L1024 - $  ; 13d offset $7E to Address: ED-ENTER
  4668.         DB    L1076 - $  ; 14d offset $CF to Address: ED-SYMBOL
  4669.         DB    L107C - $  ; 15d offset $D4 to Address: ED-GRAPH
  4670.  
  4671. ; ---------------
  4672. ; Handle EDIT key
  4673. ; ---------------
  4674. ; The user has pressed SHIFT 1 to bring edit line down to bottom of screen.
  4675. ; Alternatively the user wishes to clear the input buffer and start again.
  4676. ; Alternatively ...
  4677.  
  4678. ;; ED-EDIT
  4679. L0FA9:  LD      HL,($5C49)      ; fetch E_PPC the last line number entered.
  4680.                                 ; Note. may not exist and may follow program.
  4681.         BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4682.         JP      NZ,L1097        ; jump forward to CLEAR-SP if not in editor.
  4683.  
  4684.         CALL    L196E           ; routine LINE-ADDR to find address of line
  4685.                                 ; or following line if it doesn't exist.
  4686.         CALL    L1695           ; routine LINE-NO will get line number from
  4687.                                 ; address or previous line if at end-marker.
  4688.         LD      A,D             ; if there is no program then DE will
  4689.         OR      E               ; contain zero so test for this.
  4690.         JP      Z,L1097         ; jump to to CLEAR-SP if so.
  4691.  
  4692. ; Note. at this point we have a validated line number, not just an
  4693. ; approximation and it would be best to update E_PPC with the true
  4694. ; cursor line value which would enable the line cursor to be suppressed
  4695. ; in all situations - see shortly.
  4696.  
  4697.         PUSH    HL              ; save address of line.
  4698.         INC     HL              ; address low byte of length.
  4699.         LD      C,(HL)          ; transfer to C
  4700.         INC     HL              ; next to high byte
  4701.         LD      B,(HL)          ; transfer to B.
  4702.         LD      HL,$000A        ; an overhead of ten bytes
  4703.         ADD     HL,BC           ; is added to length.
  4704.         LD      B,H             ; transfer adjusted value
  4705.         LD      C,L             ; to BC register.
  4706.         CALL    L1F05           ; routine TEST-ROOM checks free memory.
  4707.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  4708.         LD      HL,($5C51)      ; address CURCHL
  4709.         EX      (SP),HL         ; swap with line address on stack
  4710.         PUSH    HL              ; save line address underneath
  4711.  
  4712.         LD      A,$FF           ; select system channel 'R'
  4713.         CALL    L1601           ; routine CHAN-OPEN opens it
  4714.  
  4715.         POP     HL              ; drop line address
  4716.         DEC     HL              ; make it point to first byte of line num.
  4717.         DEC     (IY+$0F)        ; decrease E_PPC_lo to suppress line cursor.
  4718.                                 ; Note. ineffective when E_PPC is one
  4719.                                 ; greater than last line of program perhaps
  4720.                                 ; as a result of a delete.
  4721.                                 ; credit. Paul Harrison 1982.
  4722.  
  4723.         CALL    L1855           ; routine OUT-LINE outputs the BASIC line
  4724.                                 ; to the editing area.
  4725.         INC     (IY+$0F)        ; restore E_PPC_lo to the previous value.
  4726.         LD      HL,($5C59)      ; address E_LINE in editing area.
  4727.         INC     HL              ; advance
  4728.         INC     HL              ; past space
  4729.         INC     HL              ; and digit characters
  4730.         INC     HL              ; of line number.
  4731.  
  4732.         LD      ($5C5B),HL      ; update K_CUR to address start of BASIC.
  4733.         POP     HL              ; restore the address of CURCHL.
  4734.         CALL    L1615           ; routine CHAN-FLAG sets flags for it.
  4735.         RET                     ; RETURN to ED-LOOP.
  4736.  
  4737. ; -------------------
  4738. ; Cursor down editing
  4739. ; -------------------
  4740. ; The BASIC lines are displayed at the top of the screen and the user
  4741. ; wishes to move the cursor down one line in edit mode.
  4742. ; In input mode this key can be used as an alternative to entering STOP.
  4743.  
  4744. ;; ED-DOWN
  4745. L0FF3:  BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  4746.         JR      NZ,L1001        ; skip to ED-STOP if so
  4747.  
  4748.         LD      HL,$5C49        ; address E_PPC - 'current line'
  4749.         CALL    L190F           ; routine LN-FETCH fetches number of next
  4750.                                 ; line or same if at end of program.
  4751.         JR      L106E           ; forward to ED-LIST to produce an
  4752.                                 ; automatic listing.
  4753.  
  4754. ; ---
  4755.  
  4756. ;; ED-STOP
  4757. L1001:  LD      (IY+$00),$10    ; set ERR_NR to 'STOP in INPUT' code
  4758.         JR      L1024           ; forward to ED-ENTER to produce error.
  4759.  
  4760. ; -------------------
  4761. ; Cursor left editing
  4762. ; -------------------
  4763. ; This acts on the cursor in the lower section of the screen in both
  4764. ; editing and input mode.
  4765.  
  4766. ;; ED-LEFT
  4767. L1007:  CALL    L1031           ; routine ED-EDGE moves left if possible
  4768.         JR      L1011           ; forward to ED-CUR to update K-CUR
  4769.                                 ; and return to ED-LOOP.
  4770.  
  4771. ; --------------------
  4772. ; Cursor right editing
  4773. ; --------------------
  4774. ; This acts on the cursor in the lower screen in both editing and input
  4775. ; mode and moves it to the right.
  4776.  
  4777. ;; ED-RIGHT
  4778. L100C:  LD      A,(HL)          ; fetch addressed character.
  4779.         CP      $0D             ; is it carriage return ?
  4780.         RET     Z               ; return if so to ED-LOOP
  4781.  
  4782.         INC     HL              ; address next character
  4783.  
  4784. ;; ED-CUR
  4785. L1011:  LD      ($5C5B),HL      ; update K_CUR system variable
  4786.         RET                     ; return to ED-LOOP
  4787.  
  4788. ; --------------
  4789. ; DELETE editing
  4790. ; --------------
  4791. ; This acts on the lower screen and deletes the character to left of
  4792. ; cursor. If control characters are present these are deleted first
  4793. ; leaving the naked parameter (0-7) which appears as a '?' except in the
  4794. ; case of chr$ 6 which is the comma control character. It is not mandatory
  4795. ; to delete these second characters.
  4796.  
  4797. ;; ED-DELETE
  4798. L1015:  CALL    L1031           ; routine ED-EDGE moves cursor to left.
  4799.         LD      BC,$0001        ; of character to be deleted.
  4800.         JP      L19E8           ; to RECLAIM-2 reclaim the character.
  4801.  
  4802. ; ------------------------------------------
  4803. ; Ignore next 2 codes from key-input routine
  4804. ; ------------------------------------------
  4805. ; Since AT and TAB cannot be entered this point is never reached
  4806. ; from the keyboard. If inputting from a tape device or network then
  4807. ; the control and two following characters are ignored and processing
  4808. ; continues as if a carriage return had been received.
  4809. ; Here, perhaps, another Spectrum has said print #15; AT 0,0; "This is yellow"
  4810. ; and this one is interpreting input #15; a$.
  4811.  
  4812. ;; ED-IGNORE
  4813. L101E:  CALL    L15D4           ; routine WAIT-KEY to ignore keystroke.
  4814.         CALL    L15D4           ; routine WAIT-KEY to ignore next key.
  4815.  
  4816. ; -------------
  4817. ; Enter/newline
  4818. ; -------------
  4819. ; The enter key has been pressed to have BASIC line or input accepted.
  4820.  
  4821. ;; ED-ENTER
  4822. L1024:  POP     HL              ; discard address ED-LOOP
  4823.         POP     HL              ; drop address ED-ERROR
  4824.  
  4825. ;; ED-END
  4826. L1026:  POP     HL              ; the previous value of ERR_SP
  4827.         LD      ($5C3D),HL      ; is restored to ERR_SP system variable
  4828.         BIT     7,(IY+$00)      ; is ERR_NR $FF (= 'OK') ?
  4829.         RET     NZ              ; return if so
  4830.  
  4831.         LD      SP,HL           ; else put error routine on stack
  4832.         RET                     ; and make an indirect jump to it.
  4833.  
  4834. ; -----------------------------
  4835. ; Move cursor left when editing
  4836. ; -----------------------------
  4837. ; This routine moves the cursor left. The complication is that it must
  4838. ; not position the cursor between control codes and their parameters.
  4839. ; It is further complicated in that it deals with TAB and AT characters
  4840. ; which are never present from the keyboard.
  4841. ; The method is to advance from the beginning of the line each time,
  4842. ; jumping one, two, or three characters as necessary saving the original
  4843. ; position at each jump in DE. Once it arrives at the cursor then the next
  4844. ; legitimate leftmost position is in DE.
  4845.  
  4846. ;; ED-EDGE
  4847. L1031:  SCF                     ; carry flag must be set to call the nested
  4848.         CALL    L1195           ; subroutine SET-DE.
  4849.                                 ; if input   then DE=WORKSP
  4850.                                 ; if editing then DE=E_LINE
  4851.         SBC     HL,DE           ; subtract address from start of line
  4852.         ADD     HL,DE           ; and add back.
  4853.         INC     HL              ; adjust for carry.
  4854.         POP     BC              ; drop return address
  4855.         RET     C               ; return to ED-LOOP if already at left
  4856.                                 ; of line.
  4857.  
  4858.         PUSH    BC              ; resave return address - ED-LOOP.
  4859.         LD      B,H             ; transfer HL - cursor address
  4860.         LD      C,L             ; to BC register pair.
  4861.                                 ; at this point DE addresses start of line.
  4862.  
  4863. ;; ED-EDGE-1
  4864. L103E:  LD      H,D             ; transfer DE - leftmost pointer
  4865.         LD      L,E             ; to HL
  4866.         INC     HL              ; address next leftmost character to
  4867.                                 ; advance position each time.
  4868.         LD      A,(DE)          ; pick up previous in A
  4869.         AND     $F0             ; lose the low bits
  4870.         CP      $10             ; is it INK to TAB $10-$1F ?
  4871.                                 ; that is, is it followed by a parameter ?
  4872.         JR      NZ,L1051        ; to ED-EDGE-2 if not
  4873.                                 ; HL has been incremented once
  4874.  
  4875.         INC     HL              ; address next as at least one parameter.
  4876.  
  4877. ; in fact since 'tab' and 'at' cannot be entered the next section seems
  4878. ; superfluous.
  4879. ; The test will always fail and the jump to ED-EDGE-2 will be taken.
  4880.  
  4881.         LD      A,(DE)          ; reload leftmost character
  4882.         SUB     $17             ; decimal 23 ('tab')
  4883.         ADC     A,$00           ; will be 0 for 'tab' and 'at'.
  4884.         JR      NZ,L1051        ; forward to ED-EDGE-2 if not
  4885.                                 ; HL has been incremented twice
  4886.  
  4887.         INC     HL              ; increment a third time for 'at'/'tab'
  4888.  
  4889. ;; ED-EDGE-2
  4890. L1051:  AND     A               ; prepare for true subtraction
  4891.         SBC     HL,BC           ; subtract cursor address from pointer
  4892.         ADD     HL,BC           ; and add back
  4893.                                 ; Note when HL matches the cursor position BC,
  4894.                                 ; there is no carry and the previous
  4895.                                 ; position is in DE.
  4896.         EX      DE,HL           ; transfer result to DE if looping again.
  4897.                                 ; transfer DE to HL to be used as K-CUR
  4898.                                 ; if exiting loop.
  4899.         JR      C,L103E         ; back to ED-EDGE-1 if cursor not matched.
  4900.  
  4901.         RET                     ; return.
  4902.  
  4903. ; -----------------
  4904. ; Cursor up editing
  4905. ; -----------------
  4906. ; The main screen displays part of the BASIC program and the user wishes
  4907. ; to move up one line scrolling if necessary.
  4908. ; This has no alternative use in input mode.
  4909.  
  4910. ;; ED-UP
  4911. L1059:  BIT     5,(IY+$37)      ; test FLAGX  - input mode ?
  4912.         RET     NZ              ; return if not in editor - to ED-LOOP.
  4913.  
  4914.         LD      HL,($5C49)      ; get current line from E_PPC
  4915.         CALL    L196E           ; routine LINE-ADDR gets address
  4916.         EX      DE,HL           ; and previous in DE
  4917.         CALL    L1695           ; routine LINE-NO gets prev line number
  4918.         LD      HL,$5C4A        ; set HL to E_PPC_hi as next routine stores
  4919.                                 ; top first.
  4920.         CALL    L191C           ; routine LN-STORE loads DE value to HL
  4921.                                 ; high byte first - E_PPC_lo takes E
  4922.  
  4923. ; this branch is also taken from ed-down.
  4924.  
  4925. ;; ED-LIST
  4926. L106E:  CALL    L1795           ; routine AUTO-LIST lists to upper screen
  4927.                                 ; including adjusted current line.
  4928.         LD      A,$00           ; select lower screen again
  4929.         JP      L1601           ; exit via CHAN-OPEN to ED-LOOP
  4930.  
  4931. ; --------------------------------
  4932. ; Use of symbol and graphics codes
  4933. ; --------------------------------
  4934. ; These will not be encountered with the keyboard but would be handled
  4935. ; otherwise as follows.
  4936. ; As noted earlier, Vickers says there should have been an entry in
  4937. ; the KEYS table for chr$ 6 which also pointed here.
  4938. ; If, for simplicity, two Spectrums were both using #15 as a bi-directional
  4939. ; channel connected to each other:-
  4940. ; then when the other Spectrum has said PRINT #15; x, y
  4941. ; input #15; i ; j  would treat the comma control as a newline and the
  4942. ; control would skip to input j.
  4943. ; You can get round the missing chr$ 6 handler by sending multiple print
  4944. ; items separated by a newline '.
  4945.  
  4946. ; chr$14 would have the same functionality.
  4947.  
  4948. ; This is chr$ 14.
  4949. ;; ED-SYMBOL
  4950. L1076:  BIT     7,(IY+$37)      ; test FLAGX - is this INPUT LINE ?
  4951.         JR      Z,L1024         ; back to ED-ENTER if not to treat as if
  4952.                                 ; enter had been pressed.
  4953.                                 ; else continue and add code to buffer.
  4954.  
  4955. ; Next is chr$ 15
  4956. ; Note that ADD-CHAR precedes the table so we can't offset to it directly.
  4957.  
  4958. ;; ED-GRAPH
  4959. L107C:  JP      L0F81           ; jump back to ADD-CHAR
  4960.  
  4961. ; --------------------
  4962. ; Editor error routine
  4963. ; --------------------
  4964. ; If an error occurs while editing, or inputting, then ERR_SP
  4965. ; points to the stack location holding address ED_ERROR.
  4966.  
  4967. ;; ED-ERROR
  4968. L107F:  BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  4969.         JR      Z,L1026         ; back to ED-END if not.
  4970.  
  4971. ; but as long as we're editing lines or inputting from the keyboard, then
  4972. ; we've run out of memory so give a short rasp.
  4973.  
  4974.         LD      (IY+$00),$FF    ; reset ERR_NR to 'OK'.
  4975.         LD      D,$00           ; prepare for beeper.
  4976.         LD      E,(IY-$02)      ; use RASP value.
  4977.         LD      HL,$1A90        ; set a duration.
  4978.         CALL    L03B5           ; routine BEEPER emits a warning rasp.
  4979.         JP      L0F30           ; to ED-AGAIN to re-stack address of
  4980.                                 ; this routine and make ERR_SP point to it.
  4981.  
  4982. ; ---------------------
  4983. ; Clear edit/work space
  4984. ; ---------------------
  4985. ; The editing area or workspace is cleared depending on context.
  4986. ; This is called from ED-EDIT to clear workspace if edit key is
  4987. ; used during input, to clear editing area if no program exists
  4988. ; and to clear editing area prior to copying the edit line to it.
  4989. ; It is also used by the error routine to clear the respective
  4990. ; area depending on FLAGX.
  4991.  
  4992. ;; CLEAR-SP
  4993. L1097:  PUSH    HL              ; preserve HL
  4994.         CALL    L1190           ; routine SET-HL
  4995.                                 ; if in edit   HL = WORKSP-1, DE = E_LINE
  4996.                                 ; if in input  HL = STKBOT,   DE = WORKSP
  4997.         DEC     HL              ; adjust
  4998.         CALL    L19E5           ; routine RECLAIM-1 reclaims space
  4999.         LD      ($5C5B),HL      ; set K_CUR to start of empty area
  5000.         LD      (IY+$07),$00    ; set MODE to 'KLC'
  5001.         POP     HL              ; restore HL.
  5002.         RET                     ; return.
  5003.  
  5004. ; ---------------------
  5005. ; Handle keyboard input
  5006. ; ---------------------
  5007. ; This is the service routine for the input stream of the keyboard
  5008. ; channel 'K'.
  5009.  
  5010. ;; KEY-INPUT
  5011. L10A8:  BIT     3,(IY+$02)      ; test TV_FLAG  - has a key been pressed in
  5012.                                 ; editor ?
  5013.         CALL    NZ,L111D        ; routine ED-COPY if so to reprint the lower
  5014.                                 ; screen at every keystroke.
  5015.         AND     A               ; clear carry - required exit condition.
  5016.         BIT     5,(IY+$01)      ; test FLAGS  - has a new key been pressed ?
  5017.         RET     Z               ; return if not.
  5018.  
  5019.         LD      A,($5C08)       ; system variable LASTK will hold last key -
  5020.                                 ; from the interrupt routine.
  5021.         RES     5,(IY+$01)      ; update FLAGS  - reset the new key flag.
  5022.         PUSH    AF              ; save the input character.
  5023.         BIT     5,(IY+$02)      ; test TV_FLAG  - clear lower screen ?
  5024.         CALL    NZ,L0D6E        ; routine CLS-LOWER if so.
  5025.  
  5026.         POP     AF              ; restore the character code.
  5027.         CP      $20             ; if space or higher then
  5028.         JR      NC,L111B        ; forward to KEY-DONE2 and return with carry
  5029.                                 ; set to signal key-found.
  5030.  
  5031.         CP      $10             ; with 16d INK and higher skip
  5032.         JR      NC,L10FA        ; forward to KEY-CONTR.
  5033.  
  5034.         CP      $06             ; for 6 - 15d
  5035.         JR      NC,L10DB        ; skip forward to KEY-M-CL to handle Modes
  5036.                                 ; and CapsLock.
  5037.  
  5038. ; that only leaves 0-5, the flash bright inverse switches.
  5039.  
  5040.         LD      B,A             ; save character in B
  5041.         AND     $01             ; isolate the embedded parameter (0/1).
  5042.         LD      C,A             ; and store in C
  5043.         LD      A,B             ; re-fetch copy (0-5)
  5044.         RRA                     ; halve it 0, 1 or 2.
  5045.         ADD     A,$12           ; add 18d gives 'flash', 'bright'
  5046.                                 ; and 'inverse'.
  5047.         JR      L1105           ; forward to KEY-DATA with the
  5048.                                 ; parameter (0/1) in C.
  5049.  
  5050. ; ---
  5051.  
  5052. ; Now separate capslock 06 from modes 7-15.
  5053.  
  5054. ;; KEY-M-CL
  5055. L10DB:  JR      NZ,L10E6        ; forward to KEY-MODE if not 06 (capslock)
  5056.  
  5057.         LD      HL,$5C6A        ; point to FLAGS2
  5058.         LD      A,$08           ; value 00000100
  5059.         XOR     (HL)            ; toggle BIT 2 of FLAGS2 the capslock bit
  5060.         LD      (HL),A          ; and store result in FLAGS2 again.
  5061.         JR      L10F4           ; forward to KEY-FLAG to signal no-key.
  5062.  
  5063. ; ---
  5064.  
  5065. ;; KEY-MODE
  5066. L10E6:  CP      $0E             ; compare with chr 14d
  5067.         RET     C               ; return with carry set "key found" for
  5068.                                 ; codes 7 - 13d leaving 14d and 15d
  5069.                                 ; which are converted to mode codes.
  5070.  
  5071.         SUB     $0D             ; subtract 13d leaving 1 and 2
  5072.                                 ; 1 is 'E' mode, 2 is 'G' mode.
  5073.         LD      HL,$5C41        ; address the MODE system variable.
  5074.         CP      (HL)            ; compare with existing value before
  5075.         LD      (HL),A          ; inserting the new value.
  5076.         JR      NZ,L10F4        ; forward to KEY-FLAG if it has changed.
  5077.  
  5078.         LD      (HL),$00        ; else make MODE zero - KLC mode
  5079.                                 ; Note. while in Extended/Graphics mode,
  5080.                                 ; the Extended Mode/Graphics key is pressed
  5081.                                 ; again to get out.
  5082.  
  5083. ;; KEY-FLAG
  5084. L10F4:  SET     3,(IY+$02)      ; update TV_FLAG  - show key state has changed
  5085.         CP      A               ; clear carry and reset zero flags -
  5086.                                 ; no actual key returned.
  5087.         RET                     ; make the return.
  5088.  
  5089. ; ---
  5090.  
  5091. ; now deal with colour controls - 16-23 ink, 24-31 paper
  5092.  
  5093. ;; KEY-CONTR
  5094. L10FA:  LD      B,A             ; make a copy of character.
  5095.         AND     $07             ; mask to leave bits 0-7
  5096.         LD      C,A             ; and store in C.
  5097.         LD      A,$10           ; initialize to 16d - INK.
  5098.         BIT     3,B             ; was it paper ?
  5099.         JR      NZ,L1105        ; forward to KEY-DATA with INK 16d and
  5100.                                 ; colour in C.
  5101.  
  5102.         INC     A               ; else change from INK to PAPER (17d) if so.
  5103.  
  5104. ;; KEY-DATA
  5105. L1105:  LD      (IY-$2D),C      ; put the colour (0-7)/state(0/1) in KDATA
  5106.         LD      DE,L110D        ; address: KEY-NEXT will be next input stream
  5107.         JR      L1113           ; forward to KEY-CHAN to change it ...
  5108.  
  5109. ; ---
  5110.  
  5111. ; ... so that INPUT_AD directs control to here at next call to WAIT-KEY
  5112.  
  5113. ;; KEY-NEXT
  5114. L110D:  LD      A,($5C0D)       ; pick up the parameter stored in KDATA.
  5115.         LD      DE,L10A8        ; address: KEY-INPUT will be next input stream
  5116.                                 ; continue to restore default channel and
  5117.                                 ; make a return with the control code.
  5118.  
  5119. ;; KEY-CHAN
  5120. L1113:  LD      HL,($5C4F)      ; address start of CHANNELS area using CHANS
  5121.                                 ; system variable.
  5122.                                 ; Note. One might have expected CURCHL to
  5123.                                 ; have been used.
  5124.         INC     HL              ; step over the
  5125.         INC     HL              ; output address
  5126.         LD      (HL),E          ; and update the input
  5127.         INC     HL              ; routine address for
  5128.         LD      (HL),D          ; the next call to WAIT-KEY.
  5129.  
  5130. ;; KEY-DONE2
  5131. L111B:  SCF                     ; set carry flag to show a key has been found
  5132.         RET                     ; and return.
  5133.  
  5134. ; --------------------
  5135. ; Lower screen copying
  5136. ; --------------------
  5137. ; This subroutine is called whenever the line in the editing area or
  5138. ; input workspace is required to be printed to the lower screen.
  5139. ; It is by calling this routine after any change that the cursor, for
  5140. ; instance, appears to move to the left.
  5141. ; Remember the edit line will contain characters and tokens
  5142. ; e.g. "1000 LET a = 1" is 12 characters.
  5143.  
  5144. ;; ED-COPY
  5145. L111D:  CALL    L0D4D           ; routine TEMPS sets temporary attributes.
  5146.         RES     3,(IY+$02)      ; update TV_FLAG  - signal no change in mode
  5147.         RES     5,(IY+$02)      ; update TV_FLAG  - signal don't clear lower
  5148.                                 ; screen.
  5149.         LD      HL,($5C8A)      ; fetch SPOSNL
  5150.         PUSH    HL              ; and save on stack.
  5151.  
  5152.         LD      HL,($5C3D)      ; fetch ERR_SP
  5153.         PUSH    HL              ; and save also
  5154.         LD      HL,L1167        ; address: ED-FULL
  5155.         PUSH    HL              ; is pushed as the error routine
  5156.         LD      ($5C3D),SP      ; and ERR_SP made to point to it.
  5157.  
  5158.         LD      HL,($5C82)      ; fetch ECHO_E
  5159.         PUSH    HL              ; and push also
  5160.  
  5161.         SCF                     ; set carry flag to control SET-DE
  5162.         CALL    L1195           ; call routine SET-DE
  5163.                                 ; if in input DE = WORKSP
  5164.                                 ; if in edit  DE = E_LINE
  5165.         EX      DE,HL           ; start address to HL
  5166.  
  5167.         CALL    L187D           ; routine OUT-LINE2 outputs entire line up to
  5168.                                 ; carriage return including initial
  5169.                                 ; characterized line number when present.
  5170.         EX      DE,HL           ; transfer new address to DE
  5171.         CALL    L18E1           ; routine OUT-CURS considers a
  5172.                                 ; terminating cursor.
  5173.  
  5174.         LD      HL,($5C8A)      ; fetch updated SPOSNL
  5175.         EX      (SP),HL         ; exchange with ECHO_E on stack
  5176.         EX      DE,HL           ; transfer ECHO_E to DE
  5177.         CALL    L0D4D           ; routine TEMPS to re-set attributes
  5178.                                 ; if altered.
  5179.  
  5180. ; the lower screen was not cleared, at the outset, so if deleting then old
  5181. ; text from a previous print may follow this line and requires blanking.
  5182.  
  5183. ;; ED-BLANK
  5184. L1150:  LD      A,($5C8B)       ; fetch SPOSNL_hi is current line
  5185.         SUB     D               ; compare with old
  5186.         JR      C,L117C         ; forward to ED-C-DONE if no blanking
  5187.  
  5188.         JR      NZ,L115E        ; forward to ED-SPACES if line has changed
  5189.  
  5190.         LD      A,E             ; old column to A
  5191.         SUB     (IY+$50)        ; subtract new in SPOSNL_lo
  5192.         JR      NC,L117C        ; forward to ED-C-DONE if no backfilling.
  5193.  
  5194. ;; ED-SPACES
  5195. L115E:  LD      A,$20           ; prepare a space.
  5196.         PUSH    DE              ; save old line/column.
  5197.         CALL    L09F4           ; routine PRINT-OUT prints a space over
  5198.                                 ; any text from previous print.
  5199.                                 ; Note. Since the blanking only occurs when
  5200.                                 ; using $09F4 to print to the lower screen,
  5201.                                 ; there is no need to vector via a RST 10
  5202.                                 ; and we can use this alternate set.
  5203.         POP     DE              ; restore the old line column.
  5204.         JR      L1150           ; back to ED-BLANK until all old text blanked.
  5205.  
  5206. ; -------
  5207. ; ED-FULL
  5208. ; -------
  5209. ; this is the error routine addressed by ERR_SP. This is not for the out of
  5210. ; memory situation as we're just printing. The pitch and duration are exactly
  5211. ; the same as used by ED-ERROR from which this has been augmented. The
  5212. ; situation is that the lower screen is full and a rasp is given to suggest
  5213. ; that this is perhaps not the best idea you've had that day.
  5214.  
  5215. ;; ED-FULL
  5216. L1167:  LD      D,$00           ; prepare to moan.
  5217.         LD      E,(IY-$02)      ; fetch RASP value.
  5218.         LD      HL,$1A90        ; set duration.
  5219.         CALL    L03B5           ; routine BEEPER.
  5220.         LD      (IY+$00),$FF    ; clear ERR_NR.
  5221.         LD      DE,($5C8A)      ; fetch SPOSNL.
  5222.         JR      L117E           ; forward to ED-C-END
  5223.  
  5224. ; -------
  5225.  
  5226. ; the exit point from line printing continues here.
  5227.  
  5228. ;; ED-C-DONE
  5229. L117C:  POP     DE              ; fetch new line/column.
  5230.         POP     HL              ; fetch the error address.
  5231.  
  5232. ; the error path rejoins here.
  5233.  
  5234. ;; ED-C-END
  5235. L117E:  POP     HL              ; restore the old value of ERR_SP.
  5236.         LD      ($5C3D),HL      ; update the system variable ERR_SP
  5237.         POP     BC              ; old value of SPOSN_L
  5238.         PUSH    DE              ; save new value
  5239.         CALL    L0DD9           ; routine CL-SET and PO-STORE
  5240.                                 ; update ECHO_E and SPOSN_L from BC
  5241.         POP     HL              ; restore new value
  5242.         LD      ($5C82),HL      ; and update ECHO_E
  5243.         LD      (IY+$26),$00    ; make error pointer X_PTR_hi out of bounds
  5244.         RET                     ; return
  5245.  
  5246. ; -----------------------------------------------
  5247. ; Point to first and last locations of work space
  5248. ; -----------------------------------------------
  5249. ; These two nested routines ensure that the appropriate pointers are
  5250. ; selected for the editing area or workspace. The routines that call
  5251. ; these routines are designed to work on either area.
  5252.  
  5253. ; this routine is called once
  5254. ;; SET-HL
  5255. L1190:  LD      HL,($5C61)      ; fetch WORKSP to HL.
  5256.         DEC     HL              ; point to last location of editing area.
  5257.         AND     A               ; clear carry to limit exit points to first
  5258.                                 ; or last.
  5259.  
  5260. ; this routine is called with carry set and exits at a conditional return.
  5261.  
  5262. ;; SET-DE
  5263. L1195:  LD      DE,($5C59)      ; fetch E_LINE to DE
  5264.         BIT     5,(IY+$37)      ; test FLAGX  - Input Mode ?
  5265.         RET     Z               ; return now if in editing mode
  5266.  
  5267.         LD      DE,($5C61)      ; fetch WORKSP to DE
  5268.         RET     C               ; return if carry set ( entry = set-de)
  5269.  
  5270.         LD      HL,($5C63)      ; fetch STKBOT to HL as well
  5271.         RET                     ; and return  (entry = set-hl (in input))
  5272.  
  5273. ; -------------------------------
  5274. ; Remove floating point from line
  5275. ; -------------------------------
  5276. ; When a BASIC LINE or the INPUT BUFFER is parsed any numbers will have
  5277. ; an invisible chr 14d inserted after them and the 5-byte integer or
  5278. ; floating point form inserted after that. Similar invisible value holders
  5279. ; are also created after the numeric and string variables in a DEF FN list.
  5280. ; This routine removes these 'compiled' numbers from the edit line or
  5281. ; input workspace.
  5282.  
  5283. ;; REMOVE-FP
  5284. L11A7:  LD      A,(HL)          ; fetch character
  5285.         CP      $0E             ; is it the number marker ?
  5286.         LD      BC,$0006        ; prepare for six bytes
  5287.         CALL    Z,L19E8         ; routine RECLAIM-2 reclaims space if $0E
  5288.         LD      A,(HL)          ; reload next (or same) character
  5289.         INC     HL              ; and advance address
  5290.         CP      $0D             ; end of line or input buffer ?
  5291.         JR      NZ,L11A7        ; back to REMOVE-FP until entire line done.
  5292.  
  5293.         RET                     ; return
  5294.  
  5295.  
  5296. ;*********************************
  5297. ;** Part 6. EXECUTIVE ROUTINES  **
  5298. ;*********************************
  5299.  
  5300.  
  5301. ; The memory.
  5302. ;
  5303. ; +---------+-----------+------------+--------------+-------------+--
  5304. ; | BASIC   |  Display  | Attributes | ZX Printer   |    System   |
  5305. ; |  ROM    |   File    |    File    |   Buffer     |  Variables  |
  5306. ; +---------+-----------+------------+--------------+-------------+--
  5307. ; ^         ^           ^            ^              ^             ^
  5308. ; $0000   $4000       $5800        $5B00          $5C00         $5CB6 = CHANS
  5309. ;
  5310. ;
  5311. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5312. ;    | Channel  |$80|  BASIC  | Variables |$80| Edit Line  |NL|$80|
  5313. ;    |   Info   |   | Program |   Area    |   | or Command |  |   |
  5314. ;  --+----------+---+---------+-----------+---+------------+--+---+--
  5315. ;    ^              ^         ^               ^                   ^
  5316. ;  CHANS           PROG      VARS           E_LINE              WORKSP
  5317. ;
  5318. ;
  5319. ;                             ---5-->         <---2---  <--3---
  5320. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5321. ;    | INPUT |NL| Temporary  | Calc. | Spare | Machine | GOSUB |?|$3E| UDGs |
  5322. ;    | data  |  | Work Space | Stack |       |  Stack  | Stack | |   |      |
  5323. ;  --+-------+--+------------+-------+-------+---------+-------+-+---+------+
  5324. ;    ^                       ^       ^       ^                   ^   ^      ^
  5325. ;  WORKSP                  STKBOT  STKEND   sp               RAMTOP UDG  P_RAMT
  5326. ;                                                                        
  5327.  
  5328. ; -------------------
  5329. ; Handle NEW command
  5330. ; -------------------
  5331. ; The NEW command is about to set all RAM below RAMTOP to zero and
  5332. ; then re-initialize the system. All RAM above RAMTOP should, and will be,
  5333. ; preserved.
  5334. ; There is nowhere to store values in RAM or on the stack which becomes
  5335. ; inoperable. Similarly PUSH and CALL instructions cannot be used to
  5336. ; store values or section common code. The alternate register set is the only
  5337. ; place available to store 3 persistent 16-bit system variables.
  5338.  
  5339. ;; NEW
  5340. L11B7:  DI                      ; disable interrupts - machine stack will be
  5341.                                 ; cleared.
  5342.         LD      A,$FF           ; flag coming from NEW.
  5343.         LD      DE,($5CB2)      ; fetch RAMTOP as top value.
  5344.         EXX                     ; switch in alternate set.
  5345.         LD      BC,($5CB4)      ; fetch P-RAMT differs on 16K/48K machines.
  5346.         LD      DE,($5C38)      ; fetch RASP/PIP.
  5347.         LD      HL,($5C7B)      ; fetch UDG    differs on 16K/48K machines.
  5348.         EXX                     ; switch back to main set and continue into...
  5349.  
  5350. ; ---------------------------
  5351. ; Main entry (initialization)
  5352. ; ---------------------------
  5353. ; This common code tests ram and sets it to zero re-initializing
  5354. ; all the non-zero system variables and channel information.
  5355. ; The A register tells if coming from START or NEW
  5356.  
  5357. ;; START-NEW
  5358. L11CB:  LD      B,A             ; save the flag for later branching.
  5359.  
  5360.         LD      A,$07           ; select a white border
  5361.         OUT     ($FE),A         ; and set it now.
  5362.  
  5363.         LD      A,$3F           ; load accumulator with last page in ROM.
  5364.         LD      I,A             ; set the I register - this remains constant
  5365.                                 ; and can't be in range $40 - $7F as 'snow'
  5366.                                 ; appears on the screen.
  5367.         NOP                     ; these seem unnecessary.
  5368.         NOP                     ;
  5369.         NOP                     ;
  5370.         NOP                     ;
  5371.         NOP                     ;
  5372.         NOP                     ;
  5373.  
  5374. ; ------------
  5375. ; Check RAM
  5376. ; ------------
  5377. ; Typically a Spectrum will have 16K or 48K of Ram and this code will
  5378. ; test it all till it finds an unpopulated location or, less likely, a
  5379. ; faulty location. Usually it stops when it reaches the top $FFFF or
  5380. ; in the case of NEW the supplied top value. The entire screen turns
  5381. ; black with sometimes red stripes on black paper visible.
  5382.  
  5383. ;; ram-check
  5384. L11DA:  LD      H,D             ; transfer the top value to
  5385.         LD      L,E             ; the HL register pair.
  5386.  
  5387. ;; RAM-FILL
  5388. L11DC:  LD      (HL),$02        ; load with 2 - red ink on black paper
  5389.         DEC     HL              ; next lower
  5390.         CP      H               ; have we reached ROM - $3F ?
  5391.         JR      NZ,L11DC        ; back to RAM-FILL if not.
  5392.  
  5393. ;; RAM-READ
  5394. L11E2:  AND     A               ; clear carry - prepare to subtract
  5395.         SBC     HL,DE           ; subtract and add back setting
  5396.         ADD     HL,DE           ; carry when back at start.
  5397.         INC     HL              ; and increment for next iteration.
  5398.         JR      NC,L11EF        ; forward to RAM-DONE if we've got back to
  5399.                                 ; starting point with no errors.
  5400.  
  5401.         DEC     (HL)            ; decrement to 1.
  5402.         JR      Z,L11EF         ; forward to RAM-DONE if faulty.
  5403.  
  5404.         DEC     (HL)            ; decrement to zero.
  5405.         JR      Z,L11E2         ; back to RAM-READ if zero flag was set.
  5406.  
  5407. ;; RAM-DONE
  5408. L11EF:  DEC     HL              ; step back to last valid location.
  5409.         EXX                     ; regardless of state, set up possibly
  5410.                                 ; stored system variables in case from NEW.
  5411.         LD      ($5CB4),BC      ; insert P-RAMT.
  5412.         LD      ($5C38),DE      ; insert RASP/PIP.
  5413.         LD      ($5C7B),HL      ; insert UDG.
  5414.         EXX                     ; switch in main set.
  5415.         INC     B               ; now test if we arrived here from NEW.
  5416.         JR      Z,L1219         ; forward to RAM-SET if we did.
  5417.  
  5418. ; this section applies to START only.
  5419.  
  5420.         LD      ($5CB4),HL      ; set P-RAMT to the highest working RAM
  5421.                                 ; address.
  5422.         LD      DE,$3EAF        ; address of last byte of 'U' bitmap in ROM.
  5423.         LD      BC,$00A8        ; there are 21 user defined graphics.
  5424.         EX      DE,HL           ; switch pointers and make the UDGs a
  5425.         LDDR                    ; copy of the standard characters A - U.
  5426.         EX      DE,HL           ; switch the pointer to HL.
  5427.         INC     HL              ; update to start of 'A' in RAM.
  5428.         LD      ($5C7B),HL      ; make UDG system variable address the first
  5429.                                 ; bitmap.
  5430.         DEC     HL              ; point at RAMTOP again.
  5431.  
  5432.         LD      BC,$0040        ; set the values of
  5433.         LD      ($5C38),BC      ; the PIP and RASP system variables.
  5434.  
  5435. ; the NEW command path rejoins here.
  5436.  
  5437. ;; RAM-SET
  5438. L1219:  LD      ($5CB2),HL      ; set system variable RAMTOP to HL.
  5439.  
  5440.                 LD HL,CHARS-0X100       ;$3C00        ; a strange place to set the pointer to the
  5441.         LD      ($5C36),HL      ; character set, CHARS - as no printing yet.
  5442.  
  5443.         LD      HL,($5CB2)      ; fetch RAMTOP to HL again as we've lost it.
  5444.  
  5445.         LD      (HL),$3E        ; top of user ram holds GOSUB end marker
  5446.                                 ; an impossible line number - see RETURN.
  5447.                                 ; no significance in the number $3E. It has
  5448.                                 ; been traditional since the ZX80.
  5449.  
  5450.         DEC     HL              ; followed by empty byte (not important).
  5451.         LD      SP,HL           ; set up the machine stack pointer.
  5452.         DEC     HL              ;
  5453.         DEC     HL              ;
  5454.         LD      ($5C3D),HL      ; ERR_SP is where the error pointer is
  5455.                                 ; at moment empty - will take address MAIN-4
  5456.                                 ; at the call preceding that address,
  5457.                                 ; although interrupts and calls will make use
  5458.                                 ; of this location in meantime.
  5459.  
  5460.         IM      1               ; select interrupt mode 1.
  5461.         LD      IY,$5C3A        ; set IY to ERR_NR. IY can reach all standard
  5462.                                 ; system variables but shadow ROM system
  5463.                                 ; variables will be mostly out of range.
  5464.  
  5465.         EI                      ; enable interrupts now that we have a stack.
  5466.  
  5467.         LD      HL,$5CB6        ; the address of the channels - initially
  5468.                                 ; following system variables.
  5469.         LD      ($5C4F),HL      ; set the CHANS system variable.
  5470.  
  5471.         LD      DE,L15AF        ; address: init-chan in ROM.
  5472.         LD      BC,$0015        ; there are 21 bytes of initial data in ROM.
  5473.         EX      DE,HL           ; swap the pointers.
  5474.         LDIR                    ; copy the bytes to RAM.
  5475.  
  5476.         EX      DE,HL           ; swap pointers. HL points to program area.
  5477.         DEC     HL              ; decrement address.
  5478.         LD      ($5C57),HL      ; set DATADD to location before program area.
  5479.         INC     HL              ; increment again.
  5480.  
  5481.         LD      ($5C53),HL      ; set PROG the location where BASIC starts.
  5482.         LD      ($5C4B),HL      ; set VARS to same location with a
  5483.         LD      (HL),$80        ; variables end-marker.
  5484.         INC     HL              ; advance address.
  5485.         LD      ($5C59),HL      ; set E_LINE, where the edit line
  5486.                                 ; will be created.
  5487.                                 ; Note. it is not strictly necessary to
  5488.                                 ; execute the next fifteen bytes of code
  5489.                                 ; as this will be done by the call to SET-MIN.
  5490.                                 ; --
  5491.         LD      (HL),$0D        ; initially just has a carriage return
  5492.         INC     HL              ; followed by
  5493.         LD      (HL),$80        ; an end-marker.
  5494.         INC     HL              ; address the next location.
  5495.         LD      ($5C61),HL      ; set WORKSP - empty workspace.
  5496.         LD      ($5C63),HL      ; set STKBOT - bottom of the empty stack.
  5497.         LD      ($5C65),HL      ; set STKEND to the end of the empty stack.
  5498.                                 ; --
  5499.         LD      A,$38           ; the colour system is set to white paper,
  5500.                                 ; black ink, no flash or bright.
  5501.         LD      ($5C8D),A       ; set ATTR_P permanent colour attributes.
  5502.         LD      ($5C8F),A       ; set ATTR_T temporary colour attributes.
  5503.         LD      ($5C48),A       ; set BORDCR the border colour/lower screen
  5504.                                 ; attributes.
  5505.  
  5506.         LD      HL,$0523        ; The keyboard repeat and delay values
  5507.         LD      ($5C09),HL      ; are loaded to REPDEL and REPPER.
  5508.  
  5509.         DEC     (IY-$3A)        ; set KSTATE-0 to $FF.
  5510.         DEC     (IY-$36)        ; set KSTATE-4 to $FF.
  5511.                                 ; thereby marking both available.
  5512.  
  5513.         LD      HL,L15C6        ; set source to ROM Address: init-strm
  5514.         LD      DE,$5C10        ; set destination to system variable STRMS-FD
  5515.         LD      BC,$000E        ; copy the 14 bytes of initial 7 streams data
  5516.         LDIR                    ; from ROM to RAM.
  5517.  
  5518.         SET     1,(IY+$01)      ; update FLAGS  - signal printer in use.
  5519.  
  5520. ;===============================
  5521.                 CALL PRINTER_INITER
  5522. ;               CALL L0EDF                      ; call routine CLEAR-PRB to initialize system
  5523.                                                 ; variables associated with printer.
  5524. ;===============================
  5525.  
  5526.         LD      (IY+$31),$02    ; set DF_SZ the lower screen display size to
  5527.                                 ; two lines
  5528.         CALL    L0D6B           ; call routine CLS to set up system
  5529.                                 ; variables associated with screen and clear
  5530.                                 ; the screen and set attributes.
  5531.         XOR     A               ; clear accumulator so that we can address
  5532.         LD      DE,L1539 - 1    ; the message table directly.
  5533.         CALL    L0C0A           ; routine PO-MSG puts
  5534.                                 ; '(c) 1982 Sinclair Research Ltd'
  5535.                                 ; at bottom of display.
  5536.         SET     5,(IY+$02)      ; update TV_FLAG  - signal lower screen will
  5537.                                 ; require clearing.
  5538.  
  5539.         JR      L12A9           ; forward to MAIN-1
  5540.  
  5541. ; -------------------
  5542. ; Main execution loop
  5543. ; -------------------
  5544. ;
  5545. ;
  5546.  
  5547. ;; MAIN-EXEC
  5548. L12A2:  LD      (IY+$31),$02    ; set DF_SZ lower screen display file
  5549.                                 ; size to 2 lines.
  5550.         CALL    L1795           ; routine AUTO-LIST
  5551.  
  5552. ;; MAIN-1
  5553. L12A9:  CALL    L16B0           ; routine SET-MIN clears work areas.
  5554.  
  5555. ;; MAIN-2
  5556. L12AC:  LD      A,$00           ; select channel 'K' the keyboard
  5557.         CALL    L1601           ; routine CHAN-OPEN opens it
  5558.         CALL    L0F2C           ; routine EDITOR is called.
  5559.                                 ; Note the above routine is where the Spectrum
  5560.                                 ; waits for user-interaction. Perhaps the
  5561.                                 ; most common input at this stage
  5562.                                 ; is LOAD "".
  5563.         CALL    L1B17           ; routine LINE-SCAN scans the input.
  5564.         BIT     7,(IY+$00)      ; test ERR_NR - will be $FF if syntax
  5565.                                 ; is correct.
  5566.         JR      NZ,L12CF        ; forward, if correct, to MAIN-3.
  5567.  
  5568. ;
  5569.  
  5570.         BIT     4,(IY+$30)      ; test FLAGS2 - K channel in use ?
  5571.         JR      Z,L1303         ; forward to MAIN-4 if not.
  5572.  
  5573. ;
  5574.  
  5575.         LD      HL,($5C59)      ; an editing error so address E_LINE.
  5576.         CALL    L11A7           ; routine REMOVE-FP removes the hidden
  5577.                                 ; floating-point forms.
  5578.         LD      (IY+$00),$FF    ; system variable ERR_NR is reset to 'OK'.
  5579.         JR      L12AC           ; back to MAIN-2 to allow user to correct.
  5580.  
  5581. ; ---
  5582.  
  5583. ; the branch was here if syntax has passed test.
  5584.  
  5585. ;; MAIN-3
  5586. L12CF:  LD      HL,($5C59)      ; fetch the edit line address from E_LINE.
  5587.         LD      ($5C5D),HL      ; system variable CH_ADD is set to first
  5588.                                 ; character of edit line.
  5589.                                 ; Note. the above two instructions are a little
  5590.                                 ; inadequate.
  5591.                                 ; They are repeated with a subtle difference
  5592.                                 ; at the start of the next subroutine and are
  5593.                                 ; therefore not required above.
  5594.  
  5595.         CALL    L19FB           ; routine E-LINE-NO will fetch any line
  5596.                                 ; number to BC if this is a program line.
  5597.  
  5598.         LD      A,B             ; test if the number of
  5599.         OR      C               ; the line is non-zero.
  5600.         JP      NZ,L155D        ; jump forward to MAIN-ADD if so to add the
  5601.                                 ; line to the BASIC program.
  5602.  
  5603. ; Has the user just pressed the ENTER key ?
  5604.  
  5605.         RST     18H             ; GET-CHAR gets character addressed by CH_ADD.
  5606.         CP      $0D             ; is it a carriage return ?
  5607.         JR      Z,L12A2         ; back to MAIN-EXEC if so for an automatic
  5608.                                 ; listing.
  5609.  
  5610. ; this must be a direct command.
  5611.  
  5612.         BIT     0,(IY+$30)      ; test FLAGS2 - clear the main screen ?
  5613.         CALL    NZ,L0DAF        ; routine CL-ALL, if so, e.g. after listing.
  5614.         CALL    L0D6E           ; routine CLS-LOWER anyway.
  5615.         LD      A,$19           ; compute scroll count to 25 minus
  5616.         SUB     (IY+$4F)        ; value of S_POSN_hi.
  5617.         LD      ($5C8C),A       ; update SCR_CT system variable.
  5618.         SET     7,(IY+$01)      ; update FLAGS - signal running program.
  5619.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK'.
  5620.         LD      (IY+$0A),$01    ; set NSPPC to one for first statement.
  5621.         CALL    L1B8A           ; call routine LINE-RUN to run the line.
  5622.                                 ; sysvar ERR_SP therefore addresses MAIN-4
  5623.  
  5624. ; Examples of direct commands are RUN, CLS, LOAD "", PRINT USR 40000,
  5625. ; LPRINT "A"; etc..
  5626. ; If a user written machine-code program disables interrupts then it
  5627. ; must enable them to pass the next step. We also jumped to here if the
  5628. ; keyboard was not being used.
  5629.  
  5630. ;; MAIN-4
  5631. L1303:  HALT                    ; wait for interrupt.
  5632.  
  5633.         RES     5,(IY+$01)      ; update FLAGS - signal no new key.
  5634.         BIT     1,(IY+$30)      ; test FLAGS2 - is printer buffer clear ?
  5635.         CALL    NZ,L0ECD        ; call routine COPY-BUFF if not.
  5636.                                 ; Note. the programmer has neglected
  5637.                                 ; to set bit 1 of FLAGS first.
  5638.  
  5639.         LD      A,($5C3A)       ; fetch ERR_NR
  5640.         INC     A               ; increment to give true code.
  5641.  
  5642. ; Now deal with a runtime error as opposed to an editing error.
  5643. ; However if the error code is now zero then the OK message will be printed.
  5644.  
  5645. ;; MAIN-G
  5646. L1313:  PUSH    AF              ; save the error number.
  5647.  
  5648.         LD      HL,$0000        ; prepare to clear some system variables.
  5649.         LD      (IY+$37),H      ; clear all the bits of FLAGX.
  5650.         LD      (IY+$26),H      ; blank X_PTR_hi to suppress error marker.
  5651.         LD      ($5C0B),HL      ; blank DEFADD to signal that no defined
  5652.                                 ; function is currently being evaluated.
  5653.  
  5654.         LD      HL,$0001        ; explicit - inc hl would do.
  5655.         LD      ($5C16),HL      ; ensure STRMS-00 is keyboard.
  5656.  
  5657.         CALL    L16B0           ; routine SET-MIN clears workspace etc.
  5658.         RES     5,(IY+$37)      ; update FLAGX - signal in EDIT not INPUT mode.
  5659.                                 ; Note. all the bits were reset earlier.
  5660.  
  5661.         CALL    L0D6E           ; call routine CLS-LOWER.
  5662.         SET     5,(IY+$02)      ; update TV_FLAG - signal lower screen
  5663.                                 ; requires clearing.
  5664.  
  5665.         POP     AF              ; bring back the error number
  5666.         LD      B,A             ; and make a copy in B.
  5667.         CP      $0A             ; is it a print-ready digit ?
  5668.         JR      C,L133C         ; forward to MAIN-5 if so.
  5669.  
  5670.         ADD     A,$07           ; add ASCII offset to letters.
  5671.  
  5672. ;; MAIN-5
  5673. L133C:  CALL    L15EF           ; call routine OUT-CODE to print the code.
  5674.  
  5675.         LD      A,$20           ; followed by a space.
  5676.         RST     10H             ; PRINT-A
  5677.  
  5678.         LD      A,B             ; fetch stored report code.
  5679.         LD      DE,L1391        ; address: rpt-mesgs.
  5680.         CALL    L0C0A           ; call routine PO-MSG to print.
  5681.  
  5682. X1349
  5683.                 IF BAS48_ONLY=1
  5684.                 XOR A
  5685.                 LD DE,L1536
  5686.                 ELSE
  5687.                 CALL L3B3B              ; Spectrum 128 patch
  5688.                 NOP
  5689.                 ENDIF
  5690.  
  5691. L134D:  CALL    L0C0A           ; routine PO-MSG prints them although it would
  5692.                                 ; be more succinct to use RST $10.
  5693.  
  5694.         LD      BC,($5C45)      ; fetch PPC the current line number.
  5695.         CALL    L1A1B           ; routine OUT-NUM-1 will print that
  5696.         LD      A,$3A           ; then a ':'.
  5697.         RST     10H             ; PRINT-A
  5698.  
  5699.         LD      C,(IY+$0D)      ; then SUBPPC for statement
  5700.         LD      B,$00           ; limited to 127
  5701.         CALL    L1A1B           ; routine OUT-NUM-1
  5702.  
  5703.         CALL    L1097           ; routine CLEAR-SP clears editing area.
  5704.                                 ; which probably contained 'RUN'.
  5705.         LD      A,($5C3A)       ; fetch ERR_NR again
  5706.         INC     A               ; test for no error originally $FF.
  5707.         JR      Z,L1386         ; forward to MAIN-9 if no error.
  5708.  
  5709.         CP      $09             ; is code Report 9 STOP ?
  5710.         JR      Z,L1373         ; forward to MAIN-6 if so
  5711.  
  5712.         CP      $15             ; is code Report L Break ?
  5713.         JR      NZ,L1376        ; forward to MAIN-7 if not
  5714.  
  5715. ; Stop or Break was encountered so consider CONTINUE.
  5716.  
  5717. ;; MAIN-6
  5718. L1373:  INC     (IY+$0D)        ; increment SUBPPC to next statement.
  5719.  
  5720. ;; MAIN-7
  5721. L1376:  LD      BC,$0003        ; prepare to copy 3 system variables to
  5722.         LD      DE,$5C70        ; address OSPPC - statement for CONTINUE.
  5723.                                 ; also updating OLDPPC line number below.
  5724.  
  5725.         LD      HL,$5C44        ; set source top to NSPPC next statement.
  5726.         BIT     7,(HL)          ; did BREAK occur before the jump ?
  5727.                                 ; e.g. between GO TO and next statement.
  5728.         JR      Z,L1384         ; skip forward to MAIN-8, if not, as setup
  5729.                                 ; is correct.
  5730.  
  5731.         ADD     HL,BC           ; set source to SUBPPC number of current
  5732.                                 ; statement/line which will be repeated.
  5733.  
  5734. ;; MAIN-8
  5735. L1384:  LDDR                    ; copy PPC to OLDPPC and SUBPPC to OSPCC
  5736.                                 ; or NSPPC to OLDPPC and NEWPPC to OSPCC
  5737.  
  5738. ;; MAIN-9
  5739. L1386:  LD      (IY+$0A),$FF    ; update NSPPC - signal 'no jump'.
  5740.         RES     3,(IY+$01)      ; update FLAGS  - signal use 'K' mode for
  5741.                                 ; the first character in the editor and
  5742.         JP      L12AC           ; jump back to MAIN-2.
  5743.  
  5744.  
  5745. ; ----------------------
  5746. ; Canned report messages
  5747. ; ----------------------
  5748. ; The Error reports with the last byte inverted. The first entry
  5749. ; is a dummy entry. The last, which begins with $7F, the Spectrum
  5750. ; character for copyright symbol, is placed here for convenience
  5751. ; as is the preceding comma and space.
  5752. ; The report line must accommodate a 4-digit line number and a 3-digit
  5753. ; statement number which limits the length of the message text to twenty
  5754. ; characters.
  5755. ; e.g.  "B Integer out of range, 1000:127"
  5756.  
  5757. ;; rpt-mesgs
  5758. L1391           DB $80
  5759.                 DC "OK"                         ;DB    'O','K'+$80              ; 0
  5760.                 DC "NEXT without FOR"           ;DEFM    "NEXT without FO"
  5761.                                                 ;DB    'R'+$80          ; 1
  5762.                 DC "Variable not found"         ;DEFM    "Variable not foun"
  5763.                                                 ;DB    'd'+$80          ; 2
  5764.                 DC "Subscript wrong"            ;DEFM    "Subscript wron"
  5765.                                                 ;DB    'g'+$80          ; 3
  5766.                 DC "Out of memory"              ;DEFM    "Out of memor"
  5767.                                                 ;DB    'y'+$80          ; 4
  5768.                 DC "Out of screen"              ;DEFM    "Out of scree"
  5769.                                                 ;DB    'n'+$80          ; 5
  5770.                 DC "Number too big"             ;DEFM    "Number too bi"
  5771.                                                 ;DB    'g'+$80          ; 6
  5772.                 DC "RETURN without GOSUB"       ;DEFM    "RETURN without GOSU"
  5773.                                                 ;DB    'B'+$80          ; 7
  5774.                 DC "End of file"                ;DEFM    "End of fil"
  5775.                                                 ;DB    'e'+$80          ; 8
  5776.                 DC "STOP statement"             ;DEFM    "STOP statemen"
  5777.                                                 ;DB    't'+$80          ; 9
  5778.                 DC "Invalid argument"           ;DEFM    "Invalid argumen"
  5779.                                                 ;DB    't'+$80          ; A
  5780.                 DC "Integer out of range"       ;DEFM    "Integer out of rang"
  5781.                                                 ;DB    'e'+$80          ; B
  5782.                 DC "Nonsense in BASIC"          ;DEFM    "Nonsense in BASI"
  5783.                                                 ;DB    'C'+$80          ; C
  5784.                 DC "BREAK - CONT repeats"       ;DEFM    "BREAK - CONT repeat"
  5785.                                                 ;DB    's'+$80          ; D
  5786.                 DC "Out of DATA"                ;DEFM    "Out of DAT"
  5787.                                                 ;DB    'A'+$80          ; E
  5788.                 DC "Invalid file name"          ;DEFM    "Invalid file nam"
  5789.                                                 ;DB    'e'+$80          ; F
  5790.                 DC "No room for line"           ;DEFM    "No room for lin"
  5791.                                                 ;DB    'e'+$80          ; G
  5792.                 DC "STOP in INPUT"              ;DEFM    "STOP in INPU"
  5793.                                                 ;DB    'T'+$80          ; H
  5794.                 DC "FOR without NEXT"           ;DEFM    "FOR without NEX"
  5795.                                                 ;DB    'T'+$80          ; I
  5796.                 DC "Invalid I/O device"         ;DEFM    "Invalid I/O devic"
  5797.                                                 ;DB    'e'+$80          ; J
  5798.                 DC "Invalid colour"             ;DEFM    "Invalid colou"
  5799.                                                 ;DB    'r'+$80          ; K
  5800.                 DC "BREAK into program"         ;DEFM    "BREAK into progra"
  5801.                                                 ;DB    'm'+$80          ; L
  5802.                 DC "RAMTOP no good"             ;DEFM    "RAMTOP no goo"
  5803.                                                 ;DB    'd'+$80          ; M
  5804.                 DC "Statement lost"             ;DEFM    "Statement los"
  5805.                                                 ;DB    't'+$80          ; N
  5806.                 DC "Invalid stream"             ;DEFM    "Invalid strea"
  5807.                                                 ;DB    'm'+$80          ; O
  5808.                 DC "FN without DEF"             ;DEFM    "FN without DE"
  5809.                                                 ;DB    'F'+$80          ; P
  5810.                 DC "Parameter error"            ;DEFM    "Parameter erro"
  5811.                                                 ;DB    'r'+$80          ; Q
  5812.                 DC "Tape loading error"         ;DEFM    "Tape loading erro"
  5813.                                                 ;DB    'r'+$80          ; R
  5814. L1536           EQU $-1
  5815. ;; comma-sp  
  5816. L1537           DC ", "                         ;DB    ',',' '+$80              ; used in report line.
  5817. ;; copyright
  5818. L1539           DB $7F                          ; copyright
  5819.                 DC " 1982 Sinclair Research Ltd"        ;DEFM    " 1982 Sinclair Research Lt"
  5820.                                                         ;DB    'd'+$80
  5821.  
  5822.  
  5823. ; -------------
  5824. ; REPORT-G
  5825. ; -------------
  5826. ; Note ERR_SP points here during line entry which allows the
  5827. ; normal 'Out of Memory' report to be augmented to the more
  5828. ; precise 'No Room for line' report.
  5829.  
  5830. ;; REPORT-G
  5831. ; No Room for line
  5832. L1555:  LD      A,$10           ; i.e. 'G' -$30 -$07
  5833.         LD      BC,$0000        ; this seems unnecessary.
  5834.         JP      L1313           ; jump back to MAIN-G
  5835.  
  5836. ; -----------------------------
  5837. ; Handle addition of BASIC line
  5838. ; -----------------------------
  5839. ; Note this is not a subroutine but a branch of the main execution loop.
  5840. ; System variable ERR_SP still points to editing error handler.
  5841. ; A new line is added to the BASIC program at the appropriate place.
  5842. ; An existing line with same number is deleted first.
  5843. ; Entering an existing line number deletes that line.
  5844. ; Entering a non-existent line allows the subsequent line to be edited next.
  5845.  
  5846. ;; MAIN-ADD
  5847. L155D:  LD      ($5C49),BC      ; set E_PPC to extracted line number.
  5848.         LD      HL,($5C5D)      ; fetch CH_ADD - points to location after the
  5849.                                 ; initial digits (set in E_LINE_NO).
  5850.         EX      DE,HL           ; save start of BASIC in DE.
  5851.  
  5852.         LD      HL,L1555        ; Address: REPORT-G
  5853.         PUSH    HL              ; is pushed on stack and addressed by ERR_SP.
  5854.                                 ; the only error that can occur is
  5855.                                 ; 'Out of memory'.
  5856.  
  5857.         LD      HL,($5C61)      ; fetch WORKSP - end of line.
  5858.         SCF                     ; prepare for true subtraction.
  5859.         SBC     HL,DE           ; find length of BASIC and
  5860.         PUSH    HL              ; save it on stack.
  5861.         LD      H,B             ; transfer line number
  5862.         LD      L,C             ; to HL register.
  5863.         CALL    L196E           ; routine LINE-ADDR will see if
  5864.                                 ; a line with the same number exists.
  5865.         JR      NZ,L157D        ; forward if no existing line to MAIN-ADD1.
  5866.  
  5867.         CALL    L19B8           ; routine NEXT-ONE finds the existing line.
  5868.         CALL    L19E8           ; routine RECLAIM-2 reclaims it.
  5869.  
  5870. ;; MAIN-ADD1
  5871. L157D:  POP     BC              ; retrieve the length of the new line.
  5872.         LD      A,C             ; and test if carriage return only
  5873.         DEC     A               ; i.e. one byte long.
  5874.         OR      B               ; result would be zero.
  5875.         JR      Z,L15AB         ; forward to MAIN-ADD2 is so.
  5876.  
  5877.         PUSH    BC              ; save the length again.
  5878.         INC     BC              ; adjust for inclusion
  5879.         INC     BC              ; of line number (two bytes)
  5880.         INC     BC              ; and line length
  5881.         INC     BC              ; (two bytes).
  5882.         DEC     HL              ; HL points to location before the destination
  5883.  
  5884.         LD      DE,($5C53)      ; fetch the address of PROG
  5885.         PUSH    DE              ; and save it on the stack
  5886.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces in
  5887.                                 ; program area and updates pointers.
  5888.         POP     HL              ; restore old program pointer.
  5889.         LD      ($5C53),HL      ; and put back in PROG as it may have been
  5890.                                 ; altered by the POINTERS routine.
  5891.  
  5892.         POP     BC              ; retrieve BASIC length
  5893.         PUSH    BC              ; and save again.
  5894.  
  5895.         INC     DE              ; points to end of new area.
  5896.         LD      HL,($5C61)      ; set HL to WORKSP - location after edit line.
  5897.         DEC     HL              ; decrement to address end marker.
  5898.         DEC     HL              ; decrement to address carriage return.
  5899.         LDDR                    ; copy the BASIC line back to initial command.
  5900.  
  5901.         LD      HL,($5C49)      ; fetch E_PPC - line number.
  5902.         EX      DE,HL           ; swap it to DE, HL points to last of
  5903.                                 ; four locations.
  5904.         POP     BC              ; retrieve length of line.
  5905.         LD      (HL),B          ; high byte last.
  5906.         DEC     HL              ;
  5907.         LD      (HL),C          ; then low byte of length.
  5908.         DEC     HL              ;
  5909.         LD      (HL),E          ; then low byte of line number.
  5910.         DEC     HL              ;
  5911.         LD      (HL),D          ; then high byte range $0 - $27 (1-9999).
  5912.  
  5913. ;; MAIN-ADD2
  5914. L15AB:  POP     AF              ; drop the address of Report G
  5915.         JP      L12A2           ; and back to MAIN-EXEC producing a listing
  5916.                                 ; and to reset ERR_SP in EDITOR.
  5917.  
  5918.  
  5919. ; ---------------------------
  5920. ; Initial channel information
  5921. ; ---------------------------
  5922. ; This initial channel information is copied from ROM to RAM,
  5923. ; during initialization. It's new location is after the system
  5924. ; variables and is addressed by the system variable CHANS
  5925. ; which means that it can slide up and down in memory.
  5926. ; The table is never searched and the last character which could be anything
  5927. ; other than a comma provides a convenient resting place for DATADD.
  5928.  
  5929. ;; init-chan
  5930. L15AF           DW L09F4                ; PRINT-OUT
  5931.                 DW L10A8                ; KEY-INPUT
  5932.                 DB "K"
  5933.                 DW L09F4                ; PRINT-OUT
  5934.                 DW L15C4                ; REPORT-J
  5935.                 DB "S"
  5936.                 DW L0F81                ; ADD-CHAR
  5937.                 DW L15C4                ; REPORT-J
  5938.                 DB "R"
  5939. ;=======================================
  5940.                 DW PRN_TOKEN
  5941. ;               DW L09F4                ; PRINT-OUT
  5942. ;=======================================
  5943.                 DW L15C4                ; REPORT-J
  5944.                 DB "P"
  5945.  
  5946.         DB    $80             ; End Marker
  5947.  
  5948. ;; REPORT-J
  5949. L15C4:  RST     08H             ; ERROR-1
  5950.         DB    $12             ; Error Report: Invalid I/O device
  5951.  
  5952.  
  5953. ; -------------------
  5954. ; Initial stream data
  5955. ; -------------------
  5956. ; This is the initial stream data for the seven streams $FD - $03 that is
  5957. ; copied from ROM to the STRMS system variables area during initialization.
  5958. ; There are reserved locations there for another 12 streams.
  5959. ; Each location contains an offset to the second byte of a channel.
  5960. ; The first byte of a channel can't be used as that would result in an
  5961. ; offset of zero for some and zero is used to denote that a stream is closed.
  5962.  
  5963. ;; init-strm
  5964. L15C6:  DB    $01, $00        ; stream $FD offset to channel 'K'
  5965.         DB    $06, $00        ; stream $FE offset to channel 'S'
  5966.         DB    $0B, $00        ; stream $FF offset to channel 'R'
  5967.  
  5968.         DB    $01, $00        ; stream $00 offset to channel 'K'
  5969.         DB    $01, $00        ; stream $01 offset to channel 'K'
  5970.         DB    $06, $00        ; stream $02 offset to channel 'S'
  5971.         DB    $10, $00        ; stream $03 offset to channel 'P'
  5972.  
  5973. ; ----------------------------
  5974. ; Control for input subroutine
  5975. ; ----------------------------
  5976. ;
  5977.  
  5978. ;; WAIT-KEY
  5979. L15D4:  BIT     5,(IY+$02)      ; test TV_FLAG - clear lower screen ?
  5980.         JR      NZ,L15DE        ; forward to WAIT-KEY1 if so.
  5981.  
  5982.         SET     3,(IY+$02)      ; update TV_FLAG - signal reprint the edit
  5983.                                 ; line to the lower screen.
  5984.  
  5985. ;; WAIT-KEY1
  5986. L15DE:  CALL    L15E6           ; routine INPUT-AD is called.
  5987.         RET     C               ; return with acceptable keys.
  5988.  
  5989.         JR      Z,L15DE         ; back to WAIT-KEY1 if no key is pressed
  5990.                                 ; or it has been handled within INPUT-AD.
  5991.  
  5992. ; Note. When inputting from the keyboard all characters are returned with
  5993. ; above conditions so this path is never taken.
  5994.  
  5995. ;; REPORT-8
  5996. L15E4:  RST     08H             ; ERROR-1
  5997.         DB    $07             ; Error Report: End of file
  5998.  
  5999. ; ------------------------------
  6000. ; Make HL point to input address
  6001. ; ------------------------------
  6002. ; This routine fetches the address of the input stream from the current
  6003. ; channel area using system variable CURCHL.
  6004.  
  6005. ;; INPUT-AD
  6006. L15E6:  EXX                     ; switch in alternate set.
  6007.         PUSH    HL              ; save HL register
  6008.         LD      HL,($5C51)      ; fetch address of CURCHL - current channel.
  6009.         INC     HL              ; step over output routine
  6010.         INC     HL              ; to point to low byte of input routine.
  6011.         JR      L15F7           ; forward to CALL-SUB.
  6012.  
  6013. ; -------------------
  6014. ; Main Output Routine
  6015. ; -------------------
  6016. ; The entry point OUT-CODE is called on five occasions to print
  6017. ; the ASCII equivalent of a value 0-9.
  6018. ;
  6019. ; PRINT-A-2 is a continuation of the RST 10 to print any character.
  6020. ; Both print to the current channel and the printing of control codes
  6021. ; may alter that channel to divert subsequent RST 10 instructions
  6022. ; to temporary routines. The normal channel is $09F4.
  6023.  
  6024. ;; OUT-CODE
  6025. L15EF:  LD      E,$30           ; add 48 decimal to give ASCII
  6026.         ADD     A,E             ; character '0' to '9'.
  6027.  
  6028. ;; PRINT-A-2
  6029. L15F2:  EXX                     ; switch in alternate set
  6030.         PUSH    HL              ; save HL register
  6031.         LD      HL,($5C51)      ; fetch CURCHL the current channel.
  6032.  
  6033. ; input-ad rejoins here also.
  6034.  
  6035. ;; CALL-SUB
  6036. L15F7:  LD      E,(HL)          ; put the low byte in E.
  6037.         INC     HL              ; advance address.
  6038.         LD      D,(HL)          ; put the high byte to D.
  6039.         EX      DE,HL           ; transfer the stream to HL.
  6040.         CALL    L162C           ; use routine CALL-JUMP.
  6041.                                 ; in effect CALL (HL).
  6042.  
  6043.         POP     HL              ; restore saved HL register.
  6044.         EXX                     ; switch back to the main set and
  6045.         RET                     ; return.
  6046.  
  6047. ; ------------
  6048. ; Open channel
  6049. ; ------------
  6050. ; This subroutine is used by the ROM to open a channel 'K', 'S', 'R' or 'P'.
  6051. ; This is either for its own use or in response to a user's request, for
  6052. ; example, when '#' is encountered with output - PRINT, LIST etc.
  6053. ; or with input - INPUT, INKEY$ etc.
  6054. ; it is entered with a system stream $FD - $FF, or a user stream $00 - $0F
  6055. ; in the accumulator.
  6056.  
  6057. ;; CHAN-OPEN
  6058. L1601:  ADD     A,A             ; double the stream ($FF will become $FE etc.)
  6059.         ADD     A,$16           ; add the offset to stream 0 from $5C00
  6060.         LD      L,A             ; result to L
  6061.         LD      H,$5C           ; now form the address in STRMS area.
  6062.         LD      E,(HL)          ; fetch low byte of CHANS offset
  6063.         INC     HL              ; address next
  6064.         LD      D,(HL)          ; fetch high byte of offset
  6065.         LD      A,D             ; test that the stream is open.
  6066.         OR      E               ; zero if closed.
  6067.         JR      NZ,L1610        ; forward to CHAN-OP-1 if open.
  6068.  
  6069. ;; REPORT-Oa
  6070. L160E:  RST     08H             ; ERROR-1
  6071.         DB    $17             ; Error Report: Invalid stream
  6072.  
  6073. ; continue here if stream was open. Note that the offset is from CHANS
  6074. ; to the second byte of the channel.
  6075.  
  6076. ;; CHAN-OP-1
  6077. L1610:  DEC     DE              ; reduce offset so it points to the channel.
  6078.         LD      HL,($5C4F)      ; fetch CHANS the location of the base of
  6079.                                 ; the channel information area
  6080.         ADD     HL,DE           ; and add the offset to address the channel.
  6081.                                 ; and continue to set flags.
  6082.  
  6083. ; -----------------
  6084. ; Set channel flags
  6085. ; -----------------
  6086. ; This subroutine is used from ED-EDIT, str$ and read-in to reset the
  6087. ; current channel when it has been temporarily altered.
  6088.  
  6089. ;; CHAN-FLAG
  6090. L1615:  LD      ($5C51),HL      ; set CURCHL system variable to the
  6091.                                 ; address in HL
  6092.         RES     4,(IY+$30)      ; update FLAGS2  - signal K channel not in use.
  6093.                                 ; Note. provide a default for channel 'R'.
  6094.         INC     HL              ; advance past
  6095.         INC     HL              ; output routine.
  6096.         INC     HL              ; advance past
  6097.         INC     HL              ; input routine.
  6098.         LD      C,(HL)          ; pick up the letter.
  6099.         LD      HL,L162D        ; address: chn-cd-lu
  6100.         CALL    L16DC           ; routine INDEXER finds offset to a
  6101.                                 ; flag-setting routine.
  6102.  
  6103.         RET     NC              ; but if the letter wasn't found in the
  6104.                                 ; table just return now. - channel 'R'.
  6105.  
  6106.         LD      D,$00           ; prepare to add
  6107.         LD      E,(HL)          ; offset to E
  6108.         ADD     HL,DE           ; add offset to location of offset to form
  6109.                                 ; address of routine
  6110.  
  6111. ;; CALL-JUMP
  6112. L162C:  JP      (HL)            ; jump to the routine
  6113.  
  6114. ; Footnote. calling any location that holds JP (HL) is the equivalent to
  6115. ; a pseudo Z80 instruction CALL (HL). The ROM uses the instruction above.
  6116.  
  6117. ; --------------------------
  6118. ; Channel code look-up table
  6119. ; --------------------------
  6120. ; This table is used by the routine above to find one of the three
  6121. ; flag setting routines below it.
  6122. ; A zero end-marker is required as channel 'R' is not present.
  6123.  
  6124. ;; chn-cd-lu
  6125. L162D:  DB    'K', L1634-$-1  ; offset $06 to CHAN-K
  6126.         DB    'S', L1642-$-1  ; offset $12 to CHAN-S
  6127.         DB    'P', L164D-$-1  ; offset $1B to CHAN-P
  6128.  
  6129.         DB    $00             ; end marker.
  6130.  
  6131. ; --------------
  6132. ; Channel K flag
  6133. ; --------------
  6134. ; routine to set flags for lower screen/keyboard channel.
  6135.  
  6136. ;; CHAN-K
  6137. L1634:  SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6138.         RES     5,(IY+$01)      ; update FLAGS    - signal no new key
  6139.         SET     4,(IY+$30)      ; update FLAGS2   - signal K channel in use
  6140.         JR      L1646           ; forward to CHAN-S-1 for indirect exit
  6141.  
  6142. ; --------------
  6143. ; Channel S flag
  6144. ; --------------
  6145. ; routine to set flags for upper screen channel.
  6146.  
  6147. ;; CHAN-S
  6148. L1642:  RES     0,(IY+$02)      ; TV_FLAG  - signal main screen in use
  6149.  
  6150. ;; CHAN-S-1
  6151. L1646:  RES     1,(IY+$01)      ; update FLAGS  - signal printer not in use
  6152.         JP      L0D4D           ; jump back to TEMPS and exit via that
  6153.                                 ; routine after setting temporary attributes.
  6154. ; --------------
  6155. ; Channel P flag
  6156. ; --------------
  6157. ; This routine sets a flag so that subsequent print related commands
  6158. ; print to printer or update the relevant system variables.
  6159. ; This status remains in force until reset by the routine above.
  6160.  
  6161. ;; CHAN-P
  6162. L164D:  SET     1,(IY+$01)      ; update FLAGS  - signal printer in use
  6163.         RET                     ; return
  6164.  
  6165. ; -----------------------
  6166. ; Just one space required
  6167. ; -----------------------
  6168. ; This routine is called once only to create a single space
  6169. ; in workspace by ADD-CHAR. It is slightly quicker than using a RST $30.
  6170. ; There are several instances in the calculator where the sequence
  6171. ; ld bc, 1; rst $30 could be replaced by a call to this routine but it
  6172. ; only gives a saving of one byte each time.
  6173.  
  6174. ;; ONE-SPACE
  6175. L1652:  LD      BC,$0001        ; create space for a single character.
  6176.  
  6177. ; ---------
  6178. ; Make Room
  6179. ; ---------
  6180. ; This entry point is used to create BC spaces in various areas such as
  6181. ; program area, variables area, workspace etc..
  6182. ; The entire free RAM is available to each BASIC statement.
  6183. ; On entry, HL addresses where the first location is to be created.
  6184. ; Afterwards, HL will point to the location before this.
  6185.  
  6186. ;; MAKE-ROOM
  6187. L1655:  PUSH    HL              ; save the address pointer.
  6188.         CALL    L1F05           ; routine TEST-ROOM checks if room
  6189.                                 ; exists and generates an error if not.
  6190.         POP     HL              ; restore the address pointer.
  6191.         CALL    L1664           ; routine POINTERS updates the
  6192.                                 ; dynamic memory location pointers.
  6193.                                 ; DE now holds the old value of STKEND.
  6194.         LD      HL,($5C65)      ; fetch new STKEND the top destination.
  6195.  
  6196.         EX      DE,HL           ; HL now addresses the top of the area to
  6197.                                 ; be moved up - old STKEND.
  6198.         LDDR                    ; the program, variables, etc are moved up.
  6199.         RET                     ; return with new area ready to be populated.
  6200.                                 ; HL points to location before new area,
  6201.                                 ; and DE to last of new locations.
  6202.  
  6203. ; -----------------------------------------------
  6204. ; Adjust pointers before making or reclaiming room
  6205. ; -----------------------------------------------
  6206. ; This routine is called by MAKE-ROOM to adjust upwards and by RECLAIM to
  6207. ; adjust downwards the pointers within dynamic memory.
  6208. ; The fourteen pointers to dynamic memory, starting with VARS and ending
  6209. ; with STKEND, are updated adding BC if they are higher than the position
  6210. ; in HL.  
  6211. ; The system variables are in no particular order except that STKEND, the first
  6212. ; free location after dynamic memory must be the last encountered.
  6213.  
  6214. ;; POINTERS
  6215. L1664:  PUSH    AF              ; preserve accumulator.
  6216.         PUSH    HL              ; put pos pointer on stack.
  6217.         LD      HL,$5C4B        ; address VARS the first of the
  6218.         LD      A,$0E           ; fourteen variables to consider.
  6219.  
  6220. ;; PTR-NEXT
  6221. L166B:  LD      E,(HL)          ; fetch the low byte of the system variable.
  6222.         INC     HL              ; advance address.
  6223.         LD      D,(HL)          ; fetch high byte of the system variable.
  6224.         EX      (SP),HL         ; swap pointer on stack with the variable
  6225.                                 ; pointer.
  6226.         AND     A               ; prepare to subtract.
  6227.         SBC     HL,DE           ; subtract variable address
  6228.         ADD     HL,DE           ; and add back
  6229.         EX      (SP),HL         ; swap pos with system variable pointer
  6230.         JR      NC,L167F        ; forward to PTR-DONE if var before pos
  6231.  
  6232.         PUSH    DE              ; save system variable address.
  6233.         EX      DE,HL           ; transfer to HL
  6234.         ADD     HL,BC           ; add the offset
  6235.         EX      DE,HL           ; back to DE
  6236.         LD      (HL),D          ; load high byte
  6237.         DEC     HL              ; move back
  6238.         LD      (HL),E          ; load low byte
  6239.         INC     HL              ; advance to high byte
  6240.         POP     DE              ; restore old system variable address.
  6241.  
  6242. ;; PTR-DONE
  6243. L167F:  INC     HL              ; address next system variable.
  6244.         DEC     A               ; decrease counter.
  6245.         JR      NZ,L166B        ; back to PTR-NEXT if more.
  6246.         EX      DE,HL           ; transfer old value of STKEND to HL.
  6247.                                 ; Note. this has always been updated.
  6248.         POP     DE              ; pop the address of the position.
  6249.  
  6250.         POP     AF              ; pop preserved accumulator.
  6251.         AND     A               ; clear carry flag preparing to subtract.
  6252.  
  6253.         SBC     HL,DE           ; subtract position from old stkend
  6254.         LD      B,H             ; to give number of data bytes
  6255.         LD      C,L             ; to be moved.
  6256.         INC     BC              ; increment as we also copy byte at old STKEND.
  6257.         ADD     HL,DE           ; recompute old stkend.
  6258.         EX      DE,HL           ; transfer to DE.
  6259.         RET                     ; return.
  6260.  
  6261.  
  6262.  
  6263. ; -------------------
  6264. ; Collect line number
  6265. ; -------------------
  6266. ; This routine extracts a line number, at an address that has previously
  6267. ; been found using LINE-ADDR, and it is entered at LINE-NO. If it encounters
  6268. ; the program 'end-marker' then the previous line is used and if that
  6269. ; should also be unacceptable then zero is used as it must be a direct
  6270. ; command. The program end-marker is the variables end-marker $80, or
  6271. ; if variables exist, then the first character of any variable name.
  6272.  
  6273. ;; LINE-ZERO
  6274. L168F:  DB    $00, $00        ; dummy line number used for direct commands
  6275.  
  6276.  
  6277. ;; LINE-NO-A
  6278. L1691:  EX      DE,HL           ; fetch the previous line to HL and set
  6279.         LD      DE,$168F        ; DE to LINE-ZERO should HL also fail.
  6280.  
  6281. ; -> The Entry Point.
  6282.  
  6283. ;; LINE-NO
  6284. L1695:  LD      A,(HL)          ; fetch the high byte - max $2F
  6285.         AND     $C0             ; mask off the invalid bits.
  6286.         JR      NZ,L1691        ; to LINE-NO-A if an end-marker.
  6287.  
  6288.         LD      D,(HL)          ; reload the high byte.
  6289.         INC     HL              ; advance address.
  6290.         LD      E,(HL)          ; pick up the low byte.
  6291.         RET                     ; return from here.
  6292.  
  6293. ; -------------------
  6294. ; Handle reserve room
  6295. ; -------------------
  6296. ; This is a continuation of the restart BC-SPACES
  6297.  
  6298. ;; RESERVE
  6299. L169E:  LD      HL,($5C63)      ; STKBOT first location of calculator stack
  6300.         DEC     HL              ; make one less than new location
  6301.         CALL    L1655           ; routine MAKE-ROOM creates the room.
  6302.         INC     HL              ; address the first new location
  6303.         INC     HL              ; advance to second
  6304.         POP     BC              ; restore old WORKSP
  6305.         LD      ($5C61),BC      ; system variable WORKSP was perhaps
  6306.                                 ; changed by POINTERS routine.
  6307.         POP     BC              ; restore count for return value.
  6308.         EX      DE,HL           ; switch. DE = location after first new space
  6309.         INC     HL              ; HL now location after new space
  6310.         RET                     ; return.
  6311.  
  6312. ; ---------------------------
  6313. ; Clear various editing areas
  6314. ; ---------------------------
  6315. ; This routine sets the editing area, workspace and calculator stack
  6316. ; to their minimum configurations as at initialization and indeed this
  6317. ; routine could have been relied on to perform that task.
  6318. ; This routine uses HL only and returns with that register holding
  6319. ; WORKSP/STKBOT/STKEND though no use is made of this. The routines also
  6320. ; reset MEM to its usual place in the systems variable area should it
  6321. ; have been relocated to a FOR-NEXT variable. The main entry point
  6322. ; SET-MIN is called at the start of the MAIN-EXEC loop and prior to
  6323. ; displaying an error.
  6324.  
  6325. ;; SET-MIN
  6326. L16B0:  LD      HL,($5C59)      ; fetch E_LINE
  6327.         LD      (HL),$0D        ; insert carriage return
  6328.         LD      ($5C5B),HL      ; make K_CUR keyboard cursor point there.
  6329.         INC     HL              ; next location
  6330.         LD      (HL),$80        ; holds end-marker $80
  6331.         INC     HL              ; next location becomes
  6332.         LD      ($5C61),HL      ; start of WORKSP
  6333.  
  6334. ; This entry point is used prior to input and prior to the execution,
  6335. ; or parsing, of each statement.
  6336.  
  6337. ;; SET-WORK
  6338. L16BF:  LD      HL,($5C61)      ; fetch WORKSP value
  6339.         LD      ($5C63),HL      ; and place in STKBOT
  6340.  
  6341. ; This entry point is used to move the stack back to its normal place
  6342. ; after temporary relocation during line entry and also from ERROR-3
  6343.  
  6344. ;; SET-STK
  6345. L16C5:  LD      HL,($5C63)      ; fetch STKBOT value
  6346.         LD      ($5C65),HL      ; and place in STKEND.
  6347.  
  6348.         PUSH    HL              ; perhaps an obsolete entry point.
  6349.         LD      HL,$5C92        ; normal location of MEM-0
  6350.         LD      ($5C68),HL      ; is restored to system variable MEM.
  6351.         POP     HL              ; saved value not required.
  6352.         RET                     ; return.
  6353.  
  6354. ; ------------------
  6355. ; Reclaim edit-line?
  6356. ; ------------------
  6357. ; This seems to be legacy code from the ZX80/ZX81 as it is
  6358. ; not used in this ROM.
  6359. ; That task, in fact, is performed here by the dual-area routine CLEAR-SP.
  6360. ; This routine is designed to deal with something that is known to be in the
  6361. ; edit buffer and not workspace.
  6362. ; On entry, HL must point to the end of the something to be deleted.
  6363.  
  6364. ;; REC-EDIT
  6365. L16D4:  LD      DE,($5C59)      ; fetch start of edit line from E_LINE.
  6366.         JP      L19E5           ; jump forward to RECLAIM-1.
  6367.  
  6368. ; --------------------------
  6369. ; The Table INDEXING routine
  6370. ; --------------------------
  6371. ; This routine is used to search two-byte hash tables for a character
  6372. ; held in C, returning the address of the following offset byte.
  6373. ; if it is known that the character is in the table e.g. for priorities,
  6374. ; then the table requires no zero end-marker. If this is not known at the
  6375. ; outset then a zero end-marker is required and carry is set to signal
  6376. ; success.
  6377.  
  6378. ;; INDEXER-1
  6379. L16DB:  INC     HL              ; address the next pair of values.
  6380.  
  6381. ; -> The Entry Point.
  6382.  
  6383. ;; INDEXER
  6384. L16DC:  LD      A,(HL)          ; fetch the first byte of pair
  6385.         AND     A               ; is it the end-marker ?
  6386.         RET     Z               ; return with carry reset if so.
  6387.  
  6388.         CP      C               ; is it the required character ?
  6389.         INC     HL              ; address next location.
  6390.         JR      NZ,L16DB        ; back to INDEXER-1 if no match.
  6391.  
  6392.         SCF                     ; else set the carry flag.
  6393.         RET                     ; return with carry set
  6394.  
  6395. ; --------------------------------
  6396. ; The Channel and Streams Routines
  6397. ; --------------------------------
  6398. ; A channel is an input/output route to a hardware device
  6399. ; and is identified to the system by a single letter e.g. 'K' for
  6400. ; the keyboard. A channel can have an input and output route
  6401. ; associated with it in which case it is bi-directional like
  6402. ; the keyboard. Others like the upper screen 'S' are output
  6403. ; only and the input routine usually points to a report message.
  6404. ; Channels 'K' and 'S' are system channels and it would be inappropriate
  6405. ; to close the associated streams so a mechanism is provided to
  6406. ; re-attach them. When the re-attachment is no longer required, then
  6407. ; closing these streams resets them as at initialization.
  6408. ; The same also would have applied to channel 'R', the RS232 channel
  6409. ; as that is used by the system. It's input stream seems to have been
  6410. ; removed and it is not available to the user. However the channel could
  6411. ; not be removed entirely as its output routine was used by the system.
  6412. ; As a result of removing this channel, channel 'P', the printer is
  6413. ; erroneously treated as a system channel.
  6414. ; Ironically the tape streamer is not accessed through streams and
  6415. ; channels.
  6416. ; Early demonstrations of the Spectrum showed a single microdrive being
  6417. ; controlled by this ROM. Adverts also said that the network and RS232
  6418. ; were in this ROM. Channels 'M' and 'N' are user channels and have been
  6419. ; removed successfully if, as seems vaguely possible, they existed.
  6420.  
  6421. ; ---------------------
  6422. ; Handle CLOSE# command
  6423. ; ---------------------
  6424. ; This command allows streams to be closed after use.
  6425. ; Any temporary memory areas used by the stream would be reclaimed and
  6426. ; finally flags set or reset if necessary.
  6427.  
  6428. ;; CLOSE
  6429. L16E5:  CALL    L171E           ; routine STR-DATA fetches parameter
  6430.                                 ; from calculator stack and gets the
  6431.                                 ; existing STRMS data pointer address in HL
  6432.                                 ; and stream offset from CHANS in BC.
  6433.  
  6434.                                 ; Note. this offset could be zero if the
  6435.                                 ; stream is already closed. A check for this
  6436.                                 ; should occur now and an error should be
  6437.                                 ; generated, for example,
  6438.                                 ; Report S 'Stream already closed'.
  6439.  
  6440.         CALL    L1701           ; routine CLOSE-2 would perform any actions
  6441.                                 ; peculiar to that stream without disturbing
  6442.                                 ; data pointer to STRMS entry in HL.
  6443.  
  6444.         LD      BC,$0000        ; the stream is to be blanked.
  6445.         LD      DE,$A3E2        ; the number of bytes from stream 4, $5C1E,
  6446.                                 ; to $10000
  6447.         EX      DE,HL           ; transfer offset to HL, STRMS data pointer
  6448.                                 ; to DE.
  6449.         ADD     HL,DE           ; add the offset to the data pointer.  
  6450.         JR      C,L16FC         ; forward to CLOSE-1 if a non-system stream.
  6451.                                 ; i.e. higher than 3.
  6452.  
  6453. ; proceed with a negative result.
  6454.  
  6455.         LD      BC,L15C6 + 14   ; prepare the address of the byte after
  6456.                                 ; the initial stream data in ROM. ($15D4)
  6457.         ADD     HL,BC           ; index into the data table with negative value.
  6458.         LD      C,(HL)          ; low byte to C
  6459.         INC     HL              ; address next.
  6460.         LD      B,(HL)          ; high byte to B.
  6461.  
  6462. ; and for streams 0 - 3 just enter the initial data back into the STRMS entry
  6463. ; streams 0 - 2 can't be closed as they are shared by the operating system.
  6464. ; -> for streams 4 - 15 then blank the entry.
  6465.  
  6466. ;; CLOSE-1
  6467. L16FC:  EX      DE,HL           ; address of stream to HL.
  6468.         LD      (HL),C          ; place zero (or low byte).
  6469.         INC     HL              ; next address.
  6470.         LD      (HL),B          ; place zero (or high byte).
  6471.         RET                     ; return.
  6472.  
  6473. ; ------------------
  6474. ; CLOSE-2 Subroutine
  6475. ; ------------------
  6476. ; There is not much point in coming here.
  6477. ; The purpose was once to find the offset to a special closing routine,
  6478. ; in this ROM and within 256 bytes of the close stream look up table that
  6479. ; would reclaim any buffers associated with a stream. At least one has been
  6480. ; removed.
  6481.  
  6482. ;; CLOSE-2
  6483. L1701:  PUSH    HL              ; * save address of stream data pointer
  6484.                                 ; in STRMS on the machine stack.
  6485.         LD      HL,($5C4F)      ; fetch CHANS address to HL
  6486.         ADD     HL,BC           ; add the offset to address the second
  6487.                                 ; byte of the output routine hopefully.
  6488.         INC     HL              ; step past
  6489.         INC     HL              ; the input routine.
  6490.         INC     HL              ; to address channel's letter
  6491.         LD      C,(HL)          ; pick it up in C.
  6492.                                 ; Note. but if stream is already closed we
  6493.                                 ; get the value $10 (the byte preceding 'K').
  6494.         EX      DE,HL           ; save the pointer to the letter in DE.
  6495.         LD      HL,L1716        ; address: cl-str-lu in ROM.
  6496.         CALL    L16DC           ; routine INDEXER uses the code to get
  6497.                                 ; the 8-bit offset from the current point to
  6498.                                 ; the address of the closing routine in ROM.
  6499.                                 ; Note. it won't find $10 there!
  6500.         LD      C,(HL)          ; transfer the offset to C.
  6501.         LD      B,$00           ; prepare to add.
  6502.         ADD     HL,BC           ; add offset to point to the address of the
  6503.                                 ; routine that closes the stream.
  6504.                                 ; (and presumably removes any buffers that
  6505.                                 ; are associated with it.)
  6506.         JP      (HL)            ; jump to that routine.
  6507.  
  6508. ; --------------------------
  6509. ; CLOSE stream look-up table
  6510. ; --------------------------
  6511. ; This table contains an entry for a letter found in the CHANS area.
  6512. ; followed by an 8-bit displacement, from that byte's address in the
  6513. ; table to the routine that performs any ancillary actions associated
  6514. ; with closing the stream of that channel.
  6515. ; The table doesn't require a zero end-marker as the letter has been
  6516. ; picked up from a channel that has an open stream.
  6517.  
  6518. ;; cl-str-lu
  6519. L1716:  DB    'K', L171C-$-1  ; offset 5 to CLOSE-STR
  6520.         DB    'S', L171C-$-1  ; offset 3 to CLOSE-STR
  6521.         DB    'P', L171C-$-1  ; offset 1 to CLOSE-STR
  6522.  
  6523.  
  6524. ; ------------------------
  6525. ; Close Stream Subroutines
  6526. ; ------------------------
  6527. ; The close stream routines in fact have no ancillary actions to perform
  6528. ; which is not surprising with regard to 'K' and 'S'.
  6529.  
  6530. ;; CLOSE-STR                    
  6531. L171C:  POP     HL              ; * now just restore the stream data pointer
  6532.         RET                     ; in STRMS and return.
  6533.  
  6534. ; -----------
  6535. ; Stream data
  6536. ; -----------
  6537. ; This routine finds the data entry in the STRMS area for the specified
  6538. ; stream which is passed on the calculator stack. It returns with HL
  6539. ; pointing to this system variable and BC holding a displacement from
  6540. ; the CHANS area to the second byte of the stream's channel. If BC holds
  6541. ; zero, then that signifies that the stream is closed.
  6542.  
  6543. ;; STR-DATA
  6544. L171E:  CALL    L1E94           ; routine FIND-INT1 fetches parameter to A
  6545.         CP      $10             ; is it less than 16d ?
  6546.         JR      C,L1727         ; skip forward to STR-DATA1 if so.
  6547.  
  6548. ;; REPORT-Ob
  6549. L1725:  RST     08H             ; ERROR-1
  6550.         DB    $17             ; Error Report: Invalid stream
  6551.  
  6552. ;; STR-DATA1
  6553. L1727:  ADD     A,$03           ; add the offset for 3 system streams.
  6554.                                 ; range 00 - 15d becomes 3 - 18d.
  6555.         RLCA                    ; double as there are two bytes per
  6556.                                 ; stream - now 06 - 36d
  6557.         LD      HL,$5C10        ; address STRMS - the start of the streams
  6558.                                 ; data area in system variables.
  6559.         LD      C,A             ; transfer the low byte to A.
  6560.         LD      B,$00           ; prepare to add offset.
  6561.         ADD     HL,BC           ; add to address the data entry in STRMS.
  6562.  
  6563. ; the data entry itself contains an offset from CHANS to the address of the
  6564. ; stream
  6565.  
  6566.         LD      C,(HL)          ; low byte of displacement to C.
  6567.         INC     HL              ; address next.
  6568.         LD      B,(HL)          ; high byte of displacement to B.
  6569.         DEC     HL              ; step back to leave HL pointing to STRMS
  6570.                                 ; data entry.
  6571.         RET                     ; return with CHANS displacement in BC
  6572.                                 ; and address of stream data entry in HL.
  6573.  
  6574. ; --------------------
  6575. ; Handle OPEN# command
  6576. ; --------------------
  6577. ; Command syntax example: OPEN #5,"s"
  6578. ; On entry the channel code entry is on the calculator stack with the next
  6579. ; value containing the stream identifier. They have to swapped.
  6580.  
  6581. ;; OPEN
  6582. L1736:  RST     28H             ;; FP-CALC    ;s,c.
  6583.         DB    $01             ;;exchange    ;c,s.
  6584.         DB    $38             ;;end-calc
  6585.  
  6586.         CALL    L171E           ; routine STR-DATA fetches the stream off
  6587.                                 ; the stack and returns with the CHANS
  6588.                                 ; displacement in BC and HL addressing
  6589.                                 ; the STRMS data entry.
  6590.         LD      A,B             ; test for zero which
  6591.         OR      C               ; indicates the stream is closed.
  6592.         JR      Z,L1756         ; skip forward to OPEN-1 if so.
  6593.  
  6594. ; if it is a system channel then it can re-attached.
  6595.  
  6596.         EX      DE,HL           ; save STRMS address in DE.
  6597.         LD      HL,($5C4F)      ; fetch CHANS.
  6598.         ADD     HL,BC           ; add the offset to address the second
  6599.                                 ; byte of the channel.
  6600.         INC     HL              ; skip over the
  6601.         INC     HL              ; input routine.
  6602.         INC     HL              ; and address the letter.
  6603.         LD      A,(HL)          ; pick up the letter.
  6604.         EX      DE,HL           ; save letter pointer and bring back
  6605.                                 ; the STRMS pointer.
  6606.  
  6607.         CP      $4B             ; is it 'K' ?
  6608.         JR      Z,L1756         ; forward to OPEN-1 if so
  6609.  
  6610.         CP      $53             ; is it 'S' ?
  6611.         JR      Z,L1756         ; forward to OPEN-1 if so
  6612.  
  6613.         CP      $50             ; is it 'P' ?
  6614.         JR      NZ,L1725        ; back to REPORT-Ob if not.
  6615.                                 ; to report 'Invalid stream'.
  6616.  
  6617. ; continue if one of the upper-case letters was found.
  6618. ; and rejoin here from above if stream was closed.
  6619.  
  6620. ;; OPEN-1
  6621. L1756:  CALL    L175D           ; routine OPEN-2 opens the stream.
  6622.  
  6623. ; it now remains to update the STRMS variable.
  6624.  
  6625.         LD      (HL),E          ; insert or overwrite the low byte.
  6626.         INC     HL              ; address high byte in STRMS.
  6627.         LD      (HL),D          ; insert or overwrite the high byte.
  6628.         RET                     ; return.
  6629.  
  6630. ; -----------------
  6631. ; OPEN-2 Subroutine
  6632. ; -----------------
  6633. ; There is some point in coming here as, as well as once creating buffers,
  6634. ; this routine also sets flags.
  6635.  
  6636. ;; OPEN-2
  6637. L175D:  PUSH    HL              ; * save the STRMS data entry pointer.
  6638.         CALL    L2BF1           ; routine STK-FETCH now fetches the
  6639.                                 ; parameters of the channel string.
  6640.                                 ; start in DE, length in BC.
  6641.  
  6642.         LD      A,B             ; test that it is not
  6643.         OR      C               ; the null string.
  6644.         JR      NZ,L1767        ; skip forward to OPEN-3 with 1 character
  6645.                                 ; or more!
  6646.  
  6647. ;; REPORT-Fb
  6648. L1765:  RST     08H             ; ERROR-1
  6649.         DB    $0E             ; Error Report: Invalid file name
  6650.  
  6651. ;; OPEN-3
  6652. L1767:  PUSH    BC              ; save the length of the string.
  6653.         LD      A,(DE)          ; pick up the first character.
  6654.                                 ; Note. if the second character is used to
  6655.                                 ; distinguish between a binary or text
  6656.                                 ; channel then it will be simply a matter
  6657.                                 ; of setting bit 7 of FLAGX.
  6658.         AND     $DF             ; make it upper-case.
  6659.         LD      C,A             ; place it in C.
  6660.         LD      HL,L177A        ; address: op-str-lu is loaded.
  6661.         CALL    L16DC           ; routine INDEXER will search for letter.
  6662.         JR      NC,L1765        ; back to REPORT-F if not found
  6663.                                 ; 'Invalid filename'
  6664.  
  6665.         LD      C,(HL)          ; fetch the displacement to opening routine.
  6666.         LD      B,$00           ; prepare to add.
  6667.         ADD     HL,BC           ; now form address of opening routine.
  6668.         POP     BC              ; restore the length of string.
  6669.         JP      (HL)            ; now jump forward to the relevant routine.
  6670.  
  6671. ; -------------------------
  6672. ; OPEN stream look-up table
  6673. ; -------------------------
  6674. ; The open stream look-up table consists of matched pairs.
  6675. ; The channel letter is followed by an 8-bit displacement to the
  6676. ; associated stream-opening routine in this ROM.
  6677. ; The table requires a zero end-marker as the letter has been
  6678. ; provided by the user and not the operating system.
  6679.  
  6680. ;; op-str-lu
  6681. L177A:  DB    'K', L1781-$-1  ; $06 offset to OPEN-K
  6682.         DB    'S', L1785-$-1  ; $08 offset to OPEN-S
  6683.         DB    'P', L1789-$-1  ; $0A offset to OPEN-P
  6684.  
  6685.         DB    $00             ; end-marker.
  6686.  
  6687. ; ----------------------------
  6688. ; The Stream Opening Routines.
  6689. ; ----------------------------
  6690. ; These routines would have opened any buffers associated with the stream
  6691. ; before jumping forward to to OPEN-END with the displacement value in E
  6692. ; and perhaps a modified value in BC. The strange pathing does seem to
  6693. ; provide for flexibility in this respect.
  6694. ;
  6695. ; There is no need to open the printer buffer as it is there already
  6696. ; even if you are still saving up for a ZX Printer or have moved onto
  6697. ; something bigger. In any case it would have to be created after
  6698. ; the system variables but apart from that it is a simple task
  6699. ; and all but one of the ROM routines can handle a buffer in that position.
  6700. ; (PR-ALL-6 would require an extra 3 bytes of code).
  6701. ; However it wouldn't be wise to have two streams attached to the ZX Printer
  6702. ; as you can now, so one assumes that if PR_CC_hi was non-zero then
  6703. ; the OPEN-P routine would have refused to attach a stream if another
  6704. ; stream was attached.
  6705.  
  6706. ; Something of significance is being passed to these ghost routines in the
  6707. ; second character. Strings 'RB', 'RT' perhaps or a drive/station number.
  6708. ; The routine would have to deal with that and exit to OPEN_END with BC
  6709. ; containing $0001 or more likely there would be an exit within the routine.
  6710. ; Anyway doesn't matter, these routines are long gone.
  6711.  
  6712. ; -----------------
  6713. ; OPEN-K Subroutine
  6714. ; -----------------
  6715. ; Open Keyboard stream.
  6716.  
  6717. ;; OPEN-K
  6718. L1781:  LD      E,$01           ; 01 is offset to second byte of channel 'K'.
  6719.         JR      L178B           ; forward to OPEN-END
  6720.  
  6721. ; -----------------
  6722. ; OPEN-S Subroutine
  6723. ; -----------------
  6724. ; Open Screen stream.
  6725.  
  6726. ;; OPEN-S
  6727. L1785:  LD      E,$06           ; 06 is offset to 2nd byte of channel 'S'
  6728.         JR      L178B           ; to OPEN-END
  6729.  
  6730. ; -----------------
  6731. ; OPEN-P Subroutine
  6732. ; -----------------
  6733. ; Open Printer stream.
  6734.  
  6735. ;; OPEN-P
  6736. L1789:  LD      E,$10           ; 16d is offset to 2nd byte of channel 'P'
  6737.  
  6738. ;; OPEN-END
  6739. L178B:  DEC     BC              ; the stored length of 'K','S','P' or
  6740.                                 ; whatever is now tested. ??
  6741.         LD      A,B             ; test now if initial or residual length
  6742.         OR      C               ; is one character.
  6743.         JR      NZ,L1765        ; to REPORT-Fb 'Invalid file name' if not.
  6744.  
  6745.         LD      D,A             ; load D with zero to form the displacement
  6746.                                 ; in the DE register.
  6747.         POP     HL              ; * restore the saved STRMS pointer.
  6748.         RET                     ; return to update STRMS entry thereby
  6749.                                 ; signaling stream is open.
  6750.  
  6751. ; ----------------------------------------
  6752. ; Handle CAT, ERASE, FORMAT, MOVE commands
  6753. ; ----------------------------------------
  6754. ; These just generate an error report as the ROM is 'incomplete'.
  6755. ;
  6756. ; Luckily this provides a mechanism for extending these in a shadow ROM
  6757. ; but without the powerful mechanisms set up in this ROM.
  6758. ; An instruction fetch on $0008 may page in a peripheral ROM,
  6759. ; e.g. the Sinclair Interface 1 ROM, to handle these commands.
  6760. ; However that wasn't the plan.
  6761. ; Development of this ROM continued for another three months until the cost
  6762. ; of replacing it and the manual became unfeasible.
  6763. ; The ultimate power of channels and streams died at birth.
  6764.  
  6765. ;; CAT-ETC
  6766. L1793:  JR      L1725           ; to REPORT-Ob
  6767.  
  6768. ; -----------------
  6769. ; Perform AUTO-LIST
  6770. ; -----------------
  6771. ; This produces an automatic listing in the upper screen.
  6772.  
  6773. ;; AUTO-LIST
  6774. L1795:  LD      ($5C3F),SP      ; save stack pointer in LIST_SP
  6775.         LD      (IY+$02),$10    ; update TV_FLAG set bit 3
  6776.         CALL    L0DAF           ; routine CL-ALL.
  6777.         SET     0,(IY+$02)      ; update TV_FLAG  - signal lower screen in use
  6778.  
  6779.         LD      B,(IY+$31)      ; fetch DF_SZ to B.
  6780.         CALL    L0E44           ; routine CL-LINE clears lower display
  6781.                                 ; preserving B.
  6782.         RES     0,(IY+$02)      ; update TV_FLAG  - signal main screen in use
  6783.         SET     0,(IY+$30)      ; update FLAGS2  - signal unnecessary to
  6784.                                 ; clear main screen.
  6785.         LD      HL,($5C49)      ; fetch E_PPC current edit line to HL.
  6786.         LD      DE,($5C6C)      ; fetch S_TOP to DE, the current top line
  6787.                                 ; (initially zero)
  6788.         AND     A               ; prepare for true subtraction.
  6789.         SBC     HL,DE           ; subtract and
  6790.         ADD     HL,DE           ; add back.
  6791.         JR      C,L17E1         ; to AUTO-L-2 if S_TOP higher than E_PPC
  6792.                                 ; to set S_TOP to E_PPC
  6793.  
  6794.         PUSH    DE              ; save the top line number.
  6795.         CALL    L196E           ; routine LINE-ADDR gets address of E_PPC.
  6796.         LD      DE,$02C0        ; prepare known number of characters in
  6797.                                 ; the default upper screen.
  6798.         EX      DE,HL           ; offset to HL, program address to DE.
  6799.         SBC     HL,DE           ; subtract high value from low to obtain
  6800.                                 ; negated result used in addition.
  6801.         EX      (SP),HL         ; swap result with top line number on stack.
  6802.         CALL    L196E           ; routine LINE-ADDR  gets address of that
  6803.                                 ; top line in HL and next line in DE.
  6804.         POP     BC              ; restore the result to balance stack.
  6805.  
  6806. ;; AUTO-L-1
  6807. L17CE:  PUSH    BC              ; save the result.
  6808.         CALL    L19B8           ; routine NEXT-ONE gets address in HL of
  6809.                                 ; line after auto-line (in DE).
  6810.         POP     BC              ; restore result.
  6811.         ADD     HL,BC           ; compute back.
  6812.         JR      C,L17E4         ; to AUTO-L-3 if line 'should' appear
  6813.  
  6814.         EX      DE,HL           ; address of next line to HL.
  6815.         LD      D,(HL)          ; get line
  6816.         INC     HL              ; number
  6817.         LD      E,(HL)          ; in DE.
  6818.         DEC     HL              ; adjust back to start.
  6819.         LD      ($5C6C),DE      ; update S_TOP.
  6820.         JR      L17CE           ; to AUTO-L-1 until estimate reached.
  6821.  
  6822. ; ---
  6823.  
  6824. ; the jump was to here if S_TOP was greater than E_PPC
  6825.  
  6826. ;; AUTO-L-2
  6827. L17E1:  LD      ($5C6C),HL      ; make S_TOP the same as E_PPC.
  6828.  
  6829. ; continue here with valid starting point from above or good estimate
  6830. ; from computation
  6831.  
  6832. ;; AUTO-L-3
  6833. L17E4:  LD      HL,($5C6C)      ; fetch S_TOP line number to HL.
  6834.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  6835.                                 ; address of next in DE.
  6836.         JR      Z,L17ED         ; to AUTO-L-4 if line exists.
  6837.  
  6838.         EX      DE,HL           ; else use address of next line.
  6839.  
  6840. ;; AUTO-L-4
  6841. L17ED:  CALL    L1833           ; routine LIST-ALL                >>>
  6842.  
  6843. ; The return will be to here if no scrolling occurred
  6844.  
  6845.         RES     4,(IY+$02)      ; update TV_FLAG  - signal no auto listing.
  6846.         RET                     ; return.
  6847.  
  6848. ; ------------
  6849. ; Handle LLIST
  6850. ; ------------
  6851. ; A short form of LIST #3. The listing goes to stream 3 - default printer.
  6852.  
  6853. ;; LLIST
  6854. L17F5:  LD      A,$03           ; the usual stream for ZX Printer
  6855.         JR      L17FB           ; forward to LIST-1
  6856.  
  6857. ; -----------
  6858. ; Handle LIST
  6859. ; -----------
  6860. ; List to any stream.
  6861. ; Note. While a starting line can be specified it is
  6862. ; not possible to specify an end line.
  6863. ; Just listing a line makes it the current edit line.
  6864.  
  6865. ;; LIST
  6866. L17F9:  LD      A,$02           ; default is stream 2 - the upper screen.
  6867.  
  6868. ;; LIST-1
  6869. L17FB:  LD      (IY+$02),$00    ; the TV_FLAG is initialized with bit 0 reset
  6870.                                 ; indicating upper screen in use.
  6871.         CALL    L2530           ; routine SYNTAX-Z - checking syntax ?
  6872.         CALL    NZ,L1601        ; routine CHAN-OPEN if in run-time.
  6873.  
  6874.         RST     18H             ; GET-CHAR
  6875.         CALL    L2070           ; routine STR-ALTER will alter if '#'.
  6876.         JR      C,L181F         ; forward to LIST-4 not a '#' .
  6877.  
  6878.  
  6879.         RST     18H             ; GET-CHAR
  6880.         CP      $3B             ; is it ';' ?
  6881.         JR      Z,L1814         ; skip to LIST-2 if so.
  6882.  
  6883.         CP      $2C             ; is it ',' ?
  6884.         JR      NZ,L181A        ; forward to LIST-3 if neither separator.
  6885.  
  6886. ; we have, say,  LIST #15, and a number must follow the separator.
  6887.  
  6888. ;; LIST-2
  6889. L1814:  RST     20H             ; NEXT-CHAR
  6890.         CALL    L1C82           ; routine EXPT-1NUM
  6891.         JR      L1822           ; forward to LIST-5
  6892.  
  6893. ; ---
  6894.  
  6895. ; the branch was here with just LIST #3 etc.
  6896.  
  6897. ;; LIST-3
  6898. L181A:  CALL    L1CE6           ; routine USE-ZERO
  6899.         JR      L1822           ; forward to LIST-5
  6900.  
  6901. ; ---
  6902.  
  6903. ; the branch was here with LIST
  6904.  
  6905. ;; LIST-4
  6906. L181F:  CALL    L1CDE           ; routine FETCH-NUM checks if a number
  6907.                                 ; follows else uses zero.
  6908.  
  6909. ;; LIST-5
  6910. L1822:  CALL    L1BEE           ; routine CHECK-END quits if syntax OK >>>
  6911.  
  6912.         CALL    L1E99           ; routine FIND-INT2 fetches the number
  6913.                                 ; from the calculator stack in run-time.
  6914.         LD      A,B             ; fetch high byte of line number and
  6915.         AND     $3F             ; make less than $40 so that NEXT-ONE
  6916.                                 ; (from LINE-ADDR) doesn't lose context.
  6917.                                 ; Note. this is not satisfactory and the typo
  6918.                                 ; LIST 20000 will list an entirely different
  6919.                                 ; section than LIST 2000. Such typos are not
  6920.                                 ; available for checking if they are direct
  6921.                                 ; commands.
  6922.  
  6923.         LD      H,A             ; transfer the modified
  6924.         LD      L,C             ; line number to HL.
  6925.         LD      ($5C49),HL      ; update E_PPC to new line number.
  6926.         CALL    L196E           ; routine LINE-ADDR gets the address of the
  6927.                                 ; line.
  6928.  
  6929. ; This routine is called from AUTO-LIST
  6930.  
  6931. ;; LIST-ALL
  6932. L1833:  LD      E,$01           ; signal current line not yet printed
  6933.  
  6934. ;; LIST-ALL-2
  6935. L1835:  CALL    L1855           ; routine OUT-LINE outputs a BASIC line
  6936.                                 ; using PRINT-OUT and makes an early return
  6937.                                 ; when no more lines to print. >>>
  6938.  
  6939.         RST     10H             ; PRINT-A prints the carriage return (in A)
  6940.  
  6941.         BIT     4,(IY+$02)      ; test TV_FLAG  - automatic listing ?
  6942.         JR      Z,L1835         ; back to LIST-ALL-2 if not
  6943.                                 ; (loop exit is via OUT-LINE)
  6944.  
  6945. ; continue here if an automatic listing required.
  6946.  
  6947.         LD      A,($5C6B)       ; fetch DF_SZ lower display file size.
  6948.         SUB     (IY+$4F)        ; subtract S_POSN_hi ithe current line number.
  6949.         JR      NZ,L1835        ; back to LIST-ALL-2 if upper screen not full.
  6950.  
  6951.         XOR     E               ; A contains zero, E contains one if the
  6952.                                 ; current edit line has not been printed
  6953.                                 ; or zero if it has (from OUT-LINE).
  6954.         RET     Z               ; return if the screen is full and the line
  6955.                                 ; has been printed.
  6956.  
  6957. ; continue with automatic listings if the screen is full and the current
  6958. ; edit line is missing. OUT-LINE will scroll automatically.
  6959.  
  6960.         PUSH    HL              ; save the pointer address.
  6961.         PUSH    DE              ; save the E flag.
  6962.         LD      HL,$5C6C        ; fetch S_TOP the rough estimate.
  6963.         CALL    L190F           ; routine LN-FETCH updates S_TOP with
  6964.                                 ; the number of the next line.
  6965.         POP     DE              ; restore the E flag.
  6966.         POP     HL              ; restore the address of the next line.
  6967.         JR      L1835           ; back to LIST-ALL-2.
  6968.  
  6969. ; ------------------------
  6970. ; Print a whole BASIC line
  6971. ; ------------------------
  6972. ; This routine prints a whole BASIC line and it is called
  6973. ; from LIST-ALL to output the line to current channel
  6974. ; and from ED-EDIT to 'sprint' the line to the edit buffer.
  6975.  
  6976. ;; OUT-LINE
  6977. L1855:  LD      BC,($5C49)      ; fetch E_PPC the current line which may be
  6978.                                 ; unchecked and not exist.
  6979.         CALL    L1980           ; routine CP-LINES finds match or line after.
  6980.         LD      D,$3E           ; prepare cursor '>' in D.
  6981.         JR      Z,L1865         ; to OUT-LINE1 if matched or line after.
  6982.  
  6983.         LD      DE,$0000        ; put zero in D, to suppress line cursor.
  6984.         RL      E               ; pick up carry in E if line before current
  6985.                                 ; leave E zero if same or after.
  6986.  
  6987. ;; OUT-LINE1
  6988. L1865:  LD      (IY+$2D),E      ; save flag in BREG which is spare.
  6989.         LD      A,(HL)          ; get high byte of line number.
  6990.         CP      $40             ; is it too high ($2F is maximum possible) ?
  6991.         POP     BC              ; drop the return address and
  6992.         RET     NC              ; make an early return if so >>>
  6993.  
  6994.         PUSH    BC              ; save return address
  6995.         CALL    L1A28           ; routine OUT-NUM-2 to print addressed number
  6996.                                 ; with leading space.
  6997.         INC     HL              ; skip low number byte.
  6998.         INC     HL              ; and the two
  6999.         INC     HL              ; length bytes.
  7000.         RES     0,(IY+$01)      ; update FLAGS - signal leading space required.
  7001.         LD      A,D             ; fetch the cursor.
  7002.         AND     A               ; test for zero.
  7003.         JR      Z,L1881         ; to OUT-LINE3 if zero.
  7004.  
  7005.  
  7006.         RST     10H             ; PRINT-A prints '>' the current line cursor.
  7007.  
  7008. ; this entry point is called from ED-COPY
  7009.  
  7010. ;; OUT-LINE2
  7011. L187D:  SET     0,(IY+$01)      ; update FLAGS - suppress leading space.
  7012.  
  7013. ;; OUT-LINE3
  7014. L1881:  PUSH    DE              ; save flag E for a return value.
  7015.         EX      DE,HL           ; save HL address in DE.
  7016.         RES     2,(IY+$30)      ; update FLAGS2 - signal NOT in QUOTES.
  7017.  
  7018.         LD      HL,$5C3B        ; point to FLAGS.
  7019.         RES     2,(HL)          ; signal 'K' mode. (starts before keyword)
  7020.         BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7021.         JR      Z,L1894         ; forward to OUT-LINE4 if not.
  7022.  
  7023.         SET     2,(HL)          ; signal 'L' mode. (used for input)
  7024.  
  7025. ;; OUT-LINE4
  7026. L1894:  LD      HL,($5C5F)      ; fetch X_PTR - possibly the error pointer
  7027.                                 ; address.
  7028.         AND     A               ; clear the carry flag.
  7029.         SBC     HL,DE           ; test if an error address has been reached.
  7030.         JR      NZ,L18A1        ; forward to OUT-LINE5 if not.
  7031.  
  7032.         LD      A,$3F           ; load A with '?' the error marker.
  7033.         CALL    L18C1           ; routine OUT-FLASH to print flashing marker.
  7034.  
  7035. ;; OUT-LINE5
  7036. L18A1:  CALL    L18E1           ; routine OUT-CURS will print the cursor if
  7037.                                 ; this is the right position.
  7038.         EX      DE,HL           ; restore address pointer to HL.
  7039.         LD      A,(HL)          ; fetch the addressed character.
  7040.         CALL    L18B6           ; routine NUMBER skips a hidden floating
  7041.                                 ; point number if present.
  7042.         INC     HL              ; now increment the pointer.
  7043.         CP      $0D             ; is character end-of-line ?
  7044.         JR      Z,L18B4         ; to OUT-LINE6, if so, as line is finished.
  7045.  
  7046.         EX      DE,HL           ; save the pointer in DE.
  7047.         CALL    L1937           ; routine OUT-CHAR to output character/token.
  7048.  
  7049.         JR      L1894           ; back to OUT-LINE4 until entire line is done.
  7050.  
  7051. ; ---
  7052.  
  7053. ;; OUT-LINE6
  7054. L18B4:  POP     DE              ; bring back the flag E, zero if current
  7055.                                 ; line printed else 1 if still to print.
  7056.         RET                     ; return with A holding $0D
  7057.  
  7058. ; -------------------------
  7059. ; Check for a number marker
  7060. ; -------------------------
  7061. ; this subroutine is called from two processes. while outputting BASIC lines
  7062. ; and while searching statements within a BASIC line.
  7063. ; during both, this routine will pass over an invisible number indicator
  7064. ; and the five bytes floating-point number that follows it.
  7065. ; Note that this causes floating point numbers to be stripped from
  7066. ; the BASIC line when it is fetched to the edit buffer by OUT_LINE.
  7067. ; the number marker also appears after the arguments of a DEF FN statement
  7068. ; and may mask old 5-byte string parameters.
  7069.  
  7070. ;; NUMBER
  7071. L18B6:  CP      $0E             ; character fourteen ?
  7072.         RET     NZ              ; return if not.
  7073.  
  7074.         INC     HL              ; skip the character
  7075.         INC     HL              ; and five bytes
  7076.         INC     HL              ; following.
  7077.         INC     HL              ;
  7078.         INC     HL              ;
  7079.         INC     HL              ;
  7080.         LD      A,(HL)          ; fetch the following character
  7081.         RET                     ; for return value.
  7082.  
  7083. ; --------------------------
  7084. ; Print a flashing character
  7085. ; --------------------------
  7086. ; This subroutine is called from OUT-LINE to print a flashing error
  7087. ; marker '?' or from the next routine to print a flashing cursor e.g. 'L'.
  7088. ; However, this only gets called from OUT-LINE when printing the edit line
  7089. ; or the input buffer to the lower screen so a direct call to $09F4 can
  7090. ; be used, even though out-line outputs to other streams.
  7091. ; In fact the alternate set is used for the whole routine.
  7092.  
  7093. ;; OUT-FLASH
  7094. L18C1:  EXX                     ; switch in alternate set
  7095.  
  7096.         LD      HL,($5C8F)      ; fetch L = ATTR_T, H = MASK-T
  7097.         PUSH    HL              ; save masks.
  7098.         RES     7,H             ; reset flash mask bit so active.
  7099.         SET     7,L             ; make attribute FLASH.
  7100.         LD      ($5C8F),HL      ; resave ATTR_T and MASK-T
  7101.  
  7102.         LD      HL,$5C91        ; address P_FLAG
  7103.         LD      D,(HL)          ; fetch to D
  7104.         PUSH    DE              ; and save.
  7105.         LD      (HL),$00        ; clear inverse, over, ink/paper 9
  7106.  
  7107.         CALL    L09F4           ; routine PRINT-OUT outputs character
  7108.                                 ; without the need to vector via RST 10.
  7109.  
  7110.         POP     HL              ; pop P_FLAG to H.
  7111.         LD      (IY+$57),H      ; and restore system variable P_FLAG.
  7112.         POP     HL              ; restore temporary masks
  7113.         LD      ($5C8F),HL      ; and restore system variables ATTR_T/MASK_T
  7114.  
  7115.         EXX                     ; switch back to main set
  7116.         RET                     ; return
  7117.  
  7118. ; ----------------
  7119. ; Print the cursor
  7120. ; ----------------
  7121. ; This routine is called before any character is output while outputting
  7122. ; a BASIC line or the input buffer. This includes listing to a printer
  7123. ; or screen, copying a BASIC line to the edit buffer and printing the
  7124. ; input buffer or edit buffer to the lower screen. It is only in the
  7125. ; latter two cases that it has any relevance and in the last case it
  7126. ; performs another very important function also.
  7127.  
  7128. ;; OUT-CURS
  7129. L18E1:  LD      HL,($5C5B)      ; fetch K_CUR the current cursor address
  7130.         AND     A               ; prepare for true subtraction.
  7131.         SBC     HL,DE           ; test against pointer address in DE and
  7132.         RET     NZ              ; return if not at exact position.
  7133.  
  7134. ; the value of MODE, maintained by KEY-INPUT, is tested and if non-zero
  7135. ; then this value 'E' or 'G' will take precedence.
  7136.  
  7137.         LD      A,($5C41)       ; fetch MODE  0='KLC', 1='E', 2='G'.
  7138.                 DB 0XCB
  7139.                 RLCA               ; double the value and set flags.
  7140.         JR      Z,L18F3         ; to OUT-C-1 if still zero ('KLC').
  7141.  
  7142.         ADD     A,$43           ; add 'C' - will become 'E' if originally 1
  7143.                                 ; or 'G' if originally 2.
  7144.         JR      L1909           ; forward to OUT-C-2 to print.
  7145.  
  7146. ; ---
  7147.  
  7148. ; If mode was zero then, while printing a BASIC line, bit 2 of flags has been
  7149. ; set if 'THEN' or ':' was encountered as a main character and reset otherwise.
  7150. ; This is now used to determine if the 'K' cursor is to be printed but this
  7151. ; transient state is also now transferred permanently to bit 3 of FLAGS
  7152. ; to let the interrupt routine know how to decode the next key.
  7153.  
  7154. ;; OUT-C-1
  7155. L18F3:  LD      HL,$5C3B        ; Address FLAGS
  7156.         RES     3,(HL)          ; signal 'K' mode initially.
  7157.         LD      A,$4B           ; prepare letter 'K'.
  7158.         BIT     2,(HL)          ; test FLAGS - was the
  7159.                                 ; previous main character ':' or 'THEN' ?
  7160.         JR      Z,L1909         ; forward to OUT-C-2 if so to print.
  7161.  
  7162.         SET     3,(HL)          ; signal 'L' mode to interrupt routine.
  7163.                                 ; Note. transient bit has been made permanent.
  7164.         INC     A               ; augment from 'K' to 'L'.
  7165.  
  7166.         BIT     3,(IY+$30)      ; test FLAGS2 - consider caps lock ?
  7167.                                 ; which is maintained by KEY-INPUT.
  7168.         JR      Z,L1909         ; forward to OUT-C-2 if not set to print.
  7169.  
  7170.         LD      A,$43           ; alter 'L' to 'C'.
  7171.  
  7172. ;; OUT-C-2
  7173. L1909:  PUSH    DE              ; save address pointer but OK as OUT-FLASH
  7174.                                 ; uses alternate set without RST 10.
  7175.  
  7176.         CALL    L18C1           ; routine OUT-FLASH to print.
  7177.  
  7178.         POP     DE              ; restore and
  7179.         RET                     ; return.
  7180.  
  7181. ; ----------------------------
  7182. ; Get line number of next line
  7183. ; ----------------------------
  7184. ; These two subroutines are called while editing.
  7185. ; This entry point is from ED-DOWN with HL addressing E_PPC
  7186. ; to fetch the next line number.
  7187. ; Also from AUTO-LIST with HL addressing S_TOP just to update S_TOP
  7188. ; with the value of the next line number. It gets fetched but is discarded.
  7189. ; These routines never get called while the editor is being used for input.
  7190.  
  7191. ;; LN-FETCH
  7192. L190F:  LD      E,(HL)          ; fetch low byte
  7193.         INC     HL              ; address next
  7194.         LD      D,(HL)          ; fetch high byte.
  7195.         PUSH    HL              ; save system variable hi pointer.
  7196.         EX      DE,HL           ; line number to HL,
  7197.         INC     HL              ; increment as a starting point.
  7198.         CALL    L196E           ; routine LINE-ADDR gets address in HL.
  7199.         CALL    L1695           ; routine LINE-NO gets line number in DE.
  7200.         POP     HL              ; restore system variable hi pointer.
  7201.  
  7202. ; This entry point is from the ED-UP with HL addressing E_PPC_hi
  7203.  
  7204. ;; LN-STORE
  7205. L191C:  BIT     5,(IY+$37)      ; test FLAGX - input mode ?
  7206.         RET     NZ              ; return if so.
  7207.                                 ; Note. above already checked by ED-UP/ED-DOWN.
  7208.  
  7209.         LD      (HL),D          ; save high byte of line number.
  7210.         DEC     HL              ; address lower
  7211.         LD      (HL),E          ; save low byte of line number.
  7212.         RET                     ; return.
  7213.  
  7214. ; -----------------------------------------
  7215. ; Outputting numbers at start of BASIC line
  7216. ; -----------------------------------------
  7217. ; This routine entered at OUT-SP-NO is used to compute then output the first
  7218. ; three digits of a 4-digit BASIC line printing a space if necessary.
  7219. ; The line number, or residual part, is held in HL and the BC register
  7220. ; holds a subtraction value -1000, -100 or -10.
  7221. ; Note. for example line number 200 -
  7222. ; space(out_char), 2(out_code), 0(out_char) final number always out-code.
  7223.  
  7224. ;; OUT-SP-2
  7225. L1925:  LD      A,E             ; will be space if OUT-CODE not yet called.
  7226.                                 ; or $FF if spaces are suppressed.
  7227.                                 ; else $30 ('0').
  7228.                                 ; (from the first instruction at OUT-CODE)
  7229.                                 ; this guy is just too clever.
  7230.         AND     A               ; test bit 7 of A.
  7231.         RET     M               ; return if $FF, as leading spaces not
  7232.                                 ; required. This is set when printing line
  7233.                                 ; number and statement in MAIN-5.
  7234.  
  7235.         JR      L1937           ; forward to exit via OUT-CHAR.
  7236.  
  7237. ; ---
  7238.  
  7239. ; -> the single entry point.
  7240.  
  7241. ;; OUT-SP-NO
  7242. L192A:  XOR     A               ; initialize digit to 0
  7243.  
  7244. ;; OUT-SP-1
  7245. L192B:  ADD     HL,BC           ; add negative number to HL.
  7246.         INC     A               ; increment digit
  7247.         JR      C,L192B         ; back to OUT-SP-1 until no carry from
  7248.                                 ; the addition.
  7249.  
  7250.         SBC     HL,BC           ; cancel the last addition
  7251.         DEC     A               ; and decrement the digit.
  7252.         JR      Z,L1925         ; back to OUT-SP-2 if it is zero.
  7253.  
  7254.         JP      L15EF           ; jump back to exit via OUT-CODE.    ->
  7255.  
  7256.  
  7257. ; -------------------------------------
  7258. ; Outputting characters in a BASIC line
  7259. ; -------------------------------------
  7260. ; This subroutine ...
  7261.  
  7262. ;; OUT-CHAR
  7263. L1937:  CALL    L2D1B           ; routine NUMERIC tests if it is a digit ?
  7264.         JR      NC,L196C        ; to OUT-CH-3 to print digit without
  7265.                                 ; changing mode. Will be 'K' mode if digits
  7266.                                 ; are at beginning of edit line.
  7267.  
  7268.         CP      $21             ; less than quote character ?
  7269.         JR      C,L196C         ; to OUT-CH-3 to output controls and space.
  7270.  
  7271.         RES     2,(IY+$01)      ; initialize FLAGS to 'K' mode and leave
  7272.                                 ; unchanged if this character would precede
  7273.                                 ; a keyword.
  7274.  
  7275.         CP      $CB             ; is character 'THEN' token ?
  7276.         JR      Z,L196C         ; to OUT-CH-3 to output if so.
  7277.  
  7278.         CP      $3A             ; is it ':' ?
  7279.         JR      NZ,L195A        ; to OUT-CH-1 if not statement separator
  7280.                                 ; to change mode back to 'L'.
  7281.  
  7282.         BIT     5,(IY+$37)      ; FLAGX  - Input Mode ??
  7283.         JR      NZ,L1968        ; to OUT-CH-2 if in input as no statements.
  7284.                                 ; Note. this check should seemingly be at
  7285.                                 ; the start. Commands seem inappropriate in
  7286.                                 ; INPUT mode and are rejected by the syntax
  7287.                                 ; checker anyway.
  7288.                                 ; unless INPUT LINE is being used.
  7289.  
  7290.         BIT     2,(IY+$30)      ; test FLAGS2 - is the ':' within quotes ?
  7291.         JR      Z,L196C         ; to OUT-CH-3 if ':' is outside quoted text.
  7292.  
  7293.         JR      L1968           ; to OUT-CH-2 as ':' is within quotes
  7294.  
  7295. ; ---
  7296.  
  7297. ;; OUT-CH-1
  7298. L195A:  CP      $22             ; is it quote character '"'  ?
  7299.         JR      NZ,L1968        ; to OUT-CH-2 with others to set 'L' mode.
  7300.  
  7301.         PUSH    AF              ; save character.
  7302.         LD      A,($5C6A)       ; fetch FLAGS2.
  7303.         XOR     $04             ; toggle the quotes flag.
  7304.         LD      ($5C6A),A       ; update FLAGS2
  7305.         POP     AF              ; and restore character.
  7306.  
  7307. ;; OUT-CH-2
  7308. L1968:  SET     2,(IY+$01)      ; update FLAGS - signal L mode if the cursor
  7309.                                 ; is next.
  7310.  
  7311. ;; OUT-CH-3
  7312. L196C:  RST     10H             ; PRINT-A vectors the character to
  7313.                                 ; channel 'S', 'K', 'R' or 'P'.
  7314.         RET                     ; return.
  7315.  
  7316. ; -------------------------------------------
  7317. ; Get starting address of line, or line after
  7318. ; -------------------------------------------
  7319. ; This routine is used often to get the address, in HL, of a BASIC line
  7320. ; number supplied in HL, or failing that the address of the following line
  7321. ; and the address of the previous line in DE.
  7322.  
  7323. ;; LINE-ADDR
  7324. L196E:  PUSH    HL              ; save line number in HL register
  7325.         LD      HL,($5C53)      ; fetch start of program from PROG
  7326.         LD      D,H             ; transfer address to
  7327.         LD      E,L             ; the DE register pair.
  7328.  
  7329. ;; LINE-AD-1
  7330. L1974:  POP     BC              ; restore the line number to BC
  7331.         CALL    L1980           ; routine CP-LINES compares with that
  7332.                                 ; addressed by HL
  7333.         RET     NC              ; return if line has been passed or matched.
  7334.                                 ; if NZ, address of previous is in DE
  7335.  
  7336.         PUSH    BC              ; save the current line number
  7337.         CALL    L19B8           ; routine NEXT-ONE finds address of next
  7338.                                 ; line number in DE, previous in HL.
  7339.         EX      DE,HL           ; switch so next in HL
  7340.         JR      L1974           ; back to LINE-AD-1 for another comparison
  7341.  
  7342. ; --------------------
  7343. ; Compare line numbers
  7344. ; --------------------
  7345. ; This routine compares a line number supplied in BC with an addressed
  7346. ; line number pointed to by HL.
  7347.  
  7348. ;; CP-LINES
  7349. L1980:  LD      A,(HL)          ; Load the high byte of line number and
  7350.         CP      B               ; compare with that of supplied line number.
  7351.         RET     NZ              ; return if yet to match (carry will be set).
  7352.  
  7353.         INC     HL              ; address low byte of
  7354.         LD      A,(HL)          ; number and pick up in A.
  7355.         DEC     HL              ; step back to first position.
  7356.         CP      C               ; now compare.
  7357.         RET                     ; zero set if exact match.
  7358.                                 ; carry set if yet to match.
  7359.                                 ; no carry indicates a match or
  7360.                                 ; next available BASIC line or
  7361.                                 ; program end marker.
  7362.  
  7363. ; -------------------
  7364. ; Find each statement
  7365. ; -------------------
  7366. ; The single entry point EACH-STMT is used to
  7367. ; 1) To find the D'th statement in a line.
  7368. ; 2) To find a token in held E.
  7369.  
  7370. ;; not-used
  7371. L1988:  INC     HL              ;
  7372.         INC     HL              ;
  7373.         INC     HL              ;
  7374.  
  7375. ; -> entry point.
  7376.  
  7377. ;; EACH-STMT
  7378. L198B:  LD      ($5C5D),HL      ; save HL in CH_ADD
  7379.         LD      C,$00           ; initialize quotes flag
  7380.  
  7381. ;; EACH-S-1
  7382. L1990:  DEC     D               ; decrease statement count
  7383.         RET     Z               ; return if zero
  7384.  
  7385.  
  7386.         RST     20H             ; NEXT-CHAR
  7387.         CP      E               ; is it the search token ?
  7388.         JR      NZ,L199A        ; forward to EACH-S-3 if not
  7389.  
  7390.         AND     A               ; clear carry
  7391.         RET                     ; return signalling success.
  7392.  
  7393. ; ---
  7394.  
  7395. ;; EACH-S-2
  7396. L1998:  INC     HL              ; next address
  7397.         LD      A,(HL)          ; next character
  7398.  
  7399. ;; EACH-S-3
  7400. L199A:  CALL    L18B6           ; routine NUMBER skips if number marker
  7401.         LD      ($5C5D),HL      ; save in CH_ADD
  7402.         CP      $22             ; is it quotes '"' ?
  7403.         JR      NZ,L19A5        ; to EACH-S-4 if not
  7404.  
  7405.         DEC     C               ; toggle bit 0 of C
  7406.  
  7407. ;; EACH-S-4
  7408. L19A5:  CP      $3A             ; is it ':'
  7409.         JR      Z,L19AD         ; to EACH-S-5
  7410.  
  7411.         CP      $CB             ; 'THEN'
  7412.         JR      NZ,L19B1        ; to EACH-S-6
  7413.  
  7414. ;; EACH-S-5
  7415. L19AD:  BIT     0,C             ; is it in quotes
  7416.         JR      Z,L1990         ; to EACH-S-1 if not
  7417.  
  7418. ;; EACH-S-6
  7419. L19B1:  CP      $0D             ; end of line ?
  7420.         JR      NZ,L1998        ; to EACH-S-2
  7421.  
  7422.         DEC     D               ; decrease the statement counter
  7423.                                 ; which should be zero else
  7424.                                 ; 'Statement Lost'.
  7425.         SCF                     ; set carry flag - not found
  7426.         RET                     ; return
  7427.  
  7428. ; -----------------------------------------------------------------------
  7429. ; Storage of variables. For full details - see chapter 24.
  7430. ; ZX Spectrum BASIC Programming by Steven Vickers 1982.
  7431. ; It is bits 7-5 of the first character of a variable that allow
  7432. ; the six types to be distinguished. Bits 4-0 are the reduced letter.
  7433. ; So any variable name is higher that $3F and can be distinguished
  7434. ; also from the variables area end-marker $80.
  7435. ;
  7436. ; 76543210 meaning                               brief outline of format.
  7437. ; -------- ------------------------              -----------------------
  7438. ; 010      string variable.                      2 byte length + contents.
  7439. ; 110      string array.                         2 byte length + contents.
  7440. ; 100      array of numbers.                     2 byte length + contents.
  7441. ; 011      simple numeric variable.              5 bytes.
  7442. ; 101      variable length named numeric.        5 bytes.
  7443. ; 111      for-next loop variable.               18 bytes.
  7444. ; 10000000 the variables area end-marker.
  7445. ;
  7446. ; Note. any of the above seven will serve as a program end-marker.
  7447. ;
  7448. ; -----------------------------------------------------------------------
  7449.  
  7450. ; ------------
  7451. ; Get next one
  7452. ; ------------
  7453. ; This versatile routine is used to find the address of the next line
  7454. ; in the program area or the next variable in the variables area.
  7455. ; The reason one routine is made to handle two apparently unrelated tasks
  7456. ; is that it can be called indiscriminately when merging a line or a
  7457. ; variable.
  7458.  
  7459. ;; NEXT-ONE
  7460. L19B8:  PUSH    HL              ; save the pointer address.
  7461.         LD      A,(HL)          ; get first byte.
  7462.         CP      $40             ; compare with upper limit for line numbers.
  7463.         JR      C,L19D5         ; forward to NEXT-O-3 if within BASIC area.
  7464.  
  7465. ; the continuation here is for the next variable unless the supplied
  7466. ; line number was erroneously over 16383. see RESTORE command.
  7467.  
  7468.         BIT     5,A             ; is it a string or an array variable ?
  7469.         JR      Z,L19D6         ; forward to NEXT-O-4 to compute length.
  7470.  
  7471.         ADD     A,A             ; test bit 6 for single-character variables.
  7472.         JP      M,L19C7         ; forward to NEXT-O-1 if so
  7473.  
  7474.         CCF                     ; clear the carry for long-named variables.
  7475.                                 ; it remains set for for-next loop variables.
  7476.  
  7477. ;; NEXT-O-1
  7478. L19C7:  LD      BC,$0005        ; set BC to 5 for floating point number
  7479.         JR      NC,L19CE        ; forward to NEXT-O-2 if not a for/next
  7480.                                 ; variable.
  7481.  
  7482.         LD      C,$12           ; set BC to eighteen locations.
  7483.                                 ; value, limit, step, line and statement.
  7484.  
  7485. ; now deal with long-named variables
  7486.  
  7487. ;; NEXT-O-2
  7488. L19CE:  RLA                     ; test if character inverted. carry will also
  7489.                                 ; be set for single character variables
  7490.         INC     HL              ; address next location.
  7491.         LD      A,(HL)          ; and load character.
  7492.         JR      NC,L19CE        ; back to NEXT-O-2 if not inverted bit.
  7493.                                 ; forward immediately with single character
  7494.                                 ; variable names.
  7495.  
  7496.         JR      L19DB           ; forward to NEXT-O-5 to add length of
  7497.                                 ; floating point number(s etc.).
  7498.  
  7499. ; ---
  7500.  
  7501. ; this branch is for line numbers.
  7502.  
  7503. ;; NEXT-O-3
  7504. L19D5:  INC     HL              ; increment pointer to low byte of line no.
  7505.  
  7506. ; strings and arrays rejoin here
  7507.  
  7508. ;; NEXT-O-4
  7509. L19D6:  INC     HL              ; increment to address the length low byte.
  7510.         LD      C,(HL)          ; transfer to C and
  7511.         INC     HL              ; point to high byte of length.
  7512.         LD      B,(HL)          ; transfer that to B
  7513.         INC     HL              ; point to start of BASIC/variable contents.
  7514.  
  7515. ; the three types of numeric variables rejoin here
  7516.  
  7517. ;; NEXT-O-5
  7518. L19DB:  ADD     HL,BC           ; add the length to give address of next
  7519.                                 ; line/variable in HL.
  7520.         POP     DE              ; restore previous address to DE.
  7521.  
  7522. ; ------------------
  7523. ; Difference routine
  7524. ; ------------------
  7525. ; This routine terminates the above routine and is also called from the
  7526. ; start of the next routine to calculate the length to reclaim.
  7527.  
  7528. ;; DIFFER
  7529. L19DD:  AND     A               ; prepare for true subtraction.
  7530.         SBC     HL,DE           ; subtract the two pointers.
  7531.         LD      B,H             ; transfer result
  7532.         LD      C,L             ; to BC register pair.
  7533.         ADD     HL,DE           ; add back
  7534.         EX      DE,HL           ; and switch pointers
  7535.         RET                     ; return values are the length of area in BC,
  7536.                                 ; low pointer (previous) in HL,
  7537.                                 ; high pointer (next) in DE.
  7538.  
  7539. ; -----------------------
  7540. ; Handle reclaiming space
  7541. ; -----------------------
  7542. ;
  7543.  
  7544. ;; RECLAIM-1
  7545. L19E5:  CALL    L19DD           ; routine DIFFER immediately above
  7546.  
  7547. ;; RECLAIM-2
  7548. L19E8:  PUSH    BC              ;
  7549.  
  7550.         LD      A,B             ;
  7551.         CPL                     ;
  7552.         LD      B,A             ;
  7553.         LD      A,C             ;
  7554.         CPL                     ;
  7555.         LD      C,A             ;
  7556.         INC     BC              ;
  7557.  
  7558.         CALL    L1664           ; routine POINTERS
  7559.         EX      DE,HL           ;
  7560.         POP     HL              ;
  7561.  
  7562.         ADD     HL,DE           ;
  7563.         PUSH    DE              ;
  7564.         LDIR                    ; copy bytes
  7565.  
  7566.         POP     HL              ;
  7567.         RET                     ;
  7568.  
  7569. ; ----------------------------------------
  7570. ; Read line number of line in editing area
  7571. ; ----------------------------------------
  7572. ; This routine reads a line number in the editing area returning the number
  7573. ; in the BC register or zero if no digits exist before commands.
  7574. ; It is called from LINE-SCAN to check the syntax of the digits.
  7575. ; It is called from MAIN-3 to extract the line number in preparation for
  7576. ; inclusion of the line in the BASIC program area.
  7577. ;
  7578. ; Interestingly the calculator stack is moved from its normal place at the
  7579. ; end of dynamic memory to an adequate area within the system variables area.
  7580. ; This ensures that in a low memory situation, that valid line numbers can
  7581. ; be extracted without raising an error and that memory can be reclaimed
  7582. ; by deleting lines. If the stack was in its normal place then a situation
  7583. ; arises whereby the Spectrum becomes locked with no means of reclaiming space.
  7584.  
  7585. ;; E-LINE-NO
  7586. L19FB:  LD      HL,($5C59)      ; load HL from system variable E_LINE.
  7587.  
  7588.         DEC     HL              ; decrease so that NEXT_CHAR can be used
  7589.                                 ; without skipping the first digit.
  7590.  
  7591.         LD      ($5C5D),HL      ; store in the system variable CH_ADD.
  7592.  
  7593.         RST     20H             ; NEXT-CHAR skips any noise and white-space
  7594.                                 ; to point exactly at the first digit.
  7595.  
  7596.         LD      HL,$5C92        ; use MEM-0 as a temporary calculator stack
  7597.                                 ; an overhead of three locations are needed.
  7598.         LD      ($5C65),HL      ; set new STKEND.
  7599.  
  7600.         CALL    L2D3B           ; routine INT-TO-FP will read digits till
  7601.                                 ; a non-digit found.
  7602.         CALL    L2DA2           ; routine FP-TO-BC will retrieve number
  7603.                                 ; from stack at membot.
  7604.         JR      C,L1A15         ; forward to E-L-1 if overflow i.e. > 65535.
  7605.                                 ; 'Nonsense in BASIC'
  7606.  
  7607.         LD      HL,$D8F0        ; load HL with value -9999
  7608.         ADD     HL,BC           ; add to line number in BC
  7609.  
  7610. ;; E-L-1
  7611. L1A15:  JP      C,L1C8A         ; to REPORT-C 'Nonsense in BASIC' if over.
  7612.                                 ; Note. As ERR_SP points to ED_ERROR
  7613.                                 ; the report is never produced although
  7614.                                 ; the RST $08 will update X_PTR leading to
  7615.                                 ; the error marker being displayed when
  7616.                                 ; the ED_LOOP is reiterated.
  7617.                                 ; in fact, since it is immediately
  7618.                                 ; cancelled, any report will do.
  7619.  
  7620. ; a line in the range 0 - 9999 has been entered.
  7621.  
  7622.         JP      L16C5           ; jump back to SET-STK to set the calculator
  7623.                                 ; stack back to its normal place and exit
  7624.                                 ; from there.
  7625.  
  7626. ; ---------------------------------
  7627. ; Report and line number outputting
  7628. ; ---------------------------------
  7629. ; Entry point OUT-NUM-1 is used by the Error Reporting code to print
  7630. ; the line number and later the statement number held in BC.
  7631. ; If the statement was part of a direct command then -2 is used as a
  7632. ; dummy line number so that zero will be printed in the report.
  7633. ; This routine is also used to print the exponent of E-format numbers.
  7634. ;
  7635. ; Entry point OUT-NUM-2 is used from OUT-LINE to output the line number
  7636. ; addressed by HL with leading spaces if necessary.
  7637.  
  7638. ;; OUT-NUM-1
  7639. L1A1B:  PUSH    DE              ; save the
  7640.         PUSH    HL              ; registers.
  7641.         XOR     A               ; set A to zero.
  7642.         BIT     7,B             ; is the line number minus two ?
  7643.         JR      NZ,L1A42        ; forward to OUT-NUM-4 if so to print zero
  7644.                                 ; for a direct command.
  7645.  
  7646.         LD      H,B             ; transfer the
  7647.         LD      L,C             ; number to HL.
  7648.         LD      E,$FF           ; signal 'no leading zeros'.
  7649.         JR      L1A30           ; forward to continue at OUT-NUM-3
  7650.  
  7651. ; ---
  7652.  
  7653. ; from OUT-LINE - HL addresses line number.
  7654.  
  7655. ;; OUT-NUM-2
  7656. L1A28:  PUSH    DE              ; save flags
  7657.         LD      D,(HL)          ; high byte to D
  7658.         INC     HL              ; address next
  7659.         LD      E,(HL)          ; low byte to E
  7660.         PUSH    HL              ; save pointer
  7661.         EX      DE,HL           ; transfer number to HL
  7662.         LD      E,$20           ; signal 'output leading spaces'
  7663.  
  7664. ;; OUT-NUM-3
  7665. L1A30:  LD      BC,$FC18        ; value -1000
  7666.         CALL    L192A           ; routine OUT-SP-NO outputs space or number
  7667.         LD      BC,$FF9C        ; value -100
  7668.         CALL    L192A           ; routine OUT-SP-NO
  7669.         LD      C,$F6           ; value -10 ( B is still $FF )
  7670.         CALL    L192A           ; routine OUT-SP-NO
  7671.         LD      A,L             ; remainder to A.
  7672.  
  7673. ;; OUT-NUM-4
  7674. L1A42:  CALL    L15EF           ; routine OUT-CODE for final digit.
  7675.                                 ; else report code zero wouldn't get
  7676.                                 ; printed.
  7677.         POP     HL              ; restore the
  7678.         POP     DE              ; registers and
  7679.         RET                     ; return.
  7680.  
  7681.  
  7682. ;***************************************************
  7683. ;** Part 7. BASIC LINE AND COMMAND INTERPRETATION **
  7684. ;***************************************************
  7685.  
  7686. ; ----------------
  7687. ; The offset table
  7688. ; ----------------
  7689. ; The BASIC interpreter has found a command code $CE - $FF
  7690. ; which is then reduced to range $00 - $31 and added to the base address
  7691. ; of this table to give the address of an offset which, when added to
  7692. ; the offset therein, gives the location in the following parameter table
  7693. ; where a list of class codes, separators and addresses relevant to the
  7694. ; command exists.
  7695.  
  7696. ;; offst-tbl
  7697. L1A48:  DB    L1AF9 - $       ; B1 offset to Address: P-DEF-FN
  7698.         DB    L1B14 - $       ; CB offset to Address: P-CAT
  7699.         DB    L1B06 - $       ; BC offset to Address: P-FORMAT
  7700.         DB    L1B0A - $       ; BF offset to Address: P-MOVE
  7701.         DB    L1B10 - $       ; C4 offset to Address: P-ERASE
  7702.         DB    L1AFC - $       ; AF offset to Address: P-OPEN
  7703.         DB    L1B02 - $       ; B4 offset to Address: P-CLOSE
  7704.         DB    L1AE2 - $       ; 93 offset to Address: P-MERGE
  7705.         DB    L1AE1 - $       ; 91 offset to Address: P-VERIFY
  7706.         DB    L1AE3 - $       ; 92 offset to Address: P-BEEP
  7707.         DB    L1AE7 - $       ; 95 offset to Address: P-CIRCLE
  7708.         DB    L1AEB - $       ; 98 offset to Address: P-INK
  7709.         DB    L1AEC - $       ; 98 offset to Address: P-PAPER
  7710.         DB    L1AED - $       ; 98 offset to Address: P-FLASH
  7711.         DB    L1AEE - $       ; 98 offset to Address: P-BRIGHT
  7712.         DB    L1AEF - $       ; 98 offset to Address: P-INVERSE
  7713.         DB    L1AF0 - $       ; 98 offset to Address: P-OVER
  7714.         DB    L1AF1 - $       ; 98 offset to Address: P-OUT
  7715.         DB    L1AD9 - $       ; 7F offset to Address: P-LPRINT
  7716.         DB    L1ADC - $       ; 81 offset to Address: P-LLIST
  7717.         DB    L1A8A - $       ; 2E offset to Address: P-STOP
  7718.         DB    L1AC9 - $       ; 6C offset to Address: P-READ
  7719.         DB    L1ACC - $       ; 6E offset to Address: P-DATA
  7720.         DB    L1ACF - $       ; 70 offset to Address: P-RESTORE
  7721.         DB    L1AA8 - $       ; 48 offset to Address: P-NEW
  7722.         DB    L1AF5 - $       ; 94 offset to Address: P-BORDER
  7723.         DB    L1AB8 - $       ; 56 offset to Address: P-CONT
  7724.         DB    L1AA2 - $       ; 3F offset to Address: P-DIM
  7725.         DB    L1AA5 - $       ; 41 offset to Address: P-REM
  7726.         DB    L1A90 - $       ; 2B offset to Address: P-FOR
  7727.         DB    L1A7D - $       ; 17 offset to Address: P-GO-TO
  7728.         DB    L1A86 - $       ; 1F offset to Address: P-GO-SUB
  7729.         DB    L1A9F - $       ; 37 offset to Address: P-INPUT
  7730.         DB    L1AE0 - $       ; 77 offset to Address: P-LOAD
  7731.         DB    L1AAE - $       ; 44 offset to Address: P-LIST
  7732.         DB    L1A7A - $       ; 0F offset to Address: P-LET
  7733.         DB    L1AC5 - $       ; 59 offset to Address: P-PAUSE
  7734.         DB    L1A98 - $       ; 2B offset to Address: P-NEXT
  7735.         DB    L1AB1 - $       ; 43 offset to Address: P-POKE
  7736.         DB    L1A9C - $       ; 2D offset to Address: P-PRINT
  7737.         DB    L1AC1 - $       ; 51 offset to Address: P-PLOT
  7738.         DB    L1AAB - $       ; 3A offset to Address: P-RUN
  7739.         DB    L1ADF - $       ; 6D offset to Address: P-SAVE
  7740.         DB    L1AB5 - $       ; 42 offset to Address: P-RANDOM
  7741.         DB    L1A81 - $       ; 0D offset to Address: P-IF
  7742.         DB    L1ABE - $       ; 49 offset to Address: P-CLS
  7743.         DB    L1AD2 - $       ; 5C offset to Address: P-DRAW
  7744.         DB    L1ABB - $       ; 44 offset to Address: P-CLEAR
  7745.         DB    L1A8D - $       ; 15 offset to Address: P-RETURN
  7746.         DB    L1AD6 - $       ; 5D offset to Address: P-COPY
  7747.  
  7748.  
  7749. ; -------------------------------
  7750. ; The parameter or "Syntax" table
  7751. ; -------------------------------
  7752. ; For each command there exists a variable list of parameters.
  7753. ; If the character is greater than a space it is a required separator.
  7754. ; If less, then it is a command class in the range 00 - 0B.
  7755. ; Note that classes 00, 03 and 05 will fetch the addresses from this table.
  7756. ; Some classes e.g. 07 and 0B have the same address in all invocations
  7757. ; and the command is re-computed from the low-byte of the parameter address.
  7758. ; Some e.g. 02 are only called once so a call to the command is made from
  7759. ; within the class routine rather than holding the address within the table.
  7760. ; Some class routines check syntax entirely and some leave this task for the
  7761. ; command itself.
  7762. ; Others for example CIRCLE (x,y,z) check the first part (x,y) using the
  7763. ; class routine and the final part (,z) within the command.
  7764. ; The last few commands appear to have been added in a rush but their syntax
  7765. ; is rather simple e.g. MOVE "M1","M2"
  7766.  
  7767. ;; P-LET
  7768. L1A7A:  DB    $01             ; Class-01 - A variable is required.
  7769.         DB    $3D             ; Separator:  '='
  7770.         DB    $02             ; Class-02 - An expression, numeric or string,
  7771.                                 ; must follow.
  7772.  
  7773. ;; P-GO-TO
  7774. L1A7D:  DB    $06             ; Class-06 - A numeric expression must follow.
  7775.         DB    $00             ; Class-00 - No further operands.
  7776.         DEFW    L1E67           ; Address: $1E67; Address: GO-TO
  7777.  
  7778. ;; P-IF
  7779. L1A81:  DB    $06             ; Class-06 - A numeric expression must follow.
  7780.         DB    $CB             ; Separator:  'THEN'
  7781.         DB    $05             ; Class-05 - Variable syntax checked
  7782.                                 ; by routine.
  7783.         DEFW    L1CF0           ; Address: $1CF0; Address: IF
  7784.  
  7785. ;; P-GO-SUB
  7786. L1A86:  DB    $06             ; Class-06 - A numeric expression must follow.
  7787.         DB    $00             ; Class-00 - No further operands.
  7788.         DEFW    L1EED           ; Address: $1EED; Address: GO-SUB
  7789.  
  7790. ;; P-STOP
  7791. L1A8A:  DB    $00             ; Class-00 - No further operands.
  7792.         DEFW    L1CEE           ; Address: $1CEE; Address: STOP
  7793.  
  7794. ;; P-RETURN
  7795. L1A8D:  DB    $00             ; Class-00 - No further operands.
  7796.         DEFW    L1F23           ; Address: $1F23; Address: RETURN
  7797.  
  7798. ;; P-FOR
  7799. L1A90:  DB    $04             ; Class-04 - A single character variable must
  7800.                                 ; follow.
  7801.         DB    $3D             ; Separator:  '='
  7802.         DB    $06             ; Class-06 - A numeric expression must follow.
  7803.         DB    $CC             ; Separator:  'TO'
  7804.         DB    $06             ; Class-06 - A numeric expression must follow.
  7805.         DB    $05             ; Class-05 - Variable syntax checked
  7806.                                 ; by routine.
  7807.         DEFW    L1D03           ; Address: $1D03; Address: FOR
  7808.  
  7809. ;; P-NEXT
  7810. L1A98:  DB    $04             ; Class-04 - A single character variable must
  7811.                                 ; follow.
  7812.         DB    $00             ; Class-00 - No further operands.
  7813.         DEFW    L1DAB           ; Address: $1DAB; Address: NEXT
  7814.  
  7815. ;; P-PRINT
  7816. L1A9C:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7817.                                 ; by routine.
  7818.         DEFW    L1FCD           ; Address: $1FCD; Address: PRINT
  7819.  
  7820. ;; P-INPUT
  7821. L1A9F:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7822.                                 ; by routine.
  7823.         DEFW    L2089           ; Address: $2089; Address: INPUT
  7824.  
  7825. ;; P-DIM
  7826. L1AA2:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7827.                                 ; by routine.
  7828.         DEFW    L2C02           ; Address: $2C02; Address: DIM
  7829.  
  7830. ;; P-REM
  7831. L1AA5:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7832.                                 ; by routine.
  7833.         DEFW    L1BB2           ; Address: $1BB2; Address: REM
  7834.  
  7835. ;; P-NEW
  7836. L1AA8:  DB    $00             ; Class-00 - No further operands.
  7837.         DEFW    L11B7           ; Address: $11B7; Address: NEW
  7838.  
  7839. ;; P-RUN
  7840. L1AAB:  DB    $03             ; Class-03 - A numeric expression may follow
  7841.                                 ; else default to zero.
  7842.         DEFW    L1EA1           ; Address: $1EA1; Address: RUN
  7843.  
  7844. ;; P-LIST
  7845. L1AAE:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7846.                                 ; by routine.
  7847.         DEFW    L17F9           ; Address: $17F9; Address: LIST
  7848.  
  7849. ;; P-POKE
  7850. L1AB1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7851.                                 ; expressions required.
  7852.         DB    $00             ; Class-00 - No further operands.
  7853.         DEFW    L1E80           ; Address: $1E80; Address: POKE
  7854.  
  7855. ;; P-RANDOM
  7856. L1AB5:  DB    $03             ; Class-03 - A numeric expression may follow
  7857.                                 ; else default to zero.
  7858.         DEFW    L1E4F           ; Address: $1E4F; Address: RANDOMIZE
  7859.  
  7860. ;; P-CONT
  7861. L1AB8:  DB    $00             ; Class-00 - No further operands.
  7862.         DEFW    L1E5F           ; Address: $1E5F; Address: CONTINUE
  7863.  
  7864. ;; P-CLEAR
  7865. L1ABB:  DB    $03             ; Class-03 - A numeric expression may follow
  7866.                                 ; else default to zero.
  7867.         DEFW    L1EAC           ; Address: $1EAC; Address: CLEAR
  7868.  
  7869. ;; P-CLS
  7870. L1ABE:  DB    $00             ; Class-00 - No further operands.
  7871.         DEFW    L0D6B           ; Address: $0D6B; Address: CLS
  7872.  
  7873. ;; P-PLOT
  7874. L1AC1:  DB    $09             ; Class-09 - Two comma-separated numeric
  7875.                                 ; expressions required with optional colour
  7876.                                 ; items.
  7877.         DB    $00             ; Class-00 - No further operands.
  7878.         DEFW    L22DC           ; Address: $22DC; Address: PLOT
  7879.  
  7880. ;; P-PAUSE
  7881. L1AC5:  DB    $06             ; Class-06 - A numeric expression must follow.
  7882.         DB    $00             ; Class-00 - No further operands.
  7883.         DEFW    L1F3A           ; Address: $1F3A; Address: PAUSE
  7884.  
  7885. ;; P-READ
  7886. L1AC9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7887.                                 ; by routine.
  7888.         DEFW    L1DED           ; Address: $1DED; Address: READ
  7889.  
  7890. ;; P-DATA
  7891. L1ACC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7892.                                 ; by routine.
  7893.         DEFW    L1E27           ; Address: $1E27; Address: DATA
  7894.  
  7895. ;; P-RESTORE
  7896. L1ACF:  DB    $03             ; Class-03 - A numeric expression may follow
  7897.                                 ; else default to zero.
  7898.         DEFW    L1E42           ; Address: $1E42; Address: RESTORE
  7899.  
  7900. ;; P-DRAW
  7901. L1AD2:  DB    $09             ; Class-09 - Two comma-separated numeric
  7902.                                 ; expressions required with optional colour
  7903.                                 ; items.
  7904.         DB    $05             ; Class-05 - Variable syntax checked
  7905.                                 ; by routine.
  7906.         DEFW    L2382           ; Address: $2382; Address: DRAW
  7907.  
  7908. ;; P-COPY
  7909. L1AD6:  DB    $00             ; Class-00 - No further operands.
  7910.         DEFW    L0EAC           ; Address: $0EAC; Address: COPY
  7911.  
  7912. ;; P-LPRINT
  7913. L1AD9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7914.                                 ; by routine.
  7915.         DEFW    L1FC9           ; Address: $1FC9; Address: LPRINT
  7916.  
  7917. ;; P-LLIST
  7918. L1ADC:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7919.                                 ; by routine.
  7920.         DEFW    L17F5           ; Address: $17F5; Address: LLIST
  7921.  
  7922. ;; P-SAVE
  7923. L1ADF:  DB    $0B             ; Class-0B - Offset address converted to tape
  7924.                                 ; command.
  7925.  
  7926. ;; P-LOAD
  7927. L1AE0:  DB    $0B             ; Class-0B - Offset address converted to tape
  7928.                                 ; command.
  7929.  
  7930. ;; P-VERIFY
  7931. L1AE1:  DB    $0B             ; Class-0B - Offset address converted to tape
  7932.                                 ; command.
  7933.  
  7934. ;; P-MERGE
  7935. L1AE2:  DB    $0B             ; Class-0B - Offset address converted to tape
  7936.                                 ; command.
  7937.  
  7938. ;; P-BEEP
  7939. L1AE3:  DB    $08             ; Class-08 - Two comma-separated numeric
  7940.                                 ; expressions required.
  7941.         DB    $00             ; Class-00 - No further operands.
  7942.         DEFW    L03F8           ; Address: $03F8; Address: BEEP
  7943.  
  7944. ;; P-CIRCLE
  7945. L1AE7:  DB    $09             ; Class-09 - Two comma-separated numeric
  7946.                                 ; expressions required with optional colour
  7947.                                 ; items.
  7948.         DB    $05             ; Class-05 - Variable syntax checked
  7949.                                 ; by routine.
  7950.         DEFW    L2320           ; Address: $2320; Address: CIRCLE
  7951.  
  7952. ;; P-INK
  7953. L1AEB:  DB    $07             ; Class-07 - Offset address is converted to
  7954.                                 ; colour code.
  7955.  
  7956. ;; P-PAPER
  7957. L1AEC:  DB    $07             ; Class-07 - Offset address is converted to
  7958.                                 ; colour code.
  7959.  
  7960. ;; P-FLASH
  7961. L1AED:  DB    $07             ; Class-07 - Offset address is converted to
  7962.                                 ; colour code.
  7963.  
  7964. ;; P-BRIGHT
  7965. L1AEE:  DB    $07             ; Class-07 - Offset address is converted to
  7966.                                 ; colour code.
  7967.  
  7968. ;; P-INVERSE
  7969. L1AEF:  DB    $07             ; Class-07 - Offset address is converted to
  7970.                                 ; colour code.
  7971.  
  7972. ;; P-OVER
  7973. L1AF0:  DB    $07             ; Class-07 - Offset address is converted to
  7974.                                 ; colour code.
  7975.  
  7976. ;; P-OUT
  7977. L1AF1:  DB    $08             ; Class-08 - Two comma-separated numeric
  7978.                                 ; expressions required.
  7979.         DB    $00             ; Class-00 - No further operands.
  7980.         DEFW    L1E7A           ; Address: $1E7A; Address: OUT
  7981.  
  7982. ;; P-BORDER
  7983. L1AF5:  DB    $06             ; Class-06 - A numeric expression must follow.
  7984.         DB    $00             ; Class-00 - No further operands.
  7985.         DEFW    L2294           ; Address: $2294; Address: BORDER
  7986.  
  7987. ;; P-DEF-FN
  7988. L1AF9:  DB    $05             ; Class-05 - Variable syntax checked entirely
  7989.                                 ; by routine.
  7990.         DEFW    L1F60           ; Address: $1F60; Address: DEF-FN
  7991.  
  7992. ;; P-OPEN
  7993. L1AFC:  DB    $06             ; Class-06 - A numeric expression must follow.
  7994.         DB    $2C             ; Separator:  ','          see Footnote *
  7995.         DB    $0A             ; Class-0A - A string expression must follow.
  7996.         DB    $00             ; Class-00 - No further operands.
  7997.         DEFW    L1736           ; Address: $1736; Address: OPEN
  7998.  
  7999. ;; P-CLOSE
  8000. L1B02:  DB    $06             ; Class-06 - A numeric expression must follow.
  8001.         DB    $00             ; Class-00 - No further operands.
  8002.         DEFW    L16E5           ; Address: $16E5; Address: CLOSE
  8003.  
  8004. ;; P-FORMAT
  8005. L1B06:  DB    $0A             ; Class-0A - A string expression must follow.
  8006.         DB    $00             ; Class-00 - No further operands.
  8007.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8008.  
  8009. ;; P-MOVE
  8010. L1B0A:  DB    $0A             ; Class-0A - A string expression must follow.
  8011.         DB    $2C             ; Separator:  ','
  8012.         DB    $0A             ; Class-0A - A string expression must follow.
  8013.         DB    $00             ; Class-00 - No further operands.
  8014.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8015.  
  8016. ;; P-ERASE
  8017. L1B10:  DB    $0A             ; Class-0A - A string expression must follow.
  8018.         DB    $00             ; Class-00 - No further operands.
  8019.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8020.  
  8021. ;; P-CAT
  8022. L1B14:  DB    $00             ; Class-00 - No further operands.
  8023.         DEFW    L1793           ; Address: $1793; Address: CAT-ETC
  8024.  
  8025. ; * Note that a comma is required as a separator with the OPEN command
  8026. ; but the Interface 1 programmers relaxed this allowing ';' as an
  8027. ; alternative for their channels creating a confusing mixture of
  8028. ; allowable syntax as it is this ROM which opens or re-opens the
  8029. ; normal channels.
  8030.  
  8031. ; -------------------------------
  8032. ; Main parser (BASIC interpreter)
  8033. ; -------------------------------
  8034. ; This routine is called once from MAIN-2 when the BASIC line is to
  8035. ; be entered or re-entered into the Program area and the syntax
  8036. ; requires checking.
  8037.  
  8038. ;; LINE-SCAN
  8039. L1B17:  RES     7,(IY+$01)      ; update FLAGS - signal checking syntax
  8040.         CALL    L19FB           ; routine E-LINE-NO              >>
  8041.                                 ; fetches the line number if in range.
  8042.  
  8043.         XOR     A               ; clear the accumulator.
  8044.         LD      ($5C47),A       ; set statement number SUBPPC to zero.
  8045.         DEC     A               ; set accumulator to $FF.
  8046.         LD      ($5C3A),A       ; set ERR_NR to 'OK' - 1.
  8047.         JR      L1B29           ; forward to continue at STMT-L-1.
  8048.  
  8049. ; --------------
  8050. ; Statement loop
  8051. ; --------------
  8052. ;
  8053. ;
  8054.  
  8055. ;; STMT-LOOP
  8056. L1B28:  RST     20H             ; NEXT-CHAR
  8057.  
  8058. ; -> the entry point from above or LINE-RUN
  8059. ;; STMT-L-1
  8060. L1B29:  CALL    L16BF           ; routine SET-WORK clears workspace etc.
  8061.  
  8062.         INC     (IY+$0D)        ; increment statement number SUBPPC
  8063.         JP      M,L1C8A         ; to REPORT-C to raise
  8064.                                 ; 'Nonsense in BASIC' if over 127.
  8065.  
  8066.         RST     18H             ; GET-CHAR
  8067.  
  8068.         LD      B,$00           ; set B to zero for later indexing.
  8069.                                 ; early so any other reason ???
  8070.  
  8071.         CP      $0D             ; is character carriage return ?
  8072.                                 ; i.e. an empty statement.
  8073.         JR      Z,L1BB3         ; forward to LINE-END if so.
  8074.  
  8075.         CP      $3A             ; is it statement end marker ':' ?
  8076.                                 ; i.e. another type of empty statement.
  8077.         JR      Z,L1B28         ; back to STMT-LOOP if so.
  8078.  
  8079.         LD      HL,L1B76        ; address: STMT-RET
  8080.         PUSH    HL              ; is now pushed as a return address
  8081.         LD      C,A             ; transfer the current character to C.
  8082.  
  8083. ; advance CH_ADD to a position after command and test if it is a command.
  8084.  
  8085.         RST     20H             ; NEXT-CHAR to advance pointer
  8086.         LD      A,C             ; restore current character
  8087.         SUB     $CE             ; subtract 'DEF FN' - first command
  8088.         JP      C,L1C8A         ; jump to REPORT-C if less than a command
  8089.                                 ; raising
  8090.                                 ; 'Nonsense in BASIC'
  8091.  
  8092.         LD      C,A             ; put the valid command code back in C.
  8093.                                 ; register B is zero.
  8094.         LD      HL,L1A48        ; address: offst-tbl
  8095.         ADD     HL,BC           ; index into table with one of 50 commands.
  8096.         LD      C,(HL)          ; pick up displacement to syntax table entry.
  8097.         ADD     HL,BC           ; add to address the relevant entry.
  8098.         JR      L1B55           ; forward to continue at GET-PARAM
  8099.  
  8100. ; ----------------------
  8101. ; The main scanning loop
  8102. ; ----------------------
  8103. ; not documented properly
  8104. ;
  8105.  
  8106. ;; SCAN-LOOP
  8107. L1B52:  LD      HL,($5C74)      ; fetch temporary address from T_ADDR
  8108.                                 ; during subsequent loops.
  8109.  
  8110. ; -> the initial entry point with HL addressing start of syntax table entry.
  8111.  
  8112. ;; GET-PARAM
  8113. L1B55:  LD      A,(HL)          ; pick up the parameter.
  8114.         INC     HL              ; address next one.
  8115.         LD      ($5C74),HL      ; save pointer in system variable T_ADDR
  8116.  
  8117.         LD      BC,L1B52        ; address: SCAN-LOOP
  8118.         PUSH    BC              ; is now pushed on stack as looping address.
  8119.         LD      C,A             ; store parameter in C.
  8120.         CP      $20             ; is it greater than ' '  ?
  8121.         JR      NC,L1B6F        ; forward to SEPARATOR to check that correct
  8122.                                 ; separator appears in statement if so.
  8123.  
  8124.         LD      HL,L1C01        ; address: class-tbl.
  8125.         LD      B,$00           ; prepare to index into the class table.
  8126.         ADD     HL,BC           ; index to find displacement to routine.
  8127.         LD      C,(HL)          ; displacement to BC
  8128.         ADD     HL,BC           ; add to address the CLASS routine.
  8129.         PUSH    HL              ; push the address on the stack.
  8130.  
  8131.         RST     18H             ; GET-CHAR - HL points to place in statement.
  8132.  
  8133.         DEC     B               ; reset the zero flag - the initial state
  8134.                                 ; for all class routines.
  8135.  
  8136.         RET                     ; and make an indirect jump to routine
  8137.                                 ; and then SCAN-LOOP (also on stack).
  8138.  
  8139. ; Note. one of the class routines will eventually drop the return address
  8140. ; off the stack breaking out of the above seemingly endless loop.
  8141.  
  8142. ; ----------------
  8143. ; Verify separator
  8144. ; ----------------
  8145. ; This routine is called once to verify that the mandatory separator
  8146. ; present in the parameter table is also present in the correct
  8147. ; location following the command. For example, the 'THEN' token after
  8148. ; the 'IF' token and expression.
  8149.  
  8150. ;; SEPARATOR
  8151. L1B6F:  RST     18H             ; GET-CHAR
  8152.         CP      C               ; does it match the character in C ?
  8153.         JP      NZ,L1C8A        ; jump forward to REPORT-C if not
  8154.                                 ; 'Nonsense in BASIC'.
  8155.  
  8156.         RST     20H             ; NEXT-CHAR advance to next character
  8157.         RET                     ; return.
  8158.  
  8159. ; ------------------------------
  8160. ; Come here after interpretation
  8161. ; ------------------------------
  8162. ;
  8163. ;
  8164.  
  8165. ;; STMT-RET
  8166. L1B76:  CALL    L1F54           ; routine BREAK-KEY is tested after every
  8167.                                 ; statement.
  8168.         JR      C,L1B7D         ; step forward to STMT-R-1 if not pressed.
  8169.  
  8170. ;; REPORT-L
  8171. L1B7B:  RST     08H             ; ERROR-1
  8172.         DB    $14             ; Error Report: BREAK into program
  8173.  
  8174. ;; STMT-R-1
  8175. L1B7D           IF BAS48_ONLY=1
  8176.                 BIT 7,(IY+0X0A)
  8177.                 ELSE
  8178.                 CALL L3B4D              ; Spectrum 128 patch
  8179.                 NOP
  8180.                 ENDIF
  8181.  
  8182. L1B81:  JR      NZ,L1BF4        ; forward to STMT-NEXT if a program line.
  8183.  
  8184.         LD      HL,($5C42)      ; fetch line number from NEWPPC
  8185.         BIT     7,H             ; will be set if minus two - direct command(s)
  8186.         JR      Z,L1B9E         ; forward to LINE-NEW if a jump is to be
  8187.                                 ; made to a new program line/statement.
  8188.  
  8189. ; --------------------
  8190. ; Run a direct command
  8191. ; --------------------
  8192. ; A direct command is to be run or, if continuing from above,
  8193. ; the next statement of a direct command is to be considered.
  8194.  
  8195. ;; LINE-RUN
  8196. L1B8A:  LD      HL,$FFFE        ; The dummy value minus two
  8197.         LD      ($5C45),HL      ; is set/reset as line number in PPC.
  8198.         LD      HL,($5C61)      ; point to end of line + 1 - WORKSP.
  8199.         DEC     HL              ; now point to $80 end-marker.
  8200.         LD      DE,($5C59)      ; address the start of line E_LINE.
  8201.         DEC     DE              ; now location before - for GET-CHAR.
  8202.         LD      A,($5C44)       ; load statement to A from NSPPC.
  8203.         JR      L1BD1           ; forward to NEXT-LINE.
  8204.  
  8205. ; ------------------------------
  8206. ; Find start address of new line
  8207. ; ------------------------------
  8208. ; The branch was to here if a jump is to made to a new line number
  8209. ; and statement.
  8210. ; That is the previous statement was a GO TO, GO SUB, RUN, RETURN, NEXT etc..
  8211.  
  8212. ;; LINE-NEW
  8213. L1B9E:  CALL    L196E           ; routine LINE-ADDR gets address of line
  8214.                                 ; returning zero flag set if line found.
  8215.         LD      A,($5C44)       ; fetch new statement from NSPPC
  8216.         JR      Z,L1BBF         ; forward to LINE-USE if line matched.
  8217.  
  8218. ; continue as must be a direct command.
  8219.  
  8220.         AND     A               ; test statement which should be zero
  8221.         JR      NZ,L1BEC        ; forward to REPORT-N if not.
  8222.                                 ; 'Statement lost'
  8223.  
  8224. ;
  8225.  
  8226.         LD      B,A             ; save statement in B. ?
  8227.         LD      A,(HL)          ; fetch high byte of line number.
  8228.         AND     $C0             ; test if using direct command
  8229.                                 ; a program line is less than $3F
  8230.         LD      A,B             ; retrieve statement.
  8231.                                 ; (we can assume it is zero).
  8232.         JR      Z,L1BBF         ; forward to LINE-USE if was a program line
  8233.  
  8234. ; Alternatively a direct statement has finished correctly.
  8235.  
  8236. ;; REPORT-0
  8237. L1BB0:  RST     08H             ; ERROR-1
  8238.         DB    $FF             ; Error Report: OK
  8239.  
  8240. ; ------------------
  8241. ; Handle REM command
  8242. ; ------------------
  8243. ; The REM command routine.
  8244. ; The return address STMT-RET is dropped and the rest of line ignored.
  8245.  
  8246. ;; REM
  8247. L1BB2:  POP     BC              ; drop return address STMT-RET and
  8248.                                 ; continue ignoring rest of line.
  8249.  
  8250. ; ------------
  8251. ; End of line?
  8252. ; ------------
  8253. ;
  8254. ;
  8255.  
  8256. ;; LINE-END
  8257. L1BB3:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8258.         RET     Z               ; return if checking syntax.
  8259.  
  8260.         LD      HL,($5C55)      ; fetch NXTLIN to HL.
  8261.         LD      A,$C0           ; test against the
  8262.         AND     (HL)            ; system limit $3F.
  8263.         RET     NZ              ; return if more as must be
  8264.                                 ; end of program.
  8265.                                 ; (or direct command)
  8266.  
  8267.         XOR     A               ; set statement to zero.
  8268.  
  8269. ; and continue to set up the next following line and then consider this new one.
  8270.  
  8271. ; ---------------------
  8272. ; General line checking
  8273. ; ---------------------
  8274. ; The branch was here from LINE-NEW if BASIC is branching.
  8275. ; or a continuation from above if dealing with a new sequential line.
  8276. ; First make statement zero number one leaving others unaffected.
  8277.  
  8278. ;; LINE-USE
  8279. L1BBF:  CP      $01             ; will set carry if zero.
  8280.         ADC     A,$00           ; add in any carry.
  8281.  
  8282.         LD      D,(HL)          ; high byte of line number to D.
  8283.         INC     HL              ; advance pointer.
  8284.         LD      E,(HL)          ; low byte of line number to E.
  8285.         LD      ($5C45),DE      ; set system variable PPC.
  8286.  
  8287.         INC     HL              ; advance pointer.
  8288.         LD      E,(HL)          ; low byte of line length to E.
  8289.         INC     HL              ; advance pointer.
  8290.         LD      D,(HL)          ; high byte of line length to D.
  8291.  
  8292.         EX      DE,HL           ; swap pointer to DE before
  8293.         ADD     HL,DE           ; adding to address the end of line.
  8294.         INC     HL              ; advance to start of next line.
  8295.  
  8296. ; -----------------------------
  8297. ; Update NEXT LINE but consider
  8298. ; previous line or edit line.
  8299. ; -----------------------------
  8300. ; The pointer will be the next line if continuing from above or to
  8301. ; edit line end-marker ($80) if from LINE-RUN.
  8302.  
  8303. ;; NEXT-LINE
  8304. L1BD1:  LD      ($5C55),HL      ; store pointer in system variable NXTLIN
  8305.  
  8306.         EX      DE,HL           ; bring back pointer to previous or edit line
  8307.         LD      ($5C5D),HL      ; and update CH_ADD with character address.
  8308.  
  8309.         LD      D,A             ; store statement in D.
  8310.         LD      E,$00           ; set E to zero to suppress token searching
  8311.                                 ; if EACH-STMT is to be called.
  8312.         LD      (IY+$0A),$FF    ; set statement NSPPC to $FF signalling
  8313.                                 ; no jump to be made.
  8314.         DEC     D               ; decrement and test statement
  8315.         LD      (IY+$0D),D      ; set SUBPPC to decremented statement number.
  8316.         JP      Z,L1B28         ; to STMT-LOOP if result zero as statement is
  8317.                                 ; at start of line and address is known.
  8318.  
  8319.         INC     D               ; else restore statement.
  8320.         CALL    L198B           ; routine EACH-STMT finds the D'th statement
  8321.                                 ; address as E does not contain a token.
  8322.         JR      Z,L1BF4         ; forward to STMT-NEXT if address found.
  8323.  
  8324. ;; REPORT-N
  8325. L1BEC:  RST     08H             ; ERROR-1
  8326.         DB    $16             ; Error Report: Statement lost
  8327.  
  8328. ; -----------------
  8329. ; End of statement?
  8330. ; -----------------
  8331. ; This combination of routines is called from 20 places when
  8332. ; the end of a statement should have been reached and all preceding
  8333. ; syntax is in order.
  8334.  
  8335. ;; CHECK-END
  8336. L1BEE:  CALL    L2530           ; routine SYNTAX-Z
  8337.         RET     NZ              ; return immediately in runtime
  8338.  
  8339.         POP     BC              ; drop address of calling routine.
  8340.         POP     BC              ; drop address STMT-RET.
  8341.                                 ; and continue to find next statement.
  8342.  
  8343. ; --------------------
  8344. ; Go to next statement
  8345. ; --------------------
  8346. ; Acceptable characters at this point are carriage return and ':'.
  8347. ; If so go to next statement which in the first case will be on next line.
  8348.  
  8349. ;; STMT-NEXT
  8350. L1BF4           IF BAS48_ONLY=1
  8351.                 RST 0X18
  8352.                 CP 0X0D
  8353.                 ELSE
  8354.                 CALL L3B5D              ; Spectrum 128 patch
  8355.                 ENDIF
  8356.  
  8357. L1BF7:  JR      Z,L1BB3         ; back to LINE-END if so.
  8358.  
  8359.         CP      $3A             ; is it ':' ?
  8360.         JP      Z,L1B28         ; jump back to STMT-LOOP to consider
  8361.                                 ; further statements
  8362.  
  8363.         JP      L1C8A           ; jump to REPORT-C with any other character
  8364.                                 ; 'Nonsense in BASIC'.
  8365.  
  8366. ; Note. the two-byte sequence 'rst 08; DB $0b' could replace the above jp.
  8367.  
  8368. ; -------------------
  8369. ; Command class table
  8370. ; -------------------
  8371. ;
  8372.  
  8373. ;; class-tbl
  8374. L1C01:  DB    L1C10 - $       ; 0F offset to Address: CLASS-00
  8375.         DB    L1C1F - $       ; 1D offset to Address: CLASS-01
  8376.         DB    L1C4E - $       ; 4B offset to Address: CLASS-02
  8377.         DB    L1C0D - $       ; 09 offset to Address: CLASS-03
  8378.         DB    L1C6C - $       ; 67 offset to Address: CLASS-04
  8379.         DB    L1C11 - $       ; 0B offset to Address: CLASS-05
  8380.         DB    L1C82 - $       ; 7B offset to Address: CLASS-06
  8381.         DB    L1C96 - $       ; 8E offset to Address: CLASS-07
  8382.         DB    L1C7A - $       ; 71 offset to Address: CLASS-08
  8383.         DB    L1CBE - $       ; B4 offset to Address: CLASS-09
  8384.         DB    L1C8C - $       ; 81 offset to Address: CLASS-0A
  8385.         DB    L1CDB - $       ; CF offset to Address: CLASS-0B
  8386.  
  8387.  
  8388. ; --------------------------------
  8389. ; Command classes---00, 03, and 05
  8390. ; --------------------------------
  8391. ; class-03 e.g RUN or RUN 200   ;  optional operand
  8392. ; class-00 e.g CONTINUE         ;  no operand
  8393. ; class-05 e.g PRINT            ;  variable syntax checked by routine
  8394.  
  8395. ;; CLASS-03
  8396. L1C0D:  CALL    L1CDE           ; routine FETCH-NUM
  8397.  
  8398. ;; CLASS-00
  8399.  
  8400. L1C10:  CP      A               ; reset zero flag.
  8401.  
  8402. ; if entering here then all class routines are entered with zero reset.
  8403.  
  8404. ;; CLASS-05
  8405. L1C11:  POP     BC              ; drop address SCAN-LOOP.
  8406.         CALL    Z,L1BEE         ; if zero set then call routine CHECK-END >>>
  8407.                                 ; as should be no further characters.
  8408.  
  8409.         EX      DE,HL           ; save HL to DE.
  8410.         LD      HL,($5C74)      ; fetch T_ADDR
  8411.         LD      C,(HL)          ; fetch low byte of routine
  8412.         INC     HL              ; address next.
  8413.         LD      B,(HL)          ; fetch high byte of routine.
  8414.         EX      DE,HL           ; restore HL from DE
  8415.         PUSH    BC              ; push the address
  8416.         RET                     ; and make an indirect jump to the command.
  8417.  
  8418. ; --------------------------------
  8419. ; Command classes---01, 02, and 04
  8420. ; --------------------------------
  8421. ; class-01  e.g LET A = 2*3     ; a variable is reqd
  8422.  
  8423. ; This class routine is also called from INPUT and READ to find the
  8424. ; destination variable for an assignment.
  8425.  
  8426. ;; CLASS-01
  8427. L1C1F:  CALL    L28B2           ; routine LOOK-VARS returns carry set if not
  8428.                                 ; found in runtime.
  8429.  
  8430. ; ----------------------
  8431. ; Variable in assignment
  8432. ; ----------------------
  8433. ;
  8434. ;
  8435.  
  8436. ;; VAR-A-1
  8437. L1C22:  LD      (IY+$37),$00    ; set FLAGX to zero
  8438.         JR      NC,L1C30        ; forward to VAR-A-2 if found or checking
  8439.                                 ; syntax.
  8440.  
  8441.         SET     1,(IY+$37)      ; FLAGX  - Signal a new variable
  8442.         JR      NZ,L1C46        ; to VAR-A-3 if not assigning to an array
  8443.                                 ; e.g. LET a$(3,3) = "X"
  8444.  
  8445. ;; REPORT-2
  8446. L1C2E:  RST     08H             ; ERROR-1
  8447.         DB    $01             ; Error Report: Variable not found
  8448.  
  8449. ;; VAR-A-2
  8450. L1C30:  CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  8451.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8452.         JR      NZ,L1C46        ; to VAR-A-3 if numeric
  8453.  
  8454.         XOR     A               ; default to array/slice - to be retained.
  8455.         CALL    L2530           ; routine SYNTAX-Z
  8456.         CALL    NZ,L2BF1        ; routine STK-FETCH is called in runtime
  8457.                                 ; may overwrite A with 1.
  8458.         LD      HL,$5C71        ; address system variable FLAGX
  8459.         OR      (HL)            ; set bit 0 if simple variable to be reclaimed
  8460.         LD      (HL),A          ; update FLAGX
  8461.         EX      DE,HL           ; start of string/subscript to DE
  8462.  
  8463. ;; VAR-A-3
  8464. L1C46:  LD      ($5C72),BC      ; update STRLEN
  8465.         LD      ($5C4D),HL      ; and DEST of assigned string.
  8466.         RET                     ; return.
  8467.  
  8468. ; -------------------------------------------------
  8469. ; class-02 e.g. LET a = 1 + 1   ; an expression must follow
  8470.  
  8471. ;; CLASS-02
  8472. L1C4E:  POP     BC              ; drop return address SCAN-LOOP
  8473.         CALL    L1C56           ; routine VAL-FET-1 is called to check
  8474.                                 ; expression and assign result in runtime
  8475.         CALL    L1BEE           ; routine CHECK-END checks nothing else
  8476.                                 ; is present in statement.
  8477.         RET                     ; Return
  8478.  
  8479. ; -------------
  8480. ; Fetch a value
  8481. ; -------------
  8482. ;
  8483. ;
  8484.  
  8485. ;; VAL-FET-1
  8486. L1C56:  LD      A,($5C3B)       ; initial FLAGS to A
  8487.  
  8488. ;; VAL-FET-2
  8489. L1C59:  PUSH    AF              ; save A briefly
  8490.         CALL    L24FB           ; routine SCANNING evaluates expression.
  8491.         POP     AF              ; restore A
  8492.         LD      D,(IY+$01)      ; post-SCANNING FLAGS to D
  8493.         XOR     D               ; xor the two sets of flags
  8494.         AND     $40             ; pick up bit 6 of xored FLAGS should be zero
  8495.         JR      NZ,L1C8A        ; forward to REPORT-C if not zero
  8496.                                 ; 'Nonsense in BASIC' - results don't agree.
  8497.  
  8498.         BIT     7,D             ; test FLAGS - is syntax being checked ?
  8499.         JP      NZ,L2AFF        ; jump forward to LET to make the assignment
  8500.                                 ; in runtime.
  8501.  
  8502.         RET                     ; but return from here if checking syntax.
  8503.  
  8504. ; ------------------
  8505. ; Command class---04
  8506. ; ------------------
  8507. ; class-04 e.g. FOR i            ; a single character variable must follow
  8508.  
  8509. ;; CLASS-04
  8510. L1C6C:  CALL    L28B2           ; routine LOOK-VARS
  8511.         PUSH    AF              ; preserve flags.
  8512.         LD      A,C             ; fetch type - should be 011xxxxx
  8513.         OR      $9F             ; combine with 10011111.
  8514.         INC     A               ; test if now $FF by incrementing.
  8515.         JR      NZ,L1C8A        ; forward to REPORT-C if result not zero.
  8516.  
  8517.         POP     AF              ; else restore flags.
  8518.         JR      L1C22           ; back to VAR-A-1
  8519.  
  8520.  
  8521. ; --------------------------------
  8522. ; Expect numeric/string expression
  8523. ; --------------------------------
  8524. ; This routine is used to get the two coordinates of STRING$, ATTR and POINT.
  8525. ; It is also called from PRINT-ITEM to get the two numeric expressions that
  8526. ; follow the AT ( in PRINT AT, INPUT AT).
  8527.  
  8528. ;; NEXT-2NUM
  8529. L1C79:  RST     20H             ; NEXT-CHAR advance past 'AT' or '('.
  8530.  
  8531. ; --------
  8532. ; class-08 e.g POKE 65535,2     ; two numeric expressions separated by comma
  8533. ;; CLASS-08
  8534. ;; EXPT-2NUM
  8535. L1C7A:  CALL    L1C82           ; routine EXPT-1NUM is called for first
  8536.                                 ; numeric expression
  8537.         CP      $2C             ; is character ',' ?
  8538.         JR      NZ,L1C8A        ; to REPORT-C if not required separator.
  8539.                                 ; 'Nonsense in BASIC'.
  8540.  
  8541.         RST     20H             ; NEXT-CHAR
  8542.  
  8543. ; ->
  8544. ;  class-06  e.g. GOTO a*1000   ; a numeric expression must follow
  8545. ;; CLASS-06
  8546. ;; EXPT-1NUM
  8547. L1C82:  CALL    L24FB           ; routine SCANNING
  8548.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8549.         RET     NZ              ; return if result is numeric.
  8550.  
  8551. ;; REPORT-C
  8552. L1C8A:  RST     08H             ; ERROR-1
  8553.         DB    $0B             ; Error Report: Nonsense in BASIC
  8554.  
  8555. ; ---------------------------------------------------------------
  8556. ; class-0A e.g. ERASE "????"    ; a string expression must follow.
  8557. ;                               ; these only occur in unimplemented commands
  8558. ;                               ; although the routine expt-exp is called
  8559. ;                               ; from SAVE-ETC
  8560.  
  8561. ;; CLASS-0A
  8562. ;; EXPT-EXP
  8563. L1C8C:  CALL    L24FB           ; routine SCANNING
  8564.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  8565.         RET     Z               ; return if string result.
  8566.  
  8567.         JR      L1C8A           ; back to REPORT-C if numeric.
  8568.  
  8569. ; ---------------------
  8570. ; Set permanent colours
  8571. ; class 07
  8572. ; ---------------------
  8573. ; class-07 e.g PAPER 6          ; a single class for a collection of
  8574. ;                               ; similar commands. Clever.
  8575. ;
  8576. ; Note. these commands should ensure that current channel is 'S'
  8577.  
  8578. ;; CLASS-07
  8579. L1C96:  BIT     7,(IY+$01)      ; test FLAGS - checking syntax only ?
  8580.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8581.         CALL    NZ,L0D4D        ; routine TEMPS is called in runtime.
  8582.         POP     AF              ; drop return address SCAN-LOOP
  8583.         LD      A,($5C74)       ; T_ADDR_lo to accumulator.
  8584.                                 ; points to '$07' entry + 1
  8585.                                 ; e.g. for INK points to $EC now
  8586.  
  8587. ; Note if you move alter the syntax table next line may have to be altered.
  8588.  
  8589. ; Note. For ZASM assembler replace following expression with SUB $13.
  8590.  
  8591. L1CA5           SUB LOW (L1AEB)-$D8     ; % 256 ; convert $EB to $D8 ('INK') etc.
  8592.                                 ; ( is SUB $13 in standard ROM )
  8593.  
  8594.         CALL    L21FC           ; routine CO-TEMP-4
  8595.         CALL    L1BEE           ; routine CHECK-END check that nothing else
  8596.                                 ; in statement.
  8597.  
  8598. ; return here in runtime.
  8599.  
  8600.         LD      HL,($5C8F)      ; pick up ATTR_T and MASK_T
  8601.         LD      ($5C8D),HL      ; and store in ATTR_P and MASK_P
  8602.         LD      HL,$5C91        ; point to P_FLAG.
  8603.         LD      A,(HL)          ; pick up in A
  8604.         RLCA                    ; rotate to left
  8605.         XOR     (HL)            ; combine with HL
  8606.         AND     $AA             ; 10101010
  8607.         XOR     (HL)            ; only permanent bits affected
  8608.         LD      (HL),A          ; reload into P_FLAG.
  8609.         RET                     ; return.
  8610.  
  8611. ; ------------------
  8612. ; Command class---09
  8613. ; ------------------
  8614. ; e.g. PLOT PAPER 0; 128,88     ; two coordinates preceded by optional
  8615. ;                               ; embedded colour items.
  8616. ;
  8617. ; Note. this command should ensure that current channel is actually 'S'.
  8618.  
  8619. ;; CLASS-09
  8620. L1CBE:  CALL    L2530           ; routine SYNTAX-Z
  8621.         JR      Z,L1CD6         ; forward to CL-09-1 if checking syntax.
  8622.  
  8623.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use
  8624.         CALL    L0D4D           ; routine TEMPS is called.
  8625.         LD      HL,$5C90        ; point to MASK_T
  8626.         LD      A,(HL)          ; fetch mask to accumulator.
  8627.         OR      $F8             ; or with 11111000 paper/bright/flash 8
  8628.         LD      (HL),A          ; mask back to MASK_T system variable.
  8629.         RES     6,(IY+$57)      ; reset P_FLAG  - signal NOT PAPER 9 ?
  8630.  
  8631.         RST     18H             ; GET-CHAR
  8632.  
  8633. ;; CL-09-1
  8634. L1CD6:  CALL    L21E2           ; routine CO-TEMP-2 deals with any embedded
  8635.                                 ; colour items.
  8636.         JR      L1C7A           ; exit via EXPT-2NUM to check for x,y.
  8637.  
  8638. ; Note. if either of the numeric expressions contain STR$ then the flag setting
  8639. ; above will be undone when the channel flags are reset during STR$.
  8640. ; e.g.
  8641. ; 10 BORDER 3 : PLOT VAL STR$ 128, VAL STR$ 100
  8642. ; credit John Elliott.
  8643.  
  8644. ; ------------------
  8645. ; Command class---0B
  8646. ; ------------------
  8647. ; Again a single class for four commands.
  8648. ; This command just jumps back to SAVE-ETC to handle the four tape commands.
  8649. ; The routine itself works out which command has called it by examining the
  8650. ; address in T_ADDR_lo. Note therefore that the syntax table has to be
  8651. ; located where these and other sequential command addresses are not split
  8652. ; over a page boundary.
  8653.  
  8654. ;; CLASS-0B
  8655. L1CDB:  JP      L0605           ; jump way back to SAVE-ETC
  8656.  
  8657. ; --------------
  8658. ; Fetch a number
  8659. ; --------------
  8660. ; This routine is called from CLASS-03 when a command may be followed by
  8661. ; an optional numeric expression e.g. RUN. If the end of statement has
  8662. ; been reached then zero is used as the default.
  8663. ; Also called from LIST-4.
  8664.  
  8665. ;; FETCH-NUM
  8666. L1CDE:  CP      $0D             ; is character a carriage return ?
  8667.         JR      Z,L1CE6         ; forward to USE-ZERO if so
  8668.  
  8669.         CP      $3A             ; is it ':' ?
  8670.         JR      NZ,L1C82        ; forward to EXPT-1NUM if not.
  8671.                                 ; else continue and use zero.
  8672.  
  8673. ; ----------------
  8674. ; Use zero routine
  8675. ; ----------------
  8676. ; This routine is called four times to place the value zero on the
  8677. ; calculator stack as a default value in runtime.
  8678.  
  8679. ;; USE-ZERO
  8680. L1CE6:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  8681.         RET     Z               ;
  8682.  
  8683.         RST     28H             ;; FP-CALC
  8684.         DB    $A0             ;;stk-zero       ;0.
  8685.         DB    $38             ;;end-calc
  8686.  
  8687.         RET                     ; return.
  8688.  
  8689. ; -------------------
  8690. ; Handle STOP command
  8691. ; -------------------
  8692. ; Command Syntax: STOP
  8693. ; One of the shortest and least used commands. As with 'OK' not an error.
  8694.  
  8695. ;; REPORT-9
  8696. ;; STOP
  8697. L1CEE:  RST     08H             ; ERROR-1
  8698.         DB    $08             ; Error Report: STOP statement
  8699.  
  8700. ; -----------------
  8701. ; Handle IF command
  8702. ; -----------------
  8703. ; e.g. IF score>100 THEN PRINT "You Win"
  8704. ; The parser has already checked the expression the result of which is on
  8705. ; the calculator stack. The presence of the 'THEN' separator has also been
  8706. ; checked and CH-ADD points to the command after THEN.
  8707. ;
  8708.  
  8709. ;; IF
  8710. L1CF0:  POP     BC              ; drop return address - STMT-RET
  8711.         CALL    L2530           ; routine SYNTAX-Z
  8712.         JR      Z,L1D00         ; forward to IF-1 if checking syntax
  8713.                                 ; to check syntax of PRINT "You Win"
  8714.  
  8715.  
  8716.         RST     28H             ;; FP-CALC    score>100 (1=TRUE 0=FALSE)
  8717.         DB    $02             ;;delete      .
  8718.         DB    $38             ;;end-calc
  8719.  
  8720.         EX      DE,HL           ; make HL point to deleted value
  8721.         CALL    L34E9           ; routine TEST-ZERO
  8722.         JP      C,L1BB3         ; jump to LINE-END if FALSE (0)
  8723.  
  8724. ;; IF-1
  8725. L1D00:  JP      L1B29           ; to STMT-L-1, if true (1) to execute command
  8726.                                 ; after 'THEN' token.
  8727.  
  8728. ; ------------------
  8729. ; Handle FOR command
  8730. ; ------------------
  8731. ; e.g. FOR i = 0 TO 1 STEP 0.1
  8732. ; Using the syntax tables, the parser has already checked for a start and
  8733. ; limit value and also for the intervening separator.
  8734. ; the two values v,l are on the calculator stack.
  8735. ; CLASS-04 has also checked the variable and the name is in STRLEN_lo.
  8736. ; The routine begins by checking for an optional STEP.
  8737.  
  8738. ;; FOR
  8739. L1D03:  CP      $CD             ; is there a 'STEP' ?
  8740.         JR      NZ,L1D10        ; to F-USE-1 if not to use 1 as default.
  8741.  
  8742.         RST     20H             ; NEXT-CHAR
  8743.         CALL    L1C82           ; routine EXPT-1NUM
  8744.         CALL    L1BEE           ; routine CHECK-END
  8745.         JR      L1D16           ; to F-REORDER
  8746.  
  8747. ; ---
  8748.  
  8749. ;; F-USE-1
  8750. L1D10:  CALL    L1BEE           ; routine CHECK-END
  8751.  
  8752.         RST     28H             ;; FP-CALC      v,l.
  8753.         DB    $A1             ;;stk-one       v,l,1=s.
  8754.         DB    $38             ;;end-calc
  8755.  
  8756.  
  8757. ;; F-REORDER
  8758. L1D16:  RST     28H             ;; FP-CALC       v,l,s.
  8759.         DB    $C0             ;;st-mem-0       v,l,s.
  8760.         DB    $02             ;;delete         v,l.
  8761.         DB    $01             ;;exchange       l,v.
  8762.         DB    $E0             ;;get-mem-0      l,v,s.
  8763.         DB    $01             ;;exchange       l,s,v.
  8764.         DB    $38             ;;end-calc
  8765.  
  8766.         CALL    L2AFF           ; routine LET assigns the initial value v to
  8767.                                 ; the variable altering type if necessary.
  8768.         LD      ($5C68),HL      ; The system variable MEM is made to point to
  8769.                                 ; the variable instead of its normal
  8770.                                 ; location MEMBOT
  8771.         DEC     HL              ; point to single-character name
  8772.         LD      A,(HL)          ; fetch name
  8773.         SET     7,(HL)          ; set bit 7 at location
  8774.         LD      BC,$0006        ; add six to HL
  8775.         ADD     HL,BC           ; to address where limit should be.
  8776.         RLCA                    ; test bit 7 of original name.
  8777.         JR      C,L1D34         ; forward to F-L-S if already a FOR/NEXT
  8778.                                 ; variable
  8779.  
  8780.         LD      C,$0D           ; otherwise an additional 13 bytes are needed.
  8781.                                 ; 5 for each value, two for line number and
  8782.                                 ; 1 byte for looping statement.
  8783.         CALL    L1655           ; routine MAKE-ROOM creates them.
  8784.         INC     HL              ; make HL address limit.
  8785.  
  8786. ;; F-L-S
  8787. L1D34:  PUSH    HL              ; save position.
  8788.  
  8789.         RST     28H             ;; FP-CALC         l,s.
  8790.         DB    $02             ;;delete           l.
  8791.         DB    $02             ;;delete           .
  8792.         DB    $38             ;;end-calc
  8793.                                 ; DE points to STKEND, l.
  8794.  
  8795.         POP     HL              ; restore variable position
  8796.         EX      DE,HL           ; swap pointers
  8797.         LD      C,$0A           ; ten bytes to move
  8798.         LDIR                    ; Copy 'deleted' values to variable.
  8799.         LD      HL,($5C45)      ; Load with current line number from PPC
  8800.         EX      DE,HL           ; exchange pointers.
  8801.         LD      (HL),E          ; save the looping line
  8802.         INC     HL              ; in the next
  8803.         LD      (HL),D          ; two locations.
  8804.         LD      D,(IY+$0D)      ; fetch statement from SUBPPC system variable.
  8805.         INC     D               ; increment statement.
  8806.         INC     HL              ; and pointer
  8807.         LD      (HL),D          ; and store the looping statement.
  8808.                                 ;
  8809.         CALL    L1DDA           ; routine NEXT-LOOP considers an initial
  8810.         RET     NC              ; iteration. Return to STMT-RET if a loop is
  8811.                                 ; possible to execute next statement.
  8812.  
  8813. ; no loop is possible so execution continues after the matching 'NEXT'
  8814.  
  8815.         LD      B,(IY+$38)      ; get single-character name from STRLEN_lo
  8816.         LD      HL,($5C45)      ; get the current line from PPC
  8817.         LD      ($5C42),HL      ; and store it in NEWPPC
  8818.         LD      A,($5C47)       ; fetch current statement from SUBPPC
  8819.         NEG                     ; Negate as counter decrements from zero
  8820.                                 ; initially and we are in the middle of a
  8821.                                 ; line.
  8822.         LD      D,A             ; Store result in D.
  8823.         LD      HL,($5C5D)      ; get current address from CH_ADD
  8824.         LD      E,$F3           ; search will be for token 'NEXT'
  8825.  
  8826. ;; F-LOOP
  8827. L1D64:  PUSH    BC              ; save variable name.
  8828.         LD      BC,($5C55)      ; fetch NXTLIN
  8829.         CALL    L1D86           ; routine LOOK-PROG searches for 'NEXT' token.
  8830.         LD      ($5C55),BC      ; update NXTLIN
  8831.         POP     BC              ; and fetch the letter
  8832.         JR      C,L1D84         ; forward to REPORT-I if the end of program
  8833.                                 ; was reached by LOOK-PROG.
  8834.                                 ; 'FOR without NEXT'
  8835.  
  8836.         RST     20H             ; NEXT-CHAR fetches character after NEXT
  8837.         OR      $20             ; ensure it is upper-case.
  8838.         CP      B               ; compare with FOR variable name
  8839.         JR      Z,L1D7C         ; forward to F-FOUND if it matches.
  8840.  
  8841. ; but if no match i.e. nested FOR/NEXT loops then continue search.
  8842.  
  8843.         RST     20H             ; NEXT-CHAR
  8844.         JR      L1D64           ; back to F-LOOP
  8845.  
  8846. ; ---
  8847.  
  8848.  
  8849. ;; F-FOUND
  8850. L1D7C:  RST     20H             ; NEXT-CHAR
  8851.         LD      A,$01           ; subtract the negated counter from 1
  8852.         SUB     D               ; to give the statement after the NEXT
  8853.         LD      ($5C44),A       ; set system variable NSPPC
  8854.         RET                     ; return to STMT-RET to branch to new
  8855.                                 ; line and statement. ->
  8856. ; ---
  8857.  
  8858. ;; REPORT-I
  8859. L1D84:  RST     08H             ; ERROR-1
  8860.         DB    $11             ; Error Report: FOR without NEXT
  8861.  
  8862. ; ---------
  8863. ; LOOK-PROG
  8864. ; ---------
  8865. ; Find DATA, DEF FN or NEXT.
  8866. ; This routine searches the program area for one of the above three keywords.
  8867. ; On entry, HL points to start of search area.
  8868. ; The token is in E, and D holds a statement count, decremented from zero.
  8869.  
  8870. ;; LOOK-PROG
  8871. L1D86:  LD      A,(HL)          ; fetch current character
  8872.         CP      $3A             ; is it ':' a statement separator ?
  8873.         JR      Z,L1DA3         ; forward to LOOK-P-2 if so.
  8874.  
  8875. ; The starting point was PROG - 1 or the end of a line.
  8876.  
  8877. ;; LOOK-P-1
  8878. L1D8B:  INC     HL              ; increment pointer to address
  8879.         LD      A,(HL)          ; the high byte of line number
  8880.         AND     $C0             ; test for program end marker $80 or a
  8881.                                 ; variable
  8882.         SCF                     ; Set Carry Flag
  8883.         RET     NZ              ; return with carry set if at end
  8884.                                 ; of program.           ->
  8885.  
  8886.         LD      B,(HL)          ; high byte of line number to B
  8887.         INC     HL              ;
  8888.         LD      C,(HL)          ; low byte to C.
  8889.         LD      ($5C42),BC      ; set system variable NEWPPC.
  8890.         INC     HL              ;
  8891.         LD      C,(HL)          ; low byte of line length to C.
  8892.         INC     HL              ;
  8893.         LD      B,(HL)          ; high byte to B.
  8894.         PUSH    HL              ; save address
  8895.         ADD     HL,BC           ; add length to position.
  8896.         LD      B,H             ; and save result
  8897.         LD      C,L             ; in BC.
  8898.         POP     HL              ; restore address.
  8899.         LD      D,$00           ; initialize statement counter to zero.
  8900.  
  8901. ;; LOOK-P-2
  8902. L1DA3:  PUSH    BC              ; save address of next line
  8903.         CALL    L198B           ; routine EACH-STMT searches current line.
  8904.         POP     BC              ; restore address.
  8905.         RET     NC              ; return if match was found. ->
  8906.  
  8907.         JR      L1D8B           ; back to LOOK-P-1 for next line.
  8908.  
  8909. ; -------------------
  8910. ; Handle NEXT command
  8911. ; -------------------
  8912. ; e.g. NEXT i
  8913. ; The parameter tables have already evaluated the presence of a variable
  8914.  
  8915. ;; NEXT
  8916. L1DAB:  BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  8917.         JP      NZ,L1C2E        ; jump back to REPORT-2 if so
  8918.                                 ; 'Variable not found'
  8919.  
  8920. ; now test if found variable is a simple variable uninitialized by a FOR.
  8921.  
  8922.         LD      HL,($5C4D)      ; load address of variable from DEST
  8923.         BIT     7,(HL)          ; is it correct type ?
  8924.         JR      Z,L1DD8         ; forward to REPORT-1 if not
  8925.                                 ; 'NEXT without FOR'
  8926.  
  8927.         INC     HL              ; step past variable name
  8928.         LD      ($5C68),HL      ; and set MEM to point to three 5-byte values
  8929.                                 ; value, limit, step.
  8930.  
  8931.         RST     28H             ;; FP-CALC     add step and re-store
  8932.         DB    $E0             ;;get-mem-0    v.
  8933.         DB    $E2             ;;get-mem-2    v,s.
  8934.         DB    $0F             ;;addition     v+s.
  8935.         DB    $C0             ;;st-mem-0     v+s.
  8936.         DB    $02             ;;delete       .
  8937.         DB    $38             ;;end-calc
  8938.  
  8939.         CALL    L1DDA           ; routine NEXT-LOOP tests against limit.
  8940.         RET     C               ; return if no more iterations possible.
  8941.  
  8942.         LD      HL,($5C68)      ; find start of variable contents from MEM.
  8943.         LD      DE,$000F        ; add 3*5 to
  8944.         ADD     HL,DE           ; address the looping line number
  8945.         LD      E,(HL)          ; low byte to E
  8946.         INC     HL              ;
  8947.         LD      D,(HL)          ; high byte to D
  8948.         INC     HL              ; address looping statement
  8949.         LD      H,(HL)          ; and store in H
  8950.         EX      DE,HL           ; swap registers
  8951.         JP      L1E73           ; exit via GO-TO-2 to execute another loop.
  8952.  
  8953. ; ---
  8954.  
  8955. ;; REPORT-1
  8956. L1DD8:  RST     08H             ; ERROR-1
  8957.         DB    $00             ; Error Report: NEXT without FOR
  8958.  
  8959.  
  8960. ; -----------------
  8961. ; Perform NEXT loop
  8962. ; -----------------
  8963. ; This routine is called from the FOR command to test for an initial
  8964. ; iteration and from the NEXT command to test for all subsequent iterations.
  8965. ; the system variable MEM addresses the variable's contents which, in the
  8966. ; latter case, have had the step, possibly negative, added to the value.
  8967.  
  8968. ;; NEXT-LOOP
  8969. L1DDA:  RST     28H             ;; FP-CALC
  8970.         DB    $E1             ;;get-mem-1        l.
  8971.         DB    $E0             ;;get-mem-0        l,v.
  8972.         DB    $E2             ;;get-mem-2        l,v,s.
  8973.         DB    $36             ;;less-0           l,v,(1/0) negative step ?
  8974.         DB    $00             ;;jump-true        l,v.(1/0)
  8975.  
  8976.         DB    $02             ;;to L1DE2, NEXT-1 if step negative
  8977.  
  8978.         DB    $01             ;;exchange         v,l.
  8979.  
  8980. ;; NEXT-1
  8981. L1DE2:  DB    $03             ;;subtract         l-v OR v-l.
  8982.         DB    $37             ;;greater-0        (1/0)
  8983.         DB    $00             ;;jump-true        .
  8984.  
  8985.         DB    $04             ;;to L1DE9, NEXT-2 if no more iterations.
  8986.  
  8987.         DB    $38             ;;end-calc         .
  8988.  
  8989.         AND     A               ; clear carry flag signalling another loop.
  8990.         RET                     ; return
  8991.  
  8992. ; ---
  8993.  
  8994. ;; NEXT-2
  8995. L1DE9:  DB    $38             ;;end-calc         .
  8996.  
  8997.         SCF                     ; set carry flag signalling looping exhausted.
  8998.         RET                     ; return
  8999.  
  9000.  
  9001. ; -------------------
  9002. ; Handle READ command
  9003. ; -------------------
  9004. ; e.g. READ a, b$, c$(1000 TO 3000)
  9005. ; A list of comma-separated variables is assigned from a list of
  9006. ; comma-separated expressions.
  9007. ; As it moves along the first list, the character address CH_ADD is stored
  9008. ; in X_PTR while CH_ADD is used to read the second list.
  9009.  
  9010. ;; READ-3
  9011. L1DEC:  RST     20H             ; NEXT-CHAR
  9012.  
  9013. ; -> Entry point.
  9014. ;; READ
  9015. L1DED:  CALL    L1C1F           ; routine CLASS-01 checks variable.
  9016.         CALL    L2530           ; routine SYNTAX-Z
  9017.         JR      Z,L1E1E         ; forward to READ-2 if checking syntax
  9018.  
  9019.  
  9020.         RST     18H             ; GET-CHAR
  9021.         LD      ($5C5F),HL      ; save character position in X_PTR.
  9022.         LD      HL,($5C57)      ; load HL with Data Address DATADD, which is
  9023.                                 ; the start of the program or the address
  9024.                                 ; after the last expression that was read or
  9025.                                 ; the address of the line number of the
  9026.                                 ; last RESTORE command.
  9027.         LD      A,(HL)          ; fetch character
  9028.         CP      $2C             ; is it a comma ?
  9029.         JR      Z,L1E0A         ; forward to READ-1 if so.
  9030.  
  9031. ; else all data in this statement has been read so look for next DATA token
  9032.  
  9033.         LD      E,$E4           ; token 'DATA'
  9034.         CALL    L1D86           ; routine LOOK-PROG
  9035.         JR      NC,L1E0A        ; forward to READ-1 if DATA found
  9036.  
  9037. ; else report the error.
  9038.  
  9039. ;; REPORT-E
  9040. L1E08:  RST     08H             ; ERROR-1
  9041.         DB    $0D             ; Error Report: Out of DATA
  9042.  
  9043. ;; READ-1
  9044. L1E0A:  CALL    L0077           ; routine TEMP-PTR1 advances updating CH_ADD
  9045.                                 ; with new DATADD position.
  9046.         CALL    L1C56           ; routine VAL-FET-1 assigns value to variable
  9047.                                 ; checking type match and adjusting CH_ADD.
  9048.  
  9049.         RST     18H             ; GET-CHAR fetches adjusted character position
  9050.         LD      ($5C57),HL      ; store back in DATADD
  9051.         LD      HL,($5C5F)      ; fetch X_PTR  the original READ CH_ADD
  9052.         LD      (IY+$26),$00    ; now nullify X_PTR_hi
  9053.         CALL    L0078           ; routine TEMP-PTR2 restores READ CH_ADD
  9054.  
  9055. ;; READ-2
  9056. L1E1E:  RST     18H             ; GET-CHAR
  9057.         CP      $2C             ; is it ',' indicating more variables to read ?
  9058.         JR      Z,L1DEC         ; back to READ-3 if so
  9059.  
  9060.         CALL    L1BEE           ; routine CHECK-END
  9061.         RET                     ; return from here in runtime to STMT-RET.
  9062.  
  9063. ; -------------------
  9064. ; Handle DATA command
  9065. ; -------------------
  9066. ; In runtime this 'command' is passed by but the syntax is checked when such
  9067. ; a statement is found while parsing a line.
  9068. ; e.g. DATA 1, 2, "text", score-1, a$(location, room, object), FN r(49),
  9069. ;         wages - tax, TRUE, The meaning of life
  9070.  
  9071. ;; DATA
  9072. L1E27:  CALL    L2530           ; routine SYNTAX-Z to check status
  9073.         JR      NZ,L1E37        ; forward to DATA-2 if in runtime
  9074.  
  9075. ;; DATA-1
  9076. L1E2C:  CALL    L24FB           ; routine SCANNING to check syntax of
  9077.                                 ; expression
  9078.         CP      $2C             ; is it a comma ?
  9079.         CALL    NZ,L1BEE        ; routine CHECK-END checks that statement
  9080.                                 ; is complete. Will make an early exit if
  9081.                                 ; so. >>>
  9082.         RST     20H             ; NEXT-CHAR
  9083.         JR      L1E2C           ; back to DATA-1
  9084.  
  9085. ; ---
  9086.  
  9087. ;; DATA-2
  9088. L1E37:  LD      A,$E4           ; set token to 'DATA' and continue into
  9089.                                 ; the the PASS-BY routine.
  9090.  
  9091.  
  9092. ; ----------------------------------
  9093. ; Check statement for DATA or DEF FN
  9094. ; ----------------------------------
  9095. ; This routine is used to backtrack to a command token and then
  9096. ; forward to the next statement in runtime.
  9097.  
  9098. ;; PASS-BY
  9099. L1E39:  LD      B,A             ; Give BC enough space to find token.
  9100.         CPDR                    ; Compare decrement and repeat. (Only use).
  9101.                                 ; Work backwards till keyword is found which
  9102.                                 ; is start of statement before any quotes.
  9103.                                 ; HL points to location before keyword.
  9104.         LD      DE,$0200        ; count 1+1 statements, dummy value in E to
  9105.                                 ; inhibit searching for a token.
  9106.         JP      L198B           ; to EACH-STMT to find next statement
  9107.  
  9108. ; -----------------------------------------------------------------------
  9109. ; A General Note on Invalid Line Numbers.
  9110. ; =======================================
  9111. ; One of the revolutionary concepts of Sinclair BASIC was that it supported
  9112. ; virtual line numbers. That is the destination of a GO TO, RESTORE etc. need
  9113. ; not exist. It could be a point before or after an actual line number.
  9114. ; Zero suffices for a before but the after should logically be infinity.
  9115. ; Since the maximum actual line limit is 9999 then the system limit, 16383
  9116. ; when variables kick in, would serve fine as a virtual end point.
  9117. ; However, ironically, only the LOAD command gets it right. It will not
  9118. ; autostart a program that has been saved with a line higher than 16383.
  9119. ; All the other commands deal with the limit unsatisfactorily.
  9120. ; LIST, RUN, GO TO, GO SUB and RESTORE have problems and the latter may
  9121. ; crash the machine when supplied with an inappropriate virtual line number.
  9122. ; This is puzzling as very careful consideration must have been given to
  9123. ; this point when the new variable types were allocated their masks and also
  9124. ; when the routine NEXT-ONE was successfully re-written to reflect this.
  9125. ; An enigma.
  9126. ; -------------------------------------------------------------------------
  9127.  
  9128. ; ----------------------
  9129. ; Handle RESTORE command
  9130. ; ----------------------
  9131. ; The restore command sets the system variable for the data address to
  9132. ; point to the location before the supplied line number or first line
  9133. ; thereafter.
  9134. ; This alters the position where subsequent READ commands look for data.
  9135. ; Note. If supplied with inappropriate high numbers the system may crash
  9136. ; in the LINE-ADDR routine as it will pass the program/variables end-marker
  9137. ; and then lose control of what it is looking for - variable or line number.
  9138. ; - observation, Steven Vickers, 1984, Pitman.
  9139.  
  9140. ;; RESTORE
  9141. L1E42:  CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9142.                                 ; Note. B should be checked against limit $3F
  9143.                                 ; and an error generated if higher.
  9144.  
  9145. ; this entry point is used from RUN command with BC holding zero
  9146.  
  9147. ;; REST-RUN
  9148. L1E45:  LD      H,B             ; transfer the line
  9149.         LD      L,C             ; number to the HL register.
  9150.         CALL    L196E           ; routine LINE-ADDR to fetch the address.
  9151.         DEC     HL              ; point to the location before the line.
  9152.         LD      ($5C57),HL      ; update system variable DATADD.
  9153.         RET                     ; return to STMT-RET (or RUN)
  9154.  
  9155. ; ------------------------
  9156. ; Handle RANDOMIZE command
  9157. ; ------------------------
  9158. ; This command sets the SEED for the RND function to a fixed value.
  9159. ; With the parameter zero, a random start point is used depending on
  9160. ; how long the computer has been switched on.
  9161.  
  9162. ;; RANDOMIZE
  9163. L1E4F:  CALL    L1E99           ; routine FIND-INT2 puts parameter in BC.
  9164.         LD      A,B             ; test this
  9165.         OR      C               ; for zero.
  9166.         JR      NZ,L1E5A        ; forward to RAND-1 if not zero.
  9167.  
  9168.         LD      BC,($5C78)      ; use the lower two bytes at FRAMES1.
  9169.  
  9170. ;; RAND-1
  9171. L1E5A:  LD      ($5C76),BC      ; place in SEED system variable.
  9172.         RET                     ; return to STMT-RET
  9173.  
  9174. ; -----------------------
  9175. ; Handle CONTINUE command
  9176. ; -----------------------
  9177. ; The CONTINUE command transfers the OLD (but incremented) values of
  9178. ; line number and statement to the equivalent "NEW VALUE" system variables
  9179. ; by using the last part of GO TO and exits indirectly to STMT-RET.
  9180.  
  9181. ;; CONTINUE
  9182. L1E5F:  LD      HL,($5C6E)      ; fetch OLDPPC line number.
  9183.         LD      D,(IY+$36)      ; fetch OSPPC statement.
  9184.         JR      L1E73           ; forward to GO-TO-2
  9185.  
  9186. ; --------------------
  9187. ; Handle GO TO command
  9188. ; --------------------
  9189. ; The GO TO command routine is also called by GO SUB and RUN routines
  9190. ; to evaluate the parameters of both commands.
  9191. ; It updates the system variables used to fetch the next line/statement.
  9192. ; It is at STMT-RET that the actual change in control takes place.
  9193. ; Unlike some BASICs the line number need not exist.
  9194. ; Note. the high byte of the line number is incorrectly compared with $F0
  9195. ; instead of $3F. This leads to commands with operands greater than 32767
  9196. ; being considered as having been run from the editing area and the
  9197. ; error report 'Statement Lost' is given instead of 'OK'.
  9198. ; - Steven Vickers, 1984.
  9199.  
  9200. ;; GO-TO
  9201. L1E67:  CALL    L1E99           ; routine FIND-INT2 puts operand in BC
  9202.         LD      H,B             ; transfer line
  9203.         LD      L,C             ; number to HL.
  9204.         LD      D,$00           ; set statement to 0 - first.
  9205.         LD      A,H             ; compare high byte only
  9206.         CP      $F0             ; to $F0 i.e. 61439 in full.
  9207.         JR      NC,L1E9F        ; forward to REPORT-B if above.
  9208.  
  9209. ; This call entry point is used to update the system variables e.g. by RETURN.
  9210.  
  9211. ;; GO-TO-2
  9212. L1E73:  LD      ($5C42),HL      ; save line number in NEWPPC
  9213.         LD      (IY+$0A),D      ; and statement in NSPPC
  9214.         RET                     ; to STMT-RET (or GO-SUB command)
  9215.  
  9216. ; ------------------
  9217. ; Handle OUT command
  9218. ; ------------------
  9219. ; Syntax has been checked and the two comma-separated values are on the
  9220. ; calculator stack.
  9221.  
  9222. ;; OUT
  9223. L1E7A:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9224.                                 ; to BC and A.
  9225.         OUT     (C),A           ; perform the operation.
  9226.         RET                     ; return to STMT-RET.
  9227.  
  9228. ; -------------------
  9229. ; Handle POKE command
  9230. ; -------------------
  9231. ; This routine alters a single byte in the 64K address space.
  9232. ; Happily no check is made as to whether ROM or RAM is addressed.
  9233. ; Sinclair BASIC requires no poking of system variables.
  9234.  
  9235. ;; POKE
  9236. L1E80:  CALL    L1E85           ; routine TWO-PARAM fetches values
  9237.                                 ; to BC and A.
  9238.         LD      (BC),A          ; load memory location with A.
  9239.         RET                     ; return to STMT-RET.
  9240.  
  9241. ; ------------------------------------
  9242. ; Fetch two  parameters from calculator stack
  9243. ; ------------------------------------
  9244. ; This routine fetches a byte and word from the calculator stack
  9245. ; producing an error if either is out of range.
  9246.  
  9247. ;; TWO-PARAM
  9248. L1E85:  CALL    L2DD5           ; routine FP-TO-A
  9249.         JR      C,L1E9F         ; forward to REPORT-B if overflow occurred
  9250.  
  9251.         JR      Z,L1E8E         ; forward to TWO-P-1 if positive
  9252.  
  9253.         NEG                     ; negative numbers are made positive
  9254.  
  9255. ;; TWO-P-1
  9256. L1E8E:  PUSH    AF              ; save the value
  9257.         CALL    L1E99           ; routine FIND-INT2 gets integer to BC
  9258.         POP     AF              ; restore the value
  9259.         RET                     ; return
  9260.  
  9261. ; -------------
  9262. ; Find integers
  9263. ; -------------
  9264. ; The first of these routines fetches a 8-bit integer (range 0-255) from the
  9265. ; calculator stack to the accumulator and is used for colours, streams,
  9266. ; durations and coordinates.
  9267. ; The second routine fetches 16-bit integers to the BC register pair
  9268. ; and is used to fetch command and function arguments involving line numbers
  9269. ; or memory addresses and also array subscripts and tab arguments.
  9270. ; ->
  9271.  
  9272. ;; FIND-INT1
  9273. L1E94:  CALL    L2DD5           ; routine FP-TO-A
  9274.         JR      L1E9C           ; forward to FIND-I-1 for common exit routine.
  9275.  
  9276. ; ---
  9277.  
  9278. ; ->
  9279.  
  9280. ;; FIND-INT2
  9281. L1E99:  CALL    L2DA2           ; routine FP-TO-BC
  9282.  
  9283. ;; FIND-I-1
  9284. L1E9C:  JR      C,L1E9F         ; to REPORT-Bb with overflow.
  9285.  
  9286.         RET     Z               ; return if positive.
  9287.  
  9288.  
  9289. ;; REPORT-Bb
  9290. L1E9F:  RST     08H             ; ERROR-1
  9291.         DB    $0A             ; Error Report: Integer out of range
  9292.  
  9293. ; ------------------
  9294. ; Handle RUN command
  9295. ; ------------------
  9296. ; This command runs a program starting at an optional line.
  9297. ; It performs a 'RESTORE 0' then CLEAR
  9298.  
  9299. ;; RUN
  9300. L1EA1:  CALL    L1E67           ; routine GO-TO puts line number in
  9301.                                 ; system variables.
  9302.         LD      BC,$0000        ; prepare to set DATADD to first line.
  9303.         CALL    L1E45           ; routine REST-RUN does the 'restore'.
  9304.                                 ; Note BC still holds zero.
  9305.         JR      L1EAF           ; forward to CLEAR-RUN to clear variables
  9306.                                 ; without disturbing RAMTOP and
  9307.                                 ; exit indirectly to STMT-RET
  9308.  
  9309. ; --------------------
  9310. ; Handle CLEAR command
  9311. ; --------------------
  9312. ; This command reclaims the space used by the variables.
  9313. ; It also clears the screen and the GO SUB stack.
  9314. ; With an integer expression, it sets the uppermost memory
  9315. ; address within the BASIC system.
  9316. ; "Contrary to the manual, CLEAR doesn't execute a RESTORE" -
  9317. ; Steven Vickers, Pitman Pocket Guide to the Spectrum, 1984.
  9318.  
  9319. ;; CLEAR
  9320. L1EAC:  CALL    L1E99           ; routine FIND-INT2 fetches to BC.
  9321.  
  9322. ;; CLEAR-RUN
  9323. L1EAF:  LD      A,B             ; test for
  9324.         OR      C               ; zero.
  9325.         JR      NZ,L1EB7        ; skip to CLEAR-1 if not zero.
  9326.  
  9327.         LD      BC,($5CB2)      ; use the existing value of RAMTOP if zero.
  9328.  
  9329. ;; CLEAR-1
  9330. L1EB7:  PUSH    BC              ; save ramtop value.
  9331.  
  9332.         LD      DE,($5C4B)      ; fetch VARS
  9333.         LD      HL,($5C59)      ; fetch E_LINE
  9334.         DEC     HL              ; adjust to point at variables end-marker.
  9335.         CALL    L19E5           ; routine RECLAIM-1 reclaims the space used by
  9336.                                 ; the variables.
  9337.         CALL    L0D6B           ; routine CLS to clear screen.
  9338.         LD      HL,($5C65)      ; fetch STKEND the start of free memory.
  9339.         LD      DE,$0032        ; allow for another 50 bytes.
  9340.         ADD     HL,DE           ; add the overhead to HL.
  9341.  
  9342.         POP     DE              ; restore the ramtop value.
  9343.         SBC     HL,DE           ; if HL is greater than the value then jump
  9344.         JR      NC,L1EDA        ; forward to REPORT-M
  9345.                                 ; 'RAMTOP no good'
  9346.  
  9347.         LD      HL,($5CB4)      ; now P-RAMT ($7FFF on 16K RAM machine)
  9348.         AND     A               ; exact this time.
  9349.         SBC     HL,DE           ; new ramtop must be lower or the same.
  9350.         JR      NC,L1EDC        ; skip to CLEAR-2 if in actual RAM.
  9351.  
  9352. ;; REPORT-M
  9353. L1EDA:  RST     08H             ; ERROR-1
  9354.         DB    $15             ; Error Report: RAMTOP no good
  9355.  
  9356. ;; CLEAR-2
  9357. L1EDC:  EX      DE,HL           ; transfer ramtop value to HL.
  9358.         LD      ($5CB2),HL      ; update system variable RAMTOP.
  9359.         POP     DE              ; pop the return address STMT-RET.
  9360.         POP     BC              ; pop the Error Address.
  9361.         LD      (HL),$3E        ; now put the GO SUB end-marker at RAMTOP.
  9362.         DEC     HL              ; leave a location beneath it.
  9363.         LD      SP,HL           ; initialize the machine stack pointer.
  9364.         PUSH    BC              ; push the error address.
  9365.         LD      ($5C3D),SP      ; make ERR_SP point to location.
  9366.         EX      DE,HL           ; put STMT-RET in HL.
  9367.         JP      (HL)            ; and go there directly.
  9368.  
  9369. ; ---------------------
  9370. ; Handle GO SUB command
  9371. ; ---------------------
  9372. ; The GO SUB command diverts BASIC control to a new line number
  9373. ; in a very similar manner to GO TO but
  9374. ; the current line number and current statement + 1
  9375. ; are placed on the GO SUB stack as a RETURN point.
  9376.  
  9377. ;; GO-SUB
  9378. L1EED:  POP     DE              ; drop the address STMT-RET
  9379.         LD      H,(IY+$0D)      ; fetch statement from SUBPPC and
  9380.         INC     H               ; increment it
  9381.         EX      (SP),HL         ; swap - error address to HL,
  9382.                                 ; H (statement) at top of stack,
  9383.                                 ; L (unimportant) beneath.
  9384.         INC     SP              ; adjust to overwrite unimportant byte
  9385.         LD      BC,($5C45)      ; fetch the current line number from PPC
  9386.         PUSH    BC              ; and PUSH onto GO SUB stack.
  9387.                                 ; the empty machine-stack can be rebuilt
  9388.         PUSH    HL              ; push the error address.
  9389.         LD      ($5C3D),SP      ; make system variable ERR_SP point to it.
  9390.         PUSH    DE              ; push the address STMT-RET.
  9391.         CALL    L1E67           ; call routine GO-TO to update the system
  9392.                                 ; variables NEWPPC and NSPPC.
  9393.                                 ; then make an indirect exit to STMT-RET via
  9394.         LD      BC,$0014        ; a 20-byte overhead memory check.
  9395.  
  9396. ; ----------------------
  9397. ; Check available memory
  9398. ; ----------------------
  9399. ; This routine is used on many occasions when extending a dynamic area
  9400. ; upwards or the GO SUB stack downwards.
  9401.  
  9402. ;; TEST-ROOM
  9403. L1F05:  LD      HL,($5C65)      ; fetch STKEND
  9404.         ADD     HL,BC           ; add the supplied test value
  9405.         JR      C,L1F15         ; forward to REPORT-4 if over $FFFF
  9406.  
  9407.         EX      DE,HL           ; was less so transfer to DE
  9408.         LD      HL,$0050        ; test against another 80 bytes
  9409.         ADD     HL,DE           ; anyway
  9410.         JR      C,L1F15         ; forward to REPORT-4 if this passes $FFFF
  9411.  
  9412.         SBC     HL,SP           ; if less than the machine stack pointer
  9413.         RET     C               ; then return - OK.
  9414.  
  9415. ;; REPORT-4
  9416. L1F15:  LD      L,$03           ; prepare 'Out of Memory'
  9417.         JP      L0055           ; jump back to ERROR-3 at $0055
  9418.                                 ; Note. this error can't be trapped at $0008
  9419.  
  9420. ; ------------------------------
  9421. ; THE 'FREE MEMORY' USER ROUTINE
  9422. ; ------------------------------
  9423. ; This routine is not used by the ROM but allows users to evaluate
  9424. ; approximate free memory with PRINT 65536 - USR 7962.
  9425.  
  9426. ;; free-mem
  9427. L1F1A:  LD      BC,$0000        ; allow no overhead.
  9428.  
  9429.         CALL    L1F05           ; routine TEST-ROOM.
  9430.  
  9431.         LD      B,H             ; transfer the result
  9432.         LD      C,L             ; to the BC register.
  9433.         RET                     ; the USR function returns value of BC.
  9434.  
  9435. ; --------------------
  9436. ; THE 'RETURN' COMMAND
  9437. ; --------------------
  9438. ; As with any command, there are two values on the machine stack at the time
  9439. ; it is invoked.  The machine stack is below the GOSUB stack.  Both grow
  9440. ; downwards, the machine stack by two bytes, the GOSUB stack by 3 bytes.
  9441. ; The highest location is a statement byte followed by a two-byte line number.
  9442.  
  9443. ;; RETURN
  9444. L1F23:  POP     BC              ; drop the address STMT-RET.
  9445.         POP     HL              ; now the error address.
  9446.         POP     DE              ; now a possible BASIC return line.
  9447.         LD      A,D             ; the high byte $00 - $27 is
  9448.         CP      $3E             ; compared with the traditional end-marker $3E.
  9449.         JR      Z,L1F36         ; forward to REPORT-7 with a match.
  9450.                                 ; 'RETURN without GOSUB'
  9451.  
  9452. ; It was not the end-marker so a single statement byte remains at the base of
  9453. ; the calculator stack. It can't be popped off.
  9454.  
  9455.         DEC     SP              ; adjust stack pointer to create room for two
  9456.                                 ; bytes.
  9457.         EX      (SP),HL         ; statement to H, error address to base of
  9458.                                 ; new machine stack.
  9459.         EX      DE,HL           ; statement to D,  BASIC line number to HL.
  9460.         LD      ($5C3D),SP      ; adjust ERR_SP to point to new stack pointer
  9461.         PUSH    BC              ; now re-stack the address STMT-RET
  9462.         JP      L1E73           ; to GO-TO-2 to update statement and line
  9463.                                 ; system variables and exit indirectly to the
  9464.                                 ; address just pushed on stack.
  9465.  
  9466. ; ---
  9467.  
  9468. ;; REPORT-7
  9469. L1F36:  PUSH    DE              ; replace the end-marker.
  9470.         PUSH    HL              ; now restore the error address
  9471.                                 ; as will be required in a few clock cycles.
  9472.  
  9473.         RST     08H             ; ERROR-1
  9474.         DB    $06             ; Error Report: RETURN without GOSUB
  9475.  
  9476. ; --------------------
  9477. ; Handle PAUSE command
  9478. ; --------------------
  9479. ; The pause command takes as its parameter the number of interrupts
  9480. ; for which to wait. PAUSE 50 pauses for about a second.
  9481. ; PAUSE 0 pauses indefinitely.
  9482. ; Both forms can be finished by pressing a key.
  9483.  
  9484. ;; PAUSE
  9485. L1F3A:  CALL    L1E99           ; routine FIND-INT2 puts value in BC
  9486.  
  9487. ;; PAUSE-1
  9488. L1F3D:  HALT                    ; wait for interrupt.
  9489.         DEC     BC              ; decrease counter.
  9490.         LD      A,B             ; test if
  9491.         OR      C               ; result is zero.
  9492.         JR      Z,L1F4F         ; forward to PAUSE-END if so.
  9493.  
  9494.         LD      A,B             ; test if
  9495.         AND     C               ; now $FFFF
  9496.         INC     A               ; that is, initially zero.
  9497.         JR      NZ,L1F49        ; skip forward to PAUSE-2 if not.
  9498.  
  9499.         INC     BC              ; restore counter to zero.
  9500.  
  9501. ;; PAUSE-2
  9502. L1F49:  BIT     5,(IY+$01)      ; test FLAGS - has a new key been pressed ?
  9503.         JR      Z,L1F3D         ; back to PAUSE-1 if not.
  9504.  
  9505. ;; PAUSE-END
  9506. L1F4F:  RES     5,(IY+$01)      ; update FLAGS - signal no new key
  9507.         RET                     ; and return.
  9508.  
  9509. ; -------------------
  9510. ; Check for BREAK key
  9511. ; -------------------
  9512. ; This routine is called from COPY-LINE, when interrupts are disabled,
  9513. ; to test if BREAK (SHIFT - SPACE) is being pressed.
  9514. ; It is also called at STMT-RET after every statement.
  9515.  
  9516. ;; BREAK-KEY
  9517. L1F54:  LD      A,$7F           ; Input address: $7FFE
  9518.         IN      A,($FE)         ; read lower right keys
  9519.         RRA                     ; rotate bit 0 - SPACE
  9520.         RET     C               ; return if not reset
  9521.  
  9522.         LD      A,$FE           ; Input address: $FEFE
  9523.         IN      A,($FE)         ; read lower left keys
  9524.         RRA                     ; rotate bit 0 - SHIFT
  9525.         RET                     ; carry will be set if not pressed.
  9526.                                 ; return with no carry if both keys
  9527.                                 ; pressed.
  9528.  
  9529. ; ---------------------
  9530. ; Handle DEF FN command
  9531. ; ---------------------
  9532. ; e.g DEF FN r$(a$,a) = a$(a TO )
  9533. ; this 'command' is ignored in runtime but has its syntax checked
  9534. ; during line-entry.
  9535.  
  9536. ;; DEF-FN
  9537. L1F60:  CALL    L2530           ; routine SYNTAX-Z
  9538.         JR      Z,L1F6A         ; forward to DEF-FN-1 if parsing
  9539.  
  9540.         LD      A,$CE           ; else load A with 'DEF FN' and
  9541.         JP      L1E39           ; jump back to PASS-BY
  9542.  
  9543. ; ---
  9544.  
  9545. ; continue here if checking syntax.
  9546.  
  9547. ;; DEF-FN-1
  9548. L1F6A:  SET      6,(IY+$01)     ; set FLAGS  - Assume numeric result
  9549.         CALL    L2C8D           ; call routine ALPHA
  9550.         JR      NC,L1F89        ; if not then to DEF-FN-4 to jump to
  9551.                                 ; 'Nonsense in BASIC'
  9552.  
  9553.  
  9554.         RST     20H             ; NEXT-CHAR
  9555.         CP      $24             ; is it '$' ?
  9556.         JR      NZ,L1F7D        ; to DEF-FN-2 if not as numeric.
  9557.  
  9558.         RES     6,(IY+$01)      ; set FLAGS  - Signal string result
  9559.  
  9560.         RST     20H             ; get NEXT-CHAR
  9561.  
  9562. ;; DEF-FN-2
  9563. L1F7D:  CP      $28             ; is it '(' ?
  9564.         JR      NZ,L1FBD        ; to DEF-FN-7 'Nonsense in BASIC'
  9565.  
  9566.  
  9567.         RST     20H             ; NEXT-CHAR
  9568.         CP      $29             ; is it ')' ?
  9569.         JR      Z,L1FA6         ; to DEF-FN-6 if null argument
  9570.  
  9571. ;; DEF-FN-3
  9572. L1F86:  CALL    L2C8D           ; routine ALPHA checks that it is the expected
  9573.                                 ; alphabetic character.
  9574.  
  9575. ;; DEF-FN-4
  9576. L1F89:  JP      NC,L1C8A        ; to REPORT-C  if not
  9577.                                 ; 'Nonsense in BASIC'.
  9578.  
  9579.         EX      DE,HL           ; save pointer in DE
  9580.  
  9581.         RST     20H             ; NEXT-CHAR re-initializes HL from CH_ADD
  9582.                                 ; and advances.
  9583.         CP      $24             ; '$' ? is it a string argument.
  9584.         JR      NZ,L1F94        ; forward to DEF-FN-5 if not.
  9585.  
  9586.         EX      DE,HL           ; save pointer to '$' in DE
  9587.  
  9588.         RST     20H             ; NEXT-CHAR re-initializes HL and advances
  9589.  
  9590. ;; DEF-FN-5
  9591. L1F94:  EX      DE,HL           ; bring back pointer.
  9592.         LD      BC,$0006        ; the function requires six hidden bytes for
  9593.                                 ; each parameter passed.
  9594.                                 ; The first byte will be $0E
  9595.                                 ; then 5-byte numeric value
  9596.                                 ; or 5-byte string pointer.
  9597.  
  9598.         CALL    L1655           ; routine MAKE-ROOM creates space in program
  9599.                                 ; area.
  9600.  
  9601.         INC     HL              ; adjust HL (set by LDDR)
  9602.         INC     HL              ; to point to first location.
  9603.         LD      (HL),$0E        ; insert the 'hidden' marker.
  9604.  
  9605. ; Note. these invisible storage locations hold nothing meaningful for the
  9606. ; moment. They will be used every time the corresponding function is
  9607. ; evaluated in runtime.
  9608. ; Now consider the following character fetched earlier.
  9609.  
  9610.         CP      $2C             ; is it ',' ? (more than one parameter)
  9611.         JR      NZ,L1FA6        ; to DEF-FN-6 if not
  9612.  
  9613.  
  9614.         RST     20H             ; else NEXT-CHAR
  9615.         JR      L1F86           ; and back to DEF-FN-3
  9616.  
  9617. ; ---
  9618.  
  9619. ;; DEF-FN-6
  9620. L1FA6:  CP      $29             ; should close with a ')'
  9621.         JR      NZ,L1FBD        ; to DEF-FN-7 if not
  9622.                                 ; 'Nonsense in BASIC'
  9623.  
  9624.  
  9625.         RST     20H             ; get NEXT-CHAR
  9626.         CP      $3D             ; is it '=' ?
  9627.         JR      NZ,L1FBD        ; to DEF-FN-7 if not 'Nonsense...'
  9628.  
  9629.  
  9630.         RST     20H             ; address NEXT-CHAR
  9631.         LD      A,($5C3B)       ; get FLAGS which has been set above
  9632.         PUSH    AF              ; and preserve
  9633.  
  9634.         CALL    L24FB           ; routine SCANNING checks syntax of expression
  9635.                                 ; and also sets flags.
  9636.  
  9637.         POP     AF              ; restore previous flags
  9638.         XOR     (IY+$01)        ; xor with FLAGS - bit 6 should be same
  9639.                                 ; therefore will be reset.
  9640.         AND     $40             ; isolate bit 6.
  9641.  
  9642. ;; DEF-FN-7
  9643. L1FBD:  JP      NZ,L1C8A        ; jump back to REPORT-C if the expected result
  9644.                                 ; is not the same type.
  9645.                                 ; 'Nonsense in BASIC'
  9646.  
  9647.         CALL    L1BEE           ; routine CHECK-END will return early if
  9648.                                 ; at end of statement and move onto next
  9649.                                 ; else produce error report. >>>
  9650.  
  9651.                                 ; There will be no return to here.
  9652.  
  9653. ; -------------------------------
  9654. ; Returning early from subroutine
  9655. ; -------------------------------
  9656. ; All routines are capable of being run in two modes - syntax checking mode
  9657. ; and runtime mode.  This routine is called often to allow a routine to return
  9658. ; early if checking syntax.
  9659.  
  9660. ;; UNSTACK-Z
  9661. L1FC3:  CALL    L2530           ; routine SYNTAX-Z sets zero flag if syntax
  9662.                                 ; is being checked.
  9663.  
  9664.         POP     HL              ; drop the return address.
  9665.         RET      Z              ; return to previous call in chain if checking
  9666.                                 ; syntax.
  9667.  
  9668.         JP      (HL)            ; jump to return address as BASIC program is
  9669.                                 ; actually running.
  9670.  
  9671. ; ---------------------
  9672. ; Handle LPRINT command
  9673. ; ---------------------
  9674. ; A simple form of 'PRINT #3' although it can output to 16 streams.
  9675. ; Probably for compatibility with other BASICs particularly ZX81 BASIC.
  9676. ; An extra UDG might have been better.
  9677.  
  9678. ;; LPRINT
  9679. L1FC9:  LD      A,$03           ; the printer channel
  9680.         JR      L1FCF           ; forward to PRINT-1
  9681.  
  9682. ; ---------------------
  9683. ; Handle PRINT commands
  9684. ; ---------------------
  9685. ; The Spectrum's main stream output command.
  9686. ; The default stream is stream 2 which is normally the upper screen
  9687. ; of the computer. However the stream can be altered in range 0 - 15.
  9688.  
  9689. ;; PRINT
  9690. L1FCD:  LD      A,$02           ; the stream for the upper screen.
  9691.  
  9692. ; The LPRINT command joins here.
  9693.  
  9694. ;; PRINT-1
  9695. L1FCF:  CALL    L2530           ; routine SYNTAX-Z checks if program running
  9696.         CALL    NZ,L1601        ; routine CHAN-OPEN if so
  9697.         CALL    L0D4D           ; routine TEMPS sets temporary colours.
  9698.         CALL    L1FDF           ; routine PRINT-2 - the actual item
  9699.         CALL    L1BEE           ; routine CHECK-END gives error if not at end
  9700.                                 ; of statement
  9701.         RET                     ; and return >>>
  9702.  
  9703. ; ------------------------------------
  9704. ; this subroutine is called from above
  9705. ; and also from INPUT.
  9706.  
  9707. ;; PRINT-2
  9708. L1FDF:  RST     18H             ; GET-CHAR gets printable character
  9709.         CALL    L2045           ; routine PR-END-Z checks if more printing
  9710.         JR      Z,L1FF2         ; to PRINT-4 if not     e.g. just 'PRINT :'
  9711.  
  9712. ; This tight loop deals with combinations of positional controls and
  9713. ; print items. An early return can be made from within the loop
  9714. ; if the end of a print sequence is reached.
  9715.  
  9716. ;; PRINT-3
  9717. L1FE5:  CALL    L204E           ; routine PR-POSN-1 returns zero if more
  9718.                                 ; but returns early at this point if
  9719.                                 ; at end of statement!
  9720.                                 ;
  9721.         JR      Z,L1FE5         ; to PRINT-3 if consecutive positioners
  9722.  
  9723.         CALL    L1FFC           ; routine PR-ITEM-1 deals with strings etc.
  9724.         CALL    L204E           ; routine PR-POSN-1 for more position codes
  9725.         JR      Z,L1FE5         ; loop back to PRINT-3 if so
  9726.  
  9727. ;; PRINT-4
  9728. L1FF2:  CP      $29             ; return now if this is ')' from input-item.
  9729.                                 ; (see INPUT.)
  9730.         RET     Z               ; or continue and print carriage return in
  9731.                                 ; runtime
  9732.  
  9733. ; ---------------------
  9734. ; Print carriage return
  9735. ; ---------------------
  9736. ; This routine which continues from above prints a carriage return
  9737. ; in run-time. It is also called once from PRINT-POSN.
  9738.  
  9739. ;; PRINT-CR
  9740. L1FF5:  CALL    L1FC3           ; routine UNSTACK-Z
  9741.  
  9742.         LD      A,$0D           ; prepare a carriage return
  9743.  
  9744.         RST     10H             ; PRINT-A
  9745.         RET                     ; return
  9746.  
  9747.  
  9748. ; -----------
  9749. ; Print items
  9750. ; -----------
  9751. ; This routine deals with print items as in
  9752. ; PRINT AT 10,0;"The value of A is ";a
  9753. ; It returns once a single item has been dealt with as it is part
  9754. ; of a tight loop that considers sequences of positional and print items
  9755.  
  9756. ;; PR-ITEM-1
  9757. L1FFC:  RST     18H             ; GET-CHAR
  9758.         CP      $AC             ; is character 'AT' ?
  9759.         JR      NZ,L200E        ; forward to PR-ITEM-2 if not.
  9760.  
  9761.         CALL    L1C79           ; routine NEXT-2NUM  check for two comma
  9762.                                 ; separated numbers placing them on the
  9763.                                 ; calculator stack in runtime.
  9764.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9765.  
  9766.         CALL    L2307           ; routine STK-TO-BC get the numbers in B and C.
  9767.         LD      A,$16           ; prepare the 'at' control.
  9768.         JR      L201E           ; forward to PR-AT-TAB to print the sequence.
  9769.  
  9770. ; ---
  9771.  
  9772. ;; PR-ITEM-2
  9773. L200E:  CP      $AD             ; is character 'TAB' ?
  9774.         JR      NZ,L2024        ; to PR-ITEM-3 if not
  9775.  
  9776.  
  9777.         RST     20H             ; NEXT-CHAR to address next character
  9778.         CALL    L1C82           ; routine EXPT-1NUM
  9779.         CALL    L1FC3           ; routine UNSTACK-Z quits if checking syntax.
  9780.  
  9781.         CALL    L1E99           ; routine FIND-INT2 puts integer in BC.
  9782.         LD      A,$17           ; prepare the 'tab' control.
  9783.  
  9784. ;; PR-AT-TAB
  9785. L201E:  RST     10H             ; PRINT-A outputs the control
  9786.  
  9787.         LD      A,C             ; first value to A
  9788.         RST     10H             ; PRINT-A outputs it.
  9789.  
  9790.         LD      A,B             ; second value
  9791.         RST     10H             ; PRINT-A
  9792.  
  9793.         RET                     ; return - item finished >>>
  9794.  
  9795. ; ---
  9796.  
  9797. ; Now consider paper 2; #2; a$
  9798.  
  9799. ;; PR-ITEM-3
  9800. L2024:  CALL    L21F2           ; routine CO-TEMP-3 will print any colour
  9801.         RET     NC              ; items - return if success.
  9802.  
  9803.         CALL    L2070           ; routine STR-ALTER considers new stream
  9804.         RET     NC              ; return if altered.
  9805.  
  9806.         CALL    L24FB           ; routine SCANNING now to evaluate expression
  9807.         CALL    L1FC3           ; routine UNSTACK-Z if not runtime.
  9808.  
  9809.         BIT     6,(IY+$01)      ; test FLAGS  - Numeric or string result ?
  9810.         CALL    Z,L2BF1         ; routine STK-FETCH if string.
  9811.                                 ; note no flags affected.
  9812.         JP      NZ,L2DE3        ; to PRINT-FP to print if numeric >>>
  9813.  
  9814. ; It was a string expression - start in DE, length in BC
  9815. ; Now enter a loop to print it
  9816.  
  9817. ;; PR-STRING
  9818. L203C:  LD      A,B             ; this tests if the
  9819.         OR      C               ; length is zero and sets flag accordingly.
  9820.         DEC     BC              ; this doesn't but decrements counter.
  9821.         RET     Z               ; return if zero.
  9822.  
  9823.         LD      A,(DE)          ; fetch character.
  9824.         INC     DE              ; address next location.
  9825.  
  9826.         RST     10H             ; PRINT-A.
  9827.  
  9828.         JR      L203C           ; loop back to PR-STRING.
  9829.  
  9830. ; ---------------
  9831. ; End of printing
  9832. ; ---------------
  9833. ; This subroutine returns zero if no further printing is required
  9834. ; in the current statement.
  9835. ; The first terminator is found in  escaped input items only,
  9836. ; the others in print_items.
  9837.  
  9838. ;; PR-END-Z
  9839. L2045:  CP      $29             ; is character a ')' ?
  9840.         RET     Z               ; return if so -        e.g. INPUT (p$); a$
  9841.  
  9842. ;; PR-ST-END
  9843. L2048:  CP      $0D             ; is it a carriage return ?
  9844.         RET     Z               ; return also -         e.g. PRINT a
  9845.  
  9846.         CP      $3A             ; is character a ':' ?
  9847.         RET                     ; return - zero flag will be set if so.
  9848.                                 ;                       e.g. PRINT a :
  9849.  
  9850. ; --------------
  9851. ; Print position
  9852. ; --------------
  9853. ; This routine considers a single positional character ';', ',', '''
  9854.  
  9855. ;; PR-POSN-1
  9856. L204E:  RST     18H             ; GET-CHAR
  9857.         CP      $3B             ; is it ';' ?            
  9858.                                 ; i.e. print from last position.
  9859.         JR      Z,L2067         ; forward to PR-POSN-3 if so.
  9860.                                 ; i.e. do nothing.
  9861.  
  9862.         CP      $2C             ; is it ',' ?
  9863.                                 ; i.e. print at next tabstop.
  9864.         JR      NZ,L2061        ; forward to PR-POSN-2 if anything else.
  9865.  
  9866.         CALL    L2530           ; routine SYNTAX-Z
  9867.         JR      Z,L2067         ; forward to PR-POSN-3 if checking syntax.
  9868.  
  9869.         LD      A,$06           ; prepare the 'comma' control character.
  9870.  
  9871.         RST     10H             ; PRINT-A  outputs to current channel in
  9872.                                 ; run-time.
  9873.  
  9874.         JR      L2067           ; skip to PR-POSN-3.
  9875.  
  9876. ; ---
  9877.  
  9878. ; check for newline.
  9879.  
  9880. ;; PR-POSN-2
  9881. L2061:  CP      $27             ; is character a "'" ? (newline)
  9882.         RET     NZ              ; return if no match              >>>
  9883.  
  9884.         CALL    L1FF5           ; routine PRINT-CR outputs a carriage return
  9885.                                 ; in runtime only.
  9886.  
  9887. ;; PR-POSN-3
  9888. L2067:  RST     20H             ; NEXT-CHAR to A.
  9889.         CALL    L2045           ; routine PR-END-Z checks if at end.
  9890.         JR      NZ,L206E        ; to PR-POSN-4 if not.
  9891.  
  9892.         POP     BC              ; drop return address if at end.
  9893.  
  9894. ;; PR-POSN-4
  9895. L206E:  CP      A               ; reset the zero flag.
  9896.         RET                     ; and return to loop or quit.
  9897.  
  9898. ; ------------
  9899. ; Alter stream
  9900. ; ------------
  9901. ; This routine is called from PRINT ITEMS above, and also LIST as in
  9902. ; LIST #15
  9903.  
  9904. ;; STR-ALTER
  9905. L2070:  CP      $23             ; is character '#' ?
  9906.         SCF                     ; set carry flag.
  9907.         RET     NZ              ; return if no match.
  9908.  
  9909.  
  9910.         RST      20H            ; NEXT-CHAR
  9911.         CALL    L1C82           ; routine EXPT-1NUM gets stream number
  9912.         AND     A               ; prepare to exit early with carry reset
  9913.         CALL    L1FC3           ; routine UNSTACK-Z exits early if parsing
  9914.         CALL    L1E94           ; routine FIND-INT1 gets number off stack
  9915.         CP      $10             ; must be range 0 - 15 decimal.
  9916.         JP      NC,L160E        ; jump back to REPORT-Oa if not
  9917.                                 ; 'Invalid stream'.
  9918.  
  9919.         CALL    L1601           ; routine CHAN-OPEN
  9920.         AND     A               ; clear carry - signal item dealt with.
  9921.         RET                     ; return
  9922.  
  9923. ; --------------------
  9924. ; Handle INPUT command
  9925. ; --------------------
  9926. ; This command
  9927. ;
  9928.  
  9929. ;; INPUT
  9930. L2089:  CALL    L2530           ; routine SYNTAX-Z to check if in runtime.
  9931.         JR      Z,L2096         ; forward to INPUT-1 if checking syntax.
  9932.  
  9933.         LD      A,$01           ; select channel 'K' the keyboard for input.
  9934.         CALL    L1601           ; routine CHAN-OPEN opens it.
  9935.         CALL    L0D6E           ; routine CLS-LOWER clears the lower screen
  9936.                                 ; and sets DF_SZ to two.
  9937.  
  9938. ;; INPUT-1
  9939. L2096:  LD      (IY+$02),$01    ; update TV_FLAG - signal lower screen in use
  9940.                                 ; ensuring that the correct set of system
  9941.                                 ; variables are updated and that the border
  9942.                                 ; colour is used.
  9943.  
  9944.         CALL    L20C1           ; routine IN-ITEM-1 to handle the input.
  9945.  
  9946.         CALL    L1BEE           ; routine CHECK-END will make an early exit
  9947.                                 ; if checking syntax. >>>
  9948.  
  9949. ; keyboard input has been made and it remains to adjust the upper
  9950. ; screen in case the lower two lines have been extended upwards.
  9951.  
  9952.         LD      BC,($5C88)      ; fetch S_POSN current line/column of
  9953.                                 ; the upper screen.
  9954.         LD      A,($5C6B)       ; fetch DF_SZ the display file size of
  9955.                                 ; the lower screen.
  9956.         CP      B               ; test that lower screen does not overlap
  9957.         JR      C,L20AD         ; forward to INPUT-2 if not.
  9958.  
  9959. ; the two screens overlap so adjust upper screen.
  9960.  
  9961.         LD      C,$21           ; set column of upper screen to leftmost.
  9962.         LD      B,A             ; and line to one above lower screen.
  9963.                                 ; continue forward to update upper screen
  9964.                                 ; print position.
  9965.  
  9966. ;; INPUT-2
  9967. L20AD:  LD      ($5C88),BC      ; set S_POSN update upper screen line/column.
  9968.         LD      A,$19           ; subtract from twenty five
  9969.         SUB     B               ; the new line number.
  9970.         LD      ($5C8C),A       ; and place result in SCR_CT - scroll count.
  9971.         RES     0,(IY+$02)      ; update TV_FLAG - signal main screen in use.
  9972.         CALL    L0DD9           ; routine CL-SET sets the print position
  9973.                                 ; system variables for the upper screen.
  9974.         JP      L0D6E           ; jump back to CLS-LOWER and make
  9975.                                 ; an indirect exit >>.
  9976.  
  9977. ; ---------------------
  9978. ; INPUT ITEM subroutine
  9979. ; ---------------------
  9980. ; This subroutine deals with the input items and print items.
  9981. ; from  the current input channel.
  9982. ; It is only called from the above INPUT routine but was obviously
  9983. ; once called from somewhere else in another context.
  9984.  
  9985. ;; IN-ITEM-1
  9986. L20C1:  CALL    L204E           ; routine PR-POSN-1 deals with a single
  9987.                                 ; position item at each call.
  9988.         JR      Z,L20C1         ; back to IN-ITEM-1 until no more in a
  9989.                                 ; sequence.
  9990.  
  9991.         CP      $28             ; is character '(' ?
  9992.         JR      NZ,L20D8        ; forward to IN-ITEM-2 if not.
  9993.  
  9994. ; any variables within braces will be treated as part, or all, of the prompt
  9995. ; instead of being used as destination variables.
  9996.  
  9997.         RST     20H             ; NEXT-CHAR
  9998.         CALL    L1FDF           ; routine PRINT-2 to output the dynamic
  9999.                                 ; prompt.
  10000.  
  10001.         RST     18H             ; GET-CHAR
  10002.         CP      $29             ; is character a matching ')' ?
  10003.         JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  10004.                                 ; 'Nonsense in BASIC'.
  10005.  
  10006.         RST     20H             ; NEXT-CHAR
  10007.         JP      L21B2           ; forward to IN-NEXT-2
  10008.  
  10009. ; ---
  10010.  
  10011. ;; IN-ITEM-2
  10012. L20D8:  CP      $CA             ; is the character the token 'LINE' ?
  10013.         JR      NZ,L20ED        ; forward to IN-ITEM-3 if not.
  10014.  
  10015.         RST     20H             ; NEXT-CHAR - variable must come next.
  10016.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10017.                                 ; address of variable to be assigned.
  10018.                                 ; or generates an error if no variable
  10019.                                 ; at this position.
  10020.  
  10021.         SET     7,(IY+$37)      ; update FLAGX  - signal handling INPUT LINE
  10022.         BIT     6,(IY+$01)      ; test FLAGS  - numeric or string result ?
  10023.         JP      NZ,L1C8A        ; jump back to REPORT-C if not string
  10024.                                 ; 'Nonsense in BASIC'.
  10025.  
  10026.         JR      L20FA           ; forward to IN-PROMPT to set up workspace.
  10027.  
  10028. ; ---
  10029.  
  10030. ; the jump was here for other variables.
  10031.  
  10032. ;; IN-ITEM-3
  10033. L20ED:  CALL     L2C8D          ; routine ALPHA checks if character is
  10034.                                 ; a suitable variable name.
  10035.         JP      NC,L21AF        ; forward to IN-NEXT-1 if not
  10036.  
  10037.         CALL    L1C1F           ; routine CLASS-01 returns destination
  10038.                                 ; address of variable to be assigned.
  10039.         RES     7,(IY+$37)      ; update FLAGX  - signal not INPUT LINE.
  10040.  
  10041. ;; IN-PROMPT
  10042. L20FA:  CALL    L2530           ; routine SYNTAX-Z
  10043.         JP      Z,L21B2         ; forward to IN-NEXT-2 if checking syntax.
  10044.  
  10045.         CALL    L16BF           ; routine SET-WORK clears workspace.
  10046.         LD      HL,$5C71        ; point to system variable FLAGX
  10047.         RES     6,(HL)          ; signal string result.
  10048.         SET     5,(HL)          ; signal in Input Mode for editor.
  10049.         LD      BC,$0001        ; initialize space required to one for
  10050.                                 ; the carriage return.
  10051.         BIT     7,(HL)          ; test FLAGX - INPUT LINE in use ?
  10052.         JR      NZ,L211C        ; forward to IN-PR-2 if so as that is
  10053.                                 ; all the space that is required.
  10054.  
  10055.         LD      A,($5C3B)       ; load accumulator from FLAGS
  10056.         AND     $40             ; mask to test BIT 6 of FLAGS and clear
  10057.                                 ; the other bits in A.
  10058.                                 ; numeric result expected ?
  10059.         JR      NZ,L211A        ; forward to IN-PR-1 if so
  10060.  
  10061.         LD      C,$03           ; increase space to three bytes for the
  10062.                                 ; pair of surrounding quotes.
  10063.  
  10064. ;; IN-PR-1
  10065. L211A:  OR      (HL)            ; if numeric result, set bit 6 of FLAGX.
  10066.         LD      (HL),A          ; and update system variable
  10067.  
  10068. ;; IN-PR-2
  10069. L211C:  RST     30H             ; BC-SPACES opens 1 or 3 bytes in workspace
  10070.         LD      (HL),$0D        ; insert carriage return at last new location.
  10071.         LD      A,C             ; fetch the length, one or three.
  10072.         RRCA                    ; lose bit 0.
  10073.         RRCA                    ; test if quotes required.
  10074.         JR      NC,L2129        ; forward to IN-PR-3 if not.
  10075.  
  10076.         LD      A,$22           ; load the '"' character
  10077.         LD      (DE),A          ; place quote in first new location at DE.
  10078.         DEC     HL              ; decrease HL - from carriage return.
  10079.         LD      (HL),A          ; and place a quote in second location.
  10080.  
  10081. ;; IN-PR-3
  10082. L2129:  LD      ($5C5B),HL      ; set keyboard cursor K_CUR to HL
  10083.         BIT     7,(IY+$37)      ; test FLAGX  - is this INPUT LINE ??
  10084.         JR      NZ,L215E        ; forward to IN-VAR-3 if so as input will
  10085.                                 ; be accepted without checking its syntax.
  10086.  
  10087.         LD      HL,($5C5D)      ; fetch CH_ADD
  10088.         PUSH    HL              ; and save on stack.
  10089.         LD      HL,($5C3D)      ; fetch ERR_SP
  10090.         PUSH    HL              ; and save on stack
  10091.  
  10092. ;; IN-VAR-1
  10093. L213A:  LD      HL,L213A        ; address: IN-VAR-1 - this address
  10094.         PUSH    HL              ; is saved on stack to handle errors.
  10095.         BIT     4,(IY+$30)      ; test FLAGS2  - is K channel in use ?
  10096.         JR      Z,L2148         ; forward to IN-VAR-2 if not using the
  10097.                                 ; keyboard for input. (??)
  10098.  
  10099.         LD      ($5C3D),SP      ; set ERR_SP to point to IN-VAR-1 on stack.
  10100.  
  10101. ;; IN-VAR-2
  10102. L2148:  LD      HL,($5C61)      ; set HL to WORKSP - start of workspace.
  10103.         CALL    L11A7           ; routine REMOVE-FP removes floating point
  10104.                                 ; forms when looping in error condition.
  10105.         LD      (IY+$00),$FF    ; set ERR_NR to 'OK' cancelling the error.
  10106.                                 ; but X_PTR causes flashing error marker
  10107.                                 ; to be displayed at each call to the editor.
  10108.         CALL    L0F2C           ; routine EDITOR allows input to be entered
  10109.                                 ; or corrected if this is second time around.
  10110.  
  10111. ; if we pass to next then there are no system errors
  10112.  
  10113.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax
  10114.         CALL    L21B9           ; routine IN-ASSIGN checks syntax using
  10115.                                 ; the VAL-FET-2 and powerful SCANNING routines.
  10116.                                 ; any syntax error and its back to IN-VAR-1.
  10117.                                 ; but with the flashing error marker showing
  10118.                                 ; where the error is.
  10119.                                 ; Note. the syntax of string input has to be
  10120.                                 ; checked as the user may have removed the
  10121.                                 ; bounding quotes or escaped them as with
  10122.                                 ; "hat" + "stand" for example.
  10123. ; proceed if syntax passed.
  10124.  
  10125.         JR      L2161           ; jump forward to IN-VAR-4
  10126.  
  10127. ; ---
  10128.  
  10129. ; the jump was to here when using INPUT LINE.
  10130.  
  10131. ;; IN-VAR-3
  10132. L215E:  CALL    L0F2C           ; routine EDITOR is called for input
  10133.  
  10134. ; when ENTER received rejoin other route but with no syntax check.
  10135.  
  10136. ; INPUT and INPUT LINE converge here.
  10137.  
  10138. ;; IN-VAR-4
  10139. L2161:  LD      (IY+$22),$00    ; set K_CUR_hi to a low value so that the cursor
  10140.                                 ; no longer appears in the input line.
  10141.  
  10142.         CALL    L21D6           ; routine IN-CHAN-K tests if the keyboard
  10143.                                 ; is being used for input.
  10144.         JR      NZ,L2174        ; forward to IN-VAR-5 if using another input
  10145.                                 ; channel.
  10146.  
  10147. ; continue here if using the keyboard.
  10148.  
  10149.         CALL    L111D           ; routine ED-COPY overprints the edit line
  10150.                                 ; to the lower screen. The only visible
  10151.                                 ; affect is that the cursor disappears.
  10152.                                 ; if you're inputting more than one item in
  10153.                                 ; a statement then that becomes apparent.
  10154.  
  10155.         LD      BC,($5C82)      ; fetch line and column from ECHO_E
  10156.         CALL    L0DD9           ; routine CL-SET sets S-POSNL to those
  10157.                                 ; values.
  10158.  
  10159. ; if using another input channel rejoin here.
  10160.  
  10161. ;; IN-VAR-5
  10162. L2174:  LD      HL,$5C71        ; point HL to FLAGX
  10163.         RES     5,(HL)          ; signal not in input mode
  10164.         BIT     7,(HL)          ; is this INPUT LINE ?
  10165.         RES     7,(HL)          ; cancel the bit anyway.
  10166.         JR      NZ,L219B        ; forward to IN-VAR-6 if INPUT LINE.
  10167.  
  10168.         POP     HL              ; drop the looping address
  10169.         POP     HL              ; drop the the address of previous
  10170.                                 ; error handler.
  10171.         LD      ($5C3D),HL      ; set ERR_SP to point to it.
  10172.         POP     HL              ; drop original CH_ADD which points to
  10173.                                 ; INPUT command in BASIC line.
  10174.         LD      ($5C5F),HL      ; save in X_PTR while input is assigned.
  10175.         SET     7,(IY+$01)      ; update FLAGS - Signal running program
  10176.         CALL    L21B9           ; routine IN-ASSIGN is called again
  10177.                                 ; this time the variable will be assigned
  10178.                                 ; the input value without error.
  10179.                                 ; Note. the previous example now
  10180.                                 ; becomes "hatstand"
  10181.  
  10182.         LD      HL,($5C5F)      ; fetch stored CH_ADD value from X_PTR.
  10183.         LD      (IY+$26),$00    ; set X_PTR_hi so that iy is no longer relevant.
  10184.         LD      ($5C5D),HL      ; put restored value back in CH_ADD
  10185.         JR      L21B2           ; forward to IN-NEXT-2 to see if anything
  10186.                                 ; more in the INPUT list.
  10187.  
  10188. ; ---
  10189.  
  10190. ; the jump was to here with INPUT LINE only
  10191.  
  10192. ;; IN-VAR-6
  10193. L219B:  LD      HL,($5C63)      ; STKBOT points to the end of the input.
  10194.         LD      DE,($5C61)      ; WORKSP points to the beginning.
  10195.         SCF                     ; prepare for true subtraction.
  10196.         SBC     HL,DE           ; subtract to get length
  10197.         LD      B,H             ; transfer it to
  10198.         LD      C,L             ; the BC register pair.
  10199.         CALL    L2AB2           ; routine STK-STO-$ stores parameters on
  10200.                                 ; the calculator stack.
  10201.         CALL    L2AFF           ; routine LET assigns it to destination.
  10202.         JR      L21B2           ; forward to IN-NEXT-2 as print items
  10203.                                 ; not allowed with INPUT LINE.
  10204.                                 ; Note. that "hat" + "stand" will, for
  10205.                                 ; example, be unchanged as also would
  10206.                                 ; 'PRINT "Iris was here"'.
  10207.  
  10208. ; ---
  10209.  
  10210. ; the jump was to here when ALPHA found more items while looking for
  10211. ; a variable name.
  10212.  
  10213. ;; IN-NEXT-1
  10214. L21AF:  CALL    L1FFC           ; routine PR-ITEM-1 considers further items.
  10215.  
  10216. ;; IN-NEXT-2
  10217. L21B2:  CALL    L204E           ; routine PR-POSN-1 handles a position item.
  10218.         JP      Z,L20C1         ; jump back to IN-ITEM-1 if the zero flag
  10219.                                 ; indicates more items are present.
  10220.  
  10221.         RET                     ; return.
  10222.  
  10223. ; ---------------------------
  10224. ; INPUT ASSIGNMENT Subroutine
  10225. ; ---------------------------
  10226. ; This subroutine is called twice from the INPUT command when normal
  10227. ; keyboard input is assigned. On the first occasion syntax is checked
  10228. ; using SCANNING. The final call with the syntax flag reset is to make
  10229. ; the assignment.
  10230.  
  10231. ;; IN-ASSIGN
  10232. L21B9:  LD      HL,($5C61)      ; fetch WORKSP start of input
  10233.         LD      ($5C5D),HL      ; set CH_ADD to first character
  10234.  
  10235.         RST     18H             ; GET-CHAR ignoring leading white-space.
  10236.         CP      $E2             ; is it 'STOP'
  10237.         JR      Z,L21D0         ; forward to IN-STOP if so.
  10238.  
  10239.         LD      A,($5C71)       ; load accumulator from FLAGX
  10240.         CALL    L1C59           ; routine VAL-FET-2 makes assignment
  10241.                                 ; or goes through the motions if checking
  10242.                                 ; syntax. SCANNING is used.
  10243.  
  10244.         RST     18H             ; GET-CHAR
  10245.         CP      $0D             ; is it carriage return ?
  10246.         RET     Z               ; return if so
  10247.                                 ; either syntax is OK
  10248.                                 ; or assignment has been made.
  10249.  
  10250. ; if another character was found then raise an error.
  10251. ; User doesn't see report but the flashing error marker
  10252. ; appears in the lower screen.
  10253.  
  10254. ;; REPORT-Cb
  10255. L21CE:  RST     08H             ; ERROR-1
  10256.         DB    $0B             ; Error Report: Nonsense in BASIC
  10257.  
  10258. ;; IN-STOP
  10259. L21D0:  CALL    L2530           ; routine SYNTAX-Z (UNSTACK-Z?)
  10260.         RET     Z               ; return if checking syntax
  10261.                                 ; as user wouldn't see error report.
  10262.                                 ; but generate visible error report
  10263.                                 ; on second invocation.
  10264.  
  10265. ;; REPORT-H
  10266. L21D4:  RST     08H             ; ERROR-1
  10267.         DB    $10             ; Error Report: STOP in INPUT
  10268.  
  10269. ; ------------------
  10270. ; Test for channel K
  10271. ; ------------------
  10272. ; This subroutine is called once from the keyboard
  10273. ; INPUT command to check if the input routine in
  10274. ; use is the one for the keyboard.
  10275.  
  10276. ;; IN-CHAN-K
  10277. L21D6:  LD      HL,($5C51)      ; fetch address of current channel CURCHL
  10278.         INC     HL              ;
  10279.         INC     HL              ; advance past
  10280.         INC     HL              ; input and
  10281.         INC     HL              ; output streams
  10282.         LD      A,(HL)          ; fetch the channel identifier.
  10283.         CP      $4B             ; test for 'K'
  10284.         RET                     ; return with zero set if keyboard is use.
  10285.  
  10286. ; --------------------
  10287. ; Colour Item Routines
  10288. ; --------------------
  10289. ;
  10290. ; These routines have 3 entry points -
  10291. ; 1) CO-TEMP-2 to handle a series of embedded Graphic colour items.
  10292. ; 2) CO-TEMP-3 to handle a single embedded print colour item.
  10293. ; 3) CO TEMP-4 to handle a colour command such as FLASH 1
  10294. ;
  10295. ; "Due to a bug, if you bring in a peripheral channel and later use a colour
  10296. ;  statement, colour controls will be sent to it by mistake." - Steven Vickers
  10297. ;  Pitman Pocket Guide, 1984.
  10298. ;
  10299. ; To be fair, this only applies if the last channel was other than 'K', 'S'
  10300. ; or 'P', which are all that are supported by this ROM, but if that last
  10301. ; channel was a microdrive file, network channel etc. then
  10302. ; PAPER 6; CLS will not turn the screen yellow and
  10303. ; CIRCLE INK 2; 128,88,50 will not draw a red circle.
  10304. ;
  10305. ; This bug does not apply to embedded PRINT items as it is quite permissible
  10306. ; to mix stream altering commands and colour items.
  10307. ; The fix therefore would be to ensure that CLASS-07 and CLASS-09 make
  10308. ; channel 'S' the current channel when not checking syntax.
  10309. ; -----------------------------------------------------------------
  10310.  
  10311. ;; CO-TEMP-1
  10312. L21E1:  RST     20H             ; NEXT-CHAR
  10313.  
  10314. ; -> Entry point from CLASS-09. Embedded Graphic colour items.
  10315. ; e.g. PLOT INK 2; PAPER 8; 128,88
  10316. ; Loops till all colour items output, finally addressing the coordinates.
  10317.  
  10318. ;; CO-TEMP-2
  10319. L21E2:  CALL    L21F2           ; routine CO-TEMP-3 to output colour control.
  10320.         RET     C               ; return if nothing more to output. ->
  10321.  
  10322.  
  10323.         RST     18H             ; GET-CHAR
  10324.         CP      $2C             ; is it ',' separator ?
  10325.         JR      Z,L21E1         ; back if so to CO-TEMP-1
  10326.  
  10327.         CP      $3B             ; is it ';' separator ?
  10328.         JR      Z,L21E1         ; back to CO-TEMP-1 for more.
  10329.  
  10330.         JP      L1C8A           ; to REPORT-C (REPORT-Cb is within range)
  10331.                                 ; 'Nonsense in BASIC'
  10332.  
  10333. ; -------------------
  10334. ; CO-TEMP-3
  10335. ; -------------------
  10336. ; -> this routine evaluates and outputs a colour control and parameter.
  10337. ; It is called from above and also from PR-ITEM-3 to handle a single embedded
  10338. ; print item e.g. PRINT PAPER 6; "Hi". In the latter case, the looping for
  10339. ; multiple items is within the PR-ITEM routine.
  10340. ; It is quite permissible to send these to any stream.
  10341.  
  10342. ;; CO-TEMP-3
  10343. L21F2:  CP      $D9             ; is it 'INK' ?
  10344.         RET     C               ; return if less.
  10345.  
  10346.         CP      $DF             ; compare with 'OUT'
  10347.         CCF                     ; Complement Carry Flag
  10348.         RET     C               ; return if greater than 'OVER', $DE.
  10349.  
  10350.         PUSH    AF              ; save the colour token.
  10351.  
  10352.         RST     20H             ; address NEXT-CHAR
  10353.         POP     AF              ; restore token and continue.
  10354.  
  10355. ; -> this entry point used by CLASS-07. e.g. the command PAPER 6.
  10356.  
  10357. ;; CO-TEMP-4
  10358. L21FC:  SUB     $C9             ; reduce to control character $10 (INK)
  10359.                                 ; thru $15 (OVER).
  10360.         PUSH    AF              ; save control.
  10361.         CALL    L1C82           ; routine EXPT-1NUM stacks addressed
  10362.                                 ; parameter on calculator stack.
  10363.         POP     AF              ; restore control.
  10364.         AND     A               ; clear carry
  10365.  
  10366.         CALL    L1FC3           ; routine UNSTACK-Z returns if checking syntax.
  10367.  
  10368.         PUSH    AF              ; save again
  10369.         CALL    L1E94           ; routine FIND-INT1 fetches parameter to A.
  10370.         LD      D,A             ; transfer now to D
  10371.         POP     AF              ; restore control.
  10372.  
  10373.         RST     10H             ; PRINT-A outputs the control to current
  10374.                                 ; channel.
  10375.         LD      A,D             ; transfer parameter to A.
  10376.  
  10377.         RST     10H             ; PRINT-A outputs parameter.
  10378.         RET                     ; return. ->
  10379.  
  10380. ; -------------------------------------------------------------------------
  10381. ;
  10382. ;         {fl}{br}{   paper   }{  ink    }    The temporary colour attributes
  10383. ;          ___ ___ ___ ___ ___ ___ ___ ___    system variable.
  10384. ; ATTR_T  |   |   |   |   |   |   |   |   |
  10385. ;         |   |   |   |   |   |   |   |   |
  10386. ; 23695   |___|___|___|___|___|___|___|___|
  10387. ;           7   6   5   4   3   2   1   0
  10388. ;
  10389. ;
  10390. ;         {fl}{br}{   paper   }{  ink    }    The temporary mask used for
  10391. ;          ___ ___ ___ ___ ___ ___ ___ ___    transparent colours. Any bit
  10392. ; MASK_T  |   |   |   |   |   |   |   |   |   that is 1 shows that the
  10393. ;         |   |   |   |   |   |   |   |   |   corresponding attribute is
  10394. ; 23696   |___|___|___|___|___|___|___|___|   taken not from ATTR-T but from
  10395. ;           7   6   5   4   3   2   1   0     what is already on the screen.
  10396. ;
  10397. ;
  10398. ;         {paper9 }{ ink9 }{ inv1 }{ over1}   The print flags. Even bits are
  10399. ;          ___ ___ ___ ___ ___ ___ ___ ___    temporary flags. The odd bits
  10400. ; P_FLAG  |   |   |   |   |   |   |   |   |   are the permanent flags.
  10401. ;         | p | t | p | t | p | t | p | t |
  10402. ; 23697   |___|___|___|___|___|___|___|___|
  10403. ;           7   6   5   4   3   2   1   0
  10404. ;
  10405. ; -----------------------------------------------------------------------
  10406.  
  10407. ; ------------------------------------
  10408. ;  The colour system variable handler.
  10409. ; ------------------------------------
  10410. ; This is an exit branch from PO-1-OPER, PO-2-OPER
  10411. ; A holds control $10 (INK) to $15 (OVER)
  10412. ; D holds parameter 0-9 for ink/paper 0,1 or 8 for bright/flash,
  10413. ; 0 or 1 for over/inverse.
  10414.  
  10415. ;; CO-TEMP-5
  10416. L2211:  SUB     $11             ; reduce range $FF-$04
  10417.         ADC     A,$00           ; add in carry if INK
  10418.         JR      Z,L2234         ; forward to CO-TEMP-7 with INK and PAPER.
  10419.  
  10420.         SUB     $02             ; reduce range $FF-$02
  10421.         ADC     A,$00           ; add carry if FLASH
  10422.         JR      Z,L2273         ; forward to CO-TEMP-C with FLASH and BRIGHT.
  10423.  
  10424.         CP      $01             ; is it 'INVERSE' ?
  10425.         LD      A,D             ; fetch parameter for INVERSE/OVER
  10426.         LD      B,$01           ; prepare OVER mask setting bit 0.
  10427.         JR      NZ,L2228        ; forward to CO-TEMP-6 if OVER
  10428.  
  10429.         RLCA                    ; shift bit 0
  10430.         RLCA                    ; to bit 2
  10431.         LD      B,$04           ; set bit 2 of mask for inverse.
  10432.  
  10433. ;; CO-TEMP-6
  10434. L2228:  LD      C,A             ; save the A
  10435.         LD      A,D             ; re-fetch parameter
  10436.         CP      $02             ; is it less than 2
  10437.         JR      NC,L2244        ; to REPORT-K if not 0 or 1.
  10438.                                 ; 'Invalid colour'.
  10439.  
  10440.         LD      A,C             ; restore A
  10441.         LD      HL,$5C91        ; address system variable P_FLAG
  10442.         JR      L226C           ; forward to exit via routine CO-CHANGE
  10443.  
  10444. ; ---
  10445.  
  10446. ; the branch was here with INK/PAPER and carry set for INK.
  10447.  
  10448. ;; CO-TEMP-7
  10449. L2234:  LD      A,D             ; fetch parameter
  10450.         LD      B,$07           ; set ink mask 00000111
  10451.         JR      C,L223E         ; forward to CO-TEMP-8 with INK
  10452.  
  10453.         RLCA                    ; shift bits 0-2
  10454.         RLCA                    ; to
  10455.         RLCA                    ; bits 3-5
  10456.         LD      B,$38           ; set paper mask 00111000
  10457.  
  10458. ; both paper and ink rejoin here
  10459.  
  10460. ;; CO-TEMP-8
  10461. L223E:  LD      C,A             ; value to C
  10462.         LD      A,D             ; fetch parameter
  10463.         CP      $0A             ; is it less than 10d ?
  10464.         JR      C,L2246         ; forward to CO-TEMP-9 if so.
  10465.  
  10466. ; ink 10 etc. is not allowed.
  10467.  
  10468. ;; REPORT-K
  10469. L2244:  RST     08H             ; ERROR-1
  10470.         DB    $13             ; Error Report: Invalid colour
  10471.  
  10472. ;; CO-TEMP-9
  10473. L2246:  LD      HL,$5C8F        ; address system variable ATTR_T initially.
  10474.         CP      $08             ; compare with 8
  10475.         JR      C,L2258         ; forward to CO-TEMP-B with 0-7.
  10476.  
  10477.         LD      A,(HL)          ; fetch temporary attribute as no change.
  10478.         JR      Z,L2257         ; forward to CO-TEMP-A with INK/PAPER 8
  10479.  
  10480. ; it is either ink 9 or paper 9 (contrasting)
  10481.  
  10482.         OR      B               ; or with mask to make white
  10483.         CPL                     ; make black and change other to dark
  10484.         AND     $24             ; 00100100
  10485.         JR      Z,L2257         ; forward to CO-TEMP-A if black and
  10486.                                 ; originally light.
  10487.  
  10488.         LD      A,B             ; else just use the mask (white)
  10489.  
  10490. ;; CO-TEMP-A
  10491. L2257:  LD      C,A             ; save A in C
  10492.  
  10493. ;; CO-TEMP-B
  10494. L2258:  LD      A,C             ; load colour to A
  10495.         CALL    L226C           ; routine CO-CHANGE addressing ATTR-T
  10496.  
  10497.         LD      A,$07           ; put 7 in accumulator
  10498.         CP      D               ; compare with parameter
  10499.         SBC     A,A             ; $00 if 0-7, $FF if 8
  10500.         CALL    L226C           ; routine CO-CHANGE addressing MASK-T
  10501.                                 ; mask returned in A.
  10502.  
  10503. ; now consider P-FLAG.
  10504.  
  10505.         RLCA                    ; 01110000 or 00001110
  10506.         RLCA                    ; 11100000 or 00011100
  10507.         AND     $50             ; 01000000 or 00010000  (AND 01010000)
  10508.         LD      B,A             ; transfer to mask
  10509.         LD      A,$08           ; load A with 8
  10510.         CP      D               ; compare with parameter
  10511.         SBC     A,A             ; $FF if was 9,  $00 if 0-8
  10512.                                 ; continue while addressing P-FLAG
  10513.                                 ; setting bit 4 if ink 9
  10514.                                 ; setting bit 6 if paper 9
  10515.  
  10516. ; -----------------------
  10517. ; Handle change of colour
  10518. ; -----------------------
  10519. ; This routine addresses a system variable ATTR_T, MASK_T or P-FLAG in HL.
  10520. ; colour value in A, mask in B.
  10521.  
  10522. ;; CO-CHANGE
  10523. L226C:  XOR     (HL)            ; impress bits specified
  10524.         AND     B               ; by mask
  10525.         XOR     (HL)            ; on system variable.
  10526.         LD      (HL),A          ; update system variable.
  10527.         INC     HL              ; address next location.
  10528.         LD      A,B             ; put current value of mask in A
  10529.         RET                     ; return.
  10530.  
  10531. ; ---
  10532.  
  10533. ; the branch was here with flash and bright
  10534.  
  10535. ;; CO-TEMP-C
  10536. L2273:  SBC     A,A             ; set zero flag for bright.
  10537.         LD      A,D             ; fetch original parameter 0,1 or 8
  10538.         RRCA                    ; rotate bit 0 to bit 7
  10539.         LD      B,$80           ; mask for flash 10000000
  10540.         JR      NZ,L227D        ; forward to CO-TEMP-D if flash
  10541.  
  10542.         RRCA                    ; rotate bit 7 to bit 6
  10543.         LD      B,$40           ; mask for bright 01000000
  10544.  
  10545. ;; CO-TEMP-D
  10546. L227D:  LD      C,A             ; store value in C
  10547.         LD      A,D             ; fetch parameter
  10548.         CP      $08             ; compare with 8
  10549.         JR      Z,L2287         ; forward to CO-TEMP-E if 8
  10550.  
  10551.         CP      $02             ; test if 0 or 1
  10552.         JR      NC,L2244        ; back to REPORT-K if not
  10553.                                 ; 'Invalid colour'
  10554.  
  10555. ;; CO-TEMP-E
  10556. L2287:  LD      A,C             ; value to A
  10557.         LD      HL,$5C8F        ; address ATTR_T
  10558.         CALL    L226C           ; routine CO-CHANGE addressing ATTR_T
  10559.         LD      A,C             ; fetch value
  10560.         RRCA                    ; for flash8/bright8 complete
  10561.         RRCA                    ; rotations to put set bit in
  10562.         RRCA                    ; bit 7 (flash) bit 6 (bright)
  10563.         JR      L226C           ; back to CO-CHANGE addressing MASK_T
  10564.                                 ; and indirect return.
  10565.  
  10566. ; ---------------------
  10567. ; Handle BORDER command
  10568. ; ---------------------
  10569. ; Command syntax example: BORDER 7
  10570. ; This command routine sets the border to one of the eight colours.
  10571. ; The colours used for the lower screen are based on this.
  10572.  
  10573. ;; BORDER
  10574. L2294:  CALL    L1E94           ; routine FIND-INT1
  10575.         CP      $08             ; must be in range 0 (black) to 7 (white)
  10576.         JR      NC,L2244        ; back to REPORT-K if not
  10577.                                 ; 'Invalid colour'.
  10578.  
  10579.         OUT     ($FE),A         ; outputting to port effects an immediate
  10580.                                 ; change.
  10581.         RLCA                    ; shift the colour to
  10582.         RLCA                    ; the paper bits setting the
  10583.         RLCA                    ; ink colour black.
  10584.         BIT     5,A             ; is the number light coloured ?
  10585.                                 ; i.e. in the range green to white.
  10586.         JR      NZ,L22A6        ; skip to BORDER-1 if so
  10587.  
  10588.         XOR     $07             ; make the ink white.
  10589.  
  10590. ;; BORDER-1
  10591. L22A6:  LD      ($5C48),A       ; update BORDCR with new paper/ink
  10592.         RET                     ; return.
  10593.  
  10594. ; -----------------
  10595. ; Get pixel address
  10596. ; -----------------
  10597. ;
  10598. ;
  10599.  
  10600. ;; PIXEL-ADD
  10601. L22AA:  LD      A,$AF           ; load with 175 decimal.
  10602.         SUB     B               ; subtract the y value.
  10603.         JP      C,L24F9         ; jump forward to REPORT-Bc if greater.
  10604.                                 ; 'Integer out of range'
  10605.  
  10606. ; the high byte is derived from Y only.
  10607. ; the first 3 bits are always 010
  10608. ; the next 2 bits denote in which third of the screen the byte is.
  10609. ; the last 3 bits denote in which of the 8 scan lines within a third
  10610. ; the byte is located. There are 24 discrete values.
  10611.  
  10612.  
  10613.         LD      B,A             ; the line number from top of screen to B.
  10614.         AND     A               ; clear carry (already clear)
  10615.         RRA                     ;                     0xxxxxxx
  10616.         SCF                     ; set carry flag
  10617.         RRA                     ;                     10xxxxxx
  10618.         AND     A               ; clear carry flag
  10619.         RRA                     ;                     010xxxxx
  10620.  
  10621.         XOR     B               ;
  10622.         AND     $F8             ; keep the top 5 bits 11111000
  10623.         XOR     B               ;                     010xxbbb
  10624.         LD      H,A             ; transfer high byte to H.
  10625.  
  10626. ; the low byte is derived from both X and Y.
  10627.  
  10628.         LD      A,C             ; the x value 0-255.
  10629.         RLCA                    ;
  10630.         RLCA                    ;
  10631.         RLCA                    ;
  10632.         XOR     B               ; the y value
  10633.         AND     $C7             ; apply mask             11000111
  10634.         XOR     B               ; restore unmasked bits  xxyyyxxx
  10635.         RLCA                    ; rotate to              xyyyxxxx
  10636.         RLCA                    ; required position.     yyyxxxxx
  10637.         LD      L,A             ; low byte to L.
  10638.  
  10639. ; finally form the pixel position in A.
  10640.  
  10641.         LD      A,C             ; x value to A
  10642.         AND     $07             ; mod 8
  10643.         RET                     ; return
  10644.  
  10645. ; ----------------
  10646. ; Point Subroutine
  10647. ; ----------------
  10648. ; The point subroutine is called from s-point via the scanning functions
  10649. ; table.
  10650.  
  10651. ;; POINT-SUB
  10652. L22CB:  CALL    L2307           ; routine STK-TO-BC
  10653.         CALL    L22AA           ; routine PIXEL-ADD finds address of pixel.
  10654.         LD      B,A             ; pixel position to B, 0-7.
  10655.         INC     B               ; increment to give rotation count 1-8.
  10656.         LD      A,(HL)          ; fetch byte from screen.
  10657.  
  10658. ;; POINT-LP
  10659. L22D4:  RLCA                    ; rotate and loop back
  10660.         DJNZ    L22D4           ; to POINT-LP until pixel at right.
  10661.  
  10662.         AND      $01            ; test to give zero or one.
  10663.         JP      L2D28           ; jump forward to STACK-A to save result.
  10664.  
  10665. ; -------------------
  10666. ; Handle PLOT command
  10667. ; -------------------
  10668. ; Command Syntax example: PLOT 128,88
  10669. ;
  10670.  
  10671. ;; PLOT
  10672. L22DC:  CALL    L2307           ; routine STK-TO-BC
  10673.         CALL    L22E5           ; routine PLOT-SUB
  10674.         JP      L0D4D           ; to TEMPS
  10675.  
  10676. ; -------------------
  10677. ; The Plot subroutine
  10678. ; -------------------
  10679. ; A screen byte holds 8 pixels so it is necessary to rotate a mask
  10680. ; into the correct position to leave the other 7 pixels unaffected.
  10681. ; However all 64 pixels in the character cell take any embedded colour
  10682. ; items.
  10683. ; A pixel can be reset (inverse 1), toggled (over 1), or set ( with inverse
  10684. ; and over switches off). With both switches on, the byte is simply put
  10685. ; back on the screen though the colours may change.
  10686.  
  10687. ;; PLOT-SUB
  10688. L22E5:  LD      ($5C7D),BC      ; store new x/y values in COORDS
  10689.         CALL    L22AA           ; routine PIXEL-ADD gets address in HL,
  10690.                                 ; count from left 0-7 in B.
  10691.         LD      B,A             ; transfer count to B.
  10692.         INC     B               ; increase 1-8.
  10693.         LD      A,$FE           ; 11111110 in A.
  10694.  
  10695. ;; PLOT-LOOP
  10696. L22F0:  RRCA                    ; rotate mask.
  10697.         DJNZ    L22F0           ; to PLOT-LOOP until B circular rotations.
  10698.  
  10699.         LD      B,A             ; load mask to B
  10700.         LD      A,(HL)          ; fetch screen byte to A
  10701.  
  10702.         LD      C,(IY+$57)      ; P_FLAG to C
  10703.         BIT     0,C             ; is it to be OVER 1 ?
  10704.         JR      NZ,L22FD        ; forward to PL-TST-IN if so.
  10705.  
  10706. ; was over 0
  10707.  
  10708.         AND     B               ; combine with mask to blank pixel.
  10709.  
  10710. ;; PL-TST-IN
  10711. L22FD:  BIT     2,C             ; is it inverse 1 ?
  10712.         JR      NZ,L2303        ; to PLOT-END if so.
  10713.  
  10714.         XOR     B               ; switch the pixel
  10715.         CPL                     ; restore other 7 bits
  10716.  
  10717. ;; PLOT-END
  10718. L2303:  LD      (HL),A          ; load byte to the screen.
  10719.         JP      L0BDB           ; exit to PO-ATTR to set colours for cell.
  10720.  
  10721. ; ------------------------------
  10722. ; Put two numbers in BC register
  10723. ; ------------------------------
  10724. ;
  10725. ;
  10726.  
  10727. ;; STK-TO-BC
  10728. L2307:  CALL    L2314           ; routine STK-TO-A
  10729.         LD      B,A             ;
  10730.         PUSH    BC              ;
  10731.         CALL    L2314           ; routine STK-TO-A
  10732.         LD      E,C             ;
  10733.         POP     BC              ;
  10734.         LD      D,C             ;
  10735.         LD      C,A             ;
  10736.         RET                     ;
  10737.  
  10738. ; -----------------------
  10739. ; Put stack in A register
  10740. ; -----------------------
  10741. ; This routine puts the last value on the calculator stack into the accumulator
  10742. ; deleting the last value.
  10743.  
  10744. ;; STK-TO-A
  10745. L2314:  CALL    L2DD5           ; routine FP-TO-A compresses last value into
  10746.                                 ; accumulator. e.g. PI would become 3.
  10747.                                 ; zero flag set if positive.
  10748.         JP      C,L24F9         ; jump forward to REPORT-Bc if >= 255.5.
  10749.  
  10750.         LD      C,$01           ; prepare a positive sign byte.
  10751.         RET     Z               ; return if FP-TO-BC indicated positive.
  10752.  
  10753.         LD      C,$FF           ; prepare negative sign byte and
  10754.         RET                     ; return.
  10755.  
  10756.  
  10757. ; ---------------------
  10758. ; Handle CIRCLE command
  10759. ; ---------------------
  10760. ;
  10761. ; syntax has been partly checked using the class for draw command.
  10762.  
  10763. ;; CIRCLE
  10764. L2320:  RST     18H             ; GET-CHAR
  10765.         CP      $2C             ; is it required comma ?
  10766.         JP      NZ,L1C8A        ; jump to REPORT-C if not
  10767.  
  10768.  
  10769.         RST     20H             ; NEXT-CHAR
  10770.         CALL    L1C82           ; routine EXPT-1NUM fetches radius
  10771.         CALL    L1BEE           ; routine CHECK-END will return here if
  10772.                                 ; nothing follows command.
  10773.  
  10774.         RST     28H             ;; FP-CALC
  10775.         DB    $2A             ;;abs           ; make radius positive
  10776.         DB    $3D             ;;re-stack      ; in full floating point form
  10777.         DB    $38             ;;end-calc
  10778.  
  10779.         LD      A,(HL)          ; fetch first floating point byte
  10780.         CP      $81             ; compare to one
  10781.         JR      NC,L233B        ; forward to C-R-GRE-1 if circle radius
  10782.                                 ; is greater than one.
  10783.  
  10784.  
  10785.         RST     28H             ;; FP-CALC
  10786.         DB    $02             ;;delete        ; delete the radius from stack.
  10787.         DB    $38             ;;end-calc
  10788.  
  10789.         JR      L22DC           ; to PLOT to just plot x,y.
  10790.  
  10791. ; ---
  10792.  
  10793.  
  10794. ;; C-R-GRE-1
  10795. L233B:  RST     28H             ;; FP-CALC      ; x, y, r
  10796.         DB    $A3             ;;stk-pi/2      ; x, y, r, pi/2.
  10797.         DB    $38             ;;end-calc
  10798.  
  10799.         LD      (HL),$83        ;               ; x, y, r, 2*PI
  10800.  
  10801.         RST     28H             ;; FP-CALC
  10802.         DB    $C5             ;;st-mem-5      ; store 2*PI in mem-5
  10803.         DB    $02             ;;delete        ; x, y, z.
  10804.         DB    $38             ;;end-calc
  10805.  
  10806.         CALL    L247D           ; routine CD-PRMS1
  10807.         PUSH    BC              ;
  10808.  
  10809.         RST     28H             ;; FP-CALC
  10810.         DB    $31             ;;duplicate
  10811.         DB    $E1             ;;get-mem-1
  10812.         DB    $04             ;;multiply
  10813.         DB    $38             ;;end-calc
  10814.  
  10815.         LD      A,(HL)          ;
  10816.         CP      $80             ;
  10817.         JR      NC,L235A        ; to C-ARC-GE1
  10818.  
  10819.  
  10820.         RST     28H             ;; FP-CALC
  10821.         DB    $02             ;;delete
  10822.         DB    $02             ;;delete
  10823.         DB    $38             ;;end-calc
  10824.  
  10825.         POP     BC              ;
  10826.         JP      L22DC           ; JUMP to PLOT
  10827.  
  10828. ; ---
  10829.  
  10830.  
  10831. ;; C-ARC-GE1
  10832. L235A:  RST     28H             ;; FP-CALC
  10833.         DB    $C2             ;;st-mem-2
  10834.         DB    $01             ;;exchange
  10835.         DB    $C0             ;;st-mem-0
  10836.         DB    $02             ;;delete
  10837.         DB    $03             ;;subtract
  10838.         DB    $01             ;;exchange
  10839.         DB    $E0             ;;get-mem-0
  10840.         DB    $0F             ;;addition
  10841.         DB    $C0             ;;st-mem-0
  10842.         DB    $01             ;;exchange
  10843.         DB    $31             ;;duplicate
  10844.         DB    $E0             ;;get-mem-0
  10845.         DB    $01             ;;exchange
  10846.         DB    $31             ;;duplicate
  10847.         DB    $E0             ;;get-mem-0
  10848.         DB    $A0             ;;stk-zero
  10849.         DB    $C1             ;;st-mem-1
  10850.         DB    $02             ;;delete
  10851.         DB    $38             ;;end-calc
  10852.  
  10853.         INC     (IY+$62)        ; MEM-2-1st
  10854.         CALL     L1E94          ; routine FIND-INT1
  10855.         LD      L,A             ;
  10856.         PUSH    HL              ;
  10857.         CALL    L1E94           ; routine FIND-INT1
  10858.         POP     HL              ;
  10859.         LD      H,A             ;
  10860.         LD      ($5C7D),HL      ; COORDS
  10861.         POP     BC              ;
  10862.         JP      L2420           ; to DRW-STEPS
  10863.  
  10864.  
  10865. ; -------------------
  10866. ; Handle DRAW command
  10867. ; -------------------
  10868. ;
  10869. ;
  10870.  
  10871. ;; DRAW
  10872. L2382:  RST     18H             ; GET-CHAR
  10873.         CP      $2C             ;
  10874.         JR      Z,L238D         ; to DR-3-PRMS
  10875.  
  10876.         CALL    L1BEE           ; routine CHECK-END
  10877.         JP      L2477           ; to LINE-DRAW
  10878.  
  10879. ; ---
  10880.  
  10881. ;; DR-3-PRMS
  10882. L238D:  RST     20H             ; NEXT-CHAR
  10883.         CALL    L1C82           ; routine EXPT-1NUM
  10884.         CALL    L1BEE           ; routine CHECK-END
  10885.  
  10886.         RST     28H             ;; FP-CALC
  10887.         DB    $C5             ;;st-mem-5
  10888.         DB    $A2             ;;stk-half
  10889.         DB    $04             ;;multiply
  10890.         DB    $1F             ;;sin
  10891.         DB    $31             ;;duplicate
  10892.         DB    $30             ;;not
  10893.         DB    $30             ;;not
  10894.         DB    $00             ;;jump-true
  10895.  
  10896.         DB    $06             ;;to L23A3, DR-SIN-NZ
  10897.  
  10898.         DB    $02             ;;delete
  10899.         DB    $38             ;;end-calc
  10900.  
  10901.         JP      L2477           ; to LINE-DRAW
  10902.  
  10903. ; ---
  10904.  
  10905. ;; DR-SIN-NZ
  10906. L23A3:  DB    $C0             ;;st-mem-0
  10907.         DB    $02             ;;delete
  10908.         DB    $C1             ;;st-mem-1
  10909.         DB    $02             ;;delete
  10910.         DB    $31             ;;duplicate
  10911.         DB    $2A             ;;abs
  10912.         DB    $E1             ;;get-mem-1
  10913.         DB    $01             ;;exchange
  10914.         DB    $E1             ;;get-mem-1
  10915.         DB    $2A             ;;abs
  10916.         DB    $0F             ;;addition
  10917.         DB    $E0             ;;get-mem-0
  10918.         DB    $05             ;;division
  10919.         DB    $2A             ;;abs
  10920.         DB    $E0             ;;get-mem-0
  10921.         DB    $01             ;;exchange
  10922.         DB    $3D             ;;re-stack
  10923.         DB    $38             ;;end-calc
  10924.  
  10925.         LD      A,(HL)          ;
  10926.         CP      $81             ;
  10927.         JR      NC,L23C1        ; to DR-PRMS
  10928.  
  10929.  
  10930.         RST     28H             ;; FP-CALC
  10931.         DB    $02             ;;delete
  10932.         DB    $02             ;;delete
  10933.         DB    $38             ;;end-calc
  10934.  
  10935.         JP      L2477           ; to LINE-DRAW
  10936.  
  10937. ; ---
  10938.  
  10939. ;; DR-PRMS
  10940. L23C1:  CALL    L247D           ; routine CD-PRMS1
  10941.         PUSH    BC              ;
  10942.  
  10943.         RST     28H             ;; FP-CALC
  10944.         DB    $02             ;;delete
  10945.         DB    $E1             ;;get-mem-1
  10946.         DB    $01             ;;exchange
  10947.         DB    $05             ;;division
  10948.         DB    $C1             ;;st-mem-1
  10949.         DB    $02             ;;delete
  10950.         DB    $01             ;;exchange
  10951.         DB    $31             ;;duplicate
  10952.         DB    $E1             ;;get-mem-1
  10953.         DB    $04             ;;multiply
  10954.         DB    $C2             ;;st-mem-2
  10955.         DB    $02             ;;delete
  10956.         DB    $01             ;;exchange
  10957.         DB    $31             ;;duplicate
  10958.         DB    $E1             ;;get-mem-1
  10959.         DB    $04             ;;multiply
  10960.         DB    $E2             ;;get-mem-2
  10961.         DB    $E5             ;;get-mem-5
  10962.         DB    $E0             ;;get-mem-0
  10963.         DB    $03             ;;subtract
  10964.         DB    $A2             ;;stk-half
  10965.         DB    $04             ;;multiply
  10966.         DB    $31             ;;duplicate
  10967.         DB    $1F             ;;sin
  10968.         DB    $C5             ;;st-mem-5
  10969.         DB    $02             ;;delete
  10970.         DB    $20             ;;cos
  10971.         DB    $C0             ;;st-mem-0
  10972.         DB    $02             ;;delete
  10973.         DB    $C2             ;;st-mem-2
  10974.         DB    $02             ;;delete
  10975.         DB    $C1             ;;st-mem-1
  10976.         DB    $E5             ;;get-mem-5
  10977.         DB    $04             ;;multiply
  10978.         DB    $E0             ;;get-mem-0
  10979.         DB    $E2             ;;get-mem-2
  10980.         DB    $04             ;;multiply
  10981.         DB    $0F             ;;addition
  10982.         DB    $E1             ;;get-mem-1
  10983.         DB    $01             ;;exchange
  10984.         DB    $C1             ;;st-mem-1
  10985.         DB    $02             ;;delete
  10986.         DB    $E0             ;;get-mem-0
  10987.         DB    $04             ;;multiply
  10988.         DB    $E2             ;;get-mem-2
  10989.         DB    $E5             ;;get-mem-5
  10990.         DB    $04             ;;multiply
  10991.         DB    $03             ;;subtract
  10992.         DB    $C2             ;;st-mem-2
  10993.         DB    $2A             ;;abs
  10994.         DB    $E1             ;;get-mem-1
  10995.         DB    $2A             ;;abs
  10996.         DB    $0F             ;;addition
  10997.         DB    $02             ;;delete
  10998.         DB    $38             ;;end-calc
  10999.  
  11000.         LD      A,(DE)          ;
  11001.         CP       $81            ;
  11002.         POP     BC              ;
  11003.         JP      C,L2477         ; to LINE-DRAW
  11004.  
  11005.         PUSH    BC              ;
  11006.  
  11007.         RST     28H             ;; FP-CALC
  11008.         DB    $01             ;;exchange
  11009.         DB    $38             ;;end-calc
  11010.  
  11011.         LD      A,($5C7D)       ; COORDS-x
  11012.         CALL    L2D28           ; routine STACK-A
  11013.  
  11014.         RST     28H             ;; FP-CALC
  11015.         DB    $C0             ;;st-mem-0
  11016.         DB    $0F             ;;addition
  11017.         DB    $01             ;;exchange
  11018.         DB    $38             ;;end-calc
  11019.  
  11020.         LD      A,($5C7E)       ; COORDS-y
  11021.         CALL    L2D28           ; routine STACK-A
  11022.  
  11023.         RST     28H             ;; FP-CALC
  11024.         DB    $C5             ;;st-mem-5
  11025.         DB    $0F             ;;addition
  11026.         DB    $E0             ;;get-mem-0
  11027.         DB    $E5             ;;get-mem-5
  11028.         DB    $38             ;;end-calc
  11029.  
  11030.         POP     BC              ;
  11031.  
  11032. ;; DRW-STEPS
  11033. L2420:  DEC     B               ;
  11034.         JR      Z,L245F         ; to ARC-END
  11035.  
  11036.         JR      L2439           ; to ARC-START
  11037.  
  11038. ; ---
  11039.  
  11040.  
  11041. ;; ARC-LOOP
  11042. L2425:  RST     28H             ;; FP-CALC
  11043.         DB    $E1             ;;get-mem-1
  11044.         DB    $31             ;;duplicate
  11045.         DB    $E3             ;;get-mem-3
  11046.         DB    $04             ;;multiply
  11047.         DB    $E2             ;;get-mem-2
  11048.         DB    $E4             ;;get-mem-4
  11049.         DB    $04             ;;multiply
  11050.         DB    $03             ;;subtract
  11051.         DB    $C1             ;;st-mem-1
  11052.         DB    $02             ;;delete
  11053.         DB    $E4             ;;get-mem-4
  11054.         DB    $04             ;;multiply
  11055.         DB    $E2             ;;get-mem-2
  11056.         DB    $E3             ;;get-mem-3
  11057.         DB    $04             ;;multiply
  11058.         DB    $0F             ;;addition
  11059.         DB    $C2             ;;st-mem-2
  11060.         DB    $02             ;;delete
  11061.         DB    $38             ;;end-calc
  11062.  
  11063. ;; ARC-START
  11064. L2439:  PUSH    BC              ;
  11065.  
  11066.         RST     28H             ;; FP-CALC
  11067.         DB    $C0             ;;st-mem-0
  11068.         DB    $02             ;;delete
  11069.         DB    $E1             ;;get-mem-1
  11070.         DB    $0F             ;;addition
  11071.         DB    $31             ;;duplicate
  11072.         DB    $38             ;;end-calc
  11073.  
  11074.         LD      A,($5C7D)       ; COORDS-x
  11075.         CALL    L2D28           ; routine STACK-A
  11076.  
  11077.         RST     28H             ;; FP-CALC
  11078.         DB    $03             ;;subtract
  11079.         DB    $E0             ;;get-mem-0
  11080.         DB    $E2             ;;get-mem-2
  11081.         DB    $0F             ;;addition
  11082.         DB    $C0             ;;st-mem-0
  11083.         DB    $01             ;;exchange
  11084.         DB    $E0             ;;get-mem-0
  11085.         DB    $38             ;;end-calc
  11086.  
  11087.         LD      A,($5C7E)       ; COORDS-y
  11088.         CALL    L2D28           ; routine STACK-A
  11089.  
  11090.         RST     28H             ;; FP-CALC
  11091.         DB    $03             ;;subtract
  11092.         DB    $38             ;;end-calc
  11093.  
  11094.         CALL    L24B7           ; routine DRAW-LINE
  11095.         POP     BC              ;
  11096.         DJNZ    L2425           ; to ARC-LOOP
  11097.  
  11098.  
  11099. ;; ARC-END
  11100. L245F:  RST     28H             ;; FP-CALC
  11101.         DB    $02             ;;delete
  11102.         DB    $02             ;;delete
  11103.         DB    $01             ;;exchange
  11104.         DB    $38             ;;end-calc
  11105.  
  11106.         LD      A,($5C7D)       ; COORDS-x
  11107.         CALL    L2D28           ; routine STACK-A
  11108.  
  11109.         RST     28H             ;; FP-CALC
  11110.         DB    $03             ;;subtract
  11111.         DB    $01             ;;exchange
  11112.         DB    $38             ;;end-calc
  11113.  
  11114.         LD      A,($5C7E)       ; COORDS-y
  11115.         CALL    L2D28           ; routine STACK-A
  11116.  
  11117.         RST     28H             ;; FP-CALC
  11118.         DB    $03             ;;subtract
  11119.         DB    $38             ;;end-calc
  11120.  
  11121. ;; LINE-DRAW
  11122. L2477:  CALL    L24B7           ; routine DRAW-LINE
  11123.         JP      L0D4D           ; to TEMPS
  11124.  
  11125.  
  11126. ; ------------------
  11127. ; Initial parameters
  11128. ; ------------------
  11129. ;
  11130. ;
  11131.  
  11132. ;; CD-PRMS1
  11133. L247D:  RST     28H             ;; FP-CALC
  11134.         DB    $31             ;;duplicate
  11135.         DB    $28             ;;sqr
  11136.         DB    $34             ;;stk-data
  11137.         DB    $32             ;;Exponent: $82, Bytes: 1
  11138.         DB    $00             ;;(+00,+00,+00)
  11139.         DB    $01             ;;exchange
  11140.         DB    $05             ;;division
  11141.         DB    $E5             ;;get-mem-5
  11142.         DB    $01             ;;exchange
  11143.         DB    $05             ;;division
  11144.         DB    $2A             ;;abs
  11145.         DB    $38             ;;end-calc
  11146.  
  11147.         CALL    L2DD5           ; routine FP-TO-A
  11148.         JR      C,L2495         ; to USE-252
  11149.  
  11150.         AND     $FC             ;
  11151.         ADD     A,$04           ;
  11152.         JR      NC,L2497        ; to DRAW-SAVE
  11153.  
  11154. ;; USE-252
  11155. L2495:  LD      A,$FC           ;
  11156.  
  11157. ;; DRAW-SAVE
  11158. L2497:  PUSH    AF              ;
  11159.         CALL    L2D28           ; routine STACK-A
  11160.  
  11161.         RST     28H             ;; FP-CALC
  11162.         DB    $E5             ;;get-mem-5
  11163.         DB    $01             ;;exchange
  11164.         DB    $05             ;;division
  11165.         DB    $31             ;;duplicate
  11166.         DB    $1F             ;;sin
  11167.         DB    $C4             ;;st-mem-4
  11168.         DB    $02             ;;delete
  11169.         DB    $31             ;;duplicate
  11170.         DB    $A2             ;;stk-half
  11171.         DB    $04             ;;multiply
  11172.         DB    $1F             ;;sin
  11173.         DB    $C1             ;;st-mem-1
  11174.         DB    $01             ;;exchange
  11175.         DB    $C0             ;;st-mem-0
  11176.         DB    $02             ;;delete
  11177.         DB    $31             ;;duplicate
  11178.         DB    $04             ;;multiply
  11179.         DB    $31             ;;duplicate
  11180.         DB    $0F             ;;addition
  11181.         DB    $A1             ;;stk-one
  11182.         DB    $03             ;;subtract
  11183.         DB    $1B             ;;negate
  11184.         DB    $C3             ;;st-mem-3
  11185.         DB    $02             ;;delete
  11186.         DB    $38             ;;end-calc
  11187.  
  11188.         POP     BC              ;
  11189.         RET                     ;
  11190.  
  11191. ; ------------
  11192. ; Line drawing
  11193. ; ------------
  11194. ;
  11195. ;
  11196.  
  11197. ;; DRAW-LINE
  11198. L24B7:  CALL    L2307           ; routine STK-TO-BC
  11199.         LD      A,C             ;
  11200.         CP      B               ;
  11201.         JR      NC,L24C4        ; to DL-X-GE-Y
  11202.  
  11203.         LD      L,C             ;
  11204.         PUSH    DE              ;
  11205.         XOR     A               ;
  11206.         LD      E,A             ;
  11207.         JR      L24CB           ; to DL-LARGER
  11208.  
  11209. ; ---
  11210.  
  11211. ;; DL-X-GE-Y
  11212. L24C4:  OR      C               ;
  11213.         RET     Z               ;
  11214.  
  11215.         LD      L,B             ;
  11216.         LD      B,C             ;
  11217.         PUSH    DE              ;
  11218.         LD      D,$00           ;
  11219.  
  11220. ;; DL-LARGER
  11221. L24CB:  LD      H,B             ;
  11222.         LD      A,B             ;
  11223.         RRA                     ;
  11224.  
  11225. ;; D-L-LOOP
  11226. L24CE:  ADD     A,L             ;
  11227.         JR      C,L24D4         ; to D-L-DIAG
  11228.  
  11229.         CP      H               ;
  11230.         JR      C,L24DB         ; to D-L-HR-VT
  11231.  
  11232. ;; D-L-DIAG
  11233. L24D4:  SUB     H               ;
  11234.         LD      C,A             ;
  11235.         EXX                     ;
  11236.         POP     BC              ;
  11237.         PUSH    BC              ;
  11238.         JR      L24DF           ; to D-L-STEP
  11239.  
  11240. ; ---
  11241.  
  11242. ;; D-L-HR-VT
  11243. L24DB:  LD      C,A             ;
  11244.         PUSH    DE              ;
  11245.         EXX                     ;
  11246.         POP     BC              ;
  11247.  
  11248. ;; D-L-STEP
  11249. L24DF:  LD      HL,($5C7D)      ; COORDS
  11250.         LD      A,B             ;
  11251.         ADD     A,H             ;
  11252.         LD      B,A             ;
  11253.         LD      A,C             ;
  11254.         INC     A               ;
  11255.         ADD     A,L             ;
  11256.         JR      C,L24F7         ; to D-L-RANGE
  11257.  
  11258.         JR      Z,L24F9         ; to REPORT-Bc
  11259.  
  11260. ;; D-L-PLOT
  11261. L24EC:  DEC     A               ;
  11262.         LD      C,A             ;
  11263.         CALL    L22E5           ; routine PLOT-SUB
  11264.         EXX                     ;
  11265.         LD      A,C             ;
  11266.         DJNZ    L24CE           ; to D-L-LOOP
  11267.  
  11268.         POP     DE              ;
  11269.         RET                     ;
  11270.  
  11271. ; ---
  11272.  
  11273. ;; D-L-RANGE
  11274. L24F7:  JR      Z,L24EC         ; to D-L-PLOT
  11275.  
  11276.  
  11277. ;; REPORT-Bc
  11278. L24F9:  RST     08H             ; ERROR-1
  11279.         DB    $0A             ; Error Report: Integer out of range
  11280.  
  11281.  
  11282.  
  11283. ;***********************************
  11284. ;** Part 8. EXPRESSION EVALUATION **
  11285. ;***********************************
  11286. ;
  11287. ; It is a this stage of the ROM that the Spectrum ceases altogether to be
  11288. ; just a colourful novelty. One remarkable feature is that in all previous
  11289. ; commands when the Spectrum is expecting a number or a string then an
  11290. ; expression of the same type can be substituted ad infinitum.
  11291. ; This is the routine that evaluates that expression.
  11292. ; This is what causes 2 + 2 to give the answer 4.
  11293. ; That is quite easy to understand. However you don't have to make it much
  11294. ; more complex to start a remarkable juggling act.
  11295. ; e.g. PRINT 2 * (VAL "2+2" + TAN 3)
  11296. ; In fact, provided there is enough free RAM, the Spectrum can evaluate
  11297. ; an expression of unlimited complexity.
  11298. ; Apart from a couple of minor glitches, which you can now correct, the
  11299. ; system is remarkably robust.
  11300.  
  11301.  
  11302. ; ---------------------------------
  11303. ; Scan expression or sub-expression
  11304. ; ---------------------------------
  11305. ;
  11306. ;
  11307.  
  11308. ;; SCANNING
  11309. L24FB:  RST     18H             ; GET-CHAR
  11310.         LD      B,$00           ; priority marker zero is pushed on stack
  11311.                                 ; to signify end of expression when it is
  11312.                                 ; popped off again.
  11313.         PUSH    BC              ; put in on stack.
  11314.                                 ; and proceed to consider the first character
  11315.                                 ; of the expression.
  11316.  
  11317. ;; S-LOOP-1
  11318. L24FF:  LD      C,A             ; store the character while a look up is done.
  11319.         LD      HL,L2596        ; Address: scan-func
  11320.         CALL    L16DC           ; routine INDEXER is called to see if it is
  11321.                                 ; part of a limited range '+', '(', 'ATTR' etc.
  11322.  
  11323.         LD      A,C             ; fetch the character back
  11324.         JP      NC,L2684        ; jump forward to S-ALPHNUM if not in primary
  11325.                                 ; operators and functions to consider in the
  11326.                                 ; first instance a digit or a variable and
  11327.                                 ; then anything else.                >>>
  11328.  
  11329.         LD      B,$00           ; but here if it was found in table so
  11330.         LD      C,(HL)          ; fetch offset from table and make B zero.
  11331.         ADD     HL,BC           ; add the offset to position found
  11332.         JP      (HL)            ; and jump to the routine e.g. S-BIN
  11333.                                 ; making an indirect exit from there.
  11334.  
  11335. ; -------------------------------------------------------------------------
  11336. ; The four service subroutines for routines in the scannings function table
  11337. ; -------------------------------------------------------------------------
  11338.  
  11339. ; PRINT """Hooray!"" he cried."
  11340.  
  11341. ;; S-QUOTE-S
  11342. L250F:  CALL    L0074           ; routine CH-ADD+1 points to next character
  11343.                                 ; and fetches that character.
  11344.         INC     BC              ; increase length counter.
  11345.         CP      $0D             ; is it carriage return ?
  11346.                                 ; inside a quote.
  11347.         JP      Z,L1C8A         ; jump back to REPORT-C if so.
  11348.                                 ; 'Nonsense in BASIC'.
  11349.  
  11350.         CP      $22             ; is it a quote '"' ?
  11351.         JR      NZ,L250F        ; back to S-QUOTE-S if not for more.
  11352.  
  11353.         CALL    L0074           ; routine CH-ADD+1
  11354.         CP      $22             ; compare with possible adjacent quote
  11355.         RET                     ; return. with zero set if two together.
  11356.  
  11357. ; ---
  11358.  
  11359. ; This subroutine is used to get two coordinate expressions for the three
  11360. ; functions SCREEN$, ATTR and POINT that have two fixed parameters and
  11361. ; therefore require surrounding braces.
  11362.  
  11363. ;; S-2-COORD
  11364. L2522:  RST     20H             ; NEXT-CHAR
  11365.         CP      $28             ; is it the opening '(' ?
  11366.         JR      NZ,L252D        ; forward to S-RPORT-C if not
  11367.                                 ; 'Nonsense in BASIC'.
  11368.  
  11369.         CALL    L1C79           ; routine NEXT-2NUM gets two comma-separated
  11370.                                 ; numeric expressions. Note. this could cause
  11371.                                 ; many more recursive calls to SCANNING but
  11372.                                 ; the parent function will be evaluated fully
  11373.                                 ; before rejoining the main juggling act.
  11374.  
  11375.         RST     18H             ; GET-CHAR
  11376.         CP      $29             ; is it the closing ')' ?
  11377.  
  11378. ;; S-RPORT-C
  11379. L252D:  JP      NZ,L1C8A        ; jump back to REPORT-C if not.
  11380.                                 ; 'Nonsense in BASIC'.
  11381.  
  11382. ; ------------
  11383. ; Check syntax
  11384. ; ------------
  11385. ; This routine is called on a number of occasions to check if syntax is being
  11386. ; checked or if the program is being run. To test the flag inline would use
  11387. ; four bytes of code, but a call instruction only uses 3 bytes of code.
  11388.  
  11389. ;; SYNTAX-Z
  11390. L2530:  BIT     7,(IY+$01)      ; test FLAGS  - checking syntax only ?
  11391.         RET                     ; return.
  11392.  
  11393. ; ----------------
  11394. ; Scanning SCREEN$
  11395. ; ----------------
  11396. ; This function returns the code of a bit-mapped character at screen
  11397. ; position at line C, column B. It is unable to detect the mosaic characters
  11398. ; which are not bit-mapped but detects the ASCII 32 - 127 range.
  11399. ; The bit-mapped UDGs are ignored which is curious as it requires only a
  11400. ; few extra bytes of code. As usual, anything to do with CHARS is weird.
  11401. ; If no match is found a null string is returned.
  11402. ; No actual check on ranges is performed - that's up to the BASIC programmer.
  11403. ; No real harm can come from SCREEN$(255,255) although the BASIC manual
  11404. ; says that invalid values will be trapped.
  11405. ; Interestingly, in the Pitman pocket guide, 1984, Vickers says that the
  11406. ; range checking will be performed.
  11407.  
  11408. ;; S-SCRN$-S
  11409. L2535:  CALL    L2307           ; routine STK-TO-BC.
  11410.         LD      HL,($5C36)      ; fetch address of CHARS.
  11411.         LD      DE,$0100        ; fetch offset to chr$ 32
  11412.         ADD     HL,DE           ; and find start of bitmaps.
  11413.                                 ; Note. not inc h. ??
  11414.         LD      A,C             ; transfer line to A.
  11415.         RRCA                    ; multiply
  11416.         RRCA                    ; by
  11417.         RRCA                    ; thirty-two.
  11418.         AND     $E0             ; and with 11100000
  11419.         XOR     B               ; combine with column $00 - $1F
  11420.         LD      E,A             ; to give the low byte of top line
  11421.         LD      A,C             ; column to A range 00000000 to 00011111
  11422.         AND     $18             ; and with 00011000
  11423.         XOR     $40             ; xor with 01000000 (high byte screen start)
  11424.         LD      D,A             ; register DE now holds start address of cell.
  11425.         LD      B,$60           ; there are 96 characters in ASCII set.
  11426.  
  11427. ;; S-SCRN-LP
  11428. L254F:  PUSH    BC              ; save count
  11429.         PUSH    DE              ; save screen start address
  11430.         PUSH    HL              ; save bitmap start
  11431.         LD      A,(DE)          ; first byte of screen to A
  11432.         XOR     (HL)            ; xor with corresponding character byte
  11433.         JR      Z,L255A         ; forward to S-SC-MTCH if they match
  11434.                                 ; if inverse result would be $FF
  11435.                                 ; if any other then mismatch
  11436.  
  11437.         INC     A               ; set to $00 if inverse
  11438.         JR      NZ,L2573        ; forward to S-SCR-NXT if a mismatch
  11439.  
  11440.         DEC     A               ; restore $FF
  11441.  
  11442. ; a match has been found so seven more to test.
  11443.  
  11444. ;; S-SC-MTCH
  11445. L255A:  LD      C,A             ; load C with inverse mask $00 or $FF
  11446.         LD      B,$07           ; count seven more bytes
  11447.  
  11448. ;; S-SC-ROWS
  11449. L255D:  INC     D               ; increment screen address.
  11450.         INC     HL              ; increment bitmap address.
  11451.         LD      A,(DE)          ; byte to A
  11452.         XOR     (HL)            ; will give $00 or $FF (inverse)
  11453.         XOR     C               ; xor with inverse mask
  11454.         JR      NZ,L2573        ; forward to S-SCR-NXT if no match.
  11455.  
  11456.         DJNZ    L255D           ; back to S-SC-ROWS until all eight matched.
  11457.  
  11458. ; continue if a match of all eight bytes was found
  11459.  
  11460.         POP     BC              ; discard the
  11461.         POP     BC              ; saved
  11462.         POP     BC              ; pointers
  11463.         LD      A,$80           ; the endpoint of character set
  11464.         SUB     B               ; subtract the counter
  11465.                                 ; to give the code 32-127
  11466.         LD      BC,$0001        ; make one space in workspace.
  11467.  
  11468.         RST     30H             ; BC-SPACES creates the space sliding
  11469.                                 ; the calculator stack upwards.
  11470.         LD      (DE),A          ; start is addressed by DE, so insert code
  11471.         JR      L257D           ; forward to S-SCR-STO
  11472.  
  11473. ; ---
  11474.  
  11475. ; the jump was here if no match and more bitmaps to test.
  11476.  
  11477. ;; S-SCR-NXT
  11478. L2573:  POP     HL              ; restore the last bitmap start
  11479.         LD      DE,$0008        ; and prepare to add 8.
  11480.         ADD     HL,DE           ; now addresses next character bitmap.
  11481.         POP     DE              ; restore screen address
  11482.         POP     BC              ; and character counter in B
  11483.         DJNZ    L254F           ; back to S-SCRN-LP if more characters.
  11484.  
  11485.         LD      C,B             ; B is now zero, so BC now zero.
  11486.  
  11487. ;; S-SCR-STO
  11488. L257D:  JP      L2AB2           ; to STK-STO-$ to store the string in
  11489.                                 ; workspace or a string with zero length.
  11490.                                 ; (value of DE doesn't matter in last case)
  11491.  
  11492. ; Note. this exit seems correct but the general-purpose routine S-STRING
  11493. ; that calls this one will also stack any of its string results so this
  11494. ; leads to a double storing of the result in this case.
  11495. ; The instruction at L257D should just be a RET.
  11496. ; credit Stephen Kelly and others, 1982.
  11497.  
  11498. ; -------------
  11499. ; Scanning ATTR
  11500. ; -------------
  11501. ; This function subroutine returns the attributes of a screen location -
  11502. ; a numeric result.
  11503. ; Again it's up to the BASIC programmer to supply valid values of line/column.
  11504.  
  11505. ;; S-ATTR-S
  11506. L2580:  CALL    L2307           ; routine STK-TO-BC fetches line to C,
  11507.                                 ; and column to B.
  11508.         LD      A,C             ; line to A $00 - $17   (max 00010111)
  11509.         RRCA                    ; rotate
  11510.         RRCA                    ; bits
  11511.         RRCA                    ; left.
  11512.         LD      C,A             ; store in C as an intermediate value.
  11513.  
  11514.         AND     $E0             ; pick up bits 11100000 ( was 00011100 )
  11515.         XOR     B               ; combine with column $00 - $1F
  11516.         LD      L,A             ; low byte now correct.
  11517.  
  11518.         LD      A,C             ; bring back intermediate result from C
  11519.         AND     $03             ; mask to give correct third of
  11520.                                 ; screen $00 - $02
  11521.         XOR     $58             ; combine with base address.
  11522.         LD      H,A             ; high byte correct.
  11523.         LD      A,(HL)          ; pick up the colour attribute.
  11524.         JP      L2D28           ; forward to STACK-A to store result
  11525.                                 ; and make an indirect exit.
  11526.  
  11527. ; -----------------------
  11528. ; Scanning function table
  11529. ; -----------------------
  11530. ; This table is used by INDEXER routine to find the offsets to
  11531. ; four operators and eight functions. e.g. $A8 is the token 'FN'.
  11532. ; This table is used in the first instance for the first character of an
  11533. ; expression or by a recursive call to SCANNING for the first character of
  11534. ; any sub-expression. It eliminates functions that have no argument or
  11535. ; functions that can have more than one argument and therefore require
  11536. ; braces. By eliminating and dealing with these now it can later take a
  11537. ; simplistic approach to all other functions and assume that they have
  11538. ; one argument.
  11539. ; Similarly by eliminating BIN and '.' now it is later able to assume that
  11540. ; all numbers begin with a digit and that the presence of a number or
  11541. ; variable can be detected by a call to ALPHANUM.
  11542. ; By default all expressions are positive and the spurious '+' is eliminated
  11543. ; now as in print +2. This should not be confused with the operator '+'.
  11544. ; Note. this does allow a degree of nonsense to be accepted as in
  11545. ; PRINT +"3 is the greatest.".
  11546. ; An acquired programming skill is the ability to include brackets where
  11547. ; they are not necessary.
  11548. ; A bracket at the start of a sub-expression may be spurious or necessary
  11549. ; to denote that the contained expression is to be evaluated as an entity.
  11550. ; In either case this is dealt with by recursive calls to SCANNING.
  11551. ; An expression that begins with a quote requires special treatment.
  11552.  
  11553. ;; scan-func
  11554. L2596:  DB    $22, L25B3-$-1  ; $1C offset to S-QUOTE
  11555.         DB    '(', L25E8-$-1  ; $4F offset to S-BRACKET
  11556.         DB    '.', L268D-$-1  ; $F2 offset to S-DECIMAL
  11557.         DB    '+', L25AF-$-1  ; $12 offset to S-U-PLUS
  11558.  
  11559.         DB    $A8, L25F5-$-1  ; $56 offset to S-FN
  11560.         DB    $A5, L25F8-$-1  ; $57 offset to S-RND
  11561.         DB    $A7, L2627-$-1  ; $84 offset to S-PI
  11562.         DB    $A6, L2634-$-1  ; $8F offset to S-INKEY$
  11563.         DB    $C4, L268D-$-1  ; $E6 offset to S-BIN
  11564.         DB    $AA, L2668-$-1  ; $BF offset to S-SCREEN$
  11565.         DB    $AB, L2672-$-1  ; $C7 offset to S-ATTR
  11566.         DB    $A9, L267B-$-1  ; $CE offset to S-POINT
  11567.  
  11568.         DB    $00             ; zero end marker
  11569.  
  11570. ; --------------------------
  11571. ; Scanning function routines
  11572. ; --------------------------
  11573. ; These are the 11 subroutines accessed by the above table.
  11574. ; S-BIN and S-DECIMAL are the same
  11575. ; The 1-byte offset limits their location to within 255 bytes of their
  11576. ; entry in the table.
  11577.  
  11578. ; ->
  11579. ;; S-U-PLUS
  11580. L25AF:  RST     20H             ; NEXT-CHAR just ignore
  11581.         JP      L24FF           ; to S-LOOP-1
  11582.  
  11583. ; ---
  11584.  
  11585. ; ->
  11586. ;; S-QUOTE
  11587. L25B3:  RST     18H             ; GET-CHAR
  11588.         INC     HL              ; address next character (first in quotes)
  11589.         PUSH    HL              ; save start of quoted text.
  11590.         LD      BC,$0000        ; initialize length of string to zero.
  11591.         CALL    L250F           ; routine S-QUOTE-S
  11592.         JR      NZ,L25D9        ; forward to S-Q-PRMS if
  11593.  
  11594. ;; S-Q-AGAIN
  11595. L25BE:  CALL    L250F           ; routine S-QUOTE-S copies string until a
  11596.                                 ; quote is encountered
  11597.         JR      Z,L25BE         ; back to S-Q-AGAIN if two quotes WERE
  11598.                                 ; together.
  11599.  
  11600. ; but if just an isolated quote then that terminates the string.
  11601.  
  11602.         CALL    L2530           ; routine SYNTAX-Z
  11603.         JR      Z,L25D9         ; forward to S-Q-PRMS if checking syntax.
  11604.  
  11605.  
  11606.         RST     30H             ; BC-SPACES creates the space for true
  11607.                                 ; copy of string in workspace.
  11608.         POP     HL              ; re-fetch start of quoted text.
  11609.         PUSH    DE              ; save start in workspace.
  11610.  
  11611. ;; S-Q-COPY
  11612. L25CB:  LD      A,(HL)          ; fetch a character from source.
  11613.         INC     HL              ; advance source address.
  11614.         LD      (DE),A          ; place in destination.
  11615.         INC     DE              ; advance destination address.
  11616.         CP      $22             ; was it a '"' just copied ?
  11617.         JR      NZ,L25CB        ; back to S-Q-COPY to copy more if not
  11618.  
  11619.         LD      A,(HL)          ; fetch adjacent character from source.
  11620.         INC     HL              ; advance source address.
  11621.         CP      $22             ; is this '"' ? - i.e. two quotes together ?
  11622.         JR      Z,L25CB         ; to S-Q-COPY if so including just one of the
  11623.                                 ; pair of quotes.
  11624.  
  11625. ; proceed when terminating quote encountered.
  11626.  
  11627. ;; S-Q-PRMS
  11628. L25D9:  DEC     BC              ; decrease count by 1.
  11629.         POP     DE              ; restore start of string in workspace.
  11630.  
  11631. ;; S-STRING
  11632. L25DB:  LD      HL,$5C3B        ; Address FLAGS system variable.
  11633.         RES     6,(HL)          ; signal string result.
  11634.         BIT     7,(HL)          ; is syntax being checked.
  11635.         CALL    NZ,L2AB2        ; routine STK-STO-$ is called in runtime.
  11636.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11637.  
  11638. ; ---
  11639.  
  11640. ; ->
  11641. ;; S-BRACKET
  11642. L25E8:  RST     20H             ; NEXT-CHAR
  11643.         CALL    L24FB           ; routine SCANNING is called recursively.
  11644.         CP      $29             ; is it the closing ')' ?
  11645.         JP      NZ,L1C8A        ; jump back to REPORT-C if not
  11646.                                 ; 'Nonsense in BASIC'
  11647.  
  11648.         RST     20H             ; NEXT-CHAR
  11649.         JP      L2712           ; jump forward to S-CONT-2          ===>
  11650.  
  11651. ; ---
  11652.  
  11653. ; ->
  11654. ;; S-FN
  11655. L25F5:  JP      L27BD           ; jump forward to S-FN-SBRN.
  11656.  
  11657. ; ---
  11658.  
  11659. ; ->
  11660. ;; S-RND
  11661. L25F8:  CALL    L2530           ; routine SYNTAX-Z
  11662.         JR      Z,L2625         ; forward to S-RND-END if checking syntax.
  11663.  
  11664.         LD      BC,($5C76)      ; fetch system variable SEED
  11665.         CALL    L2D2B           ; routine STACK-BC places on calculator stack
  11666.  
  11667.         RST     28H             ;; FP-CALC           ;s.
  11668.         DB    $A1             ;;stk-one            ;s,1.
  11669.         DB    $0F             ;;addition           ;s+1.
  11670.         DB    $34             ;;stk-data           ;
  11671.         DB    $37             ;;Exponent: $87,
  11672.                                 ;;Bytes: 1
  11673.         DB    $16             ;;(+00,+00,+00)      ;s+1,75.
  11674.         DB    $04             ;;multiply           ;(s+1)*75 = v
  11675.         DB    $34             ;;stk-data           ;v.
  11676.         DB    $80             ;;Bytes: 3
  11677.         DB    $41             ;;Exponent $91
  11678.         DB    $00,$00,$80     ;;(+00)              ;v,65537.
  11679.         DB    $32             ;;n-mod-m            ;remainder, result.
  11680.         DB    $02             ;;delete             ;remainder.
  11681.         DB    $A1             ;;stk-one            ;remainder, 1.
  11682.         DB    $03             ;;subtract           ;remainder - 1. = rnd
  11683.         DB    $31             ;;duplicate          ;rnd,rnd.
  11684.         DB    $38             ;;end-calc
  11685.  
  11686.         CALL    L2DA2           ; routine FP-TO-BC
  11687.         LD      ($5C76),BC      ; store in SEED for next starting point.
  11688.         LD      A,(HL)          ; fetch exponent
  11689.         AND     A               ; is it zero ?
  11690.         JR      Z,L2625         ; forward if so to S-RND-END
  11691.  
  11692.         SUB     $10             ; reduce exponent by 2^16
  11693.         LD      (HL),A          ; place back
  11694.  
  11695. ;; S-RND-END
  11696. L2625:  JR      L2630           ; forward to S-PI-END
  11697.  
  11698. ; ---
  11699.  
  11700. ; the number PI 3.14159...
  11701.  
  11702. ; ->
  11703. ;; S-PI
  11704. L2627:  CALL    L2530           ; routine SYNTAX-Z
  11705.         JR      Z,L2630         ; to S-PI-END if checking syntax.
  11706.  
  11707.         RST     28H             ;; FP-CALC
  11708.         DB    $A3             ;;stk-pi/2                          pi/2.
  11709.         DB    $38             ;;end-calc
  11710.  
  11711.         INC     (HL)            ; increment the exponent leaving pi
  11712.                                 ; on the calculator stack.
  11713.  
  11714. ;; S-PI-END
  11715. L2630:  RST     20H             ; NEXT-CHAR
  11716.         JP      L26C3           ; jump forward to S-NUMERIC
  11717.  
  11718. ; ---
  11719.  
  11720. ; ->
  11721. ;; S-INKEY$
  11722. L2634:  LD      BC,$105A        ; priority $10, operation code $1A ('read-in')
  11723.                                 ; +$40 for string result, numeric operand.
  11724.                                 ; set this up now in case we need to use the
  11725.                                 ; calculator.
  11726.         RST     20H             ; NEXT-CHAR
  11727.         CP      $23             ; '#' ?
  11728.         JP      Z,L270D         ; to S-PUSH-PO if so to use the calculator
  11729.                                 ; single operation
  11730.                                 ; to read from network/RS232 etc. .
  11731.  
  11732. ; else read a key from the keyboard.
  11733.  
  11734.         LD      HL,$5C3B        ; fetch FLAGS
  11735.         RES     6,(HL)          ; signal string result.
  11736.         BIT     7,(HL)          ; checking syntax ?
  11737.         JR      Z,L2665         ; forward to S-INK$-EN if so
  11738.  
  11739. ;===============================
  11740.                 IF BAS48_ONLY=1
  11741.                 CALL L028E
  11742.                 ELSE
  11743.                 JP L3B6C                ; Spectrum 128 patch
  11744.                 ENDIF
  11745. ;===============================
  11746.  
  11747. L2649:  LD      C,$00           ; the length of an empty string
  11748.         JR      NZ,L2660        ; to S-IK$-STK to store empty string if
  11749.                                 ; no key returned.
  11750.  
  11751.         CALL    L031E           ; routine K-TEST get main code in A
  11752.         JR      NC,L2660        ; to S-IK$-STK to stack null string if
  11753.                                 ; invalid
  11754.  
  11755.         DEC     D               ; D is expected to be FLAGS so set bit 3 $FF
  11756.                                 ; 'L' Mode so no keywords.
  11757.         LD      E,A             ; main key to A
  11758.                                 ; C is MODE 0 'KLC' from above still.
  11759.         CALL    L0333           ; routine K-DECODE
  11760. L2657:  PUSH    AF              ; save the code
  11761.         LD      BC,$0001        ; make room for one character
  11762.  
  11763.         RST     30H             ; BC-SPACES
  11764.         POP     AF              ; bring the code back
  11765.         LD      (DE),A          ; put the key in workspace
  11766.         LD      C,$01           ; set C length to one
  11767.  
  11768. ;; S-IK$-STK
  11769. L2660:  LD      B,$00           ; set high byte of length to zero
  11770.         CALL    L2AB2           ; routine STK-STO-$
  11771.  
  11772. ;; S-INK$-EN
  11773. L2665:  JP      L2712           ; to S-CONT-2            ===>
  11774.  
  11775. ; ---
  11776.  
  11777. ; ->
  11778. ;; S-SCREEN$
  11779. L2668:  CALL    L2522           ; routine S-2-COORD
  11780.         CALL    NZ,L2535        ; routine S-SCRN$-S
  11781.  
  11782.         RST     20H             ; NEXT-CHAR
  11783.         JP      L25DB           ; forward to S-STRING to stack result
  11784.  
  11785. ; ---
  11786.  
  11787. ; ->
  11788. ;; S-ATTR
  11789. L2672:  CALL    L2522           ; routine S-2-COORD
  11790.         CALL    NZ,L2580        ; routine S-ATTR-S
  11791.  
  11792.         RST     20H             ; NEXT-CHAR
  11793.         JR      L26C3           ; forward to S-NUMERIC
  11794.  
  11795. ; ---
  11796.  
  11797. ; ->
  11798. ;; S-POINT
  11799. L267B:  CALL    L2522           ; routine S-2-COORD
  11800.         CALL    NZ,L22CB        ; routine POINT-SUB
  11801.  
  11802.         RST     20H             ; NEXT-CHAR
  11803.         JR      L26C3           ; forward to S-NUMERIC
  11804.  
  11805. ; -----------------------------
  11806.  
  11807. ; ==> The branch was here if not in table.
  11808.  
  11809. ;; S-ALPHNUM
  11810. L2684:  CALL    L2C88           ; routine ALPHANUM checks if variable or
  11811.                                 ; a digit.
  11812.         JR      NC,L26DF        ; forward to S-NEGATE if not to consider
  11813.                                 ; a '-' character then functions.
  11814.  
  11815.         CP      $41             ; compare 'A'
  11816.         JR      NC,L26C9        ; forward to S-LETTER if alpha       ->
  11817.                                 ; else must have been numeric so continue
  11818.                                 ; into that routine.
  11819.  
  11820. ; This important routine is called during runtime and from LINE-SCAN
  11821. ; when a BASIC line is checked for syntax. It is this routine that
  11822. ; inserts, during syntax checking, the invisible floating point numbers
  11823. ; after the numeric expression. During runtime it just picks these
  11824. ; numbers up. It also handles BIN format numbers.
  11825.  
  11826. ; ->
  11827. ;; S-BIN
  11828. ;; S-DECIMAL
  11829. L268D:  CALL    L2530           ; routine SYNTAX-Z
  11830.         JR      NZ,L26B5        ; to S-STK-DEC in runtime
  11831.  
  11832. ; this route is taken when checking syntax.
  11833.  
  11834.         CALL    L2C9B           ; routine DEC-TO-FP to evaluate number
  11835.  
  11836.         RST     18H             ; GET-CHAR to fetch HL
  11837.         LD      BC,$0006        ; six locations required
  11838.         CALL    L1655           ; routine MAKE-ROOM
  11839.         INC     HL              ; to first new location
  11840.         LD      (HL),$0E        ; insert number marker
  11841.         INC     HL              ; address next
  11842.         EX      DE,HL           ; make DE destination.
  11843.         LD      HL,($5C65)      ; STKEND points to end of stack.
  11844.         LD      C,$05           ; result is five locations lower
  11845.         AND     A               ; prepare for true subtraction
  11846.         SBC     HL,BC           ; point to start of value.
  11847.         LD      ($5C65),HL      ; update STKEND as we are taking number.
  11848.         LDIR                    ; Copy five bytes to program location
  11849.         EX      DE,HL           ; transfer pointer to HL
  11850.         DEC     HL              ; adjust
  11851.         CALL    L0077           ; routine TEMP-PTR1 sets CH-ADD
  11852.         JR      L26C3           ; to S-NUMERIC to record nature of result
  11853.  
  11854. ; ---
  11855.  
  11856. ; branch here in runtime.
  11857.  
  11858. ;; S-STK-DEC
  11859. L26B5:  RST     18H             ; GET-CHAR positions HL at digit.
  11860.  
  11861. ;; S-SD-SKIP
  11862. L26B6:  INC     HL              ; advance pointer
  11863.         LD      A,(HL)          ; until we find
  11864.         CP      $0E             ; chr 14d - the number indicator
  11865.         JR      NZ,L26B6        ; to S-SD-SKIP until a match
  11866.                                 ; it has to be here.
  11867.  
  11868.         INC     HL              ; point to first byte of number
  11869.         CALL    L33B4           ; routine STACK-NUM stacks it
  11870.         LD      ($5C5D),HL      ; update system variable CH_ADD
  11871.  
  11872. ;; S-NUMERIC
  11873. L26C3:  SET     6,(IY+$01)      ; update FLAGS  - Signal numeric result
  11874.         JR      L26DD           ; forward to S-CONT-1               ===>
  11875.                                 ; actually S-CONT-2 is destination but why
  11876.                                 ; waste a byte on a jump when a JR will do.
  11877.                                 ; actually a JR L2712 can be used. Rats.
  11878.  
  11879. ; end of functions accessed from scanning functions table.
  11880.  
  11881. ; --------------------------
  11882. ; Scanning variable routines
  11883. ; --------------------------
  11884. ;
  11885. ;
  11886.  
  11887. ;; S-LETTER
  11888. L26C9:  CALL    L28B2           ; routine LOOK-VARS
  11889.         JP      C,L1C2E         ; jump back to REPORT-2 if not found
  11890.                                 ; 'Variable not found'
  11891.                                 ; but a variable is always 'found' if syntax
  11892.                                 ; is being checked.
  11893.  
  11894.         CALL    Z,L2996         ; routine STK-VAR considers a subscript/slice
  11895.         LD      A,($5C3B)       ; fetch FLAGS value
  11896.         CP      $C0             ; compare 11000000
  11897.         JR      C,L26DD         ; step forward to S-CONT-1 if string  ===>
  11898.  
  11899.         INC     HL              ; advance pointer
  11900.         CALL    L33B4           ; routine STACK-NUM
  11901.  
  11902. ;; S-CONT-1
  11903. L26DD:  JR      L2712           ; forward to S-CONT-2                 ===>
  11904.  
  11905. ; ----------------------------------------
  11906. ; -> the scanning branch was here if not alphanumeric.
  11907. ; All the remaining functions will be evaluated by a single call to the
  11908. ; calculator. The correct priority for the operation has to be placed in
  11909. ; the B register and the operation code, calculator literal in the C register.
  11910. ; the operation code has bit 7 set if result is numeric and bit 6 is
  11911. ; set if operand is numeric. so
  11912. ; $C0 = numeric result, numeric operand.            e.g. 'sin'
  11913. ; $80 = numeric result, string operand.             e.g. 'code'
  11914. ; $40 = string result, numeric operand.             e.g. 'str$'
  11915. ; $00 = string result, string operand.              e.g. 'val$'
  11916.  
  11917. ;; S-NEGATE
  11918. L26DF:  LD      BC,$09DB        ; prepare priority 09, operation code $C0 +
  11919.                                 ; 'negate' ($1B) - bits 6 and 7 set for numeric
  11920.                                 ; result and numeric operand.
  11921.  
  11922.         CP      $2D             ; is it '-' ?
  11923.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11924.  
  11925.         LD      BC,$1018        ; prepare priority $10, operation code 'val$' -
  11926.                                 ; bits 6 and 7 reset for string result and
  11927.                                 ; string operand.
  11928.        
  11929.         CP      $AE             ; is it 'VAL$' ?
  11930.         JR      Z,L270D         ; forward if so to S-PUSH-PO
  11931.  
  11932.         SUB     $AF             ; subtract token 'CODE' value to reduce
  11933.                                 ; functions 'CODE' to 'NOT' although the
  11934.                                 ; upper range is, as yet, unchecked.
  11935.                                 ; valid range would be $00 - $14.
  11936.  
  11937.         JP      C,L1C8A         ; jump back to REPORT-C with anything else
  11938.                                 ; 'Nonsense in BASIC'
  11939.  
  11940.         LD      BC,$04F0        ; prepare priority $04, operation $C0 +
  11941.                                 ; 'not' ($30)
  11942.  
  11943.         CP      $14             ; is it 'NOT'
  11944.         JR      Z,L270D         ; forward to S-PUSH-PO if so
  11945.  
  11946.         JP      NC,L1C8A        ; to REPORT-C if higher
  11947.                                 ; 'Nonsense in BASIC'
  11948.  
  11949.         LD      B,$10           ; priority $10 for all the rest
  11950.         ADD     A,$DC           ; make range $DC - $EF
  11951.                                 ; $C0 + 'code'($1C) thru 'chr$' ($2F)
  11952.  
  11953.         LD      C,A             ; transfer 'function' to C
  11954.         CP      $DF             ; is it 'sin' ?
  11955.         JR      NC,L2707        ; forward to S-NO-TO-$  with 'sin' through
  11956.                                 ; 'chr$' as operand is numeric.
  11957.  
  11958. ; all the rest 'cos' through 'chr$' give a numeric result except 'str$'
  11959. ; and 'chr$'.
  11960.  
  11961.         RES     6,C             ; signal string operand for 'code', 'val' and
  11962.                                 ; 'len'.
  11963.  
  11964. ;; S-NO-TO-$
  11965. L2707:  CP      $EE             ; compare 'str$'
  11966.         JR      C,L270D         ; forward to S-PUSH-PO if lower as result
  11967.                                 ; is numeric.
  11968.  
  11969.         RES     7,C             ; reset bit 7 of op code for 'str$', 'chr$'
  11970.                                 ; as result is string.
  11971.  
  11972. ; >> This is where they were all headed for.
  11973.  
  11974. ;; S-PUSH-PO
  11975. L270D:  PUSH    BC              ; push the priority and calculator operation
  11976.                                 ; code.
  11977.  
  11978.         RST     20H             ; NEXT-CHAR
  11979.         JP      L24FF           ; jump back to S-LOOP-1 to go round the loop
  11980.                                 ; again with the next character.
  11981.  
  11982. ; --------------------------------
  11983.  
  11984. ; ===>  there were many branches forward to here
  11985.  
  11986. ;; S-CONT-2
  11987. L2712:  RST     18H             ; GET-CHAR
  11988.  
  11989. ;; S-CONT-3
  11990. L2713:  CP      $28             ; is it '(' ?
  11991.         JR      NZ,L2723        ; forward to S-OPERTR if not    >
  11992.  
  11993.         BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  11994.         JR      NZ,L2734        ; forward to S-LOOP if numeric to evaluate  >
  11995.  
  11996. ; if a string preceded '(' then slice it.
  11997.  
  11998.         CALL    L2A52           ; routine SLICING
  11999.  
  12000.         RST     20H             ; NEXT-CHAR
  12001.         JR      L2713           ; back to S-CONT-3
  12002.  
  12003. ; ---------------------------
  12004.  
  12005. ; the branch was here when possibility of an operator '(' has been excluded.
  12006.  
  12007. ;; S-OPERTR
  12008. L2723:  LD      B,$00           ; prepare to add
  12009.         LD      C,A             ; possible operator to C
  12010.         LD      HL,L2795        ; Address: $2795 - tbl-of-ops
  12011.         CALL    L16DC           ; routine INDEXER
  12012.         JR      NC,L2734        ; forward to S-LOOP if not in table
  12013.  
  12014. ; but if found in table the priority has to be looked up.
  12015.  
  12016.         LD      C,(HL)          ; operation code to C ( B is still zero )
  12017.         LD      HL,L27B0 - $C3  ; $26ED is base of table
  12018.         ADD     HL,BC           ; index into table.
  12019.         LD      B,(HL)          ; priority to B.
  12020.  
  12021. ; ------------------
  12022. ; Scanning main loop
  12023. ; ------------------
  12024. ; the juggling act
  12025.  
  12026. ;; S-LOOP
  12027. L2734:  POP     DE              ; fetch last priority and operation
  12028.         LD      A,D             ; priority to A
  12029.         CP      B               ; compare with this one
  12030.         JR      C,L2773         ; forward to S-TIGHTER to execute the
  12031.                                 ; last operation before this one as it has
  12032.                                 ; higher priority.
  12033.  
  12034. ; the last priority was greater or equal this one.
  12035.  
  12036.         AND     A               ; if it is zero then so is this
  12037.         JP      Z,L0018         ; jump to exit via get-char pointing at
  12038.                                 ; next character.
  12039.                                 ; This may be the character after the
  12040.                                 ; expression or, if exiting a recursive call,
  12041.                                 ; the next part of the expression to be
  12042.                                 ; evaluated.
  12043.  
  12044.         PUSH    BC              ; save current priority/operation
  12045.                                 ; as it has lower precedence than the one
  12046.                                 ; now in DE.
  12047.  
  12048. ; the 'USR' function is special in that it is overloaded to give two types
  12049. ; of result.
  12050.  
  12051.         LD      HL,$5C3B        ; address FLAGS
  12052.         LD      A,E             ; new operation to A register
  12053.         CP      $ED             ; is it $C0 + 'usr-no' ($2D)  ?
  12054.         JR      NZ,L274C        ; forward to S-STK-LST if not
  12055.  
  12056.         BIT     6,(HL)          ; string result expected ?
  12057.                                 ; (from the lower priority operand we've
  12058.                                 ; just pushed on stack )
  12059.         JR      NZ,L274C        ; forward to S-STK-LST if numeric
  12060.                                 ; as operand bits match.
  12061.  
  12062.         LD      E,$99           ; reset bit 6 and substitute $19 'usr-$'
  12063.                                 ; for string operand.
  12064.  
  12065. ;; S-STK-LST
  12066. L274C:  PUSH    DE              ; now stack this priority/operation
  12067.         CALL    L2530           ; routine SYNTAX-Z
  12068.         JR      Z,L275B         ; forward to S-SYNTEST if checking syntax.
  12069.  
  12070.         LD      A,E             ; fetch the operation code
  12071.         AND     $3F             ; mask off the result/operand bits to leave
  12072.                                 ; a calculator literal.
  12073.         LD      B,A             ; transfer to B register
  12074.  
  12075. ; now use the calculator to perform the single operation - operand is on
  12076. ; the calculator stack.
  12077. ; Note. although the calculator is performing a single operation most
  12078. ; functions e.g. TAN are written using other functions and literals and
  12079. ; these in turn are written using further strings of calculator literals so
  12080. ; another level of magical recursion joins the juggling act for a while
  12081. ; as the calculator too is calling itself.
  12082.  
  12083.         RST     28H             ;; FP-CALC
  12084.         DB    $3B             ;;fp-calc-2
  12085. L2758:  DB    $38             ;;end-calc
  12086.  
  12087.         JR      L2764           ; forward to S-RUNTEST
  12088.  
  12089. ; ---
  12090.  
  12091. ; the branch was here if checking syntax only.
  12092.  
  12093. ;; S-SYNTEST
  12094. L275B:  LD      A,E             ; fetch the operation code to accumulator
  12095.         XOR     (IY+$01)        ; compare with bits of FLAGS
  12096.         AND     $40             ; bit 6 will be zero now if operand
  12097.                                 ; matched expected result.
  12098.  
  12099. ;; S-RPORT-C2
  12100. L2761:  JP      NZ,L1C8A        ; to REPORT-C if mismatch
  12101.                                 ; 'Nonsense in BASIC'
  12102.                                 ; else continue to set flags for next
  12103.  
  12104. ; the branch is to here in runtime after a successful operation.
  12105.  
  12106. ;; S-RUNTEST
  12107. L2764:  POP     DE              ; fetch the last operation from stack
  12108.         LD      HL,$5C3B        ; address FLAGS
  12109.         SET     6,(HL)          ; set default to numeric result in FLAGS
  12110.         BIT     7,E             ; test the operational result
  12111.         JR      NZ,L2770        ; forward to S-LOOPEND if numeric
  12112.  
  12113.         RES     6,(HL)          ; reset bit 6 of FLAGS to show string result.
  12114.  
  12115. ;; S-LOOPEND
  12116. L2770:  POP     BC              ; fetch the previous priority/operation
  12117.         JR      L2734           ; back to S-LOOP to perform these
  12118.  
  12119. ; ---
  12120.  
  12121. ; the branch was here when a stacked priority/operator had higher priority
  12122. ; than the current one.
  12123.  
  12124. ;; S-TIGHTER
  12125. L2773:  PUSH    DE              ; save high priority op on stack again
  12126.         LD      A,C             ; fetch lower priority operation code
  12127.         BIT     6,(IY+$01)      ; test FLAGS - Numeric or string result ?
  12128.         JR      NZ,L2790        ; forward to S-NEXT if numeric result
  12129.  
  12130. ; if this is lower priority yet has string then must be a comparison.
  12131. ; Since these can only be evaluated in context and were defaulted to
  12132. ; numeric in operator look up they must be changed to string equivalents.
  12133.  
  12134.         AND     $3F             ; mask to give true calculator literal
  12135.         ADD     A,$08           ; augment numeric literals to string
  12136.                                 ; equivalents.
  12137.                                 ; 'no-&-no'  => 'str-&-no'
  12138.                                 ; 'no-l-eql' => 'str-l-eql'
  12139.                                 ; 'no-gr-eq' => 'str-gr-eq'
  12140.                                 ; 'nos-neql' => 'strs-neql'
  12141.                                 ; 'no-grtr'  => 'str-grtr'
  12142.                                 ; 'no-less'  => 'str-less'
  12143.                                 ; 'nos-eql'  => 'strs-eql'
  12144.                                 ; 'addition' => 'strs-add'
  12145.         LD      C,A             ; put modified comparison operator back
  12146.         CP      $10             ; is it now 'str-&-no' ?
  12147.         JR      NZ,L2788        ; forward to S-NOT-AND  if not.
  12148.  
  12149.         SET     6,C             ; set numeric operand bit
  12150.         JR      L2790           ; forward to S-NEXT
  12151.  
  12152. ; ---
  12153.  
  12154. ;; S-NOT-AND
  12155. L2788:  JR      C,L2761         ; back to S-RPORT-C2 if less
  12156.                                 ; 'Nonsense in BASIC'.
  12157.                                 ; e.g. a$ * b$
  12158.  
  12159.         CP      $17             ; is it 'strs-add' ?
  12160.         JR      Z,L2790         ; forward to to S-NEXT if so
  12161.                                 ; (bit 6 and 7 are reset)
  12162.  
  12163.         SET     7,C             ; set numeric (Boolean) result for all others
  12164.  
  12165. ;; S-NEXT
  12166. L2790:  PUSH    BC              ; now save this priority/operation on stack
  12167.  
  12168.         RST     20H             ; NEXT-CHAR
  12169.         JP      L24FF           ; jump back to S-LOOP-1
  12170.  
  12171. ; ------------------
  12172. ; Table of operators
  12173. ; ------------------
  12174. ; This table is used to look up the calculator literals associated with
  12175. ; the operator character. The thirteen calculator operations $03 - $0F
  12176. ; have bits 6 and 7 set to signify a numeric result.
  12177. ; Some of these codes and bits may be altered later if the context suggests
  12178. ; a string comparison or operation.
  12179. ; that is '+', '=', '>', '<', '<=', '>=' or '<>'.
  12180.  
  12181. ;; tbl-of-ops
  12182. L2795:  DB    '+', $CF        ;        $C0 + 'addition'
  12183.         DB    '-', $C3        ;        $C0 + 'subtract'
  12184.         DB    '*', $C4        ;        $C0 + 'multiply'
  12185.         DB    '/', $C5        ;        $C0 + 'division'
  12186.         DB    '^', $C6        ;        $C0 + 'to-power'
  12187.         DB    '=', $CE        ;        $C0 + 'nos-eql'
  12188.         DB    '>', $CC        ;        $C0 + 'no-grtr'
  12189.         DB    '<', $CD        ;        $C0 + 'no-less'
  12190.  
  12191.         DB    $C7, $C9        ; '<='   $C0 + 'no-l-eql'
  12192.         DB    $C8, $CA        ; '>='   $C0 + 'no-gr-eql'
  12193.         DB    $C9, $CB        ; '<>'   $C0 + 'nos-neql'
  12194.         DB    $C5, $C7        ; 'OR'   $C0 + 'or'
  12195.         DB    $C6, $C8        ; 'AND'  $C0 + 'no-&-no'
  12196.  
  12197.         DB    $00             ; zero end-marker.
  12198.  
  12199.  
  12200. ; -------------------
  12201. ; Table of priorities
  12202. ; -------------------
  12203. ; This table is indexed with the operation code obtained from the above
  12204. ; table $C3 - $CF to obtain the priority for the respective operation.
  12205.  
  12206. ;; tbl-priors
  12207. L27B0:  DB    $06             ; '-'   opcode $C3
  12208.         DB    $08             ; '*'   opcode $C4
  12209.         DB    $08             ; '/'   opcode $C5
  12210.         DB    $0A             ; '^'   opcode $C6
  12211.         DB    $02             ; 'OR'  opcode $C7
  12212.         DB    $03             ; 'AND' opcode $C8
  12213.         DB    $05             ; '<='  opcode $C9
  12214.         DB    $05             ; '>='  opcode $CA
  12215.         DB    $05             ; '<>'  opcode $CB
  12216.         DB    $05             ; '>'   opcode $CC
  12217.         DB    $05             ; '<'   opcode $CD
  12218.         DB    $05             ; '='   opcode $CE
  12219.         DB    $06             ; '+'   opcode $CF
  12220.  
  12221. ; ----------------------
  12222. ; Scanning function (FN)
  12223. ; ----------------------
  12224. ; This routine deals with user-defined functions.
  12225. ; The definition can be anywhere in the program area but these are best
  12226. ; placed near the start of the program as we shall see.
  12227. ; The evaluation process is quite complex as the Spectrum has to parse two
  12228. ; statements at the same time. Syntax of both has been checked previously
  12229. ; and hidden locations have been created immediately after each argument
  12230. ; of the DEF FN statement. Each of the arguments of the FN function is
  12231. ; evaluated by SCANNING and placed in the hidden locations. Then the
  12232. ; expression to the right of the DEF FN '=' is evaluated by SCANNING and for
  12233. ; any variables encountered, a search is made in the DEF FN variable list
  12234. ; in the program area before searching in the normal variables area.
  12235. ;
  12236. ; Recursion is not allowed: i.e. the definition of a function should not use
  12237. ; the same function, either directly or indirectly ( through another function).
  12238. ; You'll normally get error 4, ('Out of memory'), although sometimes the sytem
  12239. ; will crash. - Vickers, Pitman 1984.
  12240. ;
  12241. ; As the definition is just an expression, there would seem to be no means
  12242. ; of breaking out of such recursion.
  12243. ; However, by the clever use of string expressions and VAL, such recursion is
  12244. ; possible.
  12245. ; e.g. DEF FN a(n) = VAL "n+FN a(n-1)+0" ((n<1) * 10 + 1 TO )
  12246. ; will evaluate the full 11-character expression for all values where n is
  12247. ; greater than zero but just the 11th character, "0", when n drops to zero
  12248. ; thereby ending the recursion producing the correct result.
  12249. ; Recursive string functions are possible using VAL$ instead of VAL and the
  12250. ; null string as the final addend.
  12251. ; - from a turn of the century newsgroup discussion initiated by Mike Wynne.
  12252.  
  12253. ;; S-FN-SBRN
  12254. L27BD:  CALL    L2530           ; routine SYNTAX-Z
  12255.         JR      NZ,L27F7        ; forward to SF-RUN in runtime
  12256.  
  12257.  
  12258.         RST     20H             ; NEXT-CHAR
  12259.         CALL    L2C8D           ; routine ALPHA check for letters A-Z a-z
  12260.         JP      NC,L1C8A        ; jump back to REPORT-C if not
  12261.                                 ; 'Nonsense in BASIC'
  12262.  
  12263.  
  12264.         RST     20H             ; NEXT-CHAR
  12265.         CP      $24             ; is it '$' ?
  12266.         PUSH    AF              ; save character and flags
  12267.         JR      NZ,L27D0        ; forward to SF-BRKT-1 with numeric function
  12268.  
  12269.  
  12270.         RST     20H             ; NEXT-CHAR
  12271.  
  12272. ;; SF-BRKT-1
  12273. L27D0:  CP      $28             ; is '(' ?
  12274.         JR      NZ,L27E6        ; forward to SF-RPRT-C if not
  12275.                                 ; 'Nonsense in BASIC'
  12276.  
  12277.  
  12278.         RST     20H             ; NEXT-CHAR
  12279.         CP      $29             ; is it ')' ?
  12280.         JR      Z,L27E9         ; forward to SF-FLAG-6 if no arguments.
  12281.  
  12282. ;; SF-ARGMTS
  12283. L27D9:  CALL    L24FB           ; routine SCANNING checks each argument
  12284.                                 ; which may be an expression.
  12285.  
  12286.         RST     18H             ; GET-CHAR
  12287.         CP      $2C             ; is it a ',' ?
  12288.         JR      NZ,L27E4        ; forward if not to SF-BRKT-2 to test bracket
  12289.  
  12290.  
  12291.         RST     20H             ; NEXT-CHAR if a comma was found
  12292.         JR      L27D9           ; back to SF-ARGMTS to parse all arguments.
  12293.  
  12294. ; ---
  12295.  
  12296. ;; SF-BRKT-2
  12297. L27E4:  CP      $29             ; is character the closing ')' ?
  12298.  
  12299. ;; SF-RPRT-C
  12300. L27E6:  JP      NZ,L1C8A        ; jump to REPORT-C
  12301.                                 ; 'Nonsense in BASIC'
  12302.  
  12303. ; at this point any optional arguments have had their syntax checked.
  12304.  
  12305. ;; SF-FLAG-6
  12306. L27E9:  RST     20H             ; NEXT-CHAR
  12307.         LD      HL,$5C3B        ; address system variable FLAGS
  12308.         RES     6,(HL)          ; signal string result
  12309.         POP     AF              ; restore test against '$'.
  12310.         JR      Z,L27F4         ; forward to SF-SYN-EN if string function.
  12311.  
  12312.         SET     6,(HL)          ; signal numeric result
  12313.  
  12314. ;; SF-SYN-EN
  12315. L27F4:  JP      L2712           ; jump back to S-CONT-2 to continue scanning.
  12316.  
  12317. ; ---
  12318.  
  12319. ; the branch was here in runtime.
  12320.  
  12321. ;; SF-RUN
  12322. L27F7:  RST     20H             ; NEXT-CHAR fetches name
  12323.         AND     $DF             ; AND 11101111 - reset bit 5 - upper-case.
  12324.         LD      B,A             ; save in B
  12325.  
  12326.         RST     20H             ; NEXT-CHAR
  12327.         SUB     $24             ; subtract '$'
  12328.         LD      C,A             ; save result in C
  12329.         JR      NZ,L2802        ; forward if not '$' to SF-ARGMT1
  12330.  
  12331.         RST     20H             ; NEXT-CHAR advances to bracket
  12332.  
  12333. ;; SF-ARGMT1
  12334. L2802:  RST     20H             ; NEXT-CHAR advances to start of argument
  12335.         PUSH    HL              ; save address
  12336.         LD      HL,($5C53)      ; fetch start of program area from PROG
  12337.         DEC     HL              ; the search starting point is the previous
  12338.                                 ; location.
  12339.  
  12340. ;; SF-FND-DF
  12341. L2808:  LD      DE,$00CE        ; search is for token 'DEF FN' in E,
  12342.                                 ; statement count in D.
  12343.         PUSH    BC              ; save C the string test, and B the letter.
  12344.         CALL    L1D86           ; routine LOOK-PROG will search for token.
  12345.         POP     BC              ; restore BC.
  12346.         JR      NC,L2814        ; forward to SF-CP-DEF if a match was found.
  12347.  
  12348.  
  12349. ;; REPORT-P
  12350. L2812:  RST     08H             ; ERROR-1
  12351.         DB    $18             ; Error Report: FN without DEF
  12352.  
  12353. ;; SF-CP-DEF
  12354. L2814:  PUSH    HL              ; save address of DEF FN
  12355.         CALL    L28AB           ; routine FN-SKPOVR skips over white-space etc.
  12356.                                 ; without disturbing CH-ADD.
  12357.         AND     $DF             ; make fetched character upper-case.
  12358.         CP      B               ; compare with FN name
  12359.         JR      NZ,L2825        ; forward to SF-NOT-FD if no match.
  12360.  
  12361. ; the letters match so test the type.
  12362.  
  12363.         CALL    L28AB           ; routine FN-SKPOVR skips white-space
  12364.         SUB     $24             ; subtract '$' from fetched character
  12365.         CP      C               ; compare with saved result of same operation
  12366.                                 ; on FN name.
  12367.         JR      Z,L2831         ; forward to SF-VALUES with a match.
  12368.  
  12369. ; the letters matched but one was string and the other numeric.
  12370.  
  12371. ;; SF-NOT-FD
  12372. L2825:  POP     HL              ; restore search point.
  12373.         DEC     HL              ; make location before
  12374.         LD      DE,$0200        ; the search is to be for the end of the
  12375.                                 ; current definition - 2 statements forward.
  12376.         PUSH    BC              ; save the letter/type
  12377.         CALL    L198B           ; routine EACH-STMT steps past rejected
  12378.                                 ; definition.
  12379.         POP     BC              ; restore letter/type
  12380.         JR      L2808           ; back to SF-FND-DF to continue search
  12381.  
  12382. ; ---
  12383.  
  12384. ; Success!
  12385. ; the branch was here with matching letter and numeric/string type.
  12386.  
  12387. ;; SF-VALUES
  12388. L2831:  AND     A               ; test A ( will be zero if string '$' - '$' )
  12389.  
  12390.         CALL    Z,L28AB         ; routine FN-SKPOVR advances HL past '$'.
  12391.  
  12392.         POP     DE              ; discard pointer to 'DEF FN'.
  12393.         POP     DE              ; restore pointer to first FN argument.
  12394.         LD      ($5C5D),DE      ; save in CH_ADD
  12395.  
  12396.         CALL    L28AB           ; routine FN-SKPOVR advances HL past '('
  12397.         PUSH    HL              ; save start address in DEF FN  ***
  12398.         CP      $29             ; is character a ')' ?
  12399.         JR      Z,L2885         ; forward to SF-R-BR-2 if no arguments.
  12400.  
  12401. ;; SF-ARG-LP
  12402. L2843:  INC     HL              ; point to next character.
  12403.         LD      A,(HL)          ; fetch it.
  12404.         CP      $0E             ; is it the number marker
  12405.         LD      D,$40           ; signal numeric in D.
  12406.         JR      Z,L2852         ; forward to SF-ARG-VL if numeric.
  12407.  
  12408.         DEC     HL              ; back to letter
  12409.         CALL    L28AB           ; routine FN-SKPOVR skips any white-space
  12410.         INC     HL              ; advance past the expected '$' to
  12411.                                 ; the 'hidden' marker.
  12412.         LD      D,$00           ; signal string.
  12413.  
  12414. ;; SF-ARG-VL
  12415. L2852:  INC     HL              ; now address first of 5-byte location.
  12416.         PUSH    HL              ; save address in DEF FN statement
  12417.         PUSH    DE              ; save D - result type
  12418.  
  12419.         CALL    L24FB           ; routine SCANNING evaluates expression in
  12420.                                 ; the FN statement setting FLAGS and leaving
  12421.                                 ; result as last value on calculator stack.
  12422.  
  12423.         POP     AF              ; restore saved result type to A
  12424.  
  12425.         XOR     (IY+$01)        ; xor with FLAGS
  12426.         AND     $40             ; and with 01000000 to test bit 6
  12427.         JR      NZ,L288B        ; forward to REPORT-Q if type mismatch.
  12428.                                 ; 'Parameter error'
  12429.  
  12430.         POP     HL              ; pop the start address in DEF FN statement
  12431.         EX      DE,HL           ; transfer to DE ?? pop straight into de ?
  12432.  
  12433.         LD      HL,($5C65)      ; set HL to STKEND location after value
  12434.         LD      BC,$0005        ; five bytes to move
  12435.         SBC     HL,BC           ; decrease HL by 5 to point to start.
  12436.         LD      ($5C65),HL      ; set STKEND 'removing' value from stack.
  12437.  
  12438.         LDIR                    ; copy value into DEF FN statement
  12439.         EX      DE,HL           ; set HL to location after value in DEF FN
  12440.         DEC     HL              ; step back one
  12441.         CALL    L28AB           ; routine FN-SKPOVR gets next valid character
  12442.         CP      $29             ; is it ')' end of arguments ?
  12443.         JR      Z,L2885         ; forward to SF-R-BR-2 if so.
  12444.  
  12445. ; a comma separator has been encountered in the DEF FN argument list.
  12446.  
  12447.         PUSH    HL              ; save position in DEF FN statement
  12448.  
  12449.         RST     18H             ; GET-CHAR from FN statement
  12450.         CP      $2C             ; is it ',' ?
  12451.         JR      NZ,L288B        ; forward to REPORT-Q if not
  12452.                                 ; 'Parameter error'
  12453.  
  12454.         RST     20H             ; NEXT-CHAR in FN statement advances to next
  12455.                                 ; argument.
  12456.  
  12457.         POP     HL              ; restore DEF FN pointer
  12458.         CALL    L28AB           ; routine FN-SKPOVR advances to corresponding
  12459.                                 ; argument.
  12460.  
  12461.         JR      L2843           ; back to SF-ARG-LP looping until all
  12462.                                 ; arguments are passed into the DEF FN
  12463.                                 ; hidden locations.
  12464.  
  12465. ; ---
  12466.  
  12467. ; the branch was here when all arguments passed.
  12468.  
  12469. ;; SF-R-BR-2
  12470. L2885:  PUSH    HL              ; save location of ')' in DEF FN
  12471.  
  12472.         RST     18H             ; GET-CHAR gets next character in FN
  12473.         CP      $29             ; is it a ')' also ?
  12474.         JR      Z,L288D         ; forward to SF-VALUE if so.
  12475.  
  12476.  
  12477. ;; REPORT-Q
  12478. L288B:  RST     08H             ; ERROR-1
  12479.         DB    $19             ; Error Report: Parameter error
  12480.  
  12481. ;; SF-VALUE
  12482. L288D:  POP     DE              ; location of ')' in DEF FN to DE.
  12483.         EX      DE,HL           ; now to HL, FN ')' pointer to DE.
  12484.         LD      ($5C5D),HL      ; initialize CH_ADD to this value.
  12485.  
  12486. ; At this point the start of the DEF FN argument list is on the machine stack.
  12487. ; We also have to consider that this defined function may form part of the
  12488. ; definition of another defined function (though not itself).
  12489. ; As this defined function may be part of a hierarchy of defined functions
  12490. ; currently being evaluated by recursive calls to SCANNING, then we have to
  12491. ; preserve the original value of DEFADD and not assume that it is zero.
  12492.  
  12493.         LD      HL,($5C0B)      ; get original DEFADD address
  12494.         EX      (SP),HL         ; swap with DEF FN address on stack ***
  12495.         LD      ($5C0B),HL      ; set DEFADD to point to this argument list
  12496.                                 ; during scanning.
  12497.  
  12498.         PUSH    DE              ; save FN ')' pointer.
  12499.  
  12500.         RST     20H             ; NEXT-CHAR advances past ')' in define
  12501.  
  12502.         RST     20H             ; NEXT-CHAR advances past '=' to expression
  12503.  
  12504.         CALL    L24FB           ; routine SCANNING evaluates but searches
  12505.                                 ; initially for variables at DEFADD
  12506.  
  12507.         POP     HL              ; pop the FN ')' pointer
  12508.         LD      ($5C5D),HL      ; set CH_ADD to this
  12509.         POP     HL              ; pop the original DEFADD value
  12510.         LD      ($5C0B),HL      ; and re-insert into DEFADD system variable.
  12511.  
  12512.         RST     20H             ; NEXT-CHAR advances to character after ')'
  12513.         JP      L2712           ; to S-CONT-2 - to continue current
  12514.                                 ; invocation of scanning
  12515.  
  12516. ; --------------------
  12517. ; Used to parse DEF FN
  12518. ; --------------------
  12519. ; e.g. DEF FN     s $ ( x )     =  b     $ (  TO  x  ) : REM exaggerated
  12520. ;
  12521. ; This routine is used 10 times to advance along a DEF FN statement
  12522. ; skipping spaces and colour control codes. It is similar to NEXT-CHAR
  12523. ; which is, at the same time, used to skip along the corresponding FN function
  12524. ; except the latter has to deal with AT and TAB characters in string
  12525. ; expressions. These cannot occur in a program area so this routine is
  12526. ; simpler as both colour controls and their parameters are less than space.
  12527.  
  12528. ;; FN-SKPOVR
  12529. L28AB:  INC     HL              ; increase pointer
  12530.         LD      A,(HL)          ; fetch addressed character
  12531.         CP      $21             ; compare with space + 1
  12532.         JR      C,L28AB         ; back to FN-SKPOVR if less
  12533.  
  12534.         RET                     ; return pointing to a valid character.
  12535.  
  12536. ; ---------
  12537. ; LOOK-VARS
  12538. ; ---------
  12539. ;
  12540. ;
  12541.  
  12542. ;; LOOK-VARS
  12543. L28B2:  SET     6,(IY+$01)      ; update FLAGS - presume numeric result
  12544.  
  12545.         RST     18H             ; GET-CHAR
  12546.         CALL    L2C8D           ; routine ALPHA tests for A-Za-z
  12547.         JP      NC,L1C8A        ; jump to REPORT-C if not.
  12548.                                 ; 'Nonsense in BASIC'
  12549.  
  12550.         PUSH    HL              ; save pointer to first letter       ^1
  12551.         AND     $1F             ; mask lower bits, 1 - 26 decimal     000xxxxx
  12552.         LD      C,A             ; store in C.
  12553.  
  12554.         RST     20H             ; NEXT-CHAR
  12555.         PUSH    HL              ; save pointer to second character   ^2
  12556.         CP      $28             ; is it '(' - an array ?
  12557.         JR      Z,L28EF         ; forward to V-RUN/SYN if so.
  12558.  
  12559.         SET     6,C             ; set 6 signaling string if solitary  010
  12560.         CP      $24             ; is character a '$' ?
  12561.         JR      Z,L28DE         ; forward to V-STR-VAR
  12562.  
  12563.         SET     5,C             ; signal numeric                       011
  12564.         CALL    L2C88           ; routine ALPHANUM sets carry if second
  12565.                                 ; character is alphanumeric.
  12566.         JR      NC,L28E3        ; forward to V-TEST-FN if just one character
  12567.  
  12568. ; it is more than one character but re-test current character so that 6 reset
  12569. ; Note. this is a rare lack of elegance. Bit 6 could be reset once before
  12570. ; entering the loop. Another puzzle is that this loop renders the similar
  12571. ; loop at V-PASS redundant.
  12572.  
  12573. ;; V-CHAR
  12574. L28D4:  CALL    L2C88           ; routine ALPHANUM
  12575.         JR      NC,L28EF        ; to V-RUN/SYN when no more
  12576.  
  12577.         RES     6,C             ; make long named type                 001
  12578.  
  12579.         RST     20H             ; NEXT-CHAR
  12580.         JR      L28D4           ; loop back to V-CHAR
  12581.  
  12582. ; ---
  12583.  
  12584.  
  12585. ;; V-STR-VAR
  12586. L28DE:  RST     20H             ; NEXT-CHAR advances past '$'
  12587.         RES     6,(IY+$01)      ; update FLAGS - signal string result.
  12588.  
  12589. ;; V-TEST-FN
  12590. L28E3:  LD      A,($5C0C)       ; load A with DEFADD_hi
  12591.         AND     A               ; and test for zero.
  12592.         JR      Z,L28EF         ; forward to V-RUN/SYN if a defined function
  12593.                                 ; is not being evaluated.
  12594.  
  12595. ; Note.
  12596.  
  12597.         CALL    L2530           ; routine SYNTAX-Z
  12598.         JP      NZ,L2951        ; JUMP to STK-F-ARG in runtime and then
  12599.                                 ; back to this point if no variable found.
  12600.  
  12601. ;; V-RUN/SYN
  12602. L28EF:  LD      B,C             ; save flags in B
  12603.         CALL    L2530           ; routine SYNTAX-Z
  12604.         JR      NZ,L28FD        ; to V-RUN to look for the variable in runtime
  12605.  
  12606. ; if checking syntax the letter is not returned
  12607.  
  12608.         LD      A,C             ; copy letter/flags to A
  12609.         AND     $E0             ; and with 11100000 to get rid of the letter
  12610.         SET     7,A             ; use spare bit to signal checking syntax.
  12611.         LD      C,A             ; and transfer to C.
  12612.         JR      L2934           ; forward to V-SYNTAX
  12613.  
  12614. ; ---
  12615.  
  12616. ; but in runtime search for the variable.
  12617.  
  12618. ;; V-RUN
  12619. L28FD:  LD      HL,($5C4B)      ; set HL to start of variables from VARS
  12620.  
  12621. ;; V-EACH
  12622. L2900:  LD      A,(HL)          ; get first character
  12623.         AND     $7F             ; and with 01111111
  12624.                                 ; ignoring bit 7 which distinguishes
  12625.                                 ; arrays or for/next variables.
  12626.  
  12627.         JR      Z,L2932         ; to V-80-BYTE if zero as must be 10000000
  12628.                                 ; the variables end-marker.
  12629.  
  12630.         CP      C               ; compare with supplied value.
  12631.         JR      NZ,L292A        ; forward to V-NEXT if no match.
  12632.  
  12633.         RLA                     ; destructively test
  12634.         ADD     A,A             ; bits 5 and 6 of A
  12635.                                 ; jumping if bit 5 reset or 6 set
  12636.  
  12637.         JP      P,L293F         ; to V-FOUND-2  strings and arrays
  12638.  
  12639.         JR      C,L293F         ; to V-FOUND-2  simple and for next
  12640.  
  12641. ; leaving long name variables.
  12642.  
  12643.         POP     DE              ; pop pointer to 2nd. char
  12644.         PUSH    DE              ; save it again
  12645.         PUSH    HL              ; save variable first character pointer
  12646.  
  12647. ;; V-MATCHES
  12648. L2912:  INC     HL              ; address next character in vars area
  12649.  
  12650. ;; V-SPACES
  12651. L2913:  LD      A,(DE)          ; pick up letter from prog area
  12652.         INC     DE              ; and advance address
  12653.         CP      $20             ; is it a space
  12654.         JR      Z,L2913         ; back to V-SPACES until non-space
  12655.  
  12656.         OR      $20             ; convert to range 1 - 26.
  12657.         CP      (HL)            ; compare with addressed variables character
  12658.         JR      Z,L2912         ; loop back to V-MATCHES if a match on an
  12659.                                 ; intermediate letter.
  12660.  
  12661.         OR      $80             ; now set bit 7 as last character of long
  12662.                                 ; names are inverted.
  12663.         CP      (HL)            ; compare again
  12664.         JR      NZ,L2929        ; forward to V-GET-PTR if no match
  12665.  
  12666. ; but if they match check that this is also last letter in prog area
  12667.  
  12668.         LD      A,(DE)          ; fetch next character
  12669.         CALL    L2C88           ; routine ALPHANUM sets carry if not alphanum
  12670.         JR      NC,L293E        ; forward to V-FOUND-1 with a full match.
  12671.  
  12672. ;; V-GET-PTR
  12673. L2929:  POP     HL              ; pop saved pointer to char 1
  12674.  
  12675. ;; V-NEXT
  12676. L292A:  PUSH    BC              ; save flags
  12677.         CALL    L19B8           ; routine NEXT-ONE gets next variable in DE
  12678.         EX      DE,HL           ; transfer to HL.
  12679.         POP     BC              ; restore the flags
  12680.         JR      L2900           ; loop back to V-EACH
  12681.                                 ; to compare each variable
  12682.  
  12683. ; ---
  12684.  
  12685. ;; V-80-BYTE
  12686. L2932:  SET     7,B             ; will signal not found
  12687.  
  12688. ; the branch was here when checking syntax
  12689.  
  12690. ;; V-SYNTAX
  12691. L2934:  POP     DE              ; discard the pointer to 2nd. character  v2
  12692.                                 ; in BASIC line/workspace.
  12693.  
  12694.         RST     18H             ; GET-CHAR gets character after variable name.
  12695.         CP      $28             ; is it '(' ?
  12696.         JR      Z,L2943         ; forward to V-PASS
  12697.                                 ; Note. could go straight to V-END ?
  12698.  
  12699.         SET     5,B             ; signal not an array
  12700.         JR      L294B           ; forward to V-END
  12701.  
  12702. ; ---------------------------
  12703.  
  12704. ; the jump was here when a long name matched and HL pointing to last character
  12705. ; in variables area.
  12706.  
  12707. ;; V-FOUND-1
  12708. L293E:  POP     DE              ; discard pointer to first var letter
  12709.  
  12710. ; the jump was here with all other matches HL points to first var char.
  12711.  
  12712. ;; V-FOUND-2
  12713. L293F:  POP     DE              ; discard pointer to 2nd prog char       v2
  12714.         POP     DE              ; drop pointer to 1st prog char          v1
  12715.         PUSH    HL              ; save pointer to last char in vars
  12716.  
  12717.         RST     18H             ; GET-CHAR
  12718.  
  12719. ;; V-PASS
  12720. L2943:  CALL    L2C88           ; routine ALPHANUM
  12721.         JR      NC,L294B        ; forward to V-END if not
  12722.  
  12723. ; but it never will be as we advanced past long-named variables earlier.
  12724.  
  12725.         RST     20H             ; NEXT-CHAR
  12726.         JR      L2943           ; back to V-PASS
  12727.  
  12728. ; ---
  12729.  
  12730. ;; V-END
  12731. L294B:  POP     HL              ; pop the pointer to first character in
  12732.                                 ; BASIC line/workspace.
  12733.         RL      B               ; rotate the B register left
  12734.                                 ; bit 7 to carry
  12735.         BIT     6,B             ; test the array indicator bit.
  12736.         RET                     ; return
  12737.  
  12738. ; -----------------------
  12739. ; Stack function argument
  12740. ; -----------------------
  12741. ; This branch is taken from LOOK-VARS when a defined function is currently
  12742. ; being evaluated.
  12743. ; Scanning is evaluating the expression after the '=' and the variable
  12744. ; found could be in the argument list to the left of the '=' or in the
  12745. ; normal place after the program. Preference will be given to the former.
  12746. ; The variable name to be matched is in C.
  12747.  
  12748. ;; STK-F-ARG
  12749. L2951:  LD      HL,($5C0B)      ; set HL to DEFADD
  12750.         LD      A,(HL)          ; load the first character
  12751.         CP      $29             ; is it ')' ?
  12752.         JP      Z,L28EF         ; JUMP back to V-RUN/SYN, if so, as there are
  12753.                                 ; no arguments.
  12754.  
  12755. ; but proceed to search argument list of defined function first if not empty.
  12756.  
  12757. ;; SFA-LOOP
  12758. L295A:  LD      A,(HL)          ; fetch character again.
  12759.         OR      $60             ; or with 01100000 presume a simple variable.
  12760.         LD      B,A             ; save result in B.
  12761.         INC     HL              ; address next location.
  12762.         LD      A,(HL)          ; pick up byte.
  12763.         CP      $0E             ; is it the number marker ?
  12764.         JR      Z,L296B         ; forward to SFA-CP-VR if so.
  12765.  
  12766. ; it was a string. White-space may be present but syntax has been checked.
  12767.  
  12768.         DEC     HL              ; point back to letter.
  12769.         CALL    L28AB           ; routine FN-SKPOVR skips to the '$'
  12770.         INC     HL              ; now address the hidden marker.
  12771.         RES     5,B             ; signal a string variable.
  12772.  
  12773. ;; SFA-CP-VR
  12774. L296B:  LD      A,B             ; transfer found variable letter to A.
  12775.         CP      C               ; compare with expected.
  12776.         JR      Z,L2981         ; forward to SFA-MATCH with a match.
  12777.  
  12778.         INC     HL              ; step
  12779.         INC     HL              ; past
  12780.         INC     HL              ; the
  12781.         INC     HL              ; five
  12782.         INC     HL              ; bytes.
  12783.  
  12784.         CALL    L28AB           ; routine FN-SKPOVR skips to next character
  12785.         CP      $29             ; is it ')' ?
  12786.         JP      Z,L28EF         ; jump back if so to V-RUN/SYN to look in
  12787.                                 ; normal variables area.
  12788.  
  12789.         CALL    L28AB           ; routine FN-SKPOVR skips past the ','
  12790.                                 ; all syntax has been checked and these
  12791.                                 ; things can be taken as read.
  12792.         JR      L295A           ; back to SFA-LOOP while there are more
  12793.                                 ; arguments.
  12794.  
  12795. ; ---
  12796.  
  12797. ;; SFA-MATCH
  12798. L2981:  BIT     5,C             ; test if numeric
  12799.         JR      NZ,L2991        ; to SFA-END if so as will be stacked
  12800.                                 ; by scanning
  12801.  
  12802.         INC     HL              ; point to start of string descriptor
  12803.         LD      DE,($5C65)      ; set DE to STKEND
  12804.         CALL    L33C0           ; routine MOVE-FP puts parameters on stack.
  12805.         EX      DE,HL           ; new free location to HL.
  12806.         LD      ($5C65),HL      ; use it to set STKEND system variable.
  12807.  
  12808. ;; SFA-END
  12809. L2991:  POP     DE              ; discard
  12810.         POP     DE              ; pointers.
  12811.         XOR     A               ; clear carry flag.
  12812.         INC     A               ; and zero flag.
  12813.         RET                     ; return.
  12814.  
  12815. ; ------------------------
  12816. ; Stack variable component
  12817. ; ------------------------
  12818. ; This is called to evaluate a complex structure that has been found, in
  12819. ; runtime, by LOOK-VARS in the variables area.
  12820. ; In this case HL points to the initial letter, bits 7-5
  12821. ; of which indicate the type of variable.
  12822. ; 010 - simple string, 110 - string array, 100 - array of numbers.
  12823. ;
  12824. ; It is called from CLASS-01 when assigning to a string or array including
  12825. ; a slice.
  12826. ; It is called from SCANNING to isolate the required part of the structure.
  12827. ;
  12828. ; An important part of the runtime process is to check that the number of
  12829. ; dimensions of the variable match the number of subscripts supplied in the
  12830. ; BASIC line.
  12831. ;
  12832. ; If checking syntax,
  12833. ; the B register, which counts dimensions is set to zero (256) to allow
  12834. ; the loop to continue till all subscripts are checked. While doing this it
  12835. ; is reading dimension sizes from some arbitrary area of memory. Although
  12836. ; these are meaningless it is of no concern as the limit is never checked by
  12837. ; int-exp during syntax checking.
  12838. ;
  12839. ; The routine is also called from the syntax path of DIM command to check the
  12840. ; syntax of both string and numeric arrays definitions except that bit 6 of C
  12841. ; is reset so both are checked as numeric arrays. This ruse avoids a terminal
  12842. ; slice being accepted as part of the DIM command.
  12843. ; All that is being checked is that there are a valid set of comma-separated
  12844. ; expressions before a terminal ')', although, as above, it will still go
  12845. ; through the motions of checking dummy dimension sizes.
  12846.  
  12847. ;; STK-VAR
  12848. L2996:  XOR     A               ; clear A
  12849.         LD      B,A             ; and B, the syntax dimension counter (256)
  12850.         BIT     7,C             ; checking syntax ?
  12851.         JR      NZ,L29E7        ; forward to SV-COUNT if so.
  12852.  
  12853. ; runtime evaluation.
  12854.  
  12855.         BIT     7,(HL)          ; will be reset if a simple string.
  12856.         JR      NZ,L29AE        ; forward to SV-ARRAYS otherwise
  12857.  
  12858.         INC     A               ; set A to 1, simple string.
  12859.  
  12860. ;; SV-SIMPLE$
  12861. L29A1:  INC     HL              ; address length low
  12862.         LD      C,(HL)          ; place in C
  12863.         INC     HL              ; address length high
  12864.         LD      B,(HL)          ; place in B
  12865.         INC     HL              ; address start of string
  12866.         EX      DE,HL           ; DE = start now.
  12867.         CALL    L2AB2           ; routine STK-STO-$ stacks string parameters
  12868.                                 ; DE start in variables area,
  12869.                                 ; BC length, A=1 simple string
  12870.  
  12871. ; the only thing now is to consider if a slice is required.
  12872.  
  12873.         RST     18H             ; GET-CHAR puts character at CH_ADD in A
  12874.         JP      L2A49           ; jump forward to SV-SLICE? to test for '('
  12875.  
  12876. ; --------------------------------------------------------
  12877.  
  12878. ; the branch was here with string and numeric arrays in runtime.
  12879.  
  12880. ;; SV-ARRAYS
  12881. L29AE:  INC     HL              ; step past
  12882.         INC     HL              ; the total length
  12883.         INC     HL              ; to address Number of dimensions.
  12884.         LD      B,(HL)          ; transfer to B overwriting zero.
  12885.         BIT     6,C             ; a numeric array ?
  12886.         JR      Z,L29C0         ; forward to SV-PTR with numeric arrays
  12887.  
  12888.         DEC     B               ; ignore the final element of a string array
  12889.                                 ; the fixed string size.
  12890.  
  12891.         JR      Z,L29A1         ; back to SV-SIMPLE$ if result is zero as has
  12892.                                 ; been created with DIM a$(10) for instance
  12893.                                 ; and can be treated as a simple string.
  12894.  
  12895. ; proceed with multi-dimensioned string arrays in runtime.
  12896.  
  12897.         EX      DE,HL           ; save pointer to dimensions in DE
  12898.  
  12899.         RST     18H             ; GET-CHAR looks at the BASIC line
  12900.         CP      $28             ; is character '(' ?
  12901.         JR      NZ,L2A20        ; to REPORT-3 if not
  12902.                                 ; 'Subscript wrong'
  12903.  
  12904.         EX      DE,HL           ; dimensions pointer to HL to synchronize
  12905.                                 ; with next instruction.
  12906.  
  12907. ; runtime numeric arrays path rejoins here.
  12908.  
  12909. ;; SV-PTR
  12910. L29C0:  EX      DE,HL           ; save dimension pointer in DE
  12911.         JR      L29E7           ; forward to SV-COUNT with true no of dims
  12912.                                 ; in B. As there is no initial comma the
  12913.                                 ; loop is entered at the midpoint.
  12914.  
  12915. ; ----------------------------------------------------------
  12916. ; the dimension counting loop which is entered at mid-point.
  12917.  
  12918. ;; SV-COMMA
  12919. L29C3:  PUSH    HL              ; save counter
  12920.  
  12921.         RST     18H             ; GET-CHAR
  12922.  
  12923.         POP     HL              ; pop counter
  12924.         CP      $2C             ; is character ',' ?
  12925.         JR      Z,L29EA         ; forward to SV-LOOP if so
  12926.  
  12927. ; in runtime the variable definition indicates a comma should appear here
  12928.  
  12929.         BIT     7,C             ; checking syntax ?
  12930.         JR      Z,L2A20         ; forward to REPORT-3 if not
  12931.                                 ; 'Subscript error'
  12932.  
  12933. ; proceed if checking syntax of an array?
  12934.  
  12935.         BIT     6,C             ; array of strings
  12936.         JR      NZ,L29D8        ; forward to SV-CLOSE if so
  12937.  
  12938. ; an array of numbers.
  12939.  
  12940.         CP      $29             ; is character ')' ?
  12941.         JR      NZ,L2A12        ; forward to SV-RPT-C if not
  12942.                                 ; 'Nonsense in BASIC'
  12943.  
  12944.         RST     20H             ; NEXT-CHAR moves CH-ADD past the statement
  12945.         RET                     ; return ->
  12946.  
  12947. ; ---
  12948.  
  12949. ; the branch was here with an array of strings.
  12950.  
  12951. ;; SV-CLOSE
  12952. L29D8:  CP      $29             ; as above ')' could follow the expression
  12953.         JR      Z,L2A48         ; forward to SV-DIM if so
  12954.  
  12955.         CP      $CC             ; is it 'TO' ?
  12956.         JR      NZ,L2A12        ; to SV-RPT-C with anything else
  12957.                                 ; 'Nonsense in BASIC'
  12958.  
  12959. ; now backtrack CH_ADD to set up for slicing routine.
  12960. ; Note. in a BASIC line we can safely backtrack to a colour parameter.
  12961.  
  12962. ;; SV-CH-ADD
  12963. L29E0:  RST     18H             ; GET-CHAR
  12964.         DEC     HL              ; backtrack HL
  12965.         LD      ($5C5D),HL      ; to set CH_ADD up for slicing routine
  12966.         JR      L2A45           ; forward to SV-SLICE and make a return
  12967.                                 ; when all slicing complete.
  12968.  
  12969. ; ----------------------------------------
  12970. ; -> the mid-point entry point of the loop
  12971.  
  12972. ;; SV-COUNT
  12973. L29E7:  LD      HL,$0000        ; initialize data pointer to zero.
  12974.  
  12975. ;; SV-LOOP
  12976. L29EA:  PUSH    HL              ; save the data pointer.
  12977.  
  12978.         RST     20H             ; NEXT-CHAR in BASIC area points to an
  12979.                                 ; expression.
  12980.  
  12981.         POP     HL              ; restore the data pointer.
  12982.         LD      A,C             ; transfer name/type to A.
  12983.         CP      $C0             ; is it 11000000 ?
  12984.                                 ; Note. the letter component is absent if
  12985.                                 ; syntax checking.
  12986.         JR      NZ,L29FB        ; forward to SV-MULT if not an array of
  12987.                                 ; strings.
  12988.  
  12989. ; proceed to check string arrays during syntax.
  12990.  
  12991.         RST     18H             ; GET-CHAR
  12992.         CP      $29             ; ')'  end of subscripts ?
  12993.         JR      Z,L2A48         ; forward to SV-DIM to consider further slice
  12994.  
  12995.         CP      $CC             ; is it 'TO' ?
  12996.         JR      Z,L29E0         ; back to SV-CH-ADD to consider a slice.
  12997.                                 ; (no need to repeat get-char at L29E0)
  12998.  
  12999. ; if neither, then an expression is required so rejoin runtime loop ??
  13000. ; registers HL and DE only point to somewhere meaningful in runtime so
  13001. ; comments apply to that situation.
  13002.  
  13003. ;; SV-MULT
  13004. L29FB:  PUSH    BC              ; save dimension number.
  13005.         PUSH    HL              ; push data pointer/rubbish.
  13006.                                 ; DE points to current dimension.
  13007.         CALL    L2AEE           ; routine DE,(DE+1) gets next dimension in DE
  13008.                                 ; and HL points to it.
  13009.         EX      (SP),HL         ; dim pointer to stack, data pointer to HL (*)
  13010.         EX      DE,HL           ; data pointer to DE, dim size to HL.
  13011.  
  13012.         CALL    L2ACC           ; routine INT-EXP1 checks integer expression
  13013.                                 ; and gets result in BC in runtime.
  13014.         JR      C,L2A20         ; to REPORT-3 if > HL
  13015.                                 ; 'Subscript out of range'
  13016.  
  13017.         DEC     BC              ; adjust returned result from 1-x to 0-x
  13018.         CALL    L2AF4           ; routine GET-HL*DE multiplies data pointer by
  13019.                                 ; dimension size.
  13020.         ADD     HL,BC           ; add the integer returned by expression.
  13021.         POP     DE              ; pop the dimension pointer.                              ***
  13022.         POP     BC              ; pop dimension counter.
  13023.         DJNZ    L29C3           ; back to SV-COMMA if more dimensions
  13024.                                 ; Note. during syntax checking, unless there
  13025.                                 ; are more than 256 subscripts, the branch
  13026.                                 ; back to SV-COMMA is always taken.
  13027.  
  13028.         BIT     7,C             ; are we checking syntax ?
  13029.                                 ; then we've got a joker here.
  13030.  
  13031. ;; SV-RPT-C
  13032. L2A12:  JR      NZ,L2A7A        ; forward to SL-RPT-C if so
  13033.                                 ; 'Nonsense in BASIC'
  13034.                                 ; more than 256 subscripts in BASIC line.
  13035.  
  13036. ; but in runtime the number of subscripts are at least the same as dims
  13037.  
  13038.         PUSH    HL              ; save data pointer.
  13039.         BIT     6,C             ; is it a string array ?
  13040.         JR      NZ,L2A2C        ; forward to SV-ELEM$ if so.
  13041.  
  13042. ; a runtime numeric array subscript.
  13043.  
  13044.         LD      B,D             ; register DE has advanced past all dimensions
  13045.         LD      C,E             ; and points to start of data in variable.
  13046.                                 ; transfer it to BC.
  13047.  
  13048.         RST     18H             ; GET-CHAR checks BASIC line
  13049.         CP      $29             ; must be a ')' ?
  13050.         JR      Z,L2A22         ; skip to SV-NUMBER if so
  13051.  
  13052. ; else more subscripts in BASIC line than the variable definition.
  13053.  
  13054. ;; REPORT-3
  13055. L2A20:  RST     08H             ; ERROR-1
  13056.         DB    $02             ; Error Report: Subscript wrong
  13057.  
  13058. ; continue if subscripts matched the numeric array.
  13059.  
  13060. ;; SV-NUMBER
  13061. L2A22:  RST     20H             ; NEXT-CHAR moves CH_ADD to next statement
  13062.                                 ; - finished parsing.
  13063.  
  13064.         POP     HL              ; pop the data pointer.
  13065.         LD      DE,$0005        ; each numeric element is 5 bytes.
  13066.         CALL    L2AF4           ; routine GET-HL*DE multiplies.
  13067.         ADD     HL,BC           ; now add to start of data in the variable.
  13068.  
  13069.         RET                     ; return with HL pointing at the numeric
  13070.                                 ; array subscript.                       ->
  13071.  
  13072. ; ---------------------------------------------------------------
  13073.  
  13074. ; the branch was here for string subscripts when the number of subscripts
  13075. ; in the BASIC line was one less than in variable definition.
  13076.  
  13077. ;; SV-ELEM$
  13078. L2A2C:  CALL    L2AEE           ; routine DE,(DE+1) gets final dimension
  13079.                                 ; the length of strings in this array.
  13080.         EX      (SP),HL         ; start pointer to stack, data pointer to HL.
  13081.         CALL    L2AF4           ; routine GET-HL*DE multiplies by element
  13082.                                 ; size.
  13083.         POP     BC              ; the start of data pointer is added
  13084.         ADD     HL,BC           ; in - now points to location before.
  13085.         INC     HL              ; point to start of required string.
  13086.         LD      B,D             ; transfer the length (final dimension size)
  13087.         LD      C,E             ; from DE to BC.
  13088.         EX      DE,HL           ; put start in DE.
  13089.         CALL    L2AB1           ; routine STK-ST-0 stores the string parameters
  13090.                                 ; with A=0 - a slice or subscript.
  13091.  
  13092. ; now check that there were no more subscripts in the BASIC line.
  13093.  
  13094.         RST     18H             ; GET-CHAR
  13095.         CP      $29             ; is it ')' ?
  13096.         JR      Z,L2A48         ; forward to SV-DIM to consider a separate
  13097.                                 ; subscript or/and a slice.
  13098.  
  13099.         CP      $2C             ; a comma is allowed if the final subscript
  13100.                                 ; is to be sliced e.g a$(2,3,4 TO 6).
  13101.         JR      NZ,L2A20        ; to REPORT-3 with anything else
  13102.                                 ; 'Subscript error'
  13103.  
  13104. ;; SV-SLICE
  13105. L2A45:  CALL    L2A52           ; routine SLICING slices the string.
  13106.  
  13107. ; but a slice of a simple string can itself be sliced.
  13108.  
  13109. ;; SV-DIM
  13110. L2A48:  RST     20H             ; NEXT-CHAR
  13111.  
  13112. ;; SV-SLICE?
  13113. L2A49:  CP      $28             ; is character '(' ?
  13114.         JR      Z,L2A45         ; loop back if so to SV-SLICE
  13115.  
  13116.         RES     6,(IY+$01)      ; update FLAGS  - Signal string result
  13117.         RET                     ; and return.
  13118.  
  13119. ; ---
  13120.  
  13121. ; The above section deals with the flexible syntax allowed.
  13122. ; DIM a$(3,3,10) can be considered as two dimensional array of ten-character
  13123. ; strings or a 3-dimensional array of characters.
  13124. ; a$(1,1) will return a 10-character string as will a$(1,1,1 TO 10)
  13125. ; a$(1,1,1) will return a single character.
  13126. ; a$(1,1) (1 TO 6) is the same as a$(1,1,1 TO 6)
  13127. ; A slice can itself be sliced ad infinitum
  13128. ; b$ () () () () () () (2 TO 10) (2 TO 9) (3) is the same as b$(5)
  13129.  
  13130.  
  13131.  
  13132. ; -------------------------
  13133. ; Handle slicing of strings
  13134. ; -------------------------
  13135. ; The syntax of string slicing is very natural and it is as well to reflect
  13136. ; on the permutations possible.
  13137. ; a$() and a$( TO ) indicate the entire string although just a$ would do
  13138. ; and would avoid coming here.
  13139. ; h$(16) indicates the single character at position 16.
  13140. ; a$( TO 32) indicates the first 32 characters.
  13141. ; a$(257 TO) indicates all except the first 256 characters.
  13142. ; a$(19000 TO 19999) indicates the thousand characters at position 19000.
  13143. ; Also a$(9 TO 5) returns a null string not an error.
  13144. ; This enables a$(2 TO) to return a null string if the passed string is
  13145. ; of length zero or 1.
  13146. ; A string expression in brackets can be sliced. e.g. (STR$ PI) (3 TO )
  13147. ; We arrived here from SCANNING with CH-ADD pointing to the initial '('
  13148. ; or from above.
  13149.  
  13150. ;; SLICING
  13151. L2A52:  CALL    L2530           ; routine SYNTAX-Z
  13152.         CALL    NZ,L2BF1        ; routine STK-FETCH fetches parameters of
  13153.                                 ; string at runtime, start in DE, length
  13154.                                 ; in BC. This could be an array subscript.
  13155.  
  13156.         RST     20H             ; NEXT-CHAR
  13157.         CP      $29             ; is it ')' ?     e.g. a$()
  13158.         JR      Z,L2AAD         ; forward to SL-STORE to store entire string.
  13159.  
  13160.         PUSH    DE              ; else save start address of string
  13161.  
  13162.         XOR     A               ; clear accumulator to use as a running flag.
  13163.         PUSH    AF              ; and save on stack before any branching.
  13164.  
  13165.         PUSH    BC              ; save length of string to be sliced.
  13166.         LD      DE,$0001        ; default the start point to position 1.
  13167.  
  13168.         RST     18H             ; GET-CHAR
  13169.  
  13170.         POP     HL              ; pop length to HL as default end point
  13171.                                 ; and limit.
  13172.  
  13173.         CP      $CC             ; is it 'TO' ?    e.g. a$( TO 10000)
  13174.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter.
  13175.  
  13176.         POP     AF              ; pop the running flag.
  13177.  
  13178.         CALL    L2ACD           ; routine INT-EXP2 fetches first parameter.
  13179.  
  13180.         PUSH    AF              ; save flag (will be $FF if parameter>limit)
  13181.  
  13182.         LD      D,B             ; transfer the start
  13183.         LD      E,C             ; to DE overwriting 0001.
  13184.         PUSH    HL              ; save original length.
  13185.  
  13186.         RST     18H             ; GET-CHAR
  13187.         POP     HL              ; pop the limit length.
  13188.         CP      $CC             ; is it 'TO' after a start ?
  13189.         JR      Z,L2A81         ; to SL-SECOND to evaluate second parameter
  13190.  
  13191.         CP      $29             ; is it ')' ?       e.g. a$(365)
  13192.  
  13193. ;; SL-RPT-C
  13194. L2A7A:  JP      NZ,L1C8A        ; jump to REPORT-C with anything else
  13195.                                 ; 'Nonsense in BASIC'
  13196.  
  13197.         LD      H,D             ; copy start
  13198.         LD      L,E             ; to end - just a one character slice.
  13199.         JR      L2A94           ; forward to SL-DEFINE.
  13200.  
  13201. ; ---------------------
  13202.  
  13203. ;; SL-SECOND
  13204. L2A81:  PUSH    HL              ; save limit length.
  13205.  
  13206.         RST     20H             ; NEXT-CHAR
  13207.  
  13208.         POP     HL              ; pop the length.
  13209.  
  13210.         CP      $29             ; is character ')' ?        e.g a$(7 TO )
  13211.         JR      Z,L2A94         ; to SL-DEFINE using length as end point.
  13212.  
  13213.         POP     AF              ; else restore flag.
  13214.         CALL    L2ACD           ; routine INT-EXP2 gets second expression.
  13215.  
  13216.         PUSH    AF              ; save the running flag.
  13217.  
  13218.         RST     18H             ; GET-CHAR
  13219.  
  13220.         LD      H,B             ; transfer second parameter
  13221.         LD      L,C             ; to HL.              e.g. a$(42 to 99)
  13222.         CP      $29             ; is character a ')' ?
  13223.         JR      NZ,L2A7A        ; to SL-RPT-C if not
  13224.                                 ; 'Nonsense in BASIC'
  13225.  
  13226. ; we now have start in DE and an end in HL.
  13227.  
  13228. ;; SL-DEFINE
  13229. L2A94:  POP     AF              ; pop the running flag.
  13230.         EX      (SP),HL         ; put end point on stack, start address to HL
  13231.         ADD     HL,DE           ; add address of string to the start point.
  13232.         DEC     HL              ; point to first character of slice.
  13233.         EX      (SP),HL         ; start address to stack, end point to HL (*)
  13234.         AND     A               ; prepare to subtract.
  13235.         SBC     HL,DE           ; subtract start point from end point.
  13236.         LD      BC,$0000        ; default the length result to zero.
  13237.         JR      C,L2AA8         ; forward to SL-OVER if start > end.
  13238.  
  13239.         INC     HL              ; increment the length for inclusive byte.
  13240.  
  13241.         AND     A               ; now test the running flag.
  13242.         JP      M,L2A20         ; jump back to REPORT-3 if $FF.
  13243.                                 ; 'Subscript out of range'
  13244.  
  13245.         LD      B,H             ; transfer the length
  13246.         LD      C,L             ; to BC.
  13247.  
  13248. ;; SL-OVER
  13249. L2AA8:  POP     DE              ; restore start address from machine stack ***
  13250.         RES     6,(IY+$01)      ; update FLAGS - signal string result for
  13251.                                 ; syntax.
  13252.  
  13253. ;; SL-STORE
  13254. L2AAD:  CALL    L2530           ; routine SYNTAX-Z  (UNSTACK-Z?)
  13255.         RET     Z               ; return if checking syntax.
  13256.                                 ; but continue to store the string in runtime.
  13257.  
  13258. ; ------------------------------------
  13259. ; other than from above, this routine is called from STK-VAR to stack
  13260. ; a known string array element.
  13261. ; ------------------------------------
  13262.  
  13263. ;; STK-ST-0
  13264. L2AB1:  XOR     A               ; clear to signal a sliced string or element.
  13265.  
  13266. ; -------------------------
  13267. ; this routine is called from chr$, scrn$ etc. to store a simple string result.
  13268. ; --------------------------
  13269.  
  13270. ;; STK-STO-$
  13271. L2AB2:  RES     6,(IY+$01)      ; update FLAGS - signal string result.
  13272.                                 ; and continue to store parameters of string.
  13273.  
  13274. ; ---------------------------------------
  13275. ; Pass five registers to calculator stack
  13276. ; ---------------------------------------
  13277. ; This subroutine puts five registers on the calculator stack.
  13278.  
  13279. ;; STK-STORE
  13280. L2AB6:  PUSH    BC              ; save two registers
  13281.         CALL    L33A9           ; routine TEST-5-SP checks room and puts 5
  13282.                                 ; in BC.
  13283.         POP     BC              ; fetch the saved registers.
  13284.         LD      HL,($5C65)      ; make HL point to first empty location STKEND
  13285.         LD      (HL),A          ; place the 5 registers.
  13286.         INC     HL              ;
  13287.         LD      (HL),E          ;
  13288.         INC     HL              ;
  13289.         LD      (HL),D          ;
  13290.         INC     HL              ;
  13291.         LD      (HL),C          ;
  13292.         INC     HL              ;
  13293.         LD      (HL),B          ;
  13294.         INC     HL              ;
  13295.         LD      ($5C65),HL      ; update system variable STKEND.
  13296.         RET                     ; and return.
  13297.  
  13298. ; -------------------------------------------
  13299. ; Return result of evaluating next expression
  13300. ; -------------------------------------------
  13301. ; This clever routine is used to check and evaluate an integer expression
  13302. ; which is returned in BC, setting A to $FF, if greater than a limit supplied
  13303. ; in HL. It is used to check array subscripts, parameters of a string slice
  13304. ; and the arguments of the DIM command. In the latter case, the limit check
  13305. ; is not required and H is set to $FF. When checking optional string slice
  13306. ; parameters, it is entered at the second entry point so as not to disturb
  13307. ; the running flag A, which may be $00 or $FF from a previous invocation.
  13308.  
  13309. ;; INT-EXP1
  13310. L2ACC:  XOR     A               ; set result flag to zero.
  13311.  
  13312. ; -> The entry point is here if A is used as a running flag.
  13313.  
  13314. ;; INT-EXP2
  13315. L2ACD:  PUSH    DE              ; preserve DE register throughout.
  13316.         PUSH    HL              ; save the supplied limit.
  13317.         PUSH    AF              ; save the flag.
  13318.  
  13319.         CALL    L1C82           ; routine EXPT-1NUM evaluates expression
  13320.                                 ; at CH_ADD returning if numeric result,
  13321.                                 ; with value on calculator stack.
  13322.  
  13323.         POP     AF              ; pop the flag.
  13324.         CALL    L2530           ; routine SYNTAX-Z
  13325.         JR      Z,L2AEB         ; forward to I-RESTORE if checking syntax so
  13326.                                 ; avoiding a comparison with supplied limit.
  13327.  
  13328.         PUSH    AF              ; save the flag.
  13329.  
  13330.         CALL    L1E99           ; routine FIND-INT2 fetches value from
  13331.                                 ; calculator stack to BC producing an error
  13332.                                 ; if too high.
  13333.  
  13334.         POP     DE              ; pop the flag to D.
  13335.         LD      A,B             ; test value for zero and reject
  13336.         OR      C               ; as arrays and strings begin at 1.
  13337.         SCF                     ; set carry flag.
  13338.         JR      Z,L2AE8         ; forward to I-CARRY if zero.
  13339.  
  13340.         POP     HL              ; restore the limit.
  13341.         PUSH    HL              ; and save.
  13342.         AND     A               ; prepare to subtract.
  13343.         SBC     HL,BC           ; subtract value from limit.
  13344.  
  13345. ;; I-CARRY
  13346. L2AE8:  LD      A,D             ; move flag to accumulator $00 or $FF.
  13347.         SBC     A,$00           ; will set to $FF if carry set.
  13348.  
  13349. ;; I-RESTORE
  13350. L2AEB:  POP     HL              ; restore the limit.
  13351.         POP     DE              ; and DE register.
  13352.         RET                     ; return.
  13353.  
  13354.  
  13355. ; -----------------------
  13356. ; LD DE,(DE+1) Subroutine
  13357. ; -----------------------
  13358. ; This routine just loads the DE register with the contents of the two
  13359. ; locations following the location addressed by DE.
  13360. ; It is used to step along the 16-bit dimension sizes in array definitions.
  13361. ; Note. Such code is made into subroutines to make programs easier to
  13362. ; write and it would use less space to include the five instructions in-line.
  13363. ; However, there are so many exchanges going on at the places this is invoked
  13364. ; that to implement it in-line would make the code hard to follow.
  13365. ; It probably had a zippier label though as the intention is to simplify the
  13366. ; program.
  13367.  
  13368. ;; DE,(DE+1)
  13369. L2AEE:  EX      DE,HL           ;
  13370.         INC     HL              ;
  13371.         LD      E,(HL)          ;
  13372.         INC     HL              ;
  13373.         LD      D,(HL)          ;
  13374.         RET                     ;
  13375.  
  13376. ; -------------------
  13377. ; HL=HL*DE Subroutine
  13378. ; -------------------
  13379. ; This routine calls the mathematical routine to multiply HL by DE in runtime.
  13380. ; It is called from STK-VAR and from DIM. In the latter case syntax is not
  13381. ; being checked so the entry point could have been at the second CALL
  13382. ; instruction to save a few clock-cycles.
  13383.  
  13384. ;; GET-HL*DE
  13385. L2AF4:  CALL    L2530           ; routine SYNTAX-Z.
  13386.         RET     Z               ; return if checking syntax.
  13387.  
  13388.         CALL    L30A9           ; routine HL-HL*DE.
  13389.         JP      C,L1F15         ; jump back to REPORT-4 if over 65535.
  13390.  
  13391.         RET                     ; else return with 16-bit result in HL.
  13392.  
  13393. ; -----------------
  13394. ; THE 'LET' COMMAND
  13395. ; -----------------
  13396. ; Sinclair BASIC adheres to the ANSI-78 standard and a LET is required in
  13397. ; assignments e.g. LET a = 1  :   LET h$ = "hat".
  13398. ;
  13399. ; Long names may contain spaces but not colour controls (when assigned).
  13400. ; a substring can appear to the left of the equals sign.
  13401.  
  13402. ; An earlier mathematician Lewis Carroll may have been pleased that
  13403. ; 10 LET Babies cannot manage crocodiles = Babies are illogical AND
  13404. ;    Nobody is despised who can manage a crocodile AND Illogical persons
  13405. ;    are despised
  13406. ; does not give the 'Nonsense..' error if the three variables exist.
  13407. ; I digress.
  13408.  
  13409. ;; LET
  13410. L2AFF:  LD      HL,($5C4D)      ; fetch system variable DEST to HL.
  13411.         BIT     1,(IY+$37)      ; test FLAGX - handling a new variable ?
  13412.         JR      Z,L2B66         ; forward to L-EXISTS if not.
  13413.  
  13414. ; continue for a new variable. DEST points to start in BASIC line.
  13415. ; from the CLASS routines.
  13416.  
  13417.         LD      BC,$0005        ; assume numeric and assign an initial 5 bytes
  13418.  
  13419. ;; L-EACH-CH
  13420. L2B0B:  INC     BC              ; increase byte count for each relevant
  13421.                                 ; character
  13422.  
  13423. ;; L-NO-SP
  13424. L2B0C:  INC     HL              ; increase pointer.
  13425.         LD      A,(HL)          ; fetch character.
  13426.         CP      $20             ; is it a space ?
  13427.         JR      Z,L2B0C         ; back to L-NO-SP is so.
  13428.  
  13429.         JR      NC,L2B1F        ; forward to L-TEST-CH if higher.
  13430.  
  13431.         CP      $10             ; is it $00 - $0F ?
  13432.         JR      C,L2B29         ; forward to L-SPACES if so.
  13433.  
  13434.         CP      $16             ; is it $16 - $1F ?
  13435.         JR      NC,L2B29        ; forward to L-SPACES if so.
  13436.  
  13437. ; it was $10 - $15  so step over a colour code.
  13438.  
  13439.         INC     HL              ; increase pointer.
  13440.         JR      L2B0C           ; loop back to L-NO-SP.
  13441.  
  13442. ; ---
  13443.  
  13444. ; the branch was to here if higher than space.
  13445.  
  13446. ;; L-TEST-CH
  13447. L2B1F:  CALL    L2C88           ; routine ALPHANUM sets carry if alphanumeric
  13448.         JR      C,L2B0B         ; loop back to L-EACH-CH for more if so.
  13449.  
  13450.         CP      $24             ; is it '$' ?
  13451.         JP      Z,L2BC0         ; jump forward if so, to L-NEW$
  13452.                                 ; with a new string.
  13453.  
  13454. ;; L-SPACES
  13455. L2B29:  LD      A,C             ; save length lo in A.
  13456.         LD      HL,($5C59)      ; fetch E_LINE to HL.
  13457.         DEC     HL              ; point to location before, the variables
  13458.                                 ; end-marker.
  13459.         CALL    L1655           ; routine MAKE-ROOM creates BC spaces
  13460.                                 ; for name and numeric value.
  13461.         INC     HL              ; advance to first new location.
  13462.         INC     HL              ; then to second.
  13463.         EX      DE,HL           ; set DE to second location.
  13464.         PUSH    DE              ; save this pointer.
  13465.         LD      HL,($5C4D)      ; reload HL with DEST.
  13466.         DEC     DE              ; point to first.
  13467.         SUB     $06             ; subtract six from length_lo.
  13468.         LD      B,A             ; save count in B.
  13469.         JR      Z,L2B4F         ; forward to L-SINGLE if it was just
  13470.                                 ; one character.
  13471.  
  13472. ; HL points to start of variable name after 'LET' in BASIC line.
  13473.  
  13474. ;; L-CHAR
  13475. L2B3E:  INC     HL              ; increase pointer.
  13476.         LD      A,(HL)          ; pick up character.
  13477.         CP      $21             ; is it space or higher ?
  13478.         JR      C,L2B3E         ; back to L-CHAR with space and less.
  13479.  
  13480.         OR      $20             ; make variable lower-case.
  13481.         INC     DE              ; increase destination pointer.
  13482.         LD      (DE),A          ; and load to edit line.
  13483.         DJNZ    L2B3E           ; loop back to L-CHAR until B is zero.
  13484.  
  13485.         OR      $80             ; invert the last character.
  13486.         LD      (DE),A          ; and overwrite that in edit line.
  13487.  
  13488. ; now consider first character which has bit 6 set
  13489.  
  13490.         LD      A,$C0           ; set A 11000000 is xor mask for a long name.
  13491.                                 ; %101      is xor/or  result
  13492.  
  13493. ; single character numerics rejoin here with %00000000 in mask.
  13494. ;                                            %011      will be xor/or result
  13495.  
  13496. ;; L-SINGLE
  13497. L2B4F:  LD      HL,($5C4D)      ; fetch DEST - HL addresses first character.
  13498.         XOR     (HL)            ; apply variable type indicator mask (above).
  13499.         OR      $20             ; make lowercase - set bit 5.
  13500.         POP     HL              ; restore pointer to 2nd character.
  13501.         CALL    L2BEA           ; routine L-FIRST puts A in first character.
  13502.                                 ; and returns with HL holding
  13503.                                 ; new E_LINE-1  the $80 vars end-marker.
  13504.  
  13505. ;; L-NUMERIC
  13506. L2B59:  PUSH    HL              ; save the pointer.
  13507.  
  13508. ; the value of variable is deleted but remains after calculator stack.
  13509.  
  13510.         RST     28H             ;; FP-CALC
  13511.         DB    $02             ;;delete      ; delete variable value
  13512.         DB    $38             ;;end-calc
  13513.  
  13514. ; DE (STKEND) points to start of value.
  13515.  
  13516.         POP     HL              ; restore the pointer.
  13517.         LD      BC,$0005        ; start of number is five bytes before.
  13518.         AND     A               ; prepare for true subtraction.
  13519.         SBC     HL,BC           ; HL points to start of value.
  13520.         JR      L2BA6           ; forward to L-ENTER  ==>
  13521.  
  13522. ; ---
  13523.  
  13524.  
  13525. ; the jump was to here if the variable already existed.
  13526.  
  13527. ;; L-EXISTS
  13528. L2B66:  BIT     6,(IY+$01)      ; test FLAGS - numeric or string result ?
  13529.         JR      Z,L2B72         ; skip forward to L-DELETE$   -*->
  13530.                                 ; if string result.
  13531.  
  13532. ; A numeric variable could be simple or an array element.
  13533. ; They are treated the same and the old value is overwritten.
  13534.  
  13535.         LD      DE,$0006        ; six bytes forward points to loc past value.
  13536.         ADD     HL,DE           ; add to start of number.
  13537.         JR      L2B59           ; back to L-NUMERIC to overwrite value.
  13538.  
  13539. ; ---
  13540.  
  13541. ; -*-> the branch was here if a string existed.
  13542.  
  13543. ;; L-DELETE$
  13544. L2B72:  LD      HL,($5C4D)      ; fetch DEST to HL.
  13545.                                 ; (still set from first instruction)
  13546.         LD      BC,($5C72)      ; fetch STRLEN to BC.
  13547.         BIT     0,(IY+$37)      ; test FLAGX - handling a complete simple
  13548.                                 ; string ?
  13549.         JR      NZ,L2BAF        ; forward to L-ADD$ if so.
  13550.  
  13551. ; must be a string array or a slice in workspace.
  13552. ; Note. LET a$(3 TO 6) = h$   will assign "hat " if h$ = "hat"
  13553. ;                                  and    "hats" if h$ = "hatstand".
  13554. ;
  13555. ; This is known as Procrustian lengthening and shortening after a
  13556. ; character Procrustes in Greek legend who made travellers sleep in his bed,
  13557. ; cutting off their feet or stretching them so they fitted the bed perfectly.
  13558. ; The bloke was hatstand and slain by Theseus.
  13559.  
  13560.         LD      A,B             ; test if length
  13561.         OR      C               ; is zero and
  13562.         RET     Z               ; return if so.
  13563.  
  13564.         PUSH    HL              ; save pointer to start.
  13565.  
  13566.         RST     30H             ; BC-SPACES creates room.
  13567.         PUSH    DE              ; save pointer to first new location.
  13568.         PUSH    BC              ; and length            (*)
  13569.         LD      D,H             ; set DE to point to last location.
  13570.         LD      E,L             ;
  13571.         INC     HL              ; set HL to next location.
  13572.         LD      (HL),$20        ; place a space there.
  13573.         LDDR                    ; copy bytes filling with spaces.
  13574.  
  13575.         PUSH    HL              ; save pointer to start.
  13576.         CALL    L2BF1           ; routine STK-FETCH start to DE,
  13577.                                 ; length to BC.
  13578.         POP     HL              ; restore the pointer.
  13579.         EX      (SP),HL         ; (*) length to HL, pointer to stack.
  13580.         AND     A               ; prepare for true subtraction.
  13581.         SBC     HL,BC           ; subtract old length from new.
  13582.         ADD     HL,BC           ; and add back.
  13583.         JR      NC,L2B9B        ; forward if it fits to L-LENGTH.
  13584.  
  13585.         LD      B,H             ; otherwise set
  13586.         LD      C,L             ; length to old length.
  13587.                                 ; "hatstand" becomes "hats"
  13588.  
  13589. ;; L-LENGTH
  13590. L2B9B:  EX      (SP),HL         ; (*) length to stack, pointer to HL.
  13591.         EX      DE,HL           ; pointer to DE, start of string to HL.
  13592.         LD      A,B             ; is the length zero ?
  13593.         OR      C               ;
  13594.         JR      Z,L2BA3         ; forward to L-IN-W/S if so
  13595.                                 ; leaving prepared spaces.
  13596.  
  13597.         LDIR                    ; else copy bytes overwriting some spaces.
  13598.  
  13599. ;; L-IN-W/S
  13600. L2BA3:  POP     BC              ; pop the new length.  (*)
  13601.         POP     DE              ; pop pointer to new area.
  13602.         POP     HL              ; pop pointer to variable in assignment.
  13603.                                 ; and continue copying from workspace
  13604.                                 ; to variables area.
  13605.  
  13606. ; ==> branch here from  L-NUMERIC
  13607.  
  13608. ;; L-ENTER
  13609. L2BA6:  EX      DE,HL           ; exchange pointers HL=STKEND DE=end of vars.
  13610.         LD      A,B             ; test the length
  13611.         OR      C               ; and make a
  13612.         RET     Z               ; return if zero (strings only).
  13613.  
  13614.         PUSH    DE              ; save start of destination.
  13615.         LDIR                    ; copy bytes.
  13616.         POP     HL              ; address the start.
  13617.         RET                     ; and return.
  13618.  
  13619. ; ---
  13620.  
  13621. ; the branch was here from L-DELETE$ if an existing simple string.
  13622. ; register HL addresses start of string in variables area.
  13623.  
  13624. ;; L-ADD$
  13625. L2BAF:  DEC     HL              ; point to high byte of length.
  13626.         DEC     HL              ; to low byte.
  13627.         DEC     HL              ; to letter.
  13628.         LD      A,(HL)          ; fetch masked letter to A.
  13629.         PUSH    HL              ; save the pointer on stack.
  13630.         PUSH    BC              ; save new length.
  13631.         CALL    L2BC6           ; routine L-STRING adds new string at end
  13632.                                 ; of variables area.
  13633.                                 ; if no room we still have old one.
  13634.         POP     BC              ; restore length.
  13635.         POP     HL              ; restore start.
  13636.         INC     BC              ; increase
  13637.         INC     BC              ; length by three
  13638.         INC     BC              ; to include character and length bytes.
  13639.         JP      L19E8           ; jump to indirect exit via RECLAIM-2
  13640.                                 ; deleting old version and adjusting pointers.
  13641.  
  13642. ; ---
  13643.  
  13644. ; the jump was here with a new string variable.
  13645.  
  13646. ;; L-NEW$
  13647. L2BC0:  LD      A,$DF           ; indicator mask %11011111 for
  13648.                                 ;                %010xxxxx will be result
  13649.         LD      HL,($5C4D)      ; address DEST first character.
  13650.         AND     (HL)            ; combine mask with character.
  13651.  
  13652. ;; L-STRING
  13653. L2BC6:  PUSH    AF              ; save first character and mask.
  13654.         CALL    L2BF1           ; routine STK-FETCH fetches parameters of
  13655.                                 ; the string.
  13656.         EX      DE,HL           ; transfer start to HL.
  13657.         ADD     HL,BC           ; add to length.
  13658.         PUSH    BC              ; save the length.
  13659.         DEC     HL              ; point to end of string.
  13660.         LD      ($5C4D),HL      ; save pointer in DEST.
  13661.                                 ; (updated by POINTERS if in workspace)
  13662.         INC     BC              ; extra byte for letter.
  13663.         INC     BC              ; two bytes
  13664.         INC     BC              ; for the length of string.
  13665.         LD      HL,($5C59)      ; address E_LINE.
  13666.         DEC     HL              ; now end of VARS area.
  13667.         CALL    L1655           ; routine MAKE-ROOM makes room for string.
  13668.                                 ; updating pointers including DEST.
  13669.         LD      HL,($5C4D)      ; pick up pointer to end of string from DEST.
  13670.         POP     BC              ; restore length from stack.
  13671.         PUSH    BC              ; and save again on stack.
  13672.         INC     BC              ; add a byte.
  13673.         LDDR                    ; copy bytes from end to start.
  13674.         EX      DE,HL           ; HL addresses length low
  13675.         INC     HL              ; increase to address high byte
  13676.         POP     BC              ; restore length to BC
  13677.         LD      (HL),B          ; insert high byte
  13678.         DEC     HL              ; address low byte location
  13679.         LD      (HL),C          ; insert that byte
  13680.         POP     AF              ; restore character and mask
  13681.  
  13682. ;; L-FIRST
  13683. L2BEA:  DEC     HL              ; address variable name
  13684.         LD      (HL),A          ; and insert character.
  13685.         LD      HL,($5C59)      ; load HL with E_LINE.
  13686.         DEC     HL              ; now end of VARS area.
  13687.         RET                     ; return
  13688.  
  13689. ; ------------------------------------
  13690. ; Get last value from calculator stack
  13691. ; ------------------------------------
  13692. ;
  13693. ;
  13694.  
  13695. ;; STK-FETCH
  13696. L2BF1:  LD      HL,($5C65)      ; STKEND
  13697.         DEC     HL              ;
  13698.         LD      B,(HL)          ;
  13699.         DEC     HL              ;
  13700.         LD      C,(HL)          ;
  13701.         DEC     HL              ;
  13702.         LD      D,(HL)          ;
  13703.         DEC     HL              ;
  13704.         LD      E,(HL)          ;
  13705.         DEC     HL              ;
  13706.         LD      A,(HL)          ;
  13707.         LD      ($5C65),HL      ; STKEND
  13708.         RET                     ;
  13709.  
  13710. ; ------------------
  13711. ; Handle DIM command
  13712. ; ------------------
  13713. ; e.g. DIM a(2,3,4,7): DIM a$(32) : DIM b$(300,2,768) : DIM c$(20000)
  13714. ; the only limit to dimensions is memory so, for example,
  13715. ; DIM a(2,2,2,2,2,2,2,2,2,2,2,2,2) is possible and creates a multi-
  13716. ; dimensional array of zeros. String arrays are initialized to spaces.
  13717. ; It is not possible to erase an array, but it can be re-dimensioned to
  13718. ; a minimal size of 1, after use, to free up memory.
  13719.  
  13720. ;; DIM
  13721. L2C02:  CALL    L28B2           ; routine LOOK-VARS
  13722.  
  13723. ;; D-RPORT-C
  13724. L2C05:  JP      NZ,L1C8A        ; jump to REPORT-C if a long-name variable.
  13725.                                 ; DIM lottery numbers(49) doesn't work.
  13726.  
  13727.         CALL    L2530           ; routine SYNTAX-Z
  13728.         JR      NZ,L2C15        ; forward to D-RUN in runtime.
  13729.  
  13730.         RES     6,C             ; signal 'numeric' array even if string as
  13731.                                 ; this simplifies the syntax checking.
  13732.  
  13733.         CALL    L2996           ; routine STK-VAR checks syntax.
  13734.         CALL    L1BEE           ; routine CHECK-END performs early exit ->
  13735.  
  13736. ; the branch was here in runtime.
  13737.  
  13738. ;; D-RUN
  13739. L2C15:  JR      C,L2C1F         ; skip to D-LETTER if variable did not exist.
  13740.                                 ; else reclaim the old one.
  13741.  
  13742.         PUSH    BC              ; save type in C.
  13743.         CALL    L19B8           ; routine NEXT-ONE find following variable
  13744.                                 ; or position of $80 end-marker.
  13745.         CALL    L19E8           ; routine RECLAIM-2 reclaims the
  13746.                                 ; space between.
  13747.         POP     BC              ; pop the type.
  13748.  
  13749. ;; D-LETTER
  13750. L2C1F:  SET     7,C             ; signal array.
  13751.         LD      B,$00           ; initialize dimensions to zero and
  13752.         PUSH    BC              ; save with the type.
  13753.         LD      HL,$0001        ; make elements one character presuming string
  13754.         BIT     6,C             ; is it a string ?
  13755.         JR      NZ,L2C2D        ; forward to D-SIZE if so.
  13756.  
  13757.         LD      L,$05           ; make elements 5 bytes as is numeric.
  13758.  
  13759. ;; D-SIZE
  13760. L2C2D:  EX      DE,HL           ; save the element size in DE.
  13761.  
  13762. ; now enter a loop to parse each of the integers in the list.
  13763.  
  13764. ;; D-NO-LOOP
  13765. L2C2E:  RST     20H             ; NEXT-CHAR
  13766.         LD      H,$FF           ; disable limit check by setting HL high
  13767.         CALL    L2ACC           ; routine INT-EXP1
  13768.         JP      C,L2A20         ; to REPORT-3 if > 65280 and then some
  13769.                                 ; 'Subscript out of range'
  13770.  
  13771.         POP     HL              ; pop dimension counter, array type
  13772.         PUSH    BC              ; save dimension size                     ***
  13773.         INC     H               ; increment the dimension counter
  13774.         PUSH    HL              ; save the dimension counter
  13775.         LD      H,B             ; transfer size
  13776.         LD      L,C             ; to HL
  13777.         CALL    L2AF4           ; routine GET-HL*DE multiplies dimension by
  13778.                                 ; running total of size required initially
  13779.                                 ; 1 or 5.
  13780.         EX      DE,HL           ; save running total in DE
  13781.  
  13782.         RST     18H             ; GET-CHAR
  13783.         CP      $2C             ; is it ',' ?
  13784.         JR      Z,L2C2E         ; loop back to D-NO-LOOP until all dimensions
  13785.                                 ; have been considered
  13786.  
  13787. ; when loop complete continue.
  13788.  
  13789.         CP      $29             ; is it ')' ?
  13790.         JR      NZ,L2C05        ; to D-RPORT-C with anything else
  13791.                                 ; 'Nonsense in BASIC'
  13792.  
  13793.  
  13794.         RST     20H             ; NEXT-CHAR advances to next statement/CR
  13795.  
  13796.         POP     BC              ; pop dimension counter/type
  13797.         LD      A,C             ; type to A
  13798.  
  13799. ; now calculate space required for array variable
  13800.  
  13801.         LD      L,B             ; dimensions to L since these require 16 bits
  13802.                                 ; then this value will be doubled
  13803.         LD      H,$00           ; set high byte to zero
  13804.  
  13805. ; another four bytes are required for letter(1), total length(2), number of
  13806. ; dimensions(1) but since we have yet to double allow for two
  13807.  
  13808.         INC     HL              ; increment
  13809.         INC     HL              ; increment
  13810.  
  13811.         ADD     HL,HL           ; now double giving 4 + dimensions * 2
  13812.  
  13813.         ADD     HL,DE           ; add to space required for array contents
  13814.  
  13815.         JP      C,L1F15         ; to REPORT-4 if > 65535
  13816.                                 ; 'Out of memory'
  13817.  
  13818.         PUSH    DE              ; save data space
  13819.         PUSH    BC              ; save dimensions/type
  13820.         PUSH    HL              ; save total space
  13821.         LD      B,H             ; total space
  13822.         LD      C,L             ; to BC
  13823.         LD      HL,($5C59)      ; address E_LINE - first location after
  13824.                                 ; variables area
  13825.         DEC     HL              ; point to location before - the $80 end-marker
  13826.         CALL    L1655           ; routine MAKE-ROOM creates the space if
  13827.                                 ; memory is available.
  13828.  
  13829.         INC     HL              ; point to first new location and
  13830.         LD      (HL),A          ; store letter/type
  13831.  
  13832.         POP     BC              ; pop total space
  13833.         DEC     BC              ; exclude name
  13834.         DEC     BC              ; exclude the 16-bit
  13835.         DEC     BC              ; counter itself
  13836.         INC     HL              ; point to next location the 16-bit counter
  13837.         LD      (HL),C          ; insert low byte
  13838.         INC     HL              ; address next
  13839.         LD      (HL),B          ; insert high byte
  13840.  
  13841.         POP     BC              ; pop the number of dimensions.
  13842.         LD      A,B             ; dimensions to A
  13843.         INC     HL              ; address next
  13844.         LD      (HL),A          ; and insert "No. of dims"
  13845.  
  13846.         LD      H,D             ; transfer DE space + 1 from make-room
  13847.         LD      L,E             ; to HL
  13848.         DEC     DE              ; set DE to next location down.
  13849.         LD      (HL),$00        ; presume numeric and insert a zero
  13850.         BIT     6,C             ; test bit 6 of C. numeric or string ?
  13851.         JR      Z,L2C7C         ; skip to DIM-CLEAR if numeric
  13852.  
  13853.         LD      (HL),$20        ; place a space character in HL
  13854.  
  13855. ;; DIM-CLEAR
  13856. L2C7C:  POP     BC              ; pop the data length
  13857.  
  13858.         LDDR                    ; LDDR sets to zeros or spaces
  13859.  
  13860. ; The number of dimensions is still in A.
  13861. ; A loop is now entered to insert the size of each dimension that was pushed
  13862. ; during the D-NO-LOOP working downwards from position before start of data.
  13863.  
  13864. ;; DIM-SIZES
  13865. L2C7F:  POP     BC              ; pop a dimension size                    ***
  13866.         LD      (HL),B          ; insert high byte at position
  13867.         DEC     HL              ; next location down
  13868.         LD      (HL),C          ; insert low byte
  13869.         DEC     HL              ; next location down
  13870.         DEC     A               ; decrement dimension counter
  13871.         JR      NZ,L2C7F        ; back to DIM-SIZES until all done.
  13872.  
  13873.         RET                     ; return.
  13874.  
  13875. ; -----------------------------
  13876. ; Check whether digit or letter
  13877. ; -----------------------------
  13878. ; This routine checks that the character in A is alphanumeric
  13879. ; returning with carry set if so.
  13880.  
  13881. ;; ALPHANUM
  13882. L2C88:  CALL    L2D1B           ; routine NUMERIC will reset carry if so.
  13883.         CCF                     ; Complement Carry Flag
  13884.         RET     C               ; Return if numeric else continue into
  13885.                                 ; next routine.
  13886.  
  13887. ; This routine checks that the character in A is alphabetic
  13888.  
  13889. ;; ALPHA
  13890. L2C8D:  CP      $41             ; less than 'A' ?
  13891.         CCF                     ; Complement Carry Flag
  13892.         RET     NC              ; return if so
  13893.  
  13894.         CP      $5B             ; less than 'Z'+1 ?
  13895.         RET     C               ; is within first range
  13896.  
  13897.         CP      $61             ; less than 'a' ?
  13898.         CCF                     ; Complement Carry Flag
  13899.         RET     NC              ; return if so.
  13900.  
  13901.         CP      $7B             ; less than 'z'+1 ?
  13902.         RET                     ; carry set if within a-z.
  13903.  
  13904. ; -------------------------
  13905. ; Decimal to floating point
  13906. ; -------------------------
  13907. ; This routine finds the floating point number represented by an expression
  13908. ; beginning with BIN, '.' or a digit.
  13909. ; Note that BIN need not have any '0's or '1's after it.
  13910. ; BIN is really just a notational symbol and not a function.
  13911.  
  13912. ;; DEC-TO-FP
  13913. L2C9B:  CP      $C4             ; 'BIN' token ?
  13914.         JR      NZ,L2CB8        ; to NOT-BIN if not
  13915.  
  13916.         LD      DE,$0000        ; initialize 16 bit buffer register.
  13917.  
  13918. ;; BIN-DIGIT
  13919. L2CA2:  RST     20H             ; NEXT-CHAR
  13920.         SUB     $31             ; '1'
  13921.         ADC     A,$00           ; will be zero if '1' or '0'
  13922.                                 ; carry will be set if was '0'
  13923.         JR      NZ,L2CB3        ; forward to BIN-END if result not zero
  13924.  
  13925.         EX      DE,HL           ; buffer to HL
  13926.         CCF                     ; Carry now set if originally '1'
  13927.         ADC     HL,HL           ; shift the carry into HL
  13928.         JP      C,L31AD         ; to REPORT-6 if overflow - too many digits
  13929.                                 ; after first '1'. There can be an unlimited
  13930.                                 ; number of leading zeros.
  13931.                                 ; 'Number too big' - raise an error
  13932.  
  13933.         EX      DE,HL           ; save the buffer
  13934.         JR      L2CA2           ; back to BIN-DIGIT for more digits
  13935.  
  13936. ; ---
  13937.  
  13938. ;; BIN-END
  13939. L2CB3:  LD      B,D             ; transfer 16 bit buffer
  13940.         LD      C,E             ; to BC register pair.
  13941.         JP      L2D2B           ; JUMP to STACK-BC to put on calculator stack
  13942.  
  13943. ; ---
  13944.  
  13945. ; continue here with .1,  42, 3.14, 5., 2.3 E -4
  13946.  
  13947. ;; NOT-BIN
  13948. L2CB8:  CP      $2E             ; '.' - leading decimal point ?
  13949.         JR      Z,L2CCB         ; skip to DECIMAL if so.
  13950.  
  13951.         CALL    L2D3B           ; routine INT-TO-FP to evaluate all digits
  13952.                                 ; This number 'x' is placed on stack.
  13953.         CP      $2E             ; '.' - mid decimal point ?
  13954.  
  13955.         JR      NZ,L2CEB        ; to E-FORMAT if not to consider that format
  13956.  
  13957.         RST     20H             ; NEXT-CHAR
  13958.         CALL    L2D1B           ; routine NUMERIC returns carry reset if 0-9
  13959.  
  13960.         JR      C,L2CEB         ; to E-FORMAT if not a digit e.g. '1.'
  13961.  
  13962.         JR      L2CD5           ; to DEC-STO-1 to add the decimal part to 'x'
  13963.  
  13964. ; ---
  13965.  
  13966. ; a leading decimal point has been found in a number.
  13967.  
  13968. ;; DECIMAL
  13969. L2CCB:  RST     20H             ; NEXT-CHAR
  13970.         CALL    L2D1B           ; routine NUMERIC will reset carry if digit
  13971.  
  13972. ;; DEC-RPT-C
  13973. L2CCF:  JP      C,L1C8A         ; to REPORT-C if just a '.'
  13974.                                 ; raise 'Nonsense in BASIC'
  13975.  
  13976. ; since there is no leading zero put one on the calculator stack.
  13977.  
  13978.         RST     28H             ;; FP-CALC
  13979.         DB    $A0             ;;stk-zero  ; 0.
  13980.         DB    $38             ;;end-calc
  13981.  
  13982. ; If rejoining from earlier there will be a value 'x' on stack.
  13983. ; If continuing from above the value zero.
  13984. ; Now store 1 in mem-0.
  13985. ; Note. At each pass of the digit loop this will be divided by ten.
  13986.  
  13987. ;; DEC-STO-1
  13988. L2CD5:  RST     28H             ;; FP-CALC
  13989.         DB    $A1             ;;stk-one   ;x or 0,1.
  13990.         DB    $C0             ;;st-mem-0  ;x or 0,1.
  13991.         DB    $02             ;;delete    ;x or 0.
  13992.         DB    $38             ;;end-calc
  13993.  
  13994.  
  13995. ;; NXT-DGT-1
  13996. L2CDA:  RST     18H             ; GET-CHAR
  13997.         CALL    L2D22           ; routine STK-DIGIT stacks single digit 'd'
  13998.         JR      C,L2CEB         ; exit to E-FORMAT when digits exhausted  >
  13999.  
  14000.  
  14001.         RST     28H             ;; FP-CALC   ;x or 0,d.           first pass.
  14002.         DB    $E0             ;;get-mem-0  ;x or 0,d,1.
  14003.         DB    $A4             ;;stk-ten    ;x or 0,d,1,10.
  14004.         DB    $05             ;;division   ;x or 0,d,1/10.
  14005.         DB    $C0             ;;st-mem-0   ;x or 0,d,1/10.
  14006.         DB    $04             ;;multiply   ;x or 0,d/10.
  14007.         DB    $0F             ;;addition   ;x or 0 + d/10.
  14008.         DB    $38             ;;end-calc   last value.
  14009.  
  14010.         RST     20H             ; NEXT-CHAR  moves to next character
  14011.         JR      L2CDA           ; back to NXT-DGT-1
  14012.  
  14013. ; ---
  14014.  
  14015. ; although only the first pass is shown it can be seen that at each pass
  14016. ; the new less significant digit is multiplied by an increasingly smaller
  14017. ; factor (1/100, 1/1000, 1/10000 ... ) before being added to the previous
  14018. ; last value to form a new last value.
  14019.  
  14020. ; Finally see if an exponent has been input.
  14021.  
  14022. ;; E-FORMAT
  14023. L2CEB:  CP      $45             ; is character 'E' ?
  14024.         JR      Z,L2CF2         ; to SIGN-FLAG if so
  14025.  
  14026.         CP      $65             ; 'e' is acceptable as well.
  14027.         RET     NZ              ; return as no exponent.
  14028.  
  14029. ;; SIGN-FLAG
  14030. L2CF2:  LD      B,$FF           ; initialize temporary sign byte to $FF
  14031.  
  14032.         RST     20H             ; NEXT-CHAR
  14033.         CP      $2B             ; is character '+' ?
  14034.         JR      Z,L2CFE         ; to SIGN-DONE
  14035.  
  14036.         CP      $2D             ; is character '-' ?
  14037.         JR      NZ,L2CFF        ; to ST-E-PART as no sign
  14038.  
  14039.         INC     B               ; set sign to zero
  14040.  
  14041. ; now consider digits of exponent.
  14042. ; Note. incidentally this is the only occasion in Spectrum BASIC when an
  14043. ; expression may not be used when a number is expected.
  14044.  
  14045. ;; SIGN-DONE
  14046. L2CFE:  RST     20H             ; NEXT-CHAR
  14047.  
  14048. ;; ST-E-PART
  14049. L2CFF:  CALL    L2D1B           ; routine NUMERIC
  14050.         JR      C,L2CCF         ; to DEC-RPT-C if not
  14051.                                 ; raise 'Nonsense in BASIC'.
  14052.  
  14053.         PUSH    BC              ; save sign (in B)
  14054.         CALL    L2D3B           ; routine INT-TO-FP places exponent on stack
  14055.         CALL    L2DD5           ; routine FP-TO-A  transfers it to A
  14056.         POP     BC              ; restore sign
  14057.         JP      C,L31AD         ; to REPORT-6 if overflow (over 255)
  14058.                                 ; raise 'Number too big'.
  14059.  
  14060.         AND     A               ; set flags
  14061.         JP      M,L31AD         ; to REPORT-6 if over '127'.
  14062.                                 ; raise 'Number too big'.
  14063.                                 ; 127 is still way too high and it is
  14064.                                 ; impossible to enter an exponent greater
  14065.                                 ; than 39 from the keyboard. The error gets
  14066.                                 ; raised later in E-TO-FP so two different
  14067.                                 ; error messages depending how high A is.
  14068.  
  14069.         INC     B               ; $FF to $00 or $00 to $01 - expendable now.
  14070.         JR      Z,L2D18         ; forward to E-FP-JUMP if exponent positive
  14071.  
  14072.         NEG                     ; Negate the exponent.
  14073.  
  14074. ;; E-FP-JUMP
  14075. L2D18:  JP      L2D4F           ; JUMP forward to E-TO-FP to assign to
  14076.                                 ; last value x on stack x * 10 to power A
  14077.                                 ; a relative jump would have done.
  14078.  
  14079. ; ---------------------
  14080. ; Check for valid digit
  14081. ; ---------------------
  14082. ; This routine checks that the ASCII character in A is numeric
  14083. ; returning with carry reset if so.
  14084.  
  14085. ;; NUMERIC
  14086. L2D1B:  CP      $30             ; '0'
  14087.         RET     C               ; return if less than zero character.
  14088.  
  14089.         CP      $3A             ; The upper test is '9'
  14090.         CCF                     ; Complement Carry Flag
  14091.         RET                     ; Return - carry clear if character '0' - '9'
  14092.  
  14093. ; -----------
  14094. ; Stack Digit
  14095. ; -----------
  14096. ; This subroutine is called from INT-TO-FP and DEC-TO-FP to stack a digit
  14097. ; on the calculator stack.
  14098.  
  14099. ;; STK-DIGIT
  14100. L2D22:  CALL    L2D1B           ; routine NUMERIC
  14101.         RET     C               ; return if not numeric character
  14102.  
  14103.         SUB     $30             ; convert from ASCII to digit
  14104.  
  14105. ; -----------------
  14106. ; Stack accumulator
  14107. ; -----------------
  14108. ;
  14109. ;
  14110.  
  14111. ;; STACK-A
  14112. L2D28:  LD      C,A             ; transfer to C
  14113.         LD      B,$00           ; and make B zero
  14114.  
  14115. ; ----------------------
  14116. ; Stack BC register pair
  14117. ; ----------------------
  14118. ;
  14119.  
  14120. ;; STACK-BC
  14121. L2D2B:  LD      IY,$5C3A        ; re-initialize ERR_NR
  14122.  
  14123.         XOR     A               ; clear to signal small integer
  14124.         LD      E,A             ; place in E for sign
  14125.         LD      D,C             ; LSB to D
  14126.         LD      C,B             ; MSB to C
  14127.         LD      B,A             ; last byte not used
  14128.         CALL    L2AB6           ; routine STK-STORE
  14129.  
  14130.         RST     28H             ;; FP-CALC
  14131.         DB    $38             ;;end-calc  make HL = STKEND-5
  14132.  
  14133.         AND     A               ; clear carry
  14134.         RET                     ; before returning
  14135.  
  14136. ; -------------------------
  14137. ; Integer to floating point
  14138. ; -------------------------
  14139. ; This routine places one or more digits found in a BASIC line
  14140. ; on the calculator stack multiplying the previous value by ten each time
  14141. ; before adding in the new digit to form a last value on calculator stack.
  14142.  
  14143. ;; INT-TO-FP
  14144. L2D3B:  PUSH    AF              ; save first character
  14145.  
  14146.         RST     28H             ;; FP-CALC
  14147.         DB    $A0             ;;stk-zero    ; v=0. initial value
  14148.         DB    $38             ;;end-calc
  14149.  
  14150.         POP     AF              ; fetch first character back.
  14151.  
  14152. ;; NXT-DGT-2
  14153. L2D40:  CALL    L2D22           ; routine STK-DIGIT puts 0-9 on stack
  14154.         RET     C               ; will return when character is not numeric >
  14155.  
  14156.         RST     28H             ;; FP-CALC    ; v, d.
  14157.         DB    $01             ;;exchange    ; d, v.
  14158.         DB    $A4             ;;stk-ten     ; d, v, 10.
  14159.         DB    $04             ;;multiply    ; d, v*10.
  14160.         DB    $0F             ;;addition    ; d + v*10 = newvalue
  14161.         DB    $38             ;;end-calc    ; v.
  14162.  
  14163.         CALL    L0074           ; routine CH-ADD+1 get next character
  14164.         JR      L2D40           ; back to NXT-DGT-2 to process as a digit
  14165.  
  14166.  
  14167. ;*********************************
  14168. ;** Part 9. ARITHMETIC ROUTINES **
  14169. ;*********************************
  14170.  
  14171. ; --------------------------
  14172. ; E-format to floating point
  14173. ; --------------------------
  14174. ; This subroutine is used by the PRINT-FP routine and the decimal to FP
  14175. ; routines to stack a number expressed in exponent format.
  14176. ; Note. Though not used by the ROM as such, it has also been set up as
  14177. ; a unary calculator literal but this will not work as the accumulator
  14178. ; is not available from within the calculator.
  14179.  
  14180. ; on entry there is a value x on the calculator stack and an exponent of ten
  14181. ; in A.    The required value is x + 10 ^ A
  14182.  
  14183. ;; e-to-fp
  14184. ;; E-TO-FP
  14185. L2D4F:  RLCA                    ; this will set the          x.
  14186.         RRCA                    ; carry if bit 7 is set
  14187.  
  14188.         JR      NC,L2D55        ; to E-SAVE  if positive.
  14189.  
  14190.         CPL                     ; make negative positive
  14191.         INC     A               ; without altering carry.
  14192.  
  14193. ;; E-SAVE
  14194. L2D55:  PUSH    AF              ; save positive exp and sign in carry
  14195.  
  14196.         LD      HL,$5C92        ; address MEM-0
  14197.  
  14198.         CALL    L350B           ; routine FP-0/1
  14199.                                 ; places an integer zero, if no carry,
  14200.                                 ; else a one in mem-0 as a sign flag
  14201.  
  14202.         RST     28H             ;; FP-CALC
  14203.         DB    $A4             ;;stk-ten                    x, 10.
  14204.         DB    $38             ;;end-calc
  14205.  
  14206.         POP     AF              ; pop the exponent.
  14207.  
  14208. ; now enter a loop
  14209.  
  14210. ;; E-LOOP
  14211. L2D60:  SRL     A               ; 0>76543210>C
  14212.  
  14213.         JR      NC,L2D71        ; forward to E-TST-END if no bit
  14214.  
  14215.         PUSH    AF              ; save shifted exponent.
  14216.  
  14217.         RST     28H             ;; FP-CALC
  14218.         DB    $C1             ;;st-mem-1                   x, 10.
  14219.         DB    $E0             ;;get-mem-0                  x, 10, (0/1).
  14220.         DB    $00             ;;jump-true
  14221.  
  14222.         DB    $04             ;;to L2D6D, E-DIVSN
  14223.  
  14224.         DB    $04             ;;multiply                   x*10.
  14225.         DB    $33             ;;jump
  14226.  
  14227.         DB    $02             ;;to L2D6E, E-FETCH
  14228.  
  14229. ;; E-DIVSN
  14230. L2D6D:  DB    $05             ;;division                   x/10.
  14231.  
  14232. ;; E-FETCH
  14233. L2D6E:  DB    $E1             ;;get-mem-1                  x/10 or x*10, 10.
  14234.         DB    $38             ;;end-calc                   new x, 10.
  14235.  
  14236.         POP     AF              ; restore shifted exponent
  14237.  
  14238. ; the loop branched to here with no carry
  14239.  
  14240. ;; E-TST-END
  14241. L2D71:  JR      Z,L2D7B         ; forward to E-END  if A emptied of bits
  14242.  
  14243.         PUSH    AF              ; re-save shifted exponent
  14244.  
  14245.         RST     28H             ;; FP-CALC
  14246.         DB    $31             ;;duplicate                  new x, 10, 10.
  14247.         DB    $04             ;;multiply                   new x, 100.
  14248.         DB    $38             ;;end-calc
  14249.  
  14250.         POP     AF              ; restore shifted exponent
  14251.         JR      L2D60           ; back to E-LOOP  until all bits done.
  14252.  
  14253. ; ---
  14254.  
  14255. ; although only the first pass is shown it can be seen that for each set bit
  14256. ; representing a power of two, x is multiplied or divided by the
  14257. ; corresponding power of ten.
  14258.  
  14259. ;; E-END
  14260. L2D7B:  RST     28H             ;; FP-CALC                   final x, factor.
  14261.         DB    $02             ;;delete                     final x.
  14262.         DB    $38             ;;end-calc                   x.
  14263.  
  14264.         RET                     ; return
  14265.  
  14266.  
  14267.  
  14268.  
  14269. ; -------------
  14270. ; Fetch integer
  14271. ; -------------
  14272. ; This routine is called by the mathematical routines - FP-TO-BC, PRINT-FP,
  14273. ; mult, re-stack and negate to fetch an integer from address HL.
  14274. ; HL points to the stack or a location in MEM and no deletion occurs.
  14275. ; If the number is negative then a similar process to that used in INT-STORE
  14276. ; is used to restore the twos complement number to normal in DE and a sign
  14277. ; in C.
  14278.  
  14279. ;; INT-FETCH
  14280. L2D7F:  INC     HL              ; skip zero indicator.
  14281.         LD      C,(HL)          ; fetch sign to C
  14282.         INC     HL              ; address low byte
  14283.         LD      A,(HL)          ; fetch to A
  14284.         XOR     C               ; two's complement
  14285.         SUB     C               ;
  14286.         LD      E,A             ; place in E
  14287.         INC     HL              ; address high byte
  14288.         LD      A,(HL)          ; fetch to A
  14289.         ADC     A,C             ; two's complement
  14290.         XOR     C               ;
  14291.         LD      D,A             ; place in D
  14292.         RET                     ; return
  14293.  
  14294. ; ------------------------
  14295. ; Store a positive integer
  14296. ; ------------------------
  14297. ; This entry point is not used in this ROM but would
  14298. ; store any integer as positive.
  14299.  
  14300. ;; p-int-sto
  14301. L2D8C:  LD      C,$00           ; make sign byte positive and continue
  14302.  
  14303. ; -------------
  14304. ; Store integer
  14305. ; -------------
  14306. ; this routine stores an integer in DE at address HL.
  14307. ; It is called from mult, truncate, negate and sgn.
  14308. ; The sign byte $00 +ve or $FF -ve is in C.
  14309. ; If negative, the number is stored in 2's complement form so that it is
  14310. ; ready to be added.
  14311.  
  14312. ;; INT-STORE
  14313. L2D8E:  PUSH    HL              ; preserve HL
  14314.  
  14315.         LD      (HL),$00        ; first byte zero shows integer not exponent
  14316.         INC     HL              ;
  14317.         LD      (HL),C          ; then store the sign byte
  14318.         INC     HL              ;
  14319.                                 ; e.g.             +1             -1
  14320.         LD      A,E             ; fetch low byte   00000001       00000001
  14321.         XOR     C               ; xor sign         00000000   or  11111111
  14322.                                 ; gives            00000001   or  11111110
  14323.         SUB     C               ; sub sign         00000000   or  11111111
  14324.                                 ; gives            00000001>0 or  11111111>C
  14325.         LD      (HL),A          ; store 2's complement.
  14326.         INC     HL              ;
  14327.         LD      A,D             ; high byte        00000000       00000000
  14328.         ADC     A,C             ; sign             00000000<0     11111111<C
  14329.                                 ; gives            00000000   or  00000000
  14330.         XOR     C               ; xor sign         00000000       11111111
  14331.         LD      (HL),A          ; store 2's complement.
  14332.         INC     HL              ;
  14333.         LD      (HL),$00        ; last byte always zero for integers.
  14334.                                 ; is not used and need not be looked at when
  14335.                                 ; testing for zero but comes into play should
  14336.                                 ; an integer be converted to fp.
  14337.         POP     HL              ; restore HL
  14338.         RET                     ; return.
  14339.  
  14340.  
  14341. ; -----------------------------
  14342. ; Floating point to BC register
  14343. ; -----------------------------
  14344. ; This routine gets a floating point number e.g. 127.4 from the calculator
  14345. ; stack to the BC register.
  14346.  
  14347. ;; FP-TO-BC
  14348. L2DA2:  RST     28H             ;; FP-CALC            set HL to
  14349.         DB    $38             ;;end-calc            point to last value.
  14350.  
  14351.         LD      A,(HL)          ; get first of 5 bytes
  14352.         AND     A               ; and test
  14353.         JR      Z,L2DAD         ; forward to FP-DELETE if an integer
  14354.  
  14355. ; The value is first rounded up and then converted to integer.
  14356.  
  14357.         RST     28H             ;; FP-CALC           x.
  14358.         DB    $A2             ;;stk-half           x. 1/2.
  14359.         DB    $0F             ;;addition           x + 1/2.
  14360.         DB    $27             ;;int                int(x + .5)
  14361.         DB    $38             ;;end-calc
  14362.  
  14363. ; now delete but leave HL pointing at integer
  14364.  
  14365. ;; FP-DELETE
  14366. L2DAD:  RST     28H             ;; FP-CALC
  14367.         DB    $02             ;;delete
  14368.         DB    $38             ;;end-calc
  14369.  
  14370.         PUSH    HL              ; save pointer.
  14371.         PUSH    DE              ; and STKEND.
  14372.         EX      DE,HL           ; make HL point to exponent/zero indicator
  14373.         LD      B,(HL)          ; indicator to B
  14374.         CALL    L2D7F           ; routine INT-FETCH
  14375.                                 ; gets int in DE sign byte to C
  14376.                                 ; but meaningless values if a large integer
  14377.  
  14378.         XOR     A               ; clear A
  14379.         SUB     B               ; subtract indicator byte setting carry
  14380.                                 ; if not a small integer.
  14381.  
  14382.         BIT     7,C             ; test a bit of the sign byte setting zero
  14383.                                 ; if positive.
  14384.  
  14385.         LD      B,D             ; transfer int
  14386.         LD      C,E             ; to BC
  14387.         LD      A,E             ; low byte to A as a useful return value.
  14388.  
  14389.         POP     DE              ; pop STKEND
  14390.         POP     HL              ; and pointer to last value
  14391.         RET                     ; return
  14392.                                 ; if carry is set then the number was too big.
  14393.  
  14394. ; ------------
  14395. ; LOG(2^A)
  14396. ; ------------
  14397. ; This routine is used when printing floating point numbers to calculate
  14398. ; the number of digits before the decimal point.
  14399.  
  14400. ; first convert a one-byte signed integer to its five byte form.
  14401.  
  14402. ;; LOG(2^A)
  14403. L2DC1:  LD      D,A             ; store a copy of A in D.
  14404.         RLA                     ; test sign bit of A.
  14405.         SBC     A,A             ; now $FF if negative or $00
  14406.         LD      E,A             ; sign byte to E.
  14407.         LD      C,A             ; and to C
  14408.         XOR     A               ; clear A
  14409.         LD      B,A             ; and B.
  14410.         CALL    L2AB6           ; routine STK-STORE stacks number AEDCB
  14411.  
  14412. ;  so 00 00 XX 00 00 (positive) or 00 FF XX FF 00 (negative).
  14413. ;  i.e. integer indicator, sign byte, low, high, unused.
  14414.  
  14415. ; now multiply exponent by log to the base 10 of two.
  14416.  
  14417.         RST      28H            ;; FP-CALC
  14418.  
  14419.         DB    $34             ;;stk-data                      .30103 (log 2)
  14420.         DB    $EF             ;;Exponent: $7F, Bytes: 4
  14421.         DB    $1A,$20,$9A,$85 ;;
  14422.         DB    $04             ;;multiply
  14423.  
  14424.         DB    $27             ;;int
  14425.  
  14426.         DB    $38             ;;end-calc
  14427.  
  14428. ; -------------------
  14429. ; Floating point to A
  14430. ; -------------------
  14431. ; this routine collects a floating point number from the stack into the
  14432. ; accumulator returning carry set if not in range 0 - 255.
  14433. ; Not all the calling routines raise an error with overflow so no attempt
  14434. ; is made to produce an error report here.
  14435.  
  14436. ;; FP-TO-A
  14437. L2DD5:  CALL    L2DA2           ; routine FP-TO-BC returns with C in A also.
  14438.         RET     C               ; return with carry set if > 65535, overflow
  14439.  
  14440.         PUSH    AF              ; save the value and flags
  14441.         DEC     B               ; and test that
  14442.         INC     B               ; the high byte is zero.
  14443.         JR      Z,L2DE1         ; forward  FP-A-END if zero
  14444.  
  14445. ; else there has been 8-bit overflow
  14446.  
  14447.         POP     AF              ; retrieve the value
  14448.         SCF                     ; set carry flag to show overflow
  14449.         RET                     ; and return.
  14450.  
  14451. ; ---
  14452.  
  14453. ;; FP-A-END
  14454. L2DE1:  POP     AF              ; restore value and success flag and
  14455.         RET                     ; return.
  14456.  
  14457.  
  14458. ; -----------------------------
  14459. ; Print a floating point number
  14460. ; -----------------------------
  14461. ; Not a trivial task.
  14462. ; Begin by considering whether to print a leading sign for negative numbers.
  14463.  
  14464. ;; PRINT-FP
  14465. L2DE3:  RST     28H             ;; FP-CALC
  14466.         DB    $31             ;;duplicate
  14467.         DB    $36             ;;less-0
  14468.         DB    $00             ;;jump-true
  14469.  
  14470.         DB    $0B             ;;to L2DF2, PF-NEGTVE
  14471.  
  14472.         DB    $31             ;;duplicate
  14473.         DB    $37             ;;greater-0
  14474.         DB    $00             ;;jump-true
  14475.  
  14476.         DB    $0D             ;;to L2DF8, PF-POSTVE
  14477.  
  14478. ; must be zero itself
  14479.  
  14480.         DB    $02             ;;delete
  14481.         DB    $38             ;;end-calc
  14482.  
  14483.         LD      A,$30           ; prepare the character '0'
  14484.  
  14485.         RST     10H             ; PRINT-A
  14486.         RET                     ; return.                 ->
  14487. ; ---
  14488.  
  14489. ;; PF-NEGTVE
  14490. L2DF2:  DB    $2A             ;;abs
  14491.         DB    $38             ;;end-calc
  14492.  
  14493.         LD      A,$2D           ; the character '-'
  14494.  
  14495.         RST     10H             ; PRINT-A
  14496.  
  14497. ; and continue to print the now positive number.
  14498.  
  14499.         RST     28H             ;; FP-CALC
  14500.  
  14501. ;; PF-POSTVE
  14502. L2DF8:  DB    $A0             ;;stk-zero     x,0.     begin by
  14503.         DB    $C3             ;;st-mem-3     x,0.     clearing a temporary
  14504.         DB    $C4             ;;st-mem-4     x,0.     output buffer to
  14505.         DB    $C5             ;;st-mem-5     x,0.     fifteen zeros.
  14506.         DB    $02             ;;delete       x.
  14507.         DB    $38             ;;end-calc     x.
  14508.  
  14509.         EXX                     ; in case called from 'str$' then save the
  14510.         PUSH    HL              ; pointer to whatever comes after
  14511.         EXX                     ; str$ as H'L' will be used.
  14512.  
  14513. ; now enter a loop?
  14514.  
  14515. ;; PF-LOOP
  14516. L2E01:  RST     28H             ;; FP-CALC
  14517.         DB    $31             ;;duplicate    x,x.
  14518.         DB    $27             ;;int          x,int x.
  14519.         DB    $C2             ;;st-mem-2     x,int x.
  14520.         DB    $03             ;;subtract     x-int x.     fractional part.
  14521.         DB    $E2             ;;get-mem-2    x-int x, int x.
  14522.         DB    $01             ;;exchange     int x, x-int x.
  14523.         DB    $C2             ;;st-mem-2     int x, x-int x.
  14524.         DB    $02             ;;delete       int x.
  14525.         DB    $38             ;;end-calc     int x.
  14526.                                 ;
  14527.                                 ; mem-2 holds the fractional part.
  14528.  
  14529. ; HL points to last value int x
  14530.  
  14531.         LD      A,(HL)          ; fetch exponent of int x.
  14532.         AND     A               ; test
  14533.         JR      NZ,L2E56        ; forward to PF-LARGE if a large integer
  14534.                                 ; > 65535
  14535.  
  14536. ; continue with small positive integer components in range 0 - 65535
  14537. ; if original number was say .999 then this integer component is zero.
  14538.  
  14539.         CALL    L2D7F           ; routine INT-FETCH gets x in DE
  14540.                                 ; (but x is not deleted)
  14541.  
  14542.         LD      B,$10           ; set B, bit counter, to 16d
  14543.  
  14544.         LD      A,D             ; test if
  14545.         AND     A               ; high byte is zero
  14546.         JR      NZ,L2E1E        ; forward to PF-SAVE if 16-bit integer.
  14547.  
  14548. ; and continue with integer in range 0 - 255.
  14549.  
  14550.         OR      E               ; test the low byte for zero
  14551.                                 ; i.e. originally just point something or other.
  14552.         JR      Z,L2E24         ; forward if so to PF-SMALL
  14553.  
  14554. ;
  14555.  
  14556.         LD      D,E             ; transfer E to D
  14557.         LD      B,$08           ; and reduce the bit counter to 8.
  14558.  
  14559. ;; PF-SAVE
  14560. L2E1E:  PUSH    DE              ; save the part before decimal point.
  14561.         EXX                     ;
  14562.         POP     DE              ; and pop in into D'E'
  14563.         EXX                     ;
  14564.         JR      L2E7B           ; forward to PF-BITS
  14565.  
  14566. ; ---------------------
  14567.  
  14568. ; the branch was here when 'int x' was found to be zero as in say 0.5.
  14569. ; The zero has been fetched from the calculator stack but not deleted and
  14570. ; this should occur now. This omission leaves the stack unbalanced and while
  14571. ; that causes no problems with a simple PRINT statement, it will if str$ is
  14572. ; being used in an expression e.g. "2" + STR$ 0.5 gives the result "0.5"
  14573. ; instead of the expected result "20.5".
  14574. ; credit Tony Stratton, 1982.
  14575. ; A DB 02 delete is required immediately on using the calculator.
  14576.  
  14577. ;; PF-SMALL
  14578. L2E24:  RST     28H             ;; FP-CALC       int x = 0.
  14579. L2E25:  DB    $E2             ;;get-mem-2      int x = 0, x-int x.
  14580.         DB    $38             ;;end-calc
  14581.  
  14582.         LD      A,(HL)          ; fetch exponent of positive fractional number
  14583.         SUB     $7E             ; subtract
  14584.  
  14585.         CALL    L2DC1           ; routine LOG(2^A) calculates leading digits.
  14586.  
  14587.         LD      D,A             ; transfer count to D
  14588.         LD      A,($5CAC)       ; fetch total MEM-5-1
  14589.         SUB     D               ;
  14590.         LD      ($5CAC),A       ; MEM-5-1
  14591.         LD      A,D             ;
  14592.         CALL    L2D4F           ; routine E-TO-FP
  14593.  
  14594.         RST     28H             ;; FP-CALC
  14595.         DB    $31             ;;duplicate
  14596.         DB    $27             ;;int
  14597.         DB    $C1             ;;st-mem-1
  14598.         DB    $03             ;;subtract
  14599.         DB    $E1             ;;get-mem-1
  14600.         DB    $38             ;;end-calc
  14601.  
  14602.         CALL    L2DD5           ; routine FP-TO-A
  14603.  
  14604.         PUSH    HL              ; save HL
  14605.         LD      ($5CA1),A       ; MEM-3-1
  14606.         DEC     A               ;
  14607.         RLA                     ;
  14608.         SBC     A,A             ;
  14609.         INC     A               ;
  14610.  
  14611.         LD      HL,$5CAB        ; address MEM-5-1 leading digit counter
  14612.         LD      (HL),A          ; store counter
  14613.         INC     HL              ; address MEM-5-2 total digits
  14614.         ADD     A,(HL)          ; add counter to contents
  14615.         LD      (HL),A          ; and store updated value
  14616.         POP     HL              ; restore HL
  14617.  
  14618.         JP      L2ECF           ; JUMP forward to PF-FRACTN
  14619.  
  14620. ; ---
  14621.  
  14622. ; Note. while it would be pedantic to comment on every occasion a JP
  14623. ; instruction could be replaced with a JR instruction, this applies to the
  14624. ; above, which is useful if you wish to correct the unbalanced stack error
  14625. ; by inserting a 'DB 02 delete' at L2E25, and maintain main addresses.
  14626.  
  14627. ; the branch was here with a large positive integer > 65535 e.g. 123456789
  14628. ; the accumulator holds the exponent.
  14629.  
  14630. ;; PF-LARGE
  14631. L2E56:  SUB     $80             ; make exponent positive
  14632.         CP      $1C             ; compare to 28
  14633.         JR      C,L2E6F         ; to PF-MEDIUM if integer <= 2^27
  14634.  
  14635.         CALL    L2DC1           ; routine LOG(2^A)
  14636.         SUB     $07             ;
  14637.         LD      B,A             ;
  14638.         LD      HL,$5CAC        ; address MEM-5-1 the leading digits counter.
  14639.         ADD     A,(HL)          ; add A to contents
  14640.         LD      (HL),A          ; store updated value.
  14641.         LD      A,B             ;
  14642.         NEG                     ; negate
  14643.         CALL    L2D4F           ; routine E-TO-FP
  14644.         JR      L2E01           ; back to PF-LOOP
  14645.  
  14646. ; ----------------------------
  14647.  
  14648. ;; PF-MEDIUM
  14649. L2E6F:  EX      DE,HL           ;
  14650.         CALL    L2FBA           ; routine FETCH-TWO
  14651.         EXX                     ;
  14652.         SET     7,D             ;
  14653.         LD      A,L             ;
  14654.         EXX                     ;
  14655.         SUB     $80             ;
  14656.         LD      B,A             ;
  14657.  
  14658. ; the branch was here to handle bits in DE with 8 or 16 in B  if small int
  14659. ; and integer in D'E', 6 nibbles will accommodate 065535 but routine does
  14660. ; 32-bit numbers as well from above
  14661.  
  14662. ;; PF-BITS
  14663. L2E7B:  SLA     E               ;  C<xxxxxxxx<0
  14664.         RL      D               ;  C<xxxxxxxx<C
  14665.         EXX                     ;
  14666.         RL      E               ;  C<xxxxxxxx<C
  14667.         RL      D               ;  C<xxxxxxxx<C
  14668.         EXX                     ;
  14669.  
  14670.         LD      HL,$5CAA        ; set HL to mem-4-5th last byte of buffer
  14671.         LD      C,$05           ; set byte count to 5 -  10 nibbles
  14672.  
  14673. ;; PF-BYTES
  14674. L2E8A:  LD      A,(HL)          ; fetch 0 or prev value
  14675.         ADC     A,A             ; shift left add in carry    C<xxxxxxxx<C
  14676.  
  14677.         DAA                     ; Decimal Adjust Accumulator.
  14678.                                 ; if greater than 9 then the left hand
  14679.                                 ; nibble is incremented. If greater than
  14680.                                 ; 99 then adjusted and carry set.
  14681.                                 ; so if we'd built up 7 and a carry came in
  14682.                                 ;      0000 0111 < C
  14683.                                 ;      0000 1111
  14684.                                 ; daa     1 0101  which is 15 in BCD
  14685.  
  14686.         LD      (HL),A          ; put back
  14687.         DEC     HL              ; work down thru mem 4
  14688.         DEC     C               ; decrease the 5 counter.
  14689.         JR      NZ,L2E8A        ; back to PF-BYTES until the ten nibbles rolled
  14690.  
  14691.         DJNZ    L2E7B           ; back to PF-BITS until 8 or 16 (or 32) done
  14692.  
  14693. ; at most 9 digits for 32-bit number will have been loaded with digits
  14694. ; each of the 9 nibbles in mem 4 is placed into ten bytes in mem-3 and mem 4
  14695. ; unless the nibble is zero as the buffer is already zero.
  14696. ; ( or in the case of mem-5 will become zero as a result of RLD instruction )
  14697.  
  14698.         XOR     A               ; clear to accept
  14699.         LD      HL,$5CA6        ; address MEM-4-0 byte destination.
  14700.         LD      DE,$5CA1        ; address MEM-3-0 nibble source.
  14701.         LD      B,$09           ; the count is 9 (not ten) as the first
  14702.                                 ; nibble is known to be blank.
  14703.  
  14704.         RLD                     ; shift RH nibble to left in (HL)
  14705.                                 ;    A           (HL)
  14706.                                 ; 0000 0000 < 0000 3210
  14707.                                 ; 0000 0000   3210 0000
  14708.                                 ; A picks up the blank nibble
  14709.  
  14710.  
  14711.         LD      C,$FF           ; set a flag to indicate when a significant
  14712.                                 ; digit has been encountered.
  14713.  
  14714. ;; PF-DIGITS
  14715. L2EA1:  RLD                     ; pick up leftmost nibble from (HL)
  14716.                                 ;    A           (HL)
  14717.                                 ; 0000 0000 < 7654 3210
  14718.                                 ; 0000 7654   3210 0000
  14719.  
  14720.  
  14721.         JR      NZ,L2EA9        ; to PF-INSERT if non-zero value picked up.
  14722.  
  14723.         DEC     C               ; test
  14724.         INC     C               ; flag
  14725.         JR      NZ,L2EB3        ; skip forward to PF-TEST-2 if flag still $FF
  14726.                                 ; indicating this is a leading zero.
  14727.  
  14728. ; but if the zero is a significant digit e.g. 10 then include in digit totals.
  14729. ; the path for non-zero digits rejoins here.
  14730.  
  14731. ;; PF-INSERT
  14732. L2EA9:  LD      (DE),A          ; insert digit at destination
  14733.         INC     DE              ; increase the destination pointer
  14734.         INC     (IY+$71)        ; increment MEM-5-1st  digit counter
  14735.         INC     (IY+$72)        ; increment MEM-5-2nd  leading digit counter
  14736.         LD      C,$00           ; set flag to zero indicating that any
  14737.                                 ; subsequent zeros are significant and not
  14738.                                 ; leading.
  14739.  
  14740. ;; PF-TEST-2
  14741. L2EB3:  BIT     0,B             ; test if the nibble count is even
  14742.         JR      Z,L2EB8         ; skip to PF-ALL-9 if so to deal with the
  14743.                                 ; other nibble in the same byte
  14744.  
  14745.         INC     HL              ; point to next source byte if not
  14746.  
  14747. ;; PF-ALL-9
  14748. L2EB8:  DJNZ    L2EA1           ; decrement the nibble count, back to PF-DIGITS
  14749.                                 ; if all nine not done.
  14750.  
  14751. ; For 8-bit integers there will be at most 3 digits.
  14752. ; For 16-bit integers there will be at most 5 digits.
  14753. ; but for larger integers there could be nine leading digits.
  14754. ; if nine digits complete then the last one is rounded up as the number will
  14755. ; be printed using E-format notation
  14756.  
  14757.         LD      A,($5CAB)       ; fetch digit count from MEM-5-1st
  14758.         SUB     $09             ; subtract 9 - max possible
  14759.         JR      C,L2ECB         ; forward if less to PF-MORE
  14760.  
  14761.         DEC     (IY+$71)        ; decrement digit counter MEM-5-1st to 8
  14762.         LD      A,$04           ; load A with the value 4.
  14763.         CP      (IY+$6F)        ; compare with MEM-4-4th - the ninth digit
  14764.         JR      L2F0C           ; forward to PF-ROUND
  14765.                                 ; to consider rounding.
  14766.  
  14767. ; ---------------------------------------
  14768.  
  14769. ; now delete int x from calculator stack and fetch fractional part.
  14770.  
  14771. ;; PF-MORE
  14772. L2ECB:  RST     28H             ;; FP-CALC        int x.
  14773.         DB    $02             ;;delete          .
  14774.         DB    $E2             ;;get-mem-2       x - int x = f.
  14775.         DB    $38             ;;end-calca       f.
  14776.  
  14777. ;; PF-FRACTN
  14778. L2ECF:  EX      DE,HL           ;
  14779.         CALL    L2FBA           ; routine FETCH-TWO
  14780.         EXX                     ;
  14781.         LD      A,$80           ;
  14782.         SUB     L               ;
  14783.         LD      L,$00           ;
  14784.         SET     7,D             ;
  14785.         EXX                     ;
  14786.         CALL    L2FDD           ; routine SHIFT-FP
  14787.  
  14788. ;; PF-FRN-LP
  14789. L2EDF:  LD      A,(IY+$71)      ; MEM-5-1st
  14790.         CP      $08             ;
  14791.         JR      C,L2EEC         ; to PF-FR-DGT
  14792.  
  14793.         EXX                     ;
  14794.         RL      D               ;
  14795.         EXX                     ;
  14796.         JR      L2F0C           ; to PF-ROUND
  14797.  
  14798. ; ---
  14799.  
  14800. ;; PF-FR-DGT
  14801. L2EEC:  LD      BC,$0200        ;
  14802.  
  14803. ;; PF-FR-EXX
  14804. L2EEF:  LD      A,E             ;
  14805.         CALL    L2F8B           ; routine CA-10*A+C
  14806.         LD      E,A             ;
  14807.         LD      A,D             ;
  14808.         CALL    L2F8B           ; routine CA-10*A+C
  14809.         LD      D,A             ;
  14810.         PUSH    BC              ;
  14811.         EXX                     ;
  14812.         POP     BC              ;
  14813.         DJNZ    L2EEF           ; to PF-FR-EXX
  14814.  
  14815.         LD      HL,$5CA1        ; MEM-3
  14816.         LD      A,C             ;
  14817.         LD      C,(IY+$71)      ; MEM-5-1st
  14818.         ADD     HL,BC           ;
  14819.         LD      (HL),A          ;
  14820.         INC     (IY+$71)        ; MEM-5-1st
  14821.         JR      L2EDF           ; to PF-FRN-LP
  14822.  
  14823. ; ----------------
  14824.  
  14825. ; 1) with 9 digits but 8 in mem-5-1 and A holding 4, carry set if rounding up.
  14826. ; e.g.
  14827. ;      999999999 is printed as 1E+9
  14828. ;      100000001 is printed as 1E+8
  14829. ;      100000009 is printed as 1.0000001E+8
  14830.  
  14831. ;; PF-ROUND
  14832. L2F0C:  PUSH    AF              ; save A and flags
  14833.         LD      HL,$5CA1        ; address MEM-3 start of digits
  14834.         LD      C,(IY+$71)      ; MEM-5-1st No. of digits to C
  14835.         LD      B,$00           ; prepare to add
  14836.         ADD     HL,BC           ; address last digit + 1
  14837.         LD      B,C             ; No. of digits to B counter
  14838.         POP     AF              ; restore A and carry flag from comparison.
  14839.  
  14840. ;; PF-RND-LP
  14841. L2F18:  DEC     HL              ; address digit at rounding position.
  14842.         LD      A,(HL)          ; fetch it
  14843.         ADC     A,$00           ; add carry from the comparison
  14844.         LD      (HL),A          ; put back result even if $0A.
  14845.         AND     A               ; test A
  14846.         JR      Z,L2F25         ; skip to PF-R-BACK if ZERO?
  14847.  
  14848.         CP      $0A             ; compare to 'ten' - overflow
  14849.         CCF                     ; complement carry flag so that set if ten.
  14850.         JR      NC,L2F2D        ; forward to PF-COUNT with 1 - 9.
  14851.  
  14852. ;; PF-R-BACK
  14853. L2F25:  DJNZ    L2F18           ; loop back to PF-RND-LP
  14854.  
  14855. ; if B counts down to zero then we've rounded right back as in 999999995.
  14856. ; and the first 8 locations all hold $0A.
  14857.  
  14858.  
  14859.         LD      (HL),$01        ; load first location with digit 1.
  14860.         INC     B               ; make B hold 1 also.
  14861.                                 ; could save an instruction byte here.
  14862.         INC     (IY+$72)        ; make MEM-5-2nd hold 1.
  14863.                                 ; and proceed to initialize total digits to 1.
  14864.  
  14865. ;; PF-COUNT
  14866. L2F2D:  LD      (IY+$71),B      ; MEM-5-1st
  14867.  
  14868. ; now balance the calculator stack by deleting  it
  14869.  
  14870.         RST     28H             ;; FP-CALC
  14871.         DB    $02             ;;delete
  14872.         DB    $38             ;;end-calc
  14873.  
  14874. ; note if used from str$ then other values may be on the calculator stack.
  14875. ; we can also restore the next literal pointer from its position on the
  14876. ; machine stack.
  14877.  
  14878.         EXX                     ;
  14879.         POP     HL              ; restore next literal pointer.
  14880.         EXX                     ;
  14881.  
  14882.         LD      BC,($5CAB)      ; set C to MEM-5-1st digit counter.
  14883.                                 ; set B to MEM-5-2nd leading digit counter.
  14884.         LD      HL,$5CA1        ; set HL to start of digits at MEM-3-1
  14885.         LD      A,B             ;
  14886.         CP      $09             ;
  14887.         JR      C,L2F46         ; to PF-NOT-E
  14888.  
  14889.         CP      $FC             ;
  14890.         JR      C,L2F6C         ; to PF-E-FRMT
  14891.  
  14892. ;; PF-NOT-E
  14893. L2F46:  AND     A               ; test for zero leading digits as in .123
  14894.  
  14895.         CALL    Z,L15EF         ; routine OUT-CODE prints a zero e.g. 0.123
  14896.  
  14897. ;; PF-E-SBRN
  14898. L2F4A:  XOR     A               ;
  14899.         SUB     B               ;
  14900.         JP      M,L2F52         ; skip forward to PF-OUT-LP if originally +ve
  14901.  
  14902.         LD      B,A             ; else negative count now +ve
  14903.         JR      L2F5E           ; forward to PF-DC-OUT       ->
  14904.  
  14905. ; ---
  14906.  
  14907. ;; PF-OUT-LP
  14908. L2F52:  LD      A,C             ; fetch total digit count
  14909.         AND     A               ; test for zero
  14910.         JR      Z,L2F59         ; forward to PF-OUT-DT if so
  14911.  
  14912.         LD      A,(HL)          ; fetch digit
  14913.         INC     HL              ; address next digit
  14914.         DEC     C               ; decrease total digit counter
  14915.  
  14916. ;; PF-OUT-DT
  14917. L2F59:  CALL    L15EF           ; routine OUT-CODE outputs it.
  14918.         DJNZ    L2F52           ; loop back to PF-OUT-LP until B leading
  14919.                                 ; digits output.
  14920.  
  14921. ;; PF-DC-OUT
  14922. L2F5E:  LD      A,C             ; fetch total digits and
  14923.         AND     A               ; test if also zero
  14924.         RET     Z               ; return if so              -->
  14925.  
  14926. ;
  14927.  
  14928.         INC     B               ; increment B
  14929.         LD      A,$2E           ; prepare the character '.'
  14930.  
  14931. ;; PF-DEC-0$
  14932. L2F64:  RST     10H             ; PRINT-A outputs the character '.' or '0'
  14933.  
  14934.         LD      A,$30           ; prepare the character '0'
  14935.                                 ; (for cases like .000012345678)
  14936.         DJNZ    L2F64           ; loop back to PF-DEC-0$ for B times.
  14937.  
  14938.         LD      B,C             ; load B with now trailing digit counter.
  14939.         JR      L2F52           ; back to PF-OUT-LP
  14940.  
  14941. ; ---------------------------------
  14942.  
  14943. ; the branch was here for E-format printing e.g 123456789 => 1.2345679e+8
  14944.  
  14945. ;; PF-E-FRMT
  14946. L2F6C:  LD      D,B             ; counter to D
  14947.         DEC     D               ; decrement
  14948.         LD      B,$01           ; load B with 1.
  14949.  
  14950.         CALL    L2F4A           ; routine PF-E-SBRN above
  14951.  
  14952.         LD      A,$45           ; prepare character 'e'
  14953.         RST     10H             ; PRINT-A
  14954.  
  14955.         LD      C,D             ; exponent to C
  14956.         LD      A,C             ; and to A
  14957.         AND     A               ; test exponent
  14958.         JP      P,L2F83         ; to PF-E-POS if positive
  14959.  
  14960.         NEG                     ; negate
  14961.         LD      C,A             ; positive exponent to C
  14962.         LD      A,$2D           ; prepare character '-'
  14963.         JR      L2F85           ; skip to PF-E-SIGN
  14964.  
  14965. ; ---
  14966.  
  14967. ;; PF-E-POS
  14968. L2F83:  LD      A,$2B           ; prepare character '+'
  14969.  
  14970. ;; PF-E-SIGN
  14971. L2F85:  RST     10H             ; PRINT-A outputs the sign
  14972.  
  14973.         LD      B,$00           ; make the high byte zero.
  14974.         JP      L1A1B           ; exit via OUT-NUM-1 to print exponent in BC
  14975.  
  14976. ; ------------------------------
  14977. ; Handle printing floating point
  14978. ; ------------------------------
  14979. ; This subroutine is called twice from above when printing floating-point
  14980. ; numbers. It returns 10*A +C in registers C and A
  14981.  
  14982. ;; CA-10*A+C
  14983. L2F8B:  PUSH    DE              ; preserve DE.
  14984.         LD      L,A             ; transfer A to L
  14985.         LD      H,$00           ; zero high byte.
  14986.         LD      E,L             ; copy HL
  14987.         LD      D,H             ; to DE.
  14988.         ADD     HL,HL           ; double (*2)
  14989.         ADD     HL,HL           ; double (*4)
  14990.         ADD     HL,DE           ; add DE (*5)
  14991.         ADD     HL,HL           ; double (*10)
  14992.         LD      E,C             ; copy C to E    (D is 0)
  14993.         ADD     HL,DE           ; and add to give required result.
  14994.         LD      C,H             ; transfer to
  14995.         LD      A,L             ; destination registers.
  14996.         POP     DE              ; restore DE
  14997.         RET                     ; return with result.
  14998.  
  14999. ; --------------
  15000. ; Prepare to add
  15001. ; --------------
  15002. ; This routine is called twice by addition to prepare the two numbers. The
  15003. ; exponent is picked up in A and the location made zero. Then the sign bit
  15004. ; is tested before being set to the implied state. Negative numbers are twos
  15005. ; complemented.
  15006.  
  15007. ;; PREP-ADD
  15008. L2F9B:  LD      A,(HL)          ; pick up exponent
  15009.         LD      (HL),$00        ; make location zero
  15010.         AND     A               ; test if number is zero
  15011.         RET     Z               ; return if so
  15012.  
  15013.         INC     HL              ; address mantissa
  15014.         BIT     7,(HL)          ; test the sign bit
  15015.         SET     7,(HL)          ; set it to implied state
  15016.         DEC     HL              ; point to exponent
  15017.         RET     Z               ; return if positive number.
  15018.  
  15019.         PUSH    BC              ; preserve BC
  15020.         LD      BC,$0005        ; length of number
  15021.         ADD     HL,BC           ; point HL past end
  15022.         LD      B,C             ; set B to 5 counter
  15023.         LD      C,A             ; store exponent in C
  15024.         SCF                     ; set carry flag
  15025.  
  15026. ;; NEG-BYTE
  15027. L2FAF:  DEC     HL              ; work from LSB to MSB
  15028.         LD      A,(HL)          ; fetch byte
  15029.         CPL                     ; complement
  15030.         ADC     A,$00           ; add in initial carry or from prev operation
  15031.         LD      (HL),A          ; put back
  15032.         DJNZ    L2FAF           ; loop to NEG-BYTE till all 5 done
  15033.  
  15034.         LD      A,C             ; stored exponent to A
  15035.         POP     BC              ; restore original BC
  15036.         RET                     ; return
  15037.  
  15038. ; -----------------
  15039. ; Fetch two numbers
  15040. ; -----------------
  15041. ; This routine is called twice when printing floating point numbers and also
  15042. ; to fetch two numbers by the addition, multiply and division routines.
  15043. ; HL addresses the first number, DE addresses the second number.
  15044. ; For arithmetic only, A holds the sign of the result which is stored in
  15045. ; the second location.
  15046.  
  15047. ;; FETCH-TWO
  15048. L2FBA:  PUSH    HL              ; save pointer to first number, result if math.
  15049.         PUSH    AF              ; save result sign.
  15050.  
  15051.         LD      C,(HL)          ;
  15052.         INC     HL              ;
  15053.  
  15054.         LD      B,(HL)          ;
  15055.         LD      (HL),A          ; store the sign at correct location in
  15056.                                 ; destination 5 bytes for arithmetic only.
  15057.         INC     HL              ;
  15058.  
  15059.         LD      A,C             ;
  15060.         LD      C,(HL)          ;
  15061.         PUSH    BC              ;
  15062.         INC     HL              ;
  15063.         LD      C,(HL)          ;
  15064.         INC     HL              ;
  15065.         LD      B,(HL)          ;
  15066.         EX      DE,HL           ;
  15067.         LD      D,A             ;
  15068.         LD      E,(HL)          ;
  15069.         PUSH    DE              ;
  15070.         INC     HL              ;
  15071.         LD      D,(HL)          ;
  15072.         INC     HL              ;
  15073.         LD      E,(HL)          ;
  15074.         PUSH    DE              ;
  15075.         EXX                     ;
  15076.         POP     DE              ;
  15077.         POP     HL              ;
  15078.         POP     BC              ;
  15079.         EXX                     ;
  15080.         INC     HL              ;
  15081.         LD      D,(HL)          ;
  15082.         INC     HL              ;
  15083.         LD      E,(HL)          ;
  15084.  
  15085.         POP     AF              ; restore possible result sign.
  15086.         POP     HL              ; and pointer to possible result.
  15087.         RET                     ; return.
  15088.  
  15089. ; ---------------------------------
  15090. ; Shift floating point number right
  15091. ; ---------------------------------
  15092. ;
  15093. ;
  15094.  
  15095. ;; SHIFT-FP
  15096. L2FDD:  AND     A               ;
  15097.         RET     Z               ;
  15098.  
  15099.         CP      $21             ;
  15100.         JR      NC,L2FF9        ; to ADDEND-0
  15101.  
  15102.         PUSH    BC              ;
  15103.         LD      B,A             ;
  15104.  
  15105. ;; ONE-SHIFT
  15106. L2FE5:  EXX                     ;
  15107.         SRA     L               ;
  15108.         RR      D               ;
  15109.         RR      E               ;
  15110.         EXX                     ;
  15111.         RR      D               ;
  15112.         RR      E               ;
  15113.         DJNZ    L2FE5           ; to ONE-SHIFT
  15114.  
  15115.         POP     BC              ;
  15116.         RET     NC              ;
  15117.  
  15118.         CALL    L3004           ; routine ADD-BACK
  15119.         RET     NZ              ;
  15120.  
  15121. ;; ADDEND-0
  15122. L2FF9:  EXX                     ;
  15123.         XOR     A               ;
  15124.  
  15125. ;; ZEROS-4/5
  15126. L2FFB:  LD      L,$00           ;
  15127.         LD      D,A             ;
  15128.         LD      E,L             ;
  15129.         EXX                     ;
  15130.         LD      DE,$0000        ;
  15131.         RET                     ;
  15132.  
  15133. ; ------------------
  15134. ; Add back any carry
  15135. ; ------------------
  15136. ;
  15137. ;
  15138.  
  15139. ;; ADD-BACK
  15140. L3004:  INC     E               ;
  15141.         RET     NZ              ;
  15142.  
  15143.         INC      D              ;
  15144.         RET     NZ              ;
  15145.  
  15146.         EXX                     ;
  15147.         INC     E               ;
  15148.         JR      NZ,L300D        ; to ALL-ADDED
  15149.  
  15150.         INC     D               ;
  15151.  
  15152. ;; ALL-ADDED
  15153. L300D:  EXX                     ;
  15154.         RET                     ;
  15155.  
  15156. ; -----------------------
  15157. ; Handle subtraction (03)
  15158. ; -----------------------
  15159. ; Subtraction is done by switching the sign byte/bit of the second number
  15160. ; which may be integer of floating point and continuing into addition.
  15161.  
  15162. ;; subtract
  15163. L300F:  EX      DE,HL           ; address second number with HL
  15164.  
  15165.         CALL    L346E           ; routine NEGATE switches sign
  15166.  
  15167.         EX      DE,HL           ; address first number again
  15168.                                 ; and continue.
  15169.  
  15170. ; --------------------
  15171. ; Handle addition (0F)
  15172. ; --------------------
  15173. ; HL points to first number, DE to second.
  15174. ; If they are both integers, then go for the easy route.
  15175.  
  15176. ;; addition
  15177. L3014:  LD      A,(DE)          ; fetch first byte of second
  15178.         OR      (HL)            ; combine with first byte of first
  15179.         JR      NZ,L303E        ; forward to FULL-ADDN if at least one was
  15180.                                 ; in floating point form.
  15181.  
  15182. ; continue if both were small integers.
  15183.  
  15184.         PUSH    DE              ; save pointer to lowest number for result.
  15185.  
  15186.         INC     HL              ; address sign byte and
  15187.         PUSH    HL              ; push the pointer.
  15188.  
  15189.         INC     HL              ; address low byte
  15190.         LD      E,(HL)          ; to E
  15191.         INC     HL              ; address high byte
  15192.         LD      D,(HL)          ; to D
  15193.         INC     HL              ; address unused byte
  15194.  
  15195.         INC     HL              ; address known zero indicator of 1st number
  15196.         INC     HL              ; address sign byte
  15197.  
  15198.         LD      A,(HL)          ; sign to A, $00 or $FF
  15199.  
  15200.         INC     HL              ; address low byte
  15201.         LD      C,(HL)          ; to C
  15202.         INC     HL              ; address high byte
  15203.         LD      B,(HL)          ; to B
  15204.  
  15205.         POP     HL              ; pop result sign pointer
  15206.         EX      DE,HL           ; integer to HL
  15207.  
  15208.         ADD     HL,BC           ; add to the other one in BC
  15209.                                 ; setting carry if overflow.
  15210.  
  15211.         EX      DE,HL           ; save result in DE bringing back sign pointer
  15212.  
  15213.         ADC     A,(HL)          ; if pos/pos A=01 with overflow else 00
  15214.                                 ; if neg/neg A=FF with overflow else FE
  15215.                                 ; if mixture A=00 with overflow else FF
  15216.  
  15217.         RRCA                    ; bit 0 to (C)
  15218.  
  15219.         ADC     A,$00           ; both acceptable signs now zero
  15220.  
  15221.         JR      NZ,L303C        ; forward to ADDN-OFLW if not
  15222.  
  15223.         SBC     A,A             ; restore a negative result sign
  15224.  
  15225.         LD      (HL),A          ;
  15226.         INC     HL              ;
  15227.         LD      (HL),E          ;
  15228.         INC     HL              ;
  15229.         LD      (HL),D          ;
  15230.         DEC     HL              ;
  15231.         DEC     HL              ;
  15232.         DEC     HL              ;
  15233.  
  15234.         POP     DE              ; STKEND
  15235.         RET                     ;
  15236.  
  15237. ; ---
  15238.  
  15239. ;; ADDN-OFLW
  15240. L303C:  DEC     HL              ;
  15241.         POP     DE              ;
  15242.  
  15243. ;; FULL-ADDN
  15244. L303E:  CALL    L3293           ; routine RE-ST-TWO
  15245.         EXX                     ;
  15246.         PUSH    HL              ;
  15247.         EXX                     ;
  15248.         PUSH    DE              ;
  15249.         PUSH    HL              ;
  15250.         CALL    L2F9B           ; routine PREP-ADD
  15251.         LD      B,A             ;
  15252.         EX      DE,HL           ;
  15253.         CALL    L2F9B           ; routine PREP-ADD
  15254.         LD       C,A            ;
  15255.         CP      B               ;
  15256.         JR      NC,L3055        ; to SHIFT-LEN
  15257.  
  15258.         LD      A,B             ;
  15259.         LD      B,C             ;
  15260.         EX      DE,HL           ;
  15261.  
  15262. ;; SHIFT-LEN
  15263. L3055:  PUSH    AF              ;
  15264.         SUB     B               ;
  15265.         CALL    L2FBA           ; routine FETCH-TWO
  15266.         CALL    L2FDD           ; routine SHIFT-FP
  15267.         POP     AF              ;
  15268.         POP     HL              ;
  15269.         LD      (HL),A          ;
  15270.         PUSH    HL              ;
  15271.         LD      L,B             ;
  15272.         LD      H,C             ;
  15273.         ADD     HL,DE           ;
  15274.         EXX                     ;
  15275.         EX      DE,HL           ;
  15276.         ADC     HL,BC           ;
  15277.         EX      DE,HL           ;
  15278.         LD      A,H             ;
  15279.         ADC     A,L             ;
  15280.         LD      L,A             ;
  15281.         RRA                     ;
  15282.         XOR     L               ;
  15283.         EXX                     ;
  15284.         EX      DE,HL           ;
  15285.         POP     HL              ;
  15286.         RRA                     ;
  15287.         JR      NC,L307C        ; to TEST-NEG
  15288.  
  15289.         LD      A,$01           ;
  15290.         CALL    L2FDD           ; routine SHIFT-FP
  15291.         INC     (HL)            ;
  15292.         JR      Z,L309F         ; to ADD-REP-6
  15293.  
  15294. ;; TEST-NEG
  15295. L307C:  EXX                     ;
  15296.         LD      A,L             ;
  15297.         AND     $80             ;
  15298.         EXX                     ;
  15299.         INC     HL              ;
  15300.         LD      (HL),A          ;
  15301.         DEC     HL              ;
  15302.         JR      Z,L30A5         ; to GO-NC-MLT
  15303.  
  15304.         LD      A,E             ;
  15305.         NEG                     ; Negate
  15306.         CCF                     ; Complement Carry Flag
  15307.         LD      E,A             ;
  15308.         LD      A,D             ;
  15309.         CPL                     ;
  15310.         ADC     A,$00           ;
  15311.         LD      D,A             ;
  15312.         EXX                     ;
  15313.         LD      A,E             ;
  15314.         CPL                     ;
  15315.         ADC     A,$00           ;
  15316.         LD      E,A             ;
  15317.         LD      A,D             ;
  15318.         CPL                     ;
  15319.         ADC     A,$00           ;
  15320.         JR      NC,L30A3        ; to END-COMPL
  15321.  
  15322.         RRA                     ;
  15323.         EXX                     ;
  15324.         INC     (HL)            ;
  15325.  
  15326. ;; ADD-REP-6
  15327. L309F:  JP      Z,L31AD         ; to REPORT-6
  15328.  
  15329.         EXX                     ;
  15330.  
  15331. ;; END-COMPL
  15332. L30A3:  LD      D,A             ;
  15333.         EXX                     ;
  15334.  
  15335. ;; GO-NC-MLT
  15336. L30A5:  XOR     A               ;
  15337.         JP      L3155           ; to TEST-NORM
  15338.  
  15339. ; -----------------------------
  15340. ; Used in 16 bit multiplication
  15341. ; -----------------------------
  15342. ; This routine is used, in the first instance, by the multiply calculator
  15343. ; literal to perform an integer multiplication in preference to
  15344. ; 32-bit multiplication to which it will resort if this overflows.
  15345. ;
  15346. ; It is also used by STK-VAR to calculate array subscripts and by DIM to
  15347. ; calculate the space required for multi-dimensional arrays.
  15348.  
  15349. ;; HL-HL*DE
  15350. L30A9:  PUSH    BC              ; preserve BC throughout
  15351.         LD      B,$10           ; set B to 16
  15352.         LD      A,H             ; save H in A high byte
  15353.         LD      C,L             ; save L in C low byte
  15354.         LD      HL,$0000        ; initialize result to zero
  15355.  
  15356. ; now enter a loop.
  15357.  
  15358. ;; HL-LOOP
  15359. L30B1:  ADD     HL,HL           ; double result
  15360.         JR      C,L30BE         ; to HL-END if overflow
  15361.  
  15362.         RL      C               ; shift AC left into carry
  15363.         RLA                     ;
  15364.         JR      NC,L30BC        ; to HL-AGAIN to skip addition if no carry
  15365.  
  15366.         ADD     HL,DE           ; add in DE
  15367.         JR      C,L30BE         ; to HL-END if overflow
  15368.  
  15369. ;; HL-AGAIN
  15370. L30BC:  DJNZ    L30B1           ; back to HL-LOOP for all 16 bits
  15371.  
  15372. ;; HL-END
  15373. L30BE:  POP     BC              ; restore preserved BC
  15374.         RET                     ; return with carry reset if successful
  15375.                                 ; and result in HL.
  15376.  
  15377. ; -----------------------------
  15378. ; Prepare to multiply or divide
  15379. ; -----------------------------
  15380. ; This routine is called in succession from multiply and divide to prepare
  15381. ; two mantissas by setting the leftmost bit that is used for the sign.
  15382. ; On the first call A holds zero and picks up the sign bit. On the second
  15383. ; call the two bits are XORed to form the result sign - minus * minus giving
  15384. ; plus etc. If either number is zero then this is flagged.
  15385. ; HL addresses the exponent.
  15386.  
  15387. ;; PREP-M/D
  15388. L30C0:  CALL    L34E9           ; routine TEST-ZERO  preserves accumulator.
  15389.         RET     C               ; return carry set if zero
  15390.  
  15391.         INC     HL              ; address first byte of mantissa
  15392.         XOR     (HL)            ; pick up the first or xor with first.
  15393.         SET     7,(HL)          ; now set to give true 32-bit mantissa
  15394.         DEC     HL              ; point to exponent
  15395.         RET                     ; return with carry reset
  15396.  
  15397. ; --------------------------
  15398. ; Handle multiplication (04)
  15399. ; --------------------------
  15400. ;
  15401. ;
  15402.  
  15403. ;; multiply
  15404. L30CA:  LD      A,(DE)          ;
  15405.         OR      (HL)            ;
  15406.         JR      NZ,L30F0        ; to MULT-LONG
  15407.  
  15408.         PUSH    DE              ;
  15409.         PUSH    HL              ;
  15410.         PUSH    DE              ;
  15411.         CALL    L2D7F           ; routine INT-FETCH
  15412.         EX      DE,HL           ;
  15413.         EX      (SP),HL         ;
  15414.         LD      B,C             ;
  15415.         CALL    L2D7F           ; routine INT-FETCH
  15416.         LD      A,B             ;
  15417.         XOR     C               ;
  15418.         LD      C,A             ;
  15419.         POP     HL              ;
  15420.         CALL    L30A9           ; routine HL-HL*DE
  15421.         EX      DE,HL           ;
  15422.         POP     HL              ;
  15423.         JR      C,L30EF         ; to MULT-OFLW
  15424.  
  15425.         LD      A,D             ;
  15426.         OR      E               ;
  15427.         JR      NZ,L30EA        ; to MULT-RSLT
  15428.  
  15429.         LD      C,A             ;
  15430.  
  15431. ;; MULT-RSLT
  15432. L30EA:  CALL    L2D8E           ; routine INT-STORE
  15433.         POP      DE             ;
  15434.         RET                     ;
  15435.  
  15436. ; ---
  15437.  
  15438. ;; MULT-OFLW
  15439. L30EF:  POP     DE              ;
  15440.  
  15441. ;; MULT-LONG
  15442. L30F0:  CALL    L3293           ; routine RE-ST-TWO
  15443.         XOR     A               ;
  15444.         CALL    L30C0           ; routine PREP-M/D
  15445.         RET     C               ;
  15446.  
  15447.         EXX                     ;
  15448.         PUSH    HL              ;
  15449.         EXX                     ;
  15450.         PUSH    DE              ;
  15451.         EX      DE,HL           ;
  15452.         CALL    L30C0           ; routine PREP-M/D
  15453.         EX      DE,HL           ;
  15454.         JR      C,L315D         ; to ZERO-RSLT
  15455.  
  15456.         PUSH    HL              ;
  15457.         CALL    L2FBA           ; routine FETCH-TWO
  15458.         LD      A,B             ;
  15459.         AND     A               ;
  15460.         SBC     HL,HL           ;
  15461.         EXX                     ;
  15462.         PUSH    HL              ;
  15463.         SBC     HL,HL           ;
  15464.         EXX                     ;
  15465.         LD      B,$21           ;
  15466.         JR      L3125           ; to STRT-MLT
  15467.  
  15468. ; ---
  15469.  
  15470. ;; MLT-LOOP
  15471. L3114:  JR      NC,L311B        ; to NO-ADD
  15472.  
  15473.         ADD     HL,DE           ;
  15474.         EXX                     ;
  15475.         ADC     HL,DE           ;
  15476.         EXX                     ;
  15477.  
  15478. ;; NO-ADD
  15479. L311B:  EXX                     ;
  15480.         RR      H               ;
  15481.         RR      L               ;
  15482.         EXX                     ;
  15483.         RR      H               ;
  15484.         RR      L               ;
  15485.  
  15486. ;; STRT-MLT
  15487. L3125:  EXX                     ;
  15488.         RR      B               ;
  15489.         RR      C               ;
  15490.         EXX                     ;
  15491.         RR      C               ;
  15492.         RRA                     ;
  15493.         DJNZ    L3114           ; to MLT-LOOP
  15494.  
  15495.         EX      DE,HL           ;
  15496.         EXX                     ;
  15497.         EX      DE,HL           ;
  15498.         EXX                     ;
  15499.         POP     BC              ;
  15500.         POP     HL              ;
  15501.         LD      A,B             ;
  15502.         ADD     A,C             ;
  15503.         JR      NZ,L313B        ; to MAKE-EXPT
  15504.  
  15505.         AND     A               ;
  15506.  
  15507. ;; MAKE-EXPT
  15508. L313B:  DEC     A               ;
  15509.         CCF                     ; Complement Carry Flag
  15510.  
  15511. ;; DIVN-EXPT
  15512. L313D:  RLA                     ;
  15513.         CCF                     ; Complement Carry Flag
  15514.         RRA                     ;
  15515.         JP      P,L3146         ; to OFLW1-CLR
  15516.  
  15517.         JR      NC,L31AD        ; to REPORT-6
  15518.  
  15519.         AND     A               ;
  15520.  
  15521. ;; OFLW1-CLR
  15522. L3146:  INC     A               ;
  15523.         JR      NZ,L3151        ; to OFLW2-CLR
  15524.  
  15525.         JR      C,L3151         ; to OFLW2-CLR
  15526.  
  15527.         EXX                     ;
  15528.         BIT     7,D             ;
  15529.         EXX                     ;
  15530.         JR      NZ,L31AD        ; to REPORT-6
  15531.  
  15532. ;; OFLW2-CLR
  15533. L3151:  LD      (HL),A          ;
  15534.         EXX                     ;
  15535.         LD      A,B             ;
  15536.         EXX                     ;
  15537.  
  15538. ;; TEST-NORM
  15539. L3155:  JR      NC,L316C        ; to NORMALISE
  15540.  
  15541.         LD      A,(HL)          ;
  15542.         AND     A               ;
  15543.  
  15544. ;; NEAR-ZERO
  15545. L3159:  LD      A,$80           ;
  15546.         JR      Z,L315E         ; to SKIP-ZERO
  15547.  
  15548. ;; ZERO-RSLT
  15549. L315D:  XOR     A               ;
  15550.  
  15551. ;; SKIP-ZERO
  15552. L315E:  EXX                     ;
  15553.         AND     D               ;
  15554.         CALL    L2FFB           ; routine ZEROS-4/5
  15555.         RLCA                    ;
  15556.         LD      (HL),A          ;
  15557.         JR      C,L3195         ; to OFLOW-CLR
  15558.  
  15559.         INC     HL              ;
  15560.         LD      (HL),A          ;
  15561.         DEC     HL              ;
  15562.         JR      L3195           ; to OFLOW-CLR
  15563.  
  15564. ; ---
  15565.  
  15566. ;; NORMALISE
  15567. L316C:  LD      B,$20           ;
  15568.  
  15569. ;; SHIFT-ONE
  15570. L316E:  EXX                     ;
  15571.         BIT     7,D             ;
  15572.         EXX                     ;
  15573.         JR      NZ,L3186        ; to NORML-NOW
  15574.  
  15575.         RLCA                    ;
  15576.         RL      E               ;
  15577.         RL      D               ;
  15578.         EXX                     ;
  15579.         RL      E               ;
  15580.         RL      D               ;
  15581.         EXX                     ;
  15582.         DEC     (HL)            ;
  15583.         JR      Z,L3159         ; to NEAR-ZERO
  15584.  
  15585.         DJNZ    L316E           ; to SHIFT-ONE
  15586.  
  15587.         JR      L315D           ; to ZERO-RSLT
  15588.  
  15589. ; ---
  15590.  
  15591. ;; NORML-NOW
  15592. L3186:  RLA                     ;
  15593.         JR      NC,L3195        ; to OFLOW-CLR
  15594.  
  15595.         CALL    L3004           ; routine ADD-BACK
  15596.         JR      NZ,L3195        ; to OFLOW-CLR
  15597.  
  15598.         EXX                     ;
  15599.         LD       D,$80          ;
  15600.         EXX                     ;
  15601.         INC     (HL)            ;
  15602.         JR      Z,L31AD         ; to REPORT-6
  15603.  
  15604. ;; OFLOW-CLR
  15605. L3195:  PUSH    HL              ;
  15606.         INC     HL              ;
  15607.         EXX                     ;
  15608.         PUSH    DE              ;
  15609.         EXX                     ;
  15610.         POP     BC              ;
  15611.         LD      A,B             ;
  15612.         RLA                     ;
  15613.         RL      (HL)            ;
  15614.         RRA                     ;
  15615.         LD      (HL),A          ;
  15616.         INC     HL              ;
  15617.         LD      (HL),C          ;
  15618.         INC     HL              ;
  15619.         LD      (HL),D          ;
  15620.         INC     HL              ;
  15621.         LD      (HL),E          ;
  15622.         POP     HL              ;
  15623.         POP     DE              ;
  15624.         EXX                     ;
  15625.         POP     HL              ;
  15626.         EXX                     ;
  15627.         RET                     ;
  15628.  
  15629. ; ---
  15630.  
  15631. ;; REPORT-6
  15632. L31AD:  RST     08H             ; ERROR-1
  15633.         DB    $05             ; Error Report: Number too big
  15634.  
  15635. ; --------------------
  15636. ; Handle division (05)
  15637. ; --------------------
  15638. ;
  15639. ;
  15640.  
  15641. ;; division
  15642. L31AF:  CALL    L3293           ; routine RE-ST-TWO
  15643.         EX      DE,HL           ;
  15644.         XOR     A               ;
  15645.         CALL    L30C0           ; routine PREP-M/D
  15646.         JR      C,L31AD         ; to REPORT-6
  15647.  
  15648.         EX      DE,HL           ;
  15649.         CALL    L30C0           ; routine PREP-M/D
  15650.         RET     C               ;
  15651.  
  15652.         EXX                     ;
  15653.         PUSH    HL              ;
  15654.         EXX                     ;
  15655.         PUSH    DE              ;
  15656.         PUSH    HL              ;
  15657.         CALL    L2FBA           ; routine FETCH-TWO
  15658.         EXX                     ;
  15659.         PUSH    HL              ;
  15660.         LD      H,B             ;
  15661.         LD      L,C             ;
  15662.         EXX                     ;
  15663.         LD      H,C             ;
  15664.         LD      L,B             ;
  15665.         XOR     A               ;
  15666.         LD      B,$DF           ;
  15667.         JR      L31E2           ; to DIV-START
  15668.  
  15669. ; ---
  15670.  
  15671. ;; DIV-LOOP
  15672. L31D2:  RLA                     ;
  15673.         RL      C               ;
  15674.         EXX                     ;
  15675.         RL      C               ;
  15676.         RL      B               ;
  15677.         EXX                     ;
  15678.  
  15679. ;; div-34th
  15680. L31DB:  ADD     HL,HL           ;
  15681.         EXX                     ;
  15682.         ADC     HL,HL           ;
  15683.         EXX                     ;
  15684.         JR      C,L31F2         ; to SUBN-ONLY
  15685.  
  15686. ;; DIV-START
  15687. L31E2:  SBC     HL,DE           ;
  15688.         EXX                     ;
  15689.         SBC     HL,DE           ;
  15690.         EXX                     ;
  15691.         JR      NC,L31F9        ; to NO-RSTORE
  15692.  
  15693.         ADD     HL,DE           ;
  15694.         EXX                     ;
  15695.         ADC     HL,DE           ;
  15696.         EXX                     ;
  15697.         AND     A               ;
  15698.         JR      L31FA           ; to COUNT-ONE
  15699.  
  15700. ; ---
  15701.  
  15702. ;; SUBN-ONLY
  15703. L31F2:  AND     A               ;
  15704.         SBC     HL,DE           ;
  15705.         EXX                     ;
  15706.         SBC     HL,DE           ;
  15707.         EXX                     ;
  15708.  
  15709. ;; NO-RSTORE
  15710. L31F9:  SCF                     ; Set Carry Flag
  15711.  
  15712. ;; COUNT-ONE
  15713. L31FA:  INC     B               ;
  15714.         JP      M,L31D2         ; to DIV-LOOP
  15715.  
  15716.         PUSH    AF              ;
  15717.         JR      Z,L31E2         ; to DIV-START
  15718.  
  15719. ;
  15720. ;
  15721. ;
  15722. ;
  15723.  
  15724.         LD      E,A             ;
  15725.         LD      D,C             ;
  15726.         EXX                     ;
  15727.         LD      E,C             ;
  15728.         LD      D,B             ;
  15729.         POP     AF              ;
  15730.         RR      B               ;
  15731.         POP     AF              ;
  15732.         RR      B               ;
  15733.         EXX                     ;
  15734.         POP     BC              ;
  15735.         POP     HL              ;
  15736.         LD      A,B             ;
  15737.         SUB     C               ;
  15738.         JP      L313D           ; jump back to DIVN-EXPT
  15739.  
  15740. ; ------------------------------------
  15741. ; Integer truncation towards zero ($3A)
  15742. ; ------------------------------------
  15743. ;
  15744. ;
  15745.  
  15746. ;; truncate
  15747. L3214:  LD      A,(HL)          ;
  15748.         AND     A               ;
  15749.         RET     Z               ;
  15750.  
  15751.         CP      $81             ;
  15752.         JR      NC,L3221        ; to T-GR-ZERO
  15753.  
  15754.         LD      (HL),$00        ;
  15755.         LD      A,$20           ;
  15756.         JR      L3272           ; to NIL-BYTES
  15757.  
  15758. ; ---
  15759.  
  15760. ;; T-GR-ZERO
  15761. L3221:  CP      $91             ;
  15762.         JR      NZ,L323F        ; to T-SMALL
  15763.  
  15764.         INC     HL              ;
  15765.         INC     HL              ;
  15766.         INC     HL              ;
  15767.         LD      A,$80           ;
  15768.         AND     (HL)            ;
  15769.         DEC      HL             ;
  15770.         OR      (HL)            ;
  15771.         DEC     HL              ;
  15772.         JR      NZ,L3233        ; to T-FIRST
  15773.  
  15774.         LD      A,$80           ;
  15775.         XOR     (HL)            ;
  15776.  
  15777. ;; T-FIRST
  15778. L3233:  DEC     HL              ;
  15779.         JR      NZ,L326C        ; to T-EXPNENT
  15780.  
  15781.         LD      (HL),A          ;
  15782.         INC     HL              ;
  15783.         LD      (HL),$FF        ;
  15784.         DEC     HL              ;
  15785.         LD      A,$18           ;
  15786.         JR      L3272           ; to NIL-BYTES
  15787.  
  15788. ; ---
  15789.  
  15790. ;; T-SMALL
  15791. L323F:  JR      NC,L326D        ; to X-LARGE
  15792.  
  15793.         PUSH    DE              ;
  15794.         CPL                     ;
  15795.         ADD     A,$91           ;
  15796.         INC     HL              ;
  15797.         LD      D,(HL)          ;
  15798.         INC     HL              ;
  15799.         LD      E,(HL)          ;
  15800.         DEC     HL              ;
  15801.         DEC     HL              ;
  15802.         LD      C,$00           ;
  15803.         BIT     7,D             ;
  15804.         JR      Z,L3252         ; to T-NUMERIC
  15805.  
  15806.         DEC     C               ;
  15807.  
  15808. ;; T-NUMERIC
  15809. L3252:  SET     7,D             ;
  15810.         LD      B,$08           ;
  15811.         SUB     B               ;
  15812.         ADD     A,B             ;
  15813.         JR      C,L325E         ; to T-TEST
  15814.  
  15815.         LD      E,D             ;
  15816.         LD      D,$00           ;
  15817.         SUB     B               ;
  15818.  
  15819. ;; T-TEST
  15820. L325E:  JR      Z,L3267         ; to T-STORE
  15821.  
  15822.         LD      B,A             ;
  15823.  
  15824. ;; T-SHIFT
  15825. L3261:  SRL     D               ;
  15826.         RR      E               ;
  15827.         DJNZ    L3261           ; to T-SHIFT
  15828.  
  15829. ;; T-STORE
  15830. L3267:  CALL    L2D8E           ; routine INT-STORE
  15831.         POP     DE              ;
  15832.         RET                     ;
  15833.  
  15834. ; ---
  15835.  
  15836. ;; T-EXPNENT
  15837. L326C:  LD      A,(HL)          ;
  15838.  
  15839. ;; X-LARGE
  15840. L326D:  SUB     $A0             ;
  15841.         RET     P               ;
  15842.  
  15843.         NEG                     ; Negate
  15844.  
  15845. ;; NIL-BYTES
  15846. L3272:  PUSH    DE              ;
  15847.         EX      DE,HL           ;
  15848.         DEC     HL              ;
  15849.         LD      B,A             ;
  15850.         SRL     B               ;
  15851.         SRL     B               ;
  15852.         SRL     B               ;
  15853.         JR      Z,L3283         ; to BITS-ZERO
  15854.  
  15855. ;; BYTE-ZERO
  15856. L327E:  LD      (HL),$00        ;
  15857.         DEC     HL              ;
  15858.         DJNZ    L327E           ; to BYTE-ZERO
  15859.  
  15860. ;; BITS-ZERO
  15861. L3283:  AND     $07             ;
  15862.         JR      Z,L3290         ; to IX-END
  15863.  
  15864.         LD      B,A             ;
  15865.         LD      A,$FF           ;
  15866.  
  15867. ;; LESS-MASK
  15868. L328A:  SLA     A               ;
  15869.         DJNZ    L328A           ; to LESS-MASK
  15870.  
  15871.         AND     (HL)            ;
  15872.         LD      (HL),A          ;
  15873.  
  15874. ;; IX-END
  15875. L3290:  EX      DE,HL           ;
  15876.         POP     DE              ;
  15877.         RET                     ;
  15878.  
  15879. ; ----------------------------------
  15880. ; Storage of numbers in 5 byte form.
  15881. ; ==================================
  15882. ; Both integers and floating-point numbers can be stored in five bytes.
  15883. ; Zero is a special case stored as 5 zeros.
  15884. ; For integers the form is
  15885. ; Byte 1 - zero,
  15886. ; Byte 2 - sign byte, $00 +ve, $FF -ve.
  15887. ; Byte 3 - Low byte of integer.
  15888. ; Byte 4 - High byte
  15889. ; Byte 5 - unused but always zero.
  15890. ;
  15891. ; it seems unusual to store the low byte first but it is just as easy either
  15892. ; way. Statistically it just increases the chances of trailing zeros which
  15893. ; is an advantage elsewhere in saving ROM code.
  15894. ;
  15895. ;             zero     sign     low      high    unused
  15896. ; So +1 is  00000000 00000000 00000001 00000000 00000000
  15897. ;
  15898. ; and -1 is 00000000 11111111 11111111 11111111 00000000
  15899. ;
  15900. ; much of the arithmetic found in BASIC lines can be done using numbers
  15901. ; in this form using the Z80's 16 bit register operation ADD.
  15902. ; (multiplication is done by a sequence of additions).
  15903. ;
  15904. ; Storing -ve integers in two's complement form, means that they are ready for
  15905. ; addition and you might like to add the numbers above to prove that the
  15906. ; answer is zero. If, as in this case, the carry is set then that denotes that
  15907. ; the result is positive. This only applies when the signs don't match.
  15908. ; With positive numbers a carry denotes the result is out of integer range.
  15909. ; With negative numbers a carry denotes the result is within range.
  15910. ; The exception to the last rule is when the result is -65536
  15911. ;
  15912. ; Floating point form is an alternative method of storing numbers which can
  15913. ; be used for integers and larger (or fractional) numbers.
  15914. ;
  15915. ; In this form 1 is stored as
  15916. ;           10000001 00000000 00000000 00000000 00000000
  15917. ;
  15918. ; When a small integer is converted to a floating point number the last two
  15919. ; bytes are always blank so they are omitted in the following steps
  15920. ;
  15921. ; first make exponent +1 +16d  (bit 7 of the exponent is set if positive)
  15922.  
  15923. ; 10010001 00000000 00000001
  15924. ; 10010000 00000000 00000010 <-  now shift left and decrement exponent
  15925. ; ...
  15926. ; 10000010 01000000 00000000 <-  until a 1 abuts the imaginary point
  15927. ; 10000001 10000000 00000000     to the left of the mantissa.
  15928. ;
  15929. ; however since the leftmost bit of the mantissa is always set then it can
  15930. ; be used to denote the sign of the mantissa and put back when needed by the
  15931. ; PREP routines which gives
  15932. ;
  15933. ; 10000001 00000000 00000000
  15934.  
  15935. ; -----------------------------
  15936. ; Re-stack two `small' integers
  15937. ; -----------------------------
  15938. ; This routine is called to re-stack two numbers in full floating point form
  15939. ; e.g. from mult when integer multiplication has overflowed.
  15940.  
  15941. ;; RE-ST-TWO
  15942. L3293:  CALL    L3296           ; routine RESTK-SUB  below and continue
  15943.                                 ; into the routine to do the other one.
  15944.  
  15945. ;; RESTK-SUB
  15946. L3296:  EX      DE,HL           ; swap pointers
  15947.  
  15948. ; --------------------------------
  15949. ; Re-stack one number in full form
  15950. ; --------------------------------
  15951. ; This routine re-stacks an integer usually on the calculator stack
  15952. ; in full floating point form.
  15953. ; HL points to first byte.
  15954.  
  15955. ;; re-stack
  15956. L3297:  LD      A,(HL)          ; Fetch Exponent byte to A
  15957.         AND     A               ; test it
  15958.         RET     NZ              ; return if not zero as already in full
  15959.                                 ; floating-point form.
  15960.  
  15961.         PUSH    DE              ; preserve DE.
  15962.         CALL    L2D7F           ; routine INT-FETCH
  15963.                                 ; integer to DE, sign to C.
  15964.  
  15965. ; HL points to 4th byte.
  15966.  
  15967.         XOR     A               ; clear accumulator.
  15968.         INC     HL              ; point to 5th.
  15969.         LD      (HL),A          ; and blank.
  15970.         DEC     HL              ; point to 4th.
  15971.         LD      (HL),A          ; and blank.
  15972.  
  15973.         LD      B,$91           ; set exponent byte +ve $81
  15974.                                 ; and imaginary dec point 16 bits to right
  15975.                                 ; of first bit.
  15976.  
  15977. ; we could skip to normalize now but it's quicker to avoid
  15978. ; normalizing through an empty D.
  15979.  
  15980.         LD      A,D             ; fetch the high byte D
  15981.         AND     A               ; is it zero ?
  15982.         JR      NZ,L32B1        ; skip to RS-NRMLSE if not.
  15983.  
  15984.         OR      E               ; low byte E to A and test for zero
  15985.         LD      B,D             ; set B exponent to 0
  15986.         JR      Z,L32BD         ; forward to RS-STORE if value is zero.
  15987.  
  15988.         LD      D,E             ; transfer E to D
  15989.         LD      E,B             ; set E to 0
  15990.         LD      B,$89           ; reduce the initial exponent by eight.
  15991.  
  15992.  
  15993. ;; RS-NRMLSE
  15994. L32B1:  EX      DE,HL           ; integer to HL, addr of 4th byte to DE.
  15995.  
  15996. ;; RSTK-LOOP
  15997. L32B2:  DEC     B               ; decrease exponent
  15998.         ADD     HL,HL           ; shift DE left
  15999.         JR      NC,L32B2        ; loop back to RSTK-LOOP
  16000.                                 ; until a set bit pops into carry
  16001.  
  16002.         RRC     C               ; now rotate the sign byte $00 or $FF
  16003.                                 ; into carry to give a sign bit
  16004.  
  16005.         RR      H               ; rotate the sign bit to left of H
  16006.         RR      L               ; rotate any carry into L
  16007.  
  16008.         EX      DE,HL           ; address 4th byte, normalized int to DE
  16009.  
  16010. ;; RS-STORE
  16011. L32BD:  DEC     HL              ; address 3rd byte
  16012.         LD      (HL),E          ; place E
  16013.         DEC     HL              ; address 2nd byte
  16014.         LD      (HL),D          ; place D
  16015.         DEC     HL              ; address 1st byte
  16016.         LD      (HL),B          ; store the exponent
  16017.  
  16018.         POP     DE              ; restore initial DE.
  16019.         RET                     ; return.
  16020.  
  16021. ;****************************************
  16022. ;** Part 10. FLOATING-POINT CALCULATOR **
  16023. ;****************************************
  16024.  
  16025. ; As a general rule the calculator avoids using the IY register.
  16026. ; exceptions are val, val$ and str$.
  16027. ; So an assembly language programmer who has disabled interrupts to use
  16028. ; IY for other purposes can still use the calculator for mathematical
  16029. ; purposes.
  16030.  
  16031.  
  16032. ; ------------------
  16033. ; Table of constants
  16034. ; ------------------
  16035. ;
  16036. ;
  16037.  
  16038. ; used 11 times
  16039. ;; stk-zero                                                 00 00 00 00 00
  16040. L32C5:  DB    $00             ;;Bytes: 1
  16041.         DB    $B0             ;;Exponent $00
  16042.         DB    $00             ;;(+00,+00,+00)
  16043.  
  16044. ; used 19 times
  16045. ;; stk-one                                                  00 00 01 00 00
  16046. L32C8:  DB    $40             ;;Bytes: 2
  16047.         DB    $B0             ;;Exponent $00
  16048.         DB    $00,$01         ;;(+00,+00)
  16049.  
  16050. ; used 9 times
  16051. ;; stk-half                                                 80 00 00 00 00
  16052. L32CC:  DB    $30             ;;Exponent: $80, Bytes: 1
  16053.         DB    $00             ;;(+00,+00,+00)
  16054.  
  16055. ; used 4 times.
  16056. ;; stk-pi/2                                                 81 49 0F DA A2
  16057. L32CE:  DB    $F1             ;;Exponent: $81, Bytes: 4
  16058.         DB    $49,$0F,$DA,$A2 ;;
  16059.  
  16060. ; used 3 times.
  16061. ;; stk-ten                                                  00 00 0A 00 00
  16062. L32D3:  DB    $40             ;;Bytes: 2
  16063.         DB    $B0             ;;Exponent $00
  16064.         DB    $00,$0A         ;;(+00,+00)
  16065.  
  16066.  
  16067. ; ------------------
  16068. ; Table of addresses
  16069. ; ------------------
  16070. ;
  16071. ; starts with binary operations which have two operands and one result.
  16072. ; three pseudo binary operations first.
  16073.  
  16074. ;; tbl-addrs
  16075. L32D7:  DEFW    L368F           ; $00 Address: $368F - jump-true
  16076.         DEFW    L343C           ; $01 Address: $343C - exchange
  16077.         DEFW    L33A1           ; $02 Address: $33A1 - delete
  16078.  
  16079. ; true binary operations.
  16080.  
  16081.         DEFW    L300F           ; $03 Address: $300F - subtract
  16082.         DEFW    L30CA           ; $04 Address: $30CA - multiply
  16083.         DEFW    L31AF           ; $05 Address: $31AF - division
  16084.         DEFW    L3851           ; $06 Address: $3851 - to-power
  16085.         DEFW    L351B           ; $07 Address: $351B - or
  16086.  
  16087.         DEFW    L3524           ; $08 Address: $3524 - no-&-no
  16088.         DEFW    L353B           ; $09 Address: $353B - no-l-eql
  16089.         DEFW    L353B           ; $0A Address: $353B - no-gr-eql
  16090.         DEFW    L353B           ; $0B Address: $353B - nos-neql
  16091.         DEFW    L353B           ; $0C Address: $353B - no-grtr
  16092.         DEFW    L353B           ; $0D Address: $353B - no-less
  16093.         DEFW    L353B           ; $0E Address: $353B - nos-eql
  16094.         DEFW    L3014           ; $0F Address: $3014 - addition
  16095.  
  16096.         DEFW    L352D           ; $10 Address: $352D - str-&-no
  16097.         DEFW    L353B           ; $11 Address: $353B - str-l-eql
  16098.         DEFW    L353B           ; $12 Address: $353B - str-gr-eql
  16099.         DEFW    L353B           ; $13 Address: $353B - strs-neql
  16100.         DEFW    L353B           ; $14 Address: $353B - str-grtr
  16101.         DEFW    L353B           ; $15 Address: $353B - str-less
  16102.         DEFW    L353B           ; $16 Address: $353B - strs-eql
  16103.         DEFW    L359C           ; $17 Address: $359C - strs-add
  16104.  
  16105. ; unary follow
  16106.  
  16107.         DEFW    L35DE           ; $18 Address: $35DE - val$
  16108.         DEFW    L34BC           ; $19 Address: $34BC - usr-$
  16109.         DEFW    L3645           ; $1A Address: $3645 - read-in
  16110.         DEFW    L346E           ; $1B Address: $346E - negate
  16111.  
  16112.         DEFW    L3669           ; $1C Address: $3669 - code
  16113.         DEFW    L35DE           ; $1D Address: $35DE - val
  16114.         DEFW    L3674           ; $1E Address: $3674 - len
  16115.         DEFW    L37B5           ; $1F Address: $37B5 - sin
  16116.         DEFW    L37AA           ; $20 Address: $37AA - cos
  16117.         DEFW    L37DA           ; $21 Address: $37DA - tan
  16118.         DEFW    L3833           ; $22 Address: $3833 - asn
  16119.         DEFW    L3843           ; $23 Address: $3843 - acs
  16120.         DEFW    L37E2           ; $24 Address: $37E2 - atn
  16121.         DEFW    L3713           ; $25 Address: $3713 - ln
  16122.         DEFW    L36C4           ; $26 Address: $36C4 - exp
  16123.         DEFW    L36AF           ; $27 Address: $36AF - int
  16124.         DEFW    L384A           ; $28 Address: $384A - sqr
  16125.         DEFW    L3492           ; $29 Address: $3492 - sgn
  16126.         DEFW    L346A           ; $2A Address: $346A - abs
  16127.         DEFW    L34AC           ; $2B Address: $34AC - peek
  16128.         DEFW    L34A5           ; $2C Address: $34A5 - in
  16129.         DEFW    L34B3           ; $2D Address: $34B3 - usr-no
  16130.         DEFW    L361F           ; $2E Address: $361F - str$
  16131.         DEFW    L35C9           ; $2F Address: $35C9 - chrs
  16132.         DEFW    L3501           ; $30 Address: $3501 - not
  16133.  
  16134. ; end of true unary
  16135.  
  16136.         DEFW    L33C0           ; $31 Address: $33C0 - duplicate
  16137.         DEFW    L36A0           ; $32 Address: $36A0 - n-mod-m
  16138.         DEFW    L3686           ; $33 Address: $3686 - jump
  16139.         DEFW    L33C6           ; $34 Address: $33C6 - stk-data
  16140.         DEFW    L367A           ; $35 Address: $367A - dec-jr-nz
  16141.         DEFW    L3506           ; $36 Address: $3506 - less-0
  16142.         DEFW    L34F9           ; $37 Address: $34F9 - greater-0
  16143.         DEFW    L369B           ; $38 Address: $369B - end-calc
  16144.         DEFW    L3783           ; $39 Address: $3783 - get-argt
  16145.         DEFW    L3214           ; $3A Address: $3214 - truncate
  16146.         DEFW    L33A2           ; $3B Address: $33A2 - fp-calc-2
  16147.         DEFW    L2D4F           ; $3C Address: $2D4F - e-to-fp
  16148.         DEFW    L3297           ; $3D Address: $3297 - re-stack
  16149.  
  16150. ; the following are just the next available slots for the 128 compound literals
  16151. ; which are in range $80 - $FF.
  16152.  
  16153.         DEFW    L3449           ; $3E Address: $3449 - series-xx    $80 - $9F.
  16154.         DEFW    L341B           ; $3F Address: $341B - stk-const-xx $A0 - $BF.
  16155.         DEFW    L342D           ; $40 Address: $342D - st-mem-xx    $C0 - $DF.
  16156.         DEFW    L340F           ; $41 Address: $340F - get-mem-xx   $E0 - $FF.
  16157.  
  16158. ; Aside: 3E - 7F are therefore unused calculator literals.
  16159. ;        3E - 7B would be available for expansion.
  16160.  
  16161. ; --------------
  16162. ; The Calculator
  16163. ; --------------
  16164. ;
  16165. ;
  16166.  
  16167. ;; CALCULATE
  16168. L335B:  CALL    L35BF           ; routine STK-PNTRS is called to set up the
  16169.                                 ; calculator stack pointers for a default
  16170.                                 ; unary operation. HL = last value on stack.
  16171.                                 ; DE = STKEND first location after stack.
  16172.  
  16173. ; the calculate routine is called at this point by the series generator...
  16174.  
  16175. ;; GEN-ENT-1
  16176. L335E:  LD      A,B             ; fetch the Z80 B register to A
  16177.         LD      ($5C67),A       ; and store value in system variable BREG.
  16178.                                 ; this will be the counter for dec-jr-nz
  16179.                                 ; or if used from fp-calc2 the calculator
  16180.                                 ; instruction.
  16181.  
  16182. ; ... and again later at this point
  16183.  
  16184. ;; GEN-ENT-2
  16185. L3362:  EXX                     ; switch sets
  16186.         EX      (SP),HL         ; and store the address of next instruction,
  16187.                                 ; the return address, in H'L'.
  16188.                                 ; If this is a recursive call the the H'L'
  16189.                                 ; of the previous invocation goes on stack.
  16190.                                 ; c.f. end-calc.
  16191.         EXX                     ; switch back to main set
  16192.  
  16193. ; this is the re-entry looping point when handling a string of literals.
  16194.  
  16195. ;; RE-ENTRY
  16196. L3365:  LD      ($5C65),DE      ; save end of stack in system variable STKEND
  16197.         EXX                     ; switch to alt
  16198.         LD      A,(HL)          ; get next literal
  16199.         INC     HL              ; increase pointer'
  16200.  
  16201. ; single operation jumps back to here
  16202.  
  16203. ;; SCAN-ENT
  16204. L336C:  PUSH    HL              ; save pointer on stack
  16205.         AND     A               ; now test the literal
  16206.         JP      P,L3380         ; forward to FIRST-3D if in range $00 - $3D
  16207.                                 ; anything with bit 7 set will be one of
  16208.                                 ; 128 compound literals.
  16209.  
  16210. ; compound literals have the following format.
  16211. ; bit 7 set indicates compound.
  16212. ; bits 6-5 the subgroup 0-3.
  16213. ; bits 4-0 the embedded parameter $00 - $1F.
  16214. ; The subgroup 0-3 needs to be manipulated to form the next available four
  16215. ; address places after the simple literals in the address table.
  16216.  
  16217.         LD      D,A             ; save literal in D
  16218.         AND     $60             ; and with 01100000 to isolate subgroup
  16219.         RRCA                    ; rotate bits
  16220.         RRCA                    ; 4 places to right
  16221.         RRCA                    ; not five as we need offset * 2
  16222.         RRCA                    ; 00000xx0
  16223.         ADD     A,$7C           ; add ($3E * 2) to give correct offset.
  16224.                                 ; alter above if you add more literals.
  16225.         LD      L,A             ; store in L for later indexing.
  16226.         LD      A,D             ; bring back compound literal
  16227.         AND     $1F             ; use mask to isolate parameter bits
  16228.         JR      L338E           ; forward to ENT-TABLE
  16229.  
  16230. ; ---
  16231.  
  16232. ; the branch was here with simple literals.
  16233.  
  16234. ;; FIRST-3D
  16235. L3380:  CP      $18             ; compare with first unary operations.
  16236.         JR      NC,L338C        ; to DOUBLE-A with unary operations
  16237.  
  16238. ; it is binary so adjust pointers.
  16239.  
  16240.         EXX                     ;
  16241.         LD      BC,$FFFB        ; the value -5
  16242.         LD      D,H             ; transfer HL, the last value, to DE.
  16243.         LD      E,L             ;
  16244.         ADD     HL,BC           ; subtract 5 making HL point to second
  16245.                                 ; value.
  16246.         EXX                     ;
  16247.  
  16248. ;; DOUBLE-A
  16249. L338C:  RLCA                    ; double the literal
  16250.         LD      L,A             ; and store in L for indexing
  16251.  
  16252. ;; ENT-TABLE
  16253. L338E:  LD      DE,L32D7        ; Address: tbl-addrs
  16254.         LD      H,$00           ; prepare to index
  16255.         ADD     HL,DE           ; add to get address of routine
  16256.         LD      E,(HL)          ; low byte to E
  16257.         INC     HL              ;
  16258.         LD      D,(HL)          ; high byte to D
  16259.         LD      HL,L3365        ; Address: RE-ENTRY
  16260.         EX      (SP),HL         ; goes to stack
  16261.         PUSH    DE              ; now address of routine
  16262.         EXX                     ; main set
  16263.                                 ; avoid using IY register.
  16264.         LD      BC,($5C66)      ; STKEND_hi
  16265.                                 ; nothing much goes to C but BREG to B
  16266.                                 ; and continue into next ret instruction
  16267.                                 ; which has a dual identity
  16268.  
  16269.  
  16270. ; ------------------
  16271. ; Handle delete (02)
  16272. ; ------------------
  16273. ; A simple return but when used as a calculator literal this
  16274. ; deletes the last value from the calculator stack.
  16275. ; On entry, as always with binary operations,
  16276. ; HL=first number, DE=second number
  16277. ; On exit, HL=result, DE=stkend.
  16278. ; So nothing to do
  16279.  
  16280. ;; delete
  16281. L33A1:  RET                     ; return - indirect jump if from above.
  16282.  
  16283. ; ---------------------
  16284. ; Single operation (3B)
  16285. ; ---------------------
  16286. ; this single operation is used, in the first instance, to evaluate most
  16287. ; of the mathematical and string functions found in BASIC expressions.
  16288.  
  16289. ;; fp-calc-2
  16290. L33A2:  POP     AF              ; drop return address.
  16291.         LD      A,($5C67)       ; load accumulator from system variable BREG
  16292.                                 ; value will be literal eg. 'tan'
  16293.         EXX                     ; switch to alt
  16294.         JR      L336C           ; back to SCAN-ENT
  16295.                                 ; next literal will be end-calc at L2758
  16296.  
  16297. ; ----------------
  16298. ; Test five-spaces
  16299. ; ----------------
  16300. ; This routine is called from MOVE-FP, STK-CONST and STK-STORE to
  16301. ; test that there is enough space between the calculator stack and the
  16302. ; machine stack for another five-byte value. It returns with BC holding
  16303. ; the value 5 ready for any subsequent LDIR.
  16304.  
  16305. ;; TEST-5-SP
  16306. L33A9:  PUSH    DE              ; save
  16307.         PUSH    HL              ; registers
  16308.         LD      BC,$0005        ; an overhead of five bytes
  16309.         CALL    L1F05           ; routine TEST-ROOM tests free RAM raising
  16310.                                 ; an error if not.
  16311.         POP     HL              ; else restore
  16312.         POP     DE              ; registers.
  16313.         RET                     ; return with BC set at 5.
  16314.  
  16315. ; ------------
  16316. ; Stack number
  16317. ; ------------
  16318. ; This routine is called to stack a hidden floating point number found in
  16319. ; a BASIC line. It is also called to stack a numeric variable value, and
  16320. ; from BEEP, to stack an entry in the semi-tone table. It is not part of the
  16321. ; calculator suite of routines.
  16322. ; On entry HL points to the number to be stacked.
  16323.  
  16324. ;; STACK-NUM
  16325. L33B4:  LD      DE,($5C65)      ; load destination from STKEND system variable.
  16326.         CALL    L33C0           ; routine MOVE-FP puts on calculator stack
  16327.                                 ; with a memory check.
  16328.         LD      ($5C65),DE      ; set STKEND to next free location.
  16329.         RET                     ; return.
  16330.  
  16331. ; ---------------------------------
  16332. ; Move a floating point number (31)
  16333. ; ---------------------------------
  16334. ; This simple routine is a 5-byte LDIR instruction
  16335. ; that incorporates a memory check.
  16336. ; When used as a calculator literal it duplicates the last value on the
  16337. ; calculator stack.
  16338. ; Unary so on entry HL points to last value, DE to stkend
  16339.  
  16340. ;; duplicate
  16341. ;; MOVE-FP
  16342. L33C0:  CALL    L33A9           ; routine TEST-5-SP test free memory
  16343.                                 ; and sets BC to 5.
  16344.         LDIR                    ; copy the five bytes.
  16345.         RET                     ; return with DE addressing new STKEND
  16346.                                 ; and HL addressing new last value.
  16347.  
  16348. ; -------------------
  16349. ; Stack literals ($34)
  16350. ; -------------------
  16351. ; When a calculator subroutine needs to put a value on the calculator
  16352. ; stack that is not a regular constant this routine is called with a
  16353. ; variable number of following data bytes that convey to the routine
  16354. ; the integer or floating point form as succinctly as is possible.
  16355.  
  16356. ;; stk-data
  16357. L33C6:  LD      H,D             ; transfer STKEND
  16358.         LD      L,E             ; to HL for result.
  16359.  
  16360. ;; STK-CONST
  16361. L33C8:  CALL    L33A9           ; routine TEST-5-SP tests that room exists
  16362.                                 ; and sets BC to $05.
  16363.  
  16364.         EXX                     ; switch to alternate set
  16365.         PUSH    HL              ; save the pointer to next literal on stack
  16366.         EXX                     ; switch back to main set
  16367.  
  16368.         EX      (SP),HL         ; pointer to HL, destination to stack.
  16369.  
  16370.         PUSH    BC              ; save BC - value 5 from test room ??.
  16371.  
  16372.         LD      A,(HL)          ; fetch the byte following 'stk-data'
  16373.         AND     $C0             ; isolate bits 7 and 6
  16374.         RLCA                    ; rotate
  16375.         RLCA                    ; to bits 1 and 0  range $00 - $03.
  16376.         LD      C,A             ; transfer to C
  16377.         INC     C               ; and increment to give number of bytes
  16378.                                 ; to read. $01 - $04
  16379.         LD      A,(HL)          ; reload the first byte
  16380.         AND     $3F             ; mask off to give possible exponent.
  16381.         JR      NZ,L33DE        ; forward to FORM-EXP if it was possible to
  16382.                                 ; include the exponent.
  16383.  
  16384. ; else byte is just a byte count and exponent comes next.
  16385.  
  16386.         INC     HL              ; address next byte and
  16387.         LD      A,(HL)          ; pick up the exponent ( - $50).
  16388.  
  16389. ;; FORM-EXP
  16390. L33DE:  ADD     A,$50           ; now add $50 to form actual exponent
  16391.         LD      (DE),A          ; and load into first destination byte.
  16392.         LD      A,$05           ; load accumulator with $05 and
  16393.         SUB     C               ; subtract C to give count of trailing
  16394.                                 ; zeros plus one.
  16395.         INC     HL              ; increment source
  16396.         INC     DE              ; increment destination
  16397.         LD      B,$00           ; prepare to copy
  16398.         LDIR                    ; copy C bytes
  16399.  
  16400.         POP     BC              ; restore 5 counter to BC ??.
  16401.  
  16402.         EX      (SP),HL         ; put HL on stack as next literal pointer
  16403.                                 ; and the stack value - result pointer -
  16404.                                 ; to HL.
  16405.  
  16406.         EXX                     ; switch to alternate set.
  16407.         POP     HL              ; restore next literal pointer from stack
  16408.                                 ; to H'L'.
  16409.         EXX                     ; switch back to main set.
  16410.  
  16411.         LD      B,A             ; zero count to B
  16412.         XOR     A               ; clear accumulator
  16413.  
  16414. ;; STK-ZEROS
  16415. L33F1:  DEC     B               ; decrement B counter
  16416.         RET     Z               ; return if zero.          >>
  16417.                                 ; DE points to new STKEND
  16418.                                 ; HL to new number.
  16419.  
  16420.         LD      (DE),A          ; else load zero to destination
  16421.         INC     DE              ; increase destination
  16422.         JR      L33F1           ; loop back to STK-ZEROS until done.
  16423.  
  16424. ; -------------------------------
  16425. ; THE 'SKIP CONSTANTS' SUBROUTINE
  16426. ; -------------------------------
  16427. ; This routine traverses variable-length entries in the table of constants,
  16428. ; stacking intermediate, unwanted constants onto a dummy calculator stack,
  16429. ; in the first five bytes of ROM. The destination DE normally points to the
  16430. ; end of the calculator stack which might be in the normal place or in the
  16431. ; system variables area during E-LINE-NO; INT-TO-FP; stk-ten. In any case,
  16432. ; it would be simpler all round if the routine just shoved unwanted values
  16433. ; where it is going to stick the wanted value.
  16434. ; The instruction LD DE, $0000 can be removed.
  16435.  
  16436. ;; SKIP-CONS
  16437. L33F7:  AND     A               ; test if initially zero.
  16438.  
  16439. ;; SKIP-NEXT
  16440. L33F8:  RET     Z               ; return if zero.          >>
  16441.  
  16442.         PUSH    AF              ; save count.
  16443.         PUSH    DE              ; and normal STKEND
  16444.  
  16445.         LD      DE,$0000        ; dummy value for STKEND at start of ROM
  16446.                                 ; Note. not a fault but this has to be
  16447.                                 ; moved elsewhere when running in RAM.
  16448.                                 ; e.g. with Expandor Systems 'Soft ROM'.
  16449.                                 ; Better still, write to the normal place.
  16450.         CALL    L33C8           ; routine STK-CONST works through variable
  16451.                                 ; length records.
  16452.  
  16453.         POP     DE              ; restore real STKEND
  16454.         POP     AF              ; restore count
  16455.         DEC     A               ; decrease
  16456.         JR      L33F8           ; loop back to SKIP-NEXT
  16457.  
  16458. ; ---------------
  16459. ; Memory location
  16460. ; ---------------
  16461. ; This routine, when supplied with a base address in HL and an index in A
  16462. ; will calculate the address of the A'th entry, where each entry occupies
  16463. ; five bytes. It is used for reading the semi-tone table and addressing
  16464. ; floating-point numbers in the calculator's memory area.
  16465.  
  16466. ;; LOC-MEM
  16467. L3406:  LD      C,A             ; store the original number $00-$1F.
  16468.         RLCA                    ; double.
  16469.         RLCA                    ; quadruple.
  16470.         ADD     A,C             ; now add original to multiply by five.
  16471.  
  16472.         LD      C,A             ; place the result in C.
  16473.         LD      B,$00           ; set B to 0.
  16474.         ADD     HL,BC           ; add to form address of start of number in HL.
  16475.         RET                     ; return.
  16476.  
  16477. ; ------------------------------
  16478. ; Get from memory area ($E0 etc.)
  16479. ; ------------------------------
  16480. ; Literals $E0 to $FF
  16481. ; A holds $00-$1F offset.
  16482. ; The calculator stack increases by 5 bytes.
  16483.  
  16484. ;; get-mem-xx
  16485. L340F:  PUSH    DE              ; save STKEND
  16486.         LD      HL,($5C68)      ; MEM is base address of the memory cells.
  16487.         CALL    L3406           ; routine LOC-MEM so that HL = first byte
  16488.         CALL    L33C0           ; routine MOVE-FP moves 5 bytes with memory
  16489.                                 ; check.
  16490.                                 ; DE now points to new STKEND.
  16491.         POP     HL              ; original STKEND is now RESULT pointer.
  16492.         RET                     ; return.
  16493.  
  16494. ; --------------------------
  16495. ; Stack a constant (A0 etc.)
  16496. ; --------------------------
  16497. ; This routine allows a one-byte instruction to stack up to 32 constants
  16498. ; held in short form in a table of constants. In fact only 5 constants are
  16499. ; required. On entry the A register holds the literal ANDed with 1F.
  16500. ; It isn't very efficient and it would have been better to hold the
  16501. ; numbers in full, five byte form and stack them in a similar manner
  16502. ; to that used for semi-tone table values.
  16503.  
  16504. ;; stk-const-xx
  16505. L341B:  LD      H,D             ; save STKEND - required for result
  16506.         LD      L,E             ;
  16507.         EXX                     ; swap
  16508.         PUSH    HL              ; save pointer to next literal
  16509.         LD      HL,L32C5        ; Address: stk-zero - start of table of
  16510.                                 ; constants
  16511.         EXX                     ;
  16512.         CALL    L33F7           ; routine SKIP-CONS
  16513.         CALL    L33C8           ; routine STK-CONST
  16514.         EXX                     ;
  16515.         POP     HL              ; restore pointer to next literal.
  16516.         EXX                     ;
  16517.         RET                     ; return.
  16518.  
  16519. ; --------------------------------
  16520. ; Store in a memory area ($C0 etc.)
  16521. ; --------------------------------
  16522. ; Offsets $C0 to $DF
  16523. ; Although 32 memory storage locations can be addressed, only six
  16524. ; $C0 to $C5 are required by the ROM and only the thirty bytes (6*5)
  16525. ; required for these are allocated. Spectrum programmers who wish to
  16526. ; use the floating point routines from assembly language may wish to
  16527. ; alter the system variable MEM to point to 160 bytes of RAM to have
  16528. ; use the full range available.
  16529. ; A holds the derived offset $00-$1F.
  16530. ; This is a unary operation, so on entry HL points to the last value and DE
  16531. ; points to STKEND.
  16532.  
  16533. ;; st-mem-xx
  16534. L342D:  PUSH    HL              ; save the result pointer.
  16535.         EX      DE,HL           ; transfer to DE.
  16536.         LD      HL,($5C68)      ; fetch MEM the base of memory area.
  16537.         CALL    L3406           ; routine LOC-MEM sets HL to the destination.
  16538.         EX      DE,HL           ; swap - HL is start, DE is destination.
  16539.         CALL    L33C0           ; routine MOVE-FP.
  16540.                                 ; note. a short ld bc,5; ldir
  16541.                                 ; the embedded memory check is not required
  16542.                                 ; so these instructions would be faster.
  16543.         EX      DE,HL           ; DE = STKEND
  16544.         POP     HL              ; restore original result pointer
  16545.         RET                     ; return.
  16546.  
  16547. ; ------------------------------------
  16548. ; Swap first number with second number
  16549. ; ------------------------------------
  16550. ; This routine exchanges the last two values on the calculator stack
  16551. ; On entry, as always with binary operations,
  16552. ; HL=first number, DE=second number
  16553. ; On exit, HL=result, DE=stkend.
  16554.  
  16555. ;; exchange
  16556. L343C:  LD      B,$05           ; there are five bytes to be swapped
  16557.  
  16558. ; start of loop.
  16559.  
  16560. ;; SWAP-BYTE
  16561. L343E:  LD      A,(DE)          ; each byte of second
  16562.         LD      C,(HL)          ; each byte of first
  16563.         EX      DE,HL           ; swap pointers
  16564.         LD      (DE),A          ; store each byte of first
  16565.         LD      (HL),C          ; store each byte of second
  16566.         INC     HL              ; advance both
  16567.         INC     DE              ; pointers.
  16568.         DJNZ    L343E           ; loop back to SWAP-BYTE until all 5 done.
  16569.  
  16570.         EX      DE,HL           ; even up the exchanges
  16571.                                 ; so that DE addresses STKEND.
  16572.         RET                     ; return.
  16573.  
  16574. ; --------------------------
  16575. ; Series generator (86 etc.)
  16576. ; --------------------------
  16577. ; The Spectrum uses Chebyshev polynomials to generate approximations for
  16578. ; SIN, ATN, LN and EXP. These are named after the Russian mathematician
  16579. ; Pafnuty Chebyshev, born in 1821, who did much pioneering work on numerical
  16580. ; series. As far as calculators are concerned, Chebyshev polynomials have an
  16581. ; advantage over other series, for example the Taylor series, as they can
  16582. ; reach an approximation in just six iterations for SIN, eight for EXP and
  16583. ; twelve for LN and ATN. The mechanics of the routine are interesting but
  16584. ; for full treatment of how these are generated with demonstrations in
  16585. ; Sinclair BASIC see "The Complete Spectrum ROM Disassembly" by Dr Ian Logan
  16586. ; and Dr Frank O'Hara, published 1983 by Melbourne House.
  16587.  
  16588. ;; series-xx
  16589. L3449:  LD      B,A             ; parameter $00 - $1F to B counter
  16590.         CALL    L335E           ; routine GEN-ENT-1 is called.
  16591.                                 ; A recursive call to a special entry point
  16592.                                 ; in the calculator that puts the B register
  16593.                                 ; in the system variable BREG. The return
  16594.                                 ; address is the next location and where
  16595.                                 ; the calculator will expect its first
  16596.                                 ; instruction - now pointed to by HL'.
  16597.                                 ; The previous pointer to the series of
  16598.                                 ; five-byte numbers goes on the machine stack.
  16599.  
  16600. ; The initialization phase.
  16601.  
  16602.         DB    $31             ;;duplicate       x,x
  16603.         DB    $0F             ;;addition        x+x
  16604.         DB    $C0             ;;st-mem-0        x+x
  16605.         DB    $02             ;;delete          .
  16606.         DB    $A0             ;;stk-zero        0
  16607.         DB    $C2             ;;st-mem-2        0
  16608.  
  16609. ; a loop is now entered to perform the algebraic calculation for each of
  16610. ; the numbers in the series
  16611.  
  16612. ;; G-LOOP
  16613. L3453:  DB    $31             ;;duplicate       v,v.
  16614.         DB    $E0             ;;get-mem-0       v,v,x+2
  16615.         DB    $04             ;;multiply        v,v*x+2
  16616.         DB    $E2             ;;get-mem-2       v,v*x+2,v
  16617.         DB    $C1             ;;st-mem-1
  16618.         DB    $03             ;;subtract
  16619.         DB    $38             ;;end-calc
  16620.  
  16621. ; the previous pointer is fetched from the machine stack to H'L' where it
  16622. ; addresses one of the numbers of the series following the series literal.
  16623.  
  16624.         CALL    L33C6           ; routine STK-DATA is called directly to
  16625.                                 ; push a value and advance H'L'.
  16626.         CALL    L3362           ; routine GEN-ENT-2 recursively re-enters
  16627.                                 ; the calculator without disturbing
  16628.                                 ; system variable BREG
  16629.                                 ; H'L' value goes on the machine stack and is
  16630.                                 ; then loaded as usual with the next address.
  16631.  
  16632.         DB    $0F             ;;addition
  16633.         DB    $01             ;;exchange
  16634.         DB    $C2             ;;st-mem-2
  16635.         DB    $02             ;;delete
  16636.  
  16637.         DB    $35             ;;dec-jr-nz
  16638.         DB    $EE             ;;back to L3453, G-LOOP
  16639.  
  16640. ; when the counted loop is complete the final subtraction yields the result
  16641. ; for example SIN X.
  16642.  
  16643.         DB    $E1             ;;get-mem-1
  16644.         DB    $03             ;;subtract
  16645.         DB    $38             ;;end-calc
  16646.  
  16647.         RET                     ; return with H'L' pointing to location
  16648.                                 ; after last number in series.
  16649.  
  16650. ; -----------------------
  16651. ; Absolute magnitude (2A)
  16652. ; -----------------------
  16653. ; This calculator literal finds the absolute value of the last value,
  16654. ; integer or floating point, on calculator stack.
  16655.  
  16656. ;; abs
  16657. L346A:  LD      B,$FF           ; signal abs
  16658.         JR      L3474           ; forward to NEG-TEST
  16659.  
  16660. ; -----------------------
  16661. ; Handle unary minus (1B)
  16662. ; -----------------------
  16663. ; Unary so on entry HL points to last value, DE to STKEND.
  16664.  
  16665. ;; NEGATE
  16666. ;; negate
  16667. L346E:  CALL    L34E9           ; call routine TEST-ZERO and
  16668.         RET     C               ; return if so leaving zero unchanged.
  16669.  
  16670.         LD      B,$00           ; signal negate required before joining
  16671.                                 ; common code.
  16672.  
  16673. ;; NEG-TEST
  16674. L3474:  LD      A,(HL)          ; load first byte and
  16675.         AND     A               ; test for zero
  16676.         JR      Z,L3483         ; forward to INT-CASE if a small integer
  16677.  
  16678. ; for floating point numbers a single bit denotes the sign.
  16679.  
  16680.         INC     HL              ; address the first byte of mantissa.
  16681.         LD      A,B             ; action flag $FF=abs, $00=neg.
  16682.         AND     $80             ; now         $80      $00
  16683.         OR      (HL)            ; sets bit 7 for abs
  16684.         RLA                     ; sets carry for abs and if number negative
  16685.         CCF                     ; complement carry flag
  16686.         RRA                     ; and rotate back in altering sign
  16687.         LD      (HL),A          ; put the altered adjusted number back
  16688.         DEC     HL              ; HL points to result
  16689.         RET                     ; return with DE unchanged
  16690.  
  16691. ; ---
  16692.  
  16693. ; for integer numbers an entire byte denotes the sign.
  16694.  
  16695. ;; INT-CASE
  16696. L3483:  PUSH    DE              ; save STKEND.
  16697.  
  16698.         PUSH    HL              ; save pointer to the last value/result.
  16699.  
  16700.         CALL    L2D7F           ; routine INT-FETCH puts integer in DE
  16701.                                 ; and the sign in C.
  16702.  
  16703.         POP     HL              ; restore the result pointer.
  16704.  
  16705.         LD      A,B             ; $FF=abs, $00=neg
  16706.         OR      C               ; $FF for abs, no change neg
  16707.         CPL                     ; $00 for abs, switched for neg
  16708.         LD      C,A             ; transfer result to sign byte.
  16709.  
  16710.         CALL    L2D8E           ; routine INT-STORE to re-write the integer.
  16711.  
  16712.         POP     DE              ; restore STKEND.
  16713.         RET                     ; return.
  16714.  
  16715. ; -----------
  16716. ; Signum (29)
  16717. ; -----------
  16718. ; This routine replaces the last value on the calculator stack,
  16719. ; which may be in floating point or integer form, with the integer values
  16720. ; zero if zero, with one if positive and  with -minus one if negative.
  16721.  
  16722. ;; sgn
  16723. L3492:  CALL    L34E9           ; call routine TEST-ZERO and
  16724.         RET     C               ; exit if so as no change is required.
  16725.  
  16726.         PUSH    DE              ; save pointer to STKEND.
  16727.  
  16728.         LD      DE,$0001        ; the result will be 1.
  16729.         INC     HL              ; skip over the exponent.
  16730.         RL      (HL)            ; rotate the sign bit into the carry flag.
  16731.         DEC     HL              ; step back to point to the result.
  16732.         SBC     A,A             ; byte will be $FF if negative, $00 if positive.
  16733.         LD      C,A             ; store the sign byte in the C register.
  16734.         CALL    L2D8E           ; routine INT-STORE to overwrite the last
  16735.                                 ; value with 0001 and sign.
  16736.  
  16737.         POP     DE              ; restore STKEND.
  16738.         RET                     ; return.
  16739.  
  16740. ; -----------------------
  16741. ; Handle IN function (2C)
  16742. ; -----------------------
  16743. ; This function reads a byte from an input port.
  16744.  
  16745. ;; in
  16746. L34A5:  CALL    L1E99           ; routine FIND-INT2 puts port address in BC.
  16747.                                 ; all 16 bits are put on the address line.
  16748.         IN      A,(C)           ; read the port.
  16749.  
  16750.         JR      L34B0           ; exit to STACK-A (via IN-PK-STK to save a byte
  16751.                                 ; of instruction code).
  16752.  
  16753. ; -------------------------
  16754. ; Handle PEEK function (2B)
  16755. ; -------------------------
  16756. ; This function returns the contents of a memory address.
  16757. ; The entire address space can be peeked including the ROM.
  16758.  
  16759. ;; peek
  16760. L34AC:  CALL    L1E99           ; routine FIND-INT2 puts address in BC.
  16761.         LD      A,(BC)          ; load contents into A register.
  16762.  
  16763. ;; IN-PK-STK
  16764. L34B0:  JP      L2D28           ; exit via STACK-A to put value on the
  16765.                                 ; calculator stack.
  16766.  
  16767. ; ---------------
  16768. ; USR number (2D)
  16769. ; ---------------
  16770. ; The USR function followed by a number 0-65535 is the method by which
  16771. ; the Spectrum invokes machine code programs. This function returns the
  16772. ; contents of the BC register pair.
  16773. ; Note. that STACK-BC re-initializes the IY register if a user-written
  16774. ; program has altered it.
  16775.  
  16776. ;; usr-no
  16777. L34B3:  CALL    L1E99           ; routine FIND-INT2 to fetch the
  16778.                                 ; supplied address into BC.
  16779.  
  16780.         LD      HL,L2D2B        ; address: STACK-BC is
  16781.         PUSH    HL              ; pushed onto the machine stack.
  16782.         PUSH    BC              ; then the address of the machine code
  16783.                                 ; routine.
  16784.  
  16785.         RET                     ; make an indirect jump to the routine
  16786.                                 ; and, hopefully, to STACK-BC also.
  16787.  
  16788. ; ---------------
  16789. ; USR string (19)
  16790. ; ---------------
  16791. ; The user function with a one-character string argument, calculates the
  16792. ; address of the User Defined Graphic character that is in the string.
  16793. ; As an alternative, the ASCII equivalent, upper or lower case,
  16794. ; may be supplied. This provides a user-friendly method of redefining
  16795. ; the 21 User Definable Graphics e.g.
  16796. ; POKE USR "a", BIN 10000000 will put a dot in the top left corner of the
  16797. ; character 144.
  16798. ; Note. the curious double check on the range. With 26 UDGs the first check
  16799. ; only is necessary. With anything less the second check only is required.
  16800. ; It is highly likely that the first check was written by Steven Vickers.
  16801.  
  16802. ;; usr-$
  16803. L34BC:  CALL    L2BF1           ; routine STK-FETCH fetches the string
  16804.                                 ; parameters.
  16805.         DEC     BC              ; decrease BC by
  16806.         LD      A,B             ; one to test
  16807.         OR      C               ; the length.
  16808.         JR      NZ,L34E7        ; to REPORT-A if not a single character.
  16809.  
  16810.         LD      A,(DE)          ; fetch the character
  16811.         CALL    L2C8D           ; routine ALPHA sets carry if 'A-Z' or 'a-z'.
  16812.         JR      C,L34D3         ; forward to USR-RANGE if ASCII.
  16813.  
  16814.         SUB     $90             ; make udgs range 0-20d
  16815.         JR      C,L34E7         ; to REPORT-A if too low. e.g. usr " ".
  16816.  
  16817.         CP      $15             ; Note. this test is not necessary.
  16818.         JR      NC,L34E7        ; to REPORT-A if higher than 20.
  16819.  
  16820.         INC     A               ; make range 1-21d to match LSBs of ASCII
  16821.  
  16822. ;; USR-RANGE
  16823. L34D3:  DEC     A               ; make range of bits 0-4 start at zero
  16824.         ADD     A,A             ; multiply by eight
  16825.         ADD     A,A             ; and lose any set bits
  16826.         ADD     A,A             ; range now 0 - 25*8
  16827.         CP      $A8             ; compare to 21*8
  16828.         JR      NC,L34E7        ; to REPORT-A if originally higher
  16829.                                 ; than 'U','u' or graphics U.
  16830.  
  16831.         LD      BC,($5C7B)      ; fetch the UDG system variable value.
  16832.         ADD     A,C             ; add the offset to character
  16833.         LD      C,A             ; and store back in register C.
  16834.         JR      NC,L34E4        ; forward to USR-STACK if no overflow.
  16835.  
  16836.         INC     B               ; increment high byte.
  16837.  
  16838. ;; USR-STACK
  16839. L34E4:  JP      L2D2B           ; jump back and exit via STACK-BC to store
  16840.  
  16841. ; ---
  16842.  
  16843. ;; REPORT-A
  16844. L34E7:  RST     08H             ; ERROR-1
  16845.         DB    $09             ; Error Report: Invalid argument
  16846.  
  16847. ; -------------
  16848. ; Test for zero
  16849. ; -------------
  16850. ; Test if top value on calculator stack is zero.
  16851. ; The carry flag is set if the last value is zero but no registers are altered.
  16852. ; All five bytes will be zero but first four only need be tested.
  16853. ; On entry HL points to the exponent the first byte of the value.
  16854.  
  16855. ;; TEST-ZERO
  16856. L34E9:  PUSH    HL              ; preserve HL which is used to address.
  16857.         PUSH    BC              ; preserve BC which is used as a store.
  16858.         LD      B,A             ; preserve A in B.
  16859.  
  16860.         LD      A,(HL)          ; load first byte to accumulator
  16861.         INC     HL              ; advance.
  16862.         OR      (HL)            ; OR with second byte and clear carry.
  16863.         INC     HL              ; advance.
  16864.         OR      (HL)            ; OR with third byte.
  16865.         INC     HL              ; advance.
  16866.         OR      (HL)            ; OR with fourth byte.
  16867.  
  16868.         LD      A,B             ; restore A without affecting flags.
  16869.         POP     BC              ; restore the saved
  16870.         POP     HL              ; registers.
  16871.  
  16872.         RET     NZ              ; return if not zero and with carry reset.
  16873.  
  16874.         SCF                     ; set the carry flag.
  16875.         RET                     ; return with carry set if zero.
  16876.  
  16877. ; -----------------------
  16878. ; Greater than zero ($37)
  16879. ; -----------------------
  16880. ; Test if the last value on the calculator stack is greater than zero.
  16881. ; This routine is also called directly from the end-tests of the comparison
  16882. ; routine.
  16883.  
  16884. ;; GREATER-0
  16885. ;; greater-0
  16886. L34F9:  CALL    L34E9           ; routine TEST-ZERO
  16887.         RET     C               ; return if was zero as this
  16888.                                 ; is also the Boolean 'false' value.
  16889.  
  16890.         LD      A,$FF           ; prepare XOR mask for sign bit
  16891.         JR      L3507           ; forward to SIGN-TO-C
  16892.                                 ; to put sign in carry
  16893.                                 ; (carry will become set if sign is positive)
  16894.                                 ; and then overwrite location with 1 or 0
  16895.                                 ; as appropriate.
  16896.  
  16897. ; ------------------------
  16898. ; Handle NOT operator ($30)
  16899. ; ------------------------
  16900. ; This overwrites the last value with 1 if it was zero else with zero
  16901. ; if it was any other value.
  16902. ;
  16903. ; e.g NOT 0 returns 1, NOT 1 returns 0, NOT -3 returns 0.
  16904. ;
  16905. ; The subroutine is also called directly from the end-tests of the comparison
  16906. ; operator.
  16907.  
  16908. ;; NOT
  16909. ;; not
  16910. L3501:  CALL    L34E9           ; routine TEST-ZERO sets carry if zero
  16911.  
  16912.         JR      L350B           ; to FP-0/1 to overwrite operand with
  16913.                                 ; 1 if carry is set else to overwrite with zero.
  16914.  
  16915. ; -------------------
  16916. ; Less than zero (36)
  16917. ; -------------------
  16918. ; Destructively test if last value on calculator stack is less than zero.
  16919. ; Bit 7 of second byte will be set if so.
  16920.  
  16921. ;; less-0
  16922. L3506:  XOR     A               ; set xor mask to zero
  16923.                                 ; (carry will become set if sign is negative).
  16924.  
  16925. ; transfer sign of mantissa to Carry Flag.
  16926.  
  16927. ;; SIGN-TO-C
  16928. L3507:  INC     HL              ; address 2nd byte.
  16929.         XOR     (HL)            ; bit 7 of HL will be set if number is negative.
  16930.         DEC     HL              ; address 1st byte again.
  16931.         RLCA                    ; rotate bit 7 of A to carry.
  16932.  
  16933. ; -----------
  16934. ; Zero or one
  16935. ; -----------
  16936. ; This routine places an integer value of zero or one at the addressed location
  16937. ; of the calculator stack or MEM area.  The value one is written if carry is
  16938. ; set on entry else zero.
  16939.  
  16940. ;; FP-0/1
  16941. L350B:  PUSH    HL              ; save pointer to the first byte
  16942.         LD      A,$00           ; load accumulator with zero - without
  16943.                                 ; disturbing flags.
  16944.         LD      (HL),A          ; zero to first byte
  16945.         INC     HL              ; address next
  16946.         LD      (HL),A          ; zero to 2nd byte
  16947.         INC     HL              ; address low byte of integer
  16948.         RLA                     ; carry to bit 0 of A
  16949.         LD      (HL),A          ; load one or zero to low byte.
  16950.         RRA                     ; restore zero to accumulator.
  16951.         INC     HL              ; address high byte of integer.
  16952.         LD      (HL),A          ; put a zero there.
  16953.         INC     HL              ; address fifth byte.
  16954.         LD      (HL),A          ; put a zero there.
  16955.         POP     HL              ; restore pointer to the first byte.
  16956.         RET                     ; return.
  16957.  
  16958. ; -----------------------
  16959. ; Handle OR operator (07)
  16960. ; -----------------------
  16961. ; The Boolean OR operator. eg. X OR Y
  16962. ; The result is zero if both values are zero else a non-zero value.
  16963. ;
  16964. ; e.g.    0 OR 0  returns 0.
  16965. ;        -3 OR 0  returns -3.
  16966. ;         0 OR -3 returns 1.
  16967. ;        -3 OR 2  returns 1.
  16968. ;
  16969. ; A binary operation.
  16970. ; On entry HL points to first operand (X) and DE to second operand (Y).
  16971.  
  16972. ;; or
  16973. L351B:  EX      DE,HL           ; make HL point to second number
  16974.         CALL    L34E9           ; routine TEST-ZERO
  16975.         EX      DE,HL           ; restore pointers
  16976.         RET     C               ; return if result was zero - first operand,
  16977.                                 ; now the last value, is the result.
  16978.  
  16979.         SCF                     ; set carry flag
  16980.         JR      L350B           ; back to FP-0/1 to overwrite the first operand
  16981.                                 ; with the value 1.
  16982.  
  16983.  
  16984. ; -----------------------------
  16985. ; Handle number AND number (08)
  16986. ; -----------------------------
  16987. ; The Boolean AND operator.
  16988. ;
  16989. ; e.g.    -3 AND 2  returns -3.
  16990. ;         -3 AND 0  returns 0.
  16991. ;          0 and -2 returns 0.
  16992. ;          0 and 0  returns 0.
  16993. ;
  16994. ; Compare with OR routine above.
  16995.  
  16996. ;; no-&-no
  16997. L3524:  EX      DE,HL           ; make HL address second operand.
  16998.  
  16999.         CALL    L34E9           ; routine TEST-ZERO sets carry if zero.
  17000.  
  17001.         EX      DE,HL           ; restore pointers.
  17002.         RET     NC              ; return if second non-zero, first is result.
  17003.  
  17004. ;
  17005.  
  17006.         AND     A               ; else clear carry.
  17007.         JR      L350B           ; back to FP-0/1 to overwrite first operand
  17008.                                 ; with zero for return value.
  17009.  
  17010. ; -----------------------------
  17011. ; Handle string AND number (10)
  17012. ; -----------------------------
  17013. ; e.g. "You Win" AND score>99 will return the string if condition is true
  17014. ; or the null string if false.
  17015.  
  17016. ;; str-&-no
  17017. L352D:  EX      DE,HL           ; make HL point to the number.
  17018.         CALL    L34E9           ; routine TEST-ZERO.
  17019.         EX      DE,HL           ; restore pointers.
  17020.         RET     NC              ; return if number was not zero - the string
  17021.                                 ; is the result.
  17022.  
  17023. ; if the number was zero (false) then the null string must be returned by
  17024. ; altering the length of the string on the calculator stack to zero.
  17025.  
  17026.         PUSH    DE              ; save pointer to the now obsolete number
  17027.                                 ; (which will become the new STKEND)
  17028.  
  17029.         DEC     DE              ; point to the 5th byte of string descriptor.
  17030.         XOR     A               ; clear the accumulator.
  17031.         LD      (DE),A          ; place zero in high byte of length.
  17032.         DEC     DE              ; address low byte of length.
  17033.         LD      (DE),A          ; place zero there - now the null string.
  17034.  
  17035.         POP     DE              ; restore pointer - new STKEND.
  17036.         RET                     ; return.
  17037.  
  17038. ; -----------------------------------
  17039. ; Perform comparison ($09-$0E, $11-$16)
  17040. ; -----------------------------------
  17041. ; True binary operations.
  17042. ;
  17043. ; A single entry point is used to evaluate six numeric and six string
  17044. ; comparisons. On entry, the calculator literal is in the B register and
  17045. ; the two numeric values, or the two string parameters, are on the
  17046. ; calculator stack.
  17047. ; The individual bits of the literal are manipulated to group similar
  17048. ; operations although the SUB 8 instruction does nothing useful and merely
  17049. ; alters the string test bit.
  17050. ; Numbers are compared by subtracting one from the other, strings are
  17051. ; compared by comparing every character until a mismatch, or the end of one
  17052. ; or both, is reached.
  17053. ;
  17054. ; Numeric Comparisons.
  17055. ; --------------------
  17056. ; The 'x>y' example is the easiest as it employs straight-thru logic.
  17057. ; Number y is subtracted from x and the result tested for greater-0 yielding
  17058. ; a final value 1 (true) or 0 (false).
  17059. ; For 'x<y' the same logic is used but the two values are first swapped on the
  17060. ; calculator stack.
  17061. ; For 'x=y' NOT is applied to the subtraction result yielding true if the
  17062. ; difference was zero and false with anything else.
  17063. ; The first three numeric comparisons are just the opposite of the last three
  17064. ; so the same processing steps are used and then a final NOT is applied.
  17065. ;
  17066. ; literal    Test   No  sub 8       ExOrNot  1st RRCA  exch sub  ?   End-Tests
  17067. ; =========  ====   == ======== === ======== ========  ==== ===  =  === === ===
  17068. ; no-l-eql   x<=y   09 00000001 dec 00000000 00000000  ---- x-y  ?  --- >0? NOT
  17069. ; no-gr-eql  x>=y   0A 00000010 dec 00000001 10000000c swap y-x  ?  --- >0? NOT
  17070. ; nos-neql   x<>y   0B 00000011 dec 00000010 00000001  ---- x-y  ?  NOT --- NOT
  17071. ; no-grtr    x>y    0C 00000100  -  00000100 00000010  ---- x-y  ?  --- >0? ---
  17072. ; no-less    x<y    0D 00000101  -  00000101 10000010c swap y-x  ?  --- >0? ---
  17073. ; nos-eql    x=y    0E 00000110  -  00000110 00000011  ---- x-y  ?  NOT --- ---
  17074. ;
  17075. ;                                                           comp -> C/F
  17076. ;                                                           ====    ===
  17077. ; str-l-eql  x$<=y$ 11 00001001 dec 00001000 00000100  ---- x$y$ 0  !or >0? NOT
  17078. ; str-gr-eql x$>=y$ 12 00001010 dec 00001001 10000100c swap y$x$ 0  !or >0? NOT
  17079. ; strs-neql  x$<>y$ 13 00001011 dec 00001010 00000101  ---- x$y$ 0  !or >0? NOT
  17080. ; str-grtr   x$>y$  14 00001100  -  00001100 00000110  ---- x$y$ 0  !or >0? ---
  17081. ; str-less   x$<y$  15 00001101  -  00001101 10000110c swap y$x$ 0  !or >0? ---
  17082. ; strs-eql   x$=y$  16 00001110  -  00001110 00000111  ---- x$y$ 0  !or >0? ---
  17083. ;
  17084. ; String comparisons are a little different in that the eql/neql carry flag
  17085. ; from the 2nd RRCA is, as before, fed into the first of the end tests but
  17086. ; along the way it gets modified by the comparison process. The result on the
  17087. ; stack always starts off as zero and the carry fed in determines if NOT is
  17088. ; applied to it. So the only time the greater-0 test is applied is if the
  17089. ; stack holds zero which is not very efficient as the test will always yield
  17090. ; zero. The most likely explanation is that there were once separate end tests
  17091. ; for numbers and strings.
  17092.  
  17093. ;; no-l-eql, etc.
  17094. L353B:  LD      A,B             ; transfer literal to accumulator.
  17095.         SUB     $08             ; subtract eight - which is not useful.
  17096.  
  17097.         BIT     2,A             ; isolate '>', '<', '='.
  17098.  
  17099.         JR      NZ,L3543        ; skip to EX-OR-NOT with these.
  17100.  
  17101.         DEC     A               ; else make $00-$02, $08-$0A to match bits 0-2.
  17102.  
  17103. ;; EX-OR-NOT
  17104. L3543:  RRCA                    ; the first RRCA sets carry for a swap.
  17105.         JR      NC,L354E        ; forward to NU-OR-STR with other 8 cases
  17106.  
  17107. ; for the other 4 cases the two values on the calculator stack are exchanged.
  17108.  
  17109.         PUSH    AF              ; save A and carry.
  17110.         PUSH    HL              ; save HL - pointer to first operand.
  17111.                                 ; (DE points to second operand).
  17112.  
  17113.         CALL    L343C           ; routine exchange swaps the two values.
  17114.                                 ; (HL = second operand, DE = STKEND)
  17115.  
  17116.         POP     DE              ; DE = first operand
  17117.         EX      DE,HL           ; as we were.
  17118.         POP     AF              ; restore A and carry.
  17119.  
  17120. ; Note. it would be better if the 2nd RRCA preceded the string test.
  17121. ; It would save two duplicate bytes and if we also got rid of that sub 8
  17122. ; at the beginning we wouldn't have to alter which bit we test.
  17123.  
  17124. ;; NU-OR-STR
  17125. L354E:  BIT     2,A             ; test if a string comparison.
  17126.         JR      NZ,L3559        ; forward to STRINGS if so.
  17127.  
  17128. ; continue with numeric comparisons.
  17129.  
  17130.         RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17131.         PUSH    AF              ; save A and carry
  17132.  
  17133.         CALL    L300F           ; routine subtract leaves result on stack.
  17134.         JR      L358C           ; forward to END-TESTS
  17135.  
  17136. ; ---
  17137.  
  17138. ;; STRINGS
  17139. L3559:  RRCA                    ; 2nd RRCA causes eql/neql to set carry.
  17140.         PUSH    AF              ; save A and carry.
  17141.  
  17142.         CALL    L2BF1           ; routine STK-FETCH gets 2nd string params
  17143.         PUSH    DE              ; save start2 *.
  17144.         PUSH    BC              ; and the length.
  17145.  
  17146.         CALL    L2BF1           ; routine STK-FETCH gets 1st string
  17147.                                 ; parameters - start in DE, length in BC.
  17148.         POP     HL              ; restore length of second to HL.
  17149.  
  17150. ; A loop is now entered to compare, by subtraction, each corresponding character
  17151. ; of the strings. For each successful match, the pointers are incremented and
  17152. ; the lengths decreased and the branch taken back to here. If both string
  17153. ; remainders become null at the same time, then an exact match exists.
  17154.  
  17155. ;; BYTE-COMP
  17156. L3564:  LD      A,H             ; test if the second string
  17157.         OR      L               ; is the null string and hold flags.
  17158.  
  17159.         EX      (SP),HL         ; put length2 on stack, bring start2 to HL *.
  17160.         LD      A,B             ; hi byte of length1 to A
  17161.  
  17162.         JR      NZ,L3575        ; forward to SEC-PLUS if second not null.
  17163.  
  17164.         OR      C               ; test length of first string.
  17165.  
  17166. ;; SECND-LOW
  17167. L356B:  POP     BC              ; pop the second length off stack.
  17168.         JR      Z,L3572         ; forward to BOTH-NULL if first string is also
  17169.                                 ; of zero length.
  17170.  
  17171. ; the true condition - first is longer than second (SECND-LESS)
  17172.  
  17173.         POP     AF              ; restore carry (set if eql/neql)
  17174.         CCF                     ; complement carry flag.
  17175.                                 ; Note. equality becomes false.
  17176.                                 ; Inequality is true. By swapping or applying
  17177.                                 ; a terminal 'not', all comparisons have been
  17178.                                 ; manipulated so that this is success path.
  17179.         JR      L3588           ; forward to leave via STR-TEST
  17180.  
  17181. ; ---
  17182. ; the branch was here with a match
  17183.  
  17184. ;; BOTH-NULL
  17185. L3572:  POP     AF              ; restore carry - set for eql/neql
  17186.         JR      L3588           ; forward to STR-TEST
  17187.  
  17188. ; ---  
  17189. ; the branch was here when 2nd string not null and low byte of first is yet
  17190. ; to be tested.
  17191.  
  17192.  
  17193. ;; SEC-PLUS
  17194. L3575:  OR      C               ; test the length of first string.
  17195.         JR      Z,L3585         ; forward to FRST-LESS if length is zero.
  17196.  
  17197. ; both strings have at least one character left.
  17198.  
  17199.         LD      A,(DE)          ; fetch character of first string.
  17200.         SUB     (HL)            ; subtract with that of 2nd string.
  17201.         JR      C,L3585         ; forward to FRST-LESS if carry set
  17202.  
  17203.         JR      NZ,L356B        ; back to SECND-LOW and then STR-TEST
  17204.                                 ; if not exact match.
  17205.  
  17206.         DEC     BC              ; decrease length of 1st string.
  17207.         INC     DE              ; increment 1st string pointer.
  17208.  
  17209.         INC     HL              ; increment 2nd string pointer.
  17210.         EX      (SP),HL         ; swap with length on stack
  17211.         DEC     HL              ; decrement 2nd string length
  17212.         JR      L3564           ; back to BYTE-COMP
  17213.  
  17214. ; ---
  17215. ; the false condition.
  17216.  
  17217. ;; FRST-LESS
  17218. L3585:  POP     BC              ; discard length
  17219.         POP     AF              ; pop A
  17220.         AND     A               ; clear the carry for false result.
  17221.  
  17222. ; ---
  17223. ; exact match and x$>y$ rejoin here
  17224.  
  17225. ;; STR-TEST
  17226. L3588:  PUSH    AF              ; save A and carry
  17227.  
  17228.         RST     28H             ;; FP-CALC
  17229.         DB    $A0             ;;stk-zero      an initial false value.
  17230.         DB    $38             ;;end-calc
  17231.  
  17232. ; both numeric and string paths converge here.
  17233.  
  17234. ;; END-TESTS
  17235. L358C:  POP     AF              ; pop carry  - will be set if eql/neql
  17236.         PUSH    AF              ; save it again.
  17237.  
  17238.         CALL    C,L3501         ; routine NOT sets true(1) if equal(0)
  17239.                                 ; or, for strings, applies true result.
  17240.  
  17241.         POP     AF              ; pop carry and
  17242.         PUSH    AF              ; save A
  17243.  
  17244.         CALL    NC,L34F9        ; routine GREATER-0 tests numeric subtraction
  17245.                                 ; result but also needlessly tests the string
  17246.                                 ; value for zero - it must be.
  17247.  
  17248.         POP     AF              ; pop A
  17249.         RRCA                    ; the third RRCA - test for '<=', '>=' or '<>'.
  17250.         CALL    NC,L3501        ; apply a terminal NOT if so.
  17251.         RET                     ; return.
  17252.  
  17253. ; -------------------------
  17254. ; String concatenation ($17)
  17255. ; -------------------------
  17256. ; This literal combines two strings into one e.g. LET a$ = b$ + c$
  17257. ; The two parameters of the two strings to be combined are on the stack.
  17258.  
  17259. ;; strs-add
  17260. L359C:  CALL    L2BF1           ; routine STK-FETCH fetches string parameters
  17261.                                 ; and deletes calculator stack entry.
  17262.         PUSH    DE              ; save start address.
  17263.         PUSH    BC              ; and length.
  17264.  
  17265.         CALL    L2BF1           ; routine STK-FETCH for first string
  17266.         POP     HL              ; re-fetch first length
  17267.         PUSH    HL              ; and save again
  17268.         PUSH    DE              ; save start of second string
  17269.         PUSH    BC              ; and its length.
  17270.  
  17271.         ADD     HL,BC           ; add the two lengths.
  17272.         LD      B,H             ; transfer to BC
  17273.         LD      C,L             ; and create
  17274.         RST     30H             ; BC-SPACES in workspace.
  17275.                                 ; DE points to start of space.
  17276.  
  17277.         CALL    L2AB2           ; routine STK-STO-$ stores parameters
  17278.                                 ; of new string updating STKEND.
  17279.  
  17280.         POP     BC              ; length of first
  17281.         POP     HL              ; address of start
  17282.         LD      A,B             ; test for
  17283.         OR      C               ; zero length.
  17284.         JR      Z,L35B7         ; to OTHER-STR if null string
  17285.  
  17286.         LDIR                    ; copy string to workspace.
  17287.  
  17288. ;; OTHER-STR
  17289. L35B7:  POP     BC              ; now second length
  17290.         POP     HL              ; and start of string
  17291.         LD      A,B             ; test this one
  17292.         OR      C               ; for zero length
  17293.         JR      Z,L35BF         ; skip forward to STK-PNTRS if so as complete.
  17294.  
  17295.         LDIR                    ; else copy the bytes.
  17296.                                 ; and continue into next routine which
  17297.                                 ; sets the calculator stack pointers.
  17298.  
  17299. ; --------------------
  17300. ; Check stack pointers
  17301. ; --------------------
  17302. ; Register DE is set to STKEND and HL, the result pointer, is set to five
  17303. ; locations below this.
  17304. ; This routine is used when it is inconvenient to save these values at the
  17305. ; time the calculator stack is manipulated due to other activity on the
  17306. ; machine stack.
  17307. ; This routine is also used to terminate the VAL and READ-IN  routines for
  17308. ; the same reason and to initialize the calculator stack at the start of
  17309. ; the CALCULATE routine.
  17310.  
  17311. ;; STK-PNTRS
  17312. L35BF:  LD      HL,($5C65)      ; fetch STKEND value from system variable.
  17313.         LD      DE,$FFFB        ; the value -5
  17314.         PUSH    HL              ; push STKEND value.
  17315.  
  17316.         ADD     HL,DE           ; subtract 5 from HL.
  17317.  
  17318.         POP     DE              ; pop STKEND to DE.
  17319.         RET                     ; return.
  17320.  
  17321. ; ----------------
  17322. ; Handle CHR$ (2F)
  17323. ; ----------------
  17324. ; This function returns a single character string that is a result of
  17325. ; converting a number in the range 0-255 to a string e.g. CHR$ 65 = "A".
  17326.  
  17327. ;; chrs
  17328. L35C9:  CALL    L2DD5           ; routine FP-TO-A puts the number in A.
  17329.  
  17330.         JR      C,L35DC         ; forward to REPORT-Bd if overflow
  17331.         JR      NZ,L35DC        ; forward to REPORT-Bd if negative
  17332.  
  17333.         PUSH    AF              ; save the argument.
  17334.  
  17335.         LD      BC,$0001        ; one space required.
  17336.         RST     30H             ; BC-SPACES makes DE point to start
  17337.  
  17338.         POP     AF              ; restore the number.
  17339.  
  17340.         LD      (DE),A          ; and store in workspace
  17341.  
  17342.         CALL    L2AB2           ; routine STK-STO-$ stacks descriptor.
  17343.  
  17344.         EX      DE,HL           ; make HL point to result and DE to STKEND.
  17345.         RET                     ; return.
  17346.  
  17347. ; ---
  17348.  
  17349. ;; REPORT-Bd
  17350. L35DC:  RST     08H             ; ERROR-1
  17351.         DB    $0A             ; Error Report: Integer out of range
  17352.  
  17353. ; ----------------------------
  17354. ; Handle VAL and VAL$ ($1D, $18)
  17355. ; ----------------------------
  17356. ; VAL treats the characters in a string as a numeric expression.
  17357. ;     e.g. VAL "2.3" = 2.3, VAL "2+4" = 6, VAL ("2" + "4") = 24.
  17358. ; VAL$ treats the characters in a string as a string expression.
  17359. ;     e.g. VAL$ (z$+"(2)") = a$(2) if z$ happens to be "a$".
  17360.  
  17361. ;; val
  17362. ;; val$
  17363. L35DE:  LD      HL,($5C5D)      ; fetch value of system variable CH_ADD
  17364.         PUSH    HL              ; and save on the machine stack.
  17365.         LD      A,B             ; fetch the literal (either $1D or $18).
  17366.         ADD     A,$E3           ; add $E3 to form $00 (setting carry) or $FB.
  17367.         SBC     A,A             ; now form $FF bit 6 = numeric result
  17368.                                 ; or $00 bit 6 = string result.
  17369.         PUSH    AF              ; save this mask on the stack
  17370.  
  17371.         CALL    L2BF1           ; routine STK-FETCH fetches the string operand
  17372.                                 ; from calculator stack.
  17373.  
  17374.         PUSH    DE              ; save the address of the start of the string.
  17375.         INC     BC              ; increment the length for a carriage return.
  17376.  
  17377.         RST     30H             ; BC-SPACES creates the space in workspace.
  17378.         POP     HL              ; restore start of string to HL.
  17379.         LD      ($5C5D),DE      ; load CH_ADD with start DE in workspace.
  17380.  
  17381.         PUSH    DE              ; save the start in workspace
  17382.         LDIR                    ; copy string from program or variables or
  17383.                                 ; workspace to the workspace area.
  17384.         EX      DE,HL           ; end of string + 1 to HL
  17385.         DEC     HL              ; decrement HL to point to end of new area.
  17386.         LD      (HL),$0D        ; insert a carriage return at end.
  17387.         RES     7,(IY+$01)      ; update FLAGS  - signal checking syntax.
  17388.         CALL    L24FB           ; routine SCANNING evaluates string
  17389.                                 ; expression and result.
  17390.  
  17391.         RST     18H             ; GET-CHAR fetches next character.
  17392.         CP      $0D             ; is it the expected carriage return ?
  17393.         JR      NZ,L360C        ; forward to V-RPORT-C if not
  17394.                                 ; 'Nonsense in BASIC'.
  17395.  
  17396.         POP     HL              ; restore start of string in workspace.
  17397.         POP     AF              ; restore expected result flag (bit 6).
  17398.         XOR     (IY+$01)        ; xor with FLAGS now updated by SCANNING.
  17399.         AND     $40             ; test bit 6 - should be zero if result types
  17400.                                 ; match.
  17401.  
  17402. ;; V-RPORT-C
  17403. L360C:  JP      NZ,L1C8A        ; jump back to REPORT-C with a result mismatch.
  17404.  
  17405.         LD      ($5C5D),HL      ; set CH_ADD to the start of the string again.
  17406.         SET     7,(IY+$01)      ; update FLAGS  - signal running program.
  17407.         CALL    L24FB           ; routine SCANNING evaluates the string
  17408.                                 ; in full leaving result on calculator stack.
  17409.  
  17410.         POP     HL              ; restore saved character address in program.
  17411.         LD      ($5C5D),HL      ; and reset the system variable CH_ADD.
  17412.  
  17413.         JR      L35BF           ; back to exit via STK-PNTRS.
  17414.                                 ; resetting the calculator stack pointers
  17415.                                 ; HL and DE from STKEND as it wasn't possible
  17416.                                 ; to preserve them during this routine.
  17417.  
  17418. ; ----------------
  17419. ; Handle STR$ (2E)
  17420. ; ----------------
  17421. ;
  17422. ;
  17423.  
  17424. ;; str$
  17425. L361F:  LD      BC,$0001        ; create an initial byte in workspace
  17426.         RST     30H             ; using BC-SPACES restart.
  17427.  
  17428.         LD      ($5C5B),HL      ; set system variable K_CUR to new location.
  17429.         PUSH    HL              ; and save start on machine stack also.
  17430.  
  17431.         LD      HL,($5C51)      ; fetch value of system variable CURCHL
  17432.         PUSH    HL              ; and save that too.
  17433.  
  17434.         LD      A,$FF           ; select system channel 'R'.
  17435.         CALL    L1601           ; routine CHAN-OPEN opens it.
  17436.         CALL    L2DE3           ; routine PRINT-FP outputs the number to
  17437.                                 ; workspace updating K-CUR.
  17438.  
  17439.         POP     HL              ; restore current channel.
  17440.         CALL    L1615           ; routine CHAN-FLAG resets flags.
  17441.  
  17442.         POP     DE              ; fetch saved start of string to DE.
  17443.         LD      HL,($5C5B)      ; load HL with end of string from K_CUR.
  17444.  
  17445.         AND     A               ; prepare for true subtraction.
  17446.         SBC     HL,DE           ; subtract start from end to give length.
  17447.         LD      B,H             ; transfer the length to
  17448.         LD      C,L             ; the BC register pair.
  17449.  
  17450.         CALL    L2AB2           ; routine STK-STO-$ stores string parameters
  17451.                                 ; on the calculator stack.
  17452.  
  17453.         EX      DE,HL           ; HL = last value, DE = STKEND.
  17454.         RET                     ; return.
  17455.  
  17456. ; ------------
  17457. ; Read-in (1A)
  17458. ; ------------
  17459. ; This is the calculator literal used by the INKEY$ function when a '#'
  17460. ; is encountered after the keyword.
  17461. ; INKEY$ # does not interact correctly with the keyboard, #0 or #1, and
  17462. ; its uses are for other channels.
  17463.  
  17464. ;; read-in
  17465. L3645:  CALL    L1E94           ; routine FIND-INT1 fetches stream to A
  17466.         CP      $10             ; compare with 16 decimal.
  17467.         JP      NC,L1E9F        ; jump to REPORT-Bb if not in range 0 - 15.
  17468.                                 ; 'Integer out of range'
  17469.                                 ; (REPORT-Bd is within range)
  17470.  
  17471.         LD      HL,($5C51)      ; fetch current channel CURCHL
  17472.         PUSH    HL              ; save it
  17473.         CALL    L1601           ; routine CHAN-OPEN opens channel
  17474.  
  17475.         CALL    L15E6           ; routine INPUT-AD - the channel must have an
  17476.                                 ; input stream or else error here from stream
  17477.                                 ; stub.
  17478.         LD      BC,$0000        ; initialize length of string to zero
  17479.         JR      NC,L365F        ; forward to R-I-STORE if no key detected.
  17480.  
  17481.         INC     C               ; increase length to one.
  17482.  
  17483.         RST     30H             ; BC-SPACES creates space for one character
  17484.                                 ; in workspace.
  17485.         LD      (DE),A          ; the character is inserted.
  17486.  
  17487. ;; R-I-STORE
  17488. L365F:  CALL    L2AB2           ; routine STK-STO-$ stacks the string
  17489.                                 ; parameters.
  17490.         POP     HL              ; restore current channel address
  17491.         CALL    L1615           ; routine CHAN-FLAG resets current channel
  17492.                                 ; system variable and flags.
  17493.         JP      L35BF           ; jump back to STK-PNTRS
  17494.  
  17495. ; ----------------
  17496. ; Handle CODE (1C)
  17497. ; ----------------
  17498. ; Returns the ASCII code of a character or first character of a string
  17499. ; e.g. CODE "Aardvark" = 65, CODE "" = 0.
  17500.  
  17501. ;; code
  17502. L3669:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17503.                                 ; string parameters.
  17504.                                 ; DE points to the start, BC holds the length.
  17505.         LD      A,B             ; test length
  17506.         OR      C               ; of the string.
  17507.         JR      Z,L3671         ; skip to STK-CODE with zero if the null string.
  17508.  
  17509.         LD      A,(DE)          ; else fetch the first character.
  17510.  
  17511. ;; STK-CODE
  17512. L3671:  JP      L2D28           ; jump back to STACK-A (with memory check)
  17513.  
  17514. ; ---------------
  17515. ; Handle LEN (1E)
  17516. ; ---------------
  17517. ; Returns the length of a string.
  17518. ; In Sinclair BASIC strings can be more than twenty thousand characters long
  17519. ; so a sixteen-bit register is required to store the length
  17520.  
  17521. ;; len
  17522. L3674:  CALL    L2BF1           ; routine STK-FETCH to fetch and delete the
  17523.                                 ; string parameters from the calculator stack.
  17524.                                 ; register BC now holds the length of string.
  17525.  
  17526.         JP      L2D2B           ; jump back to STACK-BC to save result on the
  17527.                                 ; calculator stack (with memory check).
  17528.  
  17529. ; -------------------------
  17530. ; Decrease the counter (35)
  17531. ; -------------------------
  17532. ; The calculator has an instruction that decrements a single-byte
  17533. ; pseudo-register and makes consequential relative jumps just like
  17534. ; the Z80's DJNZ instruction.
  17535.  
  17536. ;; dec-jr-nz
  17537. L367A:  EXX                     ; switch in set that addresses code
  17538.  
  17539.         PUSH    HL              ; save pointer to offset byte
  17540.         LD      HL,$5C67        ; address BREG in system variables
  17541.         DEC     (HL)            ; decrement it
  17542.         POP     HL              ; restore pointer
  17543.  
  17544.         JR      NZ,L3687        ; to JUMP-2 if not zero
  17545.  
  17546.         INC     HL              ; step past the jump length.
  17547.         EXX                     ; switch in the main set.
  17548.         RET                     ; return.
  17549.  
  17550. ; Note. as a general rule the calculator avoids using the IY register
  17551. ; otherwise the cumbersome 4 instructions in the middle could be replaced by
  17552. ; dec (iy+$2d) - three bytes instead of six.
  17553.  
  17554.  
  17555. ; ---------
  17556. ; Jump (33)
  17557. ; ---------
  17558. ; This enables the calculator to perform relative jumps just like
  17559. ; the Z80 chip's JR instruction
  17560.  
  17561. ;; jump
  17562. ;; JUMP
  17563. L3686:  EXX                     ;switch in pointer set
  17564.  
  17565. ;; JUMP-2
  17566. L3687:  LD      E,(HL)          ; the jump byte 0-127 forward, 128-255 back.
  17567.         LD      A,E             ; transfer to accumulator.
  17568.         RLA                     ; if backward jump, carry is set.
  17569.         SBC     A,A             ; will be $FF if backward or $00 if forward.
  17570.         LD      D,A             ; transfer to high byte.
  17571.         ADD     HL,DE           ; advance calculator pointer forward or back.
  17572.         EXX                     ; switch back.
  17573.         RET                     ; return.
  17574.  
  17575. ; -----------------
  17576. ; Jump on true (00)
  17577. ; -----------------
  17578. ; This enables the calculator to perform conditional relative jumps
  17579. ; dependent on whether the last test gave a true result
  17580.  
  17581. ;; jump-true
  17582. L368F:  INC     DE              ; collect the
  17583.         INC     DE              ; third byte
  17584.         LD      A,(DE)          ; of the test
  17585.         DEC     DE              ; result and
  17586.         DEC     DE              ; backtrack.
  17587.  
  17588.         AND     A               ; is result 0 or 1 ?
  17589.         JR      NZ,L3686        ; back to JUMP if true (1).
  17590.  
  17591.         EXX                     ; else switch in the pointer set.
  17592.         INC     HL              ; step past the jump length.
  17593.         EXX                     ; switch in the main set.
  17594.         RET                     ; return.
  17595.  
  17596. ; -----------------------
  17597. ; End of calculation (38)
  17598. ; -----------------------
  17599. ; The end-calc literal terminates a mini-program written in the Spectrum's
  17600. ; internal language.
  17601.  
  17602. ;; end-calc
  17603. L369B:  POP     AF              ; drop the calculator return address RE-ENTRY
  17604.         EXX                     ; switch to the other set.
  17605.  
  17606.         EX      (SP),HL         ; transfer H'L' to machine stack for the
  17607.                                 ; return address.
  17608.                                 ; when exiting recursion then the previous
  17609.                                 ; pointer is transferred to H'L'.
  17610.  
  17611.         EXX                     ; back to main set.
  17612.         RET                     ; return.
  17613.  
  17614.  
  17615. ; ------------------------
  17616. ; THE 'MODULUS' SUBROUTINE
  17617. ; ------------------------
  17618. ; (offset: $32 'n-mod-m')
  17619. ;
  17620. ;
  17621.  
  17622. ;; n-mod-m
  17623. L36A0:  RST     28H             ;; FP-CALC          17, 3.
  17624.         DB    $C0             ;;st-mem-0          17, 3.
  17625.         DB    $02             ;;delete            17.
  17626.         DB    $31             ;;duplicate         17, 17.
  17627.         DB    $E0             ;;get-mem-0         17, 17, 3.
  17628.         DB    $05             ;;division          17, 17/3.
  17629.         DB    $27             ;;int               17, 5.
  17630.         DB    $E0             ;;get-mem-0         17, 5, 3.
  17631.         DB    $01             ;;exchange          17, 3, 5.
  17632.         DB    $C0             ;;st-mem-0          17, 3, 5.
  17633.         DB    $04             ;;multiply          17, 15.
  17634.         DB    $03             ;;subtract          2.
  17635.         DB    $E0             ;;get-mem-0         2, 5.
  17636.         DB    $38             ;;end-calc          2, 5.
  17637.  
  17638.         RET                     ; return.
  17639.  
  17640.  
  17641. ; ------------------
  17642. ; THE 'INT' FUNCTION
  17643. ; ------------------
  17644. ; (offset $27: 'int' )
  17645. ;
  17646. ; This function returns the integer of x, which is just the same as truncate
  17647. ; for positive numbers. The truncate literal truncates negative numbers
  17648. ; upwards so that -3.4 gives -3 whereas the BASIC INT function has to
  17649. ; truncate negative numbers down so that INT -3.4 is -4.
  17650. ; It is best to work through using, say, +-3.4 as examples.
  17651.  
  17652. ;; int
  17653. L36AF:  RST     28H             ;; FP-CALC              x.    (= 3.4 or -3.4).
  17654.         DB    $31             ;;duplicate             x, x.
  17655.         DB    $36             ;;less-0                x, (1/0)
  17656.         DB    $00             ;;jump-true             x, (1/0)
  17657.         DB    $04             ;;to L36B7, X-NEG
  17658.  
  17659.         DB    $3A             ;;truncate              trunc 3.4 = 3.
  17660.         DB    $38             ;;end-calc              3.
  17661.  
  17662.         RET                     ; return with + int x on stack.
  17663.  
  17664. ; ---
  17665.  
  17666.  
  17667. ;; X-NEG
  17668. L36B7:  DB    $31             ;;duplicate             -3.4, -3.4.
  17669.         DB    $3A             ;;truncate              -3.4, -3.
  17670.         DB    $C0             ;;st-mem-0              -3.4, -3.
  17671.         DB    $03             ;;subtract              -.4
  17672.         DB    $E0             ;;get-mem-0             -.4, -3.
  17673.         DB    $01             ;;exchange              -3, -.4.
  17674.         DB    $30             ;;not                   -3, (0).
  17675.         DB    $00             ;;jump-true             -3.
  17676.         DB    $03             ;;to L36C2, EXIT        -3.
  17677.  
  17678.         DB    $A1             ;;stk-one               -3, 1.
  17679.         DB    $03             ;;subtract              -4.
  17680.  
  17681. ;; EXIT
  17682. L36C2:  DB    $38             ;;end-calc              -4.
  17683.  
  17684.         RET                     ; return.
  17685.  
  17686.  
  17687. ; ----------------
  17688. ; Exponential (26)
  17689. ; ----------------
  17690. ;
  17691. ;
  17692.  
  17693. ;; EXP
  17694. ;; exp
  17695. L36C4:  RST     28H             ;; FP-CALC
  17696.         DB    $3D             ;;re-stack
  17697.         DB    $34             ;;stk-data
  17698.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17699.         DB    $38,$AA,$3B,$29 ;;
  17700.         DB    $04             ;;multiply
  17701.         DB    $31             ;;duplicate
  17702.         DB    $27             ;;int
  17703.         DB    $C3             ;;st-mem-3
  17704.         DB    $03             ;;subtract
  17705.         DB    $31             ;;duplicate
  17706.         DB    $0F             ;;addition
  17707.         DB    $A1             ;;stk-one
  17708.         DB    $03             ;;subtract
  17709.         DB    $88             ;;series-08
  17710.         DB    $13             ;;Exponent: $63, Bytes: 1
  17711.         DB    $36             ;;(+00,+00,+00)
  17712.         DB    $58             ;;Exponent: $68, Bytes: 2
  17713.         DB    $65,$66         ;;(+00,+00)
  17714.         DB    $9D             ;;Exponent: $6D, Bytes: 3
  17715.         DB    $78,$65,$40     ;;(+00)
  17716.         DB    $A2             ;;Exponent: $72, Bytes: 3
  17717.         DB    $60,$32,$C9     ;;(+00)
  17718.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17719.         DB    $21,$F7,$AF,$24 ;;
  17720.         DB    $EB             ;;Exponent: $7B, Bytes: 4
  17721.         DB    $2F,$B0,$B0,$14 ;;
  17722.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  17723.         DB    $7E,$BB,$94,$58 ;;
  17724.         DB    $F1             ;;Exponent: $81, Bytes: 4
  17725.         DB    $3A,$7E,$F8,$CF ;;
  17726.         DB    $E3             ;;get-mem-3
  17727.         DB    $38             ;;end-calc
  17728.  
  17729.         CALL    L2DD5           ; routine FP-TO-A
  17730.         JR      NZ,L3705        ; to N-NEGTV
  17731.  
  17732.         JR      C,L3703         ; to REPORT-6b
  17733.  
  17734.         ADD     A,(HL)          ;
  17735.         JR      NC,L370C        ; to RESULT-OK
  17736.  
  17737.  
  17738. ;; REPORT-6b
  17739. L3703:  RST     08H             ; ERROR-1
  17740.         DB    $05             ; Error Report: Number too big
  17741.  
  17742. ;; N-NEGTV
  17743. L3705:  JR      C,L370E         ; to RSLT-ZERO
  17744.  
  17745.         SUB     (HL)            ;
  17746.         JR      NC,L370E        ; to RSLT-ZERO
  17747.  
  17748.         NEG                     ; Negate
  17749.  
  17750. ;; RESULT-OK
  17751. L370C:  LD      (HL),A          ;
  17752.         RET                     ; return.
  17753.  
  17754. ; ---
  17755.  
  17756.  
  17757. ;; RSLT-ZERO
  17758. L370E:  RST     28H             ;; FP-CALC
  17759.         DB    $02             ;;delete
  17760.         DB    $A0             ;;stk-zero
  17761.         DB    $38             ;;end-calc
  17762.  
  17763.         RET                     ; return.
  17764.  
  17765.  
  17766. ; ----------------------
  17767. ; Natural logarithm (25)
  17768. ; ----------------------
  17769. ;
  17770. ;
  17771.  
  17772. ;; ln
  17773. L3713:  RST     28H             ;; FP-CALC
  17774.         DB    $3D             ;;re-stack
  17775.         DB    $31             ;;duplicate
  17776.         DB    $37             ;;greater-0
  17777.         DB    $00             ;;jump-true
  17778.         DB    $04             ;;to L371C, VALID
  17779.  
  17780.         DB    $38             ;;end-calc
  17781.  
  17782.  
  17783. ;; REPORT-Ab
  17784. L371A:  RST     08H             ; ERROR-1
  17785.         DB    $09             ; Error Report: Invalid argument
  17786.  
  17787. ;; VALID
  17788. L371C:  DB    $A0             ;;stk-zero
  17789.         DB    $02             ;;delete
  17790.         DB    $38             ;;end-calc
  17791.         LD      A,(HL)          ;
  17792.  
  17793.         LD      (HL),$80        ;
  17794.         CALL    L2D28           ; routine STACK-A
  17795.  
  17796.         RST     28H             ;; FP-CALC
  17797.         DB    $34             ;;stk-data
  17798.         DB    $38             ;;Exponent: $88, Bytes: 1
  17799.         DB    $00             ;;(+00,+00,+00)
  17800.         DB    $03             ;;subtract
  17801.         DB    $01             ;;exchange
  17802.         DB    $31             ;;duplicate
  17803.         DB    $34             ;;stk-data
  17804.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17805.         DB    $4C,$CC,$CC,$CD ;;
  17806.         DB    $03             ;;subtract
  17807.         DB    $37             ;;greater-0
  17808.         DB    $00             ;;jump-true
  17809.         DB    $08             ;;to L373D, GRE.8
  17810.  
  17811.         DB    $01             ;;exchange
  17812.         DB    $A1             ;;stk-one
  17813.         DB    $03             ;;subtract
  17814.         DB    $01             ;;exchange
  17815.         DB    $38             ;;end-calc
  17816.  
  17817.         INC     (HL)            ;
  17818.  
  17819.         RST     28H             ;; FP-CALC
  17820.  
  17821. ;; GRE.8
  17822. L373D:  DB    $01             ;;exchange
  17823.         DB    $34             ;;stk-data
  17824.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17825.         DB    $31,$72,$17,$F8 ;;
  17826.         DB    $04             ;;multiply
  17827.         DB    $01             ;;exchange
  17828.         DB    $A2             ;;stk-half
  17829.         DB    $03             ;;subtract
  17830.         DB    $A2             ;;stk-half
  17831.         DB    $03             ;;subtract
  17832.         DB    $31             ;;duplicate
  17833.         DB    $34             ;;stk-data
  17834.         DB    $32             ;;Exponent: $82, Bytes: 1
  17835.         DB    $20             ;;(+00,+00,+00)
  17836.         DB    $04             ;;multiply
  17837.         DB    $A2             ;;stk-half
  17838.         DB    $03             ;;subtract
  17839.         DB    $8C             ;;series-0C
  17840.         DB    $11             ;;Exponent: $61, Bytes: 1
  17841.         DB    $AC             ;;(+00,+00,+00)
  17842.         DB    $14             ;;Exponent: $64, Bytes: 1
  17843.         DB    $09             ;;(+00,+00,+00)
  17844.         DB    $56             ;;Exponent: $66, Bytes: 2
  17845.         DB    $DA,$A5         ;;(+00,+00)
  17846.         DB    $59             ;;Exponent: $69, Bytes: 2
  17847.         DB    $30,$C5         ;;(+00,+00)
  17848.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  17849.         DB    $90,$AA         ;;(+00,+00)
  17850.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  17851.         DB    $70,$6F,$61     ;;(+00)
  17852.         DB    $A1             ;;Exponent: $71, Bytes: 3
  17853.         DB    $CB,$DA,$96     ;;(+00)
  17854.         DB    $A4             ;;Exponent: $74, Bytes: 3
  17855.         DB    $31,$9F,$B4     ;;(+00)
  17856.         DB    $E7             ;;Exponent: $77, Bytes: 4
  17857.         DB    $A0,$FE,$5C,$FC ;;
  17858.         DB    $EA             ;;Exponent: $7A, Bytes: 4
  17859.         DB    $1B,$43,$CA,$36 ;;
  17860.         DB    $ED             ;;Exponent: $7D, Bytes: 4
  17861.         DB    $A7,$9C,$7E,$5E ;;
  17862.         DB    $F0             ;;Exponent: $80, Bytes: 4
  17863.         DB    $6E,$23,$80,$93 ;;
  17864.         DB    $04             ;;multiply
  17865.         DB    $0F             ;;addition
  17866.         DB    $38             ;;end-calc
  17867.  
  17868.         RET                     ; return.
  17869.  
  17870.  
  17871. ; -----------------------------
  17872. ; THE 'TRIGONOMETRIC' FUNCTIONS
  17873. ; -----------------------------
  17874. ; Trigonometry is rocket science. It is also used by carpenters and pyramid
  17875. ; builders.
  17876. ; Some uses can be quite abstract but the principles can be seen in simple
  17877. ; right-angled triangles. Triangles have some special properties -
  17878. ;
  17879. ; 1) The sum of the three angles is always PI radians (180 degrees).
  17880. ;    Very helpful if you know two angles and wish to find the third.
  17881. ; 2) In any right-angled triangle the sum of the squares of the two shorter
  17882. ;    sides is equal to the square of the longest side opposite the right-angle.
  17883. ;    Very useful if you know the length of two sides and wish to know the
  17884. ;    length of the third side.
  17885. ; 3) Functions sine, cosine and tangent enable one to calculate the length
  17886. ;    of an unknown side when the length of one other side and an angle is
  17887. ;    known.
  17888. ; 4) Functions arcsin, arccosine and arctan enable one to calculate an unknown
  17889. ;    angle when the length of two of the sides is known.
  17890.  
  17891. ;---------------------------------
  17892. ; THE 'REDUCE ARGUMENT' SUBROUTINE
  17893. ;---------------------------------
  17894. ; (offset $39: 'get-argt')
  17895. ;
  17896. ; This routine performs two functions on the angle, in radians, that forms
  17897. ; the argument to the sine and cosine functions.
  17898. ; First it ensures that the angle 'wraps round'. That if a ship turns through
  17899. ; an angle of, say, 3*PI radians (540 degrees) then the net effect is to turn
  17900. ; through an angle of PI radians (180 degrees).
  17901. ; Secondly it converts the angle in radians to a fraction of a right angle,
  17902. ; depending within which quadrant the angle lies, with the periodicity
  17903. ; resembling that of the desired sine value.
  17904. ; The result lies in the range -1 to +1.              
  17905. ;
  17906. ;                     90 deg.
  17907. ;
  17908. ;                     (pi/2)
  17909. ;              II       +1        I
  17910. ;                       |
  17911. ;        sin+      |\   |   /|    sin+
  17912. ;        cos-      | \  |  / |    cos+
  17913. ;        tan-      |  \ | /  |    tan+
  17914. ;                  |   \|/)  |          
  17915. ; 180 deg. (pi) 0 -|----+----|-- 0  (0)   0 degrees
  17916. ;                  |   /|\   |
  17917. ;        sin-      |  / | \  |    sin-
  17918. ;        cos-      | /  |  \ |    cos+
  17919. ;        tan+      |/   |   \|    tan-
  17920. ;                       |
  17921. ;              III      -1       IV
  17922. ;                     (3pi/2)
  17923. ;
  17924. ;                     270 deg.
  17925. ;
  17926.  
  17927. ;; get-argt
  17928. L3783:  RST     28H             ;; FP-CALC      X.
  17929.         DB    $3D             ;;re-stack
  17930.         DB    $34             ;;stk-data
  17931.         DB    $EE             ;;Exponent: $7E,
  17932.                                 ;;Bytes: 4
  17933.         DB    $22,$F9,$83,$6E ;;              X, 1/(2*PI)
  17934.         DB    $04             ;;multiply      X/(2*PI) = fraction
  17935.         DB    $31             ;;duplicate
  17936.         DB    $A2             ;;stk-half
  17937.         DB    $0F             ;;addition
  17938.         DB    $27             ;;int
  17939.  
  17940.         DB    $03             ;;subtract      now range -.5 to .5
  17941.  
  17942.         DB    $31             ;;duplicate
  17943.         DB    $0F             ;;addition      now range -1 to 1.
  17944.         DB    $31             ;;duplicate
  17945.         DB    $0F             ;;addition      now range -2 to +2.
  17946.  
  17947. ; quadrant I (0 to +1) and quadrant IV (-1 to 0) are now correct.
  17948. ; quadrant II ranges +1 to +2.
  17949. ; quadrant III ranges -2 to -1.
  17950.  
  17951.         DB    $31             ;;duplicate     Y, Y.
  17952.         DB    $2A             ;;abs           Y, abs(Y).    range 1 to 2
  17953.         DB    $A1             ;;stk-one       Y, abs(Y), 1.
  17954.         DB    $03             ;;subtract      Y, abs(Y)-1.  range 0 to 1
  17955.         DB    $31             ;;duplicate     Y, Z, Z.
  17956.         DB    $37             ;;greater-0     Y, Z, (1/0).
  17957.  
  17958.         DB    $C0             ;;st-mem-0         store as possible sign
  17959.                                 ;;                 for cosine function.
  17960.  
  17961.         DB    $00             ;;jump-true
  17962.         DB    $04             ;;to L37A1, ZPLUS  with quadrants II and III.
  17963.  
  17964. ; else the angle lies in quadrant I or IV and value Y is already correct.
  17965.  
  17966.         DB    $02             ;;delete        Y.   delete the test value.
  17967.         DB    $38             ;;end-calc      Y.
  17968.  
  17969.         RET                     ; return.       with Q1 and Q4           >>>
  17970.  
  17971. ; ---
  17972.  
  17973. ; the branch was here with quadrants II (0 to 1) and III (1 to 0).
  17974. ; Y will hold -2 to -1 if this is quadrant III.
  17975.  
  17976. ;; ZPLUS
  17977. L37A1:  DB    $A1             ;;stk-one         Y, Z, 1.
  17978.         DB    $03             ;;subtract        Y, Z-1.       Q3 = 0 to -1
  17979.         DB    $01             ;;exchange        Z-1, Y.
  17980.         DB    $36             ;;less-0          Z-1, (1/0).
  17981.         DB    $00             ;;jump-true       Z-1.
  17982.         DB    $02             ;;to L37A8, YNEG
  17983.                                 ;;if angle in quadrant III
  17984.  
  17985. ; else angle is within quadrant II (-1 to 0)
  17986.  
  17987.         DB    $1B             ;;negate          range +1 to 0.
  17988.  
  17989. ;; YNEG
  17990. L37A8:  DB    $38             ;;end-calc        quadrants II and III correct.
  17991.  
  17992.         RET                     ; return.
  17993.  
  17994.  
  17995. ;----------------------
  17996. ; THE 'COSINE' FUNCTION
  17997. ;----------------------
  17998. ; (offset $20: 'cos')
  17999. ; Cosines are calculated as the sine of the opposite angle rectifying the
  18000. ; sign depending on the quadrant rules.
  18001. ;
  18002. ;
  18003. ;           /|
  18004. ;        h /y|
  18005. ;         /  |o
  18006. ;        /x  |
  18007. ;       /----|    
  18008. ;         a
  18009. ;
  18010. ; The cosine of angle x is the adjacent side (a) divided by the hypotenuse 1.
  18011. ; However if we examine angle y then a/h is the sine of that angle.
  18012. ; Since angle x plus angle y equals a right-angle, we can find angle y by
  18013. ; subtracting angle x from pi/2.
  18014. ; However it's just as easy to reduce the argument first and subtract the
  18015. ; reduced argument from the value 1 (a reduced right-angle).
  18016. ; It's even easier to subtract 1 from the angle and rectify the sign.
  18017. ; In fact, after reducing the argument, the absolute value of the argument
  18018. ; is used and rectified using the test result stored in mem-0 by 'get-argt'
  18019. ; for that purpose.
  18020. ;
  18021.  
  18022. ;; cos
  18023. L37AA:  RST     28H             ;; FP-CALC              angle in radians.
  18024.         DB    $39             ;;get-argt              X     reduce -1 to +1
  18025.  
  18026.         DB    $2A             ;;abs                   ABS X.   0 to 1
  18027.         DB    $A1             ;;stk-one               ABS X, 1.
  18028.         DB    $03             ;;subtract              now opposite angle
  18029.                                 ;;                      although sign is -ve.
  18030.  
  18031.         DB    $E0             ;;get-mem-0             fetch the sign indicator
  18032.         DB    $00             ;;jump-true
  18033.         DB    $06             ;;fwd to L37B7, C-ENT
  18034.                                 ;;forward to common code if in QII or QIII.
  18035.  
  18036.         DB    $1B             ;;negate                else make sign +ve.
  18037.         DB    $33             ;;jump
  18038.         DB    $03             ;;fwd to L37B7, C-ENT
  18039.                                 ;; with quadrants I and IV.
  18040.  
  18041. ;--------------------
  18042. ; THE 'SINE' FUNCTION
  18043. ;--------------------
  18044. ; (offset $1F: 'sin')
  18045. ; This is a fundamental transcendental function from which others such as cos
  18046. ; and tan are directly, or indirectly, derived.
  18047. ; It uses the series generator to produce Chebyshev polynomials.
  18048. ;
  18049. ;
  18050. ;           /|
  18051. ;        1 / |
  18052. ;         /  |x
  18053. ;        /a  |
  18054. ;       /----|    
  18055. ;         y
  18056. ;
  18057. ; The 'get-argt' function is designed to modify the angle and its sign
  18058. ; in line with the desired sine value and afterwards it can launch straight
  18059. ; into common code.
  18060.  
  18061. ;; sin
  18062. L37B5:  RST     28H             ;; FP-CALC      angle in radians
  18063.         DB    $39             ;;get-argt      reduce - sign now correct.
  18064.  
  18065. ;; C-ENT
  18066. L37B7:  DB    $31             ;;duplicate
  18067.         DB    $31             ;;duplicate
  18068.         DB    $04             ;;multiply
  18069.         DB    $31             ;;duplicate
  18070.         DB    $0F             ;;addition
  18071.         DB    $A1             ;;stk-one
  18072.         DB    $03             ;;subtract
  18073.  
  18074.         DB    $86             ;;series-06
  18075.         DB    $14             ;;Exponent: $64, Bytes: 1
  18076.         DB    $E6             ;;(+00,+00,+00)
  18077.         DB    $5C             ;;Exponent: $6C, Bytes: 2
  18078.         DB    $1F,$0B         ;;(+00,+00)
  18079.         DB    $A3             ;;Exponent: $73, Bytes: 3
  18080.         DB    $8F,$38,$EE     ;;(+00)
  18081.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18082.         DB    $15,$63,$BB,$23 ;;
  18083.         DB    $EE             ;;Exponent: $7E, Bytes: 4
  18084.         DB    $92,$0D,$CD,$ED ;;
  18085.         DB    $F1             ;;Exponent: $81, Bytes: 4
  18086.         DB    $23,$5D,$1B,$EA ;;
  18087.         DB    $04             ;;multiply
  18088.         DB    $38             ;;end-calc
  18089.  
  18090.         RET                     ; return.
  18091.  
  18092. ;-----------------------
  18093. ; THE 'TANGENT' FUNCTION
  18094. ;-----------------------
  18095. ; (offset $21: 'tan')
  18096. ;
  18097. ; Evaluates tangent x as    sin(x) / cos(x).
  18098. ;
  18099. ;
  18100. ;           /|
  18101. ;        h / |
  18102. ;         /  |o
  18103. ;        /x  |
  18104. ;       /----|    
  18105. ;         a
  18106. ;
  18107. ; the tangent of angle x is the ratio of the length of the opposite side
  18108. ; divided by the length of the adjacent side. As the opposite length can
  18109. ; be calculates using sin(x) and the adjacent length using cos(x) then
  18110. ; the tangent can be defined in terms of the previous two functions.
  18111.  
  18112. ; Error 6 if the argument, in radians, is too close to one like pi/2
  18113. ; which has an infinite tangent. e.g. PRINT TAN (PI/2)  evaluates as 1/0.
  18114. ; Similarly PRINT TAN (3*PI/2), TAN (5*PI/2) etc.
  18115.  
  18116. ;; tan
  18117. L37DA:  RST     28H             ;; FP-CALC          x.
  18118.         DB    $31             ;;duplicate         x, x.
  18119.         DB    $1F             ;;sin               x, sin x.
  18120.         DB    $01             ;;exchange          sin x, x.
  18121.         DB    $20             ;;cos               sin x, cos x.
  18122.         DB    $05             ;;division          sin x/cos x (= tan x).
  18123.         DB    $38             ;;end-calc          tan x.
  18124.  
  18125.         RET                     ; return.
  18126.  
  18127. ;----------------------
  18128. ; THE 'ARCTAN' FUNCTION
  18129. ;----------------------
  18130. ; (Offset $24: 'atn')
  18131. ; the inverse tangent function with the result in radians.
  18132. ; This is a fundamental transcendental function from which others such as asn
  18133. ; and acs are directly, or indirectly, derived.
  18134. ; It uses the series generator to produce Chebyshev polynomials.
  18135.  
  18136. ;; atn
  18137. L37E2:  CALL    L3297           ; routine re-stack
  18138.         LD      A,(HL)          ; fetch exponent byte.
  18139.         CP      $81             ; compare to that for 'one'
  18140.         JR      C,L37F8         ; forward, if less, to SMALL
  18141.  
  18142.         RST     28H             ;; FP-CALC
  18143.         DB    $A1             ;;stk-one
  18144.         DB    $1B             ;;negate
  18145.         DB    $01             ;;exchange
  18146.         DB    $05             ;;division
  18147.         DB    $31             ;;duplicate
  18148.         DB    $36             ;;less-0
  18149.         DB    $A3             ;;stk-pi/2
  18150.         DB    $01             ;;exchange
  18151.         DB    $00             ;;jump-true
  18152.         DB    $06             ;;to L37FA, CASES
  18153.  
  18154.         DB    $1B             ;;negate
  18155.         DB    $33             ;;jump
  18156.         DB    $03             ;;to L37FA, CASES
  18157.  
  18158. ;; SMALL
  18159. L37F8:  RST     28H             ;; FP-CALC
  18160.         DB    $A0             ;;stk-zero
  18161.  
  18162. ;; CASES
  18163. L37FA:  DB    $01             ;;exchange
  18164.         DB    $31             ;;duplicate
  18165.         DB    $31             ;;duplicate
  18166.         DB    $04             ;;multiply
  18167.         DB    $31             ;;duplicate
  18168.         DB    $0F             ;;addition
  18169.         DB    $A1             ;;stk-one
  18170.         DB    $03             ;;subtract
  18171.         DB    $8C             ;;series-0C
  18172.         DB    $10             ;;Exponent: $60, Bytes: 1
  18173.         DB    $B2             ;;(+00,+00,+00)
  18174.         DB    $13             ;;Exponent: $63, Bytes: 1
  18175.         DB    $0E             ;;(+00,+00,+00)
  18176.         DB    $55             ;;Exponent: $65, Bytes: 2
  18177.         DB    $E4,$8D         ;;(+00,+00)
  18178.         DB    $58             ;;Exponent: $68, Bytes: 2
  18179.         DB    $39,$BC         ;;(+00,+00)
  18180.         DB    $5B             ;;Exponent: $6B, Bytes: 2
  18181.         DB    $98,$FD         ;;(+00,+00)
  18182.         DB    $9E             ;;Exponent: $6E, Bytes: 3
  18183.         DB    $00,$36,$75     ;;(+00)
  18184.         DB    $A0             ;;Exponent: $70, Bytes: 3
  18185.         DB    $DB,$E8,$B4     ;;(+00)
  18186.         DB    $63             ;;Exponent: $73, Bytes: 2
  18187.         DB    $42,$C4         ;;(+00,+00)
  18188.         DB    $E6             ;;Exponent: $76, Bytes: 4
  18189.         DB    $B5,$09,$36,$BE ;;
  18190.         DB    $E9             ;;Exponent: $79, Bytes: 4
  18191.         DB    $36,$73,$1B,$5D ;;
  18192.         DB    $EC             ;;Exponent: $7C, Bytes: 4
  18193.         DB    $D8,$DE,$63,$BE ;;
  18194.         DB    $F0             ;;Exponent: $80, Bytes: 4
  18195.         DB    $61,$A1,$B3,$0C ;;
  18196.         DB    $04             ;;multiply
  18197.         DB    $0F             ;;addition
  18198.         DB    $38             ;;end-calc
  18199.  
  18200.         RET                     ; return.
  18201.  
  18202.  
  18203. ;----------------------
  18204. ; THE 'ARCSIN' FUNCTION
  18205. ;----------------------
  18206. ; (Offset $22: 'asn')
  18207. ; the inverse sine function with result in radians.
  18208. ; derived from arctan function above.
  18209. ; Error A unless the argument is between -1 and +1 inclusive.
  18210. ; uses an adaptation of the formula asn(x) = atn(x/sqr(1-x*x))
  18211. ;
  18212. ;
  18213. ;           /|
  18214. ;        1 / |
  18215. ;         /  |x
  18216. ;        /a  |
  18217. ;       /----|    
  18218. ;         y
  18219. ;
  18220. ; e.g. we know the opposite side (x) and hypotenuse (1)
  18221. ; and we wish to find angle a in radians.
  18222. ; we can derive length y by Pythagorus and then use ATN instead.
  18223. ; since y*y + x*x = 1*1 (Pythagorus Theorem) then
  18224. ; y=sqr(1-x*x)                         - no need to multiply 1 by itself.
  18225. ; so, asn(a) = atn(x/y)
  18226. ; or more fully,
  18227. ; asn(a) = atn(x/sqr(1-x*x))
  18228.  
  18229. ; Close but no cigar.
  18230.  
  18231. ; While PRINT ATN (x/SQR (1-x*x)) gives the same results as PRINT ASN x,
  18232. ; it leads to division by zero when x is 1 or -1.
  18233. ; To overcome this, 1 is added to y giving half the required angle and the
  18234. ; result is then doubled.
  18235. ; That is PRINT ATN (x/(SQR (1-x*x) +1)) *2
  18236. ; A value higher than 1 gives the required error as attempting to find  the
  18237. ; square root of a negative number generates an error in Sinclair BASIC.
  18238.  
  18239. ;; asn
  18240. L3833:  RST     28H             ;; FP-CALC      x.
  18241.         DB    $31             ;;duplicate     x, x.
  18242.         DB    $31             ;;duplicate     x, x, x.
  18243.         DB    $04             ;;multiply      x, x*x.
  18244.         DB    $A1             ;;stk-one       x, x*x, 1.
  18245.         DB    $03             ;;subtract      x, x*x-1.
  18246.         DB    $1B             ;;negate        x, 1-x*x.
  18247.         DB    $28             ;;sqr           x, sqr(1-x*x) = y
  18248.         DB    $A1             ;;stk-one       x, y, 1.
  18249.         DB    $0F             ;;addition      x, y+1.
  18250.         DB    $05             ;;division      x/y+1.
  18251.         DB    $24             ;;atn           a/2       (half the angle)
  18252.         DB    $31             ;;duplicate     a/2, a/2.
  18253.         DB    $0F             ;;addition      a.
  18254.         DB    $38             ;;end-calc      a.
  18255.  
  18256.         RET                     ; return.
  18257.  
  18258.  
  18259. ;-------------------------
  18260. ; THE 'ARCCOS' FUNCTION
  18261. ;-------------------------
  18262. ; (Offset $23: 'acs')
  18263. ; the inverse cosine function with the result in radians.
  18264. ; Error A unless the argument is between -1 and +1.
  18265. ; Result in range 0 to pi.
  18266. ; Derived from asn above which is in turn derived from the preceding atn.
  18267. ; It could have been derived directly from atn using acs(x) = atn(sqr(1-x*x)/x).
  18268. ; However, as sine and cosine are horizontal translations of each other,
  18269. ; uses acs(x) = pi/2 - asn(x)
  18270.  
  18271. ; e.g. the arccosine of a known x value will give the required angle b in
  18272. ; radians.
  18273. ; We know, from above, how to calculate the angle a using asn(x).
  18274. ; Since the three angles of any triangle add up to 180 degrees, or pi radians,
  18275. ; and the largest angle in this case is a right-angle (pi/2 radians), then
  18276. ; we can calculate angle b as pi/2 (both angles) minus asn(x) (angle a).
  18277. ;
  18278. ;
  18279. ;           /|
  18280. ;        1 /b|
  18281. ;         /  |x
  18282. ;        /a  |
  18283. ;       /----|    
  18284. ;         y
  18285. ;
  18286.  
  18287. ;; acs
  18288. L3843:  RST     28H             ;; FP-CALC      x.
  18289.         DB    $22             ;;asn           asn(x).
  18290.         DB    $A3             ;;stk-pi/2      asn(x), pi/2.
  18291.         DB    $03             ;;subtract      asn(x) - pi/2.
  18292.         DB    $1B             ;;negate        pi/2 -asn(x)  =  acs(x).
  18293.         DB    $38             ;;end-calc      acs(x).
  18294.  
  18295.         RET                     ; return.
  18296.  
  18297.  
  18298. ; --------------------------
  18299. ; THE 'SQUARE ROOT' FUNCTION
  18300. ; --------------------------
  18301. ; (Offset $28: 'sqr')
  18302. ; This routine is remarkable only in its brevity - 7 bytes.
  18303. ; It wasn't written here but in the ZX81 where the programmers had to squeeze
  18304. ; a bulky operating sytem into an 8K ROM. It simply calculates
  18305. ; the square root by stacking the value .5 and continuing into the 'to-power'
  18306. ; routine. With more space available the much faster Newton-Raphson method
  18307. ; should have been used as on the Jupiter Ace.
  18308.  
  18309. ;; sqr
  18310. L384A:  RST     28H             ;; FP-CALC
  18311.         DB    $31             ;;duplicate
  18312.         DB    $30             ;;not
  18313.         DB    $00             ;;jump-true
  18314.         DB    $1E             ;;to L386C, LAST
  18315.  
  18316.         DB    $A2             ;;stk-half
  18317.         DB    $38             ;;end-calc
  18318.  
  18319.  
  18320. ; ------------------------------
  18321. ; THE 'EXPONENTIATION' OPERATION
  18322. ; ------------------------------
  18323. ; (Offset $06: 'to-power')
  18324. ; This raises the first number X to the power of the second number Y.
  18325. ; As with the ZX80,
  18326. ; 0 ^ 0 = 1.
  18327. ; 0 ^ +n = 0.
  18328. ; 0 ^ -n = arithmetic overflow.
  18329. ;
  18330.  
  18331. ;; to-power
  18332. L3851:  RST     28H             ;; FP-CALC              X, Y.
  18333.         DB    $01             ;;exchange              Y, X.
  18334.         DB    $31             ;;duplicate             Y, X, X.
  18335.         DB    $30             ;;not                   Y, X, (1/0).
  18336.         DB    $00             ;;jump-true
  18337.         DB    $07             ;;to L385D, XISO   if X is zero.
  18338.  
  18339. ; else X is non-zero. Function 'ln' will catch a negative value of X.
  18340.  
  18341.         DB    $25             ;;ln                    Y, LN X.
  18342.         DB    $04             ;;multiply              Y * LN X.
  18343.         DB    $38             ;;end-calc
  18344.  
  18345.         JP      L36C4           ; jump back to EXP routine   ->
  18346.  
  18347. ; ---
  18348.  
  18349. ; these routines form the three simple results when the number is zero.
  18350. ; begin by deleting the known zero to leave Y the power factor.
  18351.  
  18352. ;; XISO
  18353. L385D:  DB    $02             ;;delete                Y.
  18354.         DB    $31             ;;duplicate             Y, Y.
  18355.         DB    $30             ;;not                   Y, (1/0).
  18356.         DB    $00             ;;jump-true
  18357.         DB    $09             ;;to L386A, ONE         if Y is zero.
  18358.  
  18359.         DB    $A0             ;;stk-zero              Y, 0.
  18360.         DB    $01             ;;exchange              0, Y.
  18361.         DB    $37             ;;greater-0             0, (1/0).
  18362.         DB    $00             ;;jump-true             0.
  18363.         DB    $06             ;;to L386C, LAST        if Y was any positive
  18364.                                 ;;                      number.
  18365.  
  18366. ; else force division by zero thereby raising an Arithmetic overflow error.
  18367. ; There are some one and two-byte alternatives but perhaps the most formal
  18368. ; might have been to use end-calc; rst 08; DB 05.
  18369.  
  18370.         DB    $A1             ;;stk-one               0, 1.
  18371.         DB    $01             ;;exchange              1, 0.
  18372.         DB    $05             ;;division              1/0        ouch!
  18373.  
  18374. ; ---
  18375.  
  18376. ;; ONE
  18377. L386A:  DB    $02             ;;delete                .
  18378.         DB    $A1             ;;stk-one               1.
  18379.  
  18380. ;; LAST
  18381. L386C:  DB    $38             ;;end-calc              last value is 1 or 0.
  18382.  
  18383.         RET                     ; return.               Whew!
  18384.  
  18385. ;*********************************
  18386. ;** Spectrum 128 Patch Routines **
  18387. ;*********************************
  18388.  
  18389. ; The new code added to the standard 48K Spectrum ROM is mainly devoted to the scanning and decoding of the keypad.
  18390. ; 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',
  18391. ; 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.
  18392. ; Documented by Paul Farrow.
  18393.  
  18394. ; --------------------------------
  18395. ; SCAN THE KEYPAD AND THE KEYBOARD
  18396. ; --------------------------------
  18397. ; This patch will attempt to scan the keypad if in 128K mode and will then scan the keyboard.
  18398.  
  18399. ;; KEYS
  18400. L386E:  PUSH    IX
  18401.         BIT     4,(IY+$01)      ; [FLAGS] Test if in 128K mode
  18402.         JR      Z,L3879         ; Z=in 48K mode
  18403.  
  18404.         CALL    L3A42           ; Attempt to scan the keypad
  18405.  
  18406. ;; KEYS_CONT
  18407. L3879:  CALL    L02BF           ; Scan the keyboard
  18408.         POP     IX
  18409.         RET
  18410.  
  18411. ; ----------------------------------
  18412. ; READ THE STATE OF THE OUTPUT LINES
  18413. ; ----------------------------------
  18414. ; 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.
  18415. ; 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.
  18416.  
  18417. ;; READ_OUTPUTS
  18418. L387F:  LD      C,$FD           ; FFFD = Address of the
  18419.         LD      D,$FF           ; command register (register 7)
  18420.         LD      E,$BF           ; BFFD = Address of the
  18421.         LD      B,D             ; data register (register 14)
  18422.         LD      A,$07
  18423.         OUT     (C),A           ; Select command register
  18424.         IN      H,(C)           ; Read its status
  18425.         LD      A,$0E
  18426.         OUT     (C),A           ; Select data register
  18427.         IN      A,(C)           ; Read its status
  18428.         OR      $F0             ; Mask off the input lines
  18429.         LD      L,A             ; L=state of output lines at the
  18430.         RET                     ; keypad socket
  18431.  
  18432. ; --------------------------
  18433. ; SET THE OUTPUT LINE, BIT 0
  18434. ; --------------------------
  18435. ; The output line to the keypad is set via the LSB of L.
  18436.  
  18437. ;; SET_REG14
  18438. L3896:  LD      B,D
  18439.         LD      A,$0E
  18440.         OUT     (C),A           ; Select the data register
  18441.         LD      B,E
  18442.         OUT     (C),L           ; Send L out to the data register
  18443.         RET                     ; Set the output line
  18444.  
  18445. ; ----------------------------------------
  18446. ; FETCH THE STATE OF THE INPUT LINE, BIT 5
  18447. ; ----------------------------------------
  18448. ; Return the state of the input line from the keypad in bit 5 of A.
  18449.  
  18450. ;; GET_REG14
  18451. L389F:  LD      B,D
  18452.         LD      A,$0E
  18453.         OUT     (C),A           ; Select the data register
  18454.         IN      A,(C)           ; Read the input line
  18455.         RET
  18456.  
  18457. ; ------------------------------
  18458. ; SET THE OUTPUT LINE LOW, BIT 0
  18459. ; ------------------------------
  18460.  
  18461. ;; RESET_LINE
  18462. L38A7:  LD      A,L
  18463.         AND     $FE             ; Reset bit 0 of L
  18464.         LD      L,A
  18465.         JR      L3896           ; Send out L to the data register
  18466.  
  18467. ; -------------------------------
  18468. ; SET THE OUTPUT LINE HIGH, BIT 0
  18469. ; -------------------------------
  18470.  
  18471. ;; SET_LINE
  18472. L38AD:  LD      A,L
  18473.         OR      $01             ; Set bit 0 of L
  18474.         LD      L,A
  18475.         JR      L3896           ; Send out L to the data register
  18476.  
  18477. ; -------------------
  18478. ; MINOR DELAY ROUTINE
  18479. ; -------------------
  18480. ; Delay for (B*13)+5 T-States.
  18481.  
  18482. ;; DELAY
  18483. L38B3:  DJNZ    L38B3
  18484.         RET
  18485.  
  18486. ; -------------------
  18487. ; MAJOR DELAY ROUTINE
  18488. ; -------------------
  18489. ; Delay for (B*271)+5 T-states.
  18490.  
  18491. ;; DELAY2
  18492. L38B6:  PUSH    BC
  18493.         LD      B,$10
  18494.         CALL    L38B3           ; Inner delay of 135 T-States
  18495.         POP     BC
  18496.         DJNZ    L38B6
  18497.         RET
  18498.  
  18499. ; ------------------------------------
  18500. ; MONITOR FOR THE INPUT LINE TO GO LOW
  18501. ; ------------------------------------
  18502. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18503.  
  18504. ;; MON_B5_LO
  18505. L38C0:  PUSH    BC
  18506.         CALL    L389F           ; Read the state of the input line
  18507.         POP     BC
  18508.         AND     $20             ; Test bit 5, the input line
  18509.         JR      Z,L38CB         ; Exit if input line found low
  18510.         DJNZ    L38C0           ; Repeat until timeout expires
  18511.  
  18512. ;; EXT_MON_LO
  18513. L38CB:  RET
  18514.  
  18515. ; -------------------------------------
  18516. ; MONITOR FOR THE INPUT LINE TO GO HIGH
  18517. ; -------------------------------------
  18518. ; Monitor the input line, bit 5, for up to (B*108)+5 T-states.
  18519.  
  18520. ;; MON_B5_HI
  18521. L38CC:  PUSH    BC
  18522.         CALL    L389F           ; Read the state of the input line
  18523.         POP     BC
  18524.         AND     $20             ; Test bit 5, the input line
  18525.         JR      NZ,L38D7        ; Exit if input line found low
  18526.         DJNZ    L38CC           ; Repeat until timeout expires
  18527.  
  18528. ;; EXT_MON_HI
  18529. L38D7:  RET
  18530.  
  18531. ; -------------------------
  18532. ; READ KEY PRESS STATUS BIT
  18533. ; -------------------------
  18534. ; 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.
  18535.  
  18536. ;; READ_STATUS
  18537. L38D8:  CALL    L387F           ; Read the output lines
  18538.         LD      B,$01           ; Read in one bit
  18539.         JR      L38E4
  18540.  
  18541. ; ----------------
  18542. ; READ IN A NIBBLE
  18543. ; ----------------
  18544. ; 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.
  18545. ; For a nibble of key press data, a bit read in as 1 indicates that the corresponding key was pressed.
  18546.  
  18547. ;; READ_NIBBLE
  18548. L38DF:  CALL    L387F           ; Read the state of the output lines
  18549.         LD      B,$04           ; Read in four bits
  18550.  
  18551. ;; READ_BIT
  18552. L38E4:  PUSH    BC
  18553.         CALL    L389F           ; Read the input line from the keypad
  18554.         POP     BC
  18555.         AND     $20             ; This line should initially be high
  18556.         JR      Z,L392D         ; Z=read in a 0, there must be an error
  18557.  
  18558.         XOR     A               ; The bits read in will be stored in register A
  18559.  
  18560. ;; BIT_LOOP
  18561. L38EE:  PUSH    BC              ; Preserve the loop count and any bits
  18562.         PUSH    AF              ; read in so far
  18563.         CALL    L38AD           ; Set the output line high
  18564.  
  18565.         LD      B,$A3           ; Monitor for 17609 T-states for the
  18566.         CALL    L38C0           ; input line to go low
  18567.         JR      NZ,L392B        ; NZ=the line did not go low
  18568.  
  18569.         CALL    L38A7           ; Set the output line low
  18570.         JR      L3901           ; Insert a delay of 12 T-states
  18571.  
  18572. L38FF:  DB    $FF, $FF
  18573.  
  18574. ;; BL_CONTINUE
  18575. L3901:  LD      B,$2B           ; Delay for 564 T-states
  18576.         CALL    L38B3
  18577.         CALL    L389F           ; Read in the bit value
  18578.         BIT     5,A
  18579.         JR      Z,L3911         ; Z=read in a 0
  18580.  
  18581.         POP     AF              ; Retrieve read in bits
  18582.         SCF                     ; Set carry bit
  18583.         JR      L3914
  18584.  
  18585. ;; BL_READ_0
  18586. L3911:  POP     AF              ; Retrieve read in bits
  18587.         SCF
  18588.         CCF                     ; Clear carry bit
  18589.  
  18590. ;; BL_STORE
  18591. L3914:  RRA                     ; Shift the carry bit into bit 0 of A
  18592.         PUSH    AF              ; Save bits read in
  18593.         CALL    L38AD           ; Set the output line high
  18594.  
  18595.         LD      B,$26           ; Delay for 499 T-states
  18596.         CALL    L38B3
  18597.  
  18598.         CALL    L38A7           ; Set the output line low
  18599.  
  18600.         LD      B,$23           ; Delay for 460 T-states
  18601.         CALL    L38B3
  18602.  
  18603.         POP     AF              ; Retrieve read in bits
  18604.         POP     BC              ; Retrieve loop counter and repeat
  18605.         DJNZ    L38EE           ; for all bits to read in
  18606.         RET
  18607.  
  18608. ; ----------
  18609. ; LINE ERROR
  18610. ; ----------
  18611. ; 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.
  18612. ; The upper nibble of system variable FLAGS/ROW3 will be cleared to indicate that communications to the keypad is no longer in progress.
  18613.  
  18614. ;; LINE_ERROR
  18615. L392B:  POP     AF
  18616.         POP     BC              ; Clear the stack
  18617.  
  18618. ;; LINE_ERROR2
  18619. L392D:  CALL    L38AD           ; Set the output line high
  18620.  
  18621.         XOR     A               ; Clear FLAGS nibble
  18622.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18623.  
  18624.         INC     A               ; Return zero flag reset
  18625.         SCF
  18626.         CCF                     ; Return carry flag reset
  18627.         RET
  18628.  
  18629. ; ---------------
  18630. ; POLL THE KEYPAD
  18631. ; ---------------
  18632. ; 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.
  18633. ; 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.
  18634. ; 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:
  18635. ;
  18636. ; A Register    Zero Flag       Carry Flag    Cause
  18637. ; 0             set             set           Communications already established
  18638. ; 0             set             reset         Nibble read in OK
  18639. ; 1             reset           reset         Nibble read in with an error or i/p line initially low
  18640. ; 1             reset           set           Poll counter has not yet reached zero
  18641. ;
  18642. ; The third bit of the nibble read in must be set for the poll to be subsequently accepted.
  18643.  
  18644. ;; ATTEMPT_POLL
  18645. L3938:  CALL    L387F           ; Read the output line states
  18646.  
  18647.         LD      A,($5B88)       ; [FLAGS/ROW3] Has communications already been
  18648.         AND     $80             ; established with the keypad?
  18649.         JR      NZ,L3999        ; NZ=yes, so skip the poll
  18650.  
  18651.         CALL    L389F           ; Read the input line
  18652.         AND     $20             ; It should be high initially
  18653.         JR      Z,L392D         ; Z=error, input line found low
  18654.  
  18655.         LD      A,($5B88)       ; [FLAGS/ROW3] Test if poll counter already zero thus
  18656.         AND     A               ; indicating a previous comms error
  18657.         JR      NZ,L395A        ; NZ=ready to poll the keypad
  18658.  
  18659.         INC     A               ; Indicate comms not established
  18660.         LD      ($5B88),A       ; [FLAGS/ROW3]
  18661.         LD      A,$4C           ; Reset the poll counter
  18662.         LD      ($5B89),A       ; [ROW2/ROW1]
  18663.         JR      L399C           ; Exit the routine
  18664.  
  18665. ;;POLL_KEYPAD
  18666. L395A:  LD      A,($5B89)       ; [ROW2/ROW1] Decrement the poll counter
  18667.         DEC     A
  18668.         LD      ($5B89),A       ; [ROW2/ROW1]
  18669.         JR      NZ,L399C        ; Exit the routine if it is not yet zero
  18670.  
  18671. ; The poll counter has reached zero so a poll of the keypad can now occur.
  18672.  
  18673.         XOR     A
  18674.         LD      ($5B88),A       ; [FLAGS/ROW3] Indicate that a poll can occur
  18675.         LD      ($5B89),A       ; [ROW2/ROW1]
  18676.         LD      ($5B8A),A       ; [ROW4/ROW5] Clear all the row nibble stores
  18677.  
  18678.         CALL    L38A7           ; Set the output line low
  18679.  
  18680.         LD      B,$21           ; Wait up to 3569 T-States for the
  18681.         CALL    L38C0           ; input line to go low
  18682.         JR      NZ,L392D        ; NZ=line did not go low
  18683.  
  18684.         CALL    L38AD           ; Set the output line high
  18685.  
  18686.         LD      B,$24           ; Wait up to 3893 T-States for the
  18687.         CALL    L38CC           ; input line to go high
  18688.         JR      Z,L392D         ; NZ=line did not go high
  18689.  
  18690.         CALL    L38A7           ; Set the output line low
  18691.  
  18692.         LD      B,$0F
  18693.         CALL    L38B6           ; Delay for 4070 T-States
  18694.         CALL    L38DF           ; Read in a nibble of data
  18695.         JR      NZ,L392D        ; NZ=error occurred when reading in nibble
  18696.  
  18697.         SET     7,A             ; Set bit 7
  18698.         AND     $F0             ; Keep only the upper four bits
  18699.                                 ; (Bit 6 will be set if poll successful)
  18700.         LD      ($5B88),A       ; [FLAGS/ROW3] Store the flags nibble
  18701.         XOR     A
  18702.         SRL     A               ; Exit: Zero flag set, Carry flag reset
  18703.         RET
  18704.  
  18705. ;; AP_SKIP_POLL
  18706. L3999:  XOR     A               ; Communications already established
  18707.         SCF                     ; Exit: Zero flag set, Carry flag set
  18708.         RET
  18709.  
  18710. ;; PK_EXIT
  18711. L399C:  XOR     A               ; Poll counter not zero
  18712.         INC     A
  18713.         SCF                     ; Exit: Zero flag reset, Carry flag set
  18714.         RET
  18715.  
  18716. ; -----------------------
  18717. ; SCAN THE KEYPAD ROUTINE
  18718. ; -----------------------
  18719. ; If a successful poll of the keypad occurs then the five rows of keys are read in and a unique key code generated.
  18720.  
  18721. ;; KEYPAD_SCAN
  18722. L39A0:  CALL    L3938           ; Try to poll the keypad
  18723.  
  18724.         LD      A,($5B88)       ; [FLAGS/ROW3] Test the flags nibble
  18725.         CPL
  18726.         AND     $C0             ; Bits 6 and 7 must be set in FLAGS
  18727.         RET     NZ              ; NZ=poll was not successful
  18728.  
  18729. ; The poll was successful so now read in data for the five keypad rows.
  18730.  
  18731.         LD      IX,$5B8A        ; [ROW4/ROW5]
  18732.         LD      B,$05           ; The five rows
  18733.  
  18734. ;; KS_LOOP
  18735. L39B0:  PUSH    BC              ; Save counter
  18736.  
  18737.         CALL    L38D8           ; Read the key press status bit
  18738.         JP      NZ,L3A3A        ; NZ=error occurred
  18739.  
  18740.         BIT     7,A             ; Test the bit read in
  18741.         JR      Z,L39DC         ; Z=no key pressed in this row
  18742.  
  18743.         CALL    L38DF           ; Read in the row's nibble of data
  18744.         JR      NZ,L3A3A        ; NZ=error occurred
  18745.  
  18746.         POP     BC              ; Fetch the nibble loop counter
  18747.         PUSH    BC
  18748.         LD      C,A             ; Move the nibble read in to C
  18749.         LD      A,(IX+$00)      ; Fetch the nibble store
  18750.         BIT     0,B             ; Test if an upper or lower nibble
  18751.         JR      Z,L39D6         ; Z=upper nibble
  18752.  
  18753.         SRL     C               ; Shift the nibble to the lower position
  18754.         SRL     C
  18755.         SRL     C
  18756.         SRL     C
  18757.         AND     $F0             ; Mask off the lower nibble of the
  18758.         JR      L39D8           ; nibble store
  18759.  
  18760. ;; KS_UPPER
  18761. L39D6:  AND     $0F             ; Mask off the upper nibble of the nibble store
  18762.  
  18763. ;; KS_STORE
  18764. L39D8:  OR      C               ; Combine the existing and new
  18765.         LD      (IX+$00),A      ; nibbles and store them
  18766.  
  18767. ;; KS_NEXT
  18768. L39DC:  POP     BC              ; Retrieve the row counter
  18769.         BIT     0,B             ; Test if next nibble store is required
  18770.         JR      NZ,L39E3        ; NZ=use same nibble store
  18771.  
  18772.         DEC     IX              ; Point to the next nibble store
  18773.  
  18774. ;; KS_NEW
  18775. L39E3:  DJNZ    L39B0           ; Repeat for the next keypad row
  18776.  
  18777. ; All five rows have now been read so compose a unique code for the key pressed.
  18778.  
  18779.         LD      E,$80           ; Signal no key press found yet
  18780.         LD      IX,$5B88        ; [FLAGS/ROW3]
  18781.         LD      HL,$3A3F        ; Point to the key mask data
  18782.         LD      B,$03           ; Scan three nibbles
  18783.  
  18784. ;; GEN_LOOP
  18785. L39F0:  LD      A,(IX+$00)      ; Fetch a pair of nibbles
  18786.         AND     (HL)            ; This will mask off the FLAGS nibble and the SHIFT/0 key
  18787.  
  18788.         JR      Z,L3A17         ; Z=no key pressed in these nibbles
  18789.  
  18790.         BIT     7,E             ; Test if a key has already been found
  18791.         JR      Z,L3A3C         ; Z=multiple keys pressed
  18792.  
  18793.         PUSH    BC              ; Save the loop counter
  18794.         PUSH    AF              ; Save the byte of key bit data
  18795.         LD      A,B             ; Move loop counter to A
  18796.         JR      L3A01           ; A delay of 12 T-States
  18797.  
  18798. L39FF:  DB    $FF, $FF        ; Unused locations
  18799.  
  18800. ;; GEN_CONT
  18801. L3A01:  DEC     A               ; These lines of code generate base
  18802.         SLA     A               ; values of 7, 15 and 23 for the three
  18803.         SLA     A               ; nibble stores 5B88, 5B89 & 5B8A.
  18804.         SLA     A
  18805.         OR      $07
  18806.         LD      B,A             ; B=(loop counter-1)*8+7
  18807.         POP     AF              ; Fetch the byte of key press data
  18808.  
  18809. ;; GEN_BIT
  18810. L3A0C:  SLA     A               ; Shift until a set key bit drops into the
  18811.         JP      C,L3A13         ; carry flag
  18812.  
  18813.         DJNZ    L3A0C           ; Decrement B for each 'unsuccessful' shift of the A register
  18814.  
  18815. ;; GEN_FOUND
  18816. L3A13:  LD      E,B             ; E=a unique number for the key pressed, between 1 - 19 except 2 & 3
  18817.  
  18818.         POP     BC              ; As a result shifting the set key bit
  18819.                                 ; into the carry flag, the A register will
  18820.                                 ; hold 00 if only one key was pressed
  18821.         JR      NZ,L3A3C        ; NZ=multiple keys pressed
  18822.  
  18823. ;; GEN_NEXT
  18824. L3A17:  INC     IX              ; Point to the next nibble store
  18825.         INC     HL              ; Point to the corresponding mask data
  18826.         DJNZ    L39F0           ; Repeat for all three 'nibble' bytes
  18827.  
  18828.         BIT     7,E             ; Test if any keys were pressed
  18829.         JR      NZ,L3A27        ; NZ=no keys were pressed
  18830.  
  18831.         LD      A,E             ; Copy the key code
  18832.         AND     $FC             ; Test for the '.' key (E=1)
  18833.         JR      Z,L3A27         ; Z='.' key pressed
  18834.  
  18835.         DEC     E
  18836.         DEC     E               ; Key code in range 2 - 17
  18837.  
  18838. ; The E register now holds a unique key code value between 1 and 17.
  18839.  
  18840. ;; GEN_POINT
  18841. L3A27:  LD      A,($5B8A)       ; [ROW4/ROW5] Test if the SHIFT key was pressed
  18842.         AND     $08
  18843.         JR      Z,L3A34         ; Z=the SHIFT key was not pressed
  18844.  
  18845. ; The SHIFT key was pressed or no key was pressed.
  18846.  
  18847.         LD      A,E             ; Fetch the key code
  18848.         AND     $7F             ; Mask off 'no key pressed' bit
  18849.         ADD     A,$12           ; Add on a shift offset of 12
  18850.         LD      E,A
  18851.  
  18852. ; 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.
  18853.  
  18854. ;; GEN_NOSHIFT
  18855. L3A34:  LD      A,E
  18856.         ADD     A,$5A           ; Add a base offset of 5A
  18857.         LD      E,A             ; Return key codes in range 5B - 7D
  18858.         XOR     A
  18859.         RET                     ; Exit: Zero flag set, key found OK
  18860.  
  18861. ; 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.
  18862.  
  18863. ;; KS_ERROR
  18864. L3A3A:  POP     BC              ; Clear the stack and exit
  18865.         RET                     ; Exit: Zero flag reset
  18866.  
  18867. ;; GEN_INVALID
  18868. L3A3C:  XOR     A               ; Exit: Zero flag reset indicating an
  18869.         INC     A               ; invalid key press
  18870.         RET
  18871.  
  18872. ; ----------------
  18873. ; KEYPAD MASK DATA
  18874. ; ----------------
  18875.  
  18876. ;; KEY_MASKS
  18877. L3A3F:  DB    $0F, $FF, $F2   ; Key mask data
  18878.  
  18879. ; ---------------
  18880. ; READ THE KEYPAD
  18881. ; ---------------
  18882. ; 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.
  18883. ; 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.
  18884. ; 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.
  18885. ; The KSTATE system variables store the following data:
  18886. ;
  18887. ;       KSTATE0/4       Un-decoded Key Value (00-27 for keyboard, 5B-7D for keypad, FF for no key)
  18888. ;       KSTATE1/5       10 Call Counter
  18889. ;       KSTATE2/6       Repeat Delay
  18890. ;       KSTATE3/7       Decoded Key Value
  18891. ;
  18892. ; The code returned is then stored in system variable LAST_K (5C08) and a new key signalled by setting bit 5 of FLAGS (5C3B).
  18893. ;
  18894. ; 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.
  18895. ; 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
  18896. ; 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.
  18897. ;
  18898. ; 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
  18899. ; 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
  18900. ; a return upon every other call to KEYPAD and then to have used a '5 Call Counter' just as the keyboard routine does.
  18901. ;
  18902. ; 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
  18903. ; 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
  18904. ; decrement the KSTATE system variable Call Counters. The keypad routine 'knows' of the existence of keyboard key codes but the reverse is not true.
  18905.  
  18906. ;; KEYPAD
  18907. L3A42:  LD      E,$80           ; Signal no key pressed
  18908.         LD      A,($5C78)       ; [FRAMES]
  18909.         AND     $01             ; Scan the keypad every other
  18910.         JR      NZ,L3A4F        ; interrupt
  18911.  
  18912.         CALL    L39A0
  18913.         RET     NZ              ; NZ=no valid key pressed
  18914.  
  18915. ;; KP_CHECK
  18916. L3A4F:  LD HL,$5C00             ; [KSTATE0] Test the first KSTATE variable
  18917.  
  18918. ;; KP_LOOP
  18919. L3A52:  BIT     7,(HL)          ; Is the set free?
  18920.         JR      NZ,L3A62        ; NZ=yes
  18921.  
  18922.         LD      A,(HL)          ; Fetch the un-decoded key value
  18923.         CP      $5B             ; Is it a keyboard code?
  18924.         JR      C,L3A62         ; C=yes, so do not decrement counter
  18925.  
  18926.         INC     HL
  18927.         DEC     (HL)            ; Decrement the 10 Call Counter
  18928.         DEC     HL
  18929.         JR      NZ,L3A62        ; If the counter reaches zero, then
  18930.                                 ; signal the set is free
  18931.         LD      (HL),$FF
  18932.  
  18933. ;; KP_CH_SET
  18934. L3A62:  LD      A,L             ; Jump back and test the second set if
  18935.         LD      HL,$5C04        ; [KSTATE4] not yet considered
  18936.         CP      L
  18937.         JR      NZ,L3A52
  18938.  
  18939.         CALL    L3AAE           ; Test for valid key combinations and
  18940.         RET     NZ              ; return if invalid
  18941.  
  18942.         LD      A,E             ; Test if the key in the first set is being
  18943.         LD      HL,$5C00        ; [KSTATE0] repeated
  18944.         CP      (HL)
  18945.         JR      Z,L3A9E         ; Jump if being repeated
  18946.  
  18947.         EX      DE,HL           ; Save the address of KSTATE0
  18948.         LD      HL,$5C04        ; [KSTATE4] Test if the key in the second set is
  18949.         CP      (HL)            ; being repeated
  18950.         JR      Z,L3A9E         ; Jump if being repeated
  18951.  
  18952. ; A new key will not be accepted unless one of the KSTATE sets is free.
  18953.  
  18954.         BIT     7,(HL)          ; Test if the second set is free
  18955.         JR      NZ,L3A83        ; Jump if set is free
  18956.  
  18957.         EX      DE,HL
  18958.         BIT     7,(HL)          ; Test if the first set is free
  18959.         RET     Z               ; Return if no set is free
  18960.  
  18961. ;; KP_NEW
  18962. L3A83:  LD      E,A             ; Pass the key code to the E register
  18963.         LD      (HL),A          ; and to KSTATE0/4
  18964.         INC     HL
  18965.         LD      (HL),$0A        ; Set the '10 Call Counter' to 10
  18966.         INC     HL
  18967.  
  18968.         LD      A,($5C09)       ; [REPDEL] Fetch the initial repeat delay
  18969.         SRL     A               ; Divide delay by two
  18970.         LD      (HL),A          ; Store the repeat delay
  18971.         INC     HL
  18972.  
  18973.         CALL    L3AD7           ; Decode the keypad key code
  18974.         LD      (HL),E          ; and store it in KSTATE3/7
  18975.  
  18976. ; This section is common for both new keys and repeated keys.
  18977.  
  18978. ;; KP_END
  18979. L3A94:  LD      A,E
  18980.         LD      ($5C08),A       ; [LAST_K] Store the key value in LAST_K
  18981.         LD      HL,$5C3B        ; FLAGS
  18982.         SET     5,(HL)          ; Signal a new key pressed
  18983.         RET
  18984.  
  18985. ; -------------------------
  18986. ; THE KEY REPEAT SUBROUTINE
  18987. ; -------------------------
  18988.  
  18989. ;; KP_REPEAT
  18990. L3A9E:  INC     HL
  18991.         LD      (HL),$0A        ; Reset the '10 Call Counter' to 10
  18992.         INC     HL
  18993.         DEC     (HL)            ; Decrement the repeat delay
  18994.         RET     NZ              ; Return if not zero
  18995.  
  18996.         LD      A,($5C0A)       ; [REPPER] The subsequent repeat delay is
  18997.         SRL     A               ; divided by two and stored
  18998.         LD      (HL),A
  18999.         INC     HL
  19000.         LD      E,(HL)          ; The key repeating is fetched
  19001.         JR      L3A94           ; and then returned in LAST_K
  19002.  
  19003. ; ----------------------------------------
  19004. ; THE TEST FOR A VALID KEY CODE SUBROUTINE
  19005. ; ----------------------------------------
  19006. ; 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.
  19007.  
  19008. ;; KP_TEST
  19009. L3AAE:  LD      A,E
  19010.         LD      HL,$5B66        ; FLAGS3 Test if in BASIC or EDIT mode
  19011.         BIT     0,(HL)
  19012.         JR      Z,L3ABC         ; Z=EDIT mode
  19013.  
  19014. ; Test key codes when in BASIC/CALCULATOR mode
  19015.  
  19016.         CP      $6D             ; Test for shifted keys
  19017.         JR      NC,L3AD4        ; and signal an error if found
  19018.  
  19019. ;; KPT_OK
  19020. L3ABA:  XOR     A               ; Signal valid key code
  19021.         RET                     ; Exit: Zero flag set
  19022.  
  19023. ; Test key codes when in EDIT/MENU mode.
  19024.  
  19025. ;; KPT_EDIT
  19026. L3ABC:  CP      $80             ; Test for no key press
  19027.         JR      NC,L3AD4        ; NC=no key press
  19028.  
  19029.         CP      $6C             ; Test for SHIFT on its own
  19030.         JR      NZ,L3ABA        ; NZ=valid key code
  19031.  
  19032. L3AC4:  DB    $00, $00, $00   ; Delay for 64 T-States
  19033.         DB    $00, $00, $00
  19034.         DB    $00, $00, $00
  19035.         DB    $00, $00, $00
  19036.         DB    $00, $00, $00
  19037.         DB    $00
  19038.  
  19039. ;; KPT_INVALID
  19040. L3AD4:  XOR     A               ; Signal invalid key code
  19041.         INC     A
  19042.         RET                     ; Exit: Zero flag reset
  19043.  
  19044. ; ---------------------------
  19045. ; THE KEY DECODING SUBROUTINE
  19046. ; ---------------------------
  19047.  
  19048. ;; KP_DECODE
  19049. L3AD7:  PUSH    HL              ; Save the KSTATE pointer
  19050.         LD      A,E
  19051.         SUB     $5B             ; Reduce the key code range to
  19052.         LD      D,$00           ; 00 - 22 and transfer to DE
  19053.         LD      E,A
  19054.  
  19055.         LD      HL,$5B66        ; FLAGS3 Test if in EDIT or BASIC mode
  19056.         BIT     0,(HL)
  19057.         JR      Z,L3AEA         ; Z=EDIT/MENU mode
  19058.  
  19059. ; Use Table 1 when in CALCULATOR/BASIC mode.
  19060.  
  19061.         LD      HL,L3B13
  19062.         JR      L3B0F           ; Look up the key value
  19063.  
  19064. ; Deal with EDIT/MENU mode.
  19065.  
  19066. ;; KPD_EDIT
  19067. L3AEA:  LD      HL,L3B25        ; Use Table 4 for unshifted key
  19068.         CP      $11             ; presses
  19069.         JR      C,L3B0F
  19070.  
  19071. ; Deal with shifted keys in EDIT/MENU mode.
  19072.  
  19073. ; 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,
  19074. ; it actually performs no function when editing a BASIC program.
  19075.  
  19076.         LD      HL,L3B21
  19077.         CP      $15             ; Test for SHIFT 1
  19078.         JR      Z,L3B0F
  19079.  
  19080.         CP      $16             ; Test for SHIFT 2
  19081.         JR      Z,L3B0F
  19082.  
  19083.         JR      L3B01           ; Delay for 12 T-States
  19084.  
  19085. L3AFE:  DB    $00, $FF, $FF   ; Unused locations
  19086.  
  19087. ;; KPD_CONT
  19088. L3B01:  CP      $17             ; Test for SHIFT 3
  19089.         JR      Z,L3B0F
  19090.  
  19091. ; Use Table 2 with SHIFT 4 (delete to beginning of word) and SHIFT 5 (delete to end of word).
  19092.  
  19093.         LD      HL,L3B18
  19094.         CP      $21             ; Test for SHIFT 4 and above
  19095.         JR      NC,L3B0F
  19096.  
  19097. ;Use Table 1 for all other shifted key presses.
  19098.  
  19099.         LD      HL,L3B13
  19100.  
  19101. ;; KPD_EXIT
  19102. L3B0F:  ADD     HL,DE           ; Look up the key value
  19103.         LD      E,(HL)
  19104.         POP     HL              ; Retrieve the KSTATE address
  19105.         RET
  19106.  
  19107. ; --------------------------------
  19108. ; THE KEYPAD DECODE LOOK-UP TABLES
  19109. ; --------------------------------
  19110.  
  19111. ;; KPD_TABLE1
  19112. L3B13:  DB    $2E, $0D, $33   ; '.', ENTER, 3
  19113.         DB    $32, $31        ; 2, 1
  19114.  
  19115. ;; KPD_TABLE2
  19116. L3B18:  DB    $29, $28, $2A   ; ), (, *
  19117.         DB    $2F, $2D, $39   ; /, - , 9
  19118.         DB    $38, $37, $2B   ; 8, 7, +
  19119.  
  19120. ;; KPD_TABLE3
  19121. L3B21:  DB    $36, $35, $34   ; 6, 5, 4
  19122.         DB    $30             ; 0
  19123.  
  19124. ;; KPD_TABLE4
  19125. L3B25:  DB    $A5, $0D, $A6   ; Bottom, ENTER, Top
  19126.         DB    $A7, $A8, $A9   ; End of line, Start of line, TOGGLE
  19127.         DB    $AA, $0B, $0C   ; DEL right, Up, DEL
  19128.         DB    $07, $09, $0A   ; CMND, Right, Down
  19129.         DB    $08, $AC, $AD   ; Left, Down ten, Up ten
  19130.         DB    $AE, $AF        ; End word, Beginning of word
  19131.         DB    $B0, $B1, $B2   ; DEL to end of line, DEL to start of line, SHIFT TOGGLE
  19132.         DB    $B3, $B4        ; DEL to end of word, DEL to beginning of word
  19133.  
  19134. ; -----------------------------
  19135. ; PRINT NEW ERROR MESSAGE PATCH
  19136. ; -----------------------------
  19137.  
  19138. L3B3B:  BIT     4,(IY+$01)      ; FLAGS 3 - In 128K mode?
  19139.         JR      NZ,L3B46        ; NZ=128K mode
  19140.  
  19141. ; In 48K mode
  19142.  
  19143.         XOR     A               ; Replicate code from standard ROM that the patch over-wrote
  19144.         LD      DE,$1536
  19145.         RET    
  19146.  
  19147. ; In 128K mode
  19148.  
  19149. L3B46:  LD      HL,$010F        ; Vector table entry in Editor ROM -> JP $03A2
  19150.  
  19151. ; Return to Editor ROM at address in HL
  19152.  
  19153. L3B49:  EX      (SP),HL         ; Change the return address
  19154.         JP      $5B00           ; Page Editor ROM and return to the address on the stack
  19155.  
  19156. ; -------------------------------------
  19157. ; STATEMENT INTERPRETATION RETURN PATCH
  19158. ; -------------------------------------
  19159.  
  19160. L3B4D:  BIT     4,(IY+$01)      ; In 128K mode?
  19161.         JR      NZ,L3B58        ; NZ=128K mode
  19162.  
  19163. ; In 48K mode
  19164.  
  19165.         BIT     7,(IY+$0A)      ; replicate code from standard ROM that the patch over-wrote
  19166.         RET    
  19167.  
  19168. ; In 128K mode
  19169.  
  19170. L3B58:  LD      HL,$0112        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #182A
  19171.         JR      L3B49
  19172.  
  19173. ; --------------------------
  19174. ; GO TO NEXT STATEMENT PATCH
  19175. ; --------------------------
  19176.  
  19177. L3B5D:  BIT     4,(IY+$01)      ; In 128K mode?
  19178.         JR      NZ,L3B67        ; NZ=128K mode
  19179.  
  19180. ; In 48K mode
  19181.  
  19182.         RST     18H             ; replicate code from standard ROM that the patch over-wrote
  19183.         CP      $0D
  19184.         RET    
  19185.  
  19186. ; In 128K mode
  19187.  
  19188. L3B67:  LD      HL,$0115        ; Handle in Editor ROM by jumping to Vector table entry in Editor ROM -> JP #18A8
  19189.         JR      L3B49
  19190.  
  19191. ; --------------------------------------
  19192. ; INKEY$ ROUTINE TO DEAL WITH THE KEYPAD
  19193. ; --------------------------------------
  19194.  
  19195. ;; KEYSCAN2
  19196. L3B6C:  CALL    L028E           ; KEYSCAN Scan the keyboard
  19197.         LD      C,$00
  19198.         JR      NZ,L3B80        ; NZ=multiple keys
  19199.  
  19200.         CALL    L031E           ; K_TEST
  19201.         JR      NC,L3B80        ; NC=shift only or no key
  19202.  
  19203.         DEC     D
  19204.         LD      E,A
  19205.         CALL    L0333           ; K_DECODE
  19206.         JP      L2657           ; S_CONT Get string and continue scanning
  19207.  
  19208. ;; KPI_SCAN
  19209. L3B80:  BIT     4,(IY+$01)      ; 128K mode?
  19210.         JP      Z,L2660         ; S_IK$_STK Z=no, stack keyboard code
  19211.  
  19212.         DI                      ; Disable interrupts whilst scanning
  19213.         CALL    L39A0           ; the keypad
  19214.         EI
  19215.         JR      NZ,L3B9A        ; NZ=multiple keys
  19216.  
  19217.         CALL    L3AAE           ; Test the keypad
  19218.         JR      NZ,L3B9A        ; NZ=no key, shift only or invalid combination
  19219.  
  19220.         CALL    L3AD7           ; Form the key code
  19221.         LD      A,E
  19222.         JP      L2657           ; S_CONT Get string and continue scanning
  19223.  
  19224. ;; KPI_INVALID
  19225. L3B9A:  LD      C,$00           ; Signal no key, i.e. length=0
  19226.         JP      L2660           ; S_IK$_STK
  19227.  
  19228. ; ---------------------
  19229. ; PRINT TOKEN/UDG PATCH
  19230. ; ---------------------
  19231.  
  19232. L3B9F:  CP      $A3             ; SPECTRUM (T)
  19233.         JR      Z,L3BAF
  19234.  
  19235.         CP      $A4             ; PLAY (U)
  19236.         JR      Z,L3BAF
  19237.  
  19238. ; In 48K mode here
  19239.  
  19240. L3BA7:  SUB     $A5             ; Check as per original ROM
  19241.         JP      NC,$0B5F
  19242.  
  19243.         JP      $0B56           ; Rejoin original ROM routine
  19244.  
  19245. L3BAF:  BIT     4,(IY+$01)      ; FLAGS3 - Bit 4=1 if in 128K mode
  19246.         JR      Z,L3BA7         ; Rejoin code for when in 48K mode
  19247.  
  19248. ; In 128K mode here
  19249.  
  19250.         LD      DE,L3BC9
  19251.         PUSH    DE              ; Stack return address
  19252.  
  19253.         SUB     $A3             ; Check whether the SPECTRUM token
  19254.  
  19255.         LD      DE,L3BD2        ; SPECTRUM token
  19256.         JR      Z,L3BC3
  19257.  
  19258.         LD      DE,L3BDA        ; PLAY token
  19259.  
  19260. L3BC3:  LD      A,$04           ; Signal not RND, INKEY$ or PI so that a trailing space is printed
  19261.         PUSH    AF
  19262.         JP      L0C17           ; Rejoin printing routine PO-TABLE+3
  19263.  
  19264. ; Return address from above
  19265.  
  19266. L3BC9:  SCF                     ; Return as if no trailing space
  19267.  
  19268.         BIT     1,(IY+$01)      ; Test if printer is in use
  19269.         RET     NZ              ; NZ=printer in use
  19270.  
  19271.         JP      $0B03           ; PO-FETCH - Return via Position Fetch routine
  19272.  
  19273. L3BD2           DC "SPECTRUM"   ;DEFM    "SPECTRU"       ; SPECTRUM token
  19274.                                 ;DB    'M'+$80
  19275.  
  19276. L3BDA           DC "PLAY"       ;DEFM    "PLA"           ; PLAY token
  19277.                                 ;DB    'Y'+$80
  19278.  
  19279. ;; KP_SCAN2
  19280. L3BDE:  JP      L3C01           ; This is not called from either ROM. It can be used to scan the keypad.
  19281.  
  19282. ;===============================
  19283. PRINTER_INITER  RST 0X08
  19284.                 DB AY_PRN_INIT
  19285.                 RET
  19286.  
  19287. PRN_TOKEN       RST 0X08
  19288.                 DB AY_PRN_TOKEN
  19289.                 RET
  19290.  
  19291.                 DUPL 0X3BFF-$,0
  19292.                 DW 0XFFFF
  19293. ;===============================
  19294.  
  19295. ;; KP_SCAN
  19296. 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.
  19297.  
  19298. ; -----------------------
  19299. ; TV TUNER VECTOR ENTRIES
  19300. ; -----------------------
  19301.  
  19302. L3C04:  JP      L3C10
  19303. L3C07:  JP      L3C10
  19304. L3C0A:  JP      L3C10
  19305. L3C0D:  JP      L3C10
  19306.  
  19307. ; ----------------
  19308. ; TV TUNER ROUTINE
  19309. ; ----------------
  19310. ; 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.
  19311. ; 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
  19312. ; 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
  19313. ; shown the year '1986' in varying ink colours. This leads to a display that shows all possible ink colours on all possible paper colours.
  19314.  
  19315. ;; TV_TUNER
  19316. L3C10:  LD      A,$7F           ; Test for the BREAK key
  19317.         IN      A,($FE)
  19318.         RRA    
  19319.         RET     C               ; C=SPACE not pressed
  19320.  
  19321.         LD      A,$FE
  19322.         IN      A,($FE)
  19323.         RRA    
  19324.         RET     C               ; C=SPACE not pressed
  19325.  
  19326.         LD      A,$07
  19327.         OUT     ($FE),A         ; Set the border to white
  19328.  
  19329.         LD      A,$02           ; Open channel 2 (main screen)
  19330.         CALL    $1601
  19331.  
  19332.         XOR     A
  19333.         LD      ($5C3C),A       ; [TV_FLAG] Signal using main screen
  19334.  
  19335.         LD      A,$16           ; Print character 'AT'
  19336.         RST     10H
  19337.  
  19338.         XOR     A               ; Print character '0'
  19339.         RST     10H
  19340.  
  19341.         XOR     A               ; Print character '0'
  19342.         RST     10H
  19343.  
  19344.         LD      E,$08           ; Number of characters per colour
  19345.         LD      B,E             ; Paper counter + 1
  19346.         LD      D,B             ; Ink counter + 1
  19347.  
  19348. ;; TVT_ROW
  19349. L3C34:  LD      A,B             ; Calculate the paper colour
  19350.         DEC     A               ; Bits 3-5 of each screen attribute
  19351.                 DB 0XCB
  19352.                 RLA               ; holds the paper colour; bits 0-2
  19353.                 DB 0XCB
  19354.                 RLA               ; the ink colour
  19355.                 DB 0XCB
  19356.                 RLA
  19357.         ADD     A,D             ; Add the ink colour
  19358.         DEC     A
  19359.         LD      ($5C8F),A       ; [ATTR_T] Store as temporary attribute value
  19360.  
  19361.         LD      HL,L3C8F        ; TVT_DATA Point to the 'year' data
  19362.         LD      C,E             ; Get number of characters to print
  19363.  
  19364. ;; TVT_YEAR
  19365. L3C45:  LD      A,(HL)          ; Fetch a character from the data
  19366.         RST     10H             ; Print it
  19367.         INC     HL
  19368.         DEC     C
  19369.         JR      NZ,L3C45        ; Repeat for the 8 characters
  19370.  
  19371.         DJNZ    L3C34           ; Repeat for all colours in this row
  19372.  
  19373.         LD      B,E             ; Reset paper colour
  19374.         DEC     D               ; Next ink colour
  19375.         JR      NZ,L3C34        ; Produce next row with new ink colour
  19376.  
  19377.         LD      HL,$4800        ; Point to 2nd third of display file
  19378.         LD      D,H
  19379.         LD      E,L
  19380.         INC     DE              ; Point to the next display cell
  19381.         XOR     A
  19382.         LD      (HL),A          ; Clear first display cell
  19383.         LD      BC,$0FFF
  19384.         LDIR                    ; Clear lower 2 thirds of display file
  19385.  
  19386.         EX      DE,HL           ; HL points to start of attributes file
  19387.         LD      DE,$5900        ; Point to 2nd third of attributes file
  19388.         LD      BC,$0200
  19389.         LDIR                    ; Copy screen attributes
  19390.  
  19391. ; 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).
  19392.  
  19393.         DI                      ; Disable interrupts so that a pure tone can be generated
  19394.  
  19395. ;; TVT_TONE
  19396. L3C68:  LD      DE,$0370        ; DE=twice the tone frequency in Hz
  19397.         LD      L,$07           ; Border colour of white
  19398.  
  19399. ;; TVT_DURATION
  19400. L3C6D:  LD      BC,$0099        ; Delay for 950.4us
  19401.  
  19402. ;; TVT_PERIOD
  19403. L3C70:  DEC     BC
  19404.         LD      A,B
  19405.         OR      C
  19406.         JR      NZ,L3C70
  19407.  
  19408.         LD      A,L
  19409.         XOR     $10              ; Toggle the speaker output whilst
  19410.         LD      L,A              ; preserving the border colour
  19411.         OUT     ($FE),A
  19412.  
  19413.         DEC     DE               ; Generate the tone for 1 second
  19414.         LD      A,D
  19415.         OR      E
  19416.         JR      NZ,L3C6D
  19417.  
  19418. ; At this point the speaker is turned off, so delay for 1 second.
  19419.  
  19420.         LD      BC,$0000         ; Delay for 480.4us
  19421.  
  19422. ;; TVT_DELAY1
  19423. L3C83:  DEC     BC
  19424.         LD      A,B
  19425.         OR      C
  19426.         JR      NZ,L3C83
  19427.  
  19428. ;; TVT_DELAY2
  19429. L3C88:  DEC     BC               ; Delay for 480.4us
  19430.         LD      A,B
  19431.         OR      C
  19432.         JR      NZ,L3C88
  19433.  
  19434.         JR      L3C68            ; Repeat the tone cycle
  19435.  
  19436. ;; TVT_DATA
  19437. L3C8F:  DB    $13, $00         ; Bright, off
  19438.         DB    $31, $39         ; '1', '9'
  19439.         DB    $13, $01         ; Bright, on
  19440.         DB    $38, $36         ; '8', '6'
  19441.  
  19442.                 include new_proc.a80
  19443.  
  19444. ; ------
  19445. ; UNUSED
  19446. ; ------
  19447.  
  19448.                 DUPL 0X3D00-$,0
  19449.  
  19450. ; -------------------------------
  19451. ; THE 'ZX SPECTRUM CHARACTER SET'
  19452. ; -------------------------------
  19453.  
  19454. ;; char-set
  19455.  
  19456. ; $20 - Character: ' '          CHR$(32)
  19457.  
  19458. CHARS           binclude shr_3d00.bin
  19459.