BCX Basic - feature rich Basic programs up to 512k. Beta testers wanted

Announcing a new language <drumroll> - BCX Basic on the Propeller.

BCX is a free Basic to C converter that has been around for years and has a huge range of Basic language commands http://www.bcxgurus.com/help/index.html

Couple this with the Catalina C compiler and it is possible to write programs as small as 900 bytes, or as large as 512k (soon to be 32Mb).

A simple led flasher:
Do
  High(0)              ' bit 0 high (pin 1 on physical chip)
  Sleep(1000)          ' sleep this number of milliseconds
  Low(0)               ' bit 0 low
  Sleep(1000)          ' sleep
Loop

Or more complex code, using strings, huge string arrays, serial ports, the VGA display, Keyboard input, sd card file load/save and full floating point maths. You can even add inline C code into your program.

The simple idea was to just feed some code into BCX - but it turns out that BCX runs C99, and Catalina runs in C89, so there are multiple fixes behind the scenes. This is no problem for the user though, as compilation and download is still as simple as hitting the "Compile" button.

You can even save your program to an sd card and run it later using KyeDOS.

This is a beta release and there will be some instructions that don't work. This is a test program (which is messy, sorry!) which has many of the instructions tested so far:
' Big Basic for external memory on the Propeller using BCX Basic to C converter and Catalina

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
'#include <mathconst.h>
' mathconst.h upsets tinyc

$COMMAND
catalina -lcx -lm -x5 -M 128k -D DRACBLADE -D VGA_HIRES
' library cx, library maths (slow, doesn't use a cog), memory size, board, high res vga, mouse, keyboard
$COMMAND

call WhiteOnBlue
dim a as integer
dim i as integer
for a=1 to 3
    print "hello world";
    print a
next a
$COMMENT
' file test copied from bcx examples - files work - commented out for speed 
print "Opening file for output"
DIM MyFileHandle@ ' BCX Reserves @ for "C" FILE* data types
OPEN "new.txt" FOR OUTPUT  AS  FP1
MyFileHandle@ = FP1
FPRINT MyFileHandle@, "This is a test"
CLOSE FP1
print "Closing file"
$COMMENT

' ASC function 
DIM Mystring$ as string * 80 ' 80 characters
DIM RetVal%
Mystring$="A"
print Mystring$
RetVal% = ASC(Mystring$) 
PRINT RetVal%

'CHR$ function - opposite of ASC
a=65
Mystring$=chr$(a) ' declared in asc
print "Character should be A: ";Mystring$

'VAL function - converts string to double number. From the BCX help file
DIM StringNum$ as string * 20
DIM RetDub# 
StringNum$="3.1415927"
RetDub#=VAL(StringNum$)
print RetDub#

'STR$ function - opposite of VAL. Converts number to string
DIM RetStr$ as string * 20
DIM DoubleNumber#
DoubleNumber# = 1.23456789
RetStr$ = STR$(DoubleNumber#)
print RetStr$


' Test some inline C code - pass a value in myarray, C changes it, print the return value

dim myarray[5] as integer ' this can be much bigger if you like, eg 50000
myarray[1]=100
print myarray[1]
call TestCCode
print myarray[1]

' test some string functions

dim lineoftext1$ as string * 80 ' strings must end in $ otherwise compiler doesn't copy properly
dim lineoftext2$ as string * 80
lineoftext1$="Hello World" ' string name is case sensitive
lineoftext2$=left$(lineoftext1$,4) ' left characters
print "Left$ test ";lineoftext2$
lineoftext2$=mid$(lineoftext1$,5,4) ' mid characters
print "Mid$ test ";lineoftext2$
lineoftext2$=ucase$(lineoftext1$) ' upper case
print "Upper case test ";lineoftext2$
lineoftext2$=right$(lineoftext1$,3) ' right characters
print "Right$ test ";lineoftext2$
lineoftext1$="Hello"
lineoftext2$="World"
concat(lineoftext1$,lineoftext2$) ' add two strings - same as + or & in vb.net
print "String concat test ";lineoftext1$
lineoftext1$="ABCDEFG"
lineoftext2$=lcase$(lineoftext1$) ' lower case
print "Lower case test ";lineoftext2$
a=1000
lineoftext1$=hex$(a) ' hex$ working
print lineoftext1$
dim retval% ' hex2dec = two errors, DWORD (which I think can be changed to unsigned long) and CharLowerA which instr also is unhappy about
retval%=hex2dec("0xFFFF") ' reverse of hex$, can use 0x or &H syntax or even FFFF
print "Hex2Dec test ";retval%
lineoftext1$="12345abc6789"
lineoftext2$="abc"
a=instr(lineoftext1$,lineoftext2$,3,0) ' instr search for characters. Last value is needed, use dummy 0 (matches case)
print "Instr test, string is at position ";a
a=255
lineoftext1$=bin$(a) ' test binary
lineoftext1$=lpad$(lineoftext1$,32,48) ' test adding leading zeros with lpad so is always 32 char long
print "Binary value is ";lineoftext1$ ' test binary to strings and back again
lineoftext1$="0000000001010101"
print "Binary string to decimal ";Bin2dec(lineoftext1$) ' binary

lineoftext1$=string$(10,65)
print "String function ";lineoftext1$' n characters of ascii value m
lineoftext1$="   trim test    " ' trim off leading zeros, spaces etc
lineoftext1$=trim$(lineoftext1$)
print "Trim test:";lineoftext1$

' test a string array

dim stringarray[5] as string * 80 ' 80 characters
stringarray[1]="Hello"
stringarray[2]="World"
print stringarray[2]

' bitwise operators - there are many more of these but they all seem to work ok without code changes
a=1
a=a << 10
print "bitwise shift left";a
a=a >> 4
print "bitwise shift right";a
a=1
a=a and 0xFF
print "logical and";a
a=a or 0xFF
print "logical or";a
print "test starting with a binary string and shifting"; bin2dec("00000001") << 4


' test program control loops - FOR is already tested above
a=1
do
    print a
   a=a+1
loop until a=3
print "Do loop finished"

a=1
while a <3
    print a
    incr(a)' translates to a++ in C
wend
print "Wend loop finished"

a=5
select case a
    case 1
      print "1"
    case > 1 and < 6
      print "more than 1 and less than six"  
    case else
      print "something else"
end select

' test functions

  print addnumbers(5,6)
  
' test some maths with floating point numbers

dim number1 as double
number1=1.2345
print number1
print sin(number1)
print log(number1)






$CCODE
while(1); // loop instead of finishing
$CCODE

' and exitprocess does not work on the propeller. So don't use END for the moment

' subroutines and functions

function addnumbers(a as integer, b as integer)
  dim returnvalue as uint
  returnvalue = a + b
  function=returnvalue
end function

sub TestCCode
 $CCODE
  // declare the variables:
  int nNumber;
  int *pPointer;
  // now, give a value to them:
  nNumber = 15;
  pPointer = &nNumber;
  // print out the value of nNumber:
  printf("nNumber is equal to : %d\n", nNumber);
  // now, alter nNumber through pPointer:
  *pPointer = 25;
  // prove that nNumber has changed as a result of the above
  // by printing its value again:
  printf("nNumber is equal to : %d\n", nNumber);
  
  myarray[1]=110;
  
 $CCODE
end sub

Sub WhiteOnBlue()                       ' white text on blue background
$CCODE
  int i;
  for (i=0;i<40;i++)
    {
      t_setpos(0,0,i);                 // move cursor to next line
      t_color(0,0x08FC);               // RRGGBBxx eg dark blue background 00001000 white text 11111100
    }
$CCODE
End Sub

Because this tests so many functions, the C libraries fill up the memory so this entire program needs a board with external memory (Dracblade, C3, Gadget Gangster 32Mb etc). But it is certainly possible to test parts of this on a Demo board. The VGA and Keyboard fills up a demo board to about 25k leaving maybe 5k of code space.

The IDE also runs TinyC which means you can compile and run a test program in about half a second without having to download to a propeller. Very useful for testing subroutines.

To test, you will need:

1) Download and install Catalina to the default directory c:\program files\catalina http://catalina-c.sourceforge.net/
2) Download and install BCX to the default directory c:\program files\bcx http://www.bcxgurus.com/
3) (optional) Download and install TinyC to the default directory c:\program files\tcc http://bellard.org/tcc/ scroll down a little for the 'download' section.
4) Download and install the Catalina IDE "Compiled" zip below. This is a combined IDE for C and Basic (as well as a few other things, like a tile builder, movie maker and file transfer program).

5) Because this is a beta test, it may help to have the source code IDE running rather than the compiled version (there are still a few program shutdowns that I haven't put "catch ex as exceptions" into. So download the IDE source, and also vb.net. http://www.microsoft.com/express/Downloads/

This program is written in vb.net 2008 which is getting harder to find on the Microsoft website and there is a chance that vb.net 2010 won't run the 2008 version. If this happens to anyone I'll upgrade to 2010.

Suggestions, bugs, improvements all gratefully received.
Answers: 1) A quadcopter. 2) Very high. 3) The internet. 4) A lot. 5) No.

Comments

  • 7 Comments sorted by Votes Date Added
  • edited February 2011 Posts: 0Vote Up0Vote Down
    Congratulations, Dr_A!

    This will give those Basic4Android phonies a run for their money. Since you can run theoretically now run Basic on all 8 cogs, maybe you should call it Basic8Propeller!

    Ross.
    Catalina - a FREE ANSI C compiler for the Propeller.
    Download it from http://catalina-c.sourceforge.net/
  • edited February 2011 Posts: 5,431Vote Up0Vote Down
    Gr8 idea!

    Ross has just pointed out on the Catalina thread that if you have a keyboard and a VGA display and you compile so those go into cogs rather than staying in hub, that the simple keyboard/display program shrinks from 25k to only 6k of hub space.

    This means there is plenty of room for development before needing to look at external memory.
    Answers: 1) A quadcopter. 2) Very high. 3) The internet. 4) A lot. 5) No.
  • edited February 2011 Posts: 0Vote Up0Vote Down
    Dr_Acula,

    Congratulations on this great accomplishment!!!
  • edited February 2011 Posts: 0Vote Up0Vote Down
    Can you make a turn-key system that automatically boots into BCX BASIC on the Propeller chip? i.e. you power on the Propeller chip and it loads from a 32K or 64K eeprom..
  • edited February 2011 Posts: 0Vote Up0Vote Down
    Nice work Drac :)

    BTW I will be in your neck of the woods around Easter so maybe we can catch up?
    My Prop boards: CpuBlade, TriBlade, RamBlade, www.clusos.com
    Prop Tools (Index)
    Emulators (Index) ZiCog (Z80)
    Prop OS (also see Sphinx, PropDos, PropCmd)
  • edited February 2011 Posts: 5,431Vote Up0Vote Down
    Can you make a turn-key system that automatically boots into BCX BASIC on the Propeller chip? i.e. you power on the Propeller chip and it loads from a 32K or 64K eeprom..

    That is a great idea. Ok, I added that with an F11 shortcut for the C code, and a new button on the Basic screen. New code on post #1. Works for the miniature and LMM versions (I think there might be a solution for XMM programs too, where it loads the first bit off eeprom and the rest off an sd card). There is also more clever code in the Catalina manual that puts some of the library in a 64k eeprom and gives you more code space even without external memory. Many options here!

    There are many command line options, eg if you are using TV, then replace HIRES_VGA with HIRES_TV

    (See page 47 of the Catalina Reference Manual)

    @Cluso - yes, it would be great to catch up and brainstorm things.

    Experimenting with the "hmi" code - page 71 of the manual. The standard "hello world" is 25k, but with some fairly simple changes this can be shrunk down to about 4k.

    In C, use the HMI calls eg
    t_print(1,"Hello);  // print hello
    
    and in Basic just leave out the ";" and use ' for a comment instead of //
    t_print(1,"Hello) ' print hello
    
    ' BCX help file http://www.bcxgurus.com/help/index.html
    
    #include <stdio.h>
    #include <catalina_hmi.h>
    #include <ctype.h>
    
    $COMMAND
    catalina -lc -lm -D DEMO -D HIRES_VGA
    ' library c, library maths, demo board, hires vga, mouse, keyboard
    $COMMAND
    
    ' ********* main ***********
      Dim key As char
                    
      t_string(1,"Hello World. Hit a key:") 		' startup prompt
      t_char(1,13)					' carriage return	
      t_char(1,10)					' line feed
      key = (char)k_wait() 				' get the key
      t_string(1,"Key pressed=") 			' print a line
      t_char(1,key)  					' print the character
      t_char(1,13)					' new line
      t_char(1,10)					' line feed
      t_hex(1,key) 					' hex value
      Do
      Loop Until 0=1					' endless loop
    
    ' ******** end main *********
    
    Answers: 1) A quadcopter. 2) Very high. 3) The internet. 4) A lot. 5) No.
Sign In or Register to comment.