HALFBYTE Console Tiny BASIC update

XANDER - WIN_20140222_110454 (2)The HalfByte Console is nearly complete. The hardware design is pretty much done and a prototype built. It is a fairly simple ATMega328 based computer with audio and video out, Wii Nunchuck interface (the Wii Classic Controller also works), PS/2 Keyboard support and will run off of battery, wall wart or can be powered via FTDI.  It is pretty small, about the size of an Arduino UNO. It is designed to be easy to use and build. (The console featured in the photo above is the first prototype and is not the final design. The final prototype is not presentable here, this one looks nicer.)

One of the goals of the console is to aid in learning to program.  For now, this can be done in two ways: standard Arduino IDE or on console via Tiny Basic.

Yes, Tiny Basic. 

I have adapted the wonderful little version of Tiny Basic from Mike Field.  I did this for the XGamestation and have done it for the HalfByte Console as well. This version of the language supports graphics, low level hardware access and the Nunchuck. It is standard Tiny Basic in every other aspect: integer variables, single letter variables, line numbers, GOTO, small memory footprint and is easy to learn. It makes the perfect introduction to programming.

Because the 328 is limited, RAM wise AND the video takes up most of that RAM, we are left with only 726 bytes of memory to use for programming. The 52 bytes for variable storage and stack already have RAM dedicated, so you do not need to factor that in.  As such, the big feature I wanted to include, SD Card support, could not be done.  To implement FAT16, you need a 512 byte buffer and we simply do not have that, so, for now, EEPROM is used to store a program. Again, memory issues only allow for ONE saved program.  Since RAM is low anyway, we can fit it in the 1k EEPROM space we have.  And, remember, this is more of a learning tool than a ‘serious’ development platform.  The console can, of course, be programmed for games and other applications outside of Tiny Basic and there are a lot of really clever games that utilize the TV OUT library.

So, what can we do with Tiny Basic? Well, even with these limitations, a lot.  Throw some motors on the console and control them with Tiny Basic.  Add some LED’s and make them blink.  Create small, clever games.  Your imagination is the limit.

I won’t discuss the basics of BASIC, but would like to touch on the hardware specific enhancements, like graphics and controller support.

GRAPHICS

There are five graphics statements and one function.  The five statements are:

  • SET x,y
  • RESET x,y
  • LINE startx,starty,endx,endy,on or off
  • BOX startx,starty,endx,endy,on or off
  • CIRCLE startx,starty,radius,on or off

The function is:

  • <var>=GET(x,y)

GET will return the setting of a pixel at x,y. If the pixel is lit, it returns a TRUE (1) otherwise a FALSE (zero).

For LINE, BOX and CIRCLE, ‘on or off’ means a one for on and a zero for off.  If you BOX a section of the screen with text or other graphics, you can set ‘on or off’ as a 2 and it will invert that section of the screen. This is a good way to highlight text.

HARDWARE ACCESS

There are two statements that can set pins on the 328:

  • AWRITE <pin>,on or off or value (sets the ANALOG pin)
  • DWRITE <pin>,on or off or value (sets the DIGITAL pin)

There are three functions to read values from the hardware:

  • <var>=AREAD(<pin>)
  • <var>=DREAD(<pin>)
  • <var>=IN( 0 )

AWRITE and DWRITE will send a value to the specified pin. A zero sets the pin LOW, a one sets it HIGH, any other value is simply sent to the pin. AREAD and DREAD will return a value from the specified pin. NOTE: the ‘A’ in AREAD and AWRITE specify the ANALOG pins, while the ‘D’ means DIGITAL pin.

IN( 0 ) returns the current value from the RX pin (D0) on the 328p chip. This could be useful to monitor the serial interface for incoming data.

SAMPLE CODE

Since adding support for graphics and the Nunchuck, simple games like Space Invaders or simple shoot them ups are fairly easy.  Nunchuck support is complete, too…you can not only read the buttons and the thumbstick, but also the accelerometer. While the Classic Controller will work, no support for it is included. Maybe a later version.

The first sample I want to show is Kaliedoscope:

100 CLS
110 X=RND(40)
120 Y=RND(20)
130 P=RND(40)
140 Q=RND(20)
150 SET X,Y
160 SET 80-X,Y
170 SET 80-X,40-Y
180 SET X,40-Y
200 RESET P,Q
210 RESET 80-P,40-Q
220 RESET 80-P,Q
230 RESET P,40-Q
240 IF INKEY(0)=27 STOP
250 IF RND(100)<95 GOTO 290
260 BOX 0,0,79,39,0
270 FOR I=1 TO 500
280 NEXT I
290 GOTO 110

This little gem will display random pixels that are mirrored in four quadrants on the screen.  Randomly, the screen will invert. It can be quite mesmerizing.

Here’s a Nunchuck example:

100 X=1: Y=1 #INITIALIZE OUR START
110 SET(X,Y)
120 P=PAD(0) #GET THE X VALUE
130 Q=PAD(1) #GET THE Y VALUE
140 IF P>200 X=X+1 #IF VALUE IS MORE THAN 200, GO RIGHT
150 IF P<100 X=X-1 #IF VALUE IS LESS THAN 100, GO LEFT
160 IF Q>200 Y=Y+1
170 IF Q<100 Y=Y-1
180 SET(X,Y) #LIGHT UP THE PIXEL
190 IF PAD(3) STOP #IF Z BUTTON IS PRESSED, STOP
200 IF PAD(2) RUN #IF C BUTTON IS PRESSED, RESTART
210 GOTO 120 #DO IT AGAIN

This bit of code is akin to an etch-a-sketch. PAD is the function which reads the controller. The parameter passed tells Tiny Basic which part of the controller to return a value: the x and y axis for the stick, the C and Z buttons and the accelerometer.  the modes are:

PAD number

Return value

0

Thumbstick x

1

Thumbstick y

2

‘C’ button

3

‘Z’button

4

Accelerometer x

5

Accelerometer y

6

Accelerometer z

BLINKY

This example will blink the PIN 13 LED:

100 FOR K=1 TO 50
110 DWRITE 13,1 #TURN ON LED ON PIN 13
120 FOR I=1 TO 500 #DELAY
130 NEXT I
140 DWRITE 13,0 #TURN OFF THE LED
150 FOR I=1 TO 500
160 NEXT I
170 NEXT K

Looking at the code, you see that line 110 turns on the LED and 140 turns it off.  120 and 150 create a delay so you can actually see the LED turn on and off.  We could add a line between 120 and 140 to test for the status of pin 13:

115 IF DREAD(13)=1 PRINT “LED IS ON.”

165 IF DREAD(13)=0 PRINT”LED IS OFF.”

Now, these lines are rather pointless in the example, because we know they SHOULD be set accordingly, you can see the utility of them. 

As you can see, there is a nice foundation here for something that can be quite useful in not only teaching a child how to program (or, anyone) but it is also useful for scripting hardware functions as well.  This version of Tiny Basic should run, as is, on an Arduino Mega2560. On that board, you will have more RAM to work with and create some really nice, useful or fun code.  You could also modify the interpreter for a slightly high resolution.

I have prototyped using a second 328 strictly for video generation. This gave me an effective resolution of 128 x 96 graphics and 22 by 12 text.  However, communication between the two is serial and I did not fully implement graphics. Once the console is complete, I will return to this side project and fully develop it. Perhaps HB Console II will feature two 328 chips.

Developing this Tiny Basic has been fun and brought up many memories of when I was a wee lad, experimenting with this new thing called a home computer with my Dad.  I hope this can create similar memories for parents and children going forward and, along the way, spark an interest in these nifty little computers.

NOTE: I am currently stumped with the PIC16 version of TINY BASIC.  I have the interpreter working very nicely with low res color graphics, sound and hardware support. BUT…I’ve been stumped with a big bug in the SAVE-LOAD code.  We utilize the SD Card (FAT16) library and can read and write and get the directory listing. If you are familiar with the PIC and know C and would like to help, PLEASE drop me a note.  I can’t reward you with a financial reward, but you will have my eternal thanks and recognition both here and in the Tiny Basic code.

EDIT: since publishing this post, I have been able to squeeze 200 more bytes into RAM by moving the keyword scan tables into PROGRAM MEMORY. More optimization may be possible. Who knows, maybe I can get a whole 1k of RAM. That’s what the original ZX-81 had.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s