Tiny Basic Compiler

You need an actual VIC.

Moderator: Moderators

CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Tiny Basic Compiler

Post by CurtisP »

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.
Last edited by CurtisP on Thu Feb 17, 2011 11:54 pm, edited 1 time in total.
User avatar
Kweepa
Vic 20 Scientist
Posts: 1314
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Post by Kweepa »

Sounds interesting!
Is it based on Microsoft Basic?
Bacon
for breakfast
Posts: 578
Joined: Mon Apr 19, 2004 8:07 am

Post by Bacon »

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.
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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.
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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).

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
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.
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

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.
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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.
The later incarnations also require Memory Expansion. And the original MinGrafik has line drawing built in.

But TCB02 is modular, so if you wanted to use a different graphics package, you would just use a different library file.
Even though, I still rather tend to use BASIC as interface between the routines in MINIGRAFIK and application specific ML support routines.
With TCB02 you can write your glue logic in Basic and have it compiled into fast and small machine language.
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

CurtisP wrote:The later incarnations also require Memory Expansion.
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?
And the original MinGrafik has line drawing built in.
Sorry, but you're wrong in that regard. Here's the original version of MINIGRAFIK, which was written by W. Wirth in 1985:

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.
But TCB02 is modular, so if you wanted to use a different graphics package, you would just use a different library file.
Sure.
With TCB02 you can write your glue logic in Basic and have it compiled into fast and small machine language.
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.

However, then you'd probably end up with something broadly similar to Quetzalcoatl (C/UPL) or cc65. ;)
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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

Code: Select all

FOO(BING, BANG, BOOM)
is translated to

Code: Select all

LDA BING
LDY BANG
LDX BOOM
JSR FOO
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.
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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.

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"
User avatar
darkatx
Vic 20 Afficionado
Posts: 471
Joined: Wed Feb 04, 2009 2:17 pm
Location: Canada

Post by darkatx »

Pardon my ignorance, but wouldn't this be perfect for hybrid programs like Basic using some ML routines for speed?

This looks just perfect for my needs!
Thanks for the effort put into this! :D
Learning all the time... :)
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

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 gladly take that compliment for all the programs that others and I have written with MINIGRAFIK. In your current case, however ...
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.
... I need to say you are not using MINIGRAFIK, but some other graphics library.

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.
rhurst
Omega Star Commander
Posts: 1371
Joined: Thu Jan 31, 2008 2:12 pm
Website: https://robert.hurst-ri.us
Location: Providence, RI
Occupation: Tech & Innovation

Post by rhurst »

darkatx wrote:... but wouldn't this be perfect for hybrid programs like Basic using some ML routines for speed?
Jeff? Jeff? I hear this calling you? :P

I demand you write something you cannot simply LIST.
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

Hahahaha. But that's my thing. I can't break character now.
High Scores, Links, and Jeff's Basic Games page.
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Post by CurtisP »

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.
Yes, that is the one I am using. Sorry for the confusion.
Post Reply