Tiny Basic Compiler
Moderator: Moderators
Tiny Basic Compiler
I've spent the last few weeks on a Tiny Basic Compiler for the 6502. The word tiny refers more to the code created than the compiler or language itself.
It's designed to target multiple assemblers and multiple machines through the use of configuration and library files.
You can find the current version at https://docs.google.com/leaf?id=0BxwERm ... Y2Zl&hl=en
The documentation is still a bit thin, but I plan on improving it if there is enough interest.
It's designed to target multiple assemblers and multiple machines through the use of configuration and library files.
You can find the current version at https://docs.google.com/leaf?id=0BxwERm ... Y2Zl&hl=en
The documentation is still a bit thin, but I plan on improving it if there is enough interest.
Last edited by CurtisP on Thu Feb 17, 2011 11:54 pm, edited 1 time in total.
So it compiles standard VIC 20 BASIC to tiny 6502 assembler programs? As opposed to being a compiler for Tiny BASIC?
Bacon
-------------------------------------------------------
Das rubbernecken Sichtseeren keepen das cotton-pickenen Hands in die Pockets muss; relaxen und watschen die Blinkenlichten.
-------------------------------------------------------
Das rubbernecken Sichtseeren keepen das cotton-pickenen Hands in die Pockets muss; relaxen und watschen die Blinkenlichten.
It actually has more in common with Tiny Basic than with Microsoft Basic.
The syntax of the Basic language is designed around the 6502 so that the source code will compile into small and fast machine language.
Some examples:
The only native data type is Byte (0-256) and it does not directly support multiplication or division (the would be done through function calls).
Function calls always pass parameters through the A, Y, and Z registers
The overall philosophy is to give you machine language performance and size, but with higher level language constructs to make programming easier and less error-prone.
The syntax of the Basic language is designed around the 6502 so that the source code will compile into small and fast machine language.
Some examples:
The only native data type is Byte (0-256) and it does not directly support multiplication or division (the would be done through function calls).
Function calls always pass parameters through the A, Y, and Z registers
The overall philosophy is to give you machine language performance and size, but with higher level language constructs to make programming easier and less error-prone.
Here is an example program. It makes use of the original MINIGRAFIK for the unexpanded Vic.
The final assembled binary file came out to 351 bytes (192 bytes for the library code and 159 bytes for the compiled Basic code).
This makes use of some new library and macro files I created tonight. I plan to upload a new version that includes these files in the next few weeks.
The final assembled binary file came out to 351 bytes (192 bytes for the library code and 159 bytes for the compiled Basic code).
Code: Select all
REM Graphics Test/Demo for TCB02
REM To Compile for VIC20 and ccA65 compiler, use
REM TCB02 /VCBA /LVIC3K /ECBA GRAPHICS
REM CCA65 GRAPHICS
REM Standard Library and Macros
IMPORT "LIB"
INCLUDE "MACROS"
REM Graphics Library and Macros
IMPORT "GFX"
INCLUDE "MACROGFX"
REM All Variables must be declares
DIM C,I,X1,Y1,X2,Y2
REM Turn on Graphics
GRAPHICS
REM Clear Graphics Screen
CLEAR
REM Set Colors
COLORS #WHITE, #BLACK, #BLUE
REM Draw the Boxes
FOR I = 1 TO 64
LET X1 = I
LET Y1 = I
LET X2 = 127 - I
LET Y2 = 127 - I
BOX X1, Y1, X2, Y2
LET I = I + 1
NEXT
REM Wait For Keypress
INP C
REM Reset Computer
STOP
- Mike
- Herr VC
- Posts: 5134
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
CurtisP wrote: Here is an example program. It makes use of the original MINIGRAFIK for the unexpanded Vic.

The later reincarnations would supply you with a bigger resolution, multi-colour support and a built-in line drawing routine for free, besides other things.
Even though, I still rather tend to use BASIC as interface between the routines in MINIGRAFIK and application specific ML support routines.
The later incarnations also require Memory Expansion. And the original MinGrafik has line drawing built in.Mike wrote: The later reincarnations would supply you with a bigger resolution, multi-colour support and a built-in line drawing routine for free, besides other things.
But TCB02 is modular, so if you wanted to use a different graphics package, you would just use a different library file.
With TCB02 you can write your glue logic in Basic and have it compiled into fast and small machine language.Even though, I still rather tend to use BASIC as interface between the routines in MINIGRAFIK and application specific ML support routines.
- Mike
- Herr VC
- Posts: 5134
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
This is necessary because the screen bitmap already requires the full address range of $1000 to $1FFF. Where else am I supposed to put the program, if I don't want to clobber the lower 1K?CurtisP wrote:The later incarnations also require Memory Expansion.
Sorry, but you're wrong in that regard. Here's the original version of MINIGRAFIK, which was written by W. Wirth in 1985:And the original MinGrafik has line drawing built in.
http://www.sleepingelephant.com/ipw-web ... 2&start=62
I did a clean room reconstruction of MG in 2006, labeled V2, and enhanced to 160x192 pixels. Neither this version, nor the original MG do contain a line routine. That one just was introduced with V3.
Sure.But TCB02 is modular, so if you wanted to use a different graphics package, you would just use a different library file.
Since you already put the focus on an unexpanded VIC: for me, TCB gives rather a too thin abstraction layer over machine language. For smaller programs one can easily dispense with that layer. Support for 16-bit or even larger numbers, and string operations would be a big plus.With TCB02 you can write your glue logic in Basic and have it compiled into fast and small machine language.
However, then you'd probably end up with something broadly similar to Quetzalcoatl (C/UPL) or cc65.

Alright, it turns out I'm not using the original MINIGRAFIK, but a later reconstructed version, specifically the one with a 127 x 127 bitmap and line drawing routines.
I used it because it's simple and uses memory locations for parameter passing, which makes it easy to wrap functions around.
For example, the function call
is translated to
so the code for the function call itself, simply stores A, X, and Y in the appropriate memory locations, then jumps to the proper code.
I appreciate the existence of MINIGRAFIK and the work you put into it, because it made it very easy to do a proof of concept.
I used it because it's simple and uses memory locations for parameter passing, which makes it easy to wrap functions around.
For example, the function call
Code: Select all
FOO(BING, BANG, BOOM)
Code: Select all
LDA BING
LDY BANG
LDX BOOM
JSR FOO
I appreciate the existence of MINIGRAFIK and the work you put into it, because it made it very easy to do a proof of concept.
Here's another example program. This one using strings
The DIM/DATA statement at the end is a bit wonky because its a work-around to get a 256 character string, but all in all I think it closely resembles standard BASIC.
The DIM/DATA statement at the end is a bit wonky because its a work-around to get a 256 character string, but all in all I think it closely resembles standard BASIC.
Code: Select all
REM Hangman Game for TCB02
REM (C) 2011 Curtis F Kaylor
IMPORT "LIB"
INCLUDE "MACROS"
DIM C, I, J, Found
DIM Word[8]
DIM Right, Wrong, Pick[15]
LABEL Start
LOCATE 0,10
PRINT "PRESS ANY KEY TO BEGIN"
REM Set Random Seed Value
LET I = 0
REPEAT
INCR I
GET C
UNTIL C <> 0
RANDOM I
REM Draw Screen
CLS
PUT #CDNRT
FOR I = 1 TO 5
PUT #HLINE
NEXT I
PUT #CDNLT
LOCATE 6,1
PUT ':'
FOR I = 1 TO 6
LOCATE 0, I
PUT #VLINE
NEXT I
LOCATE 0,7
PRINT " WORD ********"
PRINT " MISS"
LOCATE 0,10
PRINT "PRESS A LETTER "
REM Pick Word
LET Word = 8
LET I = RND() & $F8
FOR J = 1 TO 8
LET Word[J] = WordList[I]
INCR I
NEXT
REM Initialize Variables
LET Pick = 0
LET Right = 0
LET Wrong = 0
LABEL Main
REM Input Character and Convert to Uppercase
INP C
IF C>='a' AND C<='z' THEN LET C=C-32
REM Check For Break
IF C = 3 THEN STOP
REM Make sure it's a Letter
IF C<'A' OR C>'Z' THEN GOTO Main
REM See if Letter Was Already Picked
FOR I = 1 TO Pick
IF Pick[I] = C THEN GOTO Main
NEXT
REM See if Letter is in Word
LET Found = 0
FOR I = 1 to Word
IF Word[I] = C THEN
LET Right = Right + 1
LOCATE I+5,7
PUT C
GOSUB Repos
LET Found = 1
ENDIF
NEXT
REM Letter Was Not In Word
IF Found = 0 THEN
LET Wrong = Wrong + 1
LOCATE Wrong+5,8
PUT C
GOSUB BodyPart
GOSUB Repos
ENDIF
REM Add to List of Picked Letters
INCR Pick
LET Pick[Pick] = C
IF Right > 7 THEN
LOCATE 5,9
PRINT "YOU WIN!"
GOTO Start
ENDIF
IF Wrong > 6 THEN
LOCATE 5,9
PRINT "YOU LOSE!"
GOTO Start
ENDIF
GOTO Main
LABEL BodyPart
REM Head
IF Wrong = 1 THEN
LOCATE 6,2
PUT 'O'
ENDIF
REM Left Arm
IF Wrong = 2 THEN
LOCATE 5,3
PUT #DLLUR
ENDIF
REM Right Arm
IF Wrong = 3 THEN
LOCATE 7,3
PUT #DULLR
ENDIF
REM Torso
IF Wrong = 4 THEN
LOCATE 6,3
PUT #VLINE
ENDIF
REM Abdomen
IF Wrong = 5 THEN
LOCATE 6,4
PUT #VLINE
ENDIF
REM Left Leg
IF Wrong = 6 THEN
LOCATE 5,5
PUT #DLLUR
ENDIF
REM Right Leg
IF Wrong = 7 THEN
LOCATE 7,5
PUT #DULLR
ENDIF
RETURN
LABEL Repos
LOCATE 15,10
RETURN
REM Equivalent to the array WordList[255]
DIM WordList = 'E'
DATA "MPHATIC", "CALZONES", "DINOSAUR", "DUMPSTER"
DATA "DYSTOPIA". "BESTIARY", "FATIGUED", "GERMANIC"
DATA "HARMONIC", "IGNORANT", "JEALOUSY", "KILOBYTE"
DATA "LAUGHTER", "MAGNETIC", "NOTARIZE", "ORDINARY"
DATA "PERILOUS", "QUACKERY", "REACTION", "SCROUNGE"
DATA "TAPESTRY", "VIRULENT", "BACTERIA", "AQUIFERS"
DATA "BIFOCALS", "ABNORMAL", "CAMISOLE", "DOLPHINS"
DATA "BRITCHES", "ARGUMENT", "BACHELOR", "ABDICATE"
- Mike
- Herr VC
- Posts: 5134
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
I gladly take that compliment for all the programs that others and I have written with MINIGRAFIK. In your current case, however ...CurtisP wrote:I appreciate the existence of MINIGRAFIK and the work you put into it, because it made it very easy to do a proof of concept.
... I need to say you are not using MINIGRAFIK, but some other graphics library.Alright, it turns out I'm not using the original MINIGRAFIK, but a later reconstructed version, specifically the one with a 127 x 127 bitmap and line drawing routines.
I used it because it's simple and uses memory locations for parameter passing, which makes it easy to wrap functions around.
All versions of MINIGRAFIK (the original by W. Wirth and all re-written versions by me) install themselves as extensions to CBM BASIC and add commands and functions prefaced with the '@'-sign. There is no 'POKE and SYS' interface involved (though you could call most of the routines that way if you wanted).
Presumably it is the hires toolkit written by Thomas Magnusson you are using. That one was featured in the OP of the thread 'Hires Graphics' (download) and it includes a line routine and an interface that fits your description, but it is not related to MINIGRAFIK.
-
- Omega Star Commander
- Posts: 1375
- Joined: Thu Jan 31, 2008 2:12 pm
- Website: https://robert.hurst-ri.us
- Location: Providence, RI
- Occupation: Tech & Innovation
Jeff? Jeff? I hear this calling you?darkatx wrote:... but wouldn't this be perfect for hybrid programs like Basic using some ML routines for speed?

I demand you write something you cannot simply LIST.
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
https://robert.hurst-ri.us/rob/retrocomputing
Yes, that is the one I am using. Sorry for the confusion.Mike wrote:Presumably it is the hires toolkit written by Thomas Magnusson you are using. That one was featured in the OP of the thread 'Hires Graphics' (download) and it includes a line routine and an interface that fits your description, but it is not related to MINIGRAFIK.