Z80 Simulator IDE
BASIC Compiler Reference Manual


Table Of Contents:

General info
Show Warnings, Do Not Compile Unused Code, Initialize Variables On Declaration, Optimize Variables Declaration,

About variables
Dim, As, Boolean, Short, Integer, Long, Single, True, False, Const, ASM, IncludeASM,

Mathematical and logical operations
Mod, Sqr, Sin, Cos, Tan, Exp, Ln, Log, Not, And, Or, Xor, Nand, Nor, Nxor,

Standard Basic language elements
Goto, For, To, Step, Next, Exit For, While, Wend, If, Then, Else, Endif,

Memory access
Poke, Peek,

Subroutines
End, Gosub, Return,

Bit-oriented language elements
SetBit, ResetBit, TestBit, MakeBit,

Communication with I/O ports
Get, Put, Print, CrLf, Lf,

Structured language support (procedures and functions)
Proc, End Proc, Call, Exit, Function, End Function, Include,


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.

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 popup 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

Five data types are supported:
Boolean - 1-byte, True or False
Short - 1-byte integers in the range -128 to 127
Integer - 2-byte integers in the range -32,768 to 32,767
Long - 4-byte integers in the range -2,147,483,648 to 2,147,483,647
Single - 4-byte single precision floating point numbers, 7 digits precision, IEEE754 standard

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 64K 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

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.

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.

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: LD A,(BC)

Symbolic names of all variables and constants (global and local) can be used as arguments of assembler statements. The compiler will replace that symbolic name with the proper variable address or constant value:
   Dim varname As Short
   varname = 0
   ASM:        LD A,55H
   ASM:        LD (VARNAME),A

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.

Mathematical and logical operations

Five arithmetic operations (+, -, *, /, MOD) are available for 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. Arithmetic operations are allowed only in assignment statements and all variables in one such statement must be of the same data type. 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)

There are seven single precision mathematical functions (SQR, SIN, COS, TAN, EXP, LN, LOG) that can be used with Single data type variables. All math functions can also be used in complex math expressions. For example:
   Dim a As Single
   a = 2
   a = Sqr(a)

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 Integer data type. 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 a As Integer
   Dim b As Integer
   For a = 0 To 10000
      If a < 1000 Then
         b = a
      Else
         b = 1000
      Endif
   Next a

For statement will accept all available variable 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. They can be used with integer constants and also with variables of Short, Integer or Long data type. For example:
   Dim a As Integer
   Dim b As Integer
   Dim c As Integer
   For a = 0 To 15
      b = Peek(a)
      c = 240 + a
      Poke c, b
   Next a

Subroutines

Structured programs can be written using subroutine calls with GOSUB statement that uses line label name as argument. Return from a subroutine is performed by RETURN statement. User need to take care that the program structure is consistent. When using subroutines, main routine need to be ended with END statement. Here is an example:
   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 and RESETBIT statements can be used to set or reset the individual bits in Short data type variables. The first argument is a Short variable that will be the target of the operation, and the second argument is target bit number and it must be a constant in the range 0-7.
   Dim a As Short
   a = 0xf0
   SetBit a, 0
   ResetBit a, 7

By using TESTBIT and MAKEBIT functions it is possible to assign a Boolean data type variable the value contained in the specific bit of a Short data type variable, and vice versa, to copy the value of a Boolean data type variable to the specific bit of a Short data type variable. The first argument of these functions is target bit number and it must be a constant in the range 0-7. For example:
   Dim a As Boolean
   Dim b As Short
   a = TestBit(0, b)
   b = MakeBit(7, a)

Communication with I/O ports

The communication with the outside world is done using GET function and PUT and PRINT statements. The argument of the GET function is port number and must be a constant value in the range [0-255]. It can be used to assign the value received on the port to a variable of Short, Integer or Long data type. For example:
   Dim a As Integer
   a = Get(10)

PUT statement can be used to send data to the specified port. The data can be a constant value in the range [0-255] or contained in a variable of Short, Integer or Long data type. Only the lowest byte of the variable is sent to the port. For example:
   Dim a As Integer
   a = 200
   Put 10, a

PRINT statement can be used in three different ways. It is possible to use it to send a constant string , to send a variable of any supported data type or to send the LF (Line Feed) character or CRLF (Carriage Return - Line Feed) sequence to the specified port. Here is an example:
   Dim a As Single
   a = 123.456
   Print 10, "THE NUMBER IS "
   Print 10, a
   Print 10, CrLf

This can also be done using only one PRINT statement:
   Print 10, "THE NUMBER IS ", a, 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. The procedures can be exited with EXIT statement. They must be ended with END PROC statement and must be placed after the END statement in program. Calls to procedures are implemented with CALL statement. The list of passed arguments can contain both variables and numeric constants. 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 10, "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)
   Next x
   End

   Function square(arg1 As Integer) As Integer
   square = arg1 * arg1
   End Function

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