Write your own BASIC programs on your XGS PIC

Since rediscovering the XGS PIC Gamestation board, I’ve been doing some developing for the kit. One of the things I’d wanted to be able to do is actually do some coding ON the device. However, there are currently no development tools that actually RUN ON the device, so…I have adapted a dialect of Tiny Basic to run on the board.

IMG_2184This exercise, however, has reminded me of why I dislike the C programming language. In a nutshell, POINTERS. Oh my. These things are horrid.  The rest of the language is fine, even cool at times. But…those bloody pointers.

Rather than setting about writing the dialect from scratch, I searched the Internet for an open source variation and, boy, are there plenty.  Most, however, did not lend themselves to the kinds of changes I’d need to do to make it work. Since the XGS PIC has no operating system (whatever code that runs on it BECOMES the ‘OS’, providing all I/O.

Fortunately, Nurve Networks provided a few very well documented API’s and demos that I could use to supply all of the I/O I’d need.

The XGS PIC is a really funky, game oriented device and, as such, has very minimal specs.  VGA or Composite video out, really simple audio, SD card interface, Serial communications, two Atari style controller ports and PS/2 keyboard/mouse interface.  Talking to each is rather laborious to code, but the API set eases the pain a bit.

The graphical ability is somewhat between an Atari 7800 and the original NES in appearance.

Also, memory is tight: 16k RAM and 256k Flash. Well, 256k is more than enough for the Tiny Basic code and the API libraries, it’s the 16k RAM that makes it tight as this memory is shared between the video and your program.

So, after some searching and pouring through code, I found something for the Arduino. Called Nanode Basic, it was a modification of a Tiny Basic someone (Mike Field – hamster@snap.net.nz) crammed into an Arduino.  The Nanode modification added some features for real world interfacing to temperature sensors and a few other things. The architecture of the interpreter is smart and was easily adaptable to the XGS PIC. Mr. Field allowed for serial I/O to a terminal OR for use on an Arduino with keyboard and screen.  He left the I/O generic enough for me to, quite literally, plug in the calls to the keyboard and graphic driver API’s that Nurve supplied.

Now, the XGS PIC has two graphical modes: bitmap and tile.  IMG_2185Text rendering is done via the tile mode, which has, for now, precluded the inclusion of any graphical statement additions to the Basic (which I named ArdBasic, short for Arduino Basic.) 

The original Arduino Basic did not include the ability to save or load programs nor did it have any control over the screen.  I have extended it to include these features.

Tiny Basic, for those who may not know, came into being in the mid 1970’s for use in very low power microcomputers. Flavors of Tiny Basic were available for wide range of chips (some of which even had the language burned into them) from the Intel 4004 (quite a challenge since its entire address range is 16k) to the Z80 and oddball chips like the RCA 1802, Signetics 2650 and the National Semiconductor SC/MP.  The TRS-80 Model 1 even came with an enhanced version (4k) of the language.  Most implementations used about 2k of RAM for the entire package: line editor and interpreter. Some of these flavors even introduced some advanced constructs that are common today, but not then. Most of them had the ability to interact, at some level, with the hardware that they ran on, doing things like turning relays on or off or collecting data from some kind of sensor.

IMG_2158Back to our XGS.

I examined the Nanode Tiny Basic source and began removing the Arduino/Nanode specific code. Next, I figured out all of the places in code I would need to add the XGS specific stuff. My biggest headache was trying to debug the keyboard code while, unknowingly, having a faulty keyboard. That was fun.  Getting the timing correct, between reading the keyboard and displaying text on screen was the next biggest issue.  Once I got those issues resolved, I began adding my enhancements.

Before discussing the enhancements, lets talk about what was already there.  Standard features include:

Commands:

  • LIST
  • LOAD
  • NEW
  • RUN
  • SAVE
  • FILES

Functions:

  • PEEK(address)
  • ABS(number)
  • RND(dummy)

New:

  • LOCATE x,y – puts cursor at location x,y on screen
  • ? short for PRINT
  • MEM – displays free memory
  • PLAY x-plays a sound (PLAY 0 turns it off)
  • CLS – clears the screen
  • FILES – displays a list of files on the SD card

Other:

  • Integer math
  • 26 integer variables
  • No strings
  • Line Numbers, no textual labels
  • 20 characters by 16 lines
  • Old School

Statements:

  • NEXT
  • LET
  • IF
  • GOTO
  • GOSUB
  • RETURN
  • REM
  • FOR
  • INPUT
  • PRINT or ?
  • POKE* (not fully implemented)
  • STOP
  • BYE
  • SLEEP
  • CLS
  • LOCATE
  • MEM
  • PLAY
  • FILES

Operators:

  • +, –, *, /
  • <,>,<>,=
  • (,)
  • comma, ‘, “”
  • colon for multi-statement

 

The standard stuff, like PRINT, GOTO and such are implemented straightforwardly.  In this dialect, however, the only abbreviation that is allowed is the question mark, short for PRINT. I may, at some point, add the other shortcuts (PR for PRint, INput, etc.) but, for now, I am concentrating on core functionality.

As memory is at a premium, I removed code from the Graphics driver that was not necessary. This included all of the bitmap and tile graphics code, except for code that was necessary for the text console handling, clipping code and the initialization code. The other API’s are stock from Nurve or Microchip (the SD file handling API.)

I also developed a new font, using the C64 font supplied by Nurve (the old Commodore 64 font was too fat, but, now I have too much spacing between characters, I’ll add it to my to do list.)

Currently, the interpreter still has a few bugs, chiefly the line editor is wonky and randomly will reset the board.  The SAVE code is also buggy.  Once these bugs are squashed, I plan to do some code optimizing.  Fortunately, the originally code base was pretty tight, so most of the optimizing will be from my own sloppy coding.

Stay tuned for more.

Sample ArdBasic ‘guess my number’ code:

100 REM ** Sample ArdBasic Code
110 CLS
120 LOCATE 6,10
130 PRINT "GUESS THE NUMBER"
140 N=RND(1)
150 C=1
160 PRINT "GUESS #",C
170 PRINT "ENTER YOUR GUESS:",
180 INPUT G
190 IF G=N GOTO 250
200 PRINT "SORRY, TOO ",
210 IF G>N PRINT "HIGH": PLAY 9
220 IF G<N PRINT "LOW": PLAY 2
230 C=C+1:PLAY 0
240 GOTO 160
250 PRINT "YOU GUESSED MY NUMBER!"

To-Do:

  • Fix bugs
  • Add ability to chain programs
  • Add file deletion
  • Add rudimentary text files to the language
  • Font
  • Code optimizing
  • Graphics?

For more information:

3 thoughts on “Write your own BASIC programs on your XGS PIC

    1. That is just another version. I found it after I started the porting process. I took a look and incorporated a few things, but have been doing mostly my own mods. It is functionally complete, but I am having a problem with the LOAD command/statement.

  1. Sorry, that didn’t really answer your question. The only real difference, I think, is the ‘plus’ version was more complete. Both versions are derived from Mike Field’s port. As I said, I found the Nanode version first and it was easily adaptable to the PIC.

Leave a comment