The BASIC building blocks


February 10, 2008

After a one month hiatus i picked up the project again.

I expanded the math routines to include parentheses in its calculation. I have not yet set a limit to the depth, but the code only stores 4 bytes for each step and since any line of BASIC code can only be 32 bytes long, there's already a hard-limit of 15 steps (simply because no more open/close brackets can fit in a single line). I'm hoping this limit will be enough. Currently it is, given the memory layout i'm using now leaves 368 bytes for stack.

I've also added a simple IF-THEN statement, working on a boolean condition (zero or non-zero word). I have to still implement the relational operators.

The entire flash used at this point is roughly 4kB (out of 32kB that the Mega32 has). This includes the CRT module (.5kB), the Kernel (1kB), a handful of BASIC commands (1.5kB) and a character set (1kB). Future plans include an RS232 interface module to be added to the Kernel and the ability to handle external assembly (perhaps a 6502 emulator). I still have some 28kB to fill up, so there's bound to be a lot more BASIC instructions i can implement.




February 13, 2008

Some bugs in the current code were fixed and i added the logical comparison functions. Here's the list of all mathematical and logical operations that the BASIC currently can perform.
The console uses an internal working 16-bit accumulator for calculations. The result of any operation is put into the ACCU after its completion and can be used for the next operation. In this list the word ACCU refers to this working accumulator.

=(implicit) assignmentAssigns the result of an expression to a variable. Also, the first number the Console BASIC encounters is implicitly assigned to the ACCU
+addAdds the next number to the ACCU
-subtractSubtracts the next number from the ACCU
*multiplyMultiplies the ACCU with a 16-bit number
/divideDivides the ACCU by a 16-bit number
%moduloSets the ACCU to the modulo or remainder of the division of the current value in the ACCU divided by the next number
&andPerforms a logical AND operation on the ACCU and the next number
|orPerforms a logical OR operation on the ACCU and the next number
^exclusive orPerforms a logical Exclusive OR operation on the ACCU and the next number
!notNegates the ACCU: resulting in every binary 0 to become a 1 and a 1 to become a 0. Basicly, the operation subtracts the ACCU from $FFFF (unsigned)
()parenthesesAnything between (nested) parantheses is calculated first, before continuing outside the parentheses

The representation of FALSE is $0000 (decimal 0) while TRUE is $FFFF (NOT FALSE or decimal -1).

=equalCompares the ACCU with the next number. If they are equal it sets the ACCU to TRUE and FALSE otherwise
<less thanCompares the ACCU with the next number. If the ACCU is less than the next number it sets the ACCU to TRUE and FALSE otherwise
>greater thanCompares the ACCU with the next number. If the ACCU is greater than the next number it sets the ACCU to TRUE and FALSE otherwise
<=less than or eualCompares the ACCU with the next number. If the ACCU is less than or equal to the next number it sets the ACCU to TRUE and FALSE otherwise
>=greater than or equalCompares the ACCU with the next number. If the ACCU is greater than or equal to the next number it sets the ACCU to TRUE and FALSE otherwise
<>not equalCompares the ACCU with the next number. If they are not equal it sets the ACCU to TRUE and FALSE otherwise

Several error messages have been implemented to inform the user of any problems with the BASIC instructions:

?Syntax errorA general error indicating there is a flaw in the instruction syntax. Possible reasons include misspelled keywords and illegal operator combinations like ++.
CAUTION: 10--3 is allowed and will subtract -3 from 10, with 13 as its result.
?Overflow errorThe overflow error is generator whenever a number exceeds the MAXINT value related to the 16-bit numbers. These are: -32768 and 32767. The error can be generated if a calculation result is too large or when a literal number is too large. For example: PRINT 40000 will cause an overflow error.
?Divide by zeroIt is mathematically not allowed to divide by zero. This error can be generated by the operators / (divide) as well as % (modulo).
?BreakNot an error as such: the user has stopped program execution by pressing the ESC key.
?Missing parenthesesThis error occurs whenever there is a parentheses missing. This is the case when mathematical parantheses don't match or when a compulsory bracket after a function-keyword is missing, for example: PEEK 0 without brackets is not allowed.





February 25, 2008

After some speedtesting today, i determined the average speed of the console:
It can run at 4.7MHz (Native Atmel Flash assembler code). The AVR consumes roughly 70% of its time with background processes (mostly PAL generation) leaving the remaining 30%, or 4.7MHz for BASIC decoding & execution.
When you take in consideration that most AVR instructions take just 1 or 2 cycles, while 6502 instructions take AT LEAST 2 cycles and 3 or 4 cycles for non-immediate addressing modes, this Console can execute its firmware more than 8 times faster than an original Commodore 64!

The second thing i've been working on is sound generation. I thought it be a nice touch to be able to emulate C64-like sounds or even music. After trying to fit the PWM routines into the tiny timeslot i still had left during horizontal retrace (just a couple microseconds) i was able to create a 1 voiced sawtooth waveform without compromising the overall speed. There is still enough time left for waveform selection (sawtooth, triangle or pulse) but only for 1 voice. If this is an acceptable option, i can implement sounds into the main AVR unit, without having to add extra hardware (not counting amplifier).
But.... i decided it was not acceptable (i may leave it in as an option, but no onchip SID emulation). This way it would be possible to use simple sounds even without adding an extra controller.

So, i will have to revisit the sounds topic:
The new objective will be to implement a 6581 SID Emulator into perhaps a Tiny2313 and move the keyboard controller into the same device to have one microcontroller for I/O operations. Since i will not be using all 15 ports for those 2 functions, i can use the remaining ports for general parallel user I/O, partly emulating a 6526 CIA controller.




Sounds & Music | Console 32 - BASIC