gtBASIC

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

You've found another bug in the parser, you have \t, (i.e. real tabs as whitespace), the parser expects spaces as all my editors expand \t to spaces.

I'll fix that for the next release, for now either replace your tabs with spaces or set your editor to do it automatically.
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

If you'd like to be able to use tabs in the current compiler, then you can add the following two patches to expression.cpp

Patch 1: change 1047-1053 from:

Code: Select all

            while(*str  &&  (*str != c  ||  (numQuotes & 1)  ||  numBrackets))
            {
                if(*str == '"'  &&  (str == begin  ||  (str > begin  &&  *(str-1) != '\\'))) numQuotes++;
                if(*str == '(') numBrackets++;
                if(*str == ')') numBrackets--;
                str++;
            }
To:

Code: Select all

            while(*str)
            {
                bool foundChar = (c == ' ') ? isSpace(*str) : (*str == c);
                if(!foundChar  ||  (numQuotes & 1)  ||  numBrackets)
                {
                    if(*str == '"'  &&  (str == begin  ||  (str > begin  &&  *(str-1) != '\\'))) numQuotes++;
                    if(*str == '(') numBrackets++;
                    if(*str == ')') numBrackets--;
                    str++;
                }
                else
                {
                    break;
                }
            }
Patch 2: change 1078 - 1082 from:

Code: Select all

            while(*str  &&  (*str != c  ||  (numQuotes & 1)))
            {
                if(*str == '"'  &&  (str == begin  ||  (str > begin  &&  *(str-1) != '\\'))) numQuotes++;
                str++;
            }
To:

Code: Select all

            while(*str)
            {
                bool foundChar = (c == ' ') ? isSpace(*str) : (*str == c);
                if(!foundChar  ||  (numQuotes & 1))
                {
                    if(*str == '"'  &&  (str == begin  ||  (str > begin  &&  *(str-1) != '\\'))) numQuotes++;
                    str++;
                }
                else
                {
                    break;
                }
            }
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

Optimiser patch for current release of gtBASIC/gtemuAT67 1.0.9R, increases speed of the optimiser by 10x to 20x, depending on the project.

Edit optimiser.cpp and change the following at lines 1084 - 1093 from: (if you're using the optimiser from page 12 of this thread, then lines are 1118 to 1127)

Code: Select all

                        // Optimising can cause new optimising opportunities to present, so restart
                        goto RESTART_OPTIMISE;
                    }
                }
            }
        }

        return true;
    }
}
to: (the indentation will look a little wonky, just ignore it, it will compile fine)

Code: Select all

                           optimised = true;
                        }
                    }
                }
            }

            numOptimiserPasses++;
        }
        // Successful optimisation can cause new optimising opportunities to present themselves, so restart
        while(optimised);

        fprintf(stderr, "\n*******************************************************\n");
        fprintf(stderr, "* Optimiser performed %d passes\n", numOptimiserPasses);
        fprintf(stderr, "*******************************************************\n");

        return true;
    }
}
And lines 386 - 387 from:

Code: Select all


RESTART_OPTIMISE:
to:

Code: Select all

        bool optimised;
        int numOptimiserPasses = 0;

        do
        {
            optimised = false;
wbushby
Posts: 208
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

I have my Engine Sound playing OK but it is using too many zero page variables. I have tried to create an array to hold them but the sound routine stops playing the sound and starts just clicking.

This code works but if I replace all the address references in the ASM with the array references it doesn't.

Can the addresses be constants ?

Code: Select all

ENGINESOUND:

	sndMute = Globals(MUTE)

'	DIM sndAddr(2,2) = {&h01FA, &h01FB, &h01FC,
'					&h02FA, &h02FB, &h02FC,
'					&h03FA, &h03FB, &h03FC}
	sndVolAddr1  = &h01FA                      ' channel 1, (out of 1 to 4)
	sndWaveAddr1 = &h01FB                      ' channel 1, (out of 1 to 4)
	sndFreqAddr1 = &h01FC                      ' channel 1, (out of 1 to 4)
	sndVolAddr2  = &h02FA                      ' channel 2, (out of 1 to 4)
	sndWaveAddr2 = &h02FB                      ' channel 2, (out of 1 to 4)
	sndFreqAddr2 = &h02FC                      ' channel 2, (out of 1 to 4)
	sndVolAddr3  = &h03FA                      ' channel 3, (out of 1 to 4)
	sndWaveAddr3 = &h03FB                      ' channel 3, (out of 1 to 4)
	sndFreqAddr3 = &h03FC                      ' channel 3, (out of 1 to 4)
    	sndTimer = &h2C

	sndVol  = 127 - Globals(VELOCITY)			' 127 to 64 -> 0 to 63 volume
	sndWave = 0                                 ' wave type, (noise=0, triangle=1, square=2, ramp=3)
	sndFreq = Globals(VELOCITY) + 20
    	sndTime = 255

    asm
		LD      _sndMute
		BNE     _EndSound

        LDW     _sndFreq                    ' format = high:8 low:07, (bit 8 of low byte = 0)
        DOKE    _sndFreqAddr1                ' turn on channel
	LD      _sndVol
        POKE    _sndVolAddr1
        LD      _sndWave
        POKE    _sndWaveAddr1

        LDW     _sndFreq
	LSLW    3
        DOKE    _sndFreqAddr2                
	LD      _sndVol
        POKE    _sndVolAddr2
        LD      _sndWave
        POKE    _sndWaveAddr2

        LDW     _sndFreq
	LSLW    1
	XORI     0x8F
        DOKE    _sndFreqAddr3                
	LD      _sndVol
        POKE    _sndVolAddr3
        LD      _sndWave
        POKE    _sndWaveAddr3

        LD      _sndTime

        POKE    _sndTimer

    endasm

EndSound:

RET
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

You'd have to show me the code, but generally you can't just replace zero page variable access with array accesses using the same ASM code, e.g.

Zero Page:

Code: Select all

        LDW     _sndFreq                 ' format = high:8 low:07, (bit 8 of low byte = 0)
        DOKE    _sndFreqAddr1
Non Zero Page, (e.g. DIM array of ints or DEF WORD):

Code: Select all

        LDWI    _sndFreqAddr_array + 2   ' = sndFreqAddr(1) : add _array when accessing gtBASIC arrays and *2 for words
        DEEK
        STW	_tmp
        LDW     _sndFreq
        DOKE    _tmp
wbushby
Posts: 208
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

Thank you for that information. I was able to work out my issue and move onto the next section of the code.
wbushby
Posts: 208
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

What are the file handling commands in GTBasic compiler? I want to save/load a game status.
I have found OPEN, READ but what is CLOSE for a file ?
Also I want to Load/Save from the SDCard. Are there any examples ?

One more thing. Can you dimension an array with a variable ? When I try it states ' array dimensions must be a positive constant, found 'CL'

Trying DIM CA(CL,7) where CL is a variable.
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

wbushby wrote: 25 Oct 2021, 01:58 What are the file handling commands in GTBasic compiler? I want to save/load a game status.
I have found OPEN, READ but what is CLOSE for a file ?
Also I want to Load/Save from the SDCard. Are there any examples ?
Those commands are just placeholders, they are not implemented yet, I'd love to be able to say they will be done soon, but I can't tell you when unfortunately.
wbushby wrote: 25 Oct 2021, 01:58 One more thing. Can you dimension an array with a variable ? When I try it states ' array dimensions must be a positive constant, found 'CL'
Trying DIM CA(CL,7) where CL is a variable.
All memory allocation is static under gtBASIC, otherwise the runtime would need a memory manager and that's just not worth the effort given what gtBASIC strives to do.

P.S. gtBASIC does have a memory manager, but it runs host side and thus statically manages and allocates all RAM resources whilst compiling.
wbushby
Posts: 208
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

Thank you for the reply.

I have another issue. I am writing a program with a lot of array based data but am running into Free RAM shortages.

*******************************************************
* Compiling file './pirateadventure64k.gbas'
*******************************************************
Memory::getFreeRAM() : No free RAM found of size 304 bytes
Keywords::allocDIM() : 'Main:257' : not enough RAM for int array of size 304 : DIM CA(CL,7) = 80.....

There should be about 40k available but it seems very fragmented if it can't allocate an array of 304 integers.
I know I can define a block of memory but I have more arrays to define for this program.

Is there a way to 'defrag' the memory blocks ?
at67
Site Admin
Posts: 647
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

wbushby wrote: 26 Oct 2021, 08:11 Memory::getFreeRAM() : No free RAM found of size 304 bytes
Keywords::allocDIM() : 'Main:257' : not enough RAM for int array of size 304 : DIM CA(CL,7) = 80.....

There should be about 40k available but it seems very fragmented if it can't allocate an array of 304 integers.
I know I can define a block of memory but I have more arrays to define for this program.

Is there a way to 'defrag' the memory blocks ?
I'm assuming CL is a const?

The bottom 32k of RAM has a four 250 byte segments, one 512 byte segment and 120x96 byte segments, you are asking for 304x2=608bytes, even though you are compiling for a 64K architecture, unless you specify:

Code: Select all

_arraysStart_ &hFFFF
Your code will try and allocate all arrays in the lower half by default. So add the above to your code and you will have the full 32K upper bank for arrays as well.

There is no concept of defragging in gtBASIC, because the memory allocations are all static, (i.e. all worked out at compile time), there is no memory that can be dynamically changed in size, or freed, or defragged.

P.S. You can force five of the six bottom 256 byte segments to be grouped together to give you approximately 1.25K of contiguous RAM in the lower bank, by disabling three of the 4 sound channels, by statically forcing the bottom two bits of zero page address &h0021 to 0. The problem with that is that you need to restore the correct ROM version in the top six bits as you write the bottom two bits, (at load time, hence static), and that gtBASIC doesn't yet understand that these 250byte segments can be made contiguous...one day.
Post Reply