Changes

Jump to: navigation, search

Technical information about Locomotive BASIC

8,344 bytes added, 10:58, 13 July 2019
[[Category:Cpctech.org]]
'''''This artikel article originally came from Kevin Thackers' archive at [http://www.cpctech.org.uk http://www.cpctech.org.uk].'''''
= Technical information about Locomotive BASIC =
The are two versions of Locomotive BASIC used in the Amstrad CPC and CPC+ computers. v1.0 is used by the CPC464, and v1.1 is used by the CPC664, CPC6128, CPC464+ and CPC6128+.
 
== BASIC v1.0 only ==
 
There is a region of memory between AC00-AC1B which is filled with RET instructions. The BASIC ROM calls the following locations:
AC01,AC04,AC07,AC0A,AC0D,AC10,AC13,AC16,AC19
 
It almost looks like these were used for debugging the ROM but were left in.
 
They can be used to implement new BASIC commands.
== Passing parameters to a RSX (Resident System Extension) command or binary function ==
A RSX (Resident System Extension) command is accessed through BASIC with a "|" character prefix. e.g. The "DIR" RSX, is accessed by "|DIR". A binary function is accessed through BASIC using the "CALL" command.
Both RSX and CALL commands works both work (are!) similar from the BASIC command line and invokes a invoke machine code. Difference- the only difference is: with the help of a RSX command you don't need to know the exact access address in memory. You can pass up to 32 parameters with a CALL or RSX command. Possible parameters could be integer, floating points and strings. Just store the parameter into a variable:Passing for integer:
<pre>
CALL &4000,32,34,&5E,&x10101010
 
|RSX-command,32,34,&5E,&x10101010
</pre>
Passing for floating points
<pre>
@a!=43.375
CALL or |RSX-command,@a!
</pre>
 
<br> Passing a string parameter:
<br> BASIC v1.0
<pre>a$="A":|DRIVE,@a$
</pre>
BASIC v1.1:
<pre>|DRIVE,"A"
</pre>
or
<pre>
b$="test"
CALL or |RSX-command,@b$
</pre>
 
The parameters will be stored inside the range of Stack:
<pre>
...
MSB PARAMn
IX+2 0 ------> LSB PARAMn
</pre>
with the help of that assembly code is it possible to find the parameters:
<pre>
ROUT1: CP "x" ;test register A if "x" parameters were hand-overedgiven, e.g. "x" = 2 RET NZ ; if no > failure and returnnot exactly x parameters then do not execute RSX. LD L,(IX+20) LD H,(IX+31) ; HL = value of last parameter LD E,(IX+42) LD D,(IX+53) ; DE = value of next to last parameter
...
</pre>
A register holds the first two bytes at the end number of parameters. IX points to each parameter. IX+0/IX+1 is the last parameter. This happens because each parameter in order is pushed onto the stack shows and IX then points to the internal return address from BASIC interpreter where it was executedlast parameter pushed. Each parameter is a 16-bit value.
If a parameter is a string, then IX holds the address of the string descriptor which point to a 3 byte block. The first byte of the string descriptor is the length. The next 2 bytes are an address that point to the string in memory.
<br> Passing With the help of the variable container "@" it is also possible to get a string result from an invoked mc-code back to basic.In this case the parameter: contains the address of the integer, floating point or string. You can modify these to give the result.BASIC allocates the strings, so when you modify a string, do not write more characters than the original string.
<br> BASIC v1.0 <pre>a$="A"Hint:|DRIVE,@a$</pre> the first two bytes at the end of stack shows the internal return address from BASIC v1interpreter where it was executed.1: <pre>|DRIVE,"A"</pre>
== Additional keywords and variables in BASIC v1.1 ==
<br> <br>
== Entering BASIC programs ==
A maximum of 255 characters can be entered for a single BASIC line. This is converted into the tokenised BASIC program which is more compact than the line entered. The BASIC keywords are converted into "tokens", or 1-2 byte sequences which uniquely identify each keyword.
There are some rules for assigning names for variables:: - it is not allowed to define a variable starting with a figure (the system expect then programming mode and interpret it as a program line number): - no space (or 'under' as it is common today) between variable names are allowed: - BASIC keywords can't be used: - different sign e.g.: a slash (doesn't depend which direction), a 'minus' sign, a 'and' sign can't be used in a variable names: - after a dollar sign (for defining string variables) it is not allowed to use further signs: - different defining functions together are not allowed (e.g. use '%' together with '$') : - you can use 40 characters for a variable name: - use ! for defining a REAL value variable (like the function DEFREAL but only for the assigned variable) > It's also possible to omit the exlamation mark because it's the default system declaration after powering on then CPC.: - use % for defining a INTEGER value variable (like the function DEFINT but only for the assigned variable): - use $ for defining a STRING variable (like the function DEFSTR but only for the assigned variable): - Minimum line number is 1. Maximum line number is 65535. This is converted into the tokenised BASIC program which is more compact than the line entered. The BASIC keywords are converted into "tokens", or 1-2 byte sequences which uniquely identify each keyword.  <br>  == Speeding up BASIC programs == To speed up BASIC programs following rules could help:: - Avoid loops or a branching program structure if possible. Internally the operating system searches for the right line number always from the beginning of the program 'listing'. Often recurring subroutines (if necessary) should be at the beginning of RAM to speed up things.: - Avoid the index variable after the <code>NEXT</code> command/statement (although it isn't a good program style): - Avoid spaces inside program code or remarks: - Put lots of statements as possible in ONE line: - Use variables instead of constants. A constand has always to be converted in a binary/digital format. A variable is already converted.: - Dimensioning at the beginning of the program: - Dimensioning your variables & pre-calculate: - Avoid AND/OR/XOR in IF-THEN statements. It's faster to write IF ... THEN (statement 1).. IF ... THEN (statement 2).
== Structure of a BASIC program ==
<br> Notes:
#*This 16-bit value has two functions: #**if "0" it signals the end of the BASIC program. In this case, there is no furthur BASIC program lines or data. #**, otherwise, this value defines the length of the tokenised BASIC program line in bytes, and includes the 2 bytes defining this value, the 2 bytes defining the program line number, and the end of line marker. This number is stored in little endian notation with low byte followed by high byte. #*This 16-bit value defines the line number and exists if the length of line data in bytes is not "0". A line number is a integer number in the range 1-65535. This number is stored in little endian notation with low byte followed by high byte. #*This data defines the tokenised BASIC program line and exists if the length of line data in bytes is not "0". The length is dependant on the BASIC program line contents. #*This value defines the end of the tokenised BASIC line data and exists if the length of line data in bytes is not "0". The BASIC interpreter looks for this token during the processing of this line, and if found, will stop execution and continue to the next line.
== BASIC tokens ==
|-
| &amp;1d
| 16-bit BASIC program line memory address pointer(see notes)
|-
| &amp;1e
| INPUT
|-
| &amp;a4
| KEY
|-
|-
| &amp;b5
| ON SQ
|-
| &amp;b6
<br> Notes:
#*The &amp;ff code is used as a prefix for more keywords. See the table below for the list of keywords using this prefix. #*&amp;e2,&amp;e8 and &amp;e9 are not used #*&amp;7c ("|") is a character prefix used to identify a RSX command. e.g. "|DIR".
e.g. "|DIR". An RSX is encoded in a BASIC program using the following structure:
{| border="1"
|}
#*This token identifies a integer (16-bit) number. The two bytes following this token is the number, stored low byte then high byte. e.g. &amp;191a,&amp;2d,&amp;00 represents the integer number "&amp;002d"="45". #*This token (&amp;0B) is changed at run-time to &amp;0D #*This token identifies a integer variable. 02,00,00,var name offset to number in program setup when program RUN. The variable name is stored directly, with bit 7 of the last character set. #*This token identifies a string variable. offset to string in program string stored AS IS with quotes around it. #*This token identifies a floating point number. Immediatly following the token are 5 bytes which define the number.
{| border="1"
|}
#*#This byte is the BASIC token identifying a floating point number #*#These 5 bytes define the floating point number. #*The space symbol is used as a command seperator #*Variable definitions: #**The variable definition has the following structure:
{| border="1"
<br> The 16-bit byte offset is initially set to 0, but is setup when the program is RUN. The offset is stored in little-endian format, with low byte followed by high byte.
#**The token byte defines the variable type (i.e. string, floating point, integer) and the suffix ("$", none and "%"). #**The variable name is stored in the program, with bit 7 of the last character set to "1". e.g. The variable named "abc" will be encoded as 'a','b','c'+&amp;80 #*String values: #**Each string value is prefixed with &amp;22 token. #**All strings must be enclosed by double quote symbols (&amp;quot). This symbols define the start and end of the string value. #**The string value can contain any symbol except the quote ("). i.e. 0-&amp;21, and &amp;23-&amp;ff can be used. #*After running a program, Tokens of "1d" with 16-bit BASIC program line pointers are changed to token "1e" with the 16-bit memory address of the BASIC line number in memory.
<br> This table list the BASIC tokens with a &amp;ff prefix byte.
<br> NOTES:
#*These codes are prefixed by &amp;FF. e.g. The keyword "ABS" is stored as the following sequence: <pre>&amp;FF,&amp;00</pre> #*Codes &amp;1e...&amp;3f inclusive are not used #*Codes &amp;4a...&amp;6f inclusive are not used #*Codes &amp;80...&amp;ff inclusive are not used *When a BASIC program is in memory, the BASIC ROM can replace "16-bit BASIC program line number" tokens with "16-bit BASIC program line memory address pointer". When a program is loaded or saved, there will only be "16-bit BASIC program line number" tokens followed by a 16-bit number of the line. When a program is running, BASIC will replace the "16-bit BASIC program line number" with the "16-bit BASIC program line memory address pointer" token, and replace the 16-bit line number with the memory address of the start of that line.  It does this to avoid having to lookup the address of the line each time. * When a REM or ' is seen, the characters following it are output directly (i.e. the bytes are not tokenized) until the end of line or a ':' is seen. * When a BASIC program is listed each line is converted to a string (this includes the line number and the tokens converted to their strings). There is a maximum length for this string of 256 characters. This means that if the BASIC line is LISTed or EDITed, then the line will be displayed incorrectly with missing characters on the end. This is also noticed if you have a "10-liner" program that packs as much BASIC as possible into each line and renumber it so the line number has more digits.
== Floating Point data definition ==
To obtain the signed exponent, you must subtract 128 from the stored exponent. The minimum exponent is 0 and this describes a number of 2^-127. The maximum exponent is 255 and this describes a number of 2^128.
 
=== BASIC Display of Floating point numbers ===
 
* Numbers are displayed to 9 decimal places.
* If the number doesn't have a fractional part (e.g. .0) then the fractional part is not displayed.
* Trailing zeros (zero digits after the last digit in the fraction) are not displayed
* The value is rounded in the following way for display:
 
If the 10th fractional digit is 5 or above, then the number is rounded up.
If the 10th fractional digit is 4 or below, then the number is rounded down.
e.g.
0.1234567891 is displayed as 0.123456789
0.1234567895 is displayed as 0.12345679
 
=== floating point example in RAM by MaV ===
 
The initial figure = 43.375.
An example in BASIC:
<pre>
a!=43.375
READY
PRINT a!
43.375
PRINT @a!
374
FOR I=0 TO 4:PRINT HEX$(PEEK(374+I),2);:NEXT I
0000802D86
</pre>
the result for better readability:<br>
'''00 00 80 2D 86'''<br>
The first byte contains the least significant bits of the mantissa, and every following byte up to the fourth byte contain the bits following the first in significance and in exactly that order!
 
That means that you have to turn around the first four bytes if you want to read the mantissa correctly:
2D800000
or
0010.1101.1000.0000.0000.0000.0000.0000
in binary (the stops are inserted for better readability).
 
Now the highest significant bit here is 0, so the number is positive.
What's more, you need to clear that bit and substitute the implied highest bit (see description in the link), which is always 1 (regardless of the deleted sign bit!!!).
Thus you get:
AD800000
or
1010.1101.1000.0000.0000.0000.0000.0000
in binary.
 
The mantissa has an implied decimal point before the number. Since the number is 2^32 bits (i.e. 4 bytes) long you have to divide by 2^32 to get the proper decimal representation.
 
AD800000 (hex) = 2910846976 (dec)
 
2910846976 / 2^32 = 0.677734375
 
The mantissa in a decimal representation is: 0.677734375
 
Now, the last byte contains the exponent: 86 (hex) or 134 (dec). You need to subtract 128 to get the real exponent, the highest bit tells you that the exponent is positive (1) or negative (0). Your exponent is 6.
 
0.677734375 * 2^6 = 43.375
 
'''Another example:'''<br>
PI is represented in memory as:<br>
'''A2 DA 0F 49 82'''<br>
The mantissa is<br>
490FDAA2 (highest significant bit is zero => number is positive)
The exponent is<br>
82 (=positive mantissa, value=2 thus multiply mantissa with 2^2)<br>
 
Delete the sign bit (which is 0 anyway), and then add the implied 1 bit:<br>
C90FDAA2 (hex) = 3373259426 (dec) = 1100.1001.0000.1111.1101.1010.1010.0010 (bin)<br><br>
 
Calculate the decimal representation of the mantissa:<br>
3373259426 / 2^32 = 0,7853981633670628070831298828125<br><br>
 
Then add the exponent (82h - 80h = 2 thus 2^2)<br>
0,7853981633670628070831298828125 * 2^2 = 3,14159265346825122833251953125<br><br>
 
Voilà! The internal representation of the number PI on the CPC is: 3,14159265346825122833251953125<br><br>
 
 
If you compare to a (more) correct representation of PI, you'll see that the CPC is correct up to the 9th decimal place:<br><br>
 
 
CPC:<br>
3.14159265346825122833251953125<br>
more correctly:<br>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986...
== BASIC floating-point/real variables ==
A floating point (real) variable describes a number with integer and fractional parts. The biggest figure which can be stored correctly in RAM by a variable is 2^32-1 = 4294967295.
#*A integer variable is defined with a "!" character postfix.
Where "a" should be replaced with the name of the variable.
<br>
== BASIC integer variables ==
#**A string variable can store a minimum of 0 characters and a maximum of 255 characters.
#**The length of the string in characters is defined in the string descriptor block. The string does not have a termination character.
 
[[Category:Programming]]
[[Category:BASIC| ]]
[[Category:CPC Internal Components]]
[[Category:Operating System| ]]
[[Category:Software]]
[[Category:Programming software| ]]
2,541
edits