Changes

Jump to: navigation, search

Locomotive BASIC

39,232 bytes added, 12:40, 27 September 2011
Undo revision 69857 by [[Special:Contributions/HAL6128|HAL6128]] ([[User talk:HAL6128|talk]])
For other BASIC language versions see the [[:Category:BASIC]]
 
 
[[Image:Screenshots-CPC_Start_screen.jpg|thumb|CPC Start Screen showing Locomotive copyright and BASIC version]]
 
Locomotive BASIC was a [http://en.wikipedia.org/wiki/BASIC_programming_language BASIC] interpreter for the Amstrad CPC range of computers.
 
It was availlable directly from in-built ROMs on [[CPC old generation]] and on the [[Plus System Cartridge]] for the [[Plus]] range.
 
== Description ==
 
Locomotive BASIC, was one of the best and fastest BASIC implementations of the era. The language benefited both from a clean, well-thought out implementation of the core language by Locomotive, and by the excellent [[firmware]] of the CPC, which lent most of its advanced features to the BASIC.
 
Unlike the competing Commodore 64, it featured a comprehensive graphic capabilities with its PLOT, DRAW, PAPER, INK, PEN, BORDER and (in BASIC 1.1) FILL commands. It had extensive sound commands, granting control of the [[AY-3-8912]] via the firmware's volume and tone envelope system. With the SOUND command, you could select channels, set envelopes, pitch, noise and volume. That was something unmatched by other computers of that era.
 
Also there was simple interface for memory management, with MEMORY and LOAD commands. The latter allowed for loading of raw screen data, thus providing easy picture showing. Both through this (combined with CALL, PEEK and POKE) and the firmware's [[RSX]] system, it was easy to mix BASIC and assembly code, thereby speeding up programs by coding the slowest parts directly in machine code. Many successful programs, including games such as [[Radzone]] and applications such as [[PowerPage]], made use of this technique.
 
With DEF FN, ON variable GOTO and ON variable GOSUB, Locomotive BASIC provided the rudiments of "structured programming", though nowhere near the extent of the PROCedures of [[BBC Basic]].
 
All in all, if you compare BASIC interpreters of that era, the Locomotive's seems to be the best 'all-rounder' regarding the combination of speed and complexity, and still some of its features were unmatched by others.
 
If you are interested in more information about the different versions of the CPC's BASIC and the technical details, you can have a look at the article "[[Technical information about Locomotive BASIC]]".
 
== History ==
 
The CPC implementation of Locomotive BASIC was developed directly from [[Locomotive Software]]'s existing Z80 BASIC. The existence of this is cited as one of the reasons Locomotive requested that [[Amstrad]] change the CPC's processor from a 6502 to a [[Z80]].
 
The 464 shipped with BASIC 1.0 on ROM.
 
The language was revised and debugged for the 664, 6128 and Plus machines to become BASIC 1.1. Changes were minor but significant for the programmer, and included:
 
* DEC$ bug removed (in BASIC 1.0, it required two opening brackets and was undocumented)
* Better handling of string arguments to RSXs (|DIR,"*.BAS" rather than a$="*.BAS":|DIR,@a$)
* DATA statements can appear anywhere within a line; in BASIC 1.0, they had to be at the end of a line
* FILL command (fill area with solid colour)
* COPYCHR$ function (fetch character from screen)
* Better garbage collection
* Some number-handling bugs removed (e.g. in FOR loops with negative start/end values)
* FRAME (CALL &BD19)
* Extra, optional 'plotting mode' parameter for DRAW/PLOT commands (supported only through control codes on BASIC 1.0)
* GRAPHICS PAPER, GRAPHICS PEN commands
* ON BREAK CONT (disable ESCape)
* CLEAR INPUT (flush keyboard buffer)
* The AUTO command show the whole line if it exist, it only printed a * on the 464
 
Some parts of 'BASIC' were actually housed in the firmware ROM, but were not officially accessible to other programs. This included the line editor.
 
The 'pure BASIC' parts of Locomotive BASIC - i.e. those not concerned with CPC-specific firmware and hardware features - were upgraded to become Mallard BASIC, the CP/M language shipped with the [[PCW]]. This also featured exceptionally advanced random-access file handling, a feature missing from the CPC.
 
== Command list ==
 
=== Commands and operators ===
 
==== <code>AFTER </code>‹time delay›[<code>,</code>‹timer number›]<code> GOSUB </code>‹line number› ====
: Waits for ‹time delay›/50 seconds and then jumps to the subroutine at ‹line number›. There are 4 delay timers from 0 to 3 which can be specified with ‹timer number›. If omitted ‹timer number› defaults to 0.
: In the case of parallel task has 3 the highest and 0 the lowest priority.
: With DI or EI you can disable or enable the timing interrupt. With REMAIN <timer number> you can also disable an AFTER or EVERY construct and stores the "remaining" time (> REMAIN)
 
==== <code>AUTO </code>[‹line number›][<code>,</code>‹increment›] ====
: Automatically generates line numbers starting at ‹line number› with ‹increment› after each entered line number. Use '''[ESC]''' to leave AUTO mode. Default values for ‹line number› and ‹increment› are 10.
 
: Example:
 
: <code>AUTO 100,5 :REM generates line numbers 100, 105, 110...</code>
 
==== <code>BORDER </code>‹colour›[<code>,</code>‹colour›] ====
: Changes the colour of the border. If the second argument is supplied the border flashes between the two colours.
 
==== <code>CALL </code>‹address expression›[<code>,</code>‹list of: parameter›] ====
: Allows a machine code routine to be called by BASIC. Variables, string values and constants can be passed to the routine. Values of any supported type can be passed back by supplying a variable with <code>@</code> in front of it. This passes the address of the variable to the routine although it doesn't implicitly know the data type.
: Example:
 
: <code>CALL 0 :REM resets the computer completely</code>
 
==== <code>CAT</code> ====
: Displays the names of the files on the tape or disc. Tape files are displayed in the order they are encountered. Disc files are sorted alphabetically by ACSII code. Only files matching the current user are displayed. Files marked as system are not displayed.
: Examples:
 
: <code>CAT :REM lists all disc files in alpha-numeric (ASCII) order</code>
: <code>|TAPE :CAT :REM lists names of all tape files in their storage order</code>
 
==== <code>CHAIN </code>‹file name›[<code>,</code>‹line number expression›] ====
 
:Enables the specified program to be loaded and RUN automatically. If the optional parameter ‹line number expression› is specified, the program execution will commence from that line.
 
==== <code>CHAIN MERGE </code>‹file name›[<code>,</code>‹line number expression› | [<code>,</code>[‹line number expression›]<code>,DELETE</code> ‹line number range›]] ====
 
:Loads the specified program from tape or disc, merges it into the program in memory, and starts execution of the merged program. The parameter <code>DELETE</code> ‹line number range› is used to delete part of the original program before running it, if required.
 
==== <code>CLEAR</code> ====
: Clears all variables from memory, leaving the program in memory unchanged. All open files are abandoned.
: The command clear inside a subroutine (GOSUB... RETURN) will also clear the "stack pointer" address of the gosub heap. That means that a RETURN won't work and a GOTO has to be used instead.
 
==== <code>CLG </code>[‹masked ink›] ====
: Clears the graphics screen to colour specified by ‹masked ink›. If parameter ‹masked ink› is not specified then the graphics screen is cleared to the colour specified by the GRAPHICS PAPER statement.
 
==== <code>CLOSEIN</code> ====
: Closes any input file (tape or disc).
 
==== <code>CLOSEOUT</code> ====
: Closes any output file (tape or disc).
 
==== <code>CLS</code> [<code>#</code>‹stream expression›]====
: Clears the window specified by ‹stream expression›. If ‹stream expression› is omitted it defaults to #0 (usually the whole screen). The text cursor of the stream is moved to the upper left corner.
 
==== <code>CONT</code> ====
: CONTinues program execution interrupted either by [ESC] [ESC] or as a result of STOP within a program. A program cannot be continued after being modified.
 
==== <code>CURSOR ‹expression›</code> ====
: ‹expression› must be either 0 or 1.
 
==== <code>DATA x1[,x2,x3...]</code> ====
: Defines a data ''section'' to be used by <tt>READ</tt> calls.
: Data values can be of any type (integer, real or string) as long as the corresponding <tt>READ</tt> calls use a variable of the right type.
 
:Example:
 
<pre>
10 DATA "Hello, world!", 42
20 READ message$:PRINT message$
30 READ answer:PRINT "The answer is:";answer
</pre>
 
:''See also:'' <tt>READ</tt>, <tt>RESTORE</tt>
 
==== <code>DEF</code> ====
: [...]
 
==== <code>DEFINT letter range</code> ====
 
: Forces all variables(s) starting with the specified letter(s) to be string variables. The s does not need to be added to DEFSTR variable names.
 
: Example:
 
<pre>
10 DEFSTR N - sets all variables starting with letter N as strings.
</pre>
 
==== <code>DEFREAL</code> ====
: [...]
 
==== <code>DEFSTR</code> ====
 
: Sets the default for variable(s) with the specified first letter(s) to integer. The letter range could be an inclusive range A-Z
 
: Example:
 
<pre>
10 DEFINT F,S ..... (or 10 DEFINT A-Z)
20 FIRST=111.11:SECOND=22.2
30 PRINT FIRST,SECOND - prints 111 22
</pre>
 
==== <code>DEG</code> ====
: Switch to degrees mode for trigonometric functions (<tt>SIN</tt>, <tt>COS</tt>...).
 
''See also:'' <tt>RAD</tt>
 
==== <code>DELETE [first line][-[last line]]</code> ====
: Deletes the current program completely (without arguments) or only the given line or line range. Even
: <code>DELETE -</code>
: is legal, it has the same effect as
: <code>DELETE</code>
: The lines specified do not have to exist, all lines matching the range will be deleted and having no matches does not cause an error.
 
==== <code>DI</code> ====
: Disables interrupts (but not[ESC]) until re-enabled by EI command or by RETURN at end of an interrupts service routine.
 
==== <code>DIM a[%|!|$](d1[,d2[,...]])</code> ====
 
: Creates array <code>a</code> with single or multiple dimensions. You may optionally specify integer (<code>%</code>), real (<code>!</code>) or string (<code>$</code>) type otherwise it defaults to the current type set for the first letter of the array name. <code>d1</code> is size of first dimension-1, <code>d2</code> is size of second dimension-1 and so on. <code>DIM x(10)</code> will create an array with 11 elements, <code>x(0)</code> is the first element, <code>x(10)</code> is the eleventh and last. You can specify as many dimensions as will fit on one 255 character line, which is a maximum of 125. Trying to create an array that already exists will generate an <code>Array already dimensioned</code> error. If an array is not specified by <code>DIM</code> before being accessed, each dimension value defaults to 10. The maximum dimensions that can be created this way is three.
 
==== <code>DRAW x,y[,i1 | ,[i1],i2]</code> ====
 
: Draws a line from the current cursor position to position x,y. i1 specifies colour, i2 is the drawing style.
 
<pre>
i2 = 0 normal colour i2 = 2 AND colour
i2 = 1 XOR colour i2 = 3 OR colour
</pre>
 
Example:
 
<code>
: CLG 2
: DRAW 500,400,0 :REM draws a line from 0,0 to 500,400
</code>
 
==== <code>DRAWR xr, yr, [[i1][,i2]]</code> ====
 
: Draws a line from current graphics cursor position to current cursor x position + xr, current cursor y position + yr. i1 and i2 as DRAW.
:Example:
 
<pre>
Move 200,200
DRAWR 100,100,0 - draws a line from 200,200 to 300,300
</pre>
 
==== <code>EDIT line</code> ====
: Copies one program line to screen in edition mode.
 
==== <code>EI</code> ====
: Enable interrupts which have been disabled by DI
 
==== <code>END</code> ====
: Indicates end of program
 
==== <code>ENT</code> ====
: [...]
 
==== <code>ENV</code> ====
: [...]
 
==== <code>ERASE v1[% | ! | $][,v2[% | ! | $][,...]]</code> ====
: Erases the specified array(s) and frees the memory used. Must specify existing array(s) or an <code>Improper argument</code> error will be generated.
 
==== <code>ERL</code> ====
 
: Returns the line number of the last error encountered.
 
==== <code>ERROR i</code> ====
: Returns the error message whose error code number is i.
 
==== <code>EVERY i[,t] GOSUB ln</code> ====
 
: BASIC branches to the subroutine at line ln every i/50 seconds.
 
==== <code>FILL i</code> ====
: Fills an area of a graphics screen i colour i (0-15). Default value of i is the current graphics pen colour. Only available in Basic 1.1.
 
==== <code>FN</code> ====
: [...]
 
==== <code>FOR TO STEP NEXT</code> ====
: [...]
 
==== <code>FRAME</code> ====
: Smooths character and graphics movement and reduces flicker (waits for a VSYNC signal). Only available in Basic 1.1. On a CPC 464 you can use CALL &BD19 instead.
 
==== <code>GOSUB i</code> ====
 
: Jumps to subroutine which is given as argument.
: ''Example:''
 
<pre>
10 PRINT "Calling subroutine"
20 GOSUB 100
30 PRINT "Back from subroutine"
40 END
100 REM Begin of the subroutine
110 PRINT "Subroutine started"
120 RETURN
</pre>
 
==== <code>GOTO i</code> ====
: Jumps to the line number which is given as argument.
: ''Example:''
<pre>
10 GOTO 100
20 REM not executed
30 REM not executed
100 PRINT "Hello World!"
</pre>
: GOTO is the simplest form of a jump or creating a loop '''without''' condition controll.
: ''Example: (unconditional and endless loop)''
<pre>
10 PRINT "#";
20 GOTO 10
</pre>
: Combine the GOTO command with an IF...THEN...ELSE instruction for creating a condition-controlled loop with the '''test at the end'''. Helpful because the Locomotive Basic has no practical DO...WHILE... loop.
: ''Example: (condition at the end)''
<pre>
10 I=1
20 PRINT I
30 I=I+1
40 IF I<25 THEN GOTO 10
50 END
</pre>
 
==== <code>GRAPHICS</code> ====
: [...]
 
==== <code>IF THEN ELSE</code> ====
: Asks for a choice with the '''IF...THEN...ELSE''' statement.
: '''IF''' compares the entry condition in a logical way
: '''THEN''' contains instruction if the comparison is true
: '''ELSE''' contains instruction if it's false.
: '''Example:'''
<pre>
10 INPUT "guess a figure:",f
20 IF f=10 THEN PRINT "right": END: ELSE GOTO 10
</pre>
: In case of nested choices there's a possibility to that with an IF...THEN instruction but not very recommended:
<pre>
10 INPUT "guess a figure:",f
20 IF f=10 THEN PRINT "right": END: ELSE IF f<10 THEN PRINT "too small" ELSE PRINT "too big"
30 GOTO 10
</pre>
:Other nested structures like IF...THEN...'''ELSE...ELSE''' won't work first because the Locomotive Basic only looks after the first ELSE instruction found and can't execute more single commands as a block for a certain condition in comparision to e.g. "PASCAL" > begin...end-block, "C" > {...}-block.
 
==== <code>INK</code> ====
: [...]
 
==== <code>INPUT</code> ====
: [...]
 
==== <code>KEY</code> ====
: [...]
 
==== <code>LET</code> ====
: Used to define variables. You don't need to use the ''LET'' command because it is just a command which was added for compatibility reasons.
: ''Example:''
<pre>
10 LET a$ = "hello world"
20 PRINT a$
</pre>
 
==== <code>LINE INPUT </code>‹variable›[<code>,</code>‹variable›] ====
: [...]
 
==== <code>LIST </code>[‹line number›][<code>-</code>[‹line number›]] ====
: [...]
 
==== <code>LOAD ‹file name›[<code>,</code>‹address›] ====
: [...]
 
==== <code>LOCATE x,y</code> ====
 
: Moves the text cursor to the x,y location.
 
: x starts at 1 on the left and goes up to 20 (in mode 0), 40 (in mode 1) or 80 (in mode 2).
 
: y starts at 1 at the top and ends at 25 at the bottom.
 
==== <code>MASK [i1][,i2]</code> ====
 
: Sets bits in each adjacent group of 8 pixel on (1) or off (0) according to binary value of i1 (0-255). i2 determines whether the first point of the line is to plotted (1) or not (0).
 
:Example:
 
<pre>
10 CLG 2:MASK 1:MOVE 0,0:DRAW 500,400
20 MASK 15:MOVE 0,0:DRAW 500,400
</pre>
 
==== <code>MEMORY add</code> ====
 
: Allocates the amount of memory to be used by BASIC by setting the address of the highest byte it may use.
 
==== <code>MERGE</code> ====
: [...]
 
==== <code>MID$</code> ====
: [...]
 
==== <code>MODE</code> ====
: Changes the screen mode: MODE 0 is 160x200 in 16 colors, MODE 1 is 320x200 4 colors and MODE 2 is 640x200 2 colors.
 
==== <code>MOVE x,y [[,i1][,i2]]</code> ====
 
: Moves the graphic cursor to position x,y. The parameter i1 may be used to change the pen (drawing) colour. The parameter i2 specifies the logical colour, as in DRAW.
 
==== <code>MOVER</code> ====
: [...]
 
==== <code>NEW</code> ====
: [...]
 
==== <code>ON BREAK CONT</code> ====
 
: Prevents the interruption of program execution by the ESC key.
 
==== <code>ON BREAK GOSUB ln</code> ====
 
: Passes control to subroutine at line ln when ESC ESC pressed.
 
==== <code>ON BREAK STOP</code> ====
 
: Restores normal function of ESC key during program execution.
 
==== <code>ON ERROR GOTO ln</code> ====
 
: Passes the control to line ln if an error is detected in the program.
 
: ON ERROR GOTO 0, Turns of the error trap, and restores normal error processing.
 
==== <code>ON variable GOTO ln x1, x2, x3, x4, ...</code> ====
 
: In case of passing more choices '''ON variable GOTO ln x1, x2, x3, x4, ...''' points to a table with the jumping target.
: '''Example:'''
<pre>
10 PRINT "1. LOAD - 2. SAVE - 3. EXIT"
20 INPUT choice
30 ON choice GOTO 1000, 2000, 3000
40 CLS: GOTO 10
1000 PRINT "1. LOAD":END
2000 PRINT "2. SAVE":END
3000 END
</pre>
:In case that the variable ''choice'' won't fullfil the condition (in a range between line 0 and 65535) the next instruction will be executed (here: Line 40). If "choice" is smaller than 0 an error will occur.
 
==== <code>ON variable GOSUB ln x1, x2, x3, x4, ...</code> ====
 
: In case of passing more choices targeting to SUBROUTINES '''ON variable GOSUB ln x1, x2, x3, x4, ...''' points to a jumping table.
: '''Example:'''
<pre>
10 PRINT "1. LOAD - 2. SAVE - 3. EXIT"
20 INPUT choice
30 ON choice GOSUB 1000, 2000, 3000
40 CLS: GOTO 10
1000 PRINT "1. LOAD":RETURN
2000 PRINT "2. SAVE":RETURN
3000 END
</pre>
:In case that the variable ''choice'' won't fullfil the condition (in a range between line 0 and 65535) the next instruction will be executed (here: Line 40). If "choice" is smaller than 0 an error will occur.
 
==== <code>OPENIN "datafile"</code> ====
 
: Opens the specified data file for reading.
 
==== <code>OPENOUT "datafile"</code> ====
: Opens the specified data file for writing.
 
==== <code>ORIGIN</code> ====
: [...]
 
==== <code>OUT add,i</code> ====
 
: Outputs the value of i (0-255) to the I/O address add.
 
==== <code>PAPER</code> ====
: [...]
 
==== <code>PEN</code> ====
: [...]
 
==== <code>PLOT</code> ====
: [...]
 
==== <code>PLOTR</code> ====
: [...]
 
==== <code>POKE add</code> ====
 
: Alters contents of memory location add to value i (0-255)
 
==== <code>PRINT</code> ====
: [...]
 
==== <code>RAD</code> ====
: Switch to radians mode for trigonometric functions (<tt>SIN</tt>, <tt>COS</tt>...).
 
: ''See also:'' <tt>DEG</tt>
 
==== <code>RANDOMIZE [seed]</code> ====
 
: Resets the pseudo-random generator to the given seed. What is strange is that if no seed is given, one is interactively prompted for.
 
: A common idiom to have a ''random'' random seed is to do:
 
<pre>
RANDOMIZE TIME
</pre>
 
The algorithm used for randomizing is the following: the generator starts with the seed, adds 1, multiplies by 75 (fiddle factor), divides by 65537 and then uses the remainder -1.
 
==== <code>READ variable</code> ====
: Gets the next data item (from <tt>DATA</tt> commands), stores it in the given variable and moves to the next item.
The variable must be of the correct type.
 
''See also:'' <tt>DATA</tt>, <tt>RESTORE</tt>
 
==== <code>RELEASE</code> ====
: [...]
 
==== <code>REM [any text]</code> ====
: Introduces a comment.
 
==== <code>RENUM </code>[‹newLine›][<code>,</code>‹oldLine› | <code>,</code>[‹oldLine›]<code>,</code>‹step›] ====
: Renumbers the lines of the current program.
 
: By default, the whole program is renumbered starting at line 10 with multiples of ten. Any parameter that is left out defaults to 10. It is important to note that ''jumps'' (<code>GOTO</code>, <code>GOSUB</code> and the like) are automatically converted to the new line numbers.
 
: The whole set of parameters can be used to renumber only the last part of a program.
 
: Example:
 
<pre>
10 GOTO 20
20 GOTO 30
30 GOTO 10
</pre>
 
: becomes, after calling <code>RENUM 100,20,5</code>
 
<pre>
10 GOTO 100
100 GOTO 105
105 GOTO 10
</pre>
 
==== <code>RESTORE [line]</code> ====
 
: Resets the data pointer used by <tt>READ</tt>. When used without parameters, resets the pointer to the first data in the program. Otherwise, resets the pointer to the given line number.
 
: Example:
 
<pre>
10 DATA 10,11,12,13,14
20 DATA 20
READ i:PRINT i
10
Ready
READ i:PRINT i
11
Ready
RESTORE
Ready
READ i:PRINT i
10
Ready
RESTORE 20
Ready
READ i:PRINT i
20
Ready
</pre>
 
 
: ''See also:'' <tt>DATA</tt>, <tt>READ</tt>
 
==== <code>RESUME</code> ====
: [...]
 
==== <code>RETURN</code> ====
 
: Terminates a subroutine and returns control to the line following the GOSUB call (see GOSUB)
 
==== <code>RUN [line]</code> ====
: Runs the current program, optionally starting at a given line. If no line number is given, starts at the first line.
 
==== <code>SAVE</code> ====
: [...]
 
==== <code>SOUND</code> ====
: [...]
 
==== <code>SPC</code> ====
: [...]
 
==== <code>SPEED</code> ====
: [...]
 
==== <code>SQ (channel)</code> ====
 
: Returns a bit significant integer showing state of the sound queue for specified channel where channel 1,2,3, = A, B, C
 
:Bits 0,1 and 2 Number of free entries in the queue
:Bits 3,4 and 5 Redezvous state at head of the queue
:Bit 6 Head of the queue is held
:Bit 7 Channel is currently active
 
==== <code>STOP</code> ====
 
: Breaks program execution at line containing the STOP statement. The message '''BREAK in''' is output with the line number.
 
==== <code>SUB</code> ====
: [...]
 
==== <code>SWAP</code> ====
: [...]
 
==== <code>SYMBOL n,i1[,i2,i3,i4,i5,i6,i7,i8]</code> ====
 
: Redefines the appearance of the character at index n.
: Each of the following eight integers defines the contents of one pixel row, starting at the top of the character.
: Each character fits in an 8x8 pixel grid.
: Missing lines are considered as empty.
 
: Example:
 
<pre>
SYMBOL 255,255,129,129,129,129,129,129,255
PRINT CHR$(255)
</pre>
 
: Defines character 255 to look like an empty square and prints it.
 
: Initially, only characters with indices ranging from 240 to 255 can be redefined.
: See also <tt>SYMBOL AFTER</tt> to allow redefinition of arbitrary characters.
 
==== <code>SYMBOL AFTER n</code> ====
: Allows the redefinition of character symbols from index n included.
 
: Symbol redefinitions are made using the SYMBOL command above.
: Initially, only symbols from index 240 to 255 can be redefined. This initial situation can be restored with <tt>SYMBOL AFTER 240</tt>. Symbol previously redefined are restored to their original appearance.
 
==== <code>TAB</code> ====
: [...]
 
==== <code>TAG [#st]</code> ====
 
: Allows text to print at graphics cursor position.
: see TAGOFF
 
==== <code>TAGOFF [#st]</code> ====
 
: Directs text to stream st printing it at previous text cursor position.
 
==== <code>TROFF</code> ====
 
: Turns off the program flow trace (see TRON)
 
==== <code>TRON</code> ====
 
: Turns on the program flow trace for debugging. Causes the line number of each statement executed to be displayed.
 
==== <code>USING</code> ====
: The USING assignement defines the format of the PRINT output. Its appointment happens by characters where each has a certain meaning.
 
'''Numbers / Figures'''
: the character:
:'''#''' stands for a figure between 0 - 9
:'''.''' stands for a decimal point
:'''+''' generates a positive sign if the value is positive
:'''-''' generates a negative sign if the value is nagative
:'''**''' generates a asterix for each empty "location" before the printed character
:'''$$''' generates a Dollar characters before the printed character (the same with the pound note)
:''',''' generates a comma every each three decimals (thousands per figure) and has to be set before the decimal point or at the end if without a decimal point.
:^ generates a scientific notation (with exponents) and a the end of the format appointment
 
'''Strings'''
: the character:
:'''!''' prints out only the first character
:'''\xxx\''' prints out only with x-numbers defined length of the string (from left to right). x means a blank sign made with the space bar
:'''&''' the whole string is printed out as given (unknown function)
 
: a print output (numbers) with "%" in the beginning shows that not enough positions has to be declared (at all or before the decimal point).
 
: Example:
<pre>
10 figure=123456789
20 word$="Hello"
30 print using "+**,##########";figure
40 print using "\ \";word$
 
creates following output:
 
*****+12,345,679
Hel
</pre>
 
==== <code>WAIT add, i1[,i2]</code> ====
 
: Waits until the I/O port at add returns a value (0-255). The value returned is XORed with i2 and the ANDed with i1. This is repeated until a non-zero result occurs.
 
==== <code>WHILE WEND</code> ====
: [...]
 
==== <code>WIDTH</code> ====
: [...]
 
==== <code>WINDOW</code> ====
: [...]
 
==== <code>WRITE [#st,] v[$], v[$]</code> ====
 
: Writes the values of the specified variable to the specified stream.
 
: Example:
 
<pre>
10 OPENOUT "DUMMY"
20 INPUT A$,A
30 WRITE #9,A$,A
40 CLOSEOUT
</pre>
 
==== <code>ZONE i</code> ====
 
: Changes the width of the print zone. Default is 13.
 
=== Operators ===
 
==== <code>AND</code> ====
: [...]
 
==== <code>MOD</code> ====
: [...]
 
==== <code>NOT</code> ====
: '''NOT''' executes a bit for bit '''negation''' of the current value. The output is a 16-Bit-Word.
<pre>
10 value1=1
20 PRINT HEXprint hex$(value1):PRINT BINprint bin$(value1,8)
30 value2=NOT value1
40 PRINT HEXprint hex$(value2):PRINT BINprint bin$(value2,8)
run
1
1111111111111110
</pre>
 
==== <code>OR</code> ====
: [...]
 
==== <code>XOR</code> ====
: [...]
 
=== Functions ===
 
==== <code>ABS (n)</code> ====
: Returns the absolute value of n by ignoring the sign value.
 
: Example
 
<pre>
PRINT ABS(-3.5) - prints 3.5
</pre>
 
==== <code>ASC (s)</code> ====
 
: Returns ASCII code number of first character of string s
 
==== <code>ATN (n)</code> ====
: Returns the arctangent of n.
 
==== <code>BIN$ (i1,[i2])</code> ====
: Returns binary representation of i1 between -32768 and 65535. The number of binary digits (0s and 1s) is specified by i2 (0-16)
: Example:
 
<pre>
PRINT BIN$(66,8) - prints 01000010
</pre>
 
==== <code>CHR$ (n)</code> ====
: Returns the character for a given index n. For instance CHR$(65) returns the character 'A'. Valid indices range from 0 (zero) to 255.
 
: As an example, try the following basic program :
 
<pre>10 print chr$(208+rnd(2));:goto 10</pre>
 
: It will draw a random maze with characters 208 and 209, which are an horizontal and a vertical bar.
 
==== <code>CINT (n)</code> ====
 
: Returns rounded up integer value of n between -32768 and 32767.
 
: Example:
 
<pre>
PRINT CINT(3.8) - print 4
</pre>
 
==== <code>COPYCHR$ (st)</code> ====
 
: Copies character from current position in specified stream.
 
==== <code>COS (n)</code> ====
 
: Returns cosine of n in degrees or radians (se DEG and RAD)
 
==== <code>CREAL (n)</code> ====
 
: Converts integer n to real numeric variable.
 
==== <code>DEC$(n, format)</code> ====
 
: Retruns the decimal string representation of n, according to the specified format (see PRINT USING)
 
==== <code>DERR</code> ====
 
: Gives the most recent error code number returned by [[Amsdos]].
 
==== <code>EOF</code> ====
 
: Checks to see if end of specified file has been reached during input. Returns 0 (false) until the end of file, then -1 (true)
 
==== <code>ERR</code> ====
 
: Returns the error code number of the last error encountered.
 
==== <code>EXP (i)</code> ====
 
: Returns the result of calculating e to the power i.
 
: Example:
 
<pre>
PRINT EXP(1) - prints 2.71828183
</pre>
 
==== <code>FIX (n)</code> ====
 
: Removes the fractional part of n (see INT)
 
==== <code>FRE (n/se)</code> ====
 
: Returns the amount of unused memory, irrespective of the nature or value of the dummy argument inside the bracket.
 
: Examples:
 
<pre>
PRINT FRE(o) or PRINT FRE("hello")
</pre>
 
==== <code>HEX$ (i1, i2)</code> ====
 
: Returns a string hexadecimal digit representation of i1 (0-65535). The number of hex digits in the string is given by i2 (0-15)
 
==== <code>HIMEM</code> ====
 
: Returns address of the highest memory address used by BASIC.
 
==== <code>INKEY (i)</code> ====
: Checks to see if key number i is being pressed.
 
<pre>
Value returned [SHIFT] [CTRL] Specified key
-1 ignored ignored up
0 up up down
32 down up down
128 up down down
160 down down down
</pre>
 
==== <code>INKEY$</code> ====
 
: Checkts the keyboard and returns the string character of the key pressed. The string character returned is normally assigned to a string variable. If no key pressed, a null string is returned.
 
==== <code>INP (add)</code> ====
 
: Returns value read from the I/O address add
 
==== <code>INSTR</code> ====
 
:INSTR ([startposition,]explored string, seeking string)
<blockquote>This function looks inside "explored string" after "seeking string" and return a number where the found string appear for the first time. Returns zero if not successfull. Startposition could be a figure between 1 and 255. </blockquote><pre>a$="ABCD":PRINT INTR(a$,"C") - returns with "3"</pre>
 
==== <code>INT (n)</code> ====
 
: As in FIX if n is positive; if n is negative, it rounds it down.
 
: Example:
 
<pre>
PRINT INT(3.99), INT(-3.99) - prints 3 -4
</pre>
 
==== <code>JOY (i)</code> ====
 
: Returns bit-significant value from specified joystick. i = 0 or 1.
 
<pre>
Bit Value returned
0(up) 1
1(down) 2
2(left) 4
3(right) 8
4(fire 2) 16
5(fire 1) 32
</pre>
 
==== <code>LEFT$ (se, i)</code> ====
 
: Returns a substring of se. The substring begins at the left-most character of se and contains i characters.
 
: Example:
 
<pre>
A$="ABCDEFG":PRINT LEFT$(A$,3) - prints ABC
</pre>
 
==== <code>LEN (se)</code> ====
 
: Returns the number of characters in se (0 - 255)
 
==== <code>LOG (n)</code> ====
 
: Returns the natural logarithm (to base e) of n.
 
==== <code>LOG10 (n)</code> ====
 
: Returns the logarithm to base 10 of n.
 
==== <code>LOWER$ (se)</code> ====
 
: Returns a copy of se in which all alphabetical characters are converted to lower case (see also UPPER)
 
: Example
 
<pre>
PRINT LOWER$("A1B2c3") - print a1b2c3
</pre>
 
==== <code>MAX (list of n)</code> ====
 
: Returns the maximum value from the given list.
 
: Example:
 
<pre>
PRINT MAX(3,8,25,1,2,9) - prints 25
</pre>
 
==== <code>MIN (list of n)</code> ====
 
: Returns the minimum value from the given list (see MAX)
 
==== <code>PEEK (add)</code> ====
 
: Returns the contents of the specified memory location (0-65535)
 
==== <code>PI</code> ====
 
: Returns value of PI (3.14159265)
 
==== <code>POS (#st)</code> ====
 
: Returns column number of print position relative to left edge of text window on stream st. st must be specified.
 
: Example:
 
<pre>
PRINT POS(#0) - prints 1
</pre>
 
==== <code>REMAIN (i)</code> ====
 
: Returns count remaining in delay timer i (0-3) then disables it.
 
==== <code>RIGHT$ (se,i)</code> ====
 
: Returns a substring of length i (0-255) characters from se, ending at the rightmost character of se.
 
: Example
 
<pre>
PRINT RIGHT$("ABCDEFG",3) - prints EFG
</pre>
 
==== <code>RND [(n)]</code> ====
: Generates the next random number in the current squence if n is positive or omitted. If n = 0, the random number generated will be the same as the last random number generated.
 
==== <code>ROUND (n[,i1])</code> ====
 
: Rounds n to a number of decimal places or to the power of ten specified by i. If i is negative, the n is rounded to give an absolute integer with i zeros before the decimal point.
: Example:
 
<pre>
PRINT ROUND(1562.357,2):PRINT ROUND(1562.375,-2) - prints 1562.36 1600
</pre>
 
==== <code>SGN (n)</code> ====
: Returns 1 if n is positive, 0 if n = 0, -1 if n is negative.
 
==== <code>SIN (n)</code> ====
: Returns sine of n in degree or radian mode (see DEG and RAD)
 
==== <code>SPACE$(i)</code> ====
 
: Creates a string containing i spaces (0-255)
 
==== <code>SQ (channel)</code> ====
: Returns a bit significant integer showing state of the sound queue for specified channel where channel 1, 2, 3 = A, B, C.
<pre>
Bits 0,1 and 2 number of free entries in the queue
Bits 3,4 and 5 redezvous state at head of the queue
Bit 6 head of the queue is held
Bit 7 channel is currently active
</pre>
 
==== <code>SQR (n)</code> ====
: Returns the square root of n.
 
==== <code>STR$(n)</code> ====
 
: Returns the string representation of number n.
 
==== <code>STRING$</code> ====
 
: Returns i copies of the string character specified by s.
 
: Example:
 
<pre>
PRINT STRING$(3,"*") - prints ***
</pre>
 
==== <code>TAN (n)</code> ====
 
: Returns the tangent of n. The DEG and RAD commands can be used to force the result to either mode.
 
==== <code>TEST (x,y)</code> ====
 
: Moves the graphics cursor by x and y and returns the value of the ink at that position.
 
==== <code>TESTR (x,y)</code> ====
 
: Moves the graphics cursor by x and y relative to its current position and returns the value of ink at that position.
 
==== <code>TIME</code> ====
: Returns time elapsed since the computer was switched on or reset.
: One second = TIME/300.
 
==== <code>UNT (add)</code> ====
 
: Returns an integer(-32768 to 32767) which is the two's complement of add.
 
: Example:
 
<pre>
PRINT UNT(&FF66) - prints -154
</pre>
 
==== <code>UPPER$(se)</code> ====
 
: Gives copy of se with all alphabetic characters in upper case.
 
==== <code>VAL(se)</code> ====
 
:Returns the numeric value (including signs) of first numeric character(s) in se. Returns 0 if se starts with a non-number.
:Example:
<pre>PRINT VAL("-12.34x"),VAL("A-12") - prints -12.34 0
</pre>
:Exception: if <se> starts with "&amp;" + character (and it's between "A" and "F") the whole character will be handled like a hexadezimal numeric character (...often used in DATA Loaders). The returning numeric value is a &nbsp;signed&nbsp;integer (16-Bit Word).
<pre>PRINT&nbsp;VAL("&amp;A") - returns a 10
 
PRINT&nbsp;VAL("&amp;7FFF") - returns a 32767
 
PRINT&nbsp;VAL("&amp;8000") - returns a -32768</pre>
 
==== <code>VPOS (#st)</code> ====
: Reports the current row (line) position of the text cursor relative to the top of the text windows of the specified stream.
 
==== <code>XPOS</code> ====
 
: Returns the current horizontal (x) position of the graphics cursor.
 
==== <code>YPOS</code> ====
 
: Returns the current vertical (y) position of the graphics cursor.
 
''(Please, fill in. Looks like a lot of work ;-) ...)''
 
== Error codes ==
 
* 1 '''Unexpected NEXT''' - <code>NEXT</code> encountered without matching <code>FOR</code>.
* 2 '''Syntax Error''' - Typing error or incorrect punctuation.
* 3 '''Unexpected RETURN''' - <code>RETURN</code> encountered when there was no active <code>GOSUB</code>.
* 4 '''DATA exhaused''' - Trying to <code>READ</code> data when data pointer has reached end of data.
* 5 '''Improper argument''' - The argument for a function is not legal (e.g. <code>PRINT SQR(-10)</code>).
* 6 '''Overflow''' - The computer cannot handle integers smaller than -32768 (signed) or larger than 65535 (unsigned) or floating point numbers greater than &plusmn;1.7E38.
* 7 '''Memory full''' - Not enough free RAM available to complete the operation. Program too big or control structures too deeply nested.
* 8 '''Line does not exist''' - Attempt to <code>RUN</code>, <code>GOTO</code> or <code>GOSUB</code> a non-existent line number.
* 9 '''Subscript out of range''' - Value of a subscript in an array is outside of range specified by <code>DIM</code> declaration or wrong number of dimensions supplied.
* 10 '''Array already dimensioned''' - Arrays can only be <code>DIM</code>ensioned once within a program.
* 11 '''Division by zero''' - Trying to divide a number by zero.
* 12 '''Invalid direct command''' - Using a statement as a direct command which is not allowed outside a program, e.g. <code>DEF FN</code>.
* 13 '''Type mismatch''' - Wrong data type encountered, string data instead of numeric value or vice versa.
* 14 '''String space full''' - String memory area is full.
* 15 '''String too long''' - String may not exceed 256 characters.
* 16 '''String expression too complex''' - A string expression needs to be broken down into smaller expressions.
* 17 '''Cannot CONTinue''' - <code>CONT</code> can only be used if program was stopped by [ESC] or a <code>STOP</code> in program - not after END. If the program is modified before issuing <code>CONT</code> you will also get this error.
* 18 '''Unknown user function''' - A <code>DEF FN</code> must be executed before calling an <code>FN</code> function.
* 19 '''RESUME missing''' - End of program has been reached while in error processing mode. Use <code>ON ERROR GOTO</code> before <code>RESUME</code>.
* 20 '''Unexpected RESUME''' - <code>RESUME</code> is only used in error processing mode, <code>ON ERROR GOTO</code> statement must be used first.
* 21 '''Direct Command found''' - A line without a line number has found while loading a file.
* 22 '''Operand missing''' - An incomplete expression has been found.
* 23 '''Line too long''' - The line contains too many statements.
* 24 '''EOF met''' - Trying to input data beyond end of data file.
* 25 '''FILE type error''' - Using a program file instead of a data file to read or write (or vice versa).
* 26 '''NEXT missing''' - The <code>NEXT</code> of a <code>FOR</code> ... <code>NEXT</code> loop is missing.
* 27 '''File already open''' - Trying to open an open file. Use <code>CLOSEIN</code> or <code>CLOSEOUT</code> first.
* 28 '''Unknown command''' - Given when an unknown command follows a <code>|</code>. e.g. <code>|DISC</code> on a CPC464 without AMSDOS installed.
* 29 '''WEND missing''' - The <code>WEND</code> part of the <code>WHILE</code> ... <code>WEND</code> loop is missing.
* 30 '''Unexpected WEND''' - <code>WEND</code> encountered without a corresponding active <code>WHILE</code>.
* 31 '''File not open''' - Attempting to read from or write to a file without <code>OPEN</code>ing it first.
* '''Unknown error''' - Executing <code>ERROR</code> command with any other legal error code number (up to 255).
== Other Basic Dialects available for the CPC ==
 
*[[BBC Basic]]
*[[C BASIC Compiler|C BASIC]]
*[[E-BASIC]]
 
== Web links ==
 
* [http://www.cpctech.org.uk/docs/bastech.html Technical information at the Unofficial Amstrad WWW Resource]
* [http://www.sean.co.uk/books/amstrad/bforbasic.shtm Locomotive Basic Tutorial by Sean McManus]
* [http://rosettacode.org/wiki/Category:Locomotive_Basic Code examples at Rosetta Code]
 
[[Category:Programming]]
[[Category:BASIC| ]]
[[Category:CPC Internal Components]]
[[Category:Operating System| ]]
[[Category:Software]]
[[Category:Programming software| ]]
205
edits