Return to Basic

My first encounter with a real computer was an HP 9830 series desktop mini computer, sometimes round about 1978. It played a game called 23 matches (a variant of NIM) and it beat me. It beat me the 2nd time too, then I started to think… Then I beat it, then the teacher showed me the program. That looks easy, I thought…

So many (35+) years ago I learned to write computer programmes. I started in BASIC on that HP 9800 desktop machine, then via 110 baud dial-up on a thundering old Teletype ASR-33 to the local computing centre (Moray House, Edinburgh), then Apple ][, BBC Micro, etc., etc., etc....

However in a fit of nostalgia, I decided to resurrect some of my old BASIC programs of that era (late 1970s, early 80s) and so I bought an Apple ][ europlus for that authentic experience then decided to write a BASIC interpreter - (How hard can it be?), so I did, and it’s called Return to BASIC – or RTB.

At the time I really wasn’t thinking of what to do with it, then this Raspberry Pi thing started to gather interest, so I decided to order one and see if it would port to their Debian image running in QEMU. And it did. It was as simple as typing ‘make’. That’s the beauty of Linux (and Unix) code – with a few small limitations, everything “just works”.

So now what… I think BASIC is fun and quick – it’s not a “real-world” programming language, and I don’t intend it to be that either, but as something to get some nice graphics going, or as an easy way to interface to hardware like an Arduino, then it’s great!

RTB is a modern BASIC – it has while/until looping constructs and named functions and procedures which can have local variables and be called recursively. It has  simplified colour scheme (or you can use all 24-bits if you like), and the usual point, line and shape drawing primitives as well as some nice turtle graphics commands.

 


Comments

Return to Basic — 75 Comments

  1. I have finally got my PI ordered and would love to use a suitable BASIC with it – and yours will fit the bill with its graphics support. For me personally, I want to interface DS1631 I2C chips and speak over the serial port to my Atmel processors. If your code can allow one to use the GPIO, Serial port, I2C ports and to write console based apps – then its the BASIC for me! I will be happy to run a few tests of your software on mine if you need someone to test the software!
    Dicky

    • There are currently 2 different I2C drivers (to my knowledge!) for the Pi – as soon as one of these proves the more popular, I’ll include uspport into my BASIC for it (or I may write my own, who knows!). General access to the GPIO is possible right now and you can fiddle with all 17 pins for general purpose input/output – one of them can be programmed for PWM too. The down-side right now is that BASIC needs to be run as root – hopefulyl soon a better driver will emerge (although I’m considering writing one).
      Keep an eye on this page as I hope to have a release, both source and binarys in the next week.
      As for the serial access to Atemel processors – I have a little remote control protocol that my BASIC can use to talk directly too them. See the DRC section of this site for more details, but in essence, a program running can access the Atmel IO pins using the Arduino “Wiring” style commands and those same commands will access the on-board GPIO pins too.
      -Gordon

  2. Gordon,
    You have some great stuff here on your projects page, thanks for sharing. I am already playing with wiringPI, and although I haven’t done anything serious yet I am having lots of fun.
    I’m looking around for the link to the RTB code but I am not seeing one. I would like to play with it and don’t mind a few bugs. I also have fond memories of simpler days programming in basic and would love to give your RTB a try.

    Thanks, Greg R

    • Have a look at: http://unicorn.drogon.net/rtb/ you’ll find some executables and demo programs there. I’ve just not had enough time to fix a few remaining bugs and package it up though.
      If writing some program of your own, note that there is a bug in the parser in that it won’t interpret unary minuses correctly, so a = -b won’t work, but a = 0 – b will work, and make sure youe type a space before the minus sign.
      hoping to have time in the next few weeks to finally put a release together though
      -Gordon

  3. Very very interested in Basic !

    and on the Pi it would be fantastic especiallu if it had code to controll the GPIO !

    A very excited Tim !

    : )))))))))))

    Tim

  4. Seems great work. Of course we have XBasic but RTB seems to be much more compact.

    As this is still developing program, would it be impossible to modify the syntax to be more like RMB (Rocky Mountain Basic)? After all, you used 9836 in the past. Old manuals which describe the syntax and semantics are available at the HP museum site so it should be doable.

    RMB had quite well thought IO so it should be especially suitable for Raspi. If implemented wisely, it would be possible an even practical to hide even remoted devices behind SPI or I2C in BASIC libraries.

    /alo

    • I’ll have a look at the RMB stuff. Just been using SPI and I2C in the Pi too – not sure they can really map easilly to the constructs avalable from BASIC, but I’ll have a look.
      -Gordon

  5. I too am interested in running BASIC on my R-Pi. I have loaded bwbasic and it seems to be running OK so far. Like Tim I am mainly interested in using the GPIO. I have had a little advice from a couple of folks who are familiar with LINUX/UNIX and I am closing in on some simple UNIX commands to get the bwbasic to run in root and to access the GPIO pins so that I can read and write to them in BASIC. I will let you know as soon as I can get it working.

  6. I sure that your basic programming language is very good, but there are a lot others out there that will work fine on Raspberry Pi and some are easier to use.

    Put RISC OS on another SD card and you got BBC Basic on your Raspberry Pi
    there tons of information and examples on this basic on the internet
    because it been around for years and lots of people have programmed it before.

    • You’re right about there being many – even a version of BBC Basic for Linux called Brandy. No need to even think about RISCOS.

      But RTB is mine and I quite like it! I didn’t even write it with the Pi in-mind either – it just happens that the Pi is Linux and I oeiginally wrote it under Linux (although it works on a Mac too and will run under MS Win shortly).

      I don’t know what your point is though.

      I wrote RTB because it was something I wanted to do, and I’m happy to share it with you and everyone else too.

      Cheers,

      -Gordon

  7. I am having a problem with the line
    arduino = SOPEN (“/dev/ttyACM0″, 115200)

    I get an error message Incorrect argument count for SOPEN (expected 1, got 2)

    Am I doing something wrong ?

    • No, you’ve hit a bug, sorry.
      Long story short – I updated the mechanism I use to count built-in procesdure arguments and goofed for the serial functions.

      Did you build from source or download the binary?

      If source, I can give a patch, or if binary I can re-compile it for you…

      -Gordon

  8. The supplied binary worked. SOPEN now works as described in the manual.
    I was able to read and write data to my usb to i2c interface which I was using to display time (hours and minutes) on a adafruit 7 segment LED and read a DS1621 thermometer and display it on a SAA1064 4 by 7 segment LED a very old Elektor magazine project.

    Adding I2C commands directly to RTB would be useful. I know the PI is very different from an arduino but could arduino type commands be added to RTB to access ports and hardware ?

    Thanks Trevor

    • Shouldn’t be hard now that wiringPi supports both SPI and I2C. slight lack of proper binary operators though, so it’s never going to be as good as C though, but I’ll look into it.

      -Gordon

    • colours can be set with the normal text colour commands, so for yellow text on a green background, then

      tcolour = yellow
      bcolour = green
      cls

      However if your program subsequently changes it then it will remain changed.

      Maybe I need a command-mode set of colours and a run-mode set…

      the font size can only be changed at launch time – and then only to double-size

      rtb -l

      will run it in double-size. (I did that mainly to make it readable on big screens, or on TVs when using composite)

      -Gordon

  9. Thanks Gordon.
    Just what the doctor ordered, or should I say optician!!!!
    Double size is OK but don’t get a lot of lines of code on the edit / run window. Is it possible to adjust the size of the run window while launching rtb?
    Many thanks for your speedy reply.
    Mike

  10. Hi Gordon

    Just got hold of a Raspberry Pi and been playing with RTBm it is very good,
    I started out learning to program on TRS80′s in the early 1980′s running level 2 basic I think it was!

    I was just wondering if you had any more thoughts on implementing i2c communication into RTB?
    I have had a play with the i2c stuff using python, but I am way to old to learn a new language.

    Thanks for RTB Gordon.

    • Hi,

      Glad you’re finding it ok!

      It’s not impossible to add I2C operations into RTB, (and possibly even SPI) so I might look at it. One issue though is lack of a proper integer data type in RTB but that’s not a big show stopper. I’ll give it some thought.

      -Gordon

      • Thanks Gordon, I hadn’t thought of SPI but I think that it could be useful to have too!.
        I will keep playing with RTB and keep and eye on your page for any updates regarding SPI or I2C.

        - Tony

  11. wow first multiple lcd’s now basic for raspberry pi?
    very cool… I’m going to download this today and get started…
    I too started with basic way back when, infact I had an Acorn Atom.
    (in Britain that may not be such a big deal, but I am in Canada and there were not too many of them around… :) ) at any rate, basic here I come :)

    cheers!

  12. What a huge work ! Terribly impressed. I’ll try it asap. I worked with Basic decades ago. I still program in assembler on µControllers, it’s light, and long as well.
    Congratulations

  13. Gordon,

    I couldn’t be more pleased. RTB allowed me to build a web server with the RPI. By using RTB to interrogate a serial device on a regular basis, I can provide real-time wind speed and temperature data via a web browser. For anyone that cares the very short RTB code is:

    4 CYCLE
    5 winds = OPENUP (“1record.txt”)
    10 breezeSP = SOPEN (“/dev/ttyAMA0″, 9600)
    20 SPUT$ (breezeSP, “t”)
    25 SPUT$ (breezeSP, CHR$ (13))
    26 junk$ = SGET$ (breezeSP)
    27 junk$ = SGET$ (breezeSP)
    28 junk$ = SGET$ (breezeSP)
    30 FOR i = 1 TO 37
    40 key$ = SGET$ (breezeSP)
    42 WAIT (.25)
    45 PRINT# winds, key$;
    60 NEXT i
    65 PRINT# winds, CHR$ (13);
    70 CLOSE (winds)
    75 SCLOSE (breezeSP)
    77 REPEAT
    1000 END

    The question I have is not exactly related to RTB. I am using a headless RPI, connecting to the RPI via WiFi with SSH. After login, I launch RTB, and run the code above. This prompts the wind and temperature data logger for a current reading and saves the text string to a file (1record.txt) in the directory /var/www. The php code reads this text file, and displays this via a browser. I need the RPI, on power up, with no intervention, to launch RTB and execute my code. Thanks for RTB, and thanks for your help.

    Bill

    • To start RTB with a program, you can give it the name on the command-line, so for example at the end of /etc/rc.local:

      cd /var/www
      /usr/local/bin/rtb myprog.rtb

      another thing you might be interested in is the SAVENN command – saves a program without line numbers – then you can edit it outside the RTB environment. You can also save it (again with savenn), then edit it and add at the top:

      #!/usr/local/bin/rtb

      then make it executable: chmod +x myprog.rtb then just execute it like any other command.

      Remember to use full pathnames for programs you start at boot time though.

      -Gordon

  14. Gordon,

    Thanks for the speedy reply. One thing I forgot to ask. I have been reluctant to stop my SSH session with the RPI. I have a sense that I would be unable to log in again. I have an Xwindow
    up, it’s currently blank, and there is no prompt in the terminal window. I can escape the CYCLE – REPEAT loop with the ESC key, now but… So if I shut down my terminal, am guseeing I would have to do a hard reset either with a power cycle or via the P6 jumpers. Am I missing something?

    Bill

    BTW, it will take me a while to come up to speed and digest your previous instructions.

  15. Hello!

    Do you think it’s possible to implement in the future playing wav, mp3, ogg or others in RTB?
    What about implementing in RTB display picture files bmp, gif, jpg or others?
    Thank you in advance!

    Ciprian

    • Playing simple tones is in the pipeline – not sure about tunes though. There is currently a screen-save function which saves it as a BMP file, but loading them in? Hm. You can load in BMP files as sprites though – with some checking I may be able to get it to load PNG, JPG, etc. too. I’ll put it on the wish-list!

      -Gordon

  16. Gordon,
    In a reply above you said that it would be possible to include SPI routines. This would greatly add to the RPi’s capability in being used as a controller.
    Is there any progress on this? I downloaded the January 2013 manual revision, but could find no mention of SPI.
    I would be happy to test such functions.
    Great piece of work, btw.
    Roger

    • Currently looking at a (long!) featrure list of things to add into RTB and SPI & I2C is on the list, but it’s been a while to get round to it.

      -Gordon

  17. Hi!

    I;m trying to use a DS 18B20 thermometer in RTB.
    I followed intructions from here:
    http://www.raspberrypi-spy.co.uk/2013/03/raspberry-pi-1-wire-digital-thermometer-sensor/
    and it;s working fine.
    But when I try to use the content of the w1_slave in the rtb it displays
    -1
    I;ve used the following code lines:
    10 myFile = OPENUP (“/sys/bus/w1/devices/10-000801a51b5f/w1_slave”)
    20 PRINT myFile
    30 STOP

    Where do I wrong?
    P.S. In the line code number 10 I wrote OPEN and it displays automatically OPENUP

    • -1 is the generic error code.
      It’s probably permissions.
      So firstly try using openin – that’ll open it read only, it’s possible that trying to open it as openup (the default) gives you a permissions denied.

      If that still doesn’t work, then from the shell command, try ls -l /sys/bus/w1/devices/10-000801a51b5f/w1_slave and check the ownership and permissions. You need it to be at least world read access – 664, or rw-rw-r ie. at least 3 r’s. You may need to change the ownership or mode before running RTB. sudo chmod 644 ….

      -Gordon

  18. Hello Gordon.
    I’m running rtb (1.0.4) on my Pi and very good it is too. As also with WiringPi.
    One small prob: – when I renumber my rtb progs, restore statements with line numbers are not renumbered. Otherwise everything seems fine!

  19. Hi Gordon

    Thanks for a great job with the RTB :-)

    Why can I not get this exampel from the manual to work?

    100 myFile = OPEN (“testfile.dat”)
    110 WHILE NOT EOF (myFile)
    120 INPUT# myFile, line$
    130 PRINT line$
    140 CLOSE (myFile)

    I get this error:
    XXX Syntax error trying to parse: … $
    Program load aborted at line 120

    Best regards Erik

  20. Can you create a function/instruction for sprites to show/unshow a sprite without loading/unloading the bmp image every time when it’s displayed?

    Thank you in advance!
    Ciprian

  21. I have just downloaded and compiled rtb for use on my laptop running OpenSUSE.
    I was a bit thrown by the instructions to compile:
    cmake -G “Unix Makefiles”

    I ended up typing: cmake -G src
    and that seemed to work OK.

    I can’t find any mention of arguments to rtb in the online .pdf file.
    I typed: rtb -?
    and got a usage message but no hints as to what the various args do.
    Some I can guess but I tried -h (think help) and got switched to a black
    screen. After a brief panic, I typed exit (which didn’t display) and managed
    to get back to normal!

    Otherwise, nice bit of work!!
    Bill (who wrote an incremental compiler for
    Basic on a mainframe back in the 60′s)

    • Personally, I’m not 100% sure about cmake myself and have reverted back to my old Makefile for some recent development, however it worked to get a port going on a Mac at one point…

      And it looks like the cmake install isn’t doing quite the right thing. If you

      cd rtb/src
      man ./rtb.1

      you’ll get a standard man page with the command-line args. Running ‘rtb’ ought to pop up a window of 640×480 under X, but from the console you can select a variety of modes – try rtb -F -D and it will list the full-screen modes avalable.

      Cheers,

      -Gordon

  22. Gordon,

    Seems like a lot of us old farts are strongly attracted to your RTB! And why not? Just learn a few more tricks in a language we already know! Thanks for the good (and I’m sure hard) work.

    Is there any support for the Raspberry Pi camera board? Once they become available again I plan to do some serious playing with it, if that’s not an oxymoron.

    Also, do you have plans to add the capacity to control other Pi programs from within RTB?

    Thanks, Kevin

    • Glad you’re having fun with it! I have a new release coming out soon that has a built-in screen editor (no line numbers), and better GPIO support – support for extra boards like Piface, etc.

      I don’t have a Pi Camera – not even got a clue about Linux video either – what would you want to do with it in RTB? It won’t be anywhere near fast enough to process live video – take a snap maybe?

      And when you say control other programs – give me an example of what you mean here?

      -Gordon

  23. Gordon,

    WordPress is giving us problems. For the second time it returned an error (could not retrieve password) when I attempted to post here. If this one does not go through I’ll PM you.

    I don’t have a camera board either, but I plan to buy one as soon as they are back in stock. If I understand correctly it feeds directly to the GPU in the Pi, bypassing (mostly) the clunky ARM CPU. Obviously the mechanics are beyond me but I’m hoping that means all RTB would have to do is start a video recording to a given file, then call some other program such as omxplayer to play it. That is what I mean by “controlling” other programs. Can RTB do that? I didn’t see anything in the manual about it.

    Thanks, Kevin

    • I prefer email… Not sure about wordpress issues though – I own & run the server it’s on – it’s not setup for logins (other than me), so not sure why you get password issues…

      However – yes, the camera connects to the GPU – the GPU decodes the camera and turns it into a stream of H264 or static images. the images at 25 frames/sec would still be too fast for RTB to process, but grabbing a static frame might be fesable.

      Calling external programs – possible – the real issue is that the RTB environment doesn’t provide a “terminal” for them to run in, although it can all be arranged, it’s not always simple. I’ll have a look at implementing a simple version of system() though – but capturing output is the tricky bit!

      -Gordon

      • Perhaps simply save to a file? Wouldn’t give instant gratification but might be just the thing for, say, a surveillance system.

        I have finally (by brute force) reclaimed the desk space my Pi sits on and have had a shot at playing with RTB. Did you put the bug in clock.rtb on purpose to see if we’re paying attention? It returns a “next without for” error. Haven’t had time to look at it yet.

        All in all, great work, and greatly appreciated!

        Thanks, Kevin

        • It’s not a bug…

          Some time back I was posting in a thread on the RPI forums about BASIC and I was whinged at for not including “traditional” for/next statements – so I spent an afternoon, adding in it (and some other useless loop constructs) – to the detriment of some existing programs that used next as a variable name. After spending time doing it not one of the whingers even bothered to look, so I have subsequently taken them out because that’s not how I want my BASIC to look.

          so sorry for that – change the ‘next’ variables into another variable name for now – the next time I do a release (complete with full-screen editor will be soon and will have reverted back to the “proper” RTB behaviour.

          -Gordon

          • Doesn’t sound like you should be the one apologizing. Again much thanks for all the great work!

            –Kevin

    • Did the cmakefile cause any issues? I’ve actually removed it and gone back to a classic Makefile for the current builds I’m working on (not released yet though).

      And that’s for pointing that site out – I’d no idea about it!

      Cheers,

      -Gordon

      • After installing cmake for Haiku it was no problem at all. But a classic makefile might be a better idea (esp. for compiling on Windows using MinGW).
        Best wishes,
        Markus

      • Hi Gordon,

        Going back to standard makefiles is good to hear. I’m trying to port your BASIC to Android Linux native using C4droid and the SDL plug-in. I have ask the C4droid other for help getting around the cmake issue on Android.

        John

  24. Hi, and thanks!!
    This is really fun, i haven’t programmed in Basic for many years now and Return To Basic has given me a fun sommer :)

    There are however one thing thats missing, possebility to use “WiringPI” with RTB!
    I have a lot of small projects where the PI running RTB programs can make good use as electronic measurement devices in my workshop.

    RTB give me a fast development for small tasks and it would be perfect to make use of the PI GPIO I2C direct from RTB.
    Do you have any plans on implementing thast possebility in RTB??

    Superb work on RTB and Thank you one more time for a fun summer :)
    Regards,
    Niklas

    • wiringPi is built into RTB. Just use pimMode (pin, 1) (1 is output, 0 is input), then digitalWrite(pin, 1) or x = digitalReal (pin)

      I’m considering I2C too, so keep an eye on the site.

      -Gordon

          • RTB will always be free, but it will lag slightly behind the FUZE BASIC release in terms of features, etc. So right now, FUZE has a screen based editor, sprite and mouse handling and some internal speed-ups and restructuring which RTB currently lacks, but the plan is to eventually back-port these updates into RTB.

            -Gordon

  25. Hi Gordon,

    It is really fun to play with rtb and GPIO. This was a great idea to implement WiringPi into Basic. Thanks!
    I just tried to convert text into Morse-code and it works very well with a LED and DigitalWrite command.
    Next I tried to use PwmWrite – to listen to my Morse lessons. (of course with PinMode (1, 2)). But during execution of the program I get the error “Undimensioned array” at the line were the PwmWrite is in.

    Do you have any idea where I am wrong?
    Thank you very much.
    Best regards
    Martin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *