Compiler Control

The compiler can be controlled, in terms of the default startup library routines. This may be required to implement a specific control function, or to disable a default startup behaviour.

Scenario #1:

You have a new LCD. The GCBASIC LCD routines fail to initialise. You want to write your own LCD initialise routine, but you want to ensure the GCBASIC standard INITLCD() does not operate before your own LCD initialise routine. How do you do this?

Scenario #2:

You want to write your own INITSYS routine. You can add your own routine to initialise the microcontroller, but the default INITSYS would always be called in the ASM.

In the first scenario, the approach would be to redirect the GCBASIC standard INITLCD() to myInitLCD using #DEFINE INITLCD myINITLCD. However, prior to the latest build, this would fail to work. The reason for the failure to redirect to your new routine is the #STARTUP INITLCD directive. The #STARTUP directive was essentially hard coded and none of the `#STARTUP`s could be changed.

In the second scenario the ASM call to INITSYS is also hard coded. You could trick the compiler into calling your own initialisation routine, but this was not easy and not intuitive.

The new build now supports the updating of the `#STARTUP`s with your own routines, or even cancelling `#STARTUP`s.

Examples:

The compiler will search for all `#STARTUP`s and update across all sources (libraries and includes). LCD.H is just an example.

#DEFINE INITLCD myINITLCD // This will change any reference in the LCD.h #startup INITLCD to #startup myINITLCD.

#DEFINE INITLCD // With no second parameter, this would cancel any #startup in LCD.h.

#DEFINE INITSYS myINITSYS // This will change the default INITSYS to myINITSYS.

#DEFINE INITSYS // This will remove the INITSYS from the initialisation of the microcontroller.

Example to change LCD initialisation

    #DEFINE INITLCD myINITLCD          ' <<< the #DEFINE instruction redirecting a #startup routine

    Sub myInitLCD
        // do stuff
    End Sub

Key line: #DEFINE INITLCD myINITLCD — redirects every library #STARTUP INITLCD call to myInitLCD instead, so a user-written LCD initialiser fully replaces the standard one rather than running alongside it.

Example to replace INITSYS

    #DEFINE INITSYS                      // Cancel call
    #STARTUP myInitSYS, 1               // New init routines, and set as highest priority

    Sub myInitSYS
        // do stuff
    End Sub

Scripts can also change the #STARTUP. You can add a script to change the behaviour depending on a specific condition (the existence of another constant).

In the user program:

    #DEFINE LCD_OCULAR_OM1614

Supported within LCD.H:

    #SCRIPT
        If Def( LCD_OCULAR_OM1614 ) Then
            'Change INITLCD to specific Initialisation sub
            INITLCD = INIT_OCULAR_OM1614_LCD
        End if
    #ENDSCRIPT

    ......

    Sub INIT_OCULAR_OM1614_LCD
        .. lots of code
    End Sub

will generate ASM like this:

    ;Program_memory_page: 0
        ORG	5
    BASPROGRAMSTART
    ;Call initialisation routines
        call	INITSYS
        call	INIT_OCULAR_OM1614_LCD

    ;Start_of_the_main_program

This new capability gives you more control of the compiler.

See Also:

  • #DEFINE — redefining constants and startup routine names, as used above
  • #startup — registering a subroutine to run at initialisation
  • #script — compile-time scripting used above to switch the initialisation routine