102 of 172
ZBasic Language Reference 95 ZX Microcontroller Family 4.2 Defining and Using External Subroutines, Functions and Variables For the native mode devices you may include .c files (C language), .S files (AVR assembly language), .o files (AVR object code) and .a files (AVR object code archives) in your project. When processing the component files of a project, the ZBasic compiler will note the presence of these special files but will otherwise ignore them except for including them in the final build phase. In order to invoke subroutines and functions contained in these special files, you must define them so the ZBasic compiler knows how to check for invocation syntax errors and how to generate code to invoke them. The syntax for defining external routines is very similar to defining a ZBasic subroutine or function as shown below. Declare Sub <name> ( [<parameter-list>] ) Declare Function <name> ( [<parameter-list>] ) as <type> Parameters of the external routines may be given a default value as described in Section 3.20. In cases where the external routine name is a ZBasic keyword or is not a valid ZBasic identifier, you can use the Alias clause to specify the actual name of the external routine as a quoted string while using the specified <name> to refer to it in ZBasic code. Declare Sub <name> ( [<parameter-list>] ) Alias "<ext-name>" Declare Function <name> ( [<parameter-list>] ) as <type> Alias "<ext-name>" Note that, with the exception of the String type, ZBasic data types are generally compatible with their similar counterparts in C and assembly language. To pass a string to an external routine, it is recommended that the string’s characters be copied to a Byte array and then pass the array by reference to the external routine. Depending on the requirements of the external routine, the length of the string must also be passed or the string’s characters must be null-terminated in the Byte array. Example Declare Sub extSub(ByVal ival as Integer) Call extSub(5) The Declare keyword may be preceded by Public or Private. If neither is present, the declaration defaults to being public meaning that it will be visible in other modules. The primary use for declaring a private external routine is when you define a routine using inline C or assembly code in the same module. Similarly, external variables may be declared so that they may be accessed in ZBasic code. Declare <name> As <type> [ Alias "<ext-name>" ] Likewise, for an external array: Declare <name>(<dim-spec-list>) As <type> [ Alias "<ext-name>" ] Making these declarations does not result in any variable space being allocated. It is up to you to provide additional code to make the external variables available. As with subroutine and function declarations, variable declarations may be Public or Private. In the absence of either attribute, a data declaration defaults to being private. For external variables that are to be treated as constant, you append the Constant attribute to the declaration. With this attribute, the compiler will generate an error if you attempt to modify the variable or to pass it to a subroutine or function by reference. Note, also, that the external variable should actually be defined with the const attribute if it is defined in C code.