Table Of Contents:
General info
About variables
Mathematical and logical operations
Standard Basic language elements
Memory access
Subroutines
Bit-oriented language elements
Communication with memory mapped I/O ports
Structured language support (procedures and functions)
C64 related basic elements
#define directive parameters
Table Of Contents:
• General info
• About variables
• Mathematical and logical operations
• Standard Basic language elements
• Memory access
• Subroutines
• Bit-oriented language elements
• Communication with memory mapped I/O ports
• Structured language support (procedures and functions)
• C64 related basic elements
• #define directive parameters
• General info
Basic compiler editor is composed of editor panel (for user program editing) and source explorer (for easy navigation through all elements of user program - variables, symbols, constants, subroutines, procedures and functions). Editor formats and colorizes entered lines of user program, that simplifies the debugging process.
In all the user-defined program element names, that is in all the variable names, names of the procedures, functions, subroutines, constants, symbols and labels, both lower-case and upper-case characters can be used, along with the underscore and numeric characters. A numeric character will not be accepted by the compiler to be the leading character in the element name.
The primary output of the compiler is an assembler source file. However, with an appropriate command from the menu it can be assembled and even loaded in the simulator with a single click. Menu commands and options are rich, as well as the commands from the right-click pop-up menus for the editor and source explorer. Basic compiler's assembler output contains many useful comment lines, that makes it very helpful for educational purposes, also.
Show Warnings
If Show Warnings option is enabled, in the Warnings window Basic compiler will show information about unused declarations, subroutines, procedures and functions in the user basic program.
Do Not Compile Unused Code
If this option is enabled, Basic compiler will not compile unused declarations, subroutines, procedures and functions, in order to save memory resources.
Initialize Variables On Declaration
If this option is enabled, Basic compiler will reset to zero all memory locations allocated for variables, at the position of their declaration in the basic program. This option is useful for beginners, because RAM memory is filled with random values at device power-up, and it is easy to make a mistake to assume that all variables are reset to zero at power-up. Experienced users can save some program memory, by disabling this option and taking control of variable initial values by user program where necessary.
Optimize Variables Declaration
This option will turn on the compiler internal routine that will optimize the variables declaration order based on the usage frequency of the variables. In this way, the most frequently used variables will be stored in higher RAM memory locations, resulting in possibly smaller size of the generated code.
• About variables
Eight data types are supported:
Boolean - 1-byte, True or False
Short - 1-byte integers in the range -128 to 127
UShort - 1-byte unsigned integers in the range 0 to 255
Integer - 2-byte integers in the range -32,768 to 32,767
UInteger - 2-byte unsigned integers in the range 0 to 65,535
Long - 4-byte integers in the range -2,147,483,648 to 2,147,483,647
ULong - 4-byte unsigned integers in the range 0 to 4,294,967,295
Single - 4-byte single precision floating point numbers, 7 digits precision
Short, UShort, Integer, UInteger, Long and ULong variable types will be sometimes referred to as 'all integer data types'.
With Single variable type added to the list, those variable types will be sometimes referred to as 'all numeric data types'.
Variables can be global (declared in the main program, before the End statement) or local (declared in subroutines, procedures and functions). Variable name used for a variable with global scope can be used again for local variable names. The compiler will reserve separate memory locations for them. There are no other limits for the total number of variables, but the available RAM memory. Variables are declared using DIM statement:
Dim a As Boolean
Dim b As Short
Dim c As Integer
Dim d As Long
Dim e As Single
Dim statement allows multiple declarations in one line of code by using comma-separated list of variable names:
Dim c1, c2, c3 As Integer
It is possible to use one-dimensional arrays for all variable types. For example:
Dim a(100) As Integer
declares an array of 100 Integer variables with array index in the range [0-99].
It is possible to make conversions between all data types (except Boolean) by simple assignment statements:
Dim a As Long
Dim b As Single
b = 123.456
a = b
This will result in variable A holding integer value 123.
Memory map that will be used by the compiler is set using the two parameters: RAMEND and ROMSTART. By default ROMSTART is set to $C000, and RAMEND to $BFFF for the 48K RAM with 16K ROM system. If needed, the new correct values should be assigned to these parameters using the #define directive (alias: Define). Variables will be allocated by the compiler to the memory locations in the defined RAM space, and the compiled code will start from the first ROM memory location. RAMEND value must be always less then ROMSTART parameter value. The compiler will automatically fix the user parameter values selection if it is not consistent:
#define ROMSTART = $8000
Constants can be used in decimal number system with no special marks, in hexadecimal number system with leading 0x or leading $ notation (or with H at the end) and in binary system with leading % mark (or with B at the end). ASCII value of a character can be expressed in string format (e.g. "A"). Keywords True and False are also available for Boolean type constants. For example:
Dim a As Boolean
Dim b As Short
Dim c As Integer
a = True
b = %01010101
c = 0x55aa
c = "C"
Constants can be assigned to symbolic names using CONST directive. Constants can be global or local. One example:
Dim a As Single
Const pi = 3.14159
a = pi
It is possible to use comments in basic source programs. The comments must begin with single quote symbol (') and may be placed anywhere in the program.
Comment sign '//' is an alternative for the standard single quote sign.
Lines of assembler source code may be placed anywhere in basic source program and must begin with ASM: prefix. If labels are used, no space should be left between the ASM: prefix and the label. For example:
ASM: NOP
ASM:LABEL1: LDA #$34
Symbolic names of all variables and constants (global and local) can be used as the arguments of the assembler instructions. The compiler will replace that symbolic name with the proper variable address or constant value:
Dim varname As Short
varname = 0
ASM: LDA #$55
ASM: STA VARNAME
If large amount of assembler code should be used, it can be loaded from an external assembler file and included to the current program by using IncludeASM directive. Its only argument is a string containing the path to the external .ASM file. This can be the full path or only the file name, if the external file is located in the same folder as the current basic program file. During the compilation process the external assembler code will be appended to the current program at its end, and not at the position of the directive. Multiple files can be included with separate IncludeASM directives. External assembler files should not contain ASM: prefix used for inline assembler code. It is also strongly suggested not to use ORG directives in the external assembler code. For example:
IncludeASM "test.asm"
IncludeASM "d:\example\test2.asm"
• Mathematical and logical operations
Five arithmetic operations (+, -, *, /, MOD) are available for all integer data types. MOD operation is not applicable for Single data type variables. The compiler is able to compile all possible complex arithmetic expressions, including those containing math functions and user-defined functions. The compiler will accept all numeric data types for both the arguments and the result of the operation. For example:
Dim a As Long
Dim b As Long
Dim c As Long
a = 1234
b = 2345
b = a * b
c = a * 100 - (a + b)
Square root of an integer number (0-65535 range) can be calculated using SQR function. The compiler will accept all numeric data types for both the argument and the result of the operation.
Dim x As Integer
Dim x2 As Single
x = 3600
x = Sqr(x)
x2 = 22500
x2 = Sqr(x2)
There are eight single precision mathematical functions (EXP, LN, LOG, SIN, COS, SQRT, TAN, POW) that can be used with Single data type variables. All math functions can also be used in complex math expressions. The compiler will accept all numeric data types for both the arguments and the result of the operation.
For example:
Dim a As Single
a = 2
a = Sqrt(a)
Another example:
Dim x1 As Integer
Dim x2 As Single
Dim x3 As Single
x1 = 1
While x1 < 6
x2 = Sin(x1)
x3 = Cos(x1)
x1 = x1 + 1
Wend
For Boolean and Short data type variables seven basic logical operations are supported. It is possible to make only one logical operation in one single statement. Logical operations are allowed only in assignment statements. For example:
Example 1:
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean
a = True
b = False
c = Not a
c = a And b
c = a Or b
c = a Xor b
c = a Nand b
c = a Nor b
c = a Nxor b
Example 2:
Dim a As Short
a = 0x55
a = Not a
• Standard Basic language elements
Unconditional jumps are performed by GOTO statement. It uses line label name as argument. Line labels can be global or local. Line labels must be followed by colon mark ':'. Here is one example:
Dim a As Long
a = 0
loop: a = a + 1
Goto loop
Three standard BASIC statements are supported: FOR-TO-STEP-NEXT, WHILE-WEND and IF-THEN-ELSE-ENDIF. In FOR-TO-STEP-NEXT statement all variables must be of the same data type as the running variable. Here are several examples:
Example 1:
Dim a As Integer
Dim b(100) As Single
For a = 0 To 99
b(a) = a
Next a
Example 2:
Dim a As Long
a = 100000
While a > 0
a = a - 1
Wend
Example 3:
Dim x1 As Single
Dim yeq As Boolean
Dim yne As Boolean
Dim ygt As Boolean
Dim ygte As Boolean
Dim ylte As Boolean
Dim ylt As Boolean
For x1 = 12.25 To 12.75 Step 0.25
yeq = False
yne = False
ygt = False
ygte = False
ylte = False
ylt = False
If x1 = 12.5 Then yeq = True
If x1 <> 12.5 Then yne = True
If x1 > 12.5 Then ygt = True
If x1 >= 12.5 Then ygte = True
If x1 <= 12.5 Then ylte = True
If x1 < 12.5 Then ylt = True
Next x1
For statement will accept all numeric data types for the running variable. Exit For statement provides a way to exit a For-Next loop. It transfers control to the statement following the Next statement.
After IF-THEN statement in the same line can be placed almost every other possible statement and then ENDIF is not used. Six standard comparison operators are available: =, <>, >, >=, <, <=. There are no limits for the number of nested statements of any kind.
• Memory access
Standard BASIC elements for accessing memory are available: POKE statement and PEEK function. For the address argument the variables of all numeric data types will be accepted, including the UInteger range constants. The compiler will also accept UShort range constants for the data argument of the POKE statement. For the PEEK function the result variable can be of any numeric data type. For example:
Dim a As UInteger
Dim b As UInteger
Dim c As UInteger
For a = 0 To 15
b = Peek(a)
c = 240 + a
Poke c, b
Next a
• Subroutines
Structured basic programs can be written with subroutines. When using subroutines, the main routine must be ended with END statement, and subroutines must be placed after END statement in program. END statement is compiled as an infinite loop (if unofficial instructions are enabled, the JAM instruction will be used). Subroutines should be declared with SUB statement followed by the subroutine name. All variables declared in a subroutine have local scope, so they don't need to have unique names. Subroutines must be ended with END SUB statement. They can be conditionally exited with EXIT SUB statement. Calls to subroutines are implemented with GOSUB statement.
For backward compatibility, global scope line labels will still be recognized as subroutine declarations, GOSUB statements will also accept the line label names as arguments and RETURN statement can be used for the final return from a subroutine. Users need to take care that the program structure is consistent when using local scope line label as GOSUB argument (the compiler will accept that, however an applicable warning will be generated).
Here are two examples:
Example 1:
Dim x1 As Integer
Dim x2 As Integer
Dim x3 As Integer
For x1 = 0 To 10
Gosub calculate_x2x3
Next x1
End
Sub calculate_x2x3
x2 = 100 + x1
If x1 > 5 Then Exit Sub
x3 = x2
End Sub
Example 2:
Dim a As Integer
Dim b As Integer
b = 100
Gosub fillmemory
b = 101
Gosub fillmemory
End
fillmemory:
For a = 20000 To 21000
Poke a, b
Next a
Return
• Bit-oriented language elements
SETBIT, RESETBIT and INVERTBIT statements can be used to set, reset or invert the individual bits in Short or UShort data type variables. The first argument is the target bit number and it must be a constant in the range 0-7 or a variable of any numeric data types. The second argument must be a Short or UShort variable that will be the target of the operation.
Dim a As UShort
a = 0xf0
SetBit 0, a
ResetBit 7, a
InvertBit 0, a
By using the TESTBIT function it is possible to copy to a Boolean data type variable the value contained in the specific bit of a Short or UShort data type variable. The MAKEBIT function will copy the value of a Boolean data type variable or constant to the specific bit of a Short or UShort data type variable. The first argument of these functions is the target bit number and it must be a constant in the range 0-7 or a variable of any numeric data type. For example:
Dim a1(8) As Boolean
Dim b1 As Short
Dim b2 As UShort
Dim w1 As Integer
b1 = $55
b2 = $aa
For w1 = 7 To 0 Step -1
a1(w1) = TestBit(w1, b1)
Next w1
For w1 = 0 To 7
a1(w1) = TestBit(w1, b2)
Next w1
b1 = 0
b2 = 0
a1(0) = 1
a1(1) = 0
a1(2) = 1
a1(3) = 0
a1(4) = 1
a1(5) = 0
a1(6) = 1
a1(7) = 0
For w1 = 0 To 7
b1 = MakeBit(w1, a1(w1))
b2 = MakeBit(w1, True)
Next w1
• Communication with memory mapped I/O ports
The communication with the outside world is done through the memory mapped I/O ports using GET function and PUT and PRINT statements. The argument of the GET function is the port number and must be a constant value in the UInteger range [0-65535]. It can be used to assign the value received on the port to a variable of any numeric data type. For example:
Dim a As Integer
a = Get($a000)
PUT statement can be used to send data to the specified memory mapped port. The data can be an UShort constant value in the range [0-255] or contained in a variable of any numeric data type. Only the lowest byte of the variable is sent to the port. For example:
Dim a As Integer
a = 200
Put $a001, a
PRINT statement can be used to send string constants and decimal string representations of variables to the specified port address. String constants should begin and end with the double quotation marks. There are four compiler symbolic string constants available: Qt (or """") for the double quotation mark (ASCII code 34), CrLf for the carriage return - line feed sequence (ASCII codes 13-10), Cr for the carriage return (ASCII code 13) and Lf for the line feed character (ASCII code 10). User-defined names for the string constants with the Const directive can also be used. Variables of all the supported data types can be used as arguments to display their decimal string representations. There are no limits for the number of the comma-separated arguments used in the statement. For the string representation of Single data type variables, the value of the PRINT_SINGLE_DIGITS parameter is used to determine the number of digits that will be printed after the decimal point. The default value is 3. Single values greater than 8388607 or lower than 0.00000191 are considered to be out of the print range and will be printed with one 'E' character.
Here is an example:
Dim a As Single
a = 123.456
Print $a001, "THE NUMBER IS "
Print $a001, a
Print $a001, CrLf
This can also be done using only one PRINT statement:
Print $a001, "THE NUMBER IS ", a, CrLf
Another example:
#define PRINT_SINGLE_DIGITS = 4
Dim x2 As Single
Dim x1 As Long
x2 = -123.45678
x1 = x2
Print $1234, "Long = ", x1, CrLf
Print $1234, "Single = ", x2, CrLf
• Structured language support (procedures and functions)
Procedures can be declared with PROC statement. They can contain up to 5 arguments (comma separated list) and all available data types can be used for argument variables. Argument variables are declared locally, so they do not need to have unique names in relation to the rest of user basic program, that makes very easy to re-use once written procedures in other basic programs. Procedures can be conditionally exited with EXIT statement. They must be ended with END PROC statement and must be placed after END statement in program. Calls to procedures are implemented with CALL statement. The passed arguments can be variables, numeric constants or complex numeric expressions. For example:
Dim x As Integer
For x = 0 To 255
Call port_display(x)
Next x
End
Proc port_display(arg1 As Integer)
Print $a001, "THE NUMBER IS ", arg1, CrLf
End Proc
All facts stated for procedures are valid for functions, also. Functions can be declared with FUNCTION statement. They can contain up to 5 arguments and argument variables are declared locally. Functions can be exited with EXIT statement and must be ended with END FUNCTION. The name of the function is declared as a global variable, so if the function is called with CALL statement, after its execution the function variable will contain the result. Standard way of function calls in assignment statements can be used, also. One simple example:
Dim x As Integer
Dim y As Integer
For x = 0 To 100
y = square(x) + 1
Next x
End
Function square(arg1 As Integer) As Integer
square = arg1 * arg1
End Function
The default mechanism is to pass an argument to a procedure by value. Either no prefix or ByVal prefix can be used for that argument. The procedure will use the argument value and will not change the input value of the calling variable. There are two passing mechanisms for passing arguments to procedures by reference - ByRef and ByRefOut. When ByRef prefix is used to pass a variable argument to a procedure, the procedure will use the input variable value, but it can also change the value of the variable during the procedure execution. The calling variable will be exposed to change. When ByRefOut prefix is used for the procedure argument, the input value of the calling variable will not be passed to the procedure at all. The procedure will only return an output value to the calling code through that argument. So, ByRefOut mechanism should be used when the procedure has a genuine need to output only a value to the calling code, and in that way the result will be a more optimized code compared to the use of the standard ByRef mechanism.
One test example:
Dim in_only As Short
Dim inc_me As Short
Dim add_inc_me_and_in_only As Short
in_only = 5
inc_me = 10
Call testbyref(in_only, inc_me, add_inc_me_and_in_only)
'after this call
'inc_me = 11
'add_inc_me_and_in_only = 16
End
Proc testbyref(arg1 As Short, ByRef arg2 As Short, ByRefOut arg3 As Short)
arg2 = arg2 + 1
arg3 = arg1 + arg2
End Proc
Procedures (and functions) can also be called without the Call statement. In that case, the procedure name should be followed by the comma-separated list of arguments.
The lines of code with the same effect:
Call port_display(x)
port_display x
Basic source code from an external file can be included to the current program by using INCLUDE directive. Its only argument is a string containing the path to the external .BAS file. This can be the full path or only the file name, if the external file is located in the same folder as the current basic program file. During the compilation process the external basic source will be appended to the current program. Multiple files can be included with separate INCLUDE directives. To maintain the overall basic code structure, it is strongly suggested that the external file contains global declarations, subroutines, procedures and functions, only. Here is one very simple example for the demonstration:
main.bas:
Dim i As Integer
Dim j As Integer
Include "inc1.bas"
Include "inc2.bas"
For i = 1 To 10
j = func1(i, 100)
Call proc1(j)
Next i
End
inc1.bas:
Dim total As Integer
Proc proc1(i As Integer)
total = total + i
End Proc
inc2.bas:
Function func1(i As Integer, j As Integer) As Integer
func1 = i + j
End Function
• C64 related basic elements
The integrated basic compiler features also the special set of Commodore 64 related statements that can be used to generate working C64 PRG files ready to be simulated together with the C64 ROM image.
In order to use any of the C64 related statement, one must set the value of the C64_TARGET parameter to 1 using the #define directive. That will also set the compiler to make the needed modifications in the generated ASM file, so that the code does not interfere with the memory locations relevant to the C64 ROM. Finally, that will also set the integrated assembler to generate C64 PRG file in addition to the standard assembler output. If C64_TARGET is set to 1, ROMSTART parameter will be set to $A000, and RAMEND to $9FFF by the compiler to match the C64 setup.
When the compiled basic program uses C64 related statements, the result is that the generated OBJ (HEX) files could not directly be simulated in the simulator, because those statements reference certain C64 ROM routines. For the simulation of the generated code, C64 ROM must be loaded into the simulator and the C64 system needs to be initialized using the Commodore 64 Rom I/O Terminal tool. Then, the PRG file that is also generated by the basic compiler should be loaded using the Load PRG command on the C64 I/O Terminal tool.
C64_Cls statement clears the C64 screen video memory using the appropriate C64 ROM routine. The cursor is set at position 1,1 (upper-left corner).
C64_CursorSet statement position the cursor at the desired location on the screen. It has two comma-separated arguments, The first is the column number in the range 1-40, the second argument is the screen raw number in the range 1-25. Both arguments will accept constant values, but also the variables of all integer data types with Single included.
C64_Print statement can be used to display string constants and decimal string representations of variables on the C64 screen at the current cursor position. String constants in the double quotation marks are accepted including the compiler symbolic string constants like CrLf. User-defined names for the string constants with the Const directive can also be used. Variables of all the supported data types can be used as arguments to display their decimal string representations. There are no limits for the number of the comma-separated arguments used in the statement. The lowercase letters will be changed to uppercase.
Here is an example:
#define C64_TARGET = 1
#define PRINT_SINGLE_DIGITS = 5
Dim x1 As Integer
Dim x2 As UInteger
Dim x3 As Short
Dim x4 As UShort
Dim x5 As Boolean
Dim x6 As Long
Dim x7 As ULong
Dim x8 As Single
Const welcome = "Display the values of the variables:"
C64_Cls
C64_Print welcome, CrLf
x1 = -31456
C64_CursorSet 3, 5
C64_Print "Integer = ", x1, CrLf
x2 = 65535
C64_CursorSet 4, 6
C64_Print "UInteger = ", x2, CrLf
x3 = -68
C64_CursorSet 5, 7
C64_Print "Short = ", x3, CrLf
x4 = 251
C64_CursorSet 6, 8
C64_Print "UShort = ", x4, CrLf
x5 = False
C64_CursorSet 7, 9
C64_Print "Boolean = ", x5, CrLf
x6 = -1234567890
C64_CursorSet 8, 10
C64_Print "Long = ", x6, CrLf
x7 = 4123456789
C64_CursorSet 9, 11
C64_Print "ULong = ", x7, CrLf
x8 = -123.456
C64_CursorSet 10, 12
C64_Print "Single = ", x8, CrLf
C64_PRG_HEADER is the #define directive parameter that sets the starting location in the C64 ram memory where the generated code should be stored to. The default value is $4000. Its value defines the way how the PRG file should be executed - it is used for the C64 basic SYS command argument. In order to be able to execute the compiled PRG file with the RUN command, the user should set the C64_PRG_ADDSYS parameter value to 1. The compiler will then embed the appropriate SYS statement at the beginning of the PRG file. If C64_PRG_ADDSYS is set to 1, C64_PRG_HEADER parameter will be automatically set to $0801 by the compiler.
C64_GetChar statement will check the number of characters in the C64 keyboard buffer queue. If there is a character waiting, its 1-byte code will be loaded to the variable argument. All integer data types will be accepted for the C64_GetChar statement argument. If the keyboard buffer is empty, the value 0 will be returned.
C64_InputChar is a similar statement to C64_GetChar. The difference is that the Input statement will wait until a key has been pressed on the keyboard. Also, there are no limits for the number of the comma-separated arguments used in the statement.
C64_PutChar statement is used to display a character on the C64 screen at the current cursor position. The arguments can be the character code constants, or the character codes stored in any of the integer data type variables. Also accepts multiple comma-separated arguments.
For example:
#define C64_TARGET = 1
#define C64_PRG_ADDSYS = 1
Dim x1 As Integer
C64_Cls
C64_Print "Press a key to start the play.", Cr
loop1:
C64_GetChar x1
If x1 = 0 Then Goto loop1
C64_Print "Now, the pressed keys", Cr
C64_Print "will be echoed twice.", Cr
loop2:
C64_InputChar x1
If x1 = "@" Then Goto endlab
C64_PutChar x1, x1
Goto loop2
endlab:
• #define directive parameters
Here is the list of all available parameters for the #define directive, along with their default values and allowed ranges of values:
ROMSTART - (default value: $C000, allowed range: $0400-$FC00)
RAMEND - (default value: $BFFF, allowed range: $03FF-$FBFF)
The default values for these parameters are set for the 48K RAM with 16K ROM system.
PRINT_SINGLE_DIGITS - (default value: 3, allowed range: 1-6)
Used to determine the number of digits that will be printed after the decimal point for the Single data type variables.
C64_TARGET - (default value: 0, allowed range: 0-1)
C64_PRG_ADDSYS - (default value: 0, allowed range: 0-1)
C64_PRG_HEADER - (default value: $4000, allowed range: $0801-$9FFF)
These parameters are explained in the C64 related basic elements section. Used for the generation of the C64 PRG files.
The compiler will automatically fix the user parameter values selection if it is not consistent.
RAMEND value must be always less then ROMSTART parameter value.
If C64_TARGET = 1, ROMSTART parameter will be set to $A000, and RAMEND to $9FFF.
And, if C64_PRG_ADDSYS = 1, C64_PRG_HEADER parameter will be set to $0801.