• AVR Freaks

Hot!Minibasic for Pic32

Page: << < ..678910.. > >> Showing page 6 of 11
Author
profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/13 20:35:58 (permalink)
0
Just an update on what I'm doing...

I ran into a problem with newlib, in that newlib has its own malloc(), and I prefer to use my own stripped down version of malloc.  Unfortunately, a lot of newlib functions seems to be fairly "heavyweight" in that they require malloc AND file descriptors, even for things such as double to string and string to double conversion!  So I wrote my own mydtoa (double to ascii) and mystrtod (string to double) functions that seem to work ok.  These are in term.c and might be useful for the PIC32 minibasic, if portable versions of these library functions are difficult to come by.  I do not claim my fp routines are strictly IEEE 754 compliant, but they work ok as far as I can tell.

The version I am including is a re-entrant minibasic.  To enable/disable it, #define/#undef MULTIPLE in basic.h.  Enabling it adds about 500 bytes to the code size, and of course the pointer must be carried on the stack.  I also had to make myatoi and mystrtol re-entrant, which added a tiny bit of code size.

Dan

DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/14 01:33:32 (permalink)
0
Xiaofan, nothing you report is happening to me, now (luckily Smile)
I may agree on that thing about "removing unused sections" (I saw it on the forum as well yesterday) - but the Project as I uploaded 2 days ago compiles correctly/links and WITHOUT those warnings.

Have you also considered the "signed char by default" issue?

GENOVA :D :D ! GODO
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/14 01:39:12 (permalink)
0
Thanks for the updates Dan. I'm still stick to SRAMalloc for C18 and built-in malloc for C32 at the moment.

One different thing: how are you dealing with integer/real variables in MiniBasic? They're all just Reals, aren't they?
I'd say that "printing" them all as 6-decimal-digits would be just crazy... that's why in my code I implemented the trick of "auto-truncate" if no decimals are present, but it is not that smart.
(apart from speed issues, of course)
Would it be time to implement Integer variables? (a% like in MS Basic?) Or implement "PRINT USING" ?

GENOVA :D :D ! GODO
profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/14 07:37:09 (permalink)
0
My version of minibasic has a define in the basic.h file called "INTONLY".

It INTONLY is defined, then numbers are integers, otherwise they are doubles.  I am targeting very small systems (32k flash/8k SRAM), so usually I have INTONLY turned on so I have no floating point.

My floating point routine mydtoa estimates a floating point accuracy and will not display decimal digits less that contribute less than that accuracy.  It also has a "rounding" method, but I am not sure if that works perfectly, so I am not confident I am achieving accurate display of the least significant digit. 

Anyways, take a look at my code (its in term.c), and you can see if it helps you.

Dan

DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/14 08:30:22 (permalink)
0
Yeah, I did analyze your code Smile

I know about INTONLY, and actually I'm not using it at the moment (no real app by now)

The "smart rounding" is nice, I thought about it as well: I'd say it is the right way to go. Or having Integer AND Float types...

Thank you Dan

GENOVA :D :D ! GODO
profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/15 08:16:21 (permalink)
0
Just some more feedback from you folks.

So I've modified my Minibasic to take out boolean expressions.  I figured that, like C, a zero/nonzero number can suffice for a boolean expression.  Also, it would have some nice benefits as you can do expressions like

x = (y=3) AND (z=4)

which will be stored as x as zero or one.

However, there are some quirky side effects, namely due to strings.  For example, lets say one does

A$="HELLO"
PRINT A$>"GOODBYE"

In my code one gets a syntax error, because it interprets A$ as a string, and then tries to interpret the rest of the line > 3 and it doesn't make sense.  On the other hand:

PRINT (A$>"GOODBYE")

works fine, because the parenthesis tell it that the following is a numerical expression.

Do you think this is too screwy (having to specifically denote when a numeric return from a string evaluation is needed) because the parser currently can not "read ahead" and figure out if the overall expresssion evaluates to a string or number?

The benefit of leaving it this way is that expressions like AND/OR/NOT could be used in numerical evaluation, which might be convenient, e.g.

GOTO 100+(((x = 5) AND (y=2))*50)

instead of

IF (x=5) AND (y=2) THEN 150 ELSE 100

Comments?

Dan

DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/15 13:12:06 (permalink)
0
Good point Dan, I'm in a hurry now, I'll be back Smile

GENOVA :D :D ! GODO
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/15 17:52:58 (permalink)
0
I was wondering...
what about operators' priority?

I mean: in this case

PRINT A$>"GOODBYE"


the whole expressione should be parsed *before* control is released to the PRINT statement: thus, it could come out as a "boolean" value, and then the PRINT itself should let the 0 or 1 out...

I'm not sure, I can't check this now.

GENOVA :D :D ! GODO
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/16 10:50:38 (permalink)
0
Today's news:
  • Added IIF for conditional expression, such as C's "? :" ternary operator
  • Added DO..LOOP WHILE or UNTIL and DO WHILE or UNTIL .. LOOP, for flow control
  • Added (preliminary) group of Graphic Functions to be implemented for some major LCD controllers
  • Added/extended STATUS(n), to return Power/On and Reset Status flags, WakeUp Status Flags, and (more than one) USART Error checking
  • Added ON IRQ command to handle Interrupts (yet to be defined in details) (thanks HBBR Basic)
  • Added some other preliminary functions
  • Implemented the "Smart Number of Decimals" PRINT, i.e. only significative number of decimals are printed (as from Dan's idea).
http://www.pic32.org/minibasic/
post edited by DarioG - 2007/12/16 16:22:33

GENOVA :D :D ! GODO
barbiani
Super Member
  • Total Posts : 246
  • Reward points : 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/16 11:07:22 (permalink)
0
Hi DarioG,
 
How well flow control constructs mix with numbered lines?
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/16 11:12:42 (permalink)
0
Oh, well: you have to keep the line numbers. But the DO..LOOP helps creating better constructs than FOR..NEXT and GOTO.

I'd call it "portability" or "clear code": Basic does not need (i.e. is born without them in mind) those "high level" flow control structs, and can do it all with GOTOs and GOSUB. But if you can use DO etc., you'll probably have a chance to write a clearer code.

GENOVA :D :D ! GODO
profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/16 12:07:53 (permalink)
0
Code size is not everything but...

Dario, how much flash does your version currently take up?  I have been slimming mine down and currently I am at 15k for the non-editor version and 22k for the editor version.  I have written my code so that it does not require newlib, there are now portable (but not necessarily optimal) versions of the C library functions that are lightweight.

My version is also modified so that booleans and ints are the same, so that conditional and numeric expressions are one and the same.  To do this, I restructured the parser (expr() ) to be a five-level recursive descent parser.

I just don't want to bloat the code size too much.  My version also avoids using longs (it uses them only in a few places where 32-bit computation is needed to prevent roll-over).

Dan

DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/16 15:46:38 (permalink)
0
Well, 27K unoptimized and 21.8K optimized on C18, plus some 3K RAM.

13.5K optimized (plus 4.5K RAM) on C32.

(no editor of course by now, and base malloc in use)

I took a look at the issue you pointed out (about BOOL expr) and indeed it makes sense - so I'm planning on implementing it as well. I also optimized ints to char etc. where possible.

Tomorrow, as I find some time, I'd like to implement the "integer" type and do some restruct on the Variable and Expression sections.

GENOVA :D :D ! GODO
profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/17 06:36:56 (permalink)
0
Dario,

Hey I was looking at your Minibasic-PIC32 code and I like the way you do the DO/LOOP construct.

Just a few questions:

What if you do something goofy like:

J=0
FOR I=1 TO 10
DO WHILE J>10
PRINT "I=",I," J=",J
NEXT I
J=J+1
LOOP

will it just crash with some sort of error (like NEXT without FOR) when the FOR loop is done?

Also, is it ok if I use a similar idea in my code?  I was thinking of using REPEAT/UNTIL or WHILE/WEND, but the way you have done it seems simpler.

Another question, if you don't mind: how do you get your code size to be so small?  I am trying to keep mine using Thumb mode to be under 20k (which I have been successful at), but you have the additional problems of separating code/data because of the Harvard architecture (and all the pointer headaches).  The reason I am so concerned about size is that I plan to make my version of BASIC web-enabled.  I have ordered (and hopefully will receive soon) a ENC28J60-based ethernet transceiver which I will connect to the SPI of my microcontroller.  I want to make it so one can serve web pages where the HTML is generated in Minibasic, and the form data returned to Minibasic.  That way one could have a network-enabled data acquisition device.  To do this, I am using the uIP TCP/IP stack, which itself takes about 15k, so it will be a tight fit.  I think the combination of a LPC2119, ENC28J60, and Minibasic will be a cheap, very capable, and easily programmable data logging device.

Anyways, I will post online my latest, which includes my newlib-substitute functions, and my restructuring to combine the boolean and numeric types.  I am only concerned that my version uses a lot of stack levels, which may be unsuitable for PIC18, but is very helpful for consolidating code on the ARM where stack size and recursion depth is not such a limitation.  For example, every time one uses a ( in the conventional Minibasic one picks up two levels of recursion!  In mine it is four because the recursive descent parser now has four levels for order of operations (from lowest to highest priority):

AND OR

>= <= <> < > =

+ -

* / MOD & | ^

all other functions (INT, unary minus, etc.) and of course parenthesis, which jumps back to the AND/OR level.

Just a note if you are trying to put the bool/int consolidated code on the PIC18 with limited stack.

Dan

profdc9
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2007/12/06 18:54:13
  • Location: 0
  • Status: offline
RE: Minibasic for Pic32 2007/12/17 18:22:56 (permalink)
0
Dario,

Just a question about your DO...LOOP implementation.

Minibasic has no notion of program blocks.  Therefore if one uses GOTO or IF/THEN to exit a DO..LOOP, the dostack will not be cleared because the corresponding LOOP will never be executed.  I tried to implement it like this:

BSTATIC linenumbertype dodo(VOIDACCEPTSTATE)
{
  int boolex, answer;
  const char *savestring;
 
  answer = Stoken;
  match(PASSSTATE answer);
  if (answer == DO) {
    if (istokenmatch(WHILE)) {
        boolex = integer( expr(VOIDPASSSTATE ) ) != 0;
    } else if (istokenmatch(UNTIL)) {
        boolex = integer( expr(VOIDPASSSTATE ) ) == 0;
    } else {
        boolex = 1;
    }
    if (Sndos >= MAXDOSTACK) {
        seterror(PASSSTATE ERR_TOMANYDOS);
        return -1;
    }
    if (boolex) {
        Sdostack[Sndos++] = Slines[Scurline].no;
        return 0;
    }
  }
  savestring = Sstring;
  answer=0;
  while ((++Scurline) < Snlines)
  {
    Sstring = &Sscriptstart[Slines[Scurline].ofs];
    Serrorflag = 0;
    Stoken = gettoken(VOIDPASSSTATE );
    istokenmatch(PASSSTATE VALUE);
    if (istokenmatch(PASSSTATE DO)) {
        answer++;
    } else if (istokenmatch(PASSSTATE LOOP))
      {
         if (answer-- == 0) {
            answer = getnextline(VOIDPASSSTATE);
            Sstring = savestring;
            Stoken = gettoken(VOIDPASSSTATE );
            return answer ? answer : -1;
        }
     }
  }     
  seterror(PASSSTATE ERR_NOLOOP);
  return -1;
}

BSTATIC linenumbertype doloop(VOIDACCEPTSTATE)
{
  int boolex;
 
  match(PASSSTATE LOOP);
  if (istokenmatch(WHILE)) {
    boolex = integer( expr(VOIDPASSSTATE ) ) != 0;
  } else if (istokenmatch(UNTIL)) {
    boolex = integer( expr(VOIDPASSSTATE ) ) == 0;
  } else {
    boolex = 1;
  }
  if (Sndos > 0) {
    Sndos--;
    return boolex ? Sdostack[Sndos] : 0;
  }
  seterror(PASSSTATE ERR_NODO);
  return -1;
}

One of the problems I found with your implementation is that if you have nested DO..LOOPS, and the DO is going to skip over the loop, you need to count the number of inner loops (which is what I did here).  However, this assumes the user does not try anything screwy like having two LOOP commands and skipping over one or the other using an IF/THEN.     Also, like I mentioned earlier, if one GOTO or IF/THEN out of a DO..LOOP, then stack will never be decremented.  So one must make sure not to exit a LOOP using GOTO or IF/THEN.

It seems that implementing control loops using a stack is problematic unless one has a way of defining program blocks.  I suppose I could add a BEGIN...END block to Minibasic (so one could count BEGIN and END to figure out where the inner blocks are).  Is this getting away from the simplicity of BASIC?

Dan

DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/18 03:29:03 (permalink)
0
ORIGINAL: profdc9
What if you do something goofy like:

J=0
FOR I=1 TO 10
DO WHILE J>10
PRINT "I=",I," J=",J
NEXT I
J=J+1
LOOP

will it just crash with some sort of error (like NEXT without FOR) when the FOR loop is done?


Yes, of course Smile
The way I "saw" MiniBasic (and, in the end, my SkyBasic too, although I develped it in 1994), it was a simple environment with no real acurate error checking.
In the end, something similar to what used to be Old MSBasic in the 80s.
Of course, it can be improved: on larger platforms, preferably.


Also, is it ok if I use a similar idea in my code?  I was thinking of using REPEAT/UNTIL or WHILE/WEND, but the way you have done it seems simpler.


Yes, I was considering them as well, but then I remembered some such DO/LOOP things and believed that they could be more standard and powerful.


Another question, if you don't mind: how do you get your code size to be so small?  I am trying to keep mine using Thumb mode to be under 20k (which I have been successful at), but you have the additional problems of separating code/data because of the Harvard architecture (and all the pointer headaches). 


Hav you tried compiling mine/ours?
On 18F, it could be due to the "basic" implementation of SRAMalloc() ... it'd need some improvements (as I said above - I still want to take a look at your library).


The reason I am so concerned about size is that I plan to make my version of BASIC web-enabled.  I have ordered (and hopefully will receive soon) a ENC28J60-based ethernet transceiver which I will connect to the SPI of my microcontroller.  I want to make it so one can serve web pages where the HTML is generated in Minibasic, and the form data returned to Minibasic.  That way one could have a network-enabled data acquisition device.  To do this, I am using the uIP TCP/IP stack, which itself takes about 15k, so it will be a tight fit.  I think the combination of a LPC2119, ENC28J60, and Minibasic will be a cheap, very capable, and easily programmable data logging device.


BINGO Smile I'm considering it as well and so is Mike, I guess.
I'm still thinking about the overall "theory": shall we use PRINT #TCP_CHANNEL,"HTTP_Strings" or alike? Will it be powerful enough? Shall we need OPEN "TCP" as well, and how many protocols are we going to implement?
I'm happy to share my thought with you, Dan.
As for code size, we'll take a look at it...


Anyways, I will post online my latest, which includes my newlib-substitute functions, and my restructuring to combine the boolean and numeric types.  I am only concerned that my version uses a lot of stack levels, which may be unsuitable for PIC18, [...]
Just a note if you are trying to put the bool/int consolidated code on the PIC18 with limited stack.



Yeah, I hope I can work on it today. I read your previous post and am taking a look at your code soon.

Thank you Dan.

GENOVA :D :D ! GODO
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/18 03:33:58 (permalink)
0
ORIGINAL: profdc9
Just a question about your DO...LOOP implementation.

Minibasic has no notion of program blocks.  Therefore if one uses GOTO or IF/THEN to exit a DO..LOOP, the dostack will not be cleared because the corresponding LOOP will never be executed.  I tried to implement it like this:


Yes, good.


One of the problems I found with your implementation is that if you have nested DO..LOOPS, and the DO is going to skip over the loop, you need to count the number of inner loops (which is what I did here).  However, this assumes the user does not try anything screwy like having two LOOP commands and skipping over one or the other using an IF/THEN.     Also, like I mentioned earlier, if one GOTO or IF/THEN out of a DO..LOOP, then stack will never be decremented.  So one must make sure not to exit a LOOP using GOTO or IF/THEN.


That's true. My code was just the first version, what you point out is correct - actually it only happens with the "condition at beginning form" (DO WHILE.. LOOP)
Of course, jumping out of the blocks is bad... I don't know if it would be worth the effort to do such a check...


It seems that implementing control loops using a stack is problematic unless one has a way of defining program blocks.  I suppose I could add a BEGIN...END block to Minibasic (so one could count BEGIN and END to figure out where the inner blocks are).  Is this getting away from the simplicity of BASIC?


...I guess so! Smile
I've seen that other BASIC (such as the aforementioned HBRR) does have local vars, procedures by name etc.
I don't know: I'd use C for that. I'd keep MiniBasic small and easy.

Anyway, the BEGIN..END idea is not bad: Commodore 128 did get it in 1985, as an upgrade to "older" Basic.
But still, I think that "GOTO" is inherently a part of Basic...



GENOVA :D :D ! GODO
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/18 13:53:50 (permalink)
0
Today's news Smile
  • Added BEGIN/BEND keywords for future awareness of "Code Blocks" (see above)
  • Added support for Integer variables (such as "a%") - the only advantage by now is memory space (2bytes vs 4), but code is not faster since math operations are still done on double (=TODO) (except for FOR blocks) - This can be disabled via Conditional Compiling.
  • Restyled bitwise operators to use standard Basic AND, OR, XOR and not C's ones. "~" is still there Smile by now, altogether with NOT.
  • Rewritten Comparator Operations in a similar manner to what Dan wrote above: i.e. to give the chance to use Relational operators in formulas. I used a different approach, basically merging the "Bool" parts of code into the Standard operations (Dan your comments will be welcome): now PRINT a<3 works. TODO: strings are still to be handled, but should not be an issue.
  • Added a "Formula Too Complex" error which traps the current level of "recursiveness" in evaluating expression by checking Stack Pointer (see point above)
  • Restyled some error messages.
  • Of course, worked out and possibly solved the "skip inner DO..LOOP"s as pointed out above by Dan. Smile
post edited by DarioG - 2007/12/18 15:15:26

GENOVA :D :D ! GODO
bryan1
Starting Member
  • Total Posts : 58
  • Reward points : 0
  • Joined: 2004/07/25 16:24:15
  • Location: Adelaide Hills, Australia
  • Status: offline
RE: Minibasic for Pic32 2007/12/20 00:39:12 (permalink)
0
Hiya Guy's,
   <rant>
               I've been follwing this post and the pic32.org site for a couple of weeks now and I'm impressed. What Dario is doing porting minibasic to the the 18 series is great. Just one question with the avaliable basic programs available now like Oshonsoft,picbasic,swordfish etc. Will mini basic become an open source mainstream alternative to the good but very restrictive picaxe? I have noticed the guys are trying to emulate the good 'ol basic from another time, but with all the hard work going on wouldn't a open source basic basic interpretor for the 18f/pic32 that will leave the expensive basic compilers in their wake be a good idea.
</rant>
              
Cheers Bryan   
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
RE: Minibasic for Pic32 2007/12/20 01:06:57 (permalink)
0
Hi Bryan Smile
thanks for your words.

Well, your concern makes sense. I've been wondering about it as well.
I don't know much about other BASIC envirnoments you quote: I've taken a look here and there, and I have to say that I would probably not use a BASIC on a PIC (or other embedded platform/micro) since it tends to hide the hardware details and in the end is not suitable to deploy the whole power of such a device.
I can see though that a simple BASIC programming interface could make life much easier, when it comes to make some simple tests, simple programs and so. This is how and why I'm leaning towards the "good ol' Basic" style: we could probably have Procedure by names and Local variables... but if it comes to this, then I'd prefer using C - for my own programming.

Of course, this all has just started, so other directions can be taken, depending on the interest that this thing is going to achieve.

GENOVA :D :D ! GODO
Page: << < ..678910.. > >> Showing page 6 of 11
Jump to:
© 2020 APG vNext Commercial Version 4.5