-
Notifications
You must be signed in to change notification settings - Fork 50
/
appendix-basic65.tex
10268 lines (7513 loc) · 364 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}
\label{cha:basic-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 {\em keyword}.\index{Keywords} Keywords are special words that BASIC understands. In this manual, keywords are always written in {\bf BOLD CAPITALS}, so that you can easily recognise them.
The ``number'' and ``string'' (in non-bold text) are examples of {\em arguments}. You replace these with values or algebraic phrases ({\em expressions}) that represent the data that controls the command's behaviour.
Punctuation and other letters in bold text represent other characters that are typed as they appear. In this example, a comma must appear between the number argument and the string argument.
Here is an example of using the {\bf KEY} command based on this pattern:
\begin{screencode}
KEY 6, "LIST" + CHR$(13)
\end{screencode}
When you see square brackets around arguments, this indicates that the arguments are optional. You are not meant to type the square brackets. Consider this description of the {\bf CIRCLE} command, which accepts optional arguments:
{\bf CIRCLE} xc{\bf,} yc{\bf,} radius [{\bf,} flags {\bf,} start{\bf,} stop]
The following examples of the {\bf CIRCLE} command are both valid. They have different behaviour based on their different arguments.
\begin{screencode}
CIRCLE 100, 150, 30
CIRCLE 100, 150, 30, 0, 45, 135
\end{screencode}
This arrangement of keywords, symbols, and arguments is called {\em syntax}. If you leave something out, or put the wrong thing in the wrong place, the computer will fail to understand the command and report a {\em syntax error}.
There is nothing to worry about if you get an error from the MEGA65. 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. For example, if you omit the comma in the {\bf KEY} command, or replace it with a period, the MEGA65 will respond with a Syntax error, as seen here:
\begin{screencode}
READY.
KEY 8"FISH"
?SYNTAX ERROR
READY.
KEY 8."FISH"
?SYNTAX ERROR
READY.
\end{screencode}
Expressions can be a number value such as \screentext{23.7}, a string value such as \screentext{"HELLO"}, or a more complex calculation that combines values, functions, and operators to describe a number or string value: \screentext{"LIST"+CHR\$(13)}
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 Type Mismatch error, to say that the type of expression you gave doesn't match what it expected. For example, the following command results in a Type Mismatch error, because \screentext{"POTATO"} is a string expression, and a numeric expression is expected:
\begin{screencode}
KEY "POTATO", "SOUP"
\end{screencode}
Commands are statements that you can use directly from the \screentext{READY.} prompt, or from within a program, for example:
\begin{screencode}
READY.
PRINT "HELLO"
HELLO
READY.
10 PRINT "HELLO"
RUN
HELLO
\end{screencode}
You can place a sequence of statements within a single line by separating them with colons, for example:
\begin{screencode}
PRINT "HELLO" : PRINT "HOW ARE YOU?" : PRINT "HOW IS THE WEATHER?"
HELLO
HOW ARE YOU?
HOW IS THE WEATHER?
\end{screencode}
\subsection{Direct Mode Commands}
\index{BASIC 65 Commands!Direct Mode}
Some commands only work in {\bf direct mode} (sometimes called ``immediate mode''). This means that the command can't be part of a BASIC program, but can be entered directly to the screen. For example, the {\bf RENUMBER} command only works in direct mode, because its function is to renumber the lines of a BASIC program.
In the two {\bf PRINT} examples above, the first was entered in direct mode, whereas the second one was part of a program. The {\bf PRINT} command works in both direct mode and in a program.
\subsection{Command Syntax Descriptions}
The following table describes the other symbols found in command syntax descriptions.
\begin{center}
\setlength{\def\arraystretch{1.5}\tabcolsep}{6pt}
\begin{longtable}{|c|L{8cm}|}
\hline
\textbf{Symbol} & \textbf{Meaning}\\
\hline
\endhead
[ ] &
Optional \\
\hline
\dots &
The bracketed syntax can be repeated zero or more times \\
\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}
Examples of text that appears on the screen, either typed by you or printed by the MEGA65, appear in the screen font: \screentext{"LIST"+CHR\$(13)}
\newpage
\subsection{BASIC 65 Constants}
\index{BASIC 65 Constants}
\setlength{\tabcolsep}{1mm}
Values that are typed directly into an expression or program are called {\em constants}. The values are ``constant'' because they do not change based on other aspects of the program state.
The following are types of constants that can appear in a BASIC 65 expression.
\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} \\
Binary & \screentext{\%11010010}& \screentext{\%101} \\
String & \screentext{"X"} & \screentext{"TEXT"} \\
\hline
\end{tabular}
\end{center}
\subsection{BASIC 65 Variables}
\index{BASIC 65 Variables}
A program manipulates data by storing values in the computer's memory, referring to stored values, and updating them based on logic. In BASIC, elements of memory that store values are called {\em variables}. Each variable has a name, there are separate sets of variable names for each type of value.
For example, the variable \screentext{AA} can store a number value. The variable \screentext{AA\$} can store a string value. Commodore BASIC considers these to be separate variables, even though the names both begin with \screentext{AA}.
One way to store a value in a variable is with the assignment operator ({\bf =}). For example:
\begin{screencode}
AA = 1.95
AA$ = "HELLO, "
\end{screencode}
Variable names must start with a letter, and contain only letters and numbers. They can be of any length, but Commodore BASIC only recognizes the first two letters of the name. \screentext{SPEED} and \screentext{SP} would be considered the same variable.
Variable names cannot contain any of the BASIC keywords. This makes using long names difficult: it is easy to use a keyword accidentally. For example, \screentext{ENFORCEMENT} is not a valid variable name, because {\bf FOR} is a keyword. It is common to use short variable names to avoid these hazards.
A variable can be used within an expression with other constants, variables, functions, and operators. It is substituted with the value that it contains at that point in the program's execution.
\begin{screencode}
10 INPUT "WHAT IS YOUR NAME"; NA$
20 MSG$ = "HELLO, " + NA$ + "!"
30 PRINT MSG$
\end{screencode}
Unlike some programming languages, BASIC variables do not need to be declared before use. A variable has a default value of zero for number type variables, or the empty string (\screentext{""}) for string type variables.
A variable that stores a single value is also known as a {\em scalar variable}. The scalar variable types and their value ranges are as follows.
\setlength{\tabcolsep}{1mm}
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\bf Type} & {\bf Name Symbol} & {\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}
A variable whose name is a single letter followed by the type symbol (or no symbol for real number variables) is a {\em fast variable}. BASIC 65 stores the variable in a way that makes it faster to access or update the value than variables with longer names. It otherwise behaves like any other variable. This is also true for functions defined by {\bf DEF FN}.
\subsection{BASIC 65 Arrays}
\index{BASIC 65 Arrays}
In addition to scalar variables, Commodore BASIC also supports a type of variable that can store multiple values, called an {\em array}.
The following example stores three string values in an array, then uses a {\bf FOR} loop to {\bf PRINT} a message for each element:
\begin{screencode}
10 DIM NA$(3)
20 NA$(0) = "DEFT"
30 NA$(1) = "GARDNERS"
40 NA$(2) = "LYDON"
50 FOR I = 0 TO 2
60 PRINT "HELLO, "; NA$(I); "!"
70 NEXT I
\end{screencode}
Each value in an array is referenced by the name of the array variable and an integer index. For example, \screentext{AA(7)} refers to the element of the array named \screentext{AA()} with index 7. Indexes are ``zero-based:'' the first element in the array has an index of 0. The index can be a numeric expression, which can be a powerful way to operate on multiple elements of data.
All values in an array must be of the same type. The type is indicated in the name of the variable, similar to scalar variables. \screentext{AA()} is an array of real numbers, \screentext{AA\$()} is an array of strings.
Array variable names are considered separate from scalar variable names. The scalar variable \screentext{AA} has no relationship to the array variable \screentext{AA()}.
BASIC needs to know the maximum size of the array before its first use, so that it can allocate the memory for the complete array. A program can declare an array's size using the {\bf DIM} keyword, with the ``dimensions'' of the array. If an array variable is used without an explicit declaration, BASIC allocates a one-dimensional array of 10 elements, and the array cannot be re-dimensioned later (unless you {\bf CLR} all variables).
An array can have multiple dimensions, each with its own index separated by a comma. The array must be declared with the maximum value for each dimension. Keep in mind that BASIC 65 allocates memory for the entire array, so large arrays may be constrained by available memory.
\begin{screencode}
DIM BO$(3, 3)
BO$(1, 1) = "X"
BO$(0, 0) = "O"
BO$(0, 2) = "X"
BO$(1, 0) = "O"
\end{screencode}
\subsection{Screen Text and Colour Arrays}
\label{sec:screentextandcolourarrays}
\index{Screen Text and Colour Arrays}
A BASIC 65 program can place text on the screen in several ways. The {\bf PRINT} command displays a string at the current cursor location, which is especially useful for terminal-like output. The {\bf CURSOR} command moves the cursor to a given position. A program can use these commands together to draw pictures or user interfaces.
A program can access individual characters on the screen using the special built-in arrays \textbf{T@\&()} and \textbf{C@\&()}. These arrays are two-dimensional with indexes corresponding to the column and row of each character on the screen, starting from (0,0) at the top left corner.
\textbf{T@\&(}column{\bf ,} row{\bf )} is the screen code of the character. Screen codes are not the same as PETSCII codes. See appendix \vref{appendix:screencodes} for a list of screen codes.
\textbf{C@\&(}column{\bf ,} row{\bf )} is the colour code of the character. This is an entry number of the system palette. See appendix \vref{appendix:colourtable} for the list of colours in the default system palette. Upper bits also set text attributes, such as blinking.
Similar to regular arrays, the screen and colour array entries can be assigned new values, or used in expressions to refer to their current values.
\begin{screencode}
10 FOR X = 10 TO 30
20 T@&(X, 2) = 1
30 C@&(X, 2) = INT(RND(1) * 16)
40 NEXT X
50 PRINT "COLOUR AT POSITION 15: "; C@&(15, 2)
\end{screencode}
The dimensions of these arrays depend on the current text screen mode. In 80 $\times$ 25 text mode, the column is in the range 0 -- 79, and the row is in the range 0 -- 24. The MEGA65 also supports 80 $\times$ 50 and 40 $\times$ 25 text modes.
\subsection{BASIC 65 Operators}
\index{BASIC 65 Operators}
An {\em operator} is a symbol or keyword that performs a function in an expression. It operates on one or two sub-expressions, called {\em operands}. The operator and its operands {\em evaluate to} the result of the operation.
For example, the {\bf *} (asterisk) operator performs a multiplication of two number operands. The operator and its operands evaluate to the result of the multiplication.
\begin{screencode}
A = 6
PRINT A * 7
\end{screencode}
The {\bf +} (plus) operator has a different meaning depending on the type of the operands. If both operands are numbers, then the operator performs an addition of the numbers. If both operands are strings, then the operator evaluates to a new string that is the concatenation of the operands.
\begin{screencode}
A = 64
PRINT A + 1
A$ = "MEGA"
PRINT A$ + "65"
\end{screencode}
The {\bf -} (minus) operator accepts either one operand or two operands. Given one number operand on the right-hand side, it evaluates to the negation of that number. Given two number operands, one on either side, it evaluates to the subtraction of the second operand from the first operand.
\begin{screencode}
A = 64
PRINT -A
PRINT A - 16
\end{screencode}
The equals symbol ({\bf =}) is used both as an assignment statement and as a relational operator. As an assignment, the {\bf =} symbol is a statement that updates the value of a variable. The left-hand side must be a variable or array element reference, and its type must match the type of the expression on the right-hand side. The assignment is not an operator: it is not part of an expression.
\begin{screencode}
AA = 7
NA$ = "DEFT"
\end{screencode}
As a relational operator, the {\bf =} symbol behaves as an expression. It evaluates the expressions on both sides of the operator, then tests whether the values are equal. If they are equal, the equality operator evaluates to $-1$, BASIC's representation of ``true.'' If they are not equal, the operator evaluates to 0, or ``false.'' The equality expression can be used with an {\bf IF} statement to control program flow, or it can be used as part of a numeric expression. Both expressions must be of the same type.
\begin{screencode}
100 IF X = 99 THEN 130
110 X = X + 1
120 GOTO 100
130 PRINT "DONE."
\end{screencode}
BASIC 65 knows the difference between assignment and equality based on context. Consider this line of code:
\begin{screencode}
A = B = 10
\end{screencode}
BASIC 65 expects a statement, and notices a variable name followed by the {\bf =} symbol. It concludes that this is a statement assigning a value to the number variable \screentext{A}. It then expects a number expression on the right-hand side of the assignment, and notices the {\bf =} symbol is an operator in that expression. It concludes that the operation is an equality test, and proceeds to evaluate the expression and assign the result.
The operators {\bf NOT}, {\bf AND}, {\bf OR} and {\bf XOR} can be used either as logical operators or as boolean operators. A logical operator joins two conditional expressions as operands and evaluates to the logical comparison of their truth values.
\begin{screencode}
IF X = 99 OR Y < 5 THEN 130
IF Y > 10 AND Y < 20 THEN 150
\end{screencode}
A boolean operator accepts two number operands and performs a calculation on the bits of the binary values.
\begin{screencode}
A = 17
PRINT A AND 20
\end{screencode}
Unlike other cases where operators have different behaviours based on how they are used, BASIC 65 does not need to determine whether these operators are behaving as logical operators or boolean operators. Because ``true'' and ``false'' are represented by carefully chosen numbers, the logical operators have the same behaviour whether their operands are conditional expressions or numbers. A ``true'' conditional expression is the number $-1$, which internally is a binary number with all bits set. The logical expression ``true and false'' is equivalent to the binary boolean expression {\tt \%....0000 \& \%....1111}. In this case, the {\bf AND} operator evaluates to 0, which is ``false.''
Conditional expressions evaluating to numbers can be used in some clever programming tricks. Consider this example:
\begin{screencode}
A = A - (B > 7)
\end{screencode}
This statement will increment the value in the \screentext{A} by 1 if the value in \screentext{B} is greater than 7. Otherwise it leaves it unchanged. If the sub-expression \screentext{B > 7} is true, then it evaluates to -1. \screentext{A - (-1)} is equivalent to \screentext{A + 1}. If the sub-expression is false, then it evaluates to 0, and \screentext{A - 0} is equivalent to \screentext{A}.
When multiple operators are used in a single expression, the order in which they are evaluated is specified by {\em precedence}. For example, in the statement \screentext{A * A - B * B}, both multiplications will be performed first, then the subtraction. As in algebra, you can use parentheses to change the order of execution. In the expression \screentext{A * (A - B) * B}, the subtraction is performed first.
The complete set of operators and their order of precedence are summarised in the sections that follow.
\subsubsection{Assignment Statement}
\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}
\subsubsection{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}
\subsubsection{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}
\underline{NOTE}: The \screentext{$\uparrow$} character used for exponentiation is entered with \megakeywhite{$\uparrow$}, which is next to \widekey{RESTORE}.
\subsubsection{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}
\subsubsection{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}
\subsubsection{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}
\subsubsection{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 [Returns:] The absolute value of the numeric argument {\bf x}.
{\bf x} numeric integer or real expression argument.
\item [Remarks:] The result is of type real.
\item [Examples:] 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:] 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) in ``two's complement'' format. Logical operands are converted to 16-bit integer using \$FFFF (-1) for TRUE, and \$0000 (0) for FALSE.
\begin{center}
\setlength{\tabcolsep}{1mm}
\begin{tabular}{|c|c|}
\hline
{\bf Expression} & {\bf Result} \\
\hline
\texttt{0 AND 0} & \texttt{0} \\
\texttt{0 AND 1} & \texttt{0} \\
\texttt{1 AND 0} & \texttt{0} \\
\texttt{1 AND 1} & \texttt{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}
Using {\bf AND} in a logical context ({\bf IF})
\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 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 1 <= channel <= 127: Line terminator is CR.
\item 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:] Using {\bf APPEND\#} (opening existing files)
\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 [Returns:] The PETSCII code of the first character of the string argument, as a number.
\item [Remarks:] {\bf ASC} returns zero for empty strings. This is different to BASIC 2, which raised an Illegal Quantity error for empty strings.
The inverse function to {\bf ASC} is {\bf CHR\$}. Refer to the {\bf CHR\$} function on page \pageref{BASIC 65 Functions!CHR} for more information.
The name was apparently chosen to be a mnemonic to ``ASCII,'' but the returned value is a PETSCII code.
\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 [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)
0.46364761
PRINT ATN(0.5) * 180 / ~
26.565051
\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 or disables automatic line numbering during BASIC program entry. 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 : REM USE AUTO WITH INCREMENT 10
AUTO : REM 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.
{\bf colour} palette entry number, in the range 0 -- 255
All colours within this range are customisable via the {\bf PALETTE} command.
On startup, the MEGA65 only has the first 32 colours configured. See appendix \vref{appendix:colourtable} for the list of colours in the default system palette.
\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:] Copies one disk to another.
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. {\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. In this case, 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 FDC UNIT 8 TO FDC UNIT 9
BACKUP U9 TO U8 : REM BACKUP FDC UNIT 9 TO FDC UNIT 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{BASIC 65 Commands!BANK}
\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:] The beginning of 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} ... {\bf BEND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
10 GET A$
20 IF A$ >= "A" AND A$ <= "Z" THEN BEGIN : REM IGNORE ALL EXCEPT (A-Z)
30 PW$ = PW$ + A$
40 IF LEN(PW$) > 7 THEN 90
50 BEND
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:] The end of 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.
\item [Example:] Using {\bf BEGIN} ... {\bf BEND}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
5 REM DEMOS THE "QUIRK" DESCRIBED
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:] Loads a file of type {\bf PRG} into RAM at address {\bf,P}. (``Binary load.'')
{\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 (800.0000).
This mode is triggered by specifying an address at parameter {\bf P} that is larger than \$FFFF. The bank parameter is ignored in this mode.
For compatibility reasons with older BASIC versions, {\bf BLOAD} accepts the syntax with a 16-bit address at P and a bank number at B as well. The attic RAM is out of range for this compatibility mode.
The optional parameter {\bf R} (``raw mode'') does not interpret or use the first two bytes of the program file as the load address, which is otherwise the default behaviour. In raw mode, every byte is read as data.
\filenamedefinition
{\bf bank} specifies the RAM bank to be used. If not specified, the current bank, as set with the last {\bf BANK} statement will be used.
{\bf address} overrides the load address that is stored in the first two bytes of the {\bf PRG} file.
\drivedefinition
\unitdefinition
\item [Remarks:] {\bf BLOAD} cannot cross bank boundaries.
{\bf BLOAD} uses the load address from the file if no {\bf P} parameter is given.
It is possible to coerce {\bf BLOAD} to load a file of type {\bf SEQ} by using a filename suffix of: \screentext{",S"}. Be sure to also enable raw mode to avoid treating the first two bytes as a {\bf PRG} load address: \screentext{BLOAD "DATA,S",R,P(\$C000)}
\item [Examples:] Using {\bf BLOAD}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BLOAD "ML DATA", B0, U9
BLOAD "SPRITES"
BLOAD "ML ROUTINES", B1, P32768
BLOAD (FI$), B(BA%), P(PA), U(UN%)
BLOAD "CHUNK", P($8000000)
\end{verbatim}
\end{tcolorbox}
\end{description}
% ****
% BOOT
% ****
\newpage
\subsection{BOOT}
\index{BASIC 65 Commands!BOOT}
\begin{description}[leftmargin=2cm,style=nextline]
\item [Token:] \$FE \$1B
\item [Format:] {\bf BOOT} filename [{\bf,B} bank][{\bf,P} address] [{\bf,D} drive] [{\bf,U} unit] \\
{\bf BOOT SYS} \\
{\bf BOOT}
\item [Usage:] Loads and runs a program or boot sector from a disk.
{\bf BOOT filename} loads a file of type {\bf PRG} into RAM at address {\bf P} and bank {\bf B}, and starts executing the code at the load address.
{\bf BOOT SYS} loads the boot sector (512 bytes in total) from sector 0, track 1 and unit 8 to address \$0400 in bank 0, and performs a \texttt{JSR \$0400} afterwards (Jump To Subroutine).
{\bf BOOT} with no parameters attempts to load and execute a file named \screentext{AUTOBOOT.C65} from the default unit. It's short for: \screentext{RUN "AUTOBOOT.C65"}
\filenamedefinition
{\bf bank} specifies the RAM bank to be used. If not specified, the current bank, as set with the last {\bf BANK} statement, will be used.
{\bf address} overrides the load address, that is stored in the first two bytes of the {\bf PRG} file.
\drivedefinition
\unitdefinition
\begin{comment}
\item [Remarks:] {\bf BOOT SYS} copies the contents of one physical sector (two logical sectors) = 512 bytes from disk to RAM, filling RAM from \$0400 to \$05FF.
\end{comment}
\item [Examples:] Using {\bf BOOT}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BOOT SYS
BOOT (FI$), B(BA%), P(PA), U(UN%)
BOOT
\end{verbatim}
\end{tcolorbox}
\end{description}
% ******
% BORDER
% ******
\newpage