forked from MEGA65/mega65-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappendix-basic65.tex
10903 lines (9174 loc) · 348 KB
/
appendix-basic65.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\hypersetup{bookmarksdepth=2}
\chapter{BASIC 65 Command Reference}
\section{Commands, Functions and Operators}
This appendix describes each of the commands, functions, and other callable
elements of BASIC 65, which is an enhanced version of BASIC 10. Some of these
can take one or more arguments, which are pieces of input that you provide as
part of the command or function call, to help describe what you want to achieve.
Some also require that you use special words.
Below is an example of how commands, functions, and operators
(all of which are also known as keywords) will be described in this appendix.
{\bf KEY} number{\bf,} string
Here, {\bf KEY} is a keyword. Keywords are special words that
BASIC understands. Keywords are always written in {\bf BOLD CAPITALS}, so that
you can easily recognise them.
The words not in bold must be replaced for the command, function or operator to
work. In this example, we need to replace number with a numeric expression, and
string with a string expression. We'll explain what expressions are a bit more
in a few moments.
The comma, and some other symbols and punctuation marks, just represent
themselves when they are in bold. In our example here, it means that there must
be a comma between the number and the string.
You might also see symbols and punctuation marks that are not in bold. When
they are not in bold they have a special meaning. You might see square
brackets around something. For example: [{\bf,} numeric expression]. This means
that whatever appears between the square brackets is optional. That is, you can
include it if you need to, but the command, function or operator will also work
just fine without it. For example, the {\bf CIRCLE} command has an optional
numeric argument to indicate if the circle should be filled when being drawn.
This arrangement of keywords, expressions and symbols is what's called syntax. If
you miss something out, or put the wrong thing in the wrong place, it is called
a syntax error. The computer will tell you that you have a syntax error by displaying
a \screentext{?SYNTAX ERROR} message.
There is nothing to worry about if you get an error from the MEGA65.
Instead, it is just the MEGA65's way of telling you that something
isn't quite right, so that you can more easily
find and fix the problem.
Error messages such as this won't hurt the computer or cause any damage to your program,
so there is nothing to worry about.
For example, if we accidentally left the comma out, or replaced it with
a full stop, the MEGA65 will respond with
a \screentext{?SYNTAX ERROR}, similar to what's shown below:
\newpage
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
KEY 8"FISH"
?SYNTAX ERROR
KEY 8."FISH"
?SYNTAX ERROR
\end{verbatim}
\end{tcolorbox}
It is very common for commands, functions and operators to use one or
more {\bf``expressions''}.
An expression is just a fancy name for something that has, or equates to a value.
This could be as simple as a string (\screentext{"HELLO"}), a number
(\screentext{23.7}), or a complex calculation that might include
one or more functions or operators (\screentext{LEN("HELLO") * (3 XOR 7)}).
Generally speaking, expressions can result in either a string or a numeric result.
In this case we call the expressions string expressions or numeric expressions.
For example, \screentext{"HELLO"} is a {\bf string expression}, while
\screentext{23.7} is a {\bf numeric expression}.
It is important to use the correct type of expression when writing your programs.
If you accidentally use the wrong type, the MEGA65 will display a
\screentext{?TYPE MISMATCH ERROR}, to say that the type
of expression you gave doesn't match what it expected. For example, we will get a
\screentext{?TYPE MISMATCH ERROR} if we type the following command,
because \screentext{"POTATO"} is a string expression instead of a numeric expression:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
KEY "POTATO","SOUP"
\end{verbatim}
\end{tcolorbox}
If you wish, you can try typing this in yourself.
Commands are statements that you can use directly from the \screentext{READY.}
prompt, or from within a program, for example:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT "HELLO"
HELLO
10 PRINT "HELLO"
RUN
HELLO
\end{verbatim}
\end{tcolorbox}
You can place a sequence of statements within a single line by separating them with colons, for example:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT "HELLO" : PRINT "HOW ARE YOU?" : PRINT "HOW IS THE WEATHER?"
HELLO
HOW ARE YOU?
HOW IS THE WEATHER?
\end{verbatim}
\end{tcolorbox}
\subsection{Direct Mode Commands}
\index{BASIC 65 Commands!Direct Mode}
Note that some commands are said to only work in {\bf direct mode}. This means that the command can't be part of a BASIC
program, but can be entered directly to the screen. In the two {\bf PRINT} examples above, the first was entered in
direct mode, whereas the second one wasn't. The examples above would work since {\bf PRINT} works in both direct and indirect mode.
\subsection{Command Format Syntax}
The following table describes what the other symbols found in this appendix mean.
\begin{center}
\setlength{\def\arraystretch{1.5}\tabcolsep}{6pt}
\begin{longtable}{|c|L{8cm}|}
\hline
\textbf{Symbol} & \textbf{Meaning}\\
\hline
\endhead
\dots &
The bracket can be repeated zero or more times \\
\hline
[ ] &
Optional \\
\hline
< | > &
Include one of the choices \\
\hline
[ | ] &
Optionally include one of the choices \\
\hline
\{ , \} &
One or more of the arguments is required. The commas to the left of the last
argument included are required. Trailing commas must be omitted. See
{\bf CURSOR} for an example.\\
\hline
[\{ , \}] &
Similar to \{ , \} but all arguments can be omitted\\
\hline
\end{longtable}
\end{center}
\subsection{Fonts}
Whenever there's a piece of text in this appendix that reflects some logic, something you can type,
or something the MEGA65 could display, the text will \screentext{WILL USE THIS FONT}. This helps make it easier
to for you to distinguish between these things and the written text.
\newpage
\subsection{BASIC 65 Constants}
\index{BASIC 65 Constants}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|}
\hline
{\bf Type} & {\bf Example} & {\bf Example} \\
\hline
Decimal Integer & \screentext{32000} & \screentext{-55} \\
Decimal Fixed Point & \screentext{3.14} & \screentext{-7654.321}\\
Decimal Floating Point & \screentext{1.5E03}& \screentext{7.7E-02} \\
Hex & \screentext{\$D020}& \screentext{\$FF} \\
String & \screentext{"X"} & \screentext{"TEXT"} \\
\hline
\end{tabular}
\end{center}
\subsection{BASIC 65 Variables}
\index{BASIC 65 Variables}
Each scalar variable consumes 8 bytes of storage in memory.
The reserved area in bank 0 from \$F700-\$FEFF can store 256 variables.
Variables don't need to be declared, and their type is determined by an appended
character. All variables without an appended character are regarded as REAL by default,
and storage is claimed at their first usage. They are also initialised to zero,
whereas string variables are initialised as an empty string "".
All 104 one-letter variables are declared as {\bf fast} variables.
26 user functions are declared as {\bf fast} functions.
These are the variables (A - Z), (A\% - Z\%), (A\& - Z\&) and (A\$ - Z\$)
and the functions (FNA() - FNZ()).
They have fixed memory addresses in the range \$FD00 - \$FEFF, the address
is generated by a hash algorithm from the variable name. The access to these
variables and functions, either use or definition, is much faster,
than the access to two letter variables and functions.
The address of fast variables and functions is computed by a very fast algorithm,
while the address of two-letter variables and functions is stored in a table,
which has to be searched for every use.
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Type} & {\bf Appended Character} & {\bf Range} & {\bf Example} \\
\hline
Byte & \screentext{\&} & 0 .. 255 & \screentext{BY\& = 23} \\
Integer & \screentext{\%} & -32768 .. 32767 & \screentext{I\% = 5} \\
Real & none & -1E37 .. 1E37 & \screentext{XY = 1/3} \\
String & \screentext{\$} & length = 0 .. 255 & \screentext{AB\$ = "TEXT"}\\
\hline
\end{tabular}
\end{center}
\subsection{BASIC 65 Arrays}
\index{BASIC 65 Arrays}
Each array consumes the number of elements multiplied by the item size,
plus the size of the header (6 + 2 * dimensions) in memory.
For example the array
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
100 DIM X(8,2,3)
\end{verbatim}
\end{tcolorbox}
has 3 dimensions and 108 (9 x 3 x 4) items. You might be asking: Why is it 9 x 3 x 4,
when the program uses 8, 2, and 3? This is because array indexes start at 0, not 1.
The size for real items is 5, so the data of that array above would occupy 540 (5 x 108) bytes.
The header size is 12 bytes (6 + 2 * 3), so the total length in memory is 552 bytes (540 + 12).
\begin{comment}
Arrays are stored in bank 1 starting at address \$2000 and expand upwards.
They share the available memory at \$2000 .. \$F6FF with the string area,
which starts in bank 1 at address \$F6FF, and expands \textit{downwards}.
\end{comment}
Each of the above scalar variable types can be used as an array, by declaring
them with a {\bf DIM} statement. The arrays are initialised to zero for all
elements on declaration. If an undeclared array element is used,
an automatic implicit declaration is performed, which sets the upper boundary
for each dimension to 10. For example, the usage of an undeclared element
\screentext{AB(3,5)} would automatically perform a \screentext{DIM AB(10,10)}.
As noted previously, the lower boundary for each dimension is always 0 (zero),
so an array initialised with \screentext{DIM AB(10)} consists of 11 elements and accepts indexes from
0 to 10.
String arrays are more precisely expressed as \textit{arrays of string
descriptors}. Each item consists of three bytes, which hold
these values: The length of the string, and the 2 byte address (low/high byte)
of the string in string memory.
The usage of the BASIC function {\bf POINTER} with a string or
string array element as the argument, returns the address of the descriptor, not the string itself.
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
\multicolumn{2}{|c|}{\bf Type \& Item Size} & {\bf Appended} & {\bf Range} & {\bf Example} \\
\multicolumn{2}{|c|}{} & {\bf Character} & & \\
\hline
Byte Array & 1 & \screentext{\&} & 0 .. 255 & \screentext{BY\&(5,6) = 23} \\
Integer Array & 2 & \screentext{\%} & -32768 .. 32767 & \screentext{I\%(0,10) = 5} \\
Real Array & 5 & none & -1E37 .. 1E37 & \screentext{XY(I\%) = 1/3} \\
String Array & 3 & \screentext{\$} & 0 .. 255 characters & \screentext{AB\$(X) = "TEXT"} \\
\hline
\end{tabular}
\end{center}
\subsection{Screen memory and colour RAM}
\label{sec:screenmemoryandcolourram}
\index{Screen memory and colour RAM}
Many programmers like to skip the standard routine {\bf PRINT} for screen output
and want to access the screen memory and the colour RAM directly
using the commands {\bf PEEK} and {\bf POKE}.
{\bf BASIC 65} provides a method for direct access, that is easier to use
and faster than {\bf POKE}. This method also accounts for the VIC-IV's ability
to relocate screen memory, so you do not need to read a register to determine
the {\bf POKE} address.
Two byte arrays are predefined, that are mapped to screen text memory
and screen colour memory.
\screentextwide{ T@\&(COLUMNS,ROWS)} is mapped to the text screen memory.
\screentextwide{ C@\&(COLUMNS,ROWS)} is mapped to the colour screen memory.
In 80 $\times$ 25 text mode the arrays are dimensioned as:
\screentextwide{ T@\&(79,24)} Column = 0 - 79, Row = 0 - 24.
\screentextwide{ C@\&(79,24)} Column = 0 - 79, Row = 0 - 24.
In 80 $\times$ 50 text mode the arrays are dimensioned as:
\screentextwide{ T@\&(79,49)} Column = 0 - 79, Row = 0 - 49.
\screentextwide{ C@\&(79,49)} Column = 0 - 79, Row = 0 - 49.
In 40 $\times$ 25 text mode the arrays are dimensioned as:
\screentextwide{ T@\&(39,24)} Column = 0 - 39, Row = 0 - 24.
\screentextwide{ C@\&(39,24)} Column = 0 - 39, Row = 0 - 24.
If you want to display the character {\bf A} (display code = 1)
in row 2 and column 5 and make it yellow, use the mapped byte arrays:
\screentextwide{T@\&(5,2) = 1} character A
\screentextwide{C@\&(5,2) = 7} colour YELLOW
Examining the contents of a screen position is equally easy.
To get the character and the colour at the left most (home) postion:
\screentextwide{T\% = T@\&(0,0)}
\screentextwide{C\% = C@\&(0,0)}
T\% will then contain the display code of the character, while C\% has the colour index.
\subsection{BASIC 65 Operators}
\index{BASIC 65 Operators}
BASIC 65 provides a set of operators that are typical of most BASIC
programming dialects. The usage and precedence of these operators is documented in this section.
The \screentext{=} symbol is used both as an assignment operator, and as a relational
operator for testing equality. For example, in the statement
\screentext{A = B = 5}, the first equal sign is the assignment operator,
while the second is a logical operator, comparing the variable \screentext{B} with \screentext{5}.
The value of \screentext{A} will either be assigned the value \screentext{-1} (for {\bf TRUE}), or
the value \screentext{0} (for {\bf FALSE}).
You may have noticed that the value of \screentext{-1} for {\bf TRUE} is different to
other programming languages, such as {\bf C}, where the value of \screentext{1}
is used for {\bf TRUE} instead.
The \screentext{+} symbol can be used as a positive sign for numerical expressions,
as an addition operator, or for string concatenation. The number and type of operands
determines the operation.
The \screentext{-} symbol can be used as a negative sign for numerical expressions,
or as a subtraction operator. The number and type of operands
determines the operation.
The operators \screentext{NOT, AND, OR} and \screentext{XOR} can be used both as
logical operators, or as boolean operators.
\begin{itemize}
\item Logical Operator Example: \screentext{IF A>B AND A<0}
\item Boolean Operator Example: \screentext{A = B AND \$7F}
\end{itemize}
Both examples always produce an integer result internally, which can be interpreted
either numerically or logically.
If the result of a comparison is {\bf TRUE}, the value will be set
to {\bf -1}, while a {\bf FALSE} result yields {\bf 0}.
In the boolean operator example above, the \screentext{AND} operator converts both operands
to a 16-bit integer value, and performs a bitwise {\bf AND} for all 16 bits.
This example will take the value of \screentext{B}, set the upper 9 bits to zero,
and store the result in \screentext{A}.
The result of logical operations can be used in numerical expressions as well,
for example,
\screentext{A = A - (B > 7)}
will increment \screentext{A} by 1 if the result of \screentext{(B > 7)} is {\bf TRUE} (-1). This is because
the mathematical expression of \screentext{A = A - (-1)} is the same as \screentext{A = A + 1}.
The operators have precedences, which are listed in the tables below.
In the statement \screentext{A * A - B * B}
both multiplications will be performed first, before the subtraction is executed.
Parentheses are used to change the precedence, for example
\screentext{A * (A - B) * B)} will execute the subtraction first.
\subsection{Assignment Operator}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Symbol} & {\bf Description} & {\bf Examples}\\
\hline
\screentext{=} & Assignment & \screentext{A = 42}, \screentext{A\$ ="HELLO"}, \screentext{A = B < 42}\\
\hline
\end{tabular}
\end{center}
\subsection{Unary Mathematical Operators}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
{\bf Name} & {\bf Symbol} & {\bf Description} & {\bf Example}\\
\hline
Plus & \screentext{+} & Positive sign & \screentext{A = +42} \\
Minus & \screentext{-} & Negative sign & \screentext{B = -42} \\
\hline
\end{tabular}
\end{center}
\subsection{Binary Mathematical Operators}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
{\bf Name} & {\bf Symbol} & {\bf Description} & {\bf Example}\\
\hline
Plus & \screentext{+} & Addition & \screentext{A = B + 42}\\
Minus & \screentext{-} & Subtraction & \screentext{B = A - 42}\\
Asterisk & \screentext{*} & Multiplication & \screentext{C = A * B}\\
Slash & \screentext{/} & Division & \screentext{D = B / 13}\\
Up Arrow & \screentext{$\uparrow$} & Exponentiation & \screentext{E = 2 $\uparrow$ 10}\\
Left Shift & \screentext{<<} & Left Shift & \screentext{A = B << 2} \\
Right Shift & \screentext{>>} & Right Shift & \screentext{A = B >> 1} \\
\hline
\end{tabular}
\end{center}
Note that the \screentext{$\uparrow$} character used for exponentiation is entered with \megakeywhite{$\uparrow$}, which is next to \widekey{RESTORE}.\\
\subsection{Relational Operators}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Symbol} & {\bf Description} & {\bf Example}\\
\hline
\screentext{>} & Greater Than & \screentext{A > 42} \\
\screentext{>=} & Greater Than or Equal To & \screentext{B >= 42} \\
\screentext{<} & Less Than & \screentext{A < 42} \\
\screentext{<=} & Less Than or Equal To & \screentext{B <= 42} \\
\screentext{=} & Equal & \screentext{A = 42} \\
\screentext{<>} & Not Equal & \screentext{B <> 42} \\
\hline
\end{tabular}
\end{center}
\subsection{Logical Operators}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Keyword} & {\bf Description} & {\bf Example}\\
\hline
\screentext{AND} & And & \screentext{A > 42 AND A < 84} \\
\screentext{OR} & Or & \screentext{A > 42 OR A = 0} \\
\screentext{XOR} & Exclusive Or & \screentext{A > 42 XOR B > 42} \\
\screentext{NOT} & Negation & \screentext{C = NOT A > B} \\
\hline
\end{tabular}
\end{center}
\subsection{Boolean Operators}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Keyword} & {\bf Description} & {\bf Example}\\
\hline
\screentext{AND} & And & \screentext{A = B AND \$FF} \\
\screentext{OR} & Or & \screentext{A = B OR \$80} \\
\screentext{XOR} & Exclusive Or & \screentext{A = B XOR 1} \\
\screentext{NOT} & Negation & \screentext{A = NOT 22} \\
\hline
\end{tabular}
\end{center}
\subsection{String Operator}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
{\bf Name} & {\bf Symbol} & {\bf Description} & {\bf Operand type} & {\bf Example}\\
\hline
Plus & \screentext{+} & Concatenates Strings & String & \screentext{A\$ = B\$ + ".PRG"} \\
\hline
\end{tabular}
\end{center}
\subsection{Operator Precedence}
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|}
\hline
{\bf Precedence} & {\bf Operators}\\
\hline
High & \screentext{$\uparrow$} \\
& \screentext{+ -} (Unary Mathematical) \\
& \screentext{* /} \\
& \screentext{+ -} (Binary Mathematical)\\
& \screentext{<< >>} (Arithmetic Shifts)\\
& \screentext{< <= > >= = <>} \\
& \screentext{NOT} \\
& \screentext{AND} \\
Low & \screentext{OR XOR}\\
\hline
\end{tabular}
\end{center}
\input{appendix-keywords}
\newpage
\section{BASIC Command Reference}
% =======================================
% Start of the BASIC 65 command reference
% =======================================
\titleformat*{\subsection}{\normalfont\huge\bfseries\color{blue}}
% ***
% ABS
% ***
\newpage
\subsection{ABS}
\index{BASIC 65 Functions!ABS}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$B6
\item [Format:] {\bf ABS(}x{\bf)}
\item [Usage:] {\bf ABS} returns
the absolute value of the numeric
argument {\bf x}.
{\bf x} numeric argument (integer or real expression).
\item [Remarks:] The result is of type real.
\item [Example:] Using {\bf ABS}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT ABS(-123)
123
PRINT ABS(4.5)
4.5
PRINT ABS(-4.5)
4.5
\end{verbatim}
\end{tcolorbox}
\end{description}
% ***
% AND
% ***
\newpage
\subsection{AND}
\index{BASIC 65 Operators!AND}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$AF
\item [Format:] operand {\bf AND} operand
\item [Usage:] {\bf AND} performs a bit-wise
logical AND operation on two 16-bit values.
Integer operands are used as they are.
Real operands are converted to a signed 16-bit integer (losing precision).
Logical operands are converted to 16-bit integer
using \$FFFF (decimal -1) for TRUE,
and \$0000 (decimal 0) for FALSE.
\begin{center}
\setlength{\tabcolsep}{1mm}
\begin{tabular}{|c|c|}
\hline
{\bf Expression} & {\bf Result} \\
\hline
\screentext{0 AND 0} & \screentext{0} \\
\screentext{0 AND 1} & \screentext{0} \\
\screentext{1 AND 0} & \screentext{0} \\
\screentext{1 AND 1} & \screentext{1} \\
\hline
\end{tabular}
\end{center}
\item [Remarks:] The result is of type integer.
If the result is used in a logical context,
the value of 0 is regarded as FALSE, and
all other non-zero values are regarded as TRUE.
\item [Examples:] Using {\bf AND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT 1 AND 3
1
PRINT 128 AND 64
0
\end{verbatim}
\end{tcolorbox}
In most cases, {\bf AND} is used in {\bf IF} statements.
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
IF (C >= 0 AND C < 256) THEN PRINT "BYTE VALUE"
\end{verbatim}
\end{tcolorbox}
\end{description}
% ******
% APPEND
% ******
\newpage
\subsection{APPEND}
\index{BASIC 65 Commands!APPEND}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$0E
\item [Format:]
{\bf APPEND\#} channel{\bf,} filename [{\bf,D} drive] [{\bf,U} unit]
\item [Usage:]
Opens an existing sequential file of type
{\bf SEQ} or {\bf USR} for writing, and positions the write pointer
at the end of the file.
{\bf channel} number, where:
\begin{itemize}
\item {\bf 1 <= channel <= 127} line terminator is CR.
\item {\bf 128 <= channel <= 255} line terminator is CR LF.
\end{itemize}
\filenamedefinition
\drivedefinition
\unitdefinition
\item [Remarks:]
{\bf APPEND\#} works similarly to {\bf DOPEN\#... ,W},
except that the file must already exist.
The content of the file is retained, and all printed text
is appended to the end.
Trying to {\bf APPEND} to a non existing file reports a DOS error.
\item [Examples:] Open existing file in append mode:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
APPEND#5,"DATA",U9
APPEND#130,(DD$),U(UN%)
APPEND#3,"USER FILE,U"
APPEND#2,"DATA BASE"
\end{verbatim}
\end{tcolorbox}
\end{description}
% ***
% ASC
% ***
\newpage
\subsection{ASC}
\index{BASIC 65 Functions!ASC}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$C6
\item [Format:] {\bf ASC(}string{\bf)}
\item [Usage:] Takes the first character of
the string argument and returns its numeric code value.
The name was apparently chosen to be a mnemonic to ASCII,
but the returned value is in fact the so-called PETSCII code.
\item [Remarks:]
{\bf ASC} returns zero for an empty string, whose behaviour
is different to BASIC 2, where \screentext{ASC("")} gave an error.
The inverse function to {\bf ASC} is {\bf CHR\$}.
Refer to the {\bf CHR\$} function on page \pageref{chrcommand}
for more information.
\item [Examples:] Using {\bf ASC}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT ASC("MEGA")
77
PRINT ASC("")
0
\end{verbatim}
\end{tcolorbox}
\end{description}
% ***
% ATN
% ***
\newpage
\subsection{ATN}
\index{BASIC 65 Functions!ATN}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$C1
\item [Format:] {\bf ATN(}numeric expression{\bf)}
\item [Usage:] Returns the arc tangent of the argument.
The result is in the range ($-\pi/2$ to $\pi/2$)
\item [Remarks:]
A multiplication of the result with $180/\pi$
converts the value to the unit "degrees".
{\bf ATN} is the inverse function to {\bf TAN}.
\item [Examples:] Using {\bf ATN}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
PRINT ATN(0.5)
.463647609
PRINT ATN(0.5) * 180 / ~
26.5650512
\end{verbatim}
\end{tcolorbox}
\end{description}
% ****
% AUTO
% ****
\newpage
\subsection{AUTO}
\index{BASIC 65 Commands!AUTO}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$DC
\item [Format:]
{\bf AUTO} [step]
\item [Usage:] Enables faster typing of BASIC programs.
After submitting a new program line to the BASIC editor with
\specialkey{RETURN}, the {\bf AUTO} function generates a new BASIC line
number for the entry of the next line. The new number is
computed by adding {\bf step} to the current line number.
{\bf step} line number increment
Typing {\bf AUTO} with no argument disables it.
\item [Examples:] Using {\bf AUTO}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
AUTO 10 : USE AUTO WITH INCREMENT 10
AUTO : SWITCH AUTO OFF
\end{verbatim}
\end{tcolorbox}
\end{description}
% **********
% BACKGROUND
% **********
\newpage
\subsection{BACKGROUND}
\index{BASIC 65 Commands!BACKGROUND}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$3B
\item [Format:] {\bf BACKGROUND} colour
\item [Usage:] Sets the background colour
of the screen to the argument, which must be in the
range of 0 to 255. All colours within this range are
customisable via the {\bf PALETTE} command. On
startup, the MEGA65 only has the first 32 colours configured,
which are described in the following table.
\item [Colours:] {\bf Index and RGB values of colour palette}
\begin{center}
{\setlength{\tabcolsep}{1mm}
\begin{tabular}{*{4}{|R{1.2cm}}|l|}
\hline
{\bf Index} & {\bf Red} & {\bf Green} & {\bf Blue} & {\bf Colour} \\
\hline
0 & 0 & 0 & 0 & Black \\
1 & 15 & 15 & 15 & White \\
2 & 15 & 0 & 0 & Red \\
3 & 0 & 15 & 15 & Cyan \\
4 & 15 & 0 & 15 & Purple\\
5 & 0 & 15 & 0 & Green \\
6 & 0 & 0 & 15 & Blue \\
7 & 15 & 15 & 0 & Yellow\\
8 & 15 & 6 & 0 & Orange\\
9 & 10 & 4 & 0 & Brown \\
10 & 15 & 7 & 7 & Light Red (Pink) \\
11 & 5 & 5 & 5 & Dark Grey\\
12 & 8 & 8 & 8 & Medium Grey\\
13 & 9 & 15 & 9 & Light Green \\
14 & 9 & 9 & 15 & Light Blue\\
15 & 11 & 11 & 11 & Light Grey\\
\hline
16 & 14 & 0 & 0 & Guru Meditation\\
17 & 15 & 5 & 0 & Rambutan\\
18 & 15 & 11 & 0 & Carrot\\
19 & 14 & 14 & 0 & Lemon Tart\\
20 & 7 & 15 & 0 & Pandan\\
21 & 6 & 14 & 6 & Seasick Green\\
22 & 0 & 14 & 3 & Soylent Green\\
23 & 0 & 15 & 9 & Slimer Green\\
24 & 0 & 13 & 13 & The Other Cyan\\
25 & 0 & 9 & 15 & Sea Sky\\
26 & 0 & 3 & 15 & Smurf Blue\\
27 & 0 & 0 & 14 & Screen of Death\\
28 & 7 & 0 & 15 & Plum Sauce\\
29 & 12 & 0 & 15 & Sour Grape\\
30 & 15 & 0 & 11 & Bubblegum\\
31 & 15 & 3 & 6 & Hot Tamales\\
\hline
\end{tabular}
}
\end{center}
\label{colourtable}
\item [Example:] Using {\bf BACKGROUND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BACKGROUND 3 : REM SELECT BACKGROUND COLOUR CYAN
\end{verbatim}
\end{tcolorbox}
\end{description}
% ******
% BACKUP
% ******
\newpage
\subsection{BACKUP}
\index{BASIC 65 Commands!BACKUP}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$F6
\item [Format:] {\bf BACKUP U} source {\bf TO U} target \\
{\bf BACKUP D} source {\bf TO D} target [{\bf,U} unit]
\item [Usage:] The first form of {\bf BACKUP}, specifying
units for source and target can only be used for the drives
connected to the internal FDC (Floppy Disk Controller).
Units 8 and 9 are reserved for this controller.
These can be either the internal floppy drive (unit 8) and
another floppy drive (unit 9) attached to the same ribbon cable,
or mounted D81 disk images. Therefore, {\bf BACKUP} can be used to
copy from floppy to floppy, floppy to image, image to floppy
and image to image, depending on image mounts and the existence of
a second physical floppy drive.
The second form of {\bf BACKUP}, specifying
drives for source and target, is meant to be used for
dual drive units connected to the IEC bus.
For example: CBM 4040, 8050, 8250 via an IEEE-488 to IEC adapter.
The backup is then done by the disk unit internally.
{\bf source} unit or drive \# of source disk. \\
{\bf target} unit or drive \# of target disk.
\item [Remarks:] The target disk will be formatted and
an identical copy of the source disk will be written. \\
{\bf BACKUP} cannot be used to backup
from internal devices to IEC devices or vice versa.
\item [Examples:] Using {\bf BACKUP}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BACKUP U8 TO U9 : REM BACKUP INTERNAL DRIVE 8 TO DRIVE 9
BACKUP U9 TO U8 : REM BACKUP DRIVE 9 TO INTERNAL DRIVE 8
BACKUP D0 TO D1, U10 : REM BACKUP ON DUAL DRIVE CONNECTED VIA IEC
\end{verbatim}
\end{tcolorbox}
\end{description}
% ****
% BANK
% ****
\newpage
\subsection{BANK}
\index{BASIC 65 Commands!BANK}
\label{bankcommand}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$02
\item [Format:] {\bf BANK} bank number
\item [Usage:] Selects the memory configuration
for BASIC commands that use 16-bit addresses.
These are {\bf LOAD}, {\bf LOADIFF}, {\bf PEEK}, {\bf POKE}, {\bf SAVE}, {\bf SYS}, and {\bf WAIT}.
Refer to the system memory map in
\ifdefined\printmanual
the {\bf MEGA65 Book}
\else
\bookvref{cha:memory-map}
\fi
for more information.
\item [Remarks:] A value > 127 selects memory mapped I/O.
The default value at system startup for the bank number is 128.
This configuration has RAM from \$0000 to \$1FFF,
the BASIC and KERNAL ROM, and I/O from \$2000 to \$FFFF.
\item [Example:] Using {\bf BANK}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BANK 1 :REM SELECT MEMORY CONFIGURATION 1
\end{verbatim}
\end{tcolorbox}
\end{description}
% *****
% BEGIN
% *****
\newpage
\subsection{BEGIN}
\index{BASIC 65 Commands!BEGIN}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$18
\item [Format:] {\bf BEGIN} ... {\bf BEND}
\item [Usage:] {\bf BEGIN} and {\bf BEND} act as
a pair of braces around a compound statement
to be executed after {\bf THEN} or {\bf ELSE}.
This overcomes the single line limitation of the
standard {\bf IF} ... {\bf THEN} ... {\bf ELSE} clause.
\item [Remarks:] Do not jump with {\bf GOTO} or {\bf GOSUB} into a
compound statement, as it may lead to unexpected
results.
\item [Example:] Using {\bf BEGIN} and {\bf BEND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
10 GET A$
20 IF A$>="A" AND A$<="Z" THEN BEGIN
30 PW$=PW$+A$
40 IF LEN(PW$)>7 THEN 90
50 BEND :REM IGNORE ALL EXCEPT (A-Z)
60 IF A$<>CHR$(13) GOTO 10
90 PRINT "PW=";PW$
\end{verbatim}
\end{tcolorbox}
\end{description}
% ****
% BEND
% ****
\newpage
\subsection{BEND}
\index{BASIC 65 Commands!BEND}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$19
\item [Format:] {\bf BEGIN} ... {\bf BEND}
\item [Usage:] {\bf BEGIN} and {\bf BEND} act as
a pair of braces around a compound statement
to be executed after {\bf THEN} or {\bf ELSE}.
This overcomes the single line limitation of the
standard {\bf IF} ... {\bf THEN} ... {\bf ELSE} clause.
\item [Remarks:] The example below shows a quirk in the implementation
of the compound statement.
If the condition evaluates to {\bf FALSE}, execution
does not resume right after {\bf BEND} as it should,
but at the beginning of the next line.
Test this behaviour with the following program:
\item [Example:] Using {\bf BEGIN} and {\bf BEND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
10 IF Z > 1 THEN BEGIN:A$="ONE"
20 B$="TWO"
30 PRINT A$;" ";B$;:BEND:PRINT " QUIRK"
40 REM EXECUTION RESUMES HERE FOR Z <= 1
\end{verbatim}
\end{tcolorbox}
\end{description}
% *****
% BLOAD
% *****
\newpage
\subsection{BLOAD}
\index{BASIC 65 Commands!BLOAD}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$11
\item [Format:] {\bf BLOAD} filename [{\bf,B} bank] [{\bf,P} address] [{\bf,R}]
[{\bf,D} drive] [{\bf,U} unit]
\item [Usage:]
"Binary LOAD" loads a file of type {\bf PRG} into RAM at address P.
{\bf BLOAD} has two modes:
The flat memory address mode can be used to load a program to any
address in the 28-bit (256MB) address range where RAM is installed.
This includes the standard RAM banks 0 to 5, as well as
the 8MB of "attic RAM" at address \$8000000.
This mode is triggered by specifying an address at parameter P
that is larger than \$FFFF. The bank parameter is ignored in this mode.
For compatibility reasons with older BASIC versions, {\bf BLOAD}